GetAllModule.py 15 KB

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