工具箱相关
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CutContourLine.cs 51KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace HCTools
  12. {
  13. class CutContourLine
  14. {
  15. public static string sqxlayer;
  16. public static string jqxlayer;
  17. public static int mapscale;
  18. public static double scalelen;
  19. public static double lenIgnore;
  20. public static double eledvalue;
  21. public void ccls()
  22. {
  23. DocumentLock documentlock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  24. Document doc = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  25. Database db = doc.Database;
  26. //类全局变量
  27. //存储地图图层
  28. //System.Collections.ArrayList layerlist = new System.Collections.ArrayList();
  29. LayerTable layertable = null;
  30. bool openflag = true;
  31. List<Entity> plentitycollection = new List<Entity>();
  32. List<Entity> plentitycollection1 = new List<Entity>();
  33. //保存裁剪掉的等高线
  34. List<Entity> cdentity = new List<Entity>();
  35. #region//第一个事务 获取计曲线
  36. using (Transaction tran = doc.TransactionManager.StartTransaction())
  37. {
  38. //layertable = tran.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
  39. ////获取图层
  40. //foreach (ObjectId id in layertable)
  41. //{
  42. // DocumentLock docLock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  43. // LayerTableRecord lyrtbrec = tran.GetObject(id, OpenMode.ForWrite) as LayerTableRecord;
  44. // docLock.Dispose();
  45. // layerlist.Add(lyrtbrec.Name);
  46. //}
  47. //CLSelectForm clsf = new CLSelectForm(layerlist);
  48. //if (clsf.ShowDialog() == DialogResult.OK)
  49. //{
  50. // openflag = true;
  51. // mapscale = clsf.mapscale;
  52. // sqxlayer = clsf.SQX;
  53. // jqxlayer = clsf.JQX;
  54. // scalelen = clsf.ScaleLength;
  55. // lenIgnore = clsf.LenIgnore;
  56. // eledvalue = clsf.EleDValue;
  57. //}
  58. if (openflag == true)
  59. {
  60. ///带1的为首曲线过滤,没有的是计曲线过滤
  61. TypedValue[] tv = new TypedValue[1];
  62. TypedValue[] tv1 = new TypedValue[1];
  63. tv.SetValue(new TypedValue((int)DxfCode.LayerName, jqxlayer), 0);
  64. tv1.SetValue(new TypedValue((int)DxfCode.LayerName, sqxlayer), 0);
  65. SelectionFilter sf = new SelectionFilter(tv);
  66. SelectionFilter sf1 = new SelectionFilter(tv1);
  67. //doc.Editor.WriteMessage("请选择计曲线!");
  68. //PromptSelectionResult psr = doc.Editor.GetSelection(sf);
  69. PromptSelectionResult psr = doc.Editor.SelectAll(sf);
  70. PromptSelectionResult psr1 = doc.Editor.SelectAll(sf1);
  71. if (psr.Status == PromptStatus.OK && psr1.Status == PromptStatus.OK)
  72. {
  73. //// 以只读方式打开块表记录 Open the Block table record for read
  74. BlockTable acbt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  75. //// 以写方式打开模型空间块表记录 Open the Block table record Model space for write
  76. BlockTableRecord acblr = tran.GetObject(acbt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  77. SelectionSet ss = psr.Value;
  78. //// 以只读方式打开块表记录 Open the Block table record for read
  79. SelectionSet ss1 = psr1.Value;
  80. foreach (ObjectId idpl in ss.GetObjectIds())
  81. {
  82. Entity enpl = tran.GetObject(idpl, OpenMode.ForWrite, true) as Entity;
  83. if (enpl is Polyline)
  84. {
  85. Polyline pl = enpl as Polyline;
  86. if (pl.Elevation != 0)
  87. {
  88. plentitycollection.Add(enpl);
  89. }
  90. }
  91. else if (enpl is Polyline2d)
  92. {
  93. Polyline2d pl2d = enpl as Polyline2d;
  94. if (pl2d.Elevation != 0)
  95. {
  96. plentitycollection.Add(enpl);
  97. }
  98. }
  99. }
  100. foreach (ObjectId idp2 in ss1.GetObjectIds())
  101. {
  102. Entity enp2 = tran.GetObject(idp2, OpenMode.ForWrite, true) as Entity;
  103. if (enp2 is Polyline)
  104. {
  105. Polyline pl = enp2 as Polyline;
  106. if (pl.Elevation != 0)
  107. {
  108. plentitycollection1.Add(enp2);
  109. }
  110. }
  111. else if (enp2 is Polyline2d)
  112. {
  113. Polyline2d pl2d = enp2 as Polyline2d;
  114. if (pl2d.Elevation != 0)
  115. {
  116. plentitycollection1.Add(enp2);
  117. }
  118. }
  119. }
  120. }
  121. else
  122. doc.Editor.WriteMessage("没有选择到任何实体\r\n");
  123. //////////////////////////////这个气死我了
  124. doc.Editor.WriteMessage("程序开始运行\r\n.........\r\n......\r\n...\r\n");
  125. tran.Commit();
  126. tran.Dispose();
  127. }
  128. else
  129. {
  130. MessageBox.Show("请选择相应的每一个参数!", "警告");
  131. return;
  132. }
  133. }//end using
  134. #endregion
  135. #region//检查、裁剪等高线
  136. if (plentitycollection.Count > 0)
  137. {
  138. //根据高程将计曲线排序
  139. plentitycollection.Sort(CompareDGXByElevation);
  140. //删除重叠,重复的等高线
  141. //int count = RemoveOverlapPolyline(ref plentitycollection);
  142. ////////////////////方便调试 仅此而已
  143. //List<Polyline2d> test = new List<Polyline2d>();
  144. //foreach (Entity en in plentitycollection)
  145. //{
  146. // Polyline2d pl = en as Polyline2d;
  147. // if (pl != null)
  148. // test.Add(pl);
  149. //}
  150. //List<Polyline> test00 = new List<Polyline>();
  151. //foreach (Entity en in plentitycollection)
  152. //{
  153. // Polyline pl = en as Polyline;
  154. // if (pl != null)
  155. // test00.Add(pl);
  156. //}
  157. ///////////////////
  158. //以每条计曲线为循环变量
  159. for (int i = 0; i < plentitycollection.Count - 1; i++)
  160. {
  161. Point3dCollection pt3dcoll = new Point3dCollection();
  162. Entity en1 = plentitycollection[i];
  163. double ele1 = 0;
  164. Point2d pten1start = new Point2d();//
  165. Point2d pten1end = new Point2d();//
  166. if (en1 is Polyline)
  167. {
  168. Polyline pl = en1 as Polyline;
  169. pten1start = new Point2d(pl.StartPoint.X, pl.StartPoint.Y);
  170. pten1end = new Point2d(pl.EndPoint.X, pl.EndPoint.Y);
  171. ele1 = Convert.ToDouble(pl.Elevation.ToString("f1"));
  172. pl.GetStretchPoints(pt3dcoll);
  173. }
  174. else if (en1 is Polyline2d)
  175. {
  176. Polyline2d pl2d = en1 as Polyline2d;
  177. pten1start = new Point2d(pl2d.StartPoint.X, pl2d.StartPoint.Y);
  178. pten1end = new Point2d(pl2d.EndPoint.X, pl2d.EndPoint.Y);
  179. ele1 = Convert.ToDouble(pl2d.Elevation.ToString("f1"));
  180. pl2d.GetStretchPoints(pt3dcoll);
  181. }
  182. int j = 1;
  183. Entity en2 = null;
  184. double ele2 = 0, ele = 0;
  185. Point2d pten2start = new Point2d();
  186. Point2d pten2end = new Point2d();
  187. double startlength = 9999999, endlength = 9999999;
  188. //寻找一条等高线(计曲线)相邻的等高线
  189. while (ele <= ele1 + eledvalue)
  190. {
  191. Entity entemp = plentitycollection[j];
  192. if (entemp is Polyline)
  193. {
  194. Polyline pl = entemp as Polyline;
  195. ele = Convert.ToDouble(pl.Elevation.ToString("f1"));
  196. if (ele == ele1 + eledvalue)
  197. {
  198. pten2start = new Point2d(pl.StartPoint.X, pl.StartPoint.Y);
  199. pten2end = new Point2d(pl.EndPoint.X, pl.EndPoint.Y);
  200. double start = pten1start.GetDistanceTo(pten2start);
  201. double end = pten1end.GetDistanceTo(pten2end);
  202. //这段代码主要解决相邻有多条等高线时选择距离最近的
  203. if (startlength > start && endlength > end)
  204. {
  205. startlength = start;
  206. endlength = end;
  207. en2 = entemp;
  208. ele2 = ele;
  209. }
  210. else if (startlength > start)
  211. {
  212. startlength = start;
  213. endlength = end;
  214. en2 = entemp;
  215. ele2 = ele;
  216. }
  217. else if (endlength > end)
  218. {
  219. startlength = start;
  220. endlength = end;
  221. en2 = entemp;
  222. ele2 = ele;
  223. }
  224. }
  225. }
  226. else if (entemp is Polyline2d)
  227. {
  228. Polyline2d pl2d = entemp as Polyline2d;
  229. ele = Convert.ToDouble(pl2d.Elevation.ToString("f1"));
  230. if (ele == ele1 + eledvalue)
  231. {
  232. pten2start = new Point2d(pl2d.StartPoint.X, pl2d.StartPoint.Y);
  233. pten2end = new Point2d(pl2d.EndPoint.X, pl2d.EndPoint.Y);
  234. double start = pten1start.GetDistanceTo(pten2start);
  235. double end = pten1end.GetDistanceTo(pten2end);
  236. if (startlength > start && endlength > end)
  237. {
  238. startlength = start;
  239. endlength = end;
  240. en2 = entemp;
  241. ele2 = ele;
  242. }
  243. else if (startlength > start)
  244. {
  245. startlength = start;
  246. endlength = end;
  247. en2 = entemp;
  248. ele2 = ele;
  249. }
  250. else if (endlength > end)
  251. {
  252. startlength = start;
  253. endlength = end;
  254. en2 = entemp;
  255. ele2 = ele;
  256. }
  257. }
  258. }
  259. j++;
  260. if (j > plentitycollection.Count - 1)
  261. break;
  262. }
  263. //满足裁剪条件的节点
  264. Point3dCollection cjcoll = new Point3dCollection();
  265. bool flag = false;
  266. if (en2 != null)
  267. {
  268. //以计曲线节点为循环
  269. for (int ii = 0; ii < pt3dcoll.Count; ii++)
  270. {
  271. Point3d pt3d = pt3dcoll[ii];
  272. Point3d temppt3d = new Point3d();
  273. temppt3d = GetClosestPoint(pt3d, en2);
  274. //临时计算两个3d点之间的平面距离
  275. Point2d pt2d1 = new Point2d(pt3d.X, pt3d.Y);
  276. Point2d pt2d2 = new Point2d(temppt3d.X, temppt3d.Y);
  277. double length = pt2d1.GetDistanceTo(pt2d2);
  278. if (length < scalelen)
  279. {
  280. //flag为false标志继续添加满足条件节点
  281. //flag为true标志结束添加节点,进行后续计算
  282. if (ii == pt3dcoll.Count - 1)//最后一组节点
  283. {
  284. flag = true;
  285. }
  286. else
  287. {
  288. cjcoll.Add(pt3d);
  289. cjcoll.Add(temppt3d);
  290. }
  291. }
  292. else
  293. {
  294. flag = true;
  295. }
  296. if (flag == true)
  297. {
  298. if (cjcoll.Count > 0)
  299. {
  300. if (cjcoll.Count > 3)
  301. {
  302. //矩形框四个角点
  303. //选取2个为裁剪的两个节点
  304. //为计曲线上的点
  305. Point3d pt1cjnode = new Point3d();
  306. Point3d pt2node = new Point3d();
  307. Point3d pt3cjnode = new Point3d();
  308. Point3d pt4node = new Point3d();
  309. pt1cjnode = cjcoll[0];
  310. pt2node = cjcoll[1];
  311. pt3cjnode = cjcoll[cjcoll.Count - 2];
  312. pt4node = cjcoll[cjcoll.Count - 1];
  313. Point3dCollection rectcj = new Point3dCollection();
  314. rectcj.Add(pt1cjnode);
  315. rectcj.Add(pt2node);
  316. rectcj.Add(pt3cjnode);
  317. rectcj.Add(pt4node);
  318. #region
  319. Point2d pt2djudge1 = new Point2d(pt1cjnode.X, pt1cjnode.Y);
  320. Point2d pt2djudge2 = new Point2d(pt3cjnode.X, pt3cjnode.Y);
  321. if (pt2djudge1.GetDistanceTo(pt2djudge2) > lenIgnore)
  322. {
  323. #region//画裁剪线
  324. ////using (Transaction tran = doc.TransactionManager.StartTransaction())
  325. ////{
  326. //// //// 以只读方式打开块表记录 Open the Block table record for read
  327. //// BlockTable acbt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  328. //// //// 以写方式打开模型空间块表记录 Open the Block table record Model space for write
  329. //// BlockTableRecord acblr = tran.GetObject(acbt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  330. //// //设置激活图层
  331. //// layertable = tran.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
  332. //// if (!layertable.Has("CJX"))
  333. //// {
  334. //// LayerTableRecord ltr = new LayerTableRecord();
  335. //// ltr.Name = "CJX";
  336. //// ObjectId layerid = layertable.Add(ltr);
  337. //// tran.AddNewlyCreatedDBObject(ltr, true);
  338. //// db.Clayer = layerid;
  339. //// }
  340. //// else
  341. //// {
  342. //// //设置首曲线为当期激活图层
  343. //// foreach (ObjectId id in layertable)
  344. //// {
  345. //// LayerTableRecord lyrtbrec = tran.GetObject(id, OpenMode.ForWrite) as LayerTableRecord;
  346. //// if (lyrtbrec.Name == "CJX")
  347. //// db.Clayer = id;
  348. //// }
  349. //// }
  350. //// Polyline3d cjxpl = new Polyline3d(Poly3dType.SimplePoly, cjcoll, false);
  351. //// cjxpl.Color = GrxCAD.Colors.Color.FromColor(System.Drawing.Color.Blue);
  352. //// acblr.AppendEntity(cjxpl);
  353. //// cjxpl.Layer = "CJX";
  354. //// tran.AddNewlyCreatedDBObject(cjxpl, true);
  355. //// //doc.Editor.WriteMessage("");
  356. //// tran.Commit();
  357. //// tran.Dispose();
  358. ////}
  359. #endregion
  360. double Xmin = 9999999999, Xmax = 0, Ymin = 9999999999, Ymax = 0;
  361. //获取该点集的最大值最小值
  362. foreach (Point3d pt3dcorner in cjcoll)
  363. {
  364. if (Xmin >= pt3dcorner.X)
  365. Xmin = pt3dcorner.X;
  366. if (Xmax < pt3dcorner.X)
  367. Xmax = pt3dcorner.X;
  368. if (Ymin >= pt3dcorner.Y)
  369. Ymin = pt3dcorner.Y;
  370. if (Ymax < pt3dcorner.Y)
  371. Ymax = pt3dcorner.Y;
  372. }
  373. if (Xmin > Xmax)
  374. {
  375. double tempt = 0;
  376. tempt = Xmin;
  377. Xmin = Xmax;
  378. Xmax = tempt;
  379. }
  380. if (Ymin > Ymax)
  381. {
  382. double tempt = 0;
  383. tempt = Ymin;
  384. Ymin = Ymax;
  385. Ymax = tempt;
  386. }
  387. #region//设置视图
  388. //设置当前视图
  389. ViewTableRecord viewtr = new ViewTableRecord();
  390. viewtr.CenterPoint = new Point2d(Xmin + (Xmax - Xmin) / 2, Ymin + (Ymax - Ymin) / 2);
  391. viewtr.ViewDirection = new Vector3d(0, 0, 1);
  392. viewtr.Height = Ymax - Ymin + 100;
  393. viewtr.Width = Xmax - Xmin + 100;
  394. doc.Editor.SetCurrentView(viewtr);
  395. #endregion
  396. //对选择到的首曲线进行处理
  397. string stttr = "";
  398. TypedValue[] tvtemp = new TypedValue[1];
  399. tvtemp.SetValue(new TypedValue((int)DxfCode.LayerName, sqxlayer), 0);
  400. SelectionFilter sftemp = new SelectionFilter(tvtemp);
  401. PromptSelectionResult psrtempt = doc.Editor.SelectCrossingWindow(new Point3d(Xmin, Ymin, 0), new Point3d(Xmax, Ymax, 0), sftemp);
  402. if (psrtempt.Status == PromptStatus.OK)
  403. {
  404. SelectionSet sstempt = psrtempt.Value;
  405. #region
  406. using (Transaction tran = doc.TransactionManager.StartTransaction())
  407. {
  408. //// 以只读方式打开块表记录 Open the Block table record for read
  409. BlockTable acbt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  410. //// 以写方式打开模型空间块表记录 Open the Block table record Model space for write
  411. BlockTableRecord acblr = tran.GetObject(acbt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  412. //设置激活图层
  413. layertable = tran.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
  414. //设置首曲线为当期激活图层
  415. foreach (ObjectId id in layertable)
  416. {
  417. LayerTableRecord lyrtbrec = tran.GetObject(id, OpenMode.ForWrite) as LayerTableRecord;
  418. if (lyrtbrec.Name == sqxlayer)
  419. db.Clayer = id;
  420. }
  421. foreach (ObjectId oidtempt in sstempt.GetObjectIds())
  422. {
  423. Entity entempt = tran.GetObject(oidtempt, OpenMode.ForWrite) as Entity;
  424. //if (ele1 > ele2)
  425. //{
  426. // double tempt = 0;
  427. // tempt = ele1;
  428. // ele1 = ele2;
  429. // ele2 = tempt;
  430. //}
  431. if (entempt is Polyline)
  432. {
  433. Polyline pltempt = entempt as Polyline;
  434. if (Convert.ToDouble(pltempt.Elevation.ToString("f1")) > ele1 && Convert.ToDouble(pltempt.Elevation.ToString("f1")) < ele2)
  435. {
  436. //裁剪节点集
  437. Point3dCollection pt3dcolltempt = new Point3dCollection();
  438. //裁剪节点集
  439. Point3dCollection pt3dcolltempt2 = new Point3dCollection();
  440. //裁剪节点
  441. Point3d pt3dtt1 = pltempt.GetClosestPointTo(pt1cjnode, false);
  442. Point3d pt3dtt2 = pltempt.GetClosestPointTo(pt3cjnode, false);
  443. //将线端点以外的点添加进裁剪接节点
  444. if (pt3dtt1 != new Point3d())
  445. {
  446. if (pt3dtt1 != pltempt.StartPoint && pt3dtt1 != pltempt.EndPoint)
  447. pt3dcolltempt.Add(pt3dtt1);
  448. }
  449. if (pt3dtt2 != new Point3d())
  450. {
  451. if (pt3dtt2 != pltempt.StartPoint && pt3dtt2 != pltempt.EndPoint)
  452. pt3dcolltempt.Add(pt3dtt2);
  453. }
  454. stttr += pltempt.StartPoint.X + "," + pltempt.StartPoint.Y + "," + pltempt.StartPoint.Z + "!" + pltempt.EndPoint.X + ","
  455. + pltempt.EndPoint.Y + "," + pltempt.EndPoint.Z + "\r\n";
  456. stttr += pt3dtt1.X + "@" + pt3dtt1.Y + "@" + pt3dtt1.Z + "!" + pt3dtt2.X + "@" + pt3dtt2.Y + "@" + pt3dtt2.Z + "\r\n";
  457. if (pt3dcolltempt.Count > 0)
  458. {
  459. for (int ch = 0; ch < pt3dcolltempt.Count; ch++)
  460. {
  461. pt3dcolltempt2.Add(pt3dcolltempt[pt3dcolltempt.Count - 1 - ch]);
  462. }
  463. DBObjectCollection dbojcoll = null;
  464. DBObjectCollection dbojcoll2 = null;
  465. try
  466. {
  467. dbojcoll = pltempt.GetSplitCurves(pt3dcolltempt);
  468. }
  469. catch { }
  470. try
  471. {
  472. dbojcoll2 = pltempt.GetSplitCurves(pt3dcolltempt2);
  473. }
  474. catch { }
  475. if (dbojcoll2.Count > dbojcoll.Count)
  476. dbojcoll = dbojcoll2;
  477. //if (dbojcoll.Count >0)
  478. if (dbojcoll.Count < 1)
  479. continue;
  480. //{
  481. if (pltempt.Closed)//如果该线闭合
  482. {
  483. foreach (DBObject enen in dbojcoll)
  484. {
  485. Polyline plcjh = enen as Polyline;
  486. Point3dCollection ptcolltempt3d = new Point3dCollection();
  487. plcjh.GetStretchPoints(ptcolltempt3d);
  488. Point3d ptptpt = ptcolltempt3d[(int)(ptcolltempt3d.Count / 2)];
  489. //Point3d ptptpt = ptcolltempt3d[1];
  490. Point2d ptinen1 = new Point2d();
  491. Point2d ptinen2 = new Point2d();
  492. //double llen = 9999;
  493. if (en1 is Polyline)
  494. {
  495. Polyline plen1 = en1 as Polyline;
  496. Point3d mypt = plen1.GetClosestPointTo(ptptpt, false);
  497. ptinen1 = new Point2d(mypt.X, mypt.Y);
  498. //llen = plen1.GetClosestPointTo(ptptpt, false).DistanceTo(ptptpt);
  499. }
  500. else if (en1 is Polyline2d)
  501. {
  502. Polyline2d pl2den1 = en1 as Polyline2d;
  503. Point3d mypt = pl2den1.GetClosestPointTo(ptptpt, false);
  504. ptinen1 = new Point2d(mypt.X, mypt.Y);
  505. //llen = pl2den1.GetClosestPointTo(ptptpt, false).DistanceTo(ptptpt);
  506. }
  507. if (en2 is Polyline)
  508. {
  509. Polyline plen2 = en2 as Polyline;
  510. Point3d mypt = plen2.GetClosestPointTo(ptptpt, false);
  511. ptinen2 = new Point2d(mypt.X, mypt.Y);
  512. }
  513. else if (en2 is Polyline2d)
  514. {
  515. Polyline2d pl2den2 = en2 as Polyline2d;
  516. Point3d mypt = pl2den2.GetClosestPointTo(ptptpt, false);
  517. ptinen2 = new Point2d(mypt.X, mypt.Y);
  518. }
  519. double llen = 9999;
  520. if ((ptinen1 != new Point2d()) && (ptinen2 != new Point2d()))
  521. {
  522. llen = ptinen1.GetDistanceTo(ptinen2);
  523. }
  524. if (llen < scalelen)
  525. {
  526. cdentity.Add(plcjh);
  527. }
  528. else
  529. {
  530. acblr.AppendEntity(plcjh);
  531. plcjh.Layer = sqxlayer;
  532. tran.AddNewlyCreatedDBObject(plcjh, true);
  533. }
  534. }
  535. }
  536. else
  537. {
  538. foreach (DBObject enen in dbojcoll)
  539. {
  540. Polyline plcjh = enen as Polyline;
  541. Point2d startpoint = new Point2d(plcjh.StartPoint.X, plcjh.StartPoint.Y);
  542. Point2d endpoint = new Point2d(plcjh.EndPoint.X, plcjh.EndPoint.Y);
  543. double linelength = startpoint.GetDistanceTo(endpoint);
  544. if (linelength > 1.5 * mapscale)
  545. {
  546. //if ((plcjh.GetClosestPointTo(pt3dtt1, false) == pt3dtt1) && (plcjh.GetClosestPointTo(pt3dtt2, false) == pt3dtt2))
  547. if ((pt3dtt1 == plcjh.StartPoint || pt3dtt1 == plcjh.EndPoint) && (pt3dtt2 == plcjh.StartPoint || pt3dtt2 == plcjh.EndPoint))
  548. {
  549. cdentity.Add(plcjh);
  550. }
  551. else
  552. {
  553. acblr.AppendEntity(plcjh);
  554. plcjh.Layer = sqxlayer;
  555. tran.AddNewlyCreatedDBObject(plcjh, true);
  556. }
  557. }
  558. else
  559. cdentity.Add(plcjh);
  560. }
  561. }
  562. //}
  563. entempt.Erase(true);
  564. //tran.Commit();
  565. }
  566. }
  567. }
  568. else if (entempt is Polyline2d)
  569. {
  570. ////裁剪二维多段线代码
  571. Polyline2d pl2dtempt = entempt as Polyline2d;
  572. if (Convert.ToDouble(pl2dtempt.Elevation.ToString("f1")) > ele1 && Convert.ToDouble(pl2dtempt.Elevation.ToString("f1")) < ele2)
  573. {
  574. Point3dCollection pt3dcolltempt = new Point3dCollection();
  575. Point3d pt3dtt1 = pl2dtempt.GetClosestPointTo(pt1cjnode, false);
  576. Point3d pt3dtt2 = pl2dtempt.GetClosestPointTo(pt3cjnode, false);
  577. //将线端点以外的点添加进裁剪接节点
  578. if (pt3dtt1 != new Point3d())
  579. {
  580. if (pt3dtt1 != pl2dtempt.StartPoint && pt3dtt1 != pl2dtempt.EndPoint)
  581. pt3dcolltempt.Add(pt3dtt1);
  582. }
  583. if (pt3dtt2 != new Point3d())
  584. {
  585. if (pt3dtt2 != pl2dtempt.StartPoint && pt3dtt2 != pl2dtempt.EndPoint)
  586. pt3dcolltempt.Add(pt3dtt2);
  587. }
  588. if (pt3dcolltempt.Count > 0)
  589. {
  590. Polyline pltempt = new Polyline();
  591. Point3dCollection pt3dpl2dcoll = new Point3dCollection();
  592. pl2dtempt.GetStretchPoints(pt3dpl2dcoll);
  593. //foreach (Point3d pt in pt3dpl2dcoll)
  594. //{
  595. // pltempt.AddVertexAt(0,pt,0,0,0);
  596. //}
  597. DBObjectCollection dbojcoll = pl2dtempt.GetSplitCurves(pt3dcolltempt);
  598. if (dbojcoll.Count>0)
  599. {
  600. foreach (DBObject enen in dbojcoll)
  601. {
  602. Polyline2d pl2dcjh = enen as Polyline2d;
  603. Point2d startpoint = new Point2d(pl2dcjh.StartPoint.X, pl2dcjh.StartPoint.Y);
  604. Point2d endpoint = new Point2d(pl2dcjh.EndPoint.X, pl2dcjh.EndPoint.Y);
  605. double linelength = startpoint.GetDistanceTo(endpoint);
  606. if (linelength > 1.5 * mapscale)
  607. {
  608. if ((pl2dcjh.GetClosestPointTo(pt3dtt1, false) == pt3dtt1) && (pl2dcjh.GetClosestPointTo(pt3dtt2, false) == pt3dtt2))
  609. if ((pt3dtt1 == pl2dcjh.StartPoint || pt3dtt1 == pl2dcjh.EndPoint) && (pt3dtt2 == pl2dcjh.StartPoint || pt3dtt2 == pl2dcjh.EndPoint))
  610. {
  611. cdentity.Add(pl2dcjh);
  612. }
  613. else
  614. {
  615. acblr.AppendEntity(pl2dcjh);
  616. pl2dcjh.Layer = sqxlayer;
  617. tran.AddNewlyCreatedDBObject(pl2dcjh, true);
  618. }
  619. }
  620. else
  621. cdentity.Add(pl2dcjh);
  622. }
  623. }
  624. entempt.Erase(true);
  625. tran.Commit();
  626. }
  627. }
  628. }
  629. #region//画裁剪线
  630. //Polyline3d cjxpl = new Polyline3d(Poly3dType.SimplePoly, rectcj, false);
  631. //if (!layertable.Has("CJX"))
  632. //{
  633. // LayerTableRecord ltr = new LayerTableRecord();
  634. // ltr.Name = "CJX";
  635. // ObjectId layerid = layertable.Add(ltr);
  636. // tran.AddNewlyCreatedDBObject(ltr, true);
  637. // //db.Clayer = layerid;
  638. //}
  639. //cjxpl.Color = GrxCAD.Colors.Color.FromColor(System.Drawing.Color.Blue);
  640. //acblr.AppendEntity(cjxpl);
  641. //cjxpl.Layer = "CJX";
  642. //tran.AddNewlyCreatedDBObject(cjxpl, true);
  643. ////doc.Editor.WriteMessage("");
  644. #endregion
  645. }
  646. tran.Commit();
  647. tran.Dispose();
  648. }
  649. #endregion
  650. }
  651. }
  652. }
  653. cjcoll.Clear();
  654. cjcoll = new Point3dCollection();
  655. }
  656. flag = false;
  657. continue;
  658. }//end if
  659. else
  660. {
  661. continue;
  662. }
  663. }
  664. }
  665. }
  666. }
  667. else
  668. return;
  669. #endregion
  670. #region
  671. ///画裁剪掉的线
  672. if (cdentity.Count > 0)
  673. {
  674. using (Transaction tran = doc.TransactionManager.StartTransaction())
  675. {
  676. //// 以只读方式打开块表记录 Open the Block table record for read
  677. BlockTable acbt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  678. //// 以写方式打开模型空间块表记录 Open the Block table record Model space for write
  679. BlockTableRecord acblr = tran.GetObject(acbt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  680. //设置激活图层
  681. layertable = tran.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
  682. if (!layertable.Has("CJSQX"))
  683. {
  684. LayerTableRecord ltr = new LayerTableRecord();
  685. ltr.Name = "CJSQX";
  686. ObjectId layerid = layertable.Add(ltr);
  687. tran.AddNewlyCreatedDBObject(ltr, true);
  688. db.Clayer = layerid;
  689. }
  690. else
  691. {
  692. foreach (ObjectId id in layertable)
  693. {
  694. LayerTableRecord lyrtbrec = tran.GetObject(id, OpenMode.ForWrite) as LayerTableRecord;
  695. if (lyrtbrec.Name == "CJSQX")
  696. db.Clayer = id;
  697. }
  698. }
  699. foreach (Entity enenen in cdentity)
  700. {
  701. enenen.Color = GrxCAD.Colors.Color.FromColor(System.Drawing.Color.Red);
  702. acblr.AppendEntity(enenen);
  703. enenen.Layer = "CJSQX";
  704. tran.AddNewlyCreatedDBObject(enenen, true);
  705. }
  706. doc.Editor.WriteMessage("已完成裁剪\r\n裁剪掉的等高线都放在CJSQX图层中\r\n");
  707. tran.Commit();
  708. tran.Dispose();
  709. }
  710. }
  711. #endregion
  712. documentlock.Dispose();
  713. doc.Editor.WriteMessage("程序运行结束");
  714. }
  715. public static int CompareDGXByElevation(Entity en1, Entity en2)
  716. {
  717. double ele1 = 0, ele2 = 0;
  718. //because just Polyline and Polyline2d two class in plentitycollection
  719. if (en1 is Polyline)
  720. {
  721. Polyline pl = en1 as Polyline;
  722. ele1 = Convert.ToDouble(pl.Elevation.ToString("f1"));
  723. }
  724. else
  725. {
  726. Polyline2d pl2d = en1 as Polyline2d;
  727. ele1 = Convert.ToDouble(pl2d.Elevation.ToString("f1"));
  728. }
  729. if (en2 is Polyline)
  730. {
  731. Polyline pl = en2 as Polyline;
  732. ele2 = Convert.ToDouble(pl.Elevation.ToString("f1"));
  733. }
  734. else
  735. {
  736. Polyline2d pl2d = en2 as Polyline2d;
  737. ele2 = Convert.ToDouble(pl2d.Elevation.ToString("f1"));
  738. }
  739. return (int)(ele1 - ele2);
  740. }
  741. public static Point3d GetClosestPoint(Point3d pt, Entity enpl)
  742. {
  743. if (enpl is Polyline)
  744. {
  745. Polyline pl = enpl as Polyline;
  746. return pl.GetClosestPointTo(pt, true);
  747. }
  748. else
  749. {
  750. Polyline2d pl2d = enpl as Polyline2d;
  751. return pl2d.GetClosestPointTo(pt, true);
  752. }
  753. }
  754. #endregion
  755. }
  756. }