天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 51|回复: 0

曲线打断

[复制链接]
  • TA的每日心情
    开心
    前天 06:18
  • 签到天数: 49 天

    [LV.5]常住居民I

    185

    主题

    150

    回帖

    1695

    积分

    管理员

    积分
    1695
    发表于 2024-4-3 20:20:00 | 显示全部楼层 |阅读模式
    1. typedef std::vector<AcGePoint3d> PointList;
    2. const double kPi = 3.14159265358979323846;

    3. Acad::ErrorStatus MakeTempLWPoly (AcDb2dPolyline* tmp2dPoly, AcGePoint3dArray &pts, AcDbVoidPtrArray &curveSegments)
    4. {
    5.         Acad::ErrorStatus es;
    6.         AcDbObjectId curSpaceId = acdbCurDwg()->currentSpaceId();
    7.         AcDbBlockTableRecord *pBlkRec = NULL;
    8.         AcDbCurve* tmpCurve;
    9.         AcGePoint3dArray vertices;
    10.         AcGeDoubleArray bulges;
    11.         AcDb2dVertex* vert = NULL;
    12.         AcDbObjectId vId;
    13.         double c = 0;
    14.         AcDbObjectIterator *vIter;
    15.         double parametro;
    16.         vIter = tmp2dPoly->vertexIterator();
    17.         for (; !vIter->done(); vIter->step())
    18.         {
    19.                 vId = vIter->objectId();
    20.                 if (tmp2dPoly->openVertex(vert, vId, AcDb::kForRead) == Acad::eOk)
    21.                 {
    22.                         if (tmp2dPoly->getParamAtPoint(vert->position(), parametro) == Acad::eOk)
    23.                         {
    24.                                 c = vert->bulge();
    25.                                 if ((tmp2dPoly->normal()-AcGeVector3d::kZAxis).length() > 1E-6) c *= -1.0;
    26.                                 vertices.append(tmp2dPoly->vertexPosition(*vert));
    27.                                 bulges.append(c);
    28.                         }
    29.                         vert->close();
    30.                 }
    31.         }
    32.         if (tmp2dPoly->isClosed())
    33.         {
    34.                 if (vertices.first() != vertices.last())
    35.                 {
    36.                         vertices.append(vertices.first());
    37.                         bulges.append(bulges.first());
    38.                 }
    39.         }
    40.         delete vIter;

    41.         Adesk::Boolean closed;
    42.         if (tmp2dPoly->isClosed())
    43.         {
    44.                 closed = Adesk::kTrue;
    45.         }
    46.         else
    47.         {
    48.                 closed = Adesk::kFalse;
    49.         }

    50.         AcCmColor col = tmp2dPoly->color();
    51.         ACHAR* ltype = tmp2dPoly->linetype();

    52.         AcDbPolyline* pBreakLine = new AcDbPolyline(vertices.length());
    53.         for (int i=0; i < vertices.length(); i++)
    54.         {
    55.                 AcGePoint2d pt;
    56.                 pt.set(vertices.at(i).x,vertices.at(i).y);
    57.                 pBreakLine->addVertexAt(i,pt,bulges.at(i));
    58.         }

    59.         pBreakLine->setClosed(closed);
    60.         pBreakLine->setColor(col);
    61.         pBreakLine->setLinetype(ltype);
    62.         acutDelString(ltype);
    63.         vertices.removeAll();
    64.         bulges.removeAll();

    65.         es = pBreakLine->getSplitCurves(pts,curveSegments);
    66.         if (es == Acad::eOk)
    67.         {
    68.                 for (int i=0; i < curveSegments.length(); i++)
    69.                 {
    70.                         tmpCurve = static_cast<AcDbCurve*>(curveSegments[i]);
    71.                         if (tmpCurve)
    72.                         {
    73.                                 if (acdbOpenObject(pBlkRec,curSpaceId,AcDb::kForWrite) == Acad::eOk)
    74.                                 {
    75.                                         pBlkRec->appendAcDbEntity(tmpCurve);
    76.                                         pBlkRec->close();
    77.                                         tmpCurve->close();
    78.                                 }
    79.                         }
    80.                 }
    81.                 curveSegments.removeAll();
    82.         }
    83.         else
    84.         {
    85.                 for (int i = 0; i < curveSegments.length(); i++)
    86.                 {
    87.                         tmpCurve = static_cast<AcDbCurve*>(curveSegments[i]);
    88.                         delete tmpCurve;
    89.                 }
    90.                 curveSegments.removeAll();
    91.         }
    92.        
    93.         pBreakLine->close();
    94.         delete pBreakLine;

    95.         return es;
    96. }

    97. static void SplitCurves (void)
    98. {
    99.         AcDbObjectId curSpaceId = acdbCurDwg()->currentSpaceId();
    100.         AcDbBlockTableRecord *pBlkRec = NULL;
    101.         AcDbCurve* tmpCurve;
    102.         AcGePoint3dArray pts;
    103.         AcDbVoidPtrArray curveSegments;
    104.         AcGePoint3dArray ints;
    105.         AcGePoint3dArray duplicates;
    106.         PointList points;
    107.         Acad::ErrorStatus es;
    108.         ads_name ss1;
    109.         AcDbObjectIdArray ids;

    110.         resbuf *rbFilter = acutBuildList(RTDXF0, _T("LINE,ARC,LWPOLYLINE,POLYLINE,CIRCLE,SPLINE,ELLIPSE"), RTNONE);

    111.         if (acedSSGet(NULL, NULL, NULL, rbFilter, ss1) != RTNORM)
    112.         {
    113.                 acutRelRb(rbFilter);
    114.                 return;
    115.         }

    116.         acutRelRb(rbFilter);

    117.         long length1 = 0;
    118.         if ((acedSSLength(ss1, &length1) != RTNORM) || (length1 == 0))
    119.         {
    120.                 acedSSFree(ss1);
    121.                 return;
    122.         }

    123.         for (UINT i = 0; i < length1; i++)
    124.         {
    125.                 AcDbObjectId oid;
    126.                 ads_name ename;
    127.                 acedSSName(ss1, i, ename);
    128.                 if (acdbGetObjectId(oid, ename) != Acad::eOk) return;
    129.                 AcDbObjectPointer<AcDbCurve> pPoly(oid, AcDb::kForWrite);
    130.                 if (pPoly.openStatus() == Acad::eOk)
    131.                 {
    132.                         ids.append(oid);

    133.                         AcDbExtents ebox;
    134.                         pPoly->getGeomExtents(ebox);

    135.                         AcGePoint3d ll, ur;
    136.                         ll = ebox.minPoint();
    137.                         ur = ebox.maxPoint();

    138.                         acdbWcs2Ucs(asDblArray(ll), asDblArray(ll), false);
    139.                         acdbWcs2Ucs(asDblArray(ur), asDblArray(ur), false);

    140.                         ads_point ptres, a, b;
    141.                         AcGePoint3d m;

    142.                         double d = (sqrt(2.0) * acutDistance(asDblArray(ll), asDblArray(ur)) / 2.0);

    143.                         acutPolar(asDblArray(ll), acutAngle(asDblArray(ll), asDblArray(ur)),
    144.                                 acutDistance(asDblArray(ll), asDblArray(ur)), asDblArray(m));

    145.                         acutPolar(asDblArray(m), ((3 * kPi) / 4), d, ptres);
    146.                         acdbWcs2Ucs(ptres, a, false);
    147.                         acutPolar(asDblArray(m), ((7 * kPi) / 4), d, ptres);
    148.                         acdbWcs2Ucs(ptres, b, false);

    149.                         ads_name ss2;
    150.                         resbuf *rbFilter = acutBuildList(RTDXF0, _T("LINE,ARC,LWPOLYLINE,POLYLINE,CIRCLE,SPLINE,ELLIPSE"), RTNONE);
    151.                         if (acedSSGet(_T("_C"), asDblArray(ll), asDblArray(ur), rbFilter, ss2) != RTNORM)
    152.                         //if (acedSSGet(_T("_C"), a, b, rbFilter, ss2) != RTNORM)
    153.                         {
    154.                                 acutRelRb(rbFilter);
    155.                                 return;
    156.                         }
    157.                         acutRelRb(rbFilter);

    158.                         acedSSDel(ename, ss2);

    159.                         long length2 = 0;
    160.                         if ((acedSSLength(ss2, &length2) != RTNORM) || (length2 == 0))
    161.                         {
    162.                                 acedSSFree(ss2);
    163.                                 return;
    164.                         }

    165.                         for (UINT i = 0; i < length2; i++)
    166.                         {
    167.                                 AcDbObjectId oid;
    168.                                 ads_name ename;
    169.                                 acedSSName(ss2, i, ename);
    170.                                 if (acdbGetObjectId(oid, ename) != Acad::eOk) return;

    171.                                 AcDbObjectPointer<AcDbCurve> pEnt(oid, AcDb::kForRead);
    172.                                 if (pEnt.openStatus() == Acad::eOk)
    173.                                 {
    174.                                         if (pPoly->intersectWith(pEnt.object(), AcDb::kOnBothOperands, ints) == Acad::eOk)
    175.                                         {
    176.                                                 if (!ints.isEmpty())
    177.                                                 {
    178.                                                         for (UINT i = 0; i < ints.length(); i++)
    179.                                                         {
    180.                                                                 if (!duplicates.contains(ints.at(i)))
    181.                                                                 {
    182.                                                                         duplicates.append(ints.at(i));
    183.                                                                         points.push_back(ints.at(i));
    184.                                                                 }
    185.                                                                 else
    186.                                                                         continue;
    187.                                                         }
    188.                                                 }
    189.                                         }
    190.                                 }
    191.                         }
    192.                         acedSSFree(ss2);

    193.                         if (!points.empty())
    194.                         {
    195.                                 vector<double> distances;
    196.                                 map<double, AcGePoint3d> m;
    197.                                 double dist;
    198.                                 for (UINT i = 0; i < points.size(); i++)
    199.                                 {
    200.                                         AcGePoint3d pt = points.at(i);
    201.                                         if (pPoly->getDistAtPoint(pt, dist) == Acad::eOk)
    202.                                         {
    203.                                                 m.insert(make_pair(dist, pt));
    204.                                                 distances.push_back(dist);
    205.                                         }
    206.                                 }

    207.                                 map<double, AcGePoint3d>::const_iterator it;
    208.                                 int cnt = 0;

    209.                                 std::sort(distances.begin(), distances.end());
    210.                                 std::vector<AcGePoint3d> _points;

    211.                                 for (it = m.begin(); it != m.end(); ++it)
    212.                                 {
    213.                                         AcGePoint3d pt;
    214.                                         it = m.find(distances.at(cnt));
    215.                                         pt = (*it).second;
    216.                                         _points.push_back(pt);
    217.                                         cnt = (1 + cnt);
    218.                                 }

    219.                                 m.clear();
    220.                                 distances.clear();

    221.                                 for (UINT i = 0; i < _points.size(); i++)
    222.                                 {
    223.                                         AcGePoint3d pt;
    224.                                         pt = _points.at(i);
    225.                                         acdbWcs2Ucs(asDblArray(pt), asDblArray(pt), false);
    226.                                         pts.append(pt);
    227.                                 }

    228.                                 if (pPoly->isKindOf(AcDb2dPolyline::desc()))
    229.                                 {
    230.                                         AcDb2dPolyline *tmp2dPoly = AcDb2dPolyline::cast((AcRxObject*)pPoly.object());

    231.                                         switch (tmp2dPoly->polyType())
    232.                                         {
    233.                                         case AcDb::k2dSimplePoly:
    234.                                                 if (pPoly->getSplitCurves(pts, curveSegments) == Acad::eOk)
    235.                                                 {
    236.                                                         for (UINT i = 0; i < curveSegments.length(); i++)
    237.                                                         {
    238.                                                                 tmpCurve = static_cast<AcDbCurve*>(curveSegments[i]);
    239.                                                                 if (tmpCurve)
    240.                                                                 {
    241.                                                                         if (acdbOpenObject(pBlkRec, curSpaceId, AcDb::kForWrite) == Acad::eOk)
    242.                                                                         {
    243.                                                                                 pBlkRec->appendAcDbEntity(tmpCurve);
    244.                                                                                 pBlkRec->close();
    245.                                                                                 tmpCurve->close();
    246.                                                                         }
    247.                                                                 }
    248.                                                         }
    249.                                                         curveSegments.removeAll();
    250.                                                 }
    251.                                                 else
    252.                                                 {
    253.                                                         for (UINT i = 0; i < curveSegments.length(); i++)
    254.                                                         {
    255.                                                                 tmpCurve = static_cast<AcDbCurve*>(curveSegments[i]);
    256.                                                                 delete tmpCurve;
    257.                                                         }
    258.                                                         curveSegments.removeAll();
    259.                                                 }

    260.                                                 break;
    261.                                         case AcDb::k2dFitCurvePoly:
    262.                                                 MakeTempLWPoly(tmp2dPoly, pts, curveSegments);
    263.                                                 break;
    264.                                         case AcDb::k2dQuadSplinePoly:
    265.                                                 MakeTempLWPoly(tmp2dPoly, pts, curveSegments);
    266.                                                 break;
    267.                                         case AcDb::k2dCubicSplinePoly:
    268.                                                 MakeTempLWPoly(tmp2dPoly, pts, curveSegments);
    269.                                                 break;
    270.                                         }
    271.                                 }
    272.                                 else
    273.                                 {                                       
    274.                                         if (pPoly->getSplitCurves(pts, curveSegments) == Acad::eOk)
    275.                                         {
    276.                                                 for (UINT i = 0; i < curveSegments.length(); i++)
    277.                                                 {
    278.                                                         tmpCurve = static_cast<AcDbCurve*>(curveSegments[i]);
    279.                                                         if (tmpCurve)
    280.                                                         {
    281.                                                                 if (acdbOpenObject(pBlkRec, curSpaceId, AcDb::kForWrite) == Acad::eOk)
    282.                                                                 {
    283.                                                                         pBlkRec->appendAcDbEntity(tmpCurve);
    284.                                                                         pBlkRec->close();
    285.                                                                         tmpCurve->close();
    286.                                                                 }
    287.                                                         }
    288.                                                 }
    289.                                                 curveSegments.removeAll();
    290.                                         }
    291.                                         else
    292.                                         {
    293.                                                 for (UINT i = 0; i < curveSegments.length(); i++)
    294.                                                 {
    295.                                                         tmpCurve = static_cast<AcDbCurve*>(curveSegments[i]);
    296.                                                         delete tmpCurve;
    297.                                                 }
    298.                                                 curveSegments.removeAll();
    299.                                         }
    300.                                 }

    301.                                 _points.clear();
    302.                                 points.clear();
    303.                         }
    304.                         if (!ints.isEmpty())
    305.                                 ints.removeAll();

    306.                         if (!duplicates.isEmpty())
    307.                                 duplicates.removeAll();

    308.                         if (!curveSegments.isEmpty())
    309.                                 curveSegments.removeAll();

    310.                         if (!pts.isEmpty())
    311.                                 pts.removeAll();
    312.                 }
    313.         }
    314.         acedSSFree(ss1);

    315.         if (!ids.isEmpty())
    316.         {
    317.                 for (UINT i = 0; i < ids.length(); i++)
    318.                 {
    319.                         AcDbObjectPointer<AcDbEntity> pEnt(ids[i], AcDb::kForWrite);
    320.                         if (pEnt.openStatus() == Acad::eOk)
    321.                         {
    322.                                 pEnt->erase();
    323.                         }
    324.                 }
    325.                 ids.removeAll();
    326.         }
    327. }
    328. Also, a simple version in C#:

    329. #region Split Curves
    330. [CommandMethod("SPLITCURVES")]
    331. public void cmd_splitCurves()
    332. {
    333.     var editor = AcadApp.DocumentManager.MdiActiveDocument.Editor;
    334.     var tv = new[] { new TypedValue((int)DxfCode.Start, "LINE,ARC,LWPOLYLINE,POLYLINE,CIRCLE,SPLINE,ELLIPSE") };
    335.     var filter = new SelectionFilter(tv);
    336.     const string curveObjects = "curve objects";
    337.     var options = new PromptSelectionOptions
    338.     {
    339.         MessageForAdding = string.Format("\nAdd {0} to selection", curveObjects),
    340.         MessageForRemoval = string.Format("\nRemove {0} from selection", curveObjects),
    341.         AllowDuplicates = false,
    342.         RejectObjectsFromNonCurrentSpace = true
    343.     };
    344.     var mainSelection = editor.GetSelection(options, filter);
    345.     if (mainSelection.Status != PromptStatus.OK) return;

    346.     using (var transaction = editor.Document.Database.TransactionManager.StartTransaction())
    347.     {
    348.         var blockTableRecord = transaction.GetObject(editor.Document.Database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
    349.         if (blockTableRecord != null)
    350.         {
    351.             foreach (var mainObjectId in mainSelection.Value.GetObjectIds())
    352.             {
    353.                 var points = new Point3dCollection();
    354.                 var dictionary = new SortedDictionary<double, Point3d>();

    355.                 var mainCurve = transaction.GetObject(mainObjectId, OpenMode.ForWrite) as Curve;
    356.                 if (mainCurve == null) continue;

    357.                 var extents = mainCurve.GeometricExtents;
    358.                 var minPoint = extents.MinPoint;
    359.                 var maxPoint = extents.MaxPoint;

    360.                 var subSelection = editor.SelectCrossingWindow(minPoint, maxPoint, filter);
    361.                 if (subSelection.Status != PromptStatus.OK) continue;

    362.                 foreach (var subObjectId in subSelection.Value.GetObjectIds())
    363.                 {
    364.                     var subCurve = transaction.GetObject(subObjectId, OpenMode.ForRead) as Curve;
    365.                     if (subCurve == null) continue;
    366.                     var intersections = new Point3dCollection();
    367.                     mainCurve.IntersectWith(subCurve, Intersect.OnBothOperands, intersections, IntPtr.Zero, IntPtr.Zero);
    368.                     if (intersections.Count <= 0) continue;

    369.                     foreach (Point3d intersection in intersections)
    370.                     {
    371.                         if (points.Contains(intersection)) continue;

    372.                         points.Add(intersection);
    373.                     }
    374.                 }
    375.                 subSelection.Value.Dispose();

    376.                 if (points.Count <= 0) continue;

    377.                 foreach (Point3d point in points)
    378.                 {
    379.                     var distance = mainCurve.GetDistAtPoint(point);
    380.                     if (dictionary.ContainsKey(distance)) continue;

    381.                     dictionary.Add(distance, point);
    382.                 }

    383.                 if (!dictionary.Any()) continue;

    384.                 var splitPoints = new Point3dCollection(dictionary.Values.ToArray());
    385.                 var splitCurves = mainCurve.GetSplitCurves(splitPoints);
    386.                 foreach (DBObject splitCurve in splitCurves)
    387.                 {
    388.                     var curve = splitCurve as Curve;
    389.                     if (curve == null) continue;

    390.                     blockTableRecord.AppendEntity(curve);
    391.                     transaction.AddNewlyCreatedDBObject(curve, true);
    392.                 }
    393.             }
    394.         }
    395.         transaction.Commit();
    396.     }
    397. }
    398. #endregion
    复制代码

     

     

     

     

    曲线打断
    哎...膜结构车棚,签到来了1...
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|中国膜结构网_中国空间膜结构协会

    GMT+8, 2024-5-13 19:55 , Processed in 0.060880 second(s), 21 queries .

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

    快速回复 返回顶部 返回列表