GetAllModule.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. # -*- coding: UTF-8 -*-
  2. from pyautocad import Autocad, APoint
  3. from utils import conf,MovePath
  4. import math
  5. #这个true表示没有文件则打开一个,CAD有弹窗时会打开或者创建失败
  6. acad = Autocad(create_if_not_exists = False)
  7. # 组件实体点位置
  8. positoins={}
  9. currentPosition = {}
  10. # 组件使用点位置
  11. useAreaPositions={}
  12. # 组件与门距离
  13. distinceWithDoor = {}
  14. # 组件间距离
  15. distinceWithOthers={}
  16. # 旋转后需删除的组件
  17. needDeleteBlockReferences=[]
  18. scores=[]
  19. currentScore=[]
  20. # acad.prompt("Hello, Autocad from Python\n")
  21. # conf.myprint(acad.doc.Name)
  22. def printObjects():
  23. #遍历cad图形对象
  24. for obj in acad.iter_objects():
  25. conf.myprint(obj.ObjectName)
  26. def printBlockObjects():
  27. #遍历cad图形对象
  28. for obj in acad.iter_objects("BlockReference"):
  29. conf.myprint(obj.name,obj.HasAttributes)
  30. if obj.HasAttributes:
  31. attrs = obj.GetAttributes()
  32. for attrib in attrs:
  33. conf.myprint(" {}: {}".format(attrib.TagString, attrib.TextString))
  34. def printTheTypeObject(type):
  35. #按类型查找出所有某种图元
  36. for text in acad.iter_objects(type):
  37. conf.myprint(text.name)
  38. def addCurrentScoreToScores():
  39. temp = []
  40. for item in currentScore:
  41. temp.append(item)
  42. scores.append(temp)
  43. def moveBlockReference(position):
  44. positoins.clear()
  45. useAreaPositions.clear()
  46. conf.myprint(conf.moduleLengths)
  47. currentScore.clear()
  48. currentScore.append(position)
  49. moveResult = 1
  50. for blockReference in acad.iter_objects("BlockReference"):
  51. conf.myprint(blockReference.name)
  52. for item in conf.names:
  53. if(blockReference.name == item):
  54. currentPosition[0] = position
  55. currentPosition[1] = conf.names.index(item)
  56. result = moveBlockReferenceToWall(blockReference,position[conf.names.index(item)])
  57. if(result == 0):
  58. moveResult = result
  59. conf.myprint("currentScore size =",len(currentScore),",scores size=",len(scores))
  60. addCurrentScoreToScores()
  61. conf.myprint("scores=",scores)
  62. for blockReference in needDeleteBlockReferences:
  63. blockReference.delete()
  64. needDeleteBlockReferences.clear()
  65. return moveResult
  66. def moveBlockReferenceToWall(blockReference,wallIndex):
  67. wallIndex = int(wallIndex)
  68. blockReference.rotate(APoint(blockReference.InsertionPoint),0.5*(wallIndex+1)*math.pi)
  69. blockIndex = conf.names.index(blockReference.name)
  70. conf.myprint("begin to put ",blockReference.name)
  71. if(wallIndex == 0):
  72. blockReference.move(APoint(blockReference.InsertionPoint),APoint(conf.roomLengths[0]-conf.roomUsedLengths[0],0))
  73. conf.roomUsedLengths[0] = conf.roomUsedLengths[0]+conf.moduleLengths[blockIndex];
  74. elif (wallIndex == 1):
  75. blockReference.move(APoint(blockReference.InsertionPoint),APoint(conf.roomLengths[0],conf.roomLengths[1]-conf.roomUsedLengths[1]))
  76. conf.roomUsedLengths[1] = conf.roomUsedLengths[1]+conf.moduleLengths[blockIndex];
  77. elif (wallIndex == 2):
  78. blockReference.move(APoint(blockReference.InsertionPoint),APoint(conf.roomUsedLengths[2],conf.roomLengths[1]))
  79. conf.roomUsedLengths[2] = conf.roomUsedLengths[2]+conf.moduleLengths[blockIndex];
  80. else:
  81. blockReference.move(APoint(blockReference.InsertionPoint),APoint(0,conf.roomLengths[1]-conf.roomUsedLengths[3]-conf.moduleLengths[blockIndex]))
  82. conf.roomUsedLengths[3] = conf.roomUsedLengths[3]+conf.moduleLengths[blockIndex];
  83. checkPositionIntersect = tempStoragePosition(blockReference.InsertionPoint[0],blockReference.InsertionPoint[1],wallIndex,blockIndex,blockReference)
  84. if(checkPositionIntersect == 0):
  85. conf.myprint("wallIndex=",wallIndex,blockReference.name," not walkable......")
  86. return 0
  87. else:
  88. return 1
  89. # 根据新顺序移动模块
  90. def moveBlockReferenceWithNewIndex(position,moduleIndex):
  91. moveResult = 1
  92. positoins.clear()
  93. useAreaPositions.clear()
  94. currentScore.clear()
  95. currentScore.append(position)
  96. currentScore.append(moduleIndex)
  97. conf.myprint(conf.moduleLengths)
  98. blockReferences = []
  99. for blockReference in acad.iter_objects("BlockReference"):
  100. conf.myprint(blockReference.name)
  101. for item in conf.names:
  102. if(blockReference.name == item):
  103. blockReferences.append(blockReference)
  104. for index in moduleIndex:
  105. currentPosition[0] = position
  106. currentPosition[1] = moduleIndex
  107. currentPosition[2] = moduleIndex.index(index)
  108. result = moveBlockReferenceToWall(blockReferences[index],position[moduleIndex.index(index)])
  109. if(result == 0):
  110. moveResult = result
  111. conf.myprint("currentScore size =",len(currentScore),",scores size=",len(scores))
  112. addCurrentScoreToScores()
  113. conf.myprint("scores=",scores)
  114. return moveResult
  115. # 重置已使用墙的长度
  116. def resetConfUsed():
  117. conf.roomUsedLengths=[0,0,0,0]
  118. # 对模块重新排序
  119. def changeConfModuleIndex(moduleIndex):
  120. names=[]
  121. moduleLengths=[]
  122. for index in range(len(conf.moduleLengths)):
  123. names.append(conf.names[moduleIndex[index]])
  124. moduleLengths.append(conf.moduleLengths[moduleIndex[index]])
  125. conf.names = names
  126. conf.moduleLengths = tuple(moduleLengths)
  127. # 重置模块顺序
  128. def resetModuleIndex():
  129. conf.names=['md_py','md_xs','md_rc']
  130. conf.moduleLengths = (800,1000,800)
  131. # 判断位置是否可行
  132. def isWorkable(position):
  133. resetConfUsed()
  134. conf.myprint("position=",position)
  135. needLength=[0]*4
  136. for positionIndex in range(len(position)):
  137. wallIndex = int(position[positionIndex])
  138. needLength[wallIndex]+=conf.moduleLengths[positionIndex]
  139. if(needLength[wallIndex]+conf.roomUsedLengths[wallIndex] > conf.roomWallLengths[wallIndex]):
  140. conf.myprint(needLength[wallIndex]+conf.roomUsedLengths[wallIndex],conf.roomLengths[math.floor(wallIndex/2)],wallIndex)
  141. return 0
  142. return 1
  143. # 获取组件的四点坐标
  144. def getCenterPoint(wallIndex, x, y, blockIndex):
  145. height = conf.moduleLengths[blockIndex];
  146. width = conf.moduleHeights[blockIndex];
  147. useHeight = conf.moduleUseLength[blockIndex]
  148. useWidth = conf.humenLenght
  149. usePointPosition = conf.moduleUsePoint[blockIndex]
  150. useCenterPointPosition = conf.moduleUseCenterPoint[blockIndex]
  151. pointA = (x,y)
  152. conf.myprint("insertPoint=",x,y)
  153. if(wallIndex == 0):
  154. pointB = (x,y+width)
  155. pointC = (x-height,y+width)
  156. pointD = (x-height,y)
  157. centerPoint = (x-height/2,y+width/2)
  158. usePoint= (x-usePointPosition[1],y+usePointPosition[0])
  159. useCenterPoint= (x-useCenterPointPosition[1],y+useCenterPointPosition[0])
  160. centerPointOffset = (height,width)
  161. useCenterPointOffset = (useHeight,useWidth)
  162. elif(wallIndex == 1):
  163. pointB = (x-width,y)
  164. pointC = (x-width,y-height)
  165. pointD = (x,y-height)
  166. centerPoint = (x-width/2,y-height/2)
  167. usePoint= (x-usePointPosition[0],y-usePointPosition[1])
  168. useCenterPoint= (x-useCenterPointPosition[0],y-useCenterPointPosition[1])
  169. centerPointOffset = (width,height)
  170. useCenterPointOffset = (useWidth,useHeight)
  171. elif(wallIndex == 2):
  172. pointB = (x,y-width)
  173. pointC = (x+height,y-width)
  174. pointD = (x+height,y)
  175. centerPoint = (x+height/2,y-width/2)
  176. usePoint= (x+usePointPosition[1],y-usePointPosition[0])
  177. useCenterPoint= (x+useCenterPointPosition[1],y-useCenterPointPosition[0])
  178. centerPointOffset = (height,width)
  179. useCenterPointOffset = (useHeight,useWidth)
  180. else:
  181. pointB = (x+width,y)
  182. pointC = (x+width,y+height)
  183. pointD = (x,y+height)
  184. centerPoint = (x+width/2,y+height/2)
  185. usePoint= (x+usePointPosition[0],y+usePointPosition[1])
  186. useCenterPoint= (x+useCenterPointPosition[0],y+useCenterPointPosition[1])
  187. centerPointOffset = (width,height)
  188. useCenterPointOffset = (useWidth,useHeight)
  189. positoin = [centerPoint,centerPointOffset,usePoint,useCenterPoint,useCenterPointOffset];
  190. conf.myprint(wallIndex,width,height)
  191. # drawPointCircle((pointA, pointB, pointC, pointD, centerPoint, usePoint, useCenterPoint))
  192. conf.myprint(positoin)
  193. return positoin
  194. # 打印辅助点
  195. def drawPointCircle(points):
  196. for point in points:
  197. acad.model.AddCircle(APoint(point[0],point[1]), 50)
  198. # 判断可用区域中点是否超出房间
  199. def isUseCenterOverRoom(useCenterPosition):
  200. if useCenterPosition[0] < 0 or useCenterPosition[0]>conf.roomLengths[0] or useCenterPosition[1] < 0 or useCenterPosition[1]>conf.roomLengths[1]:
  201. return True
  202. return False
  203. # 从插入点开始顺时针记录已摆放的组件
  204. def tempStoragePosition(x,y,wallIndex,blockIndex,blockReference):
  205. position = getCenterPoint(wallIndex, x, y, blockIndex);
  206. # 判断可用区域中点是否超出房间
  207. if(isUseCenterOverRoom(position[3])):
  208. mirrorPy(blockReference,position)
  209. useCenterPosition = (position[3],position[4])
  210. doorFeasible = isPositionFeasibleWithDoor(position)
  211. conf.myprint("doorFeasible=",doorFeasible)
  212. if(doorFeasible == 0):
  213. return doorFeasible
  214. getDoorBeginAndModuleUsePointDistince(position,wallIndex)
  215. result = 1
  216. for item in positoins:
  217. # 判断实体是否相交
  218. result = isPositionFeasible(position,positoins[item],1)
  219. if(result == 1):
  220. # 判断当前实体是否与已摆放的可用区域相交
  221. result = isPositionFeasible(position,useAreaPositions[item],1)
  222. if(result == 1):
  223. # 判断可用区域是否与实体相交
  224. result = isPositionFeasible(useCenterPosition,positoins[item],1)
  225. if(result == 0):
  226. return result
  227. if(result == 1):
  228. positoins[len(positoins)] = position
  229. useAreaPositions[len(useAreaPositions)] = useCenterPosition
  230. return result
  231. # 计算位置是否可行type:0 实体 1可用区域
  232. def isPositionFeasible(position1,position2,type):
  233. areaName = "可用区域"
  234. if type==0:
  235. acad.model.AddLine(APoint(position1[0][0],position1[0][1]),APoint(position2[0][0],position2[0][1]))
  236. acad.model.AddCircle(APoint(position1[0][0],position1[0][1]), 50)
  237. areaName = "实体区域"
  238. if(abs(position1[0][0]-position2[0][0]) < (position1[1][0]+position2[1][0])/2 and abs(position1[0][1]-position2[0][1]) < (position1[1][1]+position2[1][1])/2):
  239. conf.myprint(areaName,"sorry! can not put here",currentPosition,"position1=",position1,"position2=",position2)
  240. return 0
  241. else:
  242. conf.myprint(areaName,"can put here")
  243. return 1
  244. # 计算实体位置是否与门使用区域相交
  245. def isPositionFeasibleWithDoor(position):
  246. length = conf.doorAlign[0]+conf.doorAlign[1]+conf.doorLength;
  247. width = conf.doorLength+conf.doorAlign[2];
  248. doorPosition = [(length/2,width/2),(length,width)]
  249. return isPositionFeasible(position,doorPosition,1)
  250. # 计算两点间距离
  251. def getDistince(point1,point2):
  252. detarX = point1[0]-point2[0]
  253. detarY = point1[1]-point2[1]
  254. return math.sqrt(detarX**2+detarY**2)
  255. def getDoorBeginPoint():
  256. return ((conf.doorAlign[0]+conf.doorLength+conf.doorAlign[1])/2,0)
  257. def drawLines(includePoints):
  258. lineLength = 0;
  259. if(len(includePoints)>1):
  260. for item in range(len(includePoints)):
  261. if(item>0):
  262. acad.model.AddLine(APoint(includePoints[item-1][0],includePoints[item-1][1]),APoint(includePoints[item][0],includePoints[item][1]))
  263. lineLength += getDistince((includePoints[item-1][0],includePoints[item-1][1]),(includePoints[item][0],includePoints[item][1]))
  264. return lineLength
  265. def getDoorBeginAndModuleUsePointDistince(position,wallIndex):
  266. doorBeginPoint = getDoorBeginPoint()
  267. includePoints = MovePath.getDoorBeginAndModuleUsePointDistince(position[0][0],position[0][1],position[1][0],position[1][1],doorBeginPoint[0],doorBeginPoint[1])
  268. currentScore.append(drawLines(includePoints))
  269. includePoints = MovePath.getMovePath(position[0][0],position[0][1],position[1][0],position[1][1],doorBeginPoint[0],doorBeginPoint[1],position[2][0],position[2][1])
  270. currentScore.append(drawLines(includePoints))
  271. # distinceWithDoor[len(distinceWithDoor)] = getDistince(doorBeginPoint,usePoint)
  272. # 以mirrorPoint和可用区域重点为对称抽翻转py
  273. def mirrorPy(py,position):
  274. insertPoint = py.InsertionPoint
  275. centerPoint = position[0]
  276. newCenterPoint = [position[3][0],position[3][1]]
  277. newUsePoint = [position[2][0],position[2][1]]
  278. conf.myprint("newUsePoint after mirror =",newUsePoint)
  279. if((insertPoint[0]-centerPoint[0])*(insertPoint[1]-centerPoint[1])>0):
  280. mirrorPoint = (insertPoint[0],centerPoint[1])
  281. direction = 1 if (insertPoint[1]-centerPoint[1])>0 else -1;
  282. newCenterPoint[1]=position[3][1]+position[4][1]*direction
  283. newUsePoint[1]=position[2][1]+position[1][1]*direction
  284. else:
  285. mirrorPoint = (centerPoint[0],insertPoint[1])
  286. direction = 1 if (insertPoint[1]-centerPoint[1])>0 else -1;
  287. newCenterPoint[0]=position[3][0]+position[4][0]*direction
  288. newUsePoint[0]=position[2][0]+position[1][0]*direction
  289. position[3] = newCenterPoint
  290. position[2] = newUsePoint
  291. conf.myprint("newUsePoint after mirror =",newUsePoint)
  292. py.mirror(APoint(mirrorPoint[0],mirrorPoint[1]),APoint(centerPoint[0],centerPoint[1]))
  293. conf.myprint("delete blockReference name",py.name)
  294. needDeleteBlockReferences.append(py)
  295. # printObjects()
  296. # printBlockObjects()
  297. # printTheTypeObject("BlockReference")
  298. # moveBlockReference('222')
  299. # a=[0,1,2]
  300. # moveBlockReferenceWithNewIndex('001',a)
  301. # conf.myprint(isWorkable('222'))
  302. # getCenterPoint(3,4162.85,0,2)
  303. # print(getDistince((0,3),(4,0)))
  304. # print(getDoorBeginPoint())