马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
问题:
我的程序在(AcDb2dPolyline)的每个顶点都附加了XDATA数据,当我使用JOIN命令连接另外一个多段线的时候,顶点的XDATA数据丢失,我如何能防止这样?
解决方案:
在使用JOIN命令的时候,AUTOCAD不会保留2D多段线顶点的XDATA数据,解决方法是写一个命令实现JOIN命令功能的同时保留住XDATA数据,下面C#代码演示如何实现。注意,通过UNDEFINE命令注销掉JOIN命令然后把自己的命令定义成JOIN,用户就像还在使用JOIN命令一样。
[it618postdisplay>0] // Define Command "njoin"[/it618postdisplay]
[sell=5] - [CommandMethod("join")]
- static public void njoin()
- {
- //Code to replace the AutoCAD's join command.
- //Please not that minimal error checking is done for code brevity
-
- //Select the source entity
- PromptEntityResult pEntrs;
- pEntrs = Application.DocumentManager.MdiActiveDocument.Editor.GetEntity("\nSelect source polyline: ");
-
-
- if(PromptStatus.OK != pEntrs.Status) return;
-
-
- ObjectId srcId = pEntrs.ObjectId;
-
-
- pEntrs = Application.DocumentManager.MdiActiveDocument.Editor.GetEntity("\nSelect polyline to join: ");
-
-
- if(PromptStatus.OK != pEntrs.Status) return;
-
-
- ObjectId joinId = pEntrs.ObjectId;
-
-
- Transaction trans = null;
- try
- {
- trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
- Polyline2d srcPLine = (Polyline2d)trans.GetObject(srcId, OpenMode.ForRead); //will throw error if the entity is not a polyline
- Polyline2d addPLine = (Polyline2d)trans.GetObject(joinId, OpenMode.ForRead); //will throw error if the entity is not a polyline
- System.Collections.IEnumerator destItr = addPLine.GetEnumerator();
- System.Collections.IEnumerator srctItr = srcPLine.GetEnumerator();
-
- //Get the last point of the source polyline
- ObjectId srcLastVertId = ObjectId.Null;
- while (false != srctItr.MoveNext())
- {
- srcLastVertId = (ObjectId)srctItr.Current;
- }
-
-
- //Check if the first point or the last point is same as the src polylines end point
-
-
- //Get all the vertices of the second polyline
- ObjectIdCollection ids = new ObjectIdCollection();
- while (false != destItr.MoveNext())
- {
- ids.Add((ObjectId)destItr.Current);
- }
-
-
- //Check which vertex is same as that of the last vertex of the src polyline
- Vertex2d srcLastVert = (Vertex2d) trans.GetObject((ObjectId)srcLastVertId, OpenMode.ForRead);
- Vertex2d addVert = (Vertex2d) trans.GetObject((ObjectId)ids[0], OpenMode.ForRead);
-
-
- int ctr, step;
- if(srcLastVert.Position == addVert.Position)
- {
- ctr = 0; step = 1;
- }
- else
- {
- addVert = (Vertex2d) trans.GetObject((ObjectId)ids[ids.Count - 1], OpenMode.ForRead);
- if(srcLastVert.Position == addVert.Position)
- {
- ctr = ids.Count - 1; step = -1;
- }
- else
- {
- Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nEnd points do not match.");
- //neither the first or the last point matches, hence quit
- throw new System.Exception();
- }
- }
- srcPLine.UpgradeOpen();
- for(int ctr1 = 0; ctr1 < ids.Count;ctr1++)
- {
- addVert = (Vertex2d) trans.GetObject((ObjectId)ids[ctr], OpenMode.ForRead);
- Vertex2d vertClone = (Vertex2d)addVert.Clone();
- srcPLine.AppendVertex(vertClone);
- trans.AddNewlyCreatedDBObject(vertClone, true);
- ctr += step;
- }
- trans.Commit();
- trans.Dispose();
- //Delete the old polyline
- trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
- addPLine = (Polyline2d)trans.GetObject(joinId, OpenMode.ForWrite);
- addPLine.Erase();
- //Commit the transaction
- trans.Commit();
-
- Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nAdded " + ctr.ToString() +" vertices.");
- }
- catch (System.Exception es)
- {
- //Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(es.Message);
- }
- finally
- {
- trans.Dispose();
- }
- }
[/sell] |