1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246 |
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.DatabaseServices.Filters;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace CutDGX
- {
- public partial class joinedge : Form
- {
- public joinedge()
- {
- InitializeComponent();
- }
-
- private void joinedge_Load(object sender, EventArgs e)
- {
- this.comboBox_tk.DropDownStyle = ComboBoxStyle.DropDownList;
-
- //自动填充下拉框
- Database database = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
- using (Transaction traction = database.TransactionManager.StartTransaction())
- {
- //存放所有图层名
- List<string> alllayername = new List<string>();
- LayerTable layertable = traction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable;
- foreach (ObjectId objid in layertable)
- {
- LayerTableRecord layertablerecord = traction.GetObject(objid, OpenMode.ForRead) as LayerTableRecord;
- alllayername.Add(layertablerecord.Name);
- }
-
- alllayername.Sort();
-
- for (int i = 0; i < alllayername.Count; i++)
- {
- this.comboBox_tk.Items.Add(alllayername[i]);
- }
-
- if (alllayername.Contains("TK"))
- {
- int index = alllayername.IndexOf("TK");
- this.comboBox_tk.SelectedIndex = index;
- }
- traction.Commit();
- }
- }
-
- private void button_ok_Click(object sender, EventArgs e)
- {
- if (comboBox_tk.Text == "")
- {
- MessageBox.Show("请选择分幅图框所在图层");
- return;
- }
- if (textBox_sele.Text == "")
- {
- MessageBox.Show("请输入搜索阈值");
- return;
- }
- string tklyr = comboBox_tk.Text;
- joineg(tklyr);
- this.Close();
- MessageBox.Show("接边完成。");
- }
-
- private void joineg(string tklyr)
- {
- dynamic acadApp = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
- acadApp.ZoomExtents();
- Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
-
- //获取分幅图框
- TypedValue[] typedvalue = new TypedValue[2];
- typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, tklyr), 0);
- typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Polyline,LWPolyline"), 1);
- SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
- PromptSelectionResult psr = ed.SelectAll(selectionfilter);
-
- if (psr.Status == PromptStatus.OK)
- {
- SelectionSet selectionset = psr.Value;
- ObjectId[] obj = new ObjectId[selectionset.Count];
- obj = selectionset.GetObjectIds();
- List<ObjectId> selectedid = new List<ObjectId>();
- for (int i = 0; i < obj.Length; i++)
- {
- Polyline pll = new Polyline();
- //获取图幅框信息和与图幅框相交的实体
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- Entity ent0 = tr.GetObject(obj[i], OpenMode.ForRead) as Entity;
- //判断图框线类型,如果是polyline3d则转换为polyline进行后续操作
- if (ent0 is Polyline3d)
- {
- Polyline3d pl3d = (Polyline3d)ent0;
- foreach (ObjectId vertexId in pl3d)
- {
- PolylineVertex3d vertex = vertexId.GetObject(OpenMode.ForRead) as PolylineVertex3d;
- if (vertex != null)
- {
- pll.AddVertexAt(pll.NumberOfVertices, new Point2d(vertex.Position.X, vertex.Position.Y), 0, 0, 0);
- }
- }
- }
- else
- pll = (Polyline)ent0;
- Point3dCollection pts = getpt(ent0);
- selectedid = selebytk(pts, ed, ent0, tr);
- tr.Commit();
- }
- //遍历与图框相交的线,以实体与图框交点为中心做框选,如果有被选中的线则判断是否需要连接
- for (int j = 0; j < selectedid.Count; j++)
- {
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- if (selectedid[j].IsErased)
- continue;
- Entity ent = tr.GetObject(selectedid[j], OpenMode.ForWrite) as Entity;
- conn(pll, ent, tr, ed);
- tr.Commit();
- }
- }
- }
- }
- }
-
- private Point3dCollection getpt(Entity ent)
- {
- Point3dCollection ptcoll = new Point3dCollection();
- if (ent is Polyline)
- {
- Polyline pll = (Polyline)ent;
- //获取图框节点并排序
- int numVertices = pll.NumberOfVertices;
- for (int j = 0; j < numVertices; j++)
- {
- ptcoll.Add(pll.GetPoint3dAt(j));
- }
- sortpts(ptcoll);
- }
- else if (ent is Polyline2d)
- {
- Polyline2d pl2d = (Polyline2d)ent;
- pl2d.GetStretchPoints(ptcoll);
- sortpts(ptcoll);
- }
- else if (ent is Polyline3d)
- {
- Polyline3d pl3d = (Polyline3d)ent;
- pl3d.GetStretchPoints(ptcoll);
- sortpts(ptcoll);
- }
- return ptcoll;
- }
-
- private List<ObjectId> selebytk(Point3dCollection ptcoll, Editor ed,
- Entity ent0, Transaction tr)
- {
- //设置当前视图
- setwindow(ptcoll[0], ptcoll[2], ed);
-
- List<ObjectId> result = new List<ObjectId>();
-
- TypedValue[] typedvalue = new TypedValue[1];
- typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Polyline,LWPolyline,Hatch,Line"), 0);
- SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
- PromptSelectionResult psr = ed.SelectCrossingWindow(ptcoll[0], ptcoll[2], selectionfilter);
- if (psr.Status == PromptStatus.OK)
- {
- SelectionSet selectionset = psr.Value;
- ObjectId[] obj = new ObjectId[selectionset.Count];
- obj = selectionset.GetObjectIds();
-
- for (int i = 0; i < obj.Length; i++)
- {
- Entity ent = tr.GetObject(obj[i], OpenMode.ForRead) as Entity;
- //如果和图框在同一个图层就跳过
- if (ent.Layer == comboBox_tk.Text)
- continue;
- var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
- //选择与图框相交的线将objectid添加到list中
- var pts = new Point3dCollection();
- ent0.IntersectWith(ent, Intersect.OnBothOperands, plane, pts, IntPtr.Zero, IntPtr.Zero);
- if (pts.Count != 0)
- result.Add(ent.ObjectId);
- }
- }
- return result;
- }
-
- //连接分别位于两图框内的部分
- private void conn(Polyline pll,Entity ent, Transaction tr,Editor ed)
- {
- //搜索阈值
- double sele = double.Parse(textBox_sele.Text);
- //获取实体与图框交点,遍历每个点,以点为中心进行框选搜索是否存在与本实体相交的其他实体
- List<Point3d> pts = GetIntersections(ent, pll);
- List<ObjectId> idlist = new List<ObjectId>();
- foreach (var item in pts)
- {
- Point3d s_pt1 = new Point3d(item.X - sele, item.Y - sele, 0);
- Point3d s_pt2 = new Point3d(item.X + sele, item.Y + sele, 0);
- TypedValue[] typedvalue = new TypedValue[1];
- typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, ent.Layer), 0);
- SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
- PromptSelectionResult psr = ed.SelectCrossingWindow(s_pt1, s_pt2,selectionfilter);
- if (psr.Status == PromptStatus.OK)
- {
- SelectionSet selectionset = psr.Value;
- ObjectId[] obj = new ObjectId[selectionset.Count];
- obj = selectionset.GetObjectIds();
- foreach (var id in obj)
- {
- if (!idlist.Contains(id)&&id!=ent.ObjectId)
- idlist.Add(id);
- }
- }
- }
-
- //如果搜索得到的结果不为0,则判断实体是否符合连接条件,如果符合则连接
- if (idlist.Count!=0)
- {
- Tolerance tl = new Tolerance(sele, sele);
- for (int i = 0; i < idlist.Count; i++)
- {
- Entity ent1 = tr.GetObject(idlist[i], OpenMode.ForWrite) as Entity;
- //如果实体图层与图框图层相同,实体objectid与待连接实体相同,两实体类型不同,则不进行连接
- if (ent1.Layer == comboBox_tk.Text || ent.ObjectId == ent1.ObjectId ||
- ent.GetType().Name != ent1.GetType().Name|| IsEntityClosed(ent1)!=IsEntityClosed(ent))
- continue;
- //闭合实体与非闭合实体采用不同的方法连接
- else if (IsEntityClosed(ent1) && IsEntityClosed(ent))
- {
- pllmerge(ent, ent1, tr);
- }
- else
- {
- if (ent is Polyline)
- {
- Polyline pl1 = (Polyline)ent;
- Polyline pl2 = (Polyline)ent1;
- if (pl1.Length < sele)
- return;
- else if (pl2.Length < sele)
- continue;
- //判断两实体是否在同一图框
- var overlay = IsOverlay(ent, ent1, pll, tl);
- //如果两线高程不同,两线首尾点没有重合点,两线有重合则不相连
- if (pl1.Elevation != pl2.Elevation || (!pl1.StartPoint.IsEqualTo(pl2.StartPoint,tl) &&
- !pl1.StartPoint.IsEqualTo(pl2.EndPoint, tl) && !pl1.EndPoint.IsEqualTo(pl2.StartPoint, tl) &&
- !pl1.EndPoint.IsEqualTo(pl2.EndPoint, tl)) || overlay)
- continue;
- }
- else if (ent is Polyline2d)
- {
- Polyline2d pl1 = (Polyline2d)ent;
- Polyline2d pl2 = (Polyline2d)ent1;
- if (pl1.Length < sele)
- return;
- else if (pl2.Length < sele)
- continue;
- //判断两实体是否在同一图框
- var overlay = IsOverlay(ent, ent1, pll,tl);
- //如果两线高程不同,两线首尾点没有重合点,两线有重合则不相连
- if (pl1.Elevation != pl2.Elevation || (!pl1.StartPoint.IsEqualTo(pl2.StartPoint, tl) &&
- !pl1.StartPoint.IsEqualTo(pl2.EndPoint, tl) && !pl1.EndPoint.IsEqualTo(pl2.StartPoint, tl) &&
- !pl1.EndPoint.IsEqualTo(pl2.EndPoint, tl)) || overlay)
- continue;
- }
- else if (ent is Polyline3d)
- {
- Polyline3d pl1 = (Polyline3d)ent;
- Polyline3d pl2 = (Polyline3d)ent1;
- if (pl1.Length < sele)
- return;
- else if (pl2.Length < sele)
- continue;
- //判断两实体是否在同一图框
- var overlay = IsOverlay(ent, ent1, pll,tl);
- //如果两线高程不同,两线首尾点没有重合点,两线有重合则不相连
- if ((!pl1.StartPoint.IsEqualTo(pl2.StartPoint, tl) &&!pl1.StartPoint.IsEqualTo(pl2.EndPoint, tl) &&
- !pl1.EndPoint.IsEqualTo(pl2.StartPoint, tl) &&!pl1.EndPoint.IsEqualTo(pl2.EndPoint, tl)) ||
- overlay)
- continue;
- }
- else if (ent is Line)
- {
- Line pl1 = (Line)ent;
- Line pl2 = (Line)ent1;
- if (pl1.Length < sele)
- return;
- else if (pl2.Length < sele)
- continue;
- //判断两实体是否在同一图框
- bool isoverlay = IsOverlay(pl1, pl2, pll,tl);
- //如果两线高程不同,两线首尾点没有重合点,两线有重合则不相连
- if ((!pl1.StartPoint.IsEqualTo(pl2.StartPoint, tl) &&!pl1.StartPoint.IsEqualTo(pl2.EndPoint, tl) &&
- !pl1.EndPoint.IsEqualTo(pl2.StartPoint, tl) &&!pl1.EndPoint.IsEqualTo(pl2.EndPoint, tl)) ||
- isoverlay)
- continue;
- }
- Entity ent_con = CreateConnOrNot(ent, ent1);
- ent_con.JoinEntity(ent1);
- ent1.Erase();
- }
- }
- }
- }
-
- //将闭合实体转换为region,使用布尔运算进行合并后再将合并后的region转换为原类型
- private void pllmerge(Entity ent1, Entity ent2, Transaction tr)
- {
- try
- {
- Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
- BlockTable newBt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
- BlockTableRecord newBtr = tr.GetObject(newBt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
- // 创建Region集合
- DBObjectCollection plines = new DBObjectCollection();
- if (ent1 is Polyline)
- {
- Polyline pl1 = (Polyline)ent1;
- Polyline pl2 = (Polyline)ent2;
- if (pl1.Elevation != pl2.Elevation)
- return;
- plines.Add(pl1);
- plines.Add(pl2);
- }
- else if (ent1 is Polyline2d)
- {
- Polyline2d pl1 = (Polyline2d)ent1;
- Polyline2d pl2 = (Polyline2d)ent2;
- if (pl1.Elevation != pl2.Elevation)
- return;
- plines.Add(pl1);
- plines.Add(pl2);
- }
- else if (ent1 is Polyline3d)
- {
- Polyline3d pl1 = (Polyline3d)ent1;
- Polyline3d pl2 = (Polyline3d)ent2;
- plines.Add(pl1);
- plines.Add(pl2);
- }
- else if (ent1 is Hatch)
- {
- Hatch hc1 = (Hatch)ent1;
- Hatch hc2 = (Hatch)ent2;
- if (hc1.PatternName != hc2.PatternName)
- return;
- for (int i = 0; i < hc1.NumberOfLoops; i++)
- {
- HatchLoop loop = hc1.GetLoopAt(i);
-
- if (loop.Curves == null)
- {
- Polyline polyline = new Polyline();
-
- // 遍历顶点并添加
- for (int ii = 0; ii < loop.Polyline.Count; ii++)
- {
- BulgeVertex vertex = loop.Polyline[ii];
- polyline.AddVertexAt(i, vertex.Vertex, vertex.Bulge, 0, 0);
- }
- polyline.Closed = true; // 闭合多段线
- plines.Add(polyline);
- }
- else
- {
- foreach (Curve curve in loop.Curves)
- {
- plines.Add(curve);
- }
- }
- }
- for (int i = 0; i < hc2.NumberOfLoops; i++)
- {
- HatchLoop loop = hc2.GetLoopAt(i);
-
- if (loop.Curves == null)
- {
- Polyline polyline = new Polyline();
-
- // 遍历顶点并添加
- for (int ii = 0; ii < loop.Polyline.Count; ii++)
- {
- BulgeVertex vertex = loop.Polyline[ii];
- polyline.AddVertexAt(i, vertex.Vertex, vertex.Bulge, 0, 0);
- }
- polyline.Closed = true; // 闭合多段线
- plines.Add(polyline);
- }
- else
- {
- foreach (Curve curve in loop.Curves)
- {
- plines.Add(curve);
- }
- }
- }
- }
-
- // 从多段线创建Region
- DBObjectCollection regions = Autodesk.AutoCAD.DatabaseServices.Region.CreateFromCurves(plines);
-
- // 合并Regions
- Autodesk.AutoCAD.DatabaseServices.Region mergedRegion = regions[0] as Autodesk.AutoCAD.DatabaseServices.Region;
- for (int i = 1; i < regions.Count; i++)
- {
- mergedRegion.BooleanOperation(BooleanOperationType.BoolUnite, regions[i] as Autodesk.AutoCAD.DatabaseServices.Region);
- }
- //炸开region以便转换回原本的类型
- DBObjectCollection explodedCurves = new DBObjectCollection();
- mergedRegion.Explode(explodedCurves);
- if (ent1 is Polyline)
- {
- Polyline polyline = createpllfromregion(explodedCurves);
- Polyline pl = (Polyline)ent1;
- polyline.Elevation = pl.Elevation;
- polyline.Layer = pl.Layer;
- polyline.Color = pl.Color;
- polyline.Linetype = pl.Linetype;
- polyline.Plinegen = pl.Plinegen;
- polyline.ConstantWidth = pl.ConstantWidth;
- newBtr.AppendEntity(polyline);
- tr.AddNewlyCreatedDBObject(polyline, true);
- }
- else if (ent1 is Polyline2d)
- {
- Polyline2d polyline2d = createpl2dfromregion(explodedCurves);
- Polyline2d pl2d = (Polyline2d)ent1;
- polyline2d.Elevation = pl2d.Elevation;
- polyline2d.Layer = pl2d.Layer;
- polyline2d.Color = pl2d.Color;
- polyline2d.Linetype = pl2d.Linetype;
- polyline2d.ConstantWidth = pl2d.ConstantWidth;
- newBtr.AppendEntity(polyline2d);
- tr.AddNewlyCreatedDBObject(polyline2d, true);
- }
- else if (ent1 is Polyline3d)
- {
- Polyline3d pline3d = createpl3dfromregion(explodedCurves);
- Polyline3d pl3d = (Polyline3d)ent1;
- pline3d.Layer = pl3d.Layer;
- pline3d.Color = pl3d.Color;
- pline3d.Linetype = pl3d.Linetype;
- pline3d.LineWeight = pl3d.LineWeight;
- newBtr.AppendEntity(pline3d);
- tr.AddNewlyCreatedDBObject(pline3d, true);
- }
- else if (ent1 is Hatch)
- {
- Hatch hc = (Hatch)ent1;
- ObjectIdCollection boundaryIds = new ObjectIdCollection();
- //hatch边缘
- Polyline regionpll = createpllfromregion(explodedCurves);
- newBtr.AppendEntity(regionpll);
- tr.AddNewlyCreatedDBObject(regionpll, true);
- boundaryIds.Add(regionpll.ObjectId);
- Hatch newHatch = new Hatch();
- newHatch.AppendLoop(HatchLoopTypes.Default, boundaryIds);
- newBtr.AppendEntity(newHatch);
- tr.AddNewlyCreatedDBObject(newHatch, true);
- string pname = hc.PatternName;//hatch填充图案名
- newHatch.SetHatchPattern(hc.PatternType, pname);
- newHatch.EvaluateHatch(true);
- regionpll.Erase();
- }
- ent1.Erase();
- ent2.Erase();
- }
- catch (Exception ex)
- {
- MessageBox.Show($"\n错误: {ex.Message}");
- }
- }
-
- //点排序
- private void sortpts(Point3dCollection ptcoll)
- {
- List<double> x = new List<double>();
- List<double> y = new List<double>();
- for (int i = 0; i < 4; i++)
- {
- if (!x.Contains(ptcoll[i].X))
- x.Add(ptcoll[i].X);
- if (!y.Contains(ptcoll[i].Y))
- y.Add(ptcoll[i].Y);
- }
- if (x[0] > x[1])
- {
- double temp = x[0];
- x[0] = x[1];
- x[1] = temp;
- }
- if (y[0] > y[1])
- {
- double temp = y[0];
- y[0] = y[1];
- y[1] = temp;
- }
- ptcoll[0] = new Point3d(Math.Round(x[0], 3, MidpointRounding.AwayFromZero), Math.Round(y[0], 3, MidpointRounding.AwayFromZero), 0); //左下
- ptcoll[1] = new Point3d(Math.Round(x[1], 3, MidpointRounding.AwayFromZero), Math.Round(y[0], 3, MidpointRounding.AwayFromZero), 0); //右下
- ptcoll[2] = new Point3d(Math.Round(x[1], 3, MidpointRounding.AwayFromZero), Math.Round(y[1], 3, MidpointRounding.AwayFromZero), 0); //右上
- ptcoll[3] = new Point3d(Math.Round(x[0], 3, MidpointRounding.AwayFromZero), Math.Round(y[1], 3, MidpointRounding.AwayFromZero), 0); //左上
- }
-
- //设置当前视图位置和大小
- private void setwindow(Point3d pt1, Point3d pt2, Editor ed)
- {
- //设置当前视图
- ViewTableRecord currview1 = ed.GetCurrentView();
- currview1.CenterPoint = new Point2d((pt2.X - pt1.X) / 2 + pt1.X,
- (pt2.Y - pt1.Y) / 2 + pt1.Y);
- currview1.Width = (pt2.X - pt1.X)*2;
- currview1.Height = (pt2.Y - pt1.Y)*2;
- ed.SetCurrentView(currview1);
- }
-
- //判断是否是闭合图形
- private bool IsEntityClosed(Entity ent)
- {
- switch (ent)
- {
- case Polyline pline:
- return pline.StartPoint== pline.EndPoint;
- case Polyline2d pline2d:
- return pline2d.StartPoint == pline2d.EndPoint;
- case Polyline3d pline3d:
- return pline3d.StartPoint == pline3d.EndPoint;
- case Spline spline:
- return spline.Closed;
- case Circle _:
- return true;
- case Ellipse ellipse:
- return Math.Abs(ellipse.EndAngle - ellipse.StartAngle) >= 2 * Math.PI - 1e-6;
- case Autodesk.AutoCAD.DatabaseServices.Region _:
- case Hatch _:
- return true; // 区域和填充图案总是闭合的
- case Line _:
- case Arc _:
- case Ray _:
- case Xline _:
- return false; // 这些实体总是开放的
- default:
- // 对于其他实体类型,检查起点和终点是否重合
- if (ent is Curve curve)
- {
- return curve.StartPoint.DistanceTo(curve.EndPoint) < Tolerance.Global.EqualPoint;
- }
- return false;
- }
- }
-
- //分别获取两实体交点旁边一点,判断该点在图框内还是外,如果两点位置关系相同,说明两实体在同侧,
- //不需要连接,返回true,否则返回false
- private bool IsOverlay(Entity ent1,Entity ent2,Polyline pl,Tolerance tl)
- {
- Point3d region_max = pl.Bounds.Value.MaxPoint;
- Point3d region_min = pl.Bounds.Value.MinPoint;
- bool ent1_inside;
- bool ent2_inside;
- Point3d pt1 = new Point3d();
- Point3d pt2 = new Point3d();
- Point3d compare = pt1;
- List<Point3d> pl1_pts = new List<Point3d>();
- List<Point3d> pl2_pts = new List<Point3d>();
- if (ent1 is Polyline)
- {
- Polyline pl1 = (Polyline)ent1;
- Polyline pl2 = (Polyline)ent2;
- pl1_pts.Add(pl1.StartPoint);
- pl1_pts.Add(pl1.EndPoint);
- pl2_pts.Add(pl2.StartPoint);
- pl2_pts.Add(pl2.EndPoint);
- var result = Comparept(pl1_pts, pl2_pts, pl1, pl2, pt1, pt2, tl);
- pt1 = result[0];
- pt2 = result[1];
- }
- else if (ent1 is Polyline2d)
- {
- Polyline2d pl1 = (Polyline2d)ent1;
- Polyline2d pl2 = (Polyline2d)ent2;
- pl1_pts.Add(pl1.StartPoint);
- pl1_pts.Add(pl1.EndPoint);
- pl2_pts.Add(pl2.StartPoint);
- pl2_pts.Add(pl2.EndPoint);
- var result = Comparept(pl1_pts, pl2_pts, pl1, pl2, pt1, pt2, tl);
- pt1 = result[0];
- pt2 = result[1];
- }
- else if (ent1 is Polyline3d)
- {
- Polyline3d pl1 = (Polyline3d)ent1;
- Polyline3d pl2 = (Polyline3d)ent2;
- pl1_pts.Add(pl1.StartPoint);
- pl1_pts.Add(pl1.EndPoint);
- pl2_pts.Add(pl2.StartPoint);
- pl2_pts.Add(pl2.EndPoint);
- var result = Comparept(pl1_pts, pl2_pts, pl1, pl2, pt1, pt2, tl);
- pt1 = result[0];
- pt2 = result[1];
- }
- else
- {
- Line pl1 = (Line)ent1;
- Line pl2 = (Line)ent2;
- pl1_pts.Add(pl1.StartPoint);
- pl1_pts.Add(pl1.EndPoint);
- pl2_pts.Add(pl2.StartPoint);
- pl2_pts.Add(pl2.EndPoint);
-
- for (int i = 0; i < pl1_pts.Count; i++)
- {
- for (int j = 0; j < pl2_pts.Count; j++)
- {
- if (pl1_pts[i].IsEqualTo(pl2_pts[j]))
- {
- if (i == 0)
- pt1 = pl1.GetPointAtParameter(pl1.StartParam + 1);
- else
- pt1 = pl1.GetPointAtParameter(pl1.EndParam - 1);
- if (j == 0)
- pt2 = pl2.GetPointAtParameter(pl2.StartParam + 1);
- else
- pt2 = pl2.GetPointAtParameter(pl2.EndParam - 1);
- }
- }
- }
- }
- //如果pt1还是初始值,说明在同侧,返回true
- if (pt1 == compare)
- return true;
- if (region_max.X >= pt1.X && region_max.Y >= pt1.Y &&
- region_min.X <= pt1.X && region_min.Y <= pt1.Y)
- ent1_inside = true;
- else
- ent1_inside = false;
- if (region_max.X >= pt2.X && region_max.Y >= pt2.Y &&
- region_min.X <= pt2.X && region_min.Y <= pt2.Y)
- ent2_inside = true;
- else
- ent2_inside = false;
- if (ent1_inside == ent2_inside)
- return true;
- else
- return false;
- }
-
- //若两实体最近的两点不重合但小于阈值,则做一条Line将两点连起来,再用jionentity连接三条线
- private Entity CreateConnOrNot(Entity ent1, Entity ent2)
- {
- Entity result;
- Point3d s1 = new Point3d();
- Point3d s2 = new Point3d();
- Point3d e1 = new Point3d();
- Point3d e2 = new Point3d();
- Line line = new Line();
- var comp = line;
- if (ent1 is Polyline)
- {
- Polyline pl1 = (Polyline)ent1;
- Polyline pl2 = (Polyline)ent2;
- s1 = pl1.StartPoint;
- s2 = pl2.StartPoint;
- e1 = pl1.EndPoint;
- e2 = pl2.EndPoint;
- }
- else if (ent1 is Polyline2d)
- {
- Polyline2d pl1 = (Polyline2d)ent1;
- Polyline2d pl2 = (Polyline2d)ent2;
- s1 = pl1.StartPoint;
- s2 = pl2.StartPoint;
- e1 = pl1.EndPoint;
- e2 = pl2.EndPoint;
- }
- else if (ent1 is Polyline3d)
- {
- Polyline3d pl1 = (Polyline3d)ent1;
- Polyline3d pl2 = (Polyline3d)ent2;
- s1 = pl1.StartPoint;
- s2 = pl2.StartPoint;
- e1 = pl1.EndPoint;
- e2 = pl2.EndPoint;
- }
- else
- {
- Line pl1 = (Line)ent1;
- Line pl2 = (Line)ent2;
- s1 = pl1.StartPoint;
- s2 = pl2.StartPoint;
- e1 = pl1.EndPoint;
- e2 = pl2.EndPoint;
- }
- List<Point3d> pts = new List<Point3d>() { s1, s2, e1, e2 };
- var groups = pts.GroupBy(p => p).Where(g => g.Count() >= 2).OrderByDescending(g => g.Count()).ToList();
- if (groups.Count == 0)
- {
- Line line1 = new Line(s1, s2);
- Line line2 = new Line(s1, e2);
- Line line3 = new Line(e1, s2);
- Line line4 = new Line(e1, e2);
- List<Line> lines = new List<Line>() { line1, line2, line3, line4 };
- var sortedlines = lines.OrderBy(x => x.Length).ToList();
- line = sortedlines[0];
- if (ent1 is Polyline)
- {
- Polyline pl1 = (Polyline)ent1;
- pl1.JoinEntity(line);
- result = pl1;
- return result;
- }
- else if (ent1 is Polyline2d)
- {
- Polyline2d pl1 = (Polyline2d)ent1;
- pl1.JoinEntity(line);
- result = pl1;
- return result;
- }
- else if (ent1 is Polyline3d)
- {
- Polyline3d pl1 = (Polyline3d)ent1;
- pl1.JoinEntity(line);
- result = pl1;
- return result;
- }
- else
- {
- Line l1 = (Line)ent1;
- l1.JoinEntity(line);
- result = l1;
- return result;
- }
-
- }
- else
- return ent1;
- }
-
- //将polyline、polyline2d、polyline3d转换为region
- public Polyline createpllfromregion(DBObjectCollection explodedCurves)
- {
- // 存储提取的线段
- List<Curve> curves = new List<Curve>();
- foreach (DBObject obj in explodedCurves)
- {
- if (obj is Curve curve)
- {
- curves.Add(curve);
- }
- }
-
- Polyline polyline = new Polyline();
- polyline.SetDatabaseDefaults();
- while (curves.Count > 0)
- {
- // 从第一条线段开始
- Curve currentCurve = curves[0];
- curves.RemoveAt(0);
-
- // 添加当前线段的起点和终点
- polyline.AddVertexAt(0, new Point2d(currentCurve.StartPoint.X, currentCurve.StartPoint.Y), 0, 0, 0);
- polyline.AddVertexAt(1, new Point2d(currentCurve.EndPoint.X, currentCurve.EndPoint.Y), 0, 0, 0);
-
- // 查找与当前线段相连的线段
- bool foundConnectedCurve;
- do
- {
- foundConnectedCurve = false;
-
- for (int i = 0; i < curves.Count; i++)
- {
- Curve nextCurve = curves[i];
-
- // 检查是否与当前线段的起点或终点相连
- if (nextCurve.StartPoint.IsEqualTo(polyline.GetPoint3dAt(polyline.NumberOfVertices - 1)))
- {
- // 添加到多段线的末尾
- polyline.AddVertexAt(polyline.NumberOfVertices, new Point2d(nextCurve.EndPoint.X, nextCurve.EndPoint.Y), 0, 0, 0);
- curves.RemoveAt(i);
- foundConnectedCurve = true;
- break;
- }
- else if (nextCurve.EndPoint.IsEqualTo(polyline.GetPoint3dAt(0)))
- {
- // 添加到多段线的开头
- polyline.AddVertexAt(0, new Point2d(nextCurve.StartPoint.X, nextCurve.StartPoint.Y), 0, 0, 0);
- curves.RemoveAt(i);
- foundConnectedCurve = true;
- break;
- }
- else if (nextCurve.EndPoint.IsEqualTo(polyline.GetPoint3dAt(polyline.NumberOfVertices - 1)))
- {
- // 添加到多段线的末尾
- polyline.AddVertexAt(polyline.NumberOfVertices, new Point2d(nextCurve.StartPoint.X, nextCurve.StartPoint.Y), 0, 0, 0);
- curves.RemoveAt(i);
- foundConnectedCurve = true;
- break;
- }
- else if (nextCurve.StartPoint.IsEqualTo(polyline.GetPoint3dAt(0)))
- {
- // 添加到多段线的开头
- polyline.AddVertexAt(0, new Point2d(nextCurve.EndPoint.X, nextCurve.EndPoint.Y), 0, 0, 0);
- curves.RemoveAt(i);
- foundConnectedCurve = true;
- break;
- }
- }
- } while (foundConnectedCurve);
-
- // 关闭多段线
- polyline.Closed = true;
- }
- return polyline;
- }
-
- public Polyline2d createpl2dfromregion(DBObjectCollection explodedCurves)
- {
- // 存储提取的线段
- List<Curve> curves = new List<Curve>();
- foreach (DBObject obj in explodedCurves)
- {
- if (obj is Curve curve)
- {
- curves.Add(curve);
- }
- }
-
- Polyline2d pl2d = new Polyline2d();
- pl2d.SetDatabaseDefaults();
- while (curves.Count > 0)
- {
- // 从第一条线段开始
- Curve currentCurve = curves[0];
- curves.RemoveAt(0);
-
- // 添加当前线段的起点和终点
- Vertex2d firstVertex = new Vertex2d(currentCurve.StartPoint, 0, 0, 0, 0);
- Vertex2d secondVertex = new Vertex2d(currentCurve.EndPoint, 0, 0, 0, 0);
- pl2d.AppendVertex(firstVertex);
- pl2d.AppendVertex(secondVertex);
-
- // 查找与当前线段相连的线段
- bool foundConnectedCurve;
- do
- {
- foundConnectedCurve = false;
-
- for (int i = 0; i < curves.Count; i++)
- {
- Curve nextCurve = curves[i];
-
- // 检查是否与当前线段的起点或终点相连
- if (nextCurve.StartPoint.IsEqualTo(pl2d.EndPoint))
- {
- // 添加到多段线的末尾
- Vertex2d nextVertex = new Vertex2d(nextCurve.EndPoint, 0, 0, 0, 0);
- pl2d.AppendVertex(nextVertex);
- curves.RemoveAt(i);
- foundConnectedCurve = true;
- break;
- }
- else if (nextCurve.EndPoint.IsEqualTo(pl2d.StartPoint))
- {
- // 添加到多段线的开头
- Vertex2d nextVertex = new Vertex2d(nextCurve.StartPoint, 0, 0, 0, 0);
- pl2d.AppendVertex(nextVertex);
- curves.RemoveAt(i);
- foundConnectedCurve = true;
- break;
- }
- else if (nextCurve.EndPoint.IsEqualTo(pl2d.EndPoint))
- {
- // 添加到多段线的末尾StartPoint
- Vertex2d nextVertex = new Vertex2d(nextCurve.StartPoint, 0, 0, 0, 0);
- pl2d.AppendVertex(nextVertex);
- curves.RemoveAt(i);
- foundConnectedCurve = true;
- break;
- }
- else if (nextCurve.StartPoint.IsEqualTo(pl2d.StartPoint))
- {
- // 添加到多段线的开头
- Vertex2d nextVertex = new Vertex2d(nextCurve.EndPoint, 0, 0, 0, 0);
- pl2d.AppendVertex(nextVertex);
- curves.RemoveAt(i);
- foundConnectedCurve = true;
- break;
- }
- }
- } while (foundConnectedCurve);
-
- // 关闭多段线
- pl2d.Closed = true;
- }
- return pl2d;
- }
-
- public Polyline3d createpl3dfromregion(DBObjectCollection explodedCurves)
- {
- // 存储提取的线段
- List<Curve> curves = new List<Curve>();
- foreach (DBObject obj in explodedCurves)
- {
- if (obj is Curve curve)
- {
- curves.Add(curve);
- }
- }
-
- Polyline3d pl3d = new Polyline3d();
- pl3d.SetDatabaseDefaults();
- while (curves.Count > 0)
- {
- // 从第一条线段开始
- Curve currentCurve = curves[0];
- curves.RemoveAt(0);
-
- // 添加当前线段的起点和终点
- pl3d.AppendVertex(new PolylineVertex3d(currentCurve.StartPoint));
- pl3d.AppendVertex(new PolylineVertex3d(currentCurve.EndPoint));
-
- // 查找与当前线段相连的线段
- bool foundConnectedCurve;
- do
- {
- foundConnectedCurve = false;
-
- for (int i = 0; i < curves.Count; i++)
- {
- Curve nextCurve = curves[i];
-
- // 检查是否与当前线段的起点或终点相连
- if (nextCurve.StartPoint.IsEqualTo(pl3d.EndPoint))
- {
- // 添加到多段线的末尾
- pl3d.AppendVertex(new PolylineVertex3d(nextCurve.EndPoint));
- curves.RemoveAt(i);
- foundConnectedCurve = true;
- break;
- }
- else if (nextCurve.EndPoint.IsEqualTo(pl3d.StartPoint))
- {
- // 添加到多段线的开头
- pl3d.AppendVertex(new PolylineVertex3d(nextCurve.StartPoint));
- curves.RemoveAt(i);
- foundConnectedCurve = true;
- break;
- }
- else if (nextCurve.EndPoint.IsEqualTo(pl3d.EndPoint))
- {
- // 添加到多段线的末尾
- pl3d.AppendVertex(new PolylineVertex3d(nextCurve.StartPoint));
- curves.RemoveAt(i);
- foundConnectedCurve = true;
- break;
- }
- else if (nextCurve.StartPoint.IsEqualTo(pl3d.StartPoint))
- {
- // 添加到多段线的开头
- pl3d.AppendVertex(new PolylineVertex3d(nextCurve.EndPoint));
- curves.RemoveAt(i);
- foundConnectedCurve = true;
- break;
- }
- }
- } while (foundConnectedCurve);
-
- // 关闭多段线
- pl3d.Closed = true;
- }
- return pl3d;
- }
-
- //为避免误差,将实体移动到原点附近进行相交检查,获取到的交点坐标再转换回原位置
- private List<Point3d> GetIntersections(Entity ent, Polyline pll)
- {
- List<Point3d> sort = new List<Point3d>();
-
- // 创建临时副本
- Entity ent1 = ent.Clone()as Entity;
- Polyline tempLine2 = (Polyline)pll.Clone();
-
- // 计算平移向量(以线1起点为基准,移动至原点)
- Vector3d translation = -tempLine2.StartPoint.GetAsVector();
- Matrix3d translateMatrix = Matrix3d.Displacement(translation);
-
- // 应用平移变换
- ent1.TransformBy(translateMatrix);
- tempLine2.TransformBy(translateMatrix);
-
- Point3dCollection translatedPoints = new Point3dCollection();
- var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
- if(ent1 is Polyline)
- {
- Polyline tempLine1 = (Polyline)ent1;
- tempLine1.IntersectWith(tempLine2, Intersect.OnBothOperands, plane, translatedPoints, IntPtr.Zero, IntPtr.Zero);
- }
- else if (ent1 is Polyline2d)
- {
- Polyline2d tempLine1 = (Polyline2d)ent1;
- tempLine1.IntersectWith(tempLine2, Intersect.OnBothOperands, plane, translatedPoints, IntPtr.Zero, IntPtr.Zero);
- }
- else if(ent1 is Polyline3d)
- {
- Polyline3d tempLine1 = (Polyline3d)ent1;
- tempLine1.IntersectWith(tempLine2, Intersect.OnBothOperands, plane, translatedPoints, IntPtr.Zero, IntPtr.Zero);
- }
- else if(ent1 is Line)
- {
- Line tempLine1 = (Line)ent1;
- tempLine1.IntersectWith(tempLine2, Intersect.OnBothOperands, plane, translatedPoints, IntPtr.Zero, IntPtr.Zero);
- }
- else if(ent1 is Hatch)
- {
- Hatch hc = (Hatch)ent1;
- hc.IntersectWith(tempLine2, Intersect.OnBothOperands, plane, translatedPoints, IntPtr.Zero, IntPtr.Zero);
- }
-
-
- // 计算逆平移矩阵
- Matrix3d inverseMatrix = Matrix3d.Displacement(-translation);
- if (translatedPoints.Count > 0)
- {
- // 获取平移后的交点
- foreach (Point3d item in translatedPoints)
- {
- // 恢复交点坐标
- var pt = item.TransformBy(inverseMatrix);
- sort.Add(pt);
- }
- var sorted = sort.OrderBy(x => pll.GetParameterAtPoint(pll.GetClosestPointTo(x, false)));
- return sorted.ToList();
- }
- else
- return sort;
- }
-
- private List<Point3d> Comparept(List<Point3d> pl1_pts, List<Point3d> pl2_pts, Polyline pl1, Polyline pl2,
- Point3d pt1, Point3d pt2,Tolerance tl)
- {
- List<Point3d> result;
- for (int i = 0; i < pl1_pts.Count; i++)
- {
- for (int j = 0; j < pl2_pts.Count; j++)
- {
- if (pl1_pts[i].IsEqualTo(pl2_pts[j],tl))
- {
- if (i == 0)
- {
- int num = 1;
- do
- {
- pt1 = pl1.GetPointAtParameter(pl1.StartParam + num);
- num++;
- }
- while (pl1.GetDistAtPoint(pt1) <= tl.EqualPoint && num < pl1.NumberOfVertices);
-
- }
- else
- {
- int num = 1;
- do
- {
- pt1 = pl1.GetPointAtParameter(pl1.EndParam - num);
- num++;
- var t = pl1.Length - pl1.GetDistAtPoint(pt1);
- }
- while ((pl1.Length - pl1.GetDistAtPoint(pt1)) <= tl.EqualPoint && num < pl1.NumberOfVertices);
-
- }
- if (j == 0)
- {
- int num = 1;
- do
- {
- pt2 = pl2.GetPointAtParameter(pl2.StartParam + num);
- num++;
- }
- while (pl2.GetDistAtPoint(pt2) <= tl.EqualPoint && num < pl2.NumberOfVertices);
-
- }
- else
- {
- int num = 1;
- do
- {
- pt2 = pl2.GetPointAtParameter(pl2.EndParam - num);
- num++;
- }
- while ((pl2.Length - pl2.GetDistAtPoint(pt2)) <= tl.EqualPoint && num < pl2.NumberOfVertices);
-
- }
- }
- }
- }
- result = new List<Point3d>() { pt1, pt2 };
- return result;
- }
- private List<Point3d> Comparept(List<Point3d> pl1_pts, List<Point3d> pl2_pts, Polyline2d pl1, Polyline2d pl2,
- Point3d pt1, Point3d pt2, Tolerance tl)
- {
- List<Point3d> result;
- int pt_num1 = 0;
- int pt_num2 = 0;
- foreach (ObjectId id in pl1)
- pt_num1++;
- foreach (ObjectId id in pl2)
- pt_num2++;
- for (int i = 0; i < pl1_pts.Count; i++)
- {
- for (int j = 0; j < pl2_pts.Count; j++)
- {
- if (pl1_pts[i].IsEqualTo(pl2_pts[j], tl))
- {
- if (i == 0)
- {
- int num = 1;
- do
- {
- pt1 = pl1.GetPointAtParameter(pl1.StartParam + num);
- num++;
- }
- while (pl1.GetDistAtPoint(pt1) <= tl.EqualPoint && num < pt_num1);
-
- }
- else
- {
- int num = 1;
- do
- {
- pt1 = pl1.GetPointAtParameter(pl1.EndParam - num);
- num++;
- var t = pl1.Length - pl1.GetDistAtPoint(pt1);
- }
- while ((pl1.Length - pl1.GetDistAtPoint(pt1)) <= tl.EqualPoint && num < pt_num1);
-
- }
- if (j == 0)
- {
- int num = 1;
- do
- {
- pt2 = pl2.GetPointAtParameter(pl2.StartParam + num);
- num++;
- }
- while (pl2.GetDistAtPoint(pt2) <= tl.EqualPoint && num < pt_num2);
-
- }
- else
- {
- int num = 1;
- do
- {
- pt2 = pl2.GetPointAtParameter(pl2.EndParam - num);
- num++;
- }
- while ((pl2.Length - pl2.GetDistAtPoint(pt2)) <= tl.EqualPoint && num < pt_num2);
-
- }
- }
- }
- }
- result = new List<Point3d>() { pt1, pt2 };
- return result;
- }
- private List<Point3d> Comparept(List<Point3d> pl1_pts, List<Point3d> pl2_pts, Polyline3d pl1, Polyline3d pl2,
- Point3d pt1, Point3d pt2, Tolerance tl)
- {
- List<Point3d> result;
- int pt_num1 = 0;
- int pt_num2 = 0;
- foreach (ObjectId id in pl1)
- pt_num1++;
- foreach (ObjectId id in pl2)
- pt_num2++;
- for (int i = 0; i < pl1_pts.Count; i++)
- {
- for (int j = 0; j < pl2_pts.Count; j++)
- {
- if (pl1_pts[i].IsEqualTo(pl2_pts[j], tl))
- {
- if (i == 0)
- {
- int num = 1;
- do
- {
- pt1 = pl1.GetPointAtParameter(pl1.StartParam + num);
- num++;
- }
- while (pl1.GetDistAtPoint(pt1) <= tl.EqualPoint && num < pt_num1);
-
- }
- else
- {
- int num = 1;
- do
- {
- pt1 = pl1.GetPointAtParameter(pl1.EndParam - num);
- num++;
- var t = pl1.Length - pl1.GetDistAtPoint(pt1);
- }
- while ((pl1.Length - pl1.GetDistAtPoint(pt1)) <= tl.EqualPoint && num < pt_num1);
-
- }
- if (j == 0)
- {
- int num = 1;
- do
- {
- pt2 = pl2.GetPointAtParameter(pl2.StartParam + num);
- num++;
- }
- while (pl2.GetDistAtPoint(pt2) <= tl.EqualPoint && num < pt_num2);
-
- }
- else
- {
- int num = 1;
- do
- {
- pt2 = pl2.GetPointAtParameter(pl2.EndParam - num);
- num++;
- }
- while ((pl2.Length - pl2.GetDistAtPoint(pt2)) <= tl.EqualPoint && num < pt_num2);
-
- }
- }
- }
- }
- result = new List<Point3d>() { pt1, pt2 };
- return result;
- }
- }
- }
|