123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- using GrxCAD.DatabaseServices;
- using GrxCAD.Geometry;
- using GrxCAD.ApplicationServices;
- using GrxCAD.EditorInput;
- using System.Windows.Forms;
-
- namespace HCTools
- {
- class Connect
- {
- Point3dCollection joinpoint = new Point3dCollection();//连接点
-
- #region 连接
- /// <summary>
- /// 连接线
- /// </summary>
- public void joinplline()
- {
- ObjectId[] line = BasicFunction.getPll();//获取待连接线
- if (line == null)
- return;
-
- //遍历比较待连接线,连接高程相同的两等高线
- Database db = HostApplicationServices.WorkingDatabase;
- DocumentLock doclock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
- Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
-
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- if (line.Length == 1)
- {
- ed.WriteMessage("请选择另一条连接线");
- return;
- }
-
- try
- {
- Entity entity1 = line[0].GetObject(OpenMode.ForWrite)as Entity;
- Entity entity2 = line[1].GetObject(OpenMode.ForWrite) as Entity;
- if (entity1 is Polyline2d && entity2 is Polyline)
- {
- Polyline2d pll1 = (Polyline2d)entity1;
- Polyline pll2 = (Polyline)trans.GetObject(entity2.Id, OpenMode.ForWrite);
- pll2.Elevation = pll1.Elevation;
- Calculate(pll1.StartPoint, pll1.EndPoint, pll2.StartPoint, pll2.EndPoint);
- Polyline pline = new Polyline();
- pline.AddVertexAt(0, new Point2d(joinpoint[0].X, joinpoint[0].Y), 0, 0, 0);
- pline.AddVertexAt(1, new Point2d(joinpoint[1].X, joinpoint[1].Y), 0, 0, 0);//创建连接线
-
- if (pll2.Elevation == pll1.Elevation)
- {
- //pll1.JoinEntity(pline);//浩辰的JoinEntity函数会报enotemplementedyet,不知道原因。
- Polyline pll_temp = PllConn(pll2, pline);
- //pll1.JoinEntity(pll2);
- Polyline pll_final = Pllpll2d(pll_temp, pll1, trans);
- pll1.Erase();
- pll2.Erase();
- BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- btr.AppendEntity(pll_final);
- trans.AddNewlyCreatedDBObject(pll_final, true);
- }
- else
- MessageBox.Show("两线高程不一致!", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
- }
- else if (entity1 is Polyline && entity2 is Polyline2d)
- {
- Polyline pll1 = (Polyline)entity1;
- Polyline2d pll2 = (Polyline2d)entity2;
- Calculate(pll1.StartPoint, pll1.EndPoint, pll2.StartPoint, pll2.EndPoint);
- Polyline pline = new Polyline();
- pline.AddVertexAt(0, new Point2d(joinpoint[0].X, joinpoint[0].Y), 0, 0, 0);
- pline.AddVertexAt(1, new Point2d(joinpoint[1].X, joinpoint[1].Y), 0, 0, 0);//创建连接线
-
- if (pll2.Elevation == pll1.Elevation)
- {
- //pll1.JoinEntity(pline);//浩辰的JoinEntity函数会报enotemplementedyet,不知道原因。
- Polyline pll_temp = PllConn(pll1,pline);
- //pll1.JoinEntity(pll2);
- Polyline pll_final = Pllpll2d(pll_temp, pll2, trans);
- pll1.Erase();
- pll2.Erase();
- BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- btr.AppendEntity(pll_final);
- trans.AddNewlyCreatedDBObject(pll_final, true);
- }
- else
- MessageBox.Show("两线高程不一致!", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
- }
- else if (entity1 is Polyline2d && entity2 is Polyline2d)
- {
- Polyline2d pll1 = (Polyline2d)entity1;
- Polyline2d pll2 = (Polyline2d)entity2;
- Calculate(pll1.StartPoint, pll1.EndPoint, pll2.StartPoint, pll2.EndPoint);
- Point3dCollection pos = new Point3dCollection{joinpoint[0], joinpoint[1]};
- DoubleCollection doubles = new DoubleCollection{0,0,0,0};
- Polyline2d pline = new Polyline2d(Poly2dType.SimplePoly, pos, 0, false, 0, 0, doubles);
- BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- btr.AppendEntity(pline);
- trans.AddNewlyCreatedDBObject(pline, true);
- //pline.AddVertexAt(0, new Point2d(joinpoint[0].X, joinpoint[0].Y), 0, 0, 0);
- //pline.AddVertexAt(1, new Point2d(joinpoint[1].X, joinpoint[1].Y), 0, 0, 0);//创建连接线
-
- if (pll2.Elevation == pll1.Elevation)
- {
- Polyline2d pll_temp = Pll2dConn(pll1, pline,trans);
- btr.AppendEntity(pll_temp);
- trans.AddNewlyCreatedDBObject(pll_temp, true);
- //pll1.JoinEntity(pline);
- //pll1.JoinEntity(pll2);
- Polyline2d pll_final = Pll2dConn(pll_temp, pll2,trans);
- pll1.Erase();
- pline.Erase();
- pll2.Erase();
- pll_temp.Erase();
- btr.AppendEntity(pll_final);
- trans.AddNewlyCreatedDBObject(pll_final, true);
- }
- else
- MessageBox.Show("两线高程不一致!", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
- }
- else
- {
- Polyline pll1 = (Polyline)entity1;
- Polyline pll2 = (Polyline)entity2;
- Calculate(pll1.StartPoint, pll1.EndPoint, pll2.StartPoint, pll2.EndPoint);
- Polyline pline = new Polyline();
- pline.AddVertexAt(0, new Point2d(joinpoint[0].X, joinpoint[0].Y), 0, 0, 0);
- pline.AddVertexAt(1, new Point2d(joinpoint[1].X, joinpoint[1].Y), 0, 0, 0);//创建连接线
-
- if (pll2.Elevation == pll1.Elevation)
- {
- //pll1.JoinEntity(pline);//浩辰的JoinEntity函数会报enotemplementedyet,不知道原因。
- Polyline pll_temp = PllConn(pll1, pline);
- //pll1.JoinEntity(pll2);
- Polyline pll_final = PllConn(pll_temp, pll2);
- pll1.Erase();
- pll2.Erase();
- BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- btr.AppendEntity(pll_final);
- trans.AddNewlyCreatedDBObject(pll_final, true);
- }
- else
- MessageBox.Show("两线高程不一致!", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
- }
- trans.Commit();
- }
-
-
- catch (GrxCAD.Runtime.Exception ex)
- {
- string str = ex.ToString();
- GrxCAD.ApplicationServices.Application.ShowAlertDialog(str);
- }
- }
- }
-
-
- /// <summary>
- /// 计算获得连接点
- /// </summary>
- private Point3dCollection Calculate(Point3d startpt1, Point3d endpt1, Point3d startpt2, Point3d endpt2)
- {
- Double[] distance=new Double[4];
- joinpoint.Clear();
- int i = 0;
-
- //两线段四个端点之间的距离
- distance[0] = Math.Sqrt(Math.Pow((startpt1.X - startpt2.X), 2) + Math.Pow((startpt1.Y - startpt2.Y), 2));
- distance[1] = Math.Sqrt(Math.Pow((startpt1.X - endpt2.X), 2) + Math.Pow((startpt1.Y - endpt2.Y), 2));
- distance[2] = Math.Sqrt(Math.Pow((endpt1.X - startpt2.X), 2) + Math.Pow((endpt1.Y - startpt2.Y), 2));
- distance[3] = Math.Sqrt(Math.Pow((endpt1.X - endpt2.X), 2) + Math.Pow((endpt1.Y - endpt2.Y), 2));
-
- //比较获取最小的距离,得到连接点
- for (i = 0; i < distance.Length; i++)
- {
- if (distance[i] == distance.Min())
- break;
- }
- switch (i.ToString())
- {
- case "0":
- joinpoint.Add(startpt1);
- joinpoint.Add(startpt2);
- break;
-
- case "1":
- joinpoint.Add(startpt1);
- joinpoint.Add(endpt2);
- break;
-
- case "2":
- joinpoint.Add(endpt1);
- joinpoint.Add(startpt2);
- break;
-
- case "3":
- joinpoint.Add(endpt1);
- joinpoint.Add(endpt2);
- break;
- }
- return joinpoint;
- }
-
- /// <summary>
- /// 连接两条线(两条都是Polyline)
- /// </summary>
- private Polyline PllConn(Polyline pll1, Polyline pll2)
- {
- List<Point2d> pllpt = new List<Point2d>();
- List<Point2d> pll1pt = new List<Point2d>();
- for (int i = 0; i < pll1.NumberOfVertices; i++)
- {
- pllpt.Add(pll1.GetPoint2dAt(i));
- }
- for (int i = 0; i < pll2.NumberOfVertices; i++)
- {
- pll1pt.Add(pll2.GetPoint2dAt(i));
- }
- //if (pllpt[pllpt.Count - 1] != pll1pt[pll1pt.Count - 1])
- // pllpt.Reverse();
- //else if (pllpt[0] != pll1pt[0])
- // pll1pt.Reverse();
- if (pllpt[pllpt.Count - 1] == pll1pt[pll1pt.Count - 1])
- pll1pt.Reverse();
- else if (pllpt[0] == pll1pt[pll1pt.Count - 1])
- {
- pllpt.Reverse();
- pll1pt.Reverse();
- }
- else if (pllpt[0] == pll1pt[0])
- pllpt.Reverse();
- for (int i = 0; i < pll1pt.Count; i++)
- {
- if (pllpt.Contains(pll1pt[i]))
- continue;
- else
- pllpt.Add(pll1pt[i]);
- }
- Polyline pline = new Polyline();
- for (int i = 0; i < pllpt.Count; i++)
- {
- pline.AddVertexAt(i, new Point2d(pllpt[i].X, pllpt[i].Y), 0, 0, 0);
- }
- pline.Elevation = pll1.Elevation;
- pline.LinetypeId = pll1.LinetypeId;
- pline.Color = pll1.Color;
- pline.Layer = pll1.Layer;
- return pline;
- }
-
-
- /// <summary>
- /// 连接两条线(两条都是Polyline2d)
- /// </summary>
- private Polyline2d Pll2dConn(Polyline2d pll1, Polyline2d pll2, Transaction tr)
- {
- List<Point3d> pllpt = new List<Point3d>();
- List<Point3d> pll1pt = new List<Point3d>();
- List<double> bulgeList = new List<double>();//存凸度
- List<double> bulgeList1 = new List<double>();
- Point3dCollection ptcoll = new Point3dCollection();
- DoubleCollection dbcoll = new DoubleCollection();
- foreach(ObjectId id in pll1)
- {
- Vertex2d vx = (Vertex2d)tr.GetObject(id, OpenMode.ForRead);
- if (vx.VertexType != Vertex2dType.SplineControlVertex)
- {
- pllpt.Add(vx.Position);
- bulgeList.Add(vx.Bulge);
- }
- }
- foreach (ObjectId id in pll2)
- {
- Vertex2d vx = (Vertex2d)tr.GetObject(id, OpenMode.ForRead);
- if (vx.VertexType != Vertex2dType.SplineControlVertex)
- {
- pll1pt.Add(vx.Position);
- bulgeList1.Add(vx.Bulge);
- }
- }
- //if (pllpt[pllpt.Count - 1] != pll1pt[pll1pt.Count - 1])
- //{
- // pllpt.Reverse();
- // bulgeList.Reverse();
- //}
- //else if (pllpt[0] != pll1pt[0])
- //{
- // pll1pt.Reverse();
- // bulgeList1.Reverse();
- //}
- if (pllpt[pllpt.Count - 1] == pll1pt[pll1pt.Count - 1])
- {
- pll1pt.Reverse();
- bulgeList1.Reverse();
- }
- else if (pllpt[0] == pll1pt[pll1pt.Count - 1])
- {
- pllpt.Reverse();
- pll1pt.Reverse();
- bulgeList.Reverse();
- bulgeList1.Reverse();
- }
- else if (pllpt[0] == pll1pt[0])
- {
- pllpt.Reverse();
- bulgeList1.Reverse();
- }
- for (int i = 0; i < pll1pt.Count; i++)
- {
- if (pllpt.Contains(pll1pt[i]))
- continue;
- else
- {
- pllpt.Add(pll1pt[i]);
- bulgeList.Add(bulgeList1[i]);
- }
- }
- for (int i = 0; i < pllpt.Count; i++)
- {
- ptcoll.Add(pllpt[i]);
- }
- for (int i = 0; i < bulgeList.Count; i++)
- {
- dbcoll.Add(bulgeList[i]);
- }
- Polyline2d pline = new Polyline2d(pll1.PolyType, ptcoll, 0, false, 0, 0, dbcoll);
- pline.Elevation = pll1.Elevation;
- pline.LinetypeId = pll1.LinetypeId;
- pline.Color = pll1.Color;
- pline.Layer = pll1.Layer;
- return pline;
- }
-
-
- /// <summary>
- /// 连接两条线(一条是Polyline,一条是Polyline2d,连接后的线为Polyline)
- /// </summary>
- private Polyline Pllpll2d(Polyline pll1, Polyline2d pll2, Transaction tr)
- {
- List<Point3d> pllpt = new List<Point3d>();
- List<Point3d> pll1pt = new List<Point3d>();
- for (int i = 0; i < pll1.NumberOfVertices; i++)
- {
- pllpt.Add(pll1.GetPoint3dAt(i));
- }
- foreach (ObjectId id in pll2)
- {
- Vertex2d vx = (Vertex2d)tr.GetObject(id, OpenMode.ForRead);
- if (vx.VertexType != Vertex2dType.SplineControlVertex)
- {
- pll1pt.Add(vx.Position);
- }
- }
- //if (pllpt[pllpt.Count - 1] != pll1pt[pll1pt.Count - 1])
- // pllpt.Reverse();
- //else if (pllpt[0] != pll1pt[0])
- // pll1pt.Reverse();
- if (pllpt[pllpt.Count - 1] == pll1pt[pll1pt.Count - 1])
- pll1pt.Reverse();
- else if (pllpt[0] == pll1pt[pll1pt.Count - 1])
- {
- pllpt.Reverse();
- pll1pt.Reverse();
- }
- else if (pllpt[0] == pll1pt[0])
- pllpt.Reverse();
- for (int i = 0; i < pll1pt.Count; i++)
- {
- if (pllpt.Contains(pll1pt[i]))
- continue;
- else
- pllpt.Add(pll1pt[i]);
- }
- Polyline pline = new Polyline();
- for (int i = 0; i < pllpt.Count; i++)
- {
- pline.AddVertexAt(i, new Point2d(pllpt[i].X, pllpt[i].Y), 0, 0, 0);
- }
- pline.Elevation = pll1.Elevation;
- pline.LinetypeId = pll1.LinetypeId;
- pline.Color = pll1.Color;
- pline.Layer = pll1.Layer;
- return pline;
- }
-
- #endregion
-
-
- //public void conn()
- //{
- // ObjectIdCollection ids = getEntity();
- // BlockReference br = null;
- // Polyline pll = null;
- // DBObjectCollection objcoll = new DBObjectCollection();
- // string Brname = "";
- // Point3d Brposition = new Point3d();
-
- // Database db = HostApplicationServices.WorkingDatabase;
- // DocumentLock doclock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
- // Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- // using (Transaction trans = db.TransactionManager.StartTransaction())
- // {
- // foreach(ObjectId id in ids)
- // {
- // if (trans.GetObject(id, OpenMode.ForRead) is BlockReference)
- // {
- // br = trans.GetObject(id, OpenMode.ForRead)as BlockReference;
- // Brname = br.Name;
- // Brposition = br.Position;
- // }
- // if (trans.GetObject(id, OpenMode.ForRead) is Polyline)
- // {
- // pll = trans.GetObject(id, OpenMode.ForRead) as Polyline;
- // }
- // }
- // br.Explode(objcoll);
- // trans.Commit();
- // }
-
- // foreach (Entity ent in objcoll)
- // {
- // using (Transaction trans = db.TransactionManager.StartTransaction())
- // {
- // BlockTable blocktable = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
- // BlockTableRecord blocktablerecord = trans.GetObject(blocktable[BlockTableRecord.ModelSpace],
- // OpenMode.ForWrite) as BlockTableRecord;
- // blocktablerecord.AppendEntity(ent);
- // trans.AddNewlyCreatedDBObject(ent, true);
- // trans.Commit();
- // }
- // }
-
-
- // doclock.Dispose();
- //}
-
-
- //private static ObjectIdCollection getEntity()
- //{
- // ObjectIdCollection ids = new ObjectIdCollection();
-
- // Document doc = Application.DocumentManager.MdiActiveDocument;
- // Database db = doc.Database;
- // Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
-
- // TypedValue[] value = new TypedValue[]
- // {
- // new TypedValue((int)DxfCode.Start,"Insert,POLYLINE"),
- // };//设置筛选条件
- // SelectionFilter filter = new SelectionFilter(value);
- // //选择区域中全部对象
- // //PromptSelectionResult psr = ed.SelectAll(filter);
- // // 要求在图形区域中手动选择对象
- // PromptSelectionResult psr = ed.GetSelection(filter);
-
-
- // if (psr.Status == PromptStatus.OK)
- // {
- // using (Transaction trans = db.TransactionManager.StartTransaction())
- // {
- // SelectionSet ss = psr.Value;
- // ids = new ObjectIdCollection(ss.GetObjectIds());
- // trans.Commit();
- // }
- // }
- // return ids;
- //}
- }
- }
|