GetAllModule.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # -*- coding: UTF-8 -*-
  2. from pyautocad import Autocad, APoint
  3. from utils import conf
  4. import math
  5. #这个true表示没有文件则打开一个,CAD有弹窗时会打开或者创建失败
  6. acad = Autocad(create_if_not_exists = False)
  7. # 组件实体点位置
  8. positoins={}
  9. # 组件使用点位置
  10. useAreaPositions={}
  11. # acad.prompt("Hello, Autocad from Python\n")
  12. # print(acad.doc.Name)
  13. def printObjects():
  14. #遍历cad图形对象
  15. for obj in acad.iter_objects():
  16. print(obj.ObjectName)
  17. def printBlockObjects():
  18. #遍历cad图形对象
  19. for obj in acad.iter_objects("BlockReference"):
  20. print(obj.name,obj.HasAttributes)
  21. if obj.HasAttributes:
  22. attrs = obj.GetAttributes()
  23. for attrib in attrs:
  24. print(" {}: {}".format(attrib.TagString, attrib.TextString))
  25. def printTheTypeObject(type):
  26. #按类型查找出所有某种图元
  27. for text in acad.iter_objects(type):
  28. print(text.name)
  29. def moveBlockReference(position):
  30. print(conf.moduleLengths)
  31. for blockReference in acad.iter_objects("BlockReference"):
  32. print(blockReference.name)
  33. for item in conf.names:
  34. if(blockReference.name == item):
  35. moveBlockReferenceToWall(blockReference,position[conf.names.index(item)])
  36. def moveBlockReferenceToWall(blockReference,wallIndex):
  37. wallIndex = int(wallIndex)
  38. blockReference.rotate(APoint(blockReference.InsertionPoint),0.5*(wallIndex+1)*math.pi)
  39. blockIndex = conf.names.index(blockReference.name)
  40. print("begin to put ",blockReference.name)
  41. if(wallIndex == 0):
  42. blockReference.move(APoint(blockReference.InsertionPoint),APoint(0+conf.moduleLengths[blockIndex]+conf.roomUsedLengths[0],0))
  43. conf.roomUsedLengths[0] = conf.roomUsedLengths[0]+conf.moduleLengths[blockIndex];
  44. elif (wallIndex == 1):
  45. blockReference.move(APoint(blockReference.InsertionPoint),APoint(conf.roomLengths[0],conf.roomLengths[1]-conf.roomUsedLengths[1]))
  46. conf.roomUsedLengths[1] = conf.roomUsedLengths[1]+conf.moduleLengths[blockIndex];
  47. elif (wallIndex == 2):
  48. blockReference.move(APoint(blockReference.InsertionPoint),APoint(conf.roomUsedLengths[2],conf.roomLengths[1]))
  49. conf.roomUsedLengths[2] = conf.roomUsedLengths[2]+conf.moduleLengths[blockIndex];
  50. else:
  51. blockReference.move(APoint(blockReference.InsertionPoint),APoint(0,conf.roomUsedLengths[3]))
  52. conf.roomUsedLengths[3] = conf.roomUsedLengths[3]+conf.moduleLengths[blockIndex];
  53. checkPositionIntersect = tempStoragePosition(blockReference.InsertionPoint[0],blockReference.InsertionPoint[1],wallIndex,blockIndex)
  54. if(checkPositionIntersect == 0):
  55. print("wallIndex=",wallIndex,blockReference.name," not walkable......")
  56. # 根据新顺序移动模块
  57. def moveBlockReferenceWithNewIndex(position,moduleIndex):
  58. print(conf.moduleLengths)
  59. blockReferences = []
  60. for blockReference in acad.iter_objects("BlockReference"):
  61. print(blockReference.name)
  62. for item in conf.names:
  63. if(blockReference.name == item):
  64. blockReferences.append(blockReference)
  65. for index in moduleIndex:
  66. moveBlockReferenceToWall(blockReferences[index],position[moduleIndex.index(index)])
  67. # 重置已使用墙的长度
  68. def resetConfUsed():
  69. conf.roomUsedLengths=[1200,0,0,1000]
  70. # 对模块重新排序
  71. def changeConfModuleIndex(moduleIndex):
  72. names=[]
  73. moduleLengths=[]
  74. for index in range(len(conf.moduleLengths)):
  75. names.append(conf.names[moduleIndex[index]])
  76. moduleLengths.append(conf.moduleLengths[moduleIndex[index]])
  77. conf.names = names
  78. conf.moduleLengths = tuple(moduleLengths)
  79. # 重置模块顺序
  80. def resetModuleIndex():
  81. conf.names=['md_py','md_xs','md_rc']
  82. conf.moduleLengths = (800,1000,800)
  83. # 判断位置是否可行
  84. def isWorkable(position):
  85. resetConfUsed()
  86. positoins.clear()
  87. print("position=",position)
  88. needLength=[0]*4
  89. for positionIndex in range(len(position)):
  90. wallIndex = int(position[positionIndex])
  91. needLength[wallIndex]+=conf.moduleLengths[positionIndex]
  92. if(needLength[wallIndex]+conf.roomUsedLengths[wallIndex] > conf.roomLengths[wallIndex%2]):
  93. print(needLength[wallIndex]+conf.roomUsedLengths[wallIndex],conf.roomLengths[math.floor(wallIndex/2)],wallIndex)
  94. return 0
  95. return 1
  96. # 获取组件的四点坐标
  97. def getCenterPoint(wallIndex, x, y, blockIndex):
  98. height = conf.moduleLengths[blockIndex];
  99. width = conf.moduleHeights[blockIndex];
  100. useHeight = conf.moduleUseLength[blockIndex]
  101. useWidth = conf.humenLenght
  102. usePointPosition = conf.moduleUsePoint[blockIndex]
  103. useCenterPointPosition = conf.moduleUseCenterPoint[blockIndex]
  104. pointA = (x,y)
  105. print("insertPoint=",x,y)
  106. if(wallIndex == 0):
  107. pointB = (x,y+width)
  108. pointC = (x-height,y+width)
  109. pointD = (x-height,y)
  110. centerPoint = (x-height/2,y+width/2)
  111. usePoint= (x-usePointPosition[1],y+usePointPosition[0])
  112. useCenterPoint= (x-useCenterPointPosition[1],y+useCenterPointPosition[0])
  113. centerPointOffset = (height,width)
  114. useCenterPointOffset = (useHeight,useWidth)
  115. elif(wallIndex == 1):
  116. pointB = (x-width,y)
  117. pointC = (x-width,y-height)
  118. pointD = (x,y-height)
  119. centerPoint = (x-width/2,y-height/2)
  120. usePoint= (x-usePointPosition[0],y-usePointPosition[1])
  121. useCenterPoint= (x-useCenterPointPosition[0],y-useCenterPointPosition[1])
  122. centerPointOffset = (width,height)
  123. useCenterPointOffset = (useWidth,useHeight)
  124. elif(wallIndex == 2):
  125. pointB = (x,y-width)
  126. pointC = (x+height,y-width)
  127. pointD = (x+height,y)
  128. centerPoint = (x+height/2,y-width/2)
  129. usePoint= (x+usePointPosition[1],y-usePointPosition[0])
  130. useCenterPoint= (x+useCenterPointPosition[1],y-useCenterPointPosition[0])
  131. centerPointOffset = (height,width)
  132. useCenterPointOffset = (useHeight,useWidth)
  133. else:
  134. pointB = (x+width,y)
  135. pointC = (x+width,y+height)
  136. pointD = (x,y+height)
  137. centerPoint = (x+width/2,y+height/2)
  138. usePoint= (x+usePointPosition[0],y+usePointPosition[1])
  139. useCenterPoint= (x+useCenterPointPosition[0],y+useCenterPointPosition[1])
  140. centerPointOffset = (width,height)
  141. useCenterPointOffset = (useWidth,useHeight)
  142. positoin = [centerPoint,centerPointOffset,usePoint,useCenterPoint,useCenterPointOffset];
  143. print(wallIndex,width,height)
  144. drawPointCircle((pointA, pointB, pointC, pointD, centerPoint, usePoint, useCenterPoint))
  145. print(positoin)
  146. return positoin
  147. # 打印辅助点
  148. def drawPointCircle(points):
  149. for point in points:
  150. acad.model.AddCircle(APoint(point[0],point[1]), 50)
  151. # 从插入点开始顺时针记录已摆放的组件
  152. def tempStoragePosition(x,y,wallIndex,blockIndex):
  153. positoin = getCenterPoint(wallIndex, x, y, blockIndex);
  154. result = 1
  155. for item in positoins:
  156. # 判断实体是否相交
  157. result = isPositionFeasible(positoin,positoins[item])
  158. if(result == 0):
  159. positoins.clear()
  160. return result
  161. else:
  162. # 判断可用区域是否与实体相交
  163. usePoint = positoin[3]
  164. positoin[0] = usePoint
  165. positoin[1] = positoin[4]
  166. result = isPositionFeasible(positoin,positoins[item])
  167. if(result == 1):
  168. positoins[len(positoins)] = positoin
  169. return result
  170. # 计算位置是否可行
  171. def isPositionFeasible(position1,position2):
  172. acad.model.AddLine(APoint(position1[0][0],position1[0][1]),APoint(position2[0][0],position2[0][1]))
  173. acad.model.AddCircle(APoint(position1[0][0],position1[0][1]), 50)
  174. if(math.ceil(position1[0][0]-position2[0][0]) < (position1[1][0]+position2[1][0])/2):
  175. if(math.ceil(position1[0][1]-position2[0][1]) < (position1[1][1]+position2[1][1])/2):
  176. print("sorry! can not put here")
  177. return 0
  178. print("can put here 1")
  179. return 1
  180. else:
  181. print("can put here 2")
  182. return 1
  183. # printObjects()
  184. # printBlockObjects()
  185. # printTheTypeObject("BlockReference")
  186. # moveBlockReference('222')
  187. # a=[0,1,2]
  188. # moveBlockReferenceWithNewIndex('001',a)
  189. # print(isWorkable('222'))
  190. # getCenterPoint(3,2200,0,1)