# -*- coding: UTF-8 -*- from pyautocad import Autocad, APoint from utils import conf import math #这个true表示没有文件则打开一个,CAD有弹窗时会打开或者创建失败 acad = Autocad(create_if_not_exists = False) # 组件实体点位置 positoins={} # 组件使用点位置 useAreaPositions={} # acad.prompt("Hello, Autocad from Python\n") # print(acad.doc.Name) def printObjects(): #遍历cad图形对象 for obj in acad.iter_objects(): print(obj.ObjectName) def printBlockObjects(): #遍历cad图形对象 for obj in acad.iter_objects("BlockReference"): print(obj.name,obj.HasAttributes) if obj.HasAttributes: attrs = obj.GetAttributes() for attrib in attrs: print(" {}: {}".format(attrib.TagString, attrib.TextString)) def printTheTypeObject(type): #按类型查找出所有某种图元 for text in acad.iter_objects(type): print(text.name) def moveBlockReference(position): print(conf.moduleLengths) for blockReference in acad.iter_objects("BlockReference"): print(blockReference.name) for item in conf.names: if(blockReference.name == item): moveBlockReferenceToWall(blockReference,position[conf.names.index(item)]) def moveBlockReferenceToWall(blockReference,wallIndex): wallIndex = int(wallIndex) blockReference.rotate(APoint(blockReference.InsertionPoint),0.5*(wallIndex+1)*math.pi) blockIndex = conf.names.index(blockReference.name) print("begin to put ",blockReference.name) if(wallIndex == 0): blockReference.move(APoint(blockReference.InsertionPoint),APoint(0+conf.moduleLengths[blockIndex]+conf.roomUsedLengths[0],0)) conf.roomUsedLengths[0] = conf.roomUsedLengths[0]+conf.moduleLengths[blockIndex]; elif (wallIndex == 1): blockReference.move(APoint(blockReference.InsertionPoint),APoint(conf.roomLengths[0],conf.roomLengths[1]-conf.roomUsedLengths[1])) conf.roomUsedLengths[1] = conf.roomUsedLengths[1]+conf.moduleLengths[blockIndex]; elif (wallIndex == 2): blockReference.move(APoint(blockReference.InsertionPoint),APoint(conf.roomUsedLengths[2],conf.roomLengths[1])) conf.roomUsedLengths[2] = conf.roomUsedLengths[2]+conf.moduleLengths[blockIndex]; else: blockReference.move(APoint(blockReference.InsertionPoint),APoint(0,conf.roomUsedLengths[3])) conf.roomUsedLengths[3] = conf.roomUsedLengths[3]+conf.moduleLengths[blockIndex]; checkPositionIntersect = tempStoragePosition(blockReference.InsertionPoint[0],blockReference.InsertionPoint[1],wallIndex,blockIndex) if(checkPositionIntersect == 0): print("wallIndex=",wallIndex,blockReference.name," not walkable......") # 根据新顺序移动模块 def moveBlockReferenceWithNewIndex(position,moduleIndex): print(conf.moduleLengths) blockReferences = [] for blockReference in acad.iter_objects("BlockReference"): print(blockReference.name) for item in conf.names: if(blockReference.name == item): blockReferences.append(blockReference) for index in moduleIndex: moveBlockReferenceToWall(blockReferences[index],position[moduleIndex.index(index)]) # 重置已使用墙的长度 def resetConfUsed(): conf.roomUsedLengths=[1200,0,0,1000] # 对模块重新排序 def changeConfModuleIndex(moduleIndex): names=[] moduleLengths=[] for index in range(len(conf.moduleLengths)): names.append(conf.names[moduleIndex[index]]) moduleLengths.append(conf.moduleLengths[moduleIndex[index]]) conf.names = names conf.moduleLengths = tuple(moduleLengths) # 重置模块顺序 def resetModuleIndex(): conf.names=['md_py','md_xs','md_rc'] conf.moduleLengths = (800,1000,800) # 判断位置是否可行 def isWorkable(position): resetConfUsed() positoins.clear() print("position=",position) needLength=[0]*4 for positionIndex in range(len(position)): wallIndex = int(position[positionIndex]) needLength[wallIndex]+=conf.moduleLengths[positionIndex] if(needLength[wallIndex]+conf.roomUsedLengths[wallIndex] > conf.roomLengths[wallIndex%2]): print(needLength[wallIndex]+conf.roomUsedLengths[wallIndex],conf.roomLengths[math.floor(wallIndex/2)],wallIndex) return 0 return 1 # 获取组件的四点坐标 def getCenterPoint(wallIndex, x, y, blockIndex): height = conf.moduleLengths[blockIndex]; width = conf.moduleHeights[blockIndex]; useHeight = conf.moduleUseLength[blockIndex] useWidth = conf.humenLenght usePointPosition = conf.moduleUsePoint[blockIndex] useCenterPointPosition = conf.moduleUseCenterPoint[blockIndex] pointA = (x,y) print("insertPoint=",x,y) if(wallIndex == 0): pointB = (x,y+width) pointC = (x-height,y+width) pointD = (x-height,y) centerPoint = (x-height/2,y+width/2) usePoint= (x-usePointPosition[1],y+usePointPosition[0]) useCenterPoint= (x-useCenterPointPosition[1],y+useCenterPointPosition[0]) centerPointOffset = (height,width) useCenterPointOffset = (useHeight,useWidth) elif(wallIndex == 1): pointB = (x-width,y) pointC = (x-width,y-height) pointD = (x,y-height) centerPoint = (x-width/2,y-height/2) usePoint= (x-usePointPosition[0],y-usePointPosition[1]) useCenterPoint= (x-useCenterPointPosition[0],y-useCenterPointPosition[1]) centerPointOffset = (width,height) useCenterPointOffset = (useWidth,useHeight) elif(wallIndex == 2): pointB = (x,y-width) pointC = (x+height,y-width) pointD = (x+height,y) centerPoint = (x+height/2,y-width/2) usePoint= (x+usePointPosition[1],y-usePointPosition[0]) useCenterPoint= (x+useCenterPointPosition[1],y-useCenterPointPosition[0]) centerPointOffset = (height,width) useCenterPointOffset = (useHeight,useWidth) else: pointB = (x+width,y) pointC = (x+width,y+height) pointD = (x,y+height) centerPoint = (x+width/2,y+height/2) usePoint= (x+usePointPosition[0],y+usePointPosition[1]) useCenterPoint= (x+useCenterPointPosition[0],y+useCenterPointPosition[1]) centerPointOffset = (width,height) useCenterPointOffset = (useWidth,useHeight) positoin = [centerPoint,centerPointOffset,usePoint,useCenterPoint,useCenterPointOffset]; print(wallIndex,width,height) drawPointCircle((pointA, pointB, pointC, pointD, centerPoint, usePoint, useCenterPoint)) print(positoin) return positoin # 打印辅助点 def drawPointCircle(points): for point in points: acad.model.AddCircle(APoint(point[0],point[1]), 50) # 从插入点开始顺时针记录已摆放的组件 def tempStoragePosition(x,y,wallIndex,blockIndex): positoin = getCenterPoint(wallIndex, x, y, blockIndex); result = 1 for item in positoins: # 判断实体是否相交 result = isPositionFeasible(positoin,positoins[item]) if(result == 0): positoins.clear() return result else: # 判断可用区域是否与实体相交 usePoint = positoin[3] positoin[0] = usePoint positoin[1] = positoin[4] result = isPositionFeasible(positoin,positoins[item]) if(result == 1): positoins[len(positoins)] = positoin return result # 计算位置是否可行 def isPositionFeasible(position1,position2): acad.model.AddLine(APoint(position1[0][0],position1[0][1]),APoint(position2[0][0],position2[0][1])) acad.model.AddCircle(APoint(position1[0][0],position1[0][1]), 50) if(math.ceil(position1[0][0]-position2[0][0]) < (position1[1][0]+position2[1][0])/2): if(math.ceil(position1[0][1]-position2[0][1]) < (position1[1][1]+position2[1][1])/2): print("sorry! can not put here") return 0 print("can put here 1") return 1 else: print("can put here 2") return 1 # printObjects() # printBlockObjects() # printTheTypeObject("BlockReference") # moveBlockReference('222') # a=[0,1,2] # moveBlockReferenceWithNewIndex('001',a) # print(isWorkable('222')) # getCenterPoint(3,2200,0,1)