- 积分
- 1142
- 明经币
- 个
- 注册时间
- 2011-2-28
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
- 经测试没有大问题,只是画完多段线后原有线段不能完全删除,希望高手给予修改意见,如有BUG请回复!
- public class mycommand
- {
- //本代码由<家在湾里>书写,仅供学习用,引用请注明出处.............
- //联系本人请Q:584457142
- [CommandMethod("my")]//将首尾相连的线段或多段线连成多段线
- public void my()
- {
- Document acDoc = Application.DocumentManager.MdiActiveDocument;
- Database acCurDb = acDoc.Database;
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- try
- {
- using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
- {
- BlockTable acBlkTbl;
- acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
- BlockTableRecord acBlkTblRec;
- acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
- OpenMode.ForWrite) as BlockTableRecord;
- PromptSelectionOptions pso=new PromptSelectionOptions();
- PromptSelectionResult psr = ed.GetSelection(pso);
- Polyline pl = new Polyline();
- if (psr.Status == PromptStatus.OK)
- {
- SelectionSet ss = psr.Value;
- int n = ss.Count;
- ObjectIdCollection oc = new ObjectIdCollection() ;
- for (int i = 0; i < n; i++)
- {
- oc.Add(ss[i].ObjectId);
- }
- Entity ent2 = acTrans.GetObject(oc[0], OpenMode.ForWrite) as Entity;
- for (int j = 0; j <n; j++)
- {
-
- for (int i = 1; i < oc.Count; i++)
- {
-
- Entity ent3 = acTrans.GetObject(oc[i], OpenMode.ForWrite) as Entity;
- pl = mycommand.EntityUnion(ent2, ent3);
- if (pl == null || pl.NumberOfVertices == 0) continue;
- else
- {
- oc.RemoveAt(i);
- ent2 = pl as Entity;
- ent3.Erase();
- break;
-
- }
- }
- }
- //ent2.Erase();
- }
-
- acBlkTblRec.AppendEntity(pl);
- acTrans.AddNewlyCreatedDBObject(pl,true);
- acTrans.Commit();
- }
- }
- catch (System.Exception)
- {
- Application.ShowAlertDialog("error");
- }
- }
- public static Polyline EntityUnion(Entity ent1, Entity ent2)
- {
- Polyline pl1 = new Polyline();
- Polyline pl2 = new Polyline();
- Polyline pl = new Polyline();
- Point3d equalpoint = new Point3d();
- Point3dCollection pc = new Point3dCollection();
- Point3dCollection final = new Point3dCollection();
- #region
- if (ent1 is Polyline && ent2 is Polyline)
- {
- pl1 = ent1 as Polyline;
- pl2 = ent2 as Polyline;
- pc.Add(pl1.StartPoint);
- pc.Add(pl1.EndPoint);
- pc.Add(pl2.StartPoint);
- pc.Add(pl2.EndPoint);
- for (int i = 0; i < 3; i++)
- {
- for (int j = i + 1; j < 4; j++)
- {
- if (pc[i] == pc[j])
- {
- equalpoint = pc[i];
- break;
- }
- }
- if (equalpoint != new Point3d(0, 0, 0)) break;
- }
- if (equalpoint == new Point3d(0, 0, 0)) return null;
- if (pl1.EndPoint.Equals(equalpoint))
- {
- for (int i = 0; i < pl1.NumberOfVertices - 1; i++)
- {
- final.Add(pl1.GetPoint3dAt(i));
- }
- if (pl2.StartPoint.Equals(equalpoint))
- {
- for (int i = 0; i < pl2.NumberOfVertices; i++)
- {
- final.Add(pl2.GetPoint3dAt(i));
- }
- }
- else
- {
- for (int i = pl2.NumberOfVertices - 1; i > -1; i--)
- {
- final.Add(pl2.GetPoint3dAt(i));
- }
-
- }
- }
- else if (pl1.StartPoint.Equals(equalpoint))
- {
- if (pl2.EndPoint.Equals(equalpoint))
- {
- for (int i = 0; i < pl2.NumberOfVertices; i++)
- {
- final.Add(pl2.GetPoint3dAt(i));
- }
- for (int i = 1; i < pl1.NumberOfVertices; i++)
- {
- final.Add(pl1.GetPoint3dAt(i));
- }
- }
- else
- {
- for (int i = pl2.NumberOfVertices - 1; i >= 0; i--)
- {
- final.Add(pl2.GetPoint3dAt(i));
- }
- for (int i = 1; i < pl1.NumberOfVertices; i++)
- {
- final.Add(pl1.GetPoint3dAt(i));
- }
- }
-
- }
- for (int i = 0; i < final.Count; i++)
- {
- pl.AddVertexAt(i, new Point2d(final[i].X, final[i].Y), 0, 0, 0);
- }
- return pl;
- }
-
- #endregion
- #region
- if (ent1 is Line && ent2 is Line)
- {
- Line l1 = ent1 as Line;
- Line l2 = ent2 as Line;
- pc.Add(l1.StartPoint);
- pc.Add(l1.EndPoint);
- pc.Add(l2.StartPoint);
- pc.Add(l2.EndPoint);
- for (int i = 0; i < 3; i++)
- {
- for (int j = i + 1; j < 4; j++)
- {
- if (pc[i] == pc[j])
- {
- equalpoint = pc[i];
- pc.Remove(pc[i]);
- pc.Remove(pc[j-1]);
- break;
- }
- }
- if (equalpoint != new Point3d(0, 0, 0)) break;
-
- }
- if (equalpoint == new Point3d(0, 0, 0)) return null;
- final.Add(pc[0]);
- Point3d p1 = pc[0];
- Point3d p2 = pc[1];
- final.Add(equalpoint);
- final.Add(pc[1]);
- for (int i = 0; i < final.Count; i++)
- {
- pl.AddVertexAt(i, new Point2d(final[i].X, final[i].Y), 0, 0, 0);
- }
- return pl;
- }
- #endregion
- #region
- if (ent1 is Line && ent2 is Polyline)
- {
- Line l = ent1 as Line;
- pl1 = ent2 as Polyline;
- pc.Add(l.StartPoint);
- pc.Add(l.EndPoint);
- pc.Add(pl1.StartPoint);
- pc.Add(pl1.EndPoint);
- for (int i = 0; i < 3; i++)
- {
- for (int j = i + 1; j < 4; j++)
- {
- if (pc[i] == pc[j])
- {
- equalpoint = pc[i];
- break;
- }
- }
- if (equalpoint != new Point3d(0, 0, 0)) break;
-
- }
- if (equalpoint == new Point3d(0, 0, 0)) return null;
- if (equalpoint.Equals(pl1.EndPoint))
- {
- for (int i = 0; i < pl1.NumberOfVertices; i++)
- {
- final.Add(pl1.GetPoint3dAt(i));
- }
- if (equalpoint.Equals(l.StartPoint))
- final.Add(l.EndPoint);
- else final.Add(l.StartPoint);
- }
- else if (equalpoint.Equals(pl1.StartPoint))
- {
- for (int i = pl1.NumberOfVertices - 1; i >= 0; i--)
- {
- final.Add(pl1.GetPoint3dAt(i));
- }
- if (equalpoint.Equals(l.StartPoint))
- final.Add(l.EndPoint);
- else final.Add(l.StartPoint);
- }
- for (int i = 0; i < final.Count; i++)
- {
- pl.AddVertexAt(i, new Point2d(final[i].X, final[i].Y), 0, 0, 0);
- }
- return pl;
- }
- #endregion
- #region
- if (ent1 is Polyline && ent2 is Line)
- {
- Line l = ent2 as Line;
- pl1 = ent1 as Polyline;
- pc.Add(l.StartPoint);
- pc.Add(l.EndPoint);
- pc.Add(pl1.StartPoint);
- pc.Add(pl1.EndPoint);
- for (int i = 0; i < 3; i++)
- {
- for (int j = i + 1; j < 4; j++)
- {
- if (pc[i] == pc[j])
- {
- equalpoint = pc[i];
- break;
- }
- }
- if (equalpoint != new Point3d(0, 0, 0)) break;
-
- }
- if (equalpoint == new Point3d(0, 0, 0)) return null;
- if (equalpoint.Equals(pl1.EndPoint))
- {
- for (int i = 0; i < pl1.NumberOfVertices; i++)
- {
- final.Add(pl1.GetPoint3dAt(i));
- }
- if (equalpoint.Equals(l.StartPoint))
- final.Add(l.EndPoint);
- else final.Add(l.StartPoint);
- }
- else if (equalpoint.Equals(pl1.StartPoint))
- {
- for (int i = pl1.NumberOfVertices - 1; i >= 0; i--)
- {
- final.Add(pl1.GetPoint3dAt(i));
- }
- if (equalpoint.Equals(l.StartPoint))
- final.Add(l.EndPoint);
- else final.Add(l.StartPoint);
- }
- for (int i = 0; i < final.Count; i++)
- {
- pl.AddVertexAt(i, new Point2d(final[i].X, final[i].Y), 0, 0, 0);
- }
- return pl;
- }
- #endregion
- else return null;
- }
|
|