test.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # -*- coding: UTF-8 -*-
  2. from pyautocad import Autocad, APoint
  3. #这个true表示没有文件则打开一个,CAD有弹窗时会打开或者创建失败
  4. acad = Autocad(create_if_not_exists = True)
  5. acad.prompt("Hello, Autocad from Python\n")
  6. print(acad.doc.Name)
  7. def drawPoints(points):
  8. for point in points:
  9. acad.model.AddCircle(APoint(point[0],point[1]), 50)
  10. def drawLines(includePoints):
  11. if(len(includePoints)>1):
  12. for item in range(len(includePoints)):
  13. if(item>0):
  14. acad.model.AddLine(APoint(includePoints[item-1][0],includePoints[item-1][1]),APoint(includePoints[item][0],includePoints[item][1]))
  15. # 获取点到长方形的最短距离
  16. def getDoorBeginAndModuleUsePointDistince(x0,y0,width,height,x,y):
  17. points = [(x,y),(x0-width/2,y0-height/2),(x0-width/2,y0+height/2),(x0+width/2,y0-height/2),(x0+width/2,y0+height/2),(x0,y0)]
  18. drawPoints(points)
  19. includePoints = [(x,y)]
  20. if x<x0-width/2:
  21. if y>y0+height/2:
  22. includePoints.append((x0-width/2,y0+height/2))
  23. elif y>y0-height/2:
  24. includePoints.append((x0-width/2,y))
  25. else:
  26. includePoints.append((x0-width/2,y0-height/2))
  27. elif x<x0+width/2:
  28. if y>y0+height/2:
  29. includePoints.append((x,y0+height/2))
  30. elif y>y0-height/2:
  31. pass
  32. else:
  33. includePoints.append((x,y0-height/2))
  34. else:
  35. if y>y0+height/2:
  36. includePoints.append((x0+width/2,y0+height/2))
  37. elif y>y0-height/2:
  38. includePoints.append((x0+width/2,y))
  39. else:
  40. includePoints.append((x0+width/2,y0-height/2))
  41. drawLines(includePoints)
  42. # 获取点到长方形指定点的最短距离,默认点在长方形下方
  43. # x0,y0为矩形中点,xy为起始点,x1,y1为终点
  44. def getMovePath(x0,y0,width,height,x,y,x1,y1):
  45. points = [(x,y),(x0-width/2,y0-height/2),(x0-width/2,y0+height/2),(x0+width/2,y0-height/2),(x0+width/2,y0+height/2),(x0,y0),(x1,y1)]
  46. drawPoints(points)
  47. includePoints = [(x,y)]
  48. # 指定点在第0边;按下右上左方向
  49. if y1 == y0-height/2:
  50. includePoints.append((x1,y1))
  51. # 指定点在第1边;按下右上左方向
  52. elif x1 == x0+width/2:
  53. if x>x0+width/2:
  54. includePoints.append((x1,y1))
  55. else:
  56. includePoints.append((x0+width/2,y0-height/2))
  57. includePoints.append((x1,y1))
  58. # 指定点在第2边,暂只考虑从左边开始接近
  59. elif y1 == y0+height/2:
  60. if x<x0-width/2:
  61. includePoints.append((x0-width/2,y0+height/2))
  62. includePoints.append((x1,y1))
  63. else:
  64. includePoints.append((x0-width/2,y0-height/2))
  65. includePoints.append((x0-width/2,y0+height/2))
  66. includePoints.append((x1,y1))
  67. # 指定点在第3边
  68. else:
  69. if x<x0-width/2:
  70. includePoints.append((x1,y1))
  71. else:
  72. includePoints.append((x0-width/2,y0-height/2))
  73. includePoints.append((x1,y1))
  74. # drawLines(includePoints)
  75. return includePoints
  76. # 获取点到长方形指定点的最短距离
  77. # x0,y0为矩形中点,xy为起始点,x1,y1为终点
  78. def getMovePath2(x0,y0,width,height,x,y,x1,y1):
  79. includePoints = [(x,y)]
  80. bottom = y0-height/2
  81. right = x0+width/2
  82. top = y0+height/2
  83. left = x0-width/2
  84. points = [(x,y),(left,bottom),(left,top),(right,bottom),(right,top),(x0,y0),(x1,y1)]
  85. drawPoints(points)
  86. # 终点在模块最上边
  87. if y1 == top:
  88. if y>=top:
  89. includePoints.append((x1,y1))
  90. elif x<=left:
  91. includePoints.append((left,top))
  92. includePoints.append((x1,y1))
  93. elif x>=right:
  94. includePoints.append((right,top))
  95. includePoints.append((x1,y1))
  96. elif x+x1<=2*x0:
  97. includePoints.append((left,bottom))
  98. includePoints.append((left,top))
  99. includePoints.append((x1,y1))
  100. else:
  101. includePoints.append((right,bottom))
  102. includePoints.append((right,top))
  103. includePoints.append((x1,y1))
  104. elif y1 == bottom:
  105. if y<=bottom:
  106. includePoints.append((x1,y1))
  107. elif x<=left:
  108. includePoints.append((left,bottom))
  109. includePoints.append((x1,y1))
  110. elif x>=right:
  111. includePoints.append((right,bottom))
  112. includePoints.append((x1,y1))
  113. elif x+x1<=2*x0:
  114. includePoints.append((left,top))
  115. includePoints.append((left,bottom))
  116. includePoints.append((x1,y1))
  117. else:
  118. includePoints.append((right,top))
  119. includePoints.append((right,bottom))
  120. includePoints.append((x1,y1))
  121. elif x1 == left:
  122. if x<=left:
  123. includePoints.append((x1,y1))
  124. elif y>=top:
  125. includePoints.append((left,top))
  126. includePoints.append((x1,y1))
  127. elif y<=bottom:
  128. includePoints.append((left,bottom))
  129. includePoints.append((x1,y1))
  130. elif y+y1<=2*y0:
  131. includePoints.append((right,bottom))
  132. includePoints.append((left,bottom))
  133. includePoints.append((x1,y1))
  134. else:
  135. includePoints.append((right,top))
  136. includePoints.append((left,top))
  137. includePoints.append((x1,y1))
  138. else:
  139. if x>=right:
  140. includePoints.append((x1,y1))
  141. elif y>=top:
  142. includePoints.append((right,top))
  143. includePoints.append((x1,y1))
  144. elif y<=bottom:
  145. includePoints.append((right,bottom))
  146. includePoints.append((x1,y1))
  147. elif y+y1<=2*y0:
  148. includePoints.append((left,bottom))
  149. includePoints.append((right,bottom))
  150. includePoints.append((x1,y1))
  151. else:
  152. includePoints.append((left,top))
  153. includePoints.append((right,top))
  154. includePoints.append((x1,y1))
  155. drawLines(includePoints)
  156. return includePoints
  157. # 获取模块可用点到另一个模块使用点间的距离
  158. def getModuleMovePath(x10,y10,width1,height1,x1,y1,x20,y20,width2,height2,x2,y2):
  159. includePoints1 = getMovePath(x10,y10,width1,height1,x2,y2,x1,y1)
  160. includePoints2 = getMovePath(x20,y20,width2,height2,x1,y1,x2,y2)
  161. includePoints = []
  162. for item in range(len(includePoints1)):
  163. if(item>0):
  164. includePoints.append(includePoints1[item])
  165. for item in range(len(includePoints2)):
  166. if(item>0):
  167. includePoints.insert(0,includePoints2[item])
  168. print(includePoints)
  169. drawLines(includePoints)
  170. return includePoints
  171. # getDoorBeginAndModuleUsePointDistince(3300,1500,900,600,10,3000)
  172. # getDoorBeginAndModuleUsePointDistince(3300,2800,900,600,1000,3000)
  173. # getDoorBeginAndModuleUsePointDistince(3300,2800,900,600,3100,3800)
  174. # getDoorBeginAndModuleUsePointDistince(3300,2800,900,600,4000,900)
  175. # getMovePath(3300,2800,900,600,0,0,3500,2500)
  176. # getMovePath(3300,2800,900,600,0,0,2850,2700)
  177. # getMovePath(3300,2800,900,600,3000,0,2850,2700)
  178. # getMovePath(3300,2800,900,600,3300,0,3500,3100)
  179. # getMovePath2(800,700,600,400,2000,2000,800,900)
  180. # getMovePath2(800,700,600,400,300,800,800,900)
  181. # getMovePath2(800,700,600,400,2000,0,800,900)
  182. # getMovePath2(800,700,600,400,1000,100,800,900)
  183. # getMovePath2(800,700,600,400,600,100,800,900)
  184. # getModuleMovePath(800,700,600,400,900,500,1500,1100,400,800,1700,800)
  185. def testAppend():
  186. score = []
  187. for i in range(100):
  188. s = []
  189. s.append(i)
  190. score.append(s)
  191. print(score)
  192. def testRange():
  193. for i in range(3,10,2):
  194. print(i)
  195. testRange()
  196. # testAppend()