using GrxCAD.ApplicationServices; using GrxCAD.DatabaseServices; using GrxCAD.EditorInput; using GrxCAD.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 HCTools { public partial class OutputbyTK_Form : Form { public OutputbyTK_Form() { InitializeComponent(); } private void OutputbyTK_Form_Load(object sender, EventArgs e) { this.comboBox_tk.DropDownStyle = ComboBoxStyle.DropDownList; this.comboBox_num.DropDownStyle = ComboBoxStyle.DropDownList; //自动填充下拉框 Database database = GrxCAD.DatabaseServices.HostApplicationServices.WorkingDatabase; using (Transaction traction = database.TransactionManager.StartTransaction()) { //存放所有图层名 List alllayername = new List(); 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]); this.comboBox_num.Items.Add(alllayername[i]); } if (alllayername.Contains("TK")) { int index = alllayername.IndexOf("TK"); this.comboBox_tk.SelectedIndex = index; } traction.Commit(); } } private void button_sele_Click(object sender, EventArgs e) { FolderBrowserDialog open = new FolderBrowserDialog(); open.Description = "请选择文件夹"; if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK) textBox_output.Text = open.SelectedPath; } private void button_ok_Click(object sender, EventArgs e) { if(comboBox_tk.Text=="") { MessageBox.Show("请选择分幅图框所在图层"); return; } if (comboBox_num.Text == "") { MessageBox.Show("请选择分幅图号所在图层"); return; } if (textBox_output.Text=="") { MessageBox.Show("请选择文件输出位置"); return; } string lyr = comboBox_tk.SelectedItem.ToString(); string output = textBox_output.Text.ToString(); cutline(lyr, output); this.Close(); } private void cutline(string lyr,string output) { dynamic acadApp = GrxCAD.ApplicationServices.Application.AcadApplication; acadApp.ZoomExtents(); Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; List tfh = new List();//图幅号 List ptcolls = new List();//图幅框节点 //List tks = new List();//图幅框 //获取分幅图框 TypedValue[] typedvalue = new TypedValue[2]; typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, lyr), 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(); for (int i = 0; i < obj.Length; i++) { Database db = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database; DBObjectCollection objcoll = new DBObjectCollection();//与图幅框相交的实体 //获取图幅框信息和与图幅框相交的实体 using (Transaction tr = db.TransactionManager.StartTransaction()) { Entity ent0 = tr.GetObject(obj[i], OpenMode.ForRead) as Entity; Point3dCollection pts = getpt(ent0); ptcolls.Add(pts); string num = ""; //获取图幅号 TypedValue[] typed_T = new TypedValue[2]; typed_T.SetValue(new TypedValue((int)DxfCode.LayerName, comboBox_num.SelectedItem.ToString()), 0); typed_T.SetValue(new TypedValue((int)DxfCode.Start, "Text"), 1); SelectionFilter filter_T = new SelectionFilter(typed_T); PromptSelectionResult psr_T = ed.SelectWindow(pts[0], pts[2], filter_T); if (psr_T.Status == PromptStatus.OK) { SelectionSet ss_T = psr_T.Value; ObjectId[] obj_T = new ObjectId[ss_T.Count]; obj_T = ss_T.GetObjectIds(); DBText T = tr.GetObject(obj_T[0], OpenMode.ForRead) as DBText; num = T.TextString.ToString(); } tfh.Add(num); ViewTableRecord currview = ed.GetCurrentView(); currview.CenterPoint = new Point2d((pts[2].X - pts[0].X) / 2 + pts[0].X, (pts[2].Y - pts[0].Y) / 2 + pts[0].Y); currview.ViewDirection = new Vector3d(0, 0, 1); currview.Width = pts[2].X - pts[0].X; currview.Height = pts[2].Y - pts[0].Y; ed.SetCurrentView(currview); PromptSelectionResult psr1 = ed.SelectCrossingPolygon(pts); if (psr1.Status == PromptStatus.OK) { SelectionSet ss = psr1.Value; ObjectId[] objids = new ObjectId[ss.Count]; objids = ss.GetObjectIds(); for (int ii = 0; ii < objids.Length; ii++) { Entity ent = tr.GetObject(objids[ii], OpenMode.ForRead) as Entity; if (ent.Layer != lyr && ent.Layer != comboBox_num.Text) objcoll.Add(ent); } } tr.Commit(); } //新建dwg string templatePath = "acad.dwt"; DocumentCollection documentcoll = GrxCAD.ApplicationServices.Application.DocumentManager; Document document = documentcoll.Add(templatePath); Database newDb = document.Database; newDb.Ltscale = db.Ltscale; //复制图层 using (Transaction traction = db.TransactionManager.StartTransaction()) { //存放所有图层名 List alllayername = new List(); LayerTable layertable = traction.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable; foreach (ObjectId objid in layertable) { LayerTableRecord layertablerecord = traction.GetObject(objid, OpenMode.ForRead) as LayerTableRecord; alllayername.Add(layertablerecord.Name); } LayerControl layerscontrol = new LayerControl(); for (int ii = 0; ii < alllayername.Count; ii++) { if (!layerscontrol.haslayername(alllayername[ii]) && alllayername[ii] != lyr && alllayername[ii] != comboBox_num.SelectedItem.ToString()) { //设置图层颜色 LayerTable layerTable = traction.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable; LayerTableRecord layerRecord = traction.GetObject(layerTable[alllayername[ii]], OpenMode.ForRead) as LayerTableRecord; colorgb col = new colorgb(layerRecord.Color.Red, layerRecord.Color.Green, layerRecord.Color.Blue); layerscontrol.creatlayer(alllayername[ii], col); } } traction.Commit(); } //复制与范围相交和在范围内的实体 ObjectIdCollection ps2ids = new ObjectIdCollection(); foreach (DBObject dbobj in objcoll) { ps2ids.Add(dbobj.ObjectId); } using (Transaction newtrans = newDb.TransactionManager.StartTransaction()) { BlockTable targetBlockTable = newtrans.GetObject(newDb.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord targetModelSpace = newtrans.GetObject(targetBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; using (IdMapping mapping = new IdMapping()) db.WblockCloneObjects(ps2ids, targetModelSpace.ObjectId, mapping, DuplicateRecordCloning.Replace, false); newtrans.Commit(); } //acadApp.ZoomExtents(); //设置当前视图 Editor edit = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; ViewTableRecord currview1 = ed.GetCurrentView(); currview1.CenterPoint = new Point2d((ptcolls[i][2].X - ptcolls[i][0].X) / 2 + ptcolls[i][0].X, (ptcolls[i][2].Y - ptcolls[i][0].Y) / 2 + ptcolls[i][0].Y); //currview1.ViewDirection = new Vector3d(0, 0, 1); currview1.Width = ptcolls[i][2].X - ptcolls[i][0].X; currview1.Height = ptcolls[i][2].Y - ptcolls[i][0].Y; edit.SetCurrentView(currview1); //按图框范围框选实体,因为浩辰的选择有问题,所以还要全选,对比两个选择集中的实体, //将漏选范围外实体删除。 PromptSelectionResult psr_intersect = edit.SelectCrossingPolygon(ptcolls[i]); PromptSelectionResult psr_all = edit.SelectAll(); if (psr_intersect.Status == PromptStatus.OK) { SelectionSet ss = psr_intersect.Value; SelectionSet ss_all = psr_all.Value; ObjectId[] objids = new ObjectId[ss.Count]; ObjectId[] objids_all = new ObjectId[ss_all.Count]; objids = ss.GetObjectIds(); objids_all = ss_all.GetObjectIds(); List outside = new List(); foreach (ObjectId id in objids_all) { outside.Add(id); } using (Transaction newtr = newDb.TransactionManager.StartTransaction()) { BlockTable newBt = newtr.GetObject(newDb.BlockTableId, OpenMode.ForWrite) as BlockTable; BlockTableRecord newBtr = newtr.GetObject(newBt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; Polyline line = new Polyline(); //画范围线 for (int ct = 0; ct < ptcolls[i].Count; ct++) { line.AddVertexAt(ct, new Point2d(ptcolls[i][ct].X, ptcolls[i][ct].Y), 0, 0, 0); } line.AddVertexAt(4, new Point2d(ptcolls[i][0].X, ptcolls[i][0].Y), 0, 0, 0); line.Color = GrxCAD.Colors.Color.FromColorIndex(GrxCAD.Colors.ColorMethod.ByColor, 1); line.Layer = "0"; line.SetDatabaseDefaults(); newBtr.AppendEntity(line); newtr.AddNewlyCreatedDBObject(line, true); //遍历实体,裁剪与范围相交的实体 for (int ii = 0; ii < objids.Length; ii++) { if (outside.Contains(objids[ii])) outside.Remove(objids[ii]); Entity ent = newtr.GetObject(objids[ii], OpenMode.ForWrite) as Entity; if (ent.Layer != lyr && ent.Layer != comboBox_num.Text.ToString()) copyclip(ent, line, newtr, newBtr, ptcolls[i]); else ent.Erase(); } if (outside.Count != 0) { foreach (ObjectId id in outside) { Entity ent_outside = newtr.GetObject(id, OpenMode.ForWrite) as Entity; ent_outside.Erase(); } } line.Erase(); newtr.Commit(); } } else if(psr_intersect.Status == PromptStatus.Error && psr_all.Status == PromptStatus.OK) { SelectionSet ss_all = psr_all.Value; ObjectId[] objids_all = new ObjectId[ss_all.Count]; objids_all = ss_all.GetObjectIds(); using (Transaction newtr = newDb.TransactionManager.StartTransaction()) { foreach(ObjectId id in objids_all) { Entity ent = newtr.GetObject(id, OpenMode.ForWrite)as Entity; ent.Erase(); } newtr.Commit(); } } acadApp.ZoomExtents(); //保存新DWG文件 string newFilePath = output + "\\" + tfh[i] + ".dwg"; newDb.SaveAs(newFilePath, true, DwgVersion.AC1021, newDb.SecurityParameters); newDb.Dispose(); document.CloseAndDiscard(); } } } /// /// 点排序 /// private void sortpts(Point3dCollection ptcoll) { List x = new List(); List y = new List(); 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 copyclip(Entity ent, Polyline pll, Transaction newtr, BlockTableRecord newBtr, Point3dCollection pts) { var plane = new Plane(Point3d.Origin, Vector3d.ZAxis); //多段线、二维多段线 if (ent is Polyline||ent is Polyline2d) { Polyline pll_clip = new Polyline(); pll_clip.SetDatabaseDefaults(); if (ent is Polyline2d) { Polyline2d pl2d = (Polyline2d)ent; int ptnum = 0; foreach (ObjectId vertexId in pl2d) { Vertex2d vtx = newtr.GetObject(vertexId, OpenMode.ForRead) as Vertex2d; Point2d point = new Point2d(pl2d.VertexPosition(vtx).X, pl2d.VertexPosition(vtx).Y); // 获取顶点坐标 double bulge = vtx.Bulge; // 获取凸度 pll_clip.AddVertexAt(ptnum, point, bulge, 0, 0); ptnum++; } pll_clip.Layer = pl2d.Layer; pll_clip.Color = pl2d.Color; pll_clip.Linetype = pl2d.Linetype; pll_clip.Elevation = pl2d.Elevation; pll_clip.Plinegen = pl2d.LinetypeGenerationOn; pll_clip.ConstantWidth = pl2d.ConstantWidth; } else pll_clip = (Polyline)ent; Point3dCollection Verptcoll = new Point3dCollection(); for (int i = 0; i < pll_clip.NumberOfVertices; i++) { double p_x = Math.Round(pll_clip.GetPoint3dAt(i).X, 3); double p_y = Math.Round(pll_clip.GetPoint3dAt(i).Y, 3); if (pts[0].X < p_x && p_x < pts[1].X && pts[0].Y < p_y && p_y < pts[2].Y) continue; else Verptcoll.Add(pll_clip.GetPoint3dAt(i)); } Point3dCollection ptcoll = new Point3dCollection(); pll_clip.IntersectWith(pll, Intersect.OnBothOperands, plane, ptcoll, IntPtr.Zero, IntPtr.Zero); if (Verptcoll.Count == pll_clip.NumberOfVertices && ptcoll.Count==0) { ent.Erase(); return; } Point3d pt1 = pll_clip.GetPoint3dAt(0); Point3d ptn = pll_clip.GetPoint3dAt(pll_clip.NumberOfVertices - 1); //非闭合 if (pll_clip.Closed == false && pt1!=ptn) { if (ptcoll.Count != 0) { DBObjectCollection objcoll = pll_clip.GetSplitCurves(ptcoll); foreach (Object obj in objcoll) { Polyline pll_cut = (Polyline)obj; pll_cut.Plinegen = pll_clip.Plinegen; if (pllinsidetk(pll_cut, pts)) { newBtr.AppendEntity(pll_cut); newtr.AddNewlyCreatedDBObject(pll_cut, true); } } ent.Erase(); } } //闭合 else { if (ptcoll.Count == 0) return; double ele = pll_clip.Elevation; pll_clip.Elevation = 0; // 将多段线转换为曲线集合 DBObjectCollection curvesToClip = new DBObjectCollection(); curvesToClip.Add(pll_clip); DBObjectCollection boundaryCurves = new DBObjectCollection(); boundaryCurves.Add(pll); // 创建 Region DBObjectCollection regionsToClip = new DBObjectCollection(); DBObjectCollection boundaryRegions = new DBObjectCollection(); try { // 将多段线转换为 Region regionsToClip = GrxCAD.DatabaseServices.Region.CreateFromCurves(curvesToClip); boundaryRegions = GrxCAD.DatabaseServices.Region.CreateFromCurves(boundaryCurves); } catch (Exception ex) { GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\n错误: {ex.Message}"); } // 执行布尔运算(交集) int deleindex = 0; foreach (GrxCAD.DatabaseServices.Region region in regionsToClip) { foreach (GrxCAD.DatabaseServices.Region boundary in boundaryRegions) { region.BooleanOperation(BooleanOperationType.BoolIntersect, boundary); DBObjectCollection explodedCurves = new DBObjectCollection(); region.Explode(explodedCurves); if (deleindex == 0 && explodedCurves.Count != 0) { pll_clip.Erase(); deleindex = 1; } foreach (DBObject obj in explodedCurves) { if (obj is GrxCAD.DatabaseServices.Region) { GrxCAD.DatabaseServices.Region subregion = (GrxCAD.DatabaseServices.Region)obj; DBObjectCollection subexplodedCurves = new DBObjectCollection(); subregion.Explode(subexplodedCurves); Polyline polyline = createpllfromregion(subexplodedCurves); polyline.Elevation = ele; polyline.Layer = pll_clip.Layer; polyline.Color = pll_clip.Color; polyline.Linetype = pll_clip.Linetype; polyline.Plinegen = pll_clip.Plinegen; polyline.ConstantWidth = polyline.ConstantWidth; newBtr.AppendEntity(polyline); newtr.AddNewlyCreatedDBObject(polyline, true); } else { Polyline polyline = createpllfromregion(explodedCurves); polyline.Elevation = ele; polyline.Layer = pll_clip.Layer; polyline.Color = pll_clip.Color; polyline.Linetype = pll_clip.Linetype; polyline.Plinegen = pll_clip.Plinegen; polyline.ConstantWidth = polyline.ConstantWidth; newBtr.AppendEntity(polyline); newtr.AddNewlyCreatedDBObject(polyline, true); break; } } } } } } //三维多段线 else if(ent is Polyline3d) { Polyline3d poly3d = (Polyline3d)ent; //Point3dCollection Verptcoll = new Point3dCollection(); //poly3d.GetStretchPoints(Verptcoll); //int num = 0; //for (int i = 0; i < Verptcoll.Count; i++) //{ // double p_x = Math.Round(Verptcoll[i].X, 3); // double p_y = Math.Round(Verptcoll[i].Y, 3); // if (pts[0].X < p_x && p_x < pts[1].X && // pts[0].Y < p_y && p_y < pts[2].Y) // continue; // else // num++; //} //if (num == Verptcoll.Count) //{ // ent.Erase(); // return; //} Point3dCollection ptcoll = new Point3dCollection(); poly3d.IntersectWith(pll, Intersect.OnBothOperands, plane, ptcoll, IntPtr.Zero, IntPtr.Zero); if (ptcoll.Count != 0) { DBObjectCollection objcoll = poly3d.GetSplitCurves(ptcoll); for (int i = 0; i < objcoll.Count; i++) { Polyline3d ply3 = (Polyline3d)objcoll[i]; double l_x = ply3.GetPointAtParameter(1).X; double l_y = ply3.GetPointAtParameter(1).Y; double l_sx = ply3.StartPoint.X; double l_sy = ply3.StartPoint.Y; double l_ex = ply3.EndPoint.X; double l_ey = ply3.EndPoint.Y; if (pts[0].X <= l_sx && l_sx <= pts[1].X && pts[0].Y <= l_sy && l_sy <= pts[2].Y && pts[0].X <= l_ex && l_ex <= pts[1].X && pts[0].Y <= l_ey && l_ey <= pts[2].Y && pts[0].X <= l_x && l_x <= pts[1].X && pts[0].Y <= l_y && l_y <= pts[2].Y) { newBtr.AppendEntity(ply3); newtr.AddNewlyCreatedDBObject(ply3, true); } } if (ptcoll.Count > 0) ent.Erase(); } } //直线 else if (ent is Line) { Point3dCollection ptcoll = new Point3dCollection(); Line ln = (Line)ent; double l_sx = Math.Round(ln.StartPoint.X, 3); double l_sy = Math.Round(ln.StartPoint.Y, 3); double l_ex = Math.Round(ln.EndPoint.X, 3); double l_ey = Math.Round(ln.EndPoint.Y, 3); if (pts[2].X <= l_sx && l_ex >= pts[2].X || pts[2].Y <= l_sy && l_ey >= pts[2].Y || pts[0].X >= l_ex && l_ex <= pts[0].X || pts[0].Y >= l_ey && l_ey <= pts[0].Y) { ent.Erase(); return; } ln.IntersectWith(pll, Intersect.OnBothOperands, plane, ptcoll, IntPtr.Zero, IntPtr.Zero); double l_x = ln.StartPoint.X; double l_y = ln.StartPoint.Y; Line line = new Line(); line.SetDatabaseDefaults(); line.Layer = ln.Layer; line.Color = ln.Color; line.Linetype = ln.Linetype; line.LineWeight = ln.LineWeight; if (ptcoll.Count == 1) { if (pts[0].X <= l_x && l_x <= pts[1].X && pts[0].Y <= l_y && l_y <= pts[2].Y) { line.StartPoint = ln.StartPoint; line.EndPoint = ptcoll[0]; newBtr.AppendEntity(line); newtr.AddNewlyCreatedDBObject(line, true); ln.Erase(); } else { line.StartPoint = ptcoll[0]; line.EndPoint = ln.EndPoint; newBtr.AppendEntity(line); newtr.AddNewlyCreatedDBObject(line, true); ln.Erase(); } } else if (ptcoll.Count >= 2) { for (int i = 0; i < ptcoll.Count - 1; i++) { line.StartPoint = ptcoll[i]; line.EndPoint = ptcoll[i + 1]; newBtr.AppendEntity(line); newtr.AddNewlyCreatedDBObject(line, true); } ln.Erase(); } } //图案填充 else if (ent is Hatch) { Hatch hc = (Hatch)ent; DBObjectCollection boundaries = new DBObjectCollection(); int num = 0; for (int i = 0; i < hc.NumberOfLoops; i++) { HatchLoop loop = hc.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); double v_x = Math.Round(vertex.Vertex.X); double v_y = Math.Round(vertex.Vertex.Y); if (pts[0].X <= v_x || v_x <= pts[1].X && pts[0].Y <= v_y || v_y <= pts[2].Y) num++; } polyline.Closed = true; // 闭合多段线 boundaries.Add(polyline); } else { foreach(Curve curve in loop.Curves) { boundaries.Add(curve); } } } if (num == 0) return; // 创建面域 DBObjectCollection regionsToClip = GrxCAD.DatabaseServices.Region.CreateFromCurves(boundaries); DBObjectCollection pllboundary = new DBObjectCollection(); pllboundary.Add(pll); DBObjectCollection boundaryRegions = GrxCAD.DatabaseServices.Region.CreateFromCurves(pllboundary); foreach (GrxCAD.DatabaseServices.Region region in regionsToClip) { foreach (GrxCAD.DatabaseServices.Region boundary in boundaryRegions) { region.BooleanOperation(BooleanOperationType.BoolIntersect, boundary); DBObjectCollection explodedCurves = new DBObjectCollection(); region.Explode(explodedCurves); int deleindex = 0; ObjectIdCollection boundaryIds = new ObjectIdCollection(); foreach (DBObject obj in explodedCurves) { if (obj is GrxCAD.DatabaseServices.Region) { GrxCAD.DatabaseServices.Region subregion = (GrxCAD.DatabaseServices.Region)obj; DBObjectCollection subexplodedCurves = new DBObjectCollection(); subregion.Explode(subexplodedCurves); Polyline regionpll = createpllfromregion(subexplodedCurves); newBtr.AppendEntity(regionpll); newtr.AddNewlyCreatedDBObject(regionpll, true); boundaryIds.Add(regionpll.ObjectId); } else { Polyline regionpll = createpllfromregion(explodedCurves); newBtr.AppendEntity(regionpll); newtr.AddNewlyCreatedDBObject(regionpll, true); boundaryIds.Add(regionpll.ObjectId); break; } } Hatch newHatch = new Hatch { PatternScale = hc.PatternScale, PatternAngle = hc.PatternAngle, HatchStyle = hc.HatchStyle, Color = hc.Color, Transparency = hc.Transparency, Layer = hc.Layer, Linetype = hc.Linetype, LinetypeScale = hc.LinetypeScale, LineWeight = hc.LineWeight, Associative = hc.Associative, HatchObjectType = hc.HatchObjectType }; newHatch.AppendLoop(HatchLoopTypes.Default, boundaryIds); newBtr.AppendEntity(newHatch); newtr.AddNewlyCreatedDBObject(newHatch, true); string pname = hc.PatternName; newHatch.SetHatchPattern(hc.PatternType, pname); newHatch.EvaluateHatch(true); if (deleindex == 0 && explodedCurves.Count != 0) { hc.UpgradeOpen(); hc.Erase(); deleindex = 1; } for (int i =0;i pts[2].X) { double dist = Math.Abs(pts[2].X - Math.Round(pt_max.X, 3)) + 0.1; Point3d targetPoint = new Point3d(blkef.Position.X - dist, blkef.Position.Y, blkef.Position.Z); Vector3d moveVector = targetPoint - basePoint; Matrix3d transform = Matrix3d.Displacement(moveVector); blkef.TransformBy(transform); } else if (Math.Round(pt_max.Y, 3) > pts[2].Y) { double dist = Math.Abs(pts[2].Y - Math.Round(pt_max.Y, 3)) + 0.1; Point3d targetPoint = new Point3d(blkef.Position.X, blkef.Position.Y - dist, blkef.Position.Z); Vector3d moveVector = targetPoint - basePoint; Matrix3d transform = Matrix3d.Displacement(moveVector); blkef.TransformBy(transform); } } //文字 else if(ent is DBText||ent is MText) { Point3d pt_min = ent.Bounds.Value.MinPoint; Point3d pt_max = ent.Bounds.Value.MaxPoint; Point3d basePoint = new Point3d(); double pos_x = 0; double pos_y = 0; double pos_z = 0; if (ptintk(pt_min, pts) && ptintk(pt_max, pts)) { ent.Erase(); return; } if (ent is DBText) { DBText dbt = (DBText)ent; basePoint = dbt.Position; pos_x = dbt.Position.X; pos_y = dbt.Position.Y; pos_z = dbt.Position.Z; } if (ent is MText) { MText mt = (MText)ent; basePoint = mt.Location; pos_x = mt.Location.X; pos_y = mt.Location.Y; pos_z = mt.Location.Z; } else if (Math.Round(pt_min.X, 3) < pts[0].X) { double dist = Math.Abs(pts[0].X - Math.Round(pt_min.X, 3)) + 0.1; Point3d targetPoint = new Point3d(pos_x + dist, pos_y, pos_z); Vector3d moveVector = targetPoint - basePoint; Matrix3d transform = Matrix3d.Displacement(moveVector); ent.TransformBy(transform); } else if (Math.Round(pt_min.Y, 3) < pts[0].Y) { double dist = Math.Abs(pts[0].Y - Math.Round(pt_min.Y, 3)) + 0.1; Point3d targetPoint = new Point3d(pos_x, pos_y + dist, pos_z); Vector3d moveVector = targetPoint - basePoint; Matrix3d transform = Matrix3d.Displacement(moveVector); ent.TransformBy(transform); } else if (Math.Round(pt_max.X, 3) > pts[2].X) { double dist = Math.Abs(pts[2].X - Math.Round(pt_max.X, 3)) + 0.1; Point3d targetPoint = new Point3d(pos_x - dist, pos_y, pos_z); Vector3d moveVector = targetPoint - basePoint; Matrix3d transform = Matrix3d.Displacement(moveVector); ent.TransformBy(transform); } else if (Math.Round(pt_max.Y, 3) > pts[2].Y) { double dist = Math.Abs(pts[2].Y - Math.Round(pt_max.Y, 3)) + 0.1; Point3d targetPoint = new Point3d(pos_x, pos_y - dist, pos_z); Vector3d moveVector = targetPoint - basePoint; Matrix3d transform = Matrix3d.Displacement(moveVector); ent.TransformBy(transform); } } #region //二维多段线 //else if (ent is Polyline2d) //{ // Point3dCollection ptcoll = new Point3dCollection(); // Polyline2d pll_clip = (Polyline2d)ent; // pll_clip.IntersectWith(pll, Intersect.OnBothOperands, plane, ptcoll, IntPtr.Zero, IntPtr.Zero); // //非闭合 // if (pll_clip.Closed == false) // { // if(ptcoll.Count!=0) // { // DBObjectCollection objcoll = pll_clip.GetSplitCurves(ptcoll); // foreach (Object obj in objcoll) // { // Polyline2d pll_cut = (Polyline2d)obj; // if (pllinsidetk(pll_cut, pts)) // { // newBtr.AppendEntity(pll_cut); // newtr.AddNewlyCreatedDBObject(pll_cut, true); // } // } // ent.Erase(); // } // } // //闭合 // else // { // double ele = pll_clip.Elevation; // pll_clip.Elevation = 0; // DBObjectCollection result = new DBObjectCollection(); // // 将多段线转换为曲线集合 // DBObjectCollection curvesToClip = new DBObjectCollection(); // curvesToClip.Add(pll_clip); // DBObjectCollection boundaryCurves = new DBObjectCollection(); // boundaryCurves.Add(pll); // // 创建 Region // DBObjectCollection regionsToClip = new DBObjectCollection(); // DBObjectCollection boundaryRegions = new DBObjectCollection(); // try // { // // 将多段线转换为 Region // regionsToClip = GrxCAD.DatabaseServices.Region.CreateFromCurves(curvesToClip); // boundaryRegions = GrxCAD.DatabaseServices.Region.CreateFromCurves(boundaryCurves); // } // catch (Exception ex) // { // GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\n错误: {ex.Message}"); // } // // 执行布尔运算(交集) // DBObjectCollection intersectionRegions = new DBObjectCollection(); // foreach (GrxCAD.DatabaseServices.Region region in regionsToClip) // { // foreach (GrxCAD.DatabaseServices.Region boundary in boundaryRegions) // { // region.BooleanOperation(BooleanOperationType.BoolIntersect, boundary); // DBObjectCollection explodedCurves = new DBObjectCollection(); // region.Explode(explodedCurves); // int flag = 0; // foreach (DBObject obj in explodedCurves) // { // if (obj is GrxCAD.DatabaseServices.Region) // { // flag = 1; // GrxCAD.DatabaseServices.Region subregion = (GrxCAD.DatabaseServices.Region)obj; // DBObjectCollection subexplodedCurves = new DBObjectCollection(); // subregion.Explode(subexplodedCurves); // Polyline2d polyline = createpll2dfromregion(subexplodedCurves, pll_clip, newtr); // polyline.Elevation = ele; // newBtr.AppendEntity(polyline); // newtr.AddNewlyCreatedDBObject(polyline, true); // } // else // { // break; // } // } // if (flag == 0) // { // Polyline2d polyline = createpll2dfromregion(explodedCurves, pll_clip, newtr); // polyline.Elevation = ele; // newBtr.AppendEntity(polyline); // newtr.AddNewlyCreatedDBObject(polyline, true); // } // } // } // if (regionsToClip.Count != 0) // { // pll_clip.Erase(); // } // } //} #endregion } public Polyline createpllfromregion(DBObjectCollection explodedCurves) { // 存储提取的线段 List curves = new List(); 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 Polyline3d createpl3dfromregion(DBObjectCollection explodedCurves) { // 存储提取的线段 List curves = new List(); 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 bool pllinsidetk(Entity ent, Point3dCollection pts) { bool inside = false; if (ent is Polyline) { Polyline pll = (Polyline)ent; double p_x = Math.Round(pll.GetPoint3dAt(1).X, 3); double p_y = Math.Round(pll.GetPoint3dAt(1).Y, 3); double sp_x = Math.Round(pll.StartPoint.X, 3); double sp_y = Math.Round(pll.StartPoint.Y, 3); double ep_x = Math.Round(pll.EndPoint.X, 3); double ep_y = Math.Round(pll.EndPoint.Y, 3); if (pts[0].X <= p_x && p_x <= pts[1].X && pts[0].Y <= p_y && p_y <= pts[2].Y && pts[0].X <= sp_x && sp_x <= pts[1].X && pts[0].Y <= sp_y && sp_y <= pts[2].Y && pts[0].X <= ep_x && ep_x <= pts[1].X && pts[0].Y <= ep_y && ep_y <= pts[2].Y) inside = true; else inside = false; } if (ent is Polyline3d) { Polyline2d pll = (Polyline2d)ent; double p_x = Math.Round(pll.GetPointAtParameter(1).X, 3); double p_y = Math.Round(pll.GetPointAtParameter(1).Y, 3); double sp_x = Math.Round(pll.StartPoint.X, 3); double sp_y = Math.Round(pll.StartPoint.Y, 3); double ep_x = Math.Round(pll.EndPoint.X, 3); double ep_y = Math.Round(pll.EndPoint.Y, 3); if (pts[0].X <= p_x && p_x <= pts[1].X && pts[0].Y <= p_y && p_y <= pts[2].Y && pts[0].X <= sp_x && sp_x <= pts[1].X && pts[0].Y <= sp_y && sp_y <= pts[2].Y && pts[0].X <= ep_x && ep_x <= pts[1].X && pts[0].Y <= ep_y && ep_y <= pts[2].Y) inside = true; else inside = false; } return inside; } 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 bool ptintk(Point3d pt,Point3dCollection pts) { bool inside; double x = Math.Round(pt.X, 3); double y = Math.Round(pt.Y, 3); if (pts[0].X < x && x < pts[2].X && pts[0].Y < y && y < pts[2].Y) inside = false; else inside = true; return inside; } #region //private Polyline2d createpll2dfromregion(DBObjectCollection explodedCurves, Polyline2d pll_clip, // Transaction newtr) //{ // // 存储提取的线段 // List curves = new List(); // foreach (DBObject obj in explodedCurves) // { // if (obj is Curve curve) // { // curves.Add(curve); // } // } // Polyline2d polyline = new Polyline2d(); // polyline.SetDatabaseDefaults(); // polyline.Layer = pll_clip.Layer; // polyline.Color = pll_clip.Color; // polyline.Linetype = pll_clip.Linetype; // polyline.LinetypeGenerationOn = pll_clip.LinetypeGenerationOn; // while (curves.Count > 0) // { // // 从第一条线段开始 // Curve currentCurve = curves[0]; // curves.RemoveAt(0); // // 添加当前线段的起点和终点 // Vertex2d splitVtx1 = new Vertex2d(); // splitVtx1.Position = currentCurve.StartPoint; // splitVtx1.Bulge = 0; // polyline.AppendVertex(splitVtx1); // Vertex2d splitVtx2 = new Vertex2d(); // splitVtx2.Position = currentCurve.StartPoint; // splitVtx2.Bulge = 0; // polyline.AppendVertex(splitVtx2); // // 查找与当前线段相连的线段 // bool foundConnectedCurve; // do // { // foundConnectedCurve = false; // for (int i = 0; i < curves.Count; i++) // { // Curve nextCurve = curves[i]; // // 检查是否与当前线段的起点或终点相连 // if (nextCurve.StartPoint.IsEqualTo(polyline.EndPoint)) // { // // 添加到多段线的末尾 // Vertex2d splitVtx = new Vertex2d(); // splitVtx.Position = nextCurve.StartPoint; // splitVtx.Bulge = 0; // polyline.AppendVertex(splitVtx); // curves.RemoveAt(i); // foundConnectedCurve = true; // break; // } // else if (nextCurve.EndPoint.IsEqualTo(polyline.StartPoint)) // { // // 添加到多段线的开头 // Vertex2d firstVertex = new Vertex2d(); // foreach (ObjectId vertexId in polyline) // { // firstVertex = newtr.GetObject(vertexId, OpenMode.ForRead) as Vertex2d; // break; // 获取第一个顶点后退出循环 // } // Vertex2d splitVtx = new Vertex2d(); // splitVtx.Position = nextCurve.EndPoint; // splitVtx.Bulge = 0; // polyline.InsertVertexAt(firstVertex.ObjectId, splitVtx); // curves.RemoveAt(i); // foundConnectedCurve = true; // break; // } // else if (nextCurve.EndPoint.IsEqualTo(polyline.EndPoint)) // { // // 添加到多段线的末尾 // Vertex2d splitVtx = new Vertex2d(); // splitVtx.Position = nextCurve.EndPoint; // splitVtx.Bulge = 0; // polyline.AppendVertex(splitVtx); // curves.RemoveAt(i); // foundConnectedCurve = true; // break; // } // else if (nextCurve.StartPoint.IsEqualTo(polyline.StartPoint)) // { // // 添加到多段线的开头 // Vertex2d firstVertex = new Vertex2d(); // foreach (ObjectId vertexId in polyline) // { // firstVertex = newtr.GetObject(vertexId, OpenMode.ForRead) as Vertex2d; // break; // 获取第一个顶点后退出循环 // } // Vertex2d splitVtx = new Vertex2d(); // splitVtx.Position = nextCurve.StartPoint; // splitVtx.Bulge = 0; // polyline.InsertVertexAt(firstVertex.ObjectId, splitVtx); // curves.RemoveAt(i); // foundConnectedCurve = true; // break; // } // } // } while (foundConnectedCurve); // // 关闭多段线 // polyline.Closed = true; // } // return polyline; //} #endregion } }