“MyCustomObject”只是一个业务对象,它使用一个通用列表来保存当前的顶点列表。
关于“PolyExample.dwg”,我看不出有什么不同,有没有检查顶点数量的命令?(除了编写代码和显示)
我测试了排序代码。它适用于我当前的测试数据集。根据传入顶点的顺序,可能需要在第一次分割时执行此操作。我注意到每次拆分时,其中一条拆分的多段线的顺序都是“错误的”。
- List<MyCustomObject> myCustomObjList = new List<MyCustomObject>();
- DBObjectCollection acDboCol_1 = null;
- try
- {
- acDboCol_1 = acPoly.GetSplitCurves(acPt3dCol_1);
- foreach (Entity acEnt in acDboCol_1)
- {
- Polyline acNewPLine = acEnt as Polyline;
- acNewPLine.Closed = true;
- // Sort the polyline so that the starting point for the second split-line lies on the first segment of the polyline.
- // This is required because the splitting will give strange result when attempt to split a polyline where the first segment does not
- // contain the start point of the split line...
- for (int i = 0; i < acNewPLine.NumberOfVertices; i++)
- {
- LineSegment3d acTmpLine = acNewPLine.GetLineSegmentAt(i);
- if (acTmpLine.IsOn(acPt3dCol2Start))
- {
- // Sort if it's not on the first segment
- if (acNewPLine.StartPoint != acTmpLine.StartPoint)
- {
- List<Point3d> acTmpPts = new List<Point3d>();
- for (int j = i; j < acNewPLine.NumberOfVertices; j++)
- {
- acTmpPts.Add(acNewPLine.GetPoint3dAt(j));
- }
- for (int j = 0; j < i; j++)
- {
- acTmpPts.Add(acNewPLine.GetPoint3dAt(j));
- }
- acNewPLine.Reset(false, 0); // clear all the existing vertices
- foreach (Point3d acTmpPt in acTmpPts)
- {
- // Add to the last position
- acNewPLine.AddVertexAt(acNewPLine.NumberOfVertices, new Point2d(acTmpPt.X, acTmpPt.Y), 0.0, 0.0, 0.0);
- }
- acNewPLine.Closed = true;
- }
- break;
- }
- }
- DBObjectCollection acDboCol_2 = null;
- try
- {
- acDboCol_2 = acNewPLine.GetSplitCurves(acPt3dCol_2);
- foreach (Entity acEnt2 in acDboCol_2)
- {
- Polyline acNewPLine2 = acEnt2 as Polyline;
- acNewPLine2.Closed = true;
- MyCustomObject myCustomObj = new MyCustomObject();
- myCustomObj.Vertices = new List<Point3d>();
- for (int i = 0; i < acNewPLine2.NumberOfVertices; i++)
- {
- myCustomObj.Vertices.Add(acNewPLine2.GetPoint3dAt(i));
- }
- myCustomObjList.Add(myCustomObj);
- acNewPLine2.Dispose();
- }
- }
- catch (Autodesk.AutoCAD.Runtime.Exception ex)
- {
- if (ex.ErrorStatus == Autodesk.AutoCAD.Runtime.ErrorStatus.InvalidInput)
- {
- MyCustomObject myCustomObj = new MyCustomObject();
- myCustomObj.Vertices = new List<Point3d>();
- for (int i = 0; i < acNewPLine2.NumberOfVertices; i++)
- {
- myCustomObj.Vertices.Add(acNewPLine2.GetPoint3dAt(i));
- }
- myCustomObjList.Add(myCustomObj);
- }
- else
- {
- // Throw out other exceptions
- throw ex;
- }
- }
- finally
- {
- acNewPLine.Dispose();
- if (acDboCol_2 != null)
- acDboCol_2.Dispose();
- }
- }
- }
- finally
- {
- if (acDboCol_1 != null)
- acDboCol_1.Dispose();
- }
|