GetAllModule.py 16 KB

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