GetAllModule.py 8.6 KB

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