工具箱相关
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OutputbyTK_Form.cs 60KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. using GrxCAD.ApplicationServices;
  2. using GrxCAD.DatabaseServices;
  3. using GrxCAD.EditorInput;
  4. using GrxCAD.Geometry;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace HCTools
  15. {
  16. public partial class OutputbyTK_Form : Form
  17. {
  18. public OutputbyTK_Form()
  19. {
  20. InitializeComponent();
  21. }
  22. private void OutputbyTK_Form_Load(object sender, EventArgs e)
  23. {
  24. this.comboBox_tk.DropDownStyle = ComboBoxStyle.DropDownList;
  25. this.comboBox_num.DropDownStyle = ComboBoxStyle.DropDownList;
  26. //自动填充下拉框
  27. Database database = GrxCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
  28. using (Transaction traction = database.TransactionManager.StartTransaction())
  29. {
  30. //存放所有图层名
  31. List<string> alllayername = new List<string>();
  32. LayerTable layertable = traction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable;
  33. foreach (ObjectId objid in layertable)
  34. {
  35. LayerTableRecord layertablerecord = traction.GetObject(objid, OpenMode.ForRead) as LayerTableRecord;
  36. alllayername.Add(layertablerecord.Name);
  37. }
  38. alllayername.Sort();
  39. for (int i = 0; i < alllayername.Count; i++)
  40. {
  41. this.comboBox_tk.Items.Add(alllayername[i]);
  42. this.comboBox_num.Items.Add(alllayername[i]);
  43. }
  44. if (alllayername.Contains("TK"))
  45. {
  46. int index = alllayername.IndexOf("TK");
  47. this.comboBox_tk.SelectedIndex = index;
  48. }
  49. traction.Commit();
  50. }
  51. }
  52. private void button_sele_Click(object sender, EventArgs e)
  53. {
  54. FolderBrowserDialog open = new FolderBrowserDialog();
  55. open.Description = "请选择文件夹";
  56. if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  57. textBox_output.Text = open.SelectedPath;
  58. }
  59. private void button_ok_Click(object sender, EventArgs e)
  60. {
  61. if(comboBox_tk.Text=="")
  62. {
  63. MessageBox.Show("请选择分幅图框所在图层");
  64. return;
  65. }
  66. if (comboBox_num.Text == "")
  67. {
  68. MessageBox.Show("请选择分幅图号所在图层");
  69. return;
  70. }
  71. if (textBox_output.Text=="")
  72. {
  73. MessageBox.Show("请选择文件输出位置");
  74. return;
  75. }
  76. string lyr = comboBox_tk.SelectedItem.ToString();
  77. string output = textBox_output.Text.ToString();
  78. cutline(lyr, output);
  79. this.Close();
  80. }
  81. private void cutline(string lyr,string output)
  82. {
  83. dynamic acadApp = GrxCAD.ApplicationServices.Application.AcadApplication;
  84. acadApp.ZoomExtents();
  85. Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  86. List<string> tfh = new List<string>();//图幅号
  87. List<Point3dCollection> ptcolls = new List<Point3dCollection>();//图幅框节点
  88. //List<Entity> tks = new List<Entity>();//图幅框
  89. //获取分幅图框
  90. TypedValue[] typedvalue = new TypedValue[2];
  91. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, lyr), 0);
  92. typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Polyline,LWPolyline"), 1);
  93. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  94. PromptSelectionResult psr = ed.SelectAll(selectionfilter);
  95. if (psr.Status == PromptStatus.OK)
  96. {
  97. SelectionSet selectionset = psr.Value;
  98. ObjectId[] obj = new ObjectId[selectionset.Count];
  99. obj = selectionset.GetObjectIds();
  100. for (int i = 0; i < obj.Length; i++)
  101. {
  102. Database db = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
  103. DBObjectCollection objcoll = new DBObjectCollection();//与图幅框相交的实体
  104. //获取图幅框信息和与图幅框相交的实体
  105. using (Transaction tr = db.TransactionManager.StartTransaction())
  106. {
  107. Entity ent0 = tr.GetObject(obj[i], OpenMode.ForRead) as Entity;
  108. Point3dCollection pts = getpt(ent0);
  109. ptcolls.Add(pts);
  110. string num = "";
  111. //获取图幅号
  112. TypedValue[] typed_T = new TypedValue[2];
  113. typed_T.SetValue(new TypedValue((int)DxfCode.LayerName, comboBox_num.SelectedItem.ToString()), 0);
  114. typed_T.SetValue(new TypedValue((int)DxfCode.Start, "Text"), 1);
  115. SelectionFilter filter_T = new SelectionFilter(typed_T);
  116. PromptSelectionResult psr_T = ed.SelectWindow(pts[0], pts[2], filter_T);
  117. if (psr_T.Status == PromptStatus.OK)
  118. {
  119. SelectionSet ss_T = psr_T.Value;
  120. ObjectId[] obj_T = new ObjectId[ss_T.Count];
  121. obj_T = ss_T.GetObjectIds();
  122. DBText T = tr.GetObject(obj_T[0], OpenMode.ForRead) as DBText;
  123. num = T.TextString.ToString();
  124. }
  125. tfh.Add(num);
  126. ViewTableRecord currview = ed.GetCurrentView();
  127. currview.CenterPoint = new Point2d((pts[2].X - pts[0].X) / 2 + pts[0].X,
  128. (pts[2].Y - pts[0].Y) / 2 + pts[0].Y);
  129. currview.ViewDirection = new Vector3d(0, 0, 1);
  130. currview.Width = pts[2].X - pts[0].X;
  131. currview.Height = pts[2].Y - pts[0].Y;
  132. ed.SetCurrentView(currview);
  133. PromptSelectionResult psr1 = ed.SelectCrossingPolygon(pts);
  134. if (psr1.Status == PromptStatus.OK)
  135. {
  136. SelectionSet ss = psr1.Value;
  137. ObjectId[] objids = new ObjectId[ss.Count];
  138. objids = ss.GetObjectIds();
  139. for (int ii = 0; ii < objids.Length; ii++)
  140. {
  141. Entity ent = tr.GetObject(objids[ii], OpenMode.ForRead) as Entity;
  142. if (ent.Layer != lyr && ent.Layer != comboBox_num.Text)
  143. objcoll.Add(ent);
  144. }
  145. }
  146. tr.Commit();
  147. }
  148. //新建dwg
  149. string templatePath = "acad.dwt";
  150. DocumentCollection documentcoll = GrxCAD.ApplicationServices.Application.DocumentManager;
  151. Document document = documentcoll.Add(templatePath);
  152. Database newDb = document.Database;
  153. newDb.Ltscale = db.Ltscale;
  154. //复制图层
  155. using (Transaction traction = db.TransactionManager.StartTransaction())
  156. {
  157. //存放所有图层名
  158. List<string> alllayername = new List<string>();
  159. LayerTable layertable = traction.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
  160. foreach (ObjectId objid in layertable)
  161. {
  162. LayerTableRecord layertablerecord = traction.GetObject(objid, OpenMode.ForRead) as LayerTableRecord;
  163. alllayername.Add(layertablerecord.Name);
  164. }
  165. LayerControl layerscontrol = new LayerControl();
  166. for (int ii = 0; ii < alllayername.Count; ii++)
  167. {
  168. if (!layerscontrol.haslayername(alllayername[ii]) &&
  169. alllayername[ii] != lyr && alllayername[ii] != comboBox_num.SelectedItem.ToString())
  170. {
  171. //设置图层颜色
  172. LayerTable layerTable = traction.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
  173. LayerTableRecord layerRecord = traction.GetObject(layerTable[alllayername[ii]], OpenMode.ForRead) as LayerTableRecord;
  174. colorgb col = new colorgb(layerRecord.Color.Red, layerRecord.Color.Green, layerRecord.Color.Blue);
  175. layerscontrol.creatlayer(alllayername[ii], col);
  176. }
  177. }
  178. traction.Commit();
  179. }
  180. //复制与范围相交和在范围内的实体
  181. ObjectIdCollection ps2ids = new ObjectIdCollection();
  182. foreach (DBObject dbobj in objcoll)
  183. {
  184. ps2ids.Add(dbobj.ObjectId);
  185. }
  186. using (Transaction newtrans = newDb.TransactionManager.StartTransaction())
  187. {
  188. BlockTable targetBlockTable = newtrans.GetObject(newDb.BlockTableId, OpenMode.ForRead) as BlockTable;
  189. BlockTableRecord targetModelSpace = newtrans.GetObject(targetBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  190. using (IdMapping mapping = new IdMapping())
  191. db.WblockCloneObjects(ps2ids, targetModelSpace.ObjectId, mapping, DuplicateRecordCloning.Replace, false);
  192. newtrans.Commit();
  193. }
  194. //acadApp.ZoomExtents();
  195. //设置当前视图
  196. Editor edit = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  197. ViewTableRecord currview1 = ed.GetCurrentView();
  198. currview1.CenterPoint = new Point2d((ptcolls[i][2].X - ptcolls[i][0].X) / 2 + ptcolls[i][0].X,
  199. (ptcolls[i][2].Y - ptcolls[i][0].Y) / 2 + ptcolls[i][0].Y);
  200. //currview1.ViewDirection = new Vector3d(0, 0, 1);
  201. currview1.Width = ptcolls[i][2].X - ptcolls[i][0].X;
  202. currview1.Height = ptcolls[i][2].Y - ptcolls[i][0].Y;
  203. edit.SetCurrentView(currview1);
  204. //按图框范围框选实体,因为浩辰的选择有问题,所以还要全选,对比两个选择集中的实体,
  205. //将漏选范围外实体删除。
  206. PromptSelectionResult psr_intersect = edit.SelectCrossingPolygon(ptcolls[i]);
  207. PromptSelectionResult psr_all = edit.SelectAll();
  208. if (psr_intersect.Status == PromptStatus.OK)
  209. {
  210. SelectionSet ss = psr_intersect.Value;
  211. SelectionSet ss_all = psr_all.Value;
  212. ObjectId[] objids = new ObjectId[ss.Count];
  213. ObjectId[] objids_all = new ObjectId[ss_all.Count];
  214. objids = ss.GetObjectIds();
  215. objids_all = ss_all.GetObjectIds();
  216. List<ObjectId> outside = new List<ObjectId>();
  217. foreach (ObjectId id in objids_all)
  218. {
  219. outside.Add(id);
  220. }
  221. using (Transaction newtr = newDb.TransactionManager.StartTransaction())
  222. {
  223. BlockTable newBt = newtr.GetObject(newDb.BlockTableId, OpenMode.ForWrite) as BlockTable;
  224. BlockTableRecord newBtr = newtr.GetObject(newBt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  225. Polyline line = new Polyline();
  226. //画范围线
  227. for (int ct = 0; ct < ptcolls[i].Count; ct++)
  228. {
  229. line.AddVertexAt(ct, new Point2d(ptcolls[i][ct].X, ptcolls[i][ct].Y), 0, 0, 0);
  230. }
  231. line.AddVertexAt(4, new Point2d(ptcolls[i][0].X, ptcolls[i][0].Y), 0, 0, 0);
  232. line.Color = GrxCAD.Colors.Color.FromColorIndex(GrxCAD.Colors.ColorMethod.ByColor, 1);
  233. line.Layer = "0";
  234. line.SetDatabaseDefaults();
  235. newBtr.AppendEntity(line);
  236. newtr.AddNewlyCreatedDBObject(line, true);
  237. //遍历实体,裁剪与范围相交的实体
  238. for (int ii = 0; ii < objids.Length; ii++)
  239. {
  240. if (outside.Contains(objids[ii]))
  241. outside.Remove(objids[ii]);
  242. Entity ent = newtr.GetObject(objids[ii], OpenMode.ForWrite) as Entity;
  243. if (ent.Layer != lyr && ent.Layer != comboBox_num.Text.ToString())
  244. copyclip(ent, line, newtr, newBtr, ptcolls[i]);
  245. else
  246. ent.Erase();
  247. }
  248. if (outside.Count != 0)
  249. {
  250. foreach (ObjectId id in outside)
  251. {
  252. Entity ent_outside = newtr.GetObject(id, OpenMode.ForWrite) as Entity;
  253. ent_outside.Erase();
  254. }
  255. }
  256. line.Erase();
  257. newtr.Commit();
  258. }
  259. }
  260. else if(psr_intersect.Status == PromptStatus.Error &&
  261. psr_all.Status == PromptStatus.OK)
  262. {
  263. SelectionSet ss_all = psr_all.Value;
  264. ObjectId[] objids_all = new ObjectId[ss_all.Count];
  265. objids_all = ss_all.GetObjectIds();
  266. using (Transaction newtr = newDb.TransactionManager.StartTransaction())
  267. {
  268. foreach(ObjectId id in objids_all)
  269. {
  270. Entity ent = newtr.GetObject(id, OpenMode.ForWrite)as Entity;
  271. ent.Erase();
  272. }
  273. newtr.Commit();
  274. }
  275. }
  276. acadApp.ZoomExtents();
  277. //保存新DWG文件
  278. string newFilePath = output + "\\" + tfh[i] + ".dwg";
  279. newDb.SaveAs(newFilePath, true, DwgVersion.AC1021, newDb.SecurityParameters);
  280. newDb.Dispose();
  281. document.CloseAndDiscard();
  282. }
  283. }
  284. }
  285. /// <summary>
  286. /// 点排序
  287. /// </summary>
  288. private void sortpts(Point3dCollection ptcoll)
  289. {
  290. List<double> x = new List<double>();
  291. List<double> y = new List<double>();
  292. for (int i = 0; i < 4; i++)
  293. {
  294. if (!x.Contains(ptcoll[i].X))
  295. x.Add(ptcoll[i].X);
  296. if (!y.Contains(ptcoll[i].Y))
  297. y.Add(ptcoll[i].Y);
  298. }
  299. if (x[0] > x[1])
  300. {
  301. double temp = x[0];
  302. x[0] = x[1];
  303. x[1] = temp;
  304. }
  305. if (y[0] > y[1])
  306. {
  307. double temp = y[0];
  308. y[0] = y[1];
  309. y[1] = temp;
  310. }
  311. ptcoll[0] = new Point3d(Math.Round(x[0], 3, MidpointRounding.AwayFromZero), Math.Round(y[0], 3, MidpointRounding.AwayFromZero), 0); //左下
  312. ptcoll[1] = new Point3d(Math.Round(x[1], 3, MidpointRounding.AwayFromZero), Math.Round(y[0], 3, MidpointRounding.AwayFromZero), 0); //右下
  313. ptcoll[2] = new Point3d(Math.Round(x[1], 3, MidpointRounding.AwayFromZero), Math.Round(y[1], 3, MidpointRounding.AwayFromZero), 0); //右上
  314. ptcoll[3] = new Point3d(Math.Round(x[0], 3, MidpointRounding.AwayFromZero), Math.Round(y[1], 3, MidpointRounding.AwayFromZero), 0); //左上
  315. }
  316. private void copyclip(Entity ent, Polyline pll, Transaction newtr,
  317. BlockTableRecord newBtr, Point3dCollection pts)
  318. {
  319. var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
  320. //多段线、二维多段线
  321. if (ent is Polyline||ent is Polyline2d)
  322. {
  323. Polyline pll_clip = new Polyline();
  324. pll_clip.SetDatabaseDefaults();
  325. if (ent is Polyline2d)
  326. {
  327. Polyline2d pl2d = (Polyline2d)ent;
  328. int ptnum = 0;
  329. foreach (ObjectId vertexId in pl2d)
  330. {
  331. Vertex2d vtx = newtr.GetObject(vertexId, OpenMode.ForRead) as Vertex2d;
  332. Point2d point = new Point2d(pl2d.VertexPosition(vtx).X, pl2d.VertexPosition(vtx).Y); // 获取顶点坐标
  333. double bulge = vtx.Bulge; // 获取凸度
  334. pll_clip.AddVertexAt(ptnum, point, bulge, 0, 0);
  335. ptnum++;
  336. }
  337. pll_clip.Layer = pl2d.Layer;
  338. pll_clip.Color = pl2d.Color;
  339. pll_clip.Linetype = pl2d.Linetype;
  340. pll_clip.Elevation = pl2d.Elevation;
  341. pll_clip.Plinegen = pl2d.LinetypeGenerationOn;
  342. pll_clip.ConstantWidth = pl2d.ConstantWidth;
  343. }
  344. else
  345. pll_clip = (Polyline)ent;
  346. Point3dCollection Verptcoll = new Point3dCollection();
  347. for (int i = 0; i < pll_clip.NumberOfVertices; i++)
  348. {
  349. double p_x = Math.Round(pll_clip.GetPoint3dAt(i).X, 3);
  350. double p_y = Math.Round(pll_clip.GetPoint3dAt(i).Y, 3);
  351. if (pts[0].X < p_x && p_x < pts[1].X &&
  352. pts[0].Y < p_y && p_y < pts[2].Y)
  353. continue;
  354. else
  355. Verptcoll.Add(pll_clip.GetPoint3dAt(i));
  356. }
  357. Point3dCollection ptcoll = new Point3dCollection();
  358. pll_clip.IntersectWith(pll, Intersect.OnBothOperands, plane, ptcoll, IntPtr.Zero, IntPtr.Zero);
  359. if (Verptcoll.Count == pll_clip.NumberOfVertices &&
  360. ptcoll.Count==0)
  361. {
  362. ent.Erase();
  363. return;
  364. }
  365. Point3d pt1 = pll_clip.GetPoint3dAt(0);
  366. Point3d ptn = pll_clip.GetPoint3dAt(pll_clip.NumberOfVertices - 1);
  367. //非闭合
  368. if (pll_clip.Closed == false && pt1!=ptn)
  369. {
  370. if (ptcoll.Count != 0)
  371. {
  372. DBObjectCollection objcoll = pll_clip.GetSplitCurves(ptcoll);
  373. foreach (Object obj in objcoll)
  374. {
  375. Polyline pll_cut = (Polyline)obj;
  376. pll_cut.Plinegen = pll_clip.Plinegen;
  377. if (pllinsidetk(pll_cut, pts))
  378. {
  379. newBtr.AppendEntity(pll_cut);
  380. newtr.AddNewlyCreatedDBObject(pll_cut, true);
  381. }
  382. }
  383. ent.Erase();
  384. }
  385. }
  386. //闭合
  387. else
  388. {
  389. if (ptcoll.Count == 0)
  390. return;
  391. double ele = pll_clip.Elevation;
  392. pll_clip.Elevation = 0;
  393. // 将多段线转换为曲线集合
  394. DBObjectCollection curvesToClip = new DBObjectCollection();
  395. curvesToClip.Add(pll_clip);
  396. DBObjectCollection boundaryCurves = new DBObjectCollection();
  397. boundaryCurves.Add(pll);
  398. // 创建 Region
  399. DBObjectCollection regionsToClip = new DBObjectCollection();
  400. DBObjectCollection boundaryRegions = new DBObjectCollection();
  401. try
  402. {
  403. // 将多段线转换为 Region
  404. regionsToClip = GrxCAD.DatabaseServices.Region.CreateFromCurves(curvesToClip);
  405. boundaryRegions = GrxCAD.DatabaseServices.Region.CreateFromCurves(boundaryCurves);
  406. }
  407. catch (Exception ex)
  408. {
  409. GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\n错误: {ex.Message}");
  410. }
  411. // 执行布尔运算(交集)
  412. int deleindex = 0;
  413. foreach (GrxCAD.DatabaseServices.Region region in regionsToClip)
  414. {
  415. foreach (GrxCAD.DatabaseServices.Region boundary in boundaryRegions)
  416. {
  417. region.BooleanOperation(BooleanOperationType.BoolIntersect, boundary);
  418. DBObjectCollection explodedCurves = new DBObjectCollection();
  419. region.Explode(explodedCurves);
  420. if (deleindex == 0 && explodedCurves.Count != 0)
  421. {
  422. pll_clip.Erase();
  423. deleindex = 1;
  424. }
  425. foreach (DBObject obj in explodedCurves)
  426. {
  427. if (obj is GrxCAD.DatabaseServices.Region)
  428. {
  429. GrxCAD.DatabaseServices.Region subregion = (GrxCAD.DatabaseServices.Region)obj;
  430. DBObjectCollection subexplodedCurves = new DBObjectCollection();
  431. subregion.Explode(subexplodedCurves);
  432. Polyline polyline = createpllfromregion(subexplodedCurves);
  433. polyline.Elevation = ele;
  434. polyline.Layer = pll_clip.Layer;
  435. polyline.Color = pll_clip.Color;
  436. polyline.Linetype = pll_clip.Linetype;
  437. polyline.Plinegen = pll_clip.Plinegen;
  438. polyline.ConstantWidth = polyline.ConstantWidth;
  439. newBtr.AppendEntity(polyline);
  440. newtr.AddNewlyCreatedDBObject(polyline, true);
  441. }
  442. else
  443. {
  444. Polyline polyline = createpllfromregion(explodedCurves);
  445. polyline.Elevation = ele;
  446. polyline.Layer = pll_clip.Layer;
  447. polyline.Color = pll_clip.Color;
  448. polyline.Linetype = pll_clip.Linetype;
  449. polyline.Plinegen = pll_clip.Plinegen;
  450. polyline.ConstantWidth = polyline.ConstantWidth;
  451. newBtr.AppendEntity(polyline);
  452. newtr.AddNewlyCreatedDBObject(polyline, true);
  453. break;
  454. }
  455. }
  456. }
  457. }
  458. }
  459. }
  460. //三维多段线
  461. else if(ent is Polyline3d)
  462. {
  463. Polyline3d poly3d = (Polyline3d)ent;
  464. //Point3dCollection Verptcoll = new Point3dCollection();
  465. //poly3d.GetStretchPoints(Verptcoll);
  466. //int num = 0;
  467. //for (int i = 0; i < Verptcoll.Count; i++)
  468. //{
  469. // double p_x = Math.Round(Verptcoll[i].X, 3);
  470. // double p_y = Math.Round(Verptcoll[i].Y, 3);
  471. // if (pts[0].X < p_x && p_x < pts[1].X &&
  472. // pts[0].Y < p_y && p_y < pts[2].Y)
  473. // continue;
  474. // else
  475. // num++;
  476. //}
  477. //if (num == Verptcoll.Count)
  478. //{
  479. // ent.Erase();
  480. // return;
  481. //}
  482. Point3dCollection ptcoll = new Point3dCollection();
  483. poly3d.IntersectWith(pll, Intersect.OnBothOperands, plane, ptcoll, IntPtr.Zero, IntPtr.Zero);
  484. if (ptcoll.Count != 0)
  485. {
  486. DBObjectCollection objcoll = poly3d.GetSplitCurves(ptcoll);
  487. for (int i = 0; i < objcoll.Count; i++)
  488. {
  489. Polyline3d ply3 = (Polyline3d)objcoll[i];
  490. double l_x = ply3.GetPointAtParameter(1).X;
  491. double l_y = ply3.GetPointAtParameter(1).Y;
  492. double l_sx = ply3.StartPoint.X;
  493. double l_sy = ply3.StartPoint.Y;
  494. double l_ex = ply3.EndPoint.X;
  495. double l_ey = ply3.EndPoint.Y;
  496. if (pts[0].X <= l_sx && l_sx <= pts[1].X &&
  497. pts[0].Y <= l_sy && l_sy <= pts[2].Y &&
  498. pts[0].X <= l_ex && l_ex <= pts[1].X &&
  499. pts[0].Y <= l_ey && l_ey <= pts[2].Y &&
  500. pts[0].X <= l_x && l_x <= pts[1].X &&
  501. pts[0].Y <= l_y && l_y <= pts[2].Y)
  502. {
  503. newBtr.AppendEntity(ply3);
  504. newtr.AddNewlyCreatedDBObject(ply3, true);
  505. }
  506. }
  507. if (ptcoll.Count > 0)
  508. ent.Erase();
  509. }
  510. }
  511. //直线
  512. else if (ent is Line)
  513. {
  514. Point3dCollection ptcoll = new Point3dCollection();
  515. Line ln = (Line)ent;
  516. double l_sx = Math.Round(ln.StartPoint.X, 3);
  517. double l_sy = Math.Round(ln.StartPoint.Y, 3);
  518. double l_ex = Math.Round(ln.EndPoint.X, 3);
  519. double l_ey = Math.Round(ln.EndPoint.Y, 3);
  520. if (pts[2].X <= l_sx && l_ex >= pts[2].X ||
  521. pts[2].Y <= l_sy && l_ey >= pts[2].Y ||
  522. pts[0].X >= l_ex && l_ex <= pts[0].X ||
  523. pts[0].Y >= l_ey && l_ey <= pts[0].Y)
  524. {
  525. ent.Erase();
  526. return;
  527. }
  528. ln.IntersectWith(pll, Intersect.OnBothOperands, plane, ptcoll, IntPtr.Zero, IntPtr.Zero);
  529. double l_x = ln.StartPoint.X;
  530. double l_y = ln.StartPoint.Y;
  531. Line line = new Line();
  532. line.SetDatabaseDefaults();
  533. line.Layer = ln.Layer;
  534. line.Color = ln.Color;
  535. line.Linetype = ln.Linetype;
  536. line.LineWeight = ln.LineWeight;
  537. if (ptcoll.Count == 1)
  538. {
  539. if (pts[0].X <= l_x && l_x <= pts[1].X &&
  540. pts[0].Y <= l_y && l_y <= pts[2].Y)
  541. {
  542. line.StartPoint = ln.StartPoint;
  543. line.EndPoint = ptcoll[0];
  544. newBtr.AppendEntity(line);
  545. newtr.AddNewlyCreatedDBObject(line, true);
  546. ln.Erase();
  547. }
  548. else
  549. {
  550. line.StartPoint = ptcoll[0];
  551. line.EndPoint = ln.EndPoint;
  552. newBtr.AppendEntity(line);
  553. newtr.AddNewlyCreatedDBObject(line, true);
  554. ln.Erase();
  555. }
  556. }
  557. else if (ptcoll.Count >= 2)
  558. {
  559. for (int i = 0; i < ptcoll.Count - 1; i++)
  560. {
  561. line.StartPoint = ptcoll[i];
  562. line.EndPoint = ptcoll[i + 1];
  563. newBtr.AppendEntity(line);
  564. newtr.AddNewlyCreatedDBObject(line, true);
  565. }
  566. ln.Erase();
  567. }
  568. }
  569. //图案填充
  570. else if (ent is Hatch)
  571. {
  572. Hatch hc = (Hatch)ent;
  573. DBObjectCollection boundaries = new DBObjectCollection();
  574. int num = 0;
  575. for (int i = 0; i < hc.NumberOfLoops; i++)
  576. {
  577. HatchLoop loop = hc.GetLoopAt(i);
  578. if (loop.Curves == null)
  579. {
  580. Polyline polyline = new Polyline();
  581. // 遍历顶点并添加
  582. for (int ii = 0; ii < loop.Polyline.Count; ii++)
  583. {
  584. BulgeVertex vertex = loop.Polyline[ii];
  585. polyline.AddVertexAt(i, vertex.Vertex, vertex.Bulge, 0, 0);
  586. double v_x = Math.Round(vertex.Vertex.X);
  587. double v_y = Math.Round(vertex.Vertex.Y);
  588. if (pts[0].X <= v_x || v_x <= pts[1].X &&
  589. pts[0].Y <= v_y || v_y <= pts[2].Y)
  590. num++;
  591. }
  592. polyline.Closed = true; // 闭合多段线
  593. boundaries.Add(polyline);
  594. }
  595. else
  596. {
  597. foreach(Curve curve in loop.Curves)
  598. {
  599. boundaries.Add(curve);
  600. }
  601. }
  602. }
  603. if (num == 0)
  604. return;
  605. // 创建面域
  606. DBObjectCollection regionsToClip = GrxCAD.DatabaseServices.Region.CreateFromCurves(boundaries);
  607. DBObjectCollection pllboundary = new DBObjectCollection();
  608. pllboundary.Add(pll);
  609. DBObjectCollection boundaryRegions = GrxCAD.DatabaseServices.Region.CreateFromCurves(pllboundary);
  610. foreach (GrxCAD.DatabaseServices.Region region in regionsToClip)
  611. {
  612. foreach (GrxCAD.DatabaseServices.Region boundary in boundaryRegions)
  613. {
  614. region.BooleanOperation(BooleanOperationType.BoolIntersect, boundary);
  615. DBObjectCollection explodedCurves = new DBObjectCollection();
  616. region.Explode(explodedCurves);
  617. int deleindex = 0;
  618. ObjectIdCollection boundaryIds = new ObjectIdCollection();
  619. foreach (DBObject obj in explodedCurves)
  620. {
  621. if (obj is GrxCAD.DatabaseServices.Region)
  622. {
  623. GrxCAD.DatabaseServices.Region subregion = (GrxCAD.DatabaseServices.Region)obj;
  624. DBObjectCollection subexplodedCurves = new DBObjectCollection();
  625. subregion.Explode(subexplodedCurves);
  626. Polyline regionpll = createpllfromregion(subexplodedCurves);
  627. newBtr.AppendEntity(regionpll);
  628. newtr.AddNewlyCreatedDBObject(regionpll, true);
  629. boundaryIds.Add(regionpll.ObjectId);
  630. }
  631. else
  632. {
  633. Polyline regionpll = createpllfromregion(explodedCurves);
  634. newBtr.AppendEntity(regionpll);
  635. newtr.AddNewlyCreatedDBObject(regionpll, true);
  636. boundaryIds.Add(regionpll.ObjectId);
  637. break;
  638. }
  639. }
  640. Hatch newHatch = new Hatch
  641. {
  642. PatternScale = hc.PatternScale,
  643. PatternAngle = hc.PatternAngle,
  644. HatchStyle = hc.HatchStyle,
  645. Color = hc.Color,
  646. Transparency = hc.Transparency,
  647. Layer = hc.Layer,
  648. Linetype = hc.Linetype,
  649. LinetypeScale = hc.LinetypeScale,
  650. LineWeight = hc.LineWeight,
  651. Associative = hc.Associative,
  652. HatchObjectType = hc.HatchObjectType
  653. };
  654. newHatch.AppendLoop(HatchLoopTypes.Default, boundaryIds);
  655. newBtr.AppendEntity(newHatch);
  656. newtr.AddNewlyCreatedDBObject(newHatch, true);
  657. string pname = hc.PatternName;
  658. newHatch.SetHatchPattern(hc.PatternType, pname);
  659. newHatch.EvaluateHatch(true);
  660. if (deleindex == 0 && explodedCurves.Count != 0)
  661. {
  662. hc.UpgradeOpen();
  663. hc.Erase();
  664. deleindex = 1;
  665. }
  666. for (int i =0;i<boundaryIds.Count;i++)
  667. {
  668. Polyline pll_dele = newtr.GetObject(boundaryIds[i], OpenMode.ForWrite) as Polyline;
  669. pll_dele.Erase();
  670. }
  671. }
  672. }
  673. }
  674. //块参照(不包括高程点)
  675. else if(ent is BlockReference)
  676. {
  677. BlockReference blkef = (BlockReference)ent;
  678. Point3d pt_min = blkef.Bounds.Value.MinPoint;
  679. Point3d pt_max = blkef.Bounds.Value.MaxPoint;
  680. Point3d basePoint = blkef.Position;
  681. if (ptintk(pt_min, pts) && ptintk(pt_max, pts))
  682. {
  683. ent.Erase();
  684. return;
  685. }
  686. if (blkef.Name == "GC200")
  687. return;
  688. if (Math.Round(pt_min.X,3) < pts[0].X)
  689. {
  690. double dist = Math.Abs(pts[0].X - Math.Round(pt_min.X, 3)) + 0.1;
  691. Point3d targetPoint = new Point3d(blkef.Position.X + dist, blkef.Position.Y, blkef.Position.Z);
  692. Vector3d moveVector = targetPoint - basePoint;
  693. Matrix3d transform = Matrix3d.Displacement(moveVector);
  694. blkef.TransformBy(transform);
  695. }
  696. else if (Math.Round(pt_min.Y, 3) < pts[0].Y)
  697. {
  698. double dist = Math.Abs(pts[0].Y - Math.Round(pt_min.Y, 3)) + 0.1;
  699. Point3d targetPoint = new Point3d(blkef.Position.X, blkef.Position.Y + dist, blkef.Position.Z);
  700. Vector3d moveVector = targetPoint - basePoint;
  701. Matrix3d transform = Matrix3d.Displacement(moveVector);
  702. blkef.TransformBy(transform);
  703. }
  704. else if (Math.Round(pt_max.X, 3) > pts[2].X)
  705. {
  706. double dist = Math.Abs(pts[2].X - Math.Round(pt_max.X, 3)) + 0.1;
  707. Point3d targetPoint = new Point3d(blkef.Position.X - dist, blkef.Position.Y, blkef.Position.Z);
  708. Vector3d moveVector = targetPoint - basePoint;
  709. Matrix3d transform = Matrix3d.Displacement(moveVector);
  710. blkef.TransformBy(transform);
  711. }
  712. else if (Math.Round(pt_max.Y, 3) > pts[2].Y)
  713. {
  714. double dist = Math.Abs(pts[2].Y - Math.Round(pt_max.Y, 3)) + 0.1;
  715. Point3d targetPoint = new Point3d(blkef.Position.X, blkef.Position.Y - dist, blkef.Position.Z);
  716. Vector3d moveVector = targetPoint - basePoint;
  717. Matrix3d transform = Matrix3d.Displacement(moveVector);
  718. blkef.TransformBy(transform);
  719. }
  720. }
  721. //文字
  722. else if(ent is DBText||ent is MText)
  723. {
  724. Point3d pt_min = ent.Bounds.Value.MinPoint;
  725. Point3d pt_max = ent.Bounds.Value.MaxPoint;
  726. Point3d basePoint = new Point3d();
  727. double pos_x = 0;
  728. double pos_y = 0;
  729. double pos_z = 0;
  730. if (ptintk(pt_min, pts) && ptintk(pt_max, pts))
  731. {
  732. ent.Erase();
  733. return;
  734. }
  735. if (ent is DBText)
  736. {
  737. DBText dbt = (DBText)ent;
  738. basePoint = dbt.Position;
  739. pos_x = dbt.Position.X;
  740. pos_y = dbt.Position.Y;
  741. pos_z = dbt.Position.Z;
  742. }
  743. if (ent is MText)
  744. {
  745. MText mt = (MText)ent;
  746. basePoint = mt.Location;
  747. pos_x = mt.Location.X;
  748. pos_y = mt.Location.Y;
  749. pos_z = mt.Location.Z;
  750. }
  751. else if (Math.Round(pt_min.X, 3) < pts[0].X)
  752. {
  753. double dist = Math.Abs(pts[0].X - Math.Round(pt_min.X, 3)) + 0.1;
  754. Point3d targetPoint = new Point3d(pos_x + dist, pos_y, pos_z);
  755. Vector3d moveVector = targetPoint - basePoint;
  756. Matrix3d transform = Matrix3d.Displacement(moveVector);
  757. ent.TransformBy(transform);
  758. }
  759. else if (Math.Round(pt_min.Y, 3) < pts[0].Y)
  760. {
  761. double dist = Math.Abs(pts[0].Y - Math.Round(pt_min.Y, 3)) + 0.1;
  762. Point3d targetPoint = new Point3d(pos_x, pos_y + dist, pos_z);
  763. Vector3d moveVector = targetPoint - basePoint;
  764. Matrix3d transform = Matrix3d.Displacement(moveVector);
  765. ent.TransformBy(transform);
  766. }
  767. else if (Math.Round(pt_max.X, 3) > pts[2].X)
  768. {
  769. double dist = Math.Abs(pts[2].X - Math.Round(pt_max.X, 3)) + 0.1;
  770. Point3d targetPoint = new Point3d(pos_x - dist, pos_y, pos_z);
  771. Vector3d moveVector = targetPoint - basePoint;
  772. Matrix3d transform = Matrix3d.Displacement(moveVector);
  773. ent.TransformBy(transform);
  774. }
  775. else if (Math.Round(pt_max.Y, 3) > pts[2].Y)
  776. {
  777. double dist = Math.Abs(pts[2].Y - Math.Round(pt_max.Y, 3)) + 0.1;
  778. Point3d targetPoint = new Point3d(pos_x, pos_y - dist, pos_z);
  779. Vector3d moveVector = targetPoint - basePoint;
  780. Matrix3d transform = Matrix3d.Displacement(moveVector);
  781. ent.TransformBy(transform);
  782. }
  783. }
  784. #region
  785. //二维多段线
  786. //else if (ent is Polyline2d)
  787. //{
  788. // Point3dCollection ptcoll = new Point3dCollection();
  789. // Polyline2d pll_clip = (Polyline2d)ent;
  790. // pll_clip.IntersectWith(pll, Intersect.OnBothOperands, plane, ptcoll, IntPtr.Zero, IntPtr.Zero);
  791. // //非闭合
  792. // if (pll_clip.Closed == false)
  793. // {
  794. // if(ptcoll.Count!=0)
  795. // {
  796. // DBObjectCollection objcoll = pll_clip.GetSplitCurves(ptcoll);
  797. // foreach (Object obj in objcoll)
  798. // {
  799. // Polyline2d pll_cut = (Polyline2d)obj;
  800. // if (pllinsidetk(pll_cut, pts))
  801. // {
  802. // newBtr.AppendEntity(pll_cut);
  803. // newtr.AddNewlyCreatedDBObject(pll_cut, true);
  804. // }
  805. // }
  806. // ent.Erase();
  807. // }
  808. // }
  809. // //闭合
  810. // else
  811. // {
  812. // double ele = pll_clip.Elevation;
  813. // pll_clip.Elevation = 0;
  814. // DBObjectCollection result = new DBObjectCollection();
  815. // // 将多段线转换为曲线集合
  816. // DBObjectCollection curvesToClip = new DBObjectCollection();
  817. // curvesToClip.Add(pll_clip);
  818. // DBObjectCollection boundaryCurves = new DBObjectCollection();
  819. // boundaryCurves.Add(pll);
  820. // // 创建 Region
  821. // DBObjectCollection regionsToClip = new DBObjectCollection();
  822. // DBObjectCollection boundaryRegions = new DBObjectCollection();
  823. // try
  824. // {
  825. // // 将多段线转换为 Region
  826. // regionsToClip = GrxCAD.DatabaseServices.Region.CreateFromCurves(curvesToClip);
  827. // boundaryRegions = GrxCAD.DatabaseServices.Region.CreateFromCurves(boundaryCurves);
  828. // }
  829. // catch (Exception ex)
  830. // {
  831. // GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\n错误: {ex.Message}");
  832. // }
  833. // // 执行布尔运算(交集)
  834. // DBObjectCollection intersectionRegions = new DBObjectCollection();
  835. // foreach (GrxCAD.DatabaseServices.Region region in regionsToClip)
  836. // {
  837. // foreach (GrxCAD.DatabaseServices.Region boundary in boundaryRegions)
  838. // {
  839. // region.BooleanOperation(BooleanOperationType.BoolIntersect, boundary);
  840. // DBObjectCollection explodedCurves = new DBObjectCollection();
  841. // region.Explode(explodedCurves);
  842. // int flag = 0;
  843. // foreach (DBObject obj in explodedCurves)
  844. // {
  845. // if (obj is GrxCAD.DatabaseServices.Region)
  846. // {
  847. // flag = 1;
  848. // GrxCAD.DatabaseServices.Region subregion = (GrxCAD.DatabaseServices.Region)obj;
  849. // DBObjectCollection subexplodedCurves = new DBObjectCollection();
  850. // subregion.Explode(subexplodedCurves);
  851. // Polyline2d polyline = createpll2dfromregion(subexplodedCurves, pll_clip, newtr);
  852. // polyline.Elevation = ele;
  853. // newBtr.AppendEntity(polyline);
  854. // newtr.AddNewlyCreatedDBObject(polyline, true);
  855. // }
  856. // else
  857. // {
  858. // break;
  859. // }
  860. // }
  861. // if (flag == 0)
  862. // {
  863. // Polyline2d polyline = createpll2dfromregion(explodedCurves, pll_clip, newtr);
  864. // polyline.Elevation = ele;
  865. // newBtr.AppendEntity(polyline);
  866. // newtr.AddNewlyCreatedDBObject(polyline, true);
  867. // }
  868. // }
  869. // }
  870. // if (regionsToClip.Count != 0)
  871. // {
  872. // pll_clip.Erase();
  873. // }
  874. // }
  875. //}
  876. #endregion
  877. }
  878. public Polyline createpllfromregion(DBObjectCollection explodedCurves)
  879. {
  880. // 存储提取的线段
  881. List<Curve> curves = new List<Curve>();
  882. foreach (DBObject obj in explodedCurves)
  883. {
  884. if (obj is Curve curve)
  885. {
  886. curves.Add(curve);
  887. }
  888. }
  889. Polyline polyline = new Polyline();
  890. polyline.SetDatabaseDefaults();
  891. while (curves.Count > 0)
  892. {
  893. // 从第一条线段开始
  894. Curve currentCurve = curves[0];
  895. curves.RemoveAt(0);
  896. // 添加当前线段的起点和终点
  897. polyline.AddVertexAt(0, new Point2d(currentCurve.StartPoint.X, currentCurve.StartPoint.Y), 0, 0, 0);
  898. polyline.AddVertexAt(1, new Point2d(currentCurve.EndPoint.X, currentCurve.EndPoint.Y), 0, 0, 0);
  899. // 查找与当前线段相连的线段
  900. bool foundConnectedCurve;
  901. do
  902. {
  903. foundConnectedCurve = false;
  904. for (int i = 0; i < curves.Count; i++)
  905. {
  906. Curve nextCurve = curves[i];
  907. // 检查是否与当前线段的起点或终点相连
  908. if (nextCurve.StartPoint.IsEqualTo(polyline.GetPoint3dAt(polyline.NumberOfVertices - 1)))
  909. {
  910. // 添加到多段线的末尾
  911. polyline.AddVertexAt(polyline.NumberOfVertices, new Point2d(nextCurve.EndPoint.X, nextCurve.EndPoint.Y), 0, 0, 0);
  912. curves.RemoveAt(i);
  913. foundConnectedCurve = true;
  914. break;
  915. }
  916. else if (nextCurve.EndPoint.IsEqualTo(polyline.GetPoint3dAt(0)))
  917. {
  918. // 添加到多段线的开头
  919. polyline.AddVertexAt(0, new Point2d(nextCurve.StartPoint.X, nextCurve.StartPoint.Y), 0, 0, 0);
  920. curves.RemoveAt(i);
  921. foundConnectedCurve = true;
  922. break;
  923. }
  924. else if (nextCurve.EndPoint.IsEqualTo(polyline.GetPoint3dAt(polyline.NumberOfVertices - 1)))
  925. {
  926. // 添加到多段线的末尾
  927. polyline.AddVertexAt(polyline.NumberOfVertices, new Point2d(nextCurve.StartPoint.X, nextCurve.StartPoint.Y), 0, 0, 0);
  928. curves.RemoveAt(i);
  929. foundConnectedCurve = true;
  930. break;
  931. }
  932. else if (nextCurve.StartPoint.IsEqualTo(polyline.GetPoint3dAt(0)))
  933. {
  934. // 添加到多段线的开头
  935. polyline.AddVertexAt(0, new Point2d(nextCurve.EndPoint.X, nextCurve.EndPoint.Y), 0, 0, 0);
  936. curves.RemoveAt(i);
  937. foundConnectedCurve = true;
  938. break;
  939. }
  940. }
  941. } while (foundConnectedCurve);
  942. // 关闭多段线
  943. polyline.Closed = true;
  944. }
  945. return polyline;
  946. }
  947. public Polyline3d createpl3dfromregion(DBObjectCollection explodedCurves)
  948. {
  949. // 存储提取的线段
  950. List<Curve> curves = new List<Curve>();
  951. foreach (DBObject obj in explodedCurves)
  952. {
  953. if (obj is Curve curve)
  954. {
  955. curves.Add(curve);
  956. }
  957. }
  958. Polyline3d pl3d = new Polyline3d();
  959. pl3d.SetDatabaseDefaults();
  960. while (curves.Count > 0)
  961. {
  962. // 从第一条线段开始
  963. Curve currentCurve = curves[0];
  964. curves.RemoveAt(0);
  965. // 添加当前线段的起点和终点
  966. pl3d.AppendVertex(new PolylineVertex3d(currentCurve.StartPoint));
  967. pl3d.AppendVertex(new PolylineVertex3d(currentCurve.EndPoint));
  968. // 查找与当前线段相连的线段
  969. bool foundConnectedCurve;
  970. do
  971. {
  972. foundConnectedCurve = false;
  973. for (int i = 0; i < curves.Count; i++)
  974. {
  975. Curve nextCurve = curves[i];
  976. // 检查是否与当前线段的起点或终点相连
  977. if (nextCurve.StartPoint.IsEqualTo(pl3d.EndPoint))
  978. {
  979. // 添加到多段线的末尾
  980. pl3d.AppendVertex(new PolylineVertex3d(nextCurve.EndPoint));
  981. curves.RemoveAt(i);
  982. foundConnectedCurve = true;
  983. break;
  984. }
  985. else if (nextCurve.EndPoint.IsEqualTo(pl3d.StartPoint))
  986. {
  987. // 添加到多段线的开头
  988. pl3d.AppendVertex(new PolylineVertex3d(nextCurve.StartPoint));
  989. curves.RemoveAt(i);
  990. foundConnectedCurve = true;
  991. break;
  992. }
  993. else if (nextCurve.EndPoint.IsEqualTo(pl3d.EndPoint))
  994. {
  995. // 添加到多段线的末尾
  996. pl3d.AppendVertex(new PolylineVertex3d(nextCurve.StartPoint));
  997. curves.RemoveAt(i);
  998. foundConnectedCurve = true;
  999. break;
  1000. }
  1001. else if (nextCurve.StartPoint.IsEqualTo(pl3d.StartPoint))
  1002. {
  1003. // 添加到多段线的开头
  1004. pl3d.AppendVertex(new PolylineVertex3d(nextCurve.EndPoint));
  1005. curves.RemoveAt(i);
  1006. foundConnectedCurve = true;
  1007. break;
  1008. }
  1009. }
  1010. } while (foundConnectedCurve);
  1011. // 关闭多段线
  1012. pl3d.Closed = true;
  1013. }
  1014. return pl3d;
  1015. }
  1016. private bool pllinsidetk(Entity ent, Point3dCollection pts)
  1017. {
  1018. bool inside = false;
  1019. if (ent is Polyline)
  1020. {
  1021. Polyline pll = (Polyline)ent;
  1022. double p_x = Math.Round(pll.GetPoint3dAt(1).X, 3);
  1023. double p_y = Math.Round(pll.GetPoint3dAt(1).Y, 3);
  1024. double sp_x = Math.Round(pll.StartPoint.X, 3);
  1025. double sp_y = Math.Round(pll.StartPoint.Y, 3);
  1026. double ep_x = Math.Round(pll.EndPoint.X, 3);
  1027. double ep_y = Math.Round(pll.EndPoint.Y, 3);
  1028. if (pts[0].X <= p_x && p_x <= pts[1].X &&
  1029. pts[0].Y <= p_y && p_y <= pts[2].Y &&
  1030. pts[0].X <= sp_x && sp_x <= pts[1].X &&
  1031. pts[0].Y <= sp_y && sp_y <= pts[2].Y &&
  1032. pts[0].X <= ep_x && ep_x <= pts[1].X &&
  1033. pts[0].Y <= ep_y && ep_y <= pts[2].Y)
  1034. inside = true;
  1035. else
  1036. inside = false;
  1037. }
  1038. if (ent is Polyline3d)
  1039. {
  1040. Polyline2d pll = (Polyline2d)ent;
  1041. double p_x = Math.Round(pll.GetPointAtParameter(1).X, 3);
  1042. double p_y = Math.Round(pll.GetPointAtParameter(1).Y, 3);
  1043. double sp_x = Math.Round(pll.StartPoint.X, 3);
  1044. double sp_y = Math.Round(pll.StartPoint.Y, 3);
  1045. double ep_x = Math.Round(pll.EndPoint.X, 3);
  1046. double ep_y = Math.Round(pll.EndPoint.Y, 3);
  1047. if (pts[0].X <= p_x && p_x <= pts[1].X &&
  1048. pts[0].Y <= p_y && p_y <= pts[2].Y &&
  1049. pts[0].X <= sp_x && sp_x <= pts[1].X &&
  1050. pts[0].Y <= sp_y && sp_y <= pts[2].Y &&
  1051. pts[0].X <= ep_x && ep_x <= pts[1].X &&
  1052. pts[0].Y <= ep_y && ep_y <= pts[2].Y)
  1053. inside = true;
  1054. else
  1055. inside = false;
  1056. }
  1057. return inside;
  1058. }
  1059. private Point3dCollection getpt(Entity ent)
  1060. {
  1061. Point3dCollection ptcoll = new Point3dCollection();
  1062. if (ent is Polyline)
  1063. {
  1064. Polyline pll = (Polyline)ent;
  1065. //获取图框节点并排序
  1066. int numVertices = pll.NumberOfVertices;
  1067. for (int j = 0; j < numVertices; j++)
  1068. {
  1069. ptcoll.Add(pll.GetPoint3dAt(j));
  1070. }
  1071. sortpts(ptcoll);
  1072. }
  1073. else if(ent is Polyline2d)
  1074. {
  1075. Polyline2d pl2d = (Polyline2d)ent;
  1076. pl2d.GetStretchPoints(ptcoll);
  1077. sortpts(ptcoll);
  1078. }
  1079. else if(ent is Polyline3d)
  1080. {
  1081. Polyline3d pl3d = (Polyline3d)ent;
  1082. pl3d.GetStretchPoints(ptcoll);
  1083. sortpts(ptcoll);
  1084. }
  1085. return ptcoll;
  1086. }
  1087. private bool ptintk(Point3d pt,Point3dCollection pts)
  1088. {
  1089. bool inside;
  1090. double x = Math.Round(pt.X, 3);
  1091. double y = Math.Round(pt.Y, 3);
  1092. if (pts[0].X < x && x < pts[2].X &&
  1093. pts[0].Y < y && y < pts[2].Y)
  1094. inside = false;
  1095. else
  1096. inside = true;
  1097. return inside;
  1098. }
  1099. #region
  1100. //private Polyline2d createpll2dfromregion(DBObjectCollection explodedCurves, Polyline2d pll_clip,
  1101. // Transaction newtr)
  1102. //{
  1103. // // 存储提取的线段
  1104. // List<Curve> curves = new List<Curve>();
  1105. // foreach (DBObject obj in explodedCurves)
  1106. // {
  1107. // if (obj is Curve curve)
  1108. // {
  1109. // curves.Add(curve);
  1110. // }
  1111. // }
  1112. // Polyline2d polyline = new Polyline2d();
  1113. // polyline.SetDatabaseDefaults();
  1114. // polyline.Layer = pll_clip.Layer;
  1115. // polyline.Color = pll_clip.Color;
  1116. // polyline.Linetype = pll_clip.Linetype;
  1117. // polyline.LinetypeGenerationOn = pll_clip.LinetypeGenerationOn;
  1118. // while (curves.Count > 0)
  1119. // {
  1120. // // 从第一条线段开始
  1121. // Curve currentCurve = curves[0];
  1122. // curves.RemoveAt(0);
  1123. // // 添加当前线段的起点和终点
  1124. // Vertex2d splitVtx1 = new Vertex2d();
  1125. // splitVtx1.Position = currentCurve.StartPoint;
  1126. // splitVtx1.Bulge = 0;
  1127. // polyline.AppendVertex(splitVtx1);
  1128. // Vertex2d splitVtx2 = new Vertex2d();
  1129. // splitVtx2.Position = currentCurve.StartPoint;
  1130. // splitVtx2.Bulge = 0;
  1131. // polyline.AppendVertex(splitVtx2);
  1132. // // 查找与当前线段相连的线段
  1133. // bool foundConnectedCurve;
  1134. // do
  1135. // {
  1136. // foundConnectedCurve = false;
  1137. // for (int i = 0; i < curves.Count; i++)
  1138. // {
  1139. // Curve nextCurve = curves[i];
  1140. // // 检查是否与当前线段的起点或终点相连
  1141. // if (nextCurve.StartPoint.IsEqualTo(polyline.EndPoint))
  1142. // {
  1143. // // 添加到多段线的末尾
  1144. // Vertex2d splitVtx = new Vertex2d();
  1145. // splitVtx.Position = nextCurve.StartPoint;
  1146. // splitVtx.Bulge = 0;
  1147. // polyline.AppendVertex(splitVtx);
  1148. // curves.RemoveAt(i);
  1149. // foundConnectedCurve = true;
  1150. // break;
  1151. // }
  1152. // else if (nextCurve.EndPoint.IsEqualTo(polyline.StartPoint))
  1153. // {
  1154. // // 添加到多段线的开头
  1155. // Vertex2d firstVertex = new Vertex2d();
  1156. // foreach (ObjectId vertexId in polyline)
  1157. // {
  1158. // firstVertex = newtr.GetObject(vertexId, OpenMode.ForRead) as Vertex2d;
  1159. // break; // 获取第一个顶点后退出循环
  1160. // }
  1161. // Vertex2d splitVtx = new Vertex2d();
  1162. // splitVtx.Position = nextCurve.EndPoint;
  1163. // splitVtx.Bulge = 0;
  1164. // polyline.InsertVertexAt(firstVertex.ObjectId, splitVtx);
  1165. // curves.RemoveAt(i);
  1166. // foundConnectedCurve = true;
  1167. // break;
  1168. // }
  1169. // else if (nextCurve.EndPoint.IsEqualTo(polyline.EndPoint))
  1170. // {
  1171. // // 添加到多段线的末尾
  1172. // Vertex2d splitVtx = new Vertex2d();
  1173. // splitVtx.Position = nextCurve.EndPoint;
  1174. // splitVtx.Bulge = 0;
  1175. // polyline.AppendVertex(splitVtx);
  1176. // curves.RemoveAt(i);
  1177. // foundConnectedCurve = true;
  1178. // break;
  1179. // }
  1180. // else if (nextCurve.StartPoint.IsEqualTo(polyline.StartPoint))
  1181. // {
  1182. // // 添加到多段线的开头
  1183. // Vertex2d firstVertex = new Vertex2d();
  1184. // foreach (ObjectId vertexId in polyline)
  1185. // {
  1186. // firstVertex = newtr.GetObject(vertexId, OpenMode.ForRead) as Vertex2d;
  1187. // break; // 获取第一个顶点后退出循环
  1188. // }
  1189. // Vertex2d splitVtx = new Vertex2d();
  1190. // splitVtx.Position = nextCurve.StartPoint;
  1191. // splitVtx.Bulge = 0;
  1192. // polyline.InsertVertexAt(firstVertex.ObjectId, splitVtx);
  1193. // curves.RemoveAt(i);
  1194. // foundConnectedCurve = true;
  1195. // break;
  1196. // }
  1197. // }
  1198. // } while (foundConnectedCurve);
  1199. // // 关闭多段线
  1200. // polyline.Closed = true;
  1201. // }
  1202. // return polyline;
  1203. //}
  1204. #endregion
  1205. }
  1206. }