Quellcode durchsuchen

判断实体间及实体与可用区域是否相交

duh vor 6 Jahren
Ursprung
Commit
ea41646abd
4 geänderte Dateien mit 125 neuen und 6 gelöschten Zeilen
  1. 109 1
      phxb/utils/GetAllModule.py
  2. 5 0
      phxb/utils/conf.py
  3. 2 2
      phxb/utils/getarray.py
  4. 9 3
      phxb/utils/main.py

+ 109 - 1
phxb/utils/GetAllModule.py

@@ -5,12 +5,24 @@ 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):
@@ -26,6 +38,7 @@ 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];
@@ -38,6 +51,9 @@ def moveBlockReferenceToWall(blockReference,wallIndex):
   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)
@@ -65,8 +81,10 @@ def changeConfModuleIndex(moduleIndex):
 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)):
@@ -76,10 +94,100 @@ def isWorkable(position):
       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'))
+# print(isWorkable('222'))
+# getCenterPoint(3,2200,0,1)

+ 5 - 0
phxb/utils/conf.py

@@ -5,7 +5,12 @@ roomWallLengths = (2000,2200,3000,1200)
 roomUsedLengths = [1200,0,0,1000]
 roomLengths = (3000,2200)
 # 模块长度
+humenLenght = 650
 moduleLengths = (800,1000,900)
+moduleHeights = (1500,550,700)
+moduleUsePoint = [[1200,800],[550,500],[700,450]]
+moduleUseCenterPoint = [[1200,800+humenLenght/2],[550+humenLenght/2,500],[700+humenLenght/2,450]]
+moduleUseLength = (500,1000,900)
 doorLength=800
 # 门与左右上下的间距
 doorAlign=[100,200,200,0]

+ 2 - 2
phxb/utils/getarray.py

@@ -171,11 +171,11 @@ def getitemIndexArrayNumber(itemIndex,m,n):
     return itemIndexArrayNumber
 # result = changeInt2Str(14,3);
 # print(result)
-# getAllPosition(3, 3)
+getAllPosition(3, 3)
 # getAllArrayNumber(4,3)
 # getitemIndexArray("11213",5,5)
 # getitemIndexArrayAndIndex("11213",5,5)
-print(getModuleIndex("11212",5,5))
+# print(getModuleIndex("11212",5,5))
 # getitemIndexArrayAndPosition("11213",5,5)
 # getitemIndexAndPositionArray("113", 4, 3)
 # number = getitemIndexArrayNumber("11143",5,5)

+ 9 - 3
phxb/utils/main.py

@@ -104,7 +104,7 @@ def myprocess(m, n):
         # indexAndPositionArray = getarray.getitemIndexAndPositionArray(positons[positon], m, n)
         number = getarray.getitemIndexArrayNumber(positons[positon], m, n)
         if (number == 1):
-            newFileName = _rootPath + "newfile" + str(positons[positon]) + ".dwg"
+            newFileName = _rootPath + "newfile\\" + "newfile" + str(positons[positon]) + ".dwg"
             if(not os.path.exists(newFileName)):
                 copyfile(_rootPath + _originFile, newFileName)
                 moveSingleTemplate(newFileName,positons[positon])
@@ -113,10 +113,12 @@ def myprocess(m, n):
             moduleIndexs = getarray.getModuleIndex(positons[positon],m,n)
             if(GetAllModule.isWorkable(positons[positon])):
                 for moduleIndex in moduleIndexs:
-                    newFileName = _rootPath + "newfile" + str(positons[positon]) + str(moduleIndex) + ".dwg"
+                    newFileName = _rootPath + "newfile\\"+ "newfile" + str(positons[positon]) + str(moduleIndex) + ".dwg"
                     if(not os.path.exists(newFileName)):
                         copyfile(_rootPath + _originFile, newFileName)
                         moveMultipleTemplate(newFileName,positons[positon],moduleIndex)
+                        # break
+                # break
             else:
                 print(positons[positon],"is not workable !!")
             # break
@@ -143,6 +145,10 @@ def moveMultipleTemplate(fileName,position,moduleIndex):
 # openOriginFile()
 # tempStorageModules()
 # print(is_open(_rootPath+_originFile))
-myprocess(4, 3)
+# myprocess(4, 3)
 # openFile(r"F:\autocad_temp\newfile023.dwg")
 # is_open(r"F:\autocad_temp\origin11.dwg")
+acad = Autocad(create_if_not_exists = False)
+acad.prompt("Hello, Autocad from Python\n")
+print(acad.doc.Name)
+moveMultipleTemplate(r"F:\autocad_temp\newfile\newfile.dwg",'002',[0,2,1])