GetAllModule.py 8.0 KB

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