XYZExtend.cs 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using Autodesk.Revit.DB;
  7. using SAGA.DotNetUtils;
  8. using SAGA.DotNetUtils.Extend;
  9. using SAGA.DotNetUtils.Geometry;
  10. using SAGA.RevitUtils.Infrastructure;
  11. using SAGA.RevitUtils.Utils;
  12. using Point = System.Windows.Point;
  13. namespace SAGA.RevitUtils.Extends
  14. {
  15. public static class UvExtend
  16. {
  17. public static UV MiddlePoint(this UV first, UV second)
  18. {
  19. double u = (first.U + second.U)/2;
  20. double v = (first.V + second.V)/2;
  21. UV midPoint = new UV(u, v);
  22. return midPoint;
  23. }
  24. }
  25. public static class XyzExtend
  26. {
  27. /// <summary>
  28. /// 获取两点距离信息
  29. /// </summary>
  30. /// <param name="p1"></param>
  31. /// <param name="p2"></param>
  32. /// <returns></returns>
  33. public static PointDistanceInfo DistanceInfoTo(this XYZ p1, XYZ p2)
  34. {
  35. return new PointDistanceInfo(p1, p2);
  36. }
  37. /// <summary>
  38. /// 判断多点是否在同一直线上,另外判断点是否在线段上
  39. /// </summary>
  40. /// <param name="p1">所要判断点</param>
  41. /// <param name="p2">另外一点或线起点</param>
  42. /// <param name="p3">另外一点或线段终点</param>
  43. /// <param name="flag">false:判断是否在同一直线 true:判断是否在线段上</param>
  44. /// <returns></returns>
  45. /// 增加可选参数flag用于判断当前点是否在两点所组成的线段上
  46. public static bool IsInLine(this XYZ p1, XYZ p2, XYZ p3, bool flag = false)
  47. {
  48. PointDistanceInfo distance1 = p1.DistanceInfoTo(p2);
  49. PointDistanceInfo distance2 = p2.DistanceInfoTo(p3);
  50. PointDistanceInfo distance3 = p3.DistanceInfoTo(p1);
  51. PointDistanceInfo max = PointDistanceInfo.Max(new PointDistanceInfo[] {distance1, distance2, distance3});
  52. Line lineMaxLength =max.P1.NewLine( max.P2);
  53. XYZ[] points = new XYZ[] {p1, p2, p3};
  54. foreach (XYZ p in points)
  55. {
  56. if (!lineMaxLength.IsOnCurve(p))
  57. {
  58. return false;
  59. }
  60. }
  61. if (flag == true) //判断p1是否在线段p2,p3上
  62. {
  63. double length = distance1.Distance + distance3.Distance;
  64. if (length.IsEqual(distance2.Distance)) //若点在线段上,则点到两端点的距离之和为线段长度
  65. {
  66. return true;
  67. }
  68. else
  69. {
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. /// <summary>
  76. /// 判断多点是否在同一直线
  77. /// </summary>
  78. /// <param name="pt"></param>
  79. /// <param name="points"></param>
  80. /// <returns></returns>
  81. public static bool IsInLine(this XYZ pt, XYZ[] points)
  82. {
  83. if (points == null || points.Length < 2)
  84. {
  85. throw new ArgumentException("points");
  86. }
  87. List<XYZ> listPs = new List<XYZ>();
  88. listPs.Add(pt);
  89. listPs.AddRange(points);
  90. for (int i = 0; i < listPs.Count; i++)
  91. {
  92. int intMaxIndex = i + 2;
  93. if (intMaxIndex < listPs.Count)
  94. {
  95. XYZ p1 = listPs[i];
  96. XYZ p2 = listPs[i + 1];
  97. XYZ p3 = listPs[i + 2];
  98. if (!p1.IsInLine(p2, p3))
  99. {
  100. return false;
  101. }
  102. }
  103. }
  104. return true;
  105. }
  106. /// <summary>
  107. /// 将点移到指定点
  108. /// </summary>
  109. /// <param name="point"></param>
  110. /// <param name="origin"></param>
  111. /// <returns></returns>
  112. public static XYZ ConvertToPoint(this XYZ point, XYZ origin)
  113. {
  114. Transform tf = new Transform(Transform.Identity);
  115. {
  116. tf.BasisX = XYZ.BasisX;
  117. tf.BasisY = XYZ.BasisY;
  118. tf.BasisZ = XYZ.BasisZ;
  119. tf.Origin = origin;
  120. }
  121. return tf.OfPoint(point);
  122. }
  123. /// <summary>
  124. /// 将水平面三维坐标转换为二维坐标求叉积判断旋转关系
  125. /// 两个点若是水平面的点可以直接使用,若是不是水平面需要进行坐标转换
  126. /// 大于0:顺时针 等于0:共线
  127. /// </summary>
  128. /// <param name="left"></param>
  129. /// <param name="right"></param>
  130. /// <returns></returns>
  131. public static double VectorProduct(this XYZ left, XYZ right)
  132. {
  133. UV uv1 = new UV(left.X, left.Y);
  134. UV uv2 = new UV(right.X, right.Y);
  135. return uv1.CrossProduct(uv2);
  136. }
  137. /// <summary>
  138. /// 判断多点是否在同一直线上
  139. /// </summary>
  140. /// <param name="pt"></param>
  141. /// <param name="points"></param>
  142. /// <returns></returns>
  143. public static bool IsOnLine(this XYZ pt, XYZ[] points)
  144. {
  145. return pt.IsInLine(points);
  146. }
  147. /// <summary>
  148. /// 判断点在线上
  149. /// </summary>
  150. /// <param name="pt"></param>
  151. /// <param name="curve"></param>
  152. /// <returns></returns>
  153. public static bool IsOnCurve(this XYZ pt, Curve curve)
  154. {
  155. if (curve is Line)
  156. {
  157. Line line = curve as Line;
  158. return pt.IsOnLine(line);
  159. }
  160. if (curve is Arc)
  161. {
  162. Arc arc = curve as Arc;
  163. return pt.IsInArc(arc);
  164. }
  165. return false;
  166. }
  167. /// <summary>
  168. /// 点在线段上
  169. /// </summary>
  170. /// <param name="pt"></param>
  171. /// <param name="line"></param>
  172. /// <returns></returns>
  173. public static bool IsOnLine(this XYZ pt, Line line)
  174. {
  175. return line.IsOnCurve(pt);
  176. }
  177. /// <summary>
  178. /// 判断点是否在弧上
  179. /// </summary>
  180. /// <param name="pt"></param>
  181. /// <param name="arc"></param>
  182. /// <returns></returns>
  183. public static bool IsInArc(this XYZ pt, Curve arc)
  184. {
  185. return arc.IsOnCurve(pt);
  186. }
  187. /// <summary>
  188. /// 判断曲线是否存在于一个曲线集合里
  189. /// </summary>
  190. /// <param name="pt"></param>
  191. /// <param name="listPoints"></param>
  192. /// <returns></returns>
  193. public static bool ExsitsInlist(this XYZ pt, IList<XYZ> listPoints, double tolerance = 0)
  194. {
  195. if (listPoints == null || listPoints.Count <= 0)
  196. {
  197. return false;
  198. }
  199. return listPoints.Contains(pt, new XyzEqualComparer(tolerance));
  200. }
  201. /// <summary>
  202. /// 转UV
  203. /// </summary>
  204. /// <param name="pt"></param>
  205. /// <returns></returns>
  206. public static UV ToUv(this XYZ pt)
  207. {
  208. return new UV(pt.X, pt.Y);
  209. }
  210. /// <summary>
  211. /// 转2DPoint
  212. /// </summary>
  213. /// <param name="pt"></param>
  214. /// <returns></returns>
  215. public static XYZ To2DPoint(this XYZ pt)
  216. {
  217. return pt.To2DPoint(XyzOptions.Z);
  218. }
  219. public static Point ToW2DPoint(this XYZ pt)
  220. {
  221. if (pt == null) return default(Point);
  222. return new Point(pt.X,pt.Y);
  223. }
  224. public static Geometry2D.PointD ToPointD(this XYZ pt)
  225. {
  226. return new Geometry2D.PointD(pt.X, pt.Y);
  227. }
  228. /// <summary>
  229. /// 转2DPoint
  230. /// </summary>
  231. /// <param name="pt"></param>
  232. /// <param name="optionWhereToZero"></param>
  233. /// <returns></returns>
  234. public static XYZ To2DPoint(this XYZ pt, XyzOptions optionWhereToZero)
  235. {
  236. switch (optionWhereToZero)
  237. {
  238. case XyzOptions.X:
  239. return new XYZ(0, pt.Y, pt.Z);
  240. case XyzOptions.Y:
  241. return new XYZ(pt.X, 0, pt.Z);
  242. case XyzOptions.Z:
  243. default:
  244. return new XYZ(pt.X, pt.Y, 0);
  245. }
  246. }
  247. /// <summary>
  248. /// 比较某一值
  249. /// </summary>
  250. /// <param name="pt1"></param>
  251. /// <param name="pt2"></param>
  252. /// <param name="whereToCompare"></param>
  253. /// <returns></returns>
  254. public static int CompareTo(this XYZ pt1, XYZ pt2, XyzOptions whereToCompare)
  255. {
  256. double a = 0, b = 0;
  257. switch (whereToCompare)
  258. {
  259. case XyzOptions.X:
  260. a = pt1.X;
  261. b = pt2.X;
  262. break;
  263. case XyzOptions.Y:
  264. a = pt1.Y;
  265. b = pt2.Y;
  266. break;
  267. case XyzOptions.Z:
  268. a = pt1.Z;
  269. b = pt2.Z;
  270. break;
  271. }
  272. if (a == b)
  273. {
  274. return 0;
  275. }
  276. return (a > b) ? 1 : -1;
  277. }
  278. /// <summary>
  279. /// 比较
  280. /// </summary>
  281. /// <param name="first"></param>
  282. /// <param name="second"></param>
  283. /// <param name="optionHowCompare"></param>
  284. /// <returns></returns>
  285. public static int CompareTo(this XYZ first, XYZ second, DirectionEnum optionHowCompare, bool ignoreZ = false)
  286. {
  287. if (!first.Z.IsEqual(second.Z, 0.0) && !ignoreZ)
  288. {
  289. if (first.Z <= second.Z)
  290. {
  291. return -1;
  292. }
  293. return 1;
  294. }
  295. switch (optionHowCompare)
  296. {
  297. case DirectionEnum.LeftRight:
  298. if (!first.X.IsEqual(second.X, 0.0))
  299. {
  300. if (first.X <= second.X)
  301. {
  302. return -1;
  303. }
  304. return 1;
  305. }
  306. return 0;
  307. case DirectionEnum.LeftRightBottomTop:
  308. if (!first.X.IsEqual(second.X, 0.0))
  309. {
  310. if (first.X <= second.X)
  311. {
  312. return -1;
  313. }
  314. return 1;
  315. }
  316. if (!first.Y.IsEqual(second.Y, 0.0))
  317. {
  318. if (first.Y >= second.Y)
  319. {
  320. return 1;
  321. }
  322. return -1;
  323. }
  324. return 0;
  325. case DirectionEnum.LeftRightTopBottom:
  326. if (!first.X.IsEqual(second.X, 0.0))
  327. {
  328. if (first.X <= second.X)
  329. {
  330. return -1;
  331. }
  332. return 1;
  333. }
  334. if (!first.Y.IsEqual(second.Y, 0.0))
  335. {
  336. if (first.Y >= second.Y)
  337. {
  338. return -1;
  339. }
  340. return 1;
  341. }
  342. return 0;
  343. case DirectionEnum.RightLeft:
  344. if (!first.X.IsEqual(second.X, 0.0))
  345. {
  346. if (first.X <= second.X)
  347. {
  348. return 1;
  349. }
  350. return -1;
  351. }
  352. return 0;
  353. case DirectionEnum.RightLeftBottomTop:
  354. if (!first.X.IsEqual(second.X, 0.0))
  355. {
  356. if (first.X <= second.X)
  357. {
  358. return 1;
  359. }
  360. return -1;
  361. }
  362. if (!first.Y.IsEqual(second.Y, 0.0))
  363. {
  364. if (first.Y >= second.Y)
  365. {
  366. return 1;
  367. }
  368. return -1;
  369. }
  370. return 0;
  371. case DirectionEnum.RightLeftTopBottom:
  372. if (!first.X.IsEqual(second.X, 0.0))
  373. {
  374. if (first.X <= second.X)
  375. {
  376. return 1;
  377. }
  378. return -1;
  379. }
  380. if (!first.Y.IsEqual(second.Y, 0.0))
  381. {
  382. if (first.Y >= second.Y)
  383. {
  384. return -1;
  385. }
  386. return 1;
  387. }
  388. return 0;
  389. case DirectionEnum.BottomTop:
  390. if (!first.Y.IsEqual(second.Y, 0.0))
  391. {
  392. if (first.Y <= second.Y)
  393. {
  394. return -1;
  395. }
  396. return 1;
  397. }
  398. return 0;
  399. case DirectionEnum.BottomTopLeftRight:
  400. if (!first.Y.IsEqual(second.Y, 0.0))
  401. {
  402. if (first.Y >= second.Y)
  403. {
  404. return 1;
  405. }
  406. return -1;
  407. }
  408. if (!first.X.IsEqual(second.X, 0.0))
  409. {
  410. if (first.X <= second.X)
  411. {
  412. return -1;
  413. }
  414. return 1;
  415. }
  416. return 0;
  417. case DirectionEnum.BottomTopRightLeft:
  418. if (!first.Y.IsEqual(second.Y, 0.0))
  419. {
  420. if (first.Y >= second.Y)
  421. {
  422. return 1;
  423. }
  424. return -1;
  425. }
  426. if (!first.X.IsEqual(second.X, 0.0))
  427. {
  428. if (first.X <= second.X)
  429. {
  430. return 1;
  431. }
  432. return -1;
  433. }
  434. return 0;
  435. case DirectionEnum.TopBottom:
  436. if (!first.Y.IsEqual(second.Y, 0.0))
  437. {
  438. if (first.Y <= second.Y)
  439. {
  440. return 1;
  441. }
  442. return -1;
  443. }
  444. return 0;
  445. case DirectionEnum.TopBottomLeftRight:
  446. case DirectionEnum.LeftTopRightBottom:
  447. if (!first.Y.IsEqual(second.Y, 0.0))
  448. {
  449. if (first.Y >= second.Y)
  450. {
  451. return -1;
  452. }
  453. return 1;
  454. }
  455. if (!first.X.IsEqual(second.X, 0.0))
  456. {
  457. if (first.X <= second.X)
  458. {
  459. return -1;
  460. }
  461. return 1;
  462. }
  463. return 0;
  464. case DirectionEnum.TopBottomRightLeft:
  465. if (!first.Y.IsEqual(second.Y, 0.0))
  466. {
  467. if (first.Y >= second.Y)
  468. {
  469. return -1;
  470. }
  471. return 1;
  472. }
  473. if (!first.X.IsEqual(second.X, 0.0))
  474. {
  475. if (first.X <= second.X)
  476. {
  477. return 1;
  478. }
  479. return -1;
  480. }
  481. return 0;
  482. }
  483. if (first.Y.IsEqual(second.Y, 0.0))
  484. {
  485. if (first.X.IsEqual(second.X, 0.0))
  486. {
  487. return 0;
  488. }
  489. if (optionHowCompare == DirectionEnum.LeftBottomRightTop)
  490. {
  491. if (first.X <= second.X)
  492. {
  493. return -1;
  494. }
  495. return 1;
  496. }
  497. if (first.X >= second.X)
  498. {
  499. return -1;
  500. }
  501. return 1;
  502. }
  503. if (first.Y <= second.Y)
  504. {
  505. return -1;
  506. }
  507. return 1;
  508. }
  509. /// <summary>
  510. /// 获取相对X轴倾斜角度,忽略Z轴,如果取圆上的点,则应该把圆上的点作为,起点 startPoint, 而圆心作为终点 endPoint
  511. /// </summary>
  512. /// <param name="endPt"></param>
  513. /// <param name="centerPt"></param>
  514. /// <returns></returns>
  515. public static double GetSlopeAngle(this XYZ endPt, XYZ centerPt)
  516. {
  517. XYZ vec = endPt.Subtract(centerPt);
  518. return (vec.QuadrantAngle().ToAngle() + 360)%360;
  519. }
  520. /// <summary>
  521. /// 获取相对X轴倾斜弧度
  522. /// </summary>
  523. /// <param name="endPt"></param>
  524. /// <param name="centerPt"></param>
  525. /// <returns></returns>
  526. public static double GetSlopeRadian(this XYZ endPt, XYZ centerPt)
  527. {
  528. XYZ vec = endPt.Subtract(centerPt);
  529. return vec.QuadrantAngle();
  530. }
  531. /// <summary>
  532. /// 矢量的象限角0到2Pi
  533. /// </summary>
  534. /// <param name="vec"></param>
  535. /// <param name="xAxis"></param>
  536. /// <returns></returns>
  537. public static double QuadrantAngle(this XYZ vec, XYZ xAxis = null)
  538. {
  539. if (xAxis == null)
  540. {
  541. xAxis = XYZ.BasisX;
  542. }
  543. double dRtn = xAxis.AngleOnPlaneTo(vec, XYZ.BasisZ);
  544. if (dRtn.IsEqual(2*Math.PI))
  545. {
  546. dRtn = 0;
  547. }
  548. return dRtn;
  549. }
  550. /// <summary>
  551. /// 两个平面向量的夹角(锐角)
  552. /// </summary>
  553. /// <param name="vec1"></param>
  554. /// <param name="vec2"></param>
  555. /// <returns></returns>
  556. public static double GetAngle(this XYZ vec1, XYZ vec2)
  557. {
  558. double dRtn;
  559. if (vec1.IsSameDirection(vec2) || vec1.IsOppositeDirection(vec2))
  560. dRtn = 0;
  561. else
  562. //主要区别在这里,因为Z轴的方向不同
  563. dRtn = vec1.AngleOnPlaneTo(vec2, (vec1.CrossProduct(vec2)).Normalize());
  564. if (dRtn.IsEqual(2*Math.PI))
  565. {
  566. dRtn = 0;
  567. }
  568. return dRtn;
  569. }
  570. /// <summary>
  571. /// 两个平面向量的夹角,逆时针角
  572. /// </summary>
  573. /// <param name="vec1"></param>
  574. /// <param name="vec2"></param>
  575. /// <returns></returns>
  576. public static double GetAngle2(this XYZ vec1, XYZ vec2)
  577. {
  578. double dRtn = vec1.AngleOnPlaneTo(vec2, XYZ.BasisZ);
  579. if (dRtn.IsEqual(2*Math.PI))
  580. {
  581. dRtn = 0;
  582. }
  583. return dRtn;
  584. }
  585. /// <summary>
  586. /// 圆弧凸度
  587. /// </summary>
  588. /// <param name="ptC">圆心</param>
  589. /// <param name="ptS">起点</param>
  590. /// <param name="ptE">终点</param>
  591. /// <returns></returns>
  592. public static double GetArcBulge(this XYZ ptC, XYZ ptS, XYZ ptE)
  593. {
  594. if (ptE.IsEqual(ptS))
  595. return 0;
  596. XYZ vec1 = ptS - ptC;
  597. XYZ vec2 = ptE - ptC;
  598. double angle = vec1.AngleOnPlaneTo(vec2, XYZ.BasisZ);
  599. if (angle.IsLess(-Math.PI))
  600. angle += Math.PI*2;
  601. else if (angle.IsThan(Math.PI))
  602. angle -= Math.PI*2;
  603. return Math.Tan(angle/4);
  604. }
  605. /// <summary>
  606. /// 修改x值
  607. /// </summary>
  608. /// <param name="p1"></param>
  609. /// <returns></returns>
  610. public static XYZ NewX(this XYZ p1)
  611. {
  612. return p1.NewX(null);
  613. }
  614. /// <summary>
  615. /// 修改x值
  616. /// </summary>
  617. /// <param name="p1"></param>
  618. /// <param name="x"></param>
  619. /// <returns></returns>
  620. public static XYZ NewX(this XYZ p1, double? x)
  621. {
  622. double xValue = x.HasValue ? x.Value : 0;
  623. double y = p1.Y;
  624. double z = p1.Z;
  625. XYZ result = new XYZ(xValue, y, z);
  626. return result;
  627. }
  628. /// <summary>
  629. /// 增加x值
  630. /// </summary>
  631. /// <param name="p1"></param>
  632. /// <param name="x"></param>
  633. /// <returns></returns>
  634. public static XYZ AddX(this XYZ p1, double? x)
  635. {
  636. double xValue = x.HasValue ? x.Value : 0;
  637. double y = p1.Y;
  638. double z = p1.Z;
  639. XYZ result = new XYZ(p1.X + xValue, y, z);
  640. return result;
  641. }
  642. /// <summary>
  643. /// 修改y值
  644. /// </summary>
  645. /// <param name="p1"></param>
  646. /// <returns></returns>
  647. public static XYZ NewY(this XYZ p1)
  648. {
  649. return p1.NewY(null);
  650. }
  651. /// <summary>
  652. /// 修改y值
  653. /// </summary>
  654. /// <param name="p1"></param>
  655. /// <param name="y"></param>
  656. /// <returns></returns>
  657. public static XYZ NewY(this XYZ p1, double? y)
  658. {
  659. double x = p1.X;
  660. double yValue = y.HasValue ? y.Value : 0;
  661. double z = p1.Z;
  662. XYZ result = new XYZ(x, yValue, z);
  663. return result;
  664. }
  665. /// <summary>
  666. /// 增加y值
  667. /// </summary>
  668. /// <param name="p1"></param>
  669. /// <param name="y"></param>
  670. /// <returns></returns>
  671. public static XYZ AddY(this XYZ p1, double? y)
  672. {
  673. double x = p1.X;
  674. double yValue = y.HasValue ? y.Value : 0;
  675. double z = p1.Z;
  676. XYZ result = new XYZ(x, p1.Y + yValue, z);
  677. return result;
  678. }
  679. /// <summary>
  680. /// 修改z值
  681. /// </summary>
  682. /// <param name="p1"></param>
  683. /// <returns></returns>
  684. public static XYZ NewZ(this XYZ p1)
  685. {
  686. return p1.NewZ(null);
  687. }
  688. /// <summary>
  689. /// 修改z值
  690. /// </summary>
  691. /// <param name="p1"></param>
  692. /// <param name="z"></param>
  693. /// <returns></returns>
  694. public static XYZ NewZ(this XYZ p1, double? z)
  695. {
  696. double x = p1.X;
  697. double y = p1.Y;
  698. double zValue = z.HasValue ? z.Value : 0;
  699. XYZ result = new XYZ(x, y, zValue);
  700. return result;
  701. }
  702. /// <summary>
  703. /// 增加z值
  704. /// </summary>
  705. /// <param name="p1"></param>
  706. /// <param name="z"></param>
  707. /// <returns></returns>
  708. public static XYZ AddZ(this XYZ p1, double? z)
  709. {
  710. double x = p1.X;
  711. double y = p1.Y;
  712. double zValue = z.HasValue ? z.Value : 0;
  713. XYZ result = new XYZ(x, y, p1.Z + zValue);
  714. return result;
  715. }
  716. /// <summary>
  717. /// 创建线
  718. /// </summary>
  719. /// <param name="first"></param>
  720. /// <param name="second"></param>
  721. /// <returns></returns>
  722. public static Line NewLine(this XYZ first, XYZ second)
  723. {
  724. return Line.CreateBound(first, second);
  725. }
  726. /// <summary>
  727. /// 两个坐标是否相等(包含Z坐标)
  728. /// </summary>
  729. /// <param name="first"></param>
  730. /// <param name="second"></param>
  731. /// <param name="tolerance"></param>
  732. /// <returns></returns>
  733. public static bool IsEqual(this XYZ first, XYZ second, double tolerance = 0)
  734. {
  735. return (first.X.IsEqual(second.X, tolerance)
  736. && first.Y.IsEqual(second.Y, tolerance)
  737. && first.Z.IsEqual(second.Z, tolerance));
  738. }
  739. /// <summary>
  740. /// 两个坐标是否相等(不包含Z坐标)
  741. /// </summary>
  742. /// <param name="first"></param>
  743. /// <param name="second"></param>
  744. /// <param name="tolerance">误差范围</param>ls 2015-01-04添加
  745. /// <returns></returns>
  746. public static bool IsEqual2(this XYZ first, XYZ second, double tolerance = 0)
  747. {
  748. return first.NewZ().IsEqual(second.NewZ(), tolerance);
  749. }
  750. /// <summary>
  751. /// 相等处理
  752. /// </summary>
  753. /// <param name="first"></param>
  754. /// <param name="second"></param>
  755. /// <param name="whereNotEqual"></param>
  756. /// <param name="tolerance"></param>
  757. /// <returns></returns>
  758. public static bool IsEqual(this XYZ first, XYZ second, XyzOptions whereNotEqual, double tolerance = 0)
  759. {
  760. switch (whereNotEqual)
  761. {
  762. case XyzOptions.X:
  763. return (first.Y.IsEqual(second.Y, tolerance)
  764. && first.Z.IsEqual(second.Z, tolerance));
  765. case XyzOptions.Y:
  766. return (first.X.IsEqual(second.X, tolerance)
  767. && first.Z.IsEqual(second.Z, tolerance));
  768. case XyzOptions.Z:
  769. default:
  770. return (first.X.IsEqual(second.X, tolerance)
  771. && first.Y.IsEqual(second.Y, tolerance));
  772. }
  773. }
  774. /// <summary>
  775. /// 转API
  776. /// </summary>
  777. /// <param name="xyz"></param>
  778. /// <returns></returns>
  779. public static XYZ ConvertToApi(this XYZ xyz)
  780. {
  781. double x = xyz.X.ToApi();
  782. double y = xyz.Y.ToApi();
  783. double z = xyz.Z.ToApi();
  784. return new XYZ(x, y, z);
  785. }
  786. /// <summary>
  787. /// 转项目单位
  788. /// </summary>
  789. /// <param name="xyz"></param>
  790. /// <returns></returns>
  791. public static XYZ ConvertFromApi(this XYZ xyz)
  792. {
  793. double x = xyz.X.FromApi();
  794. double y = xyz.Y.FromApi();
  795. double z = xyz.Z.FromApi();
  796. return new XYZ(x, y, z);
  797. }
  798. public static XYZ FromApi(this XYZ xyz,DisplayUnitType unitType=DisplayUnitType.DUT_MILLIMETERS)
  799. {
  800. double x = xyz.X.FromApi(unitType);
  801. double y = xyz.Y.FromApi(unitType);
  802. double z = xyz.Z.FromApi(unitType);
  803. return new XYZ(x, y, z);
  804. }
  805. /// <summary>
  806. /// 取整
  807. /// </summary>
  808. /// <param name="xyz"></param>
  809. /// <returns></returns>
  810. public static XYZ Round(this XYZ xyz, int digits = 0)
  811. {
  812. return new XYZ(xyz.X.Round(digits), xyz.Y.Round(digits), xyz.Z.Round(digits));
  813. }
  814. /// <summary>
  815. ///
  816. /// </summary>
  817. /// <param name="xyz"></param>
  818. /// <param name="format"></param>
  819. /// <returns></returns>
  820. public static string ToString(this XYZ xyz, string format)
  821. {
  822. if (format == null || format.Length <= 0)
  823. {
  824. format = "{0},{1},{2}";
  825. }
  826. return string.Format(format, xyz.X, xyz.Y, xyz.Z);
  827. }
  828. /// <summary>
  829. /// 转换为字符串
  830. /// </summary>
  831. /// <param name="xyz"></param>
  832. /// <returns></returns>
  833. public static string ToString2(this XYZ xyz)
  834. {
  835. string format = "[{0},{1},{2}]";
  836. return xyz.ToString(format);
  837. }
  838. /// <summary>
  839. /// 转换为二维坐标字符串
  840. /// </summary>
  841. /// <param name="xyz"></param>
  842. /// <returns></returns>
  843. public static string ToString2D(this XYZ xyz)
  844. {
  845. string format = "[{0}, {1}]";
  846. return string.Format(format, Math.Round(xyz.X, 2), Math.Round(xyz.Y, 2));
  847. }
  848. /// <summary>
  849. /// Transform old coordinate system in the new coordinate system
  850. /// </summary>
  851. /// <param name="pt">the XYZ which need to be transformed</param>
  852. /// <param name="transform">the value of the coordinate system to be transformed</param>
  853. /// <returns>the new XYZ which has been transformed</returns>
  854. public static XYZ TransformPoint(this XYZ pt, Transform transform)
  855. {
  856. //get the coordinate value in X, Y, Z axis
  857. double x = pt.X;
  858. double y = pt.Y;
  859. double z = pt.Z;
  860. //transform basis of the old coordinate system in the new coordinate system
  861. XYZ b0 = transform.get_Basis(0);
  862. XYZ b1 = transform.get_Basis(1);
  863. XYZ b2 = transform.get_Basis(2);
  864. XYZ origin = transform.Origin;
  865. //transform the origin of the old coordinate system in the new coordinate system
  866. double xTemp = x*b0.X + y*b1.X + z*b2.X + origin.X;
  867. double yTemp = x*b0.Y + y*b1.Y + z*b2.Y + origin.Y;
  868. double zTemp = x*b0.Z + y*b1.Z + z*b2.Z + origin.Z;
  869. return new XYZ(xTemp, yTemp, zTemp);
  870. }
  871. /// <summary>
  872. /// Move a pt a give offset along a given direction
  873. /// </summary>
  874. /// <param name="pt">the pt need to move</param>
  875. /// <param name="direction">the direction the pt move to</param>
  876. /// <param name="offset">indicate how long to move</param>
  877. /// <returns>the moved pt</returns>
  878. public static XYZ OffsetPoint(this XYZ pt, XYZ direction, double offset)
  879. {
  880. XYZ directUnit = direction.Normalize();
  881. XYZ offsetVect = directUnit.Multiply(offset);
  882. return pt.Add(offsetVect);
  883. }
  884. /// <summary>
  885. /// 两点间的中点
  886. /// </summary>
  887. /// <param name="first"></param>
  888. /// <param name="second"></param>
  889. /// <returns></returns>
  890. public static XYZ MiddlePoint(this XYZ first, XYZ second)
  891. {
  892. double x = (first.X + second.X)/2;
  893. double y = (first.Y + second.Y)/2;
  894. double z = (first.Z + second.Z)/2;
  895. XYZ midPoint = new XYZ(x, y, z);
  896. return midPoint;
  897. }
  898. /// <summary>
  899. /// 两点距离(参考z轴)
  900. /// </summary>
  901. /// <param name="first">the first pt</param>
  902. /// <param name="second">the first pt</param>
  903. /// <returns>the distance of two points</returns>
  904. public static double FindDistance(this XYZ first, XYZ second)
  905. {
  906. return first.DistanceTo(second);
  907. }
  908. /// <summary>
  909. /// 两点距离(忽略z轴)
  910. /// </summary>
  911. /// <param name="first">the first pt</param>
  912. /// <param name="second">the first pt</param>
  913. /// <returns>the distance of two points</returns>
  914. public static double FindDistance2(this XYZ first, XYZ second)
  915. {
  916. return first.NewZ().FindDistance(second.NewZ());
  917. }
  918. /// <summary>
  919. /// Find the direction vector from first pt to second pt
  920. /// </summary>
  921. /// <param name="first">the first pt</param>
  922. /// <param name="second">the second pt</param>
  923. /// <returns>the direction vector</returns>
  924. public static XYZ FindDirection(this XYZ first, XYZ second)
  925. {
  926. return second.Subtract(first).Normalize();
  927. }
  928. /// <summary>
  929. /// 点在圆内
  930. /// </summary>
  931. /// <param name="pt"></param>
  932. /// <param name="curveList"></param>
  933. /// <returns></returns>
  934. public static bool PointInCircle(this XYZ pt, List<Curve> curveList)
  935. {
  936. if (curveList.Count != 2)
  937. {
  938. return false;
  939. }
  940. Arc arc1 = curveList[0].GetArc();
  941. Arc arc2 = curveList[1].GetArc();
  942. if ((arc1 == null) || (arc2 == null))
  943. {
  944. return false;
  945. }
  946. CurveLoop loop = CurveLoop.Create(curveList);
  947. if (loop.IsOpen())
  948. {
  949. return false;
  950. }
  951. XYZ ptCenter = arc1.StartPoint().MiddlePoint(arc1.EndPoint());
  952. double dR = arc1.StartPoint().FindDistance2(arc1.EndPoint())/2;
  953. if (pt.FindDistance2(ptCenter).IsLess(dR))
  954. {
  955. return true;
  956. }
  957. return false;
  958. }
  959. /// <summary>
  960. /// 判断点是否在多边形内
  961. /// </summary>
  962. /// <param name="pt"></param>
  963. /// <param name="curveList">组成多边形的线</param>
  964. /// <returns></returns>
  965. /// 原理:根据点水平向左做射线,若与多边形单边交点为奇数则此点在内部,反之在外部
  966. public static bool PointInPoly(this XYZ pt, List<Curve> curveList)
  967. {
  968. Line line = Line.CreateUnbound(pt, new XYZ(-1, 0, 0)); //水平向左射线
  969. int i = 0;
  970. List<double> dList = new List<double>();
  971. curveList.ForEach(delegate(Curve p)
  972. {
  973. dList.Add(p.StartPoint().Y);
  974. dList.Add(p.EndPoint().Y);
  975. });
  976. foreach (Curve curve in curveList)
  977. {
  978. if (curve.StartPoint().Y.IsEqual(curve.EndPoint().Y)) continue;
  979. if (curve.IsIntersection(line))
  980. {
  981. XYZ xyz = curve.GetIntersections(line)[0];
  982. if (xyz.X > pt.X) continue;
  983. double minY = Math.Min(curve.StartPoint().Y, curve.EndPoint().Y);
  984. double maxY = Math.Max(curve.StartPoint().Y, curve.EndPoint().Y);
  985. //如果交点为最高点或最低点,则点肯定不在多边形内
  986. if (xyz.Y.IsEqual(dList.Max()) || xyz.Y.IsEqual(dList.Min())) continue;
  987. //判断是交点为线段端点,只取纵坐标最大的那个
  988. if (xyz.Y != minY)
  989. {
  990. i++;
  991. }
  992. }
  993. }
  994. if (i%2 == 0) return false;
  995. return true;
  996. }
  997. /// <summary>
  998. /// 判断点是否在多边形中
  999. /// </summary>
  1000. /// <param name="pt"></param>
  1001. /// <param name="listProfile"></param>
  1002. /// <returns></returns>
  1003. public static bool PointInPolygon2(this XYZ pt, IList<Curve> listProfile)
  1004. {
  1005. try
  1006. {
  1007. List<XYZ> listPt = new List<XYZ>();
  1008. foreach (Curve curve in listProfile)
  1009. {
  1010. XYZ ptS = curve.StartPoint();
  1011. XYZ ptE = curve.EndPoint();
  1012. if (!ptS.ExsitsInlist(listPt))
  1013. {
  1014. listPt.Add(ptS);
  1015. }
  1016. if (!ptE.ExsitsInlist(listPt))
  1017. {
  1018. listPt.Add(ptE);
  1019. }
  1020. }
  1021. double anglesum = 0;
  1022. int n = listPt.Count;
  1023. for (int i = 0; i < n; i++)
  1024. {
  1025. XYZ pt1 = listPt[i];
  1026. XYZ pt2 = listPt[(i + 1)%n];
  1027. XYZ v1 = pt1 - pt;
  1028. XYZ v2 = pt2 - pt;
  1029. double len1 = v1.GetLength();
  1030. double len2 = v2.GetLength();
  1031. if ((len1*len2).IsEqual(0))
  1032. {
  1033. return true;
  1034. }
  1035. else
  1036. {
  1037. double cosa = v1.DotProduct(v2)/(len1*len2);
  1038. anglesum += Math.Acos(cosa);
  1039. }
  1040. }
  1041. return anglesum.IsEqual(2*Math.PI);
  1042. }
  1043. catch
  1044. {
  1045. return false;
  1046. }
  1047. }
  1048. /// <summary>
  1049. /// 点按照Z轴旋转
  1050. /// </summary>
  1051. /// <param name="pt"></param>
  1052. /// <param name="angle">弧度(逆时针为正角)</param>
  1053. /// <param name="origin"></param>
  1054. /// <returns></returns>
  1055. public static XYZ PointRotate(this XYZ pt, double angle, XYZ origin)
  1056. {
  1057. XYZ vec = new XYZ(pt.X - origin.X, pt.Y - origin.Y, pt.Z - origin.Z);
  1058. vec = vec.VectorRotate(angle, origin);
  1059. return origin + vec;
  1060. }
  1061. /// <summary>
  1062. /// 向量以Z轴为参照轴绕原点旋转
  1063. /// </summary>
  1064. /// <param name="vec"></param>
  1065. /// <param name="angle"></param>
  1066. /// <returns></returns>
  1067. public static XYZ VectorRotate(this XYZ vec, double angle)
  1068. {
  1069. return vec.VectorRotate(angle, XYZ.Zero);
  1070. }
  1071. /// <summary>
  1072. /// 向量(点)绕任意轴旋转
  1073. /// </summary>
  1074. /// <param name="vec"></param>
  1075. /// <param name="axis">参照轴</param>
  1076. /// <param name="angle"></param>
  1077. /// <returns></returns>
  1078. public static XYZ VectorRotate(this XYZ vec, XYZ axis, double angle)
  1079. {
  1080. return vec.VectorRotate(XYZ.Zero, axis, angle);
  1081. }
  1082. /// <summary>
  1083. /// 向量绕Z轴旋转
  1084. /// </summary>
  1085. /// <param name="vec"></param>
  1086. /// <param name="angle">弧度</param>
  1087. /// <param name="origin">参照点</param>
  1088. /// <returns></returns>
  1089. public static XYZ VectorRotate(this XYZ vec, double angle, XYZ origin)
  1090. {
  1091. return vec.VectorRotate(origin, XYZ.BasisZ, angle);
  1092. }
  1093. /// <summary>
  1094. /// 向量绕任意轴旋转
  1095. /// </summary>
  1096. /// <param name="vec"></param>
  1097. /// <param name="origin">旋转所绕点</param>
  1098. /// <param name="axis">旋转所绕的轴</param>
  1099. /// <param name="angle">旋转角度</param>
  1100. /// <returns></returns>
  1101. public static XYZ VectorRotate(this XYZ vec, XYZ origin, XYZ axis, double angle)
  1102. {
  1103. Transform transform = Transform.CreateRotationAtPoint(axis,angle,origin);
  1104. return transform.OfVector(vec);
  1105. }
  1106. /// <summary>
  1107. /// 向量反向
  1108. /// </summary>
  1109. /// <param name="vec"></param>
  1110. /// <returns></returns>
  1111. [Obsolete("方法已过时,请使用API中方法XYZ.Negate()")] //ls 2015-08-15
  1112. public static XYZ Revert(this XYZ vec)
  1113. {
  1114. return -vec;
  1115. }
  1116. /// <summary>
  1117. /// 两向量垂直
  1118. /// </summary>
  1119. /// <param name="vec1"></param>
  1120. /// <param name="vec2"></param>
  1121. /// <returns></returns>
  1122. public static bool IsVertical(this XYZ vec1, XYZ vec2, double tolerance = 0.0)
  1123. {
  1124. return vec1.DotProduct(vec2).IsEqual(0.0, tolerance);
  1125. }
  1126. /// <summary>
  1127. /// 两向量平行
  1128. /// </summary>
  1129. /// <param name="vec1"></param>
  1130. /// <param name="vec2"></param>
  1131. /// <returns></returns>
  1132. public static bool IsParallel(this XYZ vec1, XYZ vec2, double tolerance = 0)
  1133. {
  1134. return vec1.Normalize().IsEqual(vec2.Normalize(), tolerance)
  1135. || vec1.Normalize().IsEqual(vec2.Normalize().Negate(), tolerance);
  1136. }
  1137. /// <summary>
  1138. /// 向量方向相同
  1139. /// </summary>
  1140. /// <param name="firstVec"></param>
  1141. /// <param name="secondVec"></param>
  1142. /// <returns></returns>
  1143. public static bool IsSameDirection(this XYZ firstVec, XYZ secondVec)
  1144. {
  1145. //a.b = |a|*|b|*cos<a,b>
  1146. // get the unit vector for two vectors
  1147. XYZ first = firstVec.Normalize();
  1148. XYZ second = secondVec.Normalize();
  1149. // if the dot product of two unit vectors is equal to 1, return true
  1150. #region ls 2015-03-14注释
  1151. double dot = first.DotProduct(second);
  1152. return (dot.IsEqual(1, 0.001));
  1153. #endregion
  1154. //return firstVec.IsEqual(secondVec);
  1155. }
  1156. /// <summary>
  1157. /// 向量方向相反
  1158. /// </summary>
  1159. /// <param name="firstVec"></param>
  1160. /// <param name="secondVec"></param>
  1161. /// <returns></returns>
  1162. public static bool IsOppositeDirection(this XYZ firstVec, XYZ secondVec)
  1163. {
  1164. //a.b = |a|*|b|*cos<a,b>
  1165. // get the unit vector for two vectors
  1166. XYZ first = firstVec.Normalize();
  1167. XYZ second = secondVec.Normalize();
  1168. // if the dot product of two unit vectors is equal to -1, return true
  1169. double dot = first.DotProduct(second);
  1170. return (dot.IsEqual(-1));
  1171. }
  1172. /// <summary>
  1173. /// 比较
  1174. /// </summary>
  1175. /// <param name="first"></param>
  1176. /// <param name="second"></param>
  1177. /// <param name="curve"></param>
  1178. /// <param name="blnStart">从起点开始</param>
  1179. /// <returns></returns>
  1180. //public static int CompareTo(this XYZ first, XYZ second, Curve curve, bool blnStart)
  1181. //{
  1182. // if (!blnStart)
  1183. // {
  1184. // curve = curve.Reverse();
  1185. // }
  1186. // if (curve.IsLine())
  1187. // {
  1188. // Line line = curve.GetLine();
  1189. // double xDist = line.StartPoint().FindDistance2(first);
  1190. // double yDist = line.StartPoint().FindDistance2(second);
  1191. // if (xDist.IsEqual(yDist))
  1192. // {
  1193. // return 0;
  1194. // }
  1195. // else
  1196. // {
  1197. // return xDist > yDist ? 1 : -1;
  1198. // }
  1199. // }
  1200. // if (curve.IsLine())
  1201. // {
  1202. // Line line = curve.GetLine();
  1203. // double xDist = line.StartPoint().FindDistance2(first);
  1204. // double yDist = line.StartPoint().FindDistance2(second);
  1205. // if (xDist.IsEqual(yDist))
  1206. // {
  1207. // return 0;
  1208. // }
  1209. // else
  1210. // {
  1211. // return xDist > yDist ? 1 : -1;
  1212. // }
  1213. // }
  1214. // return 0;
  1215. //}
  1216. public static int CompareTo(this XYZ first, XYZ second, Curve curve, bool blnStart)
  1217. {
  1218. if (!blnStart)
  1219. {
  1220. curve = curve.Reverse();
  1221. }
  1222. if (curve.IsArc())
  1223. {
  1224. Arc arc = curve.GetArc();
  1225. double startAngle = arc.GetStartAngle(first);
  1226. double num2 = arc.GetStartAngle(second);
  1227. if (startAngle.IsEqual(num2, 0.0))
  1228. {
  1229. return 0;
  1230. }
  1231. if (startAngle <= num2)
  1232. {
  1233. return -1;
  1234. }
  1235. return 1;
  1236. }
  1237. if (!curve.IsLine())
  1238. {
  1239. return 0;
  1240. }
  1241. Line line = curve.GetLine();
  1242. double num3 = line.StartPoint().FindDistance2(first);
  1243. double num4 = line.StartPoint().FindDistance2(second);
  1244. if (num3.IsEqual(num4, 0.0))
  1245. {
  1246. return 0;
  1247. }
  1248. if (num3 <= num4)
  1249. {
  1250. return -1;
  1251. }
  1252. return 1;
  1253. }
  1254. /// <summary>
  1255. /// 向量过一点的直线
  1256. /// </summary>
  1257. /// <param name="vec"></param>
  1258. /// <param name="pt"></param>
  1259. /// <param name="d"></param>
  1260. /// <param name="blnMid"></param>
  1261. /// <returns></returns>
  1262. public static Line VectorToLine(this XYZ vec, XYZ pt, double d, bool blnMid = false)
  1263. {
  1264. if (blnMid)
  1265. {
  1266. return Line.CreateBound(pt - vec*d, pt + vec*d);
  1267. }
  1268. else
  1269. {
  1270. return Line.CreateBound(pt, pt + vec*d);
  1271. }
  1272. }
  1273. /// <summary>
  1274. /// 向量过一点的射线
  1275. /// </summary>
  1276. /// <param name="vec"></param>
  1277. /// <param name="pt"></param>
  1278. /// <returns></returns>
  1279. public static Line VectorToLine(this XYZ vec, XYZ pt)
  1280. {
  1281. return Line.CreateUnbound(pt, vec);
  1282. }
  1283. /// <summary>
  1284. /// 创建直线
  1285. /// </summary>
  1286. /// <param name="pt"></param>
  1287. /// <param name="vec"></param>
  1288. /// <param name="d"></param>
  1289. /// <param name="blnMid"></param>
  1290. /// <returns></returns>
  1291. public static Line PointToLine(this XYZ pt, XYZ vec, double d, bool blnMid = false)
  1292. {
  1293. return vec.VectorToLine(pt, d, blnMid);
  1294. }
  1295. /// <summary>
  1296. /// 创建射线
  1297. /// </summary>
  1298. /// <param name="pt"></param>
  1299. /// <param name="vec"></param>
  1300. /// <returns></returns>
  1301. public static Line PointToLine(this XYZ pt, XYZ vec)
  1302. {
  1303. return vec.VectorToLine(pt);
  1304. }
  1305. /// <summary>
  1306. /// 参照十字线
  1307. /// </summary>
  1308. /// <param name="pt"></param>
  1309. /// <param name="dLen"></param>
  1310. /// <returns></returns>
  1311. public static List<DetailCurve> GetCorssLine(this XYZ pt, double dLen = 0)
  1312. {
  1313. ExternalDataWrapper wrapper = ExternalDataWrapper.Current;
  1314. XYZ basisX = wrapper.ActiveView.RightDirection;
  1315. XYZ basisY = wrapper.ActiveView.UpDirection;
  1316. XYZ basisZ = wrapper.ActiveView.ViewDirection;
  1317. List<DetailCurve> listCurCross = new List<DetailCurve>();
  1318. if (dLen == 0)
  1319. dLen = 100000d.ToApi();
  1320. Line line = basisX.VectorToLine(pt, dLen, true);
  1321. DetailCurve detailLine = wrapper.Doc.Create.NewDetailCurve(wrapper.ActiveView, line);
  1322. detailLine.SetLineStyle(SysLineStyles.LinesHiddenLines);
  1323. listCurCross.Add(detailLine);
  1324. line = basisY.VectorToLine(pt, dLen, true);
  1325. detailLine = wrapper.Doc.Create.NewDetailCurve(wrapper.ActiveView, line);
  1326. detailLine.SetLineStyle(SysLineStyles.LinesHiddenLines);
  1327. listCurCross.Add(detailLine);
  1328. wrapper.Doc.Regenerate();
  1329. return listCurCross;
  1330. }
  1331. /// <summary>
  1332. /// 项目中用到的迹线
  1333. /// </summary>
  1334. /// <param name="first"></param>
  1335. /// <param name="second"></param>
  1336. /// <returns></returns>
  1337. public static List<DetailCurve> NewTraceLine(this XYZ first, XYZ second)
  1338. {
  1339. List<DetailCurve> listCurCross = new List<DetailCurve>();
  1340. ExternalDataWrapper wrapper = ExternalDataWrapper.Current;
  1341. Line line = Line.CreateBound(first, second);
  1342. DetailCurve detailLine = wrapper.Doc.Create.NewDetailCurve(wrapper.ActiveView, line);
  1343. detailLine.SetLineStyle(SysLineStyles.LinesHiddenLines);
  1344. listCurCross.Add(detailLine);
  1345. wrapper.Doc.Regenerate();
  1346. return listCurCross;
  1347. }
  1348. /// <summary>
  1349. /// 根据两顶点获取矩形 wzc
  1350. /// </summary>
  1351. /// <param name="maxb">最大坐标顶点</param>
  1352. /// <param name="minb">最小坐标顶点</param>
  1353. /// <returns></returns>
  1354. public static List<Line> GetRectange(this XYZ maxb, XYZ minb)
  1355. {
  1356. Line l1 = Line.CreateBound(maxb.NewX(minb.X), maxb); //上
  1357. Line l2 = Line.CreateBound(maxb, maxb.NewY(minb.Y)); //右
  1358. Line l3 = Line.CreateBound(maxb.NewY(minb.Y), minb.NewZ(maxb.Z)); //下
  1359. Line l4 = Line.CreateBound(minb.NewZ(maxb.Z), maxb.NewX(minb.X)); //左
  1360. List<Line> lines = new List<Line>();
  1361. lines.Add(l1);
  1362. lines.Add(l2);
  1363. lines.Add(l3);
  1364. lines.Add(l4);
  1365. return lines;
  1366. }
  1367. /// <summary>
  1368. /// 是否为曲线的端点
  1369. /// </summary>
  1370. /// <param name="pt"></param>
  1371. /// <param name="curve"></param>
  1372. /// <param name="tolerance"></param>
  1373. /// <returns></returns>
  1374. public static bool IsEndPoint(this XYZ pt, Curve curve, double tolerance = 0)
  1375. {
  1376. return pt.IsEqual(curve.StartPoint(), tolerance) || pt.IsEqual(curve.EndPoint(), tolerance);
  1377. }
  1378. /// <summary>
  1379. /// 按逆时针排序点集(未考虑Z方向)
  1380. /// </summary>
  1381. /// <param name="ptList"></param>
  1382. public static void SortAnticlockwise(this List<XYZ> ptList)
  1383. {
  1384. XYZ center = new XYZ();
  1385. double x = 0, y = 0, z = 0;
  1386. for (int i = 0; i < ptList.Count; i++)
  1387. {
  1388. x += ptList[i].X;
  1389. y += ptList[i].Y;
  1390. z += ptList[i].Z;
  1391. }
  1392. double mx = x / ptList.Count;
  1393. double my = y / ptList.Count;
  1394. double mz = z / ptList.Count;
  1395. center = new XYZ(mx, my, mz);
  1396. for (int i = 0; i < ptList.Count - 1; i++)
  1397. {
  1398. for (int j = 0; j < ptList.Count - i - 1; j++)
  1399. {
  1400. if (PointCmp(ptList[j], ptList[j + 1], center))
  1401. {
  1402. XYZ tmp = ptList[j];
  1403. ptList[j] = ptList[j + 1];
  1404. ptList[j + 1] = tmp;
  1405. }
  1406. }
  1407. }
  1408. }
  1409. /// <summary>
  1410. /// 若a大于点b,即点a在b顺时针方向 ,返回true,否则false
  1411. /// 为SortAnticlockwise方法调用
  1412. /// </summary>
  1413. /// <param name="a"></param>
  1414. /// <param name="b"></param>
  1415. /// <param name="center"></param>
  1416. /// <returns></returns>
  1417. private static bool PointCmp(XYZ a, XYZ b, XYZ center)
  1418. {
  1419. if (a.X >= 0 && b.X < 0)
  1420. return true;
  1421. if (a.X == 0 && b.X == 0)
  1422. return a.Y > b.Y;
  1423. //向量OA和OB的叉积
  1424. double det = ((a.X - center.X) * (b.Y - center.Y) - (b.X - center.X) * (a.Y - center.Y));
  1425. if (det < 0)
  1426. return true;
  1427. if (det > 0)
  1428. return false;
  1429. //向量OA和向量OB共线,以距离判断大小
  1430. double d1 = (a.X - center.X) * (a.X - center.X) + (a.Y - center.Y) * (a.Y - center.Y);
  1431. double d2 = (b.X - center.X) * (b.X - center.X) + (b.Y - center.Y) * (b.Y - center.Y);
  1432. return d1 > d2;
  1433. }
  1434. /// <summary>
  1435. /// 矢量的象限角0到2Pi
  1436. /// </summary>
  1437. /// <param name="vec"></param>
  1438. /// <param name="xAxis"></param>
  1439. /// <param name="normal"></param>
  1440. /// <returns></returns>
  1441. public static double QuadrantAngle(this XYZ vec, XYZ xAxis, XYZ normal)
  1442. {
  1443. double num = xAxis.AngleOnPlaneTo(vec, normal);
  1444. if (num.IsEqual(6.2831853071795862, 0.0))
  1445. {
  1446. num = 0.0;
  1447. }
  1448. return num;
  1449. }
  1450. /// <summary>
  1451. /// 点是否在线上
  1452. /// </summary>
  1453. /// <param name="pt"></param>
  1454. /// <param name="line"></param>
  1455. /// <param name="tolerance"></param>
  1456. /// <returns></returns>
  1457. public static bool IsInLine(this XYZ pt, Line line, double tolerance = 0.0)
  1458. {
  1459. return line.IsContain(pt, tolerance);
  1460. }
  1461. /// <summary>
  1462. /// 判断点是否在多边形内
  1463. /// </summary>
  1464. /// <param name="pt"></param>
  1465. /// <param name="listProfile"></param>
  1466. /// <returns></returns>
  1467. public static bool PointInPolygon(this XYZ pt, IList<Curve> listProfile)
  1468. {
  1469. var flag = true;
  1470. //if (!pt.PointInPoly1(listProfile))
  1471. //{
  1472. // flag = pt.PointInPoly2(listProfile.ToList<Curve>());
  1473. //}
  1474. //点在边框上,也要判定为在多边形内部
  1475. flag = pt.PointInPolygonByRay(listProfile.OfType<Line>().ToList())>=0;
  1476. return flag;
  1477. }
  1478. /// <summary>
  1479. /// 判断点是否在多边形中 ljy
  1480. /// 此方法李林提供,好象也不准确与PointInPoly2一起使用
  1481. /// </summary>
  1482. /// <param name="pt"></param>
  1483. /// <param name="listProfile"></param>
  1484. /// <returns></returns>
  1485. private static bool PointInPoly1(this XYZ pt, IList<Curve> listProfile)
  1486. {
  1487. try
  1488. {
  1489. List<XYZ> listPoints = new List<XYZ>();
  1490. foreach (Curve curve in listProfile)
  1491. {
  1492. XYZ xyz = curve.StartPoint();
  1493. XYZ xyz2 = curve.EndPoint();
  1494. if (!xyz.ExsitsInlist(listPoints, 0.0))
  1495. {
  1496. listPoints.Add(xyz);
  1497. }
  1498. if (!xyz2.ExsitsInlist(listPoints, 0.0))
  1499. {
  1500. listPoints.Add(xyz2);
  1501. }
  1502. }
  1503. double num = 0.0;
  1504. int count = listPoints.Count;
  1505. for (int i = 0; i < count; i++)
  1506. {
  1507. XYZ xyz3 = listPoints[i];
  1508. XYZ xyz4 = listPoints[(i + 1) % count];
  1509. XYZ xyz5 = xyz3 - pt;
  1510. XYZ source = xyz4 - pt;
  1511. double length = xyz5.GetLength();
  1512. double num5 = source.GetLength();
  1513. if ((length * num5).IsEqual(0.0, 0.0))
  1514. {
  1515. return true;
  1516. }
  1517. double d = xyz5.DotProduct(source) / (length * num5);
  1518. num += Math.Acos(d);
  1519. }
  1520. return num.IsEqual(6.2831853071795862, 0.0);
  1521. }
  1522. catch
  1523. {
  1524. return false;
  1525. }
  1526. }
  1527. /// <summary>
  1528. /// 判断点是否在多边形内(水平射线法)
  1529. /// 此方法网上提供,好象也不准确与PointInPoly1一起使用
  1530. /// </summary>
  1531. /// <param name="pt"></param>
  1532. /// <param name="curveList"></param>
  1533. /// <returns></returns>
  1534. public static bool PointInPoly2(this XYZ pt, List<Curve> curveList)
  1535. {
  1536. Line line = Line.CreateUnbound(pt, new XYZ(-1.0, 0.0, 0.0));
  1537. int num = 0;
  1538. List<double> dList = new List<double>();
  1539. curveList.ForEach(delegate (Curve p) {
  1540. dList.Add(p.StartPoint().Y);
  1541. dList.Add(p.EndPoint().Y);
  1542. });
  1543. foreach (Curve curve in curveList)
  1544. {
  1545. if (!curve.StartPoint().Y.IsEqual(curve.EndPoint().Y, 0.0) && curve.IsIntersection(line, 0.0))
  1546. {
  1547. XYZ xyz = curve.GetIntersections(line, 0.0)[0];
  1548. if (xyz.X <= pt.X)
  1549. {
  1550. double num2 = Math.Min(curve.StartPoint().Y, curve.EndPoint().Y);
  1551. Math.Max(curve.StartPoint().Y, curve.EndPoint().Y);
  1552. if ((!xyz.Y.IsEqual(((IEnumerable<double>)dList).Max(), 0.0) && !xyz.Y.IsEqual(((IEnumerable<double>)dList).Min(), 0.0)) && (xyz.Y != num2))
  1553. {
  1554. num++;
  1555. }
  1556. }
  1557. }
  1558. }
  1559. if ((num % 2) == 0)
  1560. {
  1561. return false;
  1562. }
  1563. return true;
  1564. }
  1565. /// <summary>
  1566. /// 通过射线法判断点是否在封闭区域内
  1567. /// </summary>
  1568. /// <param name="inputPoint"></param>
  1569. /// <param name="lines"></param>
  1570. /// <returns>
  1571. /// 如果点在多边形内: 返回1
  1572. /// 如果点在多边形边上: 返回0
  1573. /// 如果点在多边形外: 返回-1
  1574. /// </returns>
  1575. public static int PointInPolygonByRay(this XYZ inputPoint, List<Line> lines)
  1576. {
  1577. #region 描述
  1578. /*
  1579. * 判断点是否在直线所围成的区域内;
  1580. * 结果三种,-1,在外侧;0,在边上,1,在多边形内
  1581. * 判断焦点奇偶
  1582. * 如果交点是线段的端点,则记录这个点的信息,同一个交点算一次焦点。
  1583. * 如果线段恰巧和射线共线,如果点在射线中,则记录一个点。否则该记录几个点,就记录几个点;
  1584. * 这个不考虑阿拉伯数字⑧字形这种种,轮廓线有非端点相交情况,这种情况需要记录焦点的具体值
  1585. */
  1586. #endregion
  1587. //以给定点,向x正方向做射线
  1588. //线段端点在射线上的集合
  1589. //List<XYZ> points = new List<XYZ>();
  1590. List<PointGroup> groups = new List<PointGroup>();
  1591. //默认是偶数
  1592. bool isEven = true;
  1593. double baseX = inputPoint.X;
  1594. double baseY = inputPoint.Y;
  1595. foreach (var line in lines)
  1596. {
  1597. var start = line.StartPoint();
  1598. var end = line.EndPoint();
  1599. if (start.IsEqual(inputPoint) || end.IsEqual(inputPoint))
  1600. {
  1601. return 0;
  1602. }
  1603. //判断给定线的端点是否在射线两侧,利用水平线的性质
  1604. if ((start.Y.IsLessEq(baseY) && baseY.IsLessEq(end.Y)) || (end.Y.IsLessEq(baseY) && baseY.IsLessEq(start.Y)))
  1605. {
  1606. double minX = Math.Min(start.X, end.X);
  1607. double maxX = Math.Min(start.X, end.X);
  1608. if (start.Y.IsEqual(end.Y))
  1609. {
  1610. #region 与给定射线共线处理
  1611. //给定线段与射线平行
  1612. if (minX.IsLessEq(baseX) && baseX.IsLessEq(maxX))
  1613. {
  1614. return 0;
  1615. }
  1616. else if (baseX.IsLess(minX))
  1617. {
  1618. PointGroup.AddGroup(groups, new PointGroup() { start, end });
  1619. }
  1620. #endregion
  1621. }
  1622. else if (baseX.IsLessEq(maxX))
  1623. {
  1624. //当给定线,小于等于线最大坐标时,才有可能相交
  1625. /*
  1626. * p1(x1,x2) vecter(xv,yv)
  1627. * x1+xv*c=x
  1628. * y1+yv*c=y
  1629. * 求出c,带入公式1
  1630. */
  1631. double c = (baseY - start.Y) / (end.Y - start.Y);
  1632. double useX = start.X + c * (end.X - start.X);
  1633. if (useX.IsEqual(baseX))
  1634. {
  1635. //给定点,在线上
  1636. return 0;
  1637. }
  1638. if (baseX.IsLessEq(useX))
  1639. {
  1640. //这种情况下肯定相交
  1641. var intersectionPoint = new XYZ(useX, baseY,0);
  1642. if (start.IsEqual(intersectionPoint))
  1643. {
  1644. PointGroup.AddGroup(groups, new PointGroup() { start });
  1645. }
  1646. else if (end.IsEqual(intersectionPoint))
  1647. {
  1648. PointGroup.AddGroup(groups, new PointGroup() { end });
  1649. }
  1650. else
  1651. {
  1652. isEven = !isEven;
  1653. }
  1654. }
  1655. }
  1656. }
  1657. }
  1658. //整理通过的端点
  1659. if (groups.Any())
  1660. {
  1661. foreach (var group in groups)
  1662. {
  1663. var usePoint = group.FirstOrDefault();
  1664. var refLines = lines.Where(l => LineUtil.GetPosition(l, usePoint) != -1).ToList();
  1665. if (refLines.Count < 2)
  1666. {
  1667. continue;
  1668. }
  1669. List<double> results = new List<double>();
  1670. foreach (Line line in refLines)
  1671. {
  1672. List<Line> useLines = new List<Line>(lines);
  1673. useLines.Remove(line);
  1674. var extendPoint = LineUtil.GetFirstPoint(useLines, usePoint, p => !p.Y.IsEqual(usePoint.Y));
  1675. results.Add(extendPoint.Y);
  1676. }
  1677. if (results[0].IsLess(baseY) == results[1].IsThan(baseY))
  1678. {
  1679. if (!results[0].IsEqual(baseY) && !results[1].IsEqual(baseY))
  1680. {
  1681. isEven = !isEven;
  1682. }
  1683. }
  1684. }
  1685. }
  1686. return isEven ? -1 : 1;
  1687. }
  1688. /// <summary>
  1689. /// 通过射线法判断点是否在封闭区域内
  1690. /// </summary>
  1691. /// <param name="inputPoint"></param>
  1692. /// <param name="points"></param>
  1693. /// <returns>
  1694. /// 如果点在多边形内: 返回1
  1695. /// 如果点在多边形边上: 返回0
  1696. /// 如果点在多边形外: 返回-1
  1697. /// </returns>
  1698. public static int PointInPolygonByRay(this XYZ inputPoint, List<XYZ> points)
  1699. {
  1700. #region 老方法
  1701. //List<Line> lines = new List<Line>();
  1702. //for (int i = 0; i < points.Count; i++)
  1703. //{
  1704. // var start = points[i];
  1705. // var end = points[(i + 1) % points.Count];
  1706. // lines.Add(Line.CreateBound(start, end));
  1707. //}
  1708. //return PointInPolygonByRay(inputPoint, lines);
  1709. #endregion
  1710. bool isEven = true;
  1711. double baseX = inputPoint.X;
  1712. double baseY = inputPoint.Y;
  1713. var total = points.Count;
  1714. for (int i = 0; i < total; i++)
  1715. {
  1716. var endIndex = (i + 1) % total;
  1717. var start = points[i];
  1718. var end = points[endIndex];
  1719. if (start.IsEqual2(inputPoint) || end.IsEqual2(inputPoint))
  1720. {
  1721. return 0;
  1722. }
  1723. bool isEndIntersection = false;//判断交点是否是end
  1724. //判断给定线的端点是否在射线两侧,利用水平线的性质
  1725. if ((start.Y.IsLessEq(baseY) && baseY.IsLessEq(end.Y)) || (end.Y.IsLessEq(baseY) && baseY.IsLessEq(start.Y)))
  1726. {
  1727. double minX = Math.Min(start.X, end.X);
  1728. double maxX = Math.Min(start.X, end.X);
  1729. if (start.Y.IsEqual(end.Y))
  1730. {
  1731. #region 与给定射线共线处理
  1732. //给定线段与射线平行
  1733. if (minX.IsLessEq(baseX) && baseX.IsLessEq(maxX))
  1734. {
  1735. return 0;
  1736. }
  1737. else if (baseX.IsLess(minX))
  1738. {
  1739. isEndIntersection = true;
  1740. }
  1741. #endregion
  1742. }
  1743. else if (baseX.IsLessEq(maxX))
  1744. {
  1745. double c = (baseY - start.Y) / (end.Y - start.Y);
  1746. double useX = start.X + c * (end.X - start.X);
  1747. if (useX.IsEqual(baseX))
  1748. {
  1749. //给定点,在线上
  1750. return 0;
  1751. }
  1752. if (baseX.IsLessEq(useX))
  1753. {
  1754. //这种情况下肯定相交
  1755. var intersectionPoint = new XYZ(useX, baseY,0);
  1756. if (end.IsEqual2(intersectionPoint))
  1757. {
  1758. //开始点不处理
  1759. isEndIntersection = true;
  1760. }
  1761. else if (start.IsEqual2(intersectionPoint))
  1762. {
  1763. isEndIntersection = false;
  1764. }
  1765. else
  1766. {
  1767. isEven = !isEven;
  1768. }
  1769. }
  1770. }
  1771. if (isEndIntersection)
  1772. {
  1773. var end2 = points[(endIndex + 1) % total];
  1774. if (!baseY.IsEqual(end2.Y))
  1775. {
  1776. var point = points.GetFirstElement(endIndex, p => !p.Y.IsEqual(baseY), false);
  1777. if (end2.Y.IsLess(baseY) == point.Y.IsThan(baseY))
  1778. {
  1779. //排除同时相等的情况
  1780. if (!end2.Y.IsEqual(baseY) && !point.Y.IsEqual(baseY))
  1781. {
  1782. isEven = !isEven;
  1783. }
  1784. }
  1785. }
  1786. }
  1787. }
  1788. }
  1789. return isEven ? -1 : 1;
  1790. }
  1791. }
  1792. }