123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- # -*- coding: UTF-8 -*-
- from pyautocad import Autocad, APoint
- #这个true表示没有文件则打开一个,CAD有弹窗时会打开或者创建失败
- acad = Autocad(create_if_not_exists = True)
- acad.prompt("Hello, Autocad from Python\n")
- print(acad.doc.Name)
- def drawPoints(points):
- for point in points:
- acad.model.AddCircle(APoint(point[0],point[1]), 50)
- def drawLines(includePoints):
- if(len(includePoints)>1):
- for item in range(len(includePoints)):
- if(item>0):
- acad.model.AddLine(APoint(includePoints[item-1][0],includePoints[item-1][1]),APoint(includePoints[item][0],includePoints[item][1]))
- # 获取点到长方形的最短距离
- def getDoorBeginAndModuleUsePointDistince(x0,y0,width,height,x,y):
- points = [(x,y),(x0-width/2,y0-height/2),(x0-width/2,y0+height/2),(x0+width/2,y0-height/2),(x0+width/2,y0+height/2),(x0,y0)]
- drawPoints(points)
- includePoints = [(x,y)]
- if x<x0-width/2:
- if y>y0+height/2:
- includePoints.append((x0-width/2,y0+height/2))
- elif y>y0-height/2:
- includePoints.append((x0-width/2,y))
- else:
- includePoints.append((x0-width/2,y0-height/2))
- elif x<x0+width/2:
- if y>y0+height/2:
- includePoints.append((x,y0+height/2))
- elif y>y0-height/2:
- pass
- else:
- includePoints.append((x,y0-height/2))
- else:
- if y>y0+height/2:
- includePoints.append((x0+width/2,y0+height/2))
- elif y>y0-height/2:
- includePoints.append((x0+width/2,y))
- else:
- includePoints.append((x0+width/2,y0-height/2))
- drawLines(includePoints)
- # 获取点到长方形指定点的最短距离,默认点在长方形下方
- # x0,y0为矩形中点,xy为起始点,x1,y1为终点
- def getMovePath(x0,y0,width,height,x,y,x1,y1):
- points = [(x,y),(x0-width/2,y0-height/2),(x0-width/2,y0+height/2),(x0+width/2,y0-height/2),(x0+width/2,y0+height/2),(x0,y0),(x1,y1)]
- drawPoints(points)
- includePoints = [(x,y)]
- # 指定点在第0边;按下右上左方向
- if y1 == y0-height/2:
- includePoints.append((x1,y1))
- # 指定点在第1边;按下右上左方向
- elif x1 == x0+width/2:
- if x>x0+width/2:
- includePoints.append((x1,y1))
- else:
- includePoints.append((x0+width/2,y0-height/2))
- includePoints.append((x1,y1))
- # 指定点在第2边,暂只考虑从左边开始接近
- elif y1 == y0+height/2:
- if x<x0-width/2:
- includePoints.append((x0-width/2,y0+height/2))
- includePoints.append((x1,y1))
- else:
- includePoints.append((x0-width/2,y0-height/2))
- includePoints.append((x0-width/2,y0+height/2))
- includePoints.append((x1,y1))
- # 指定点在第3边
- else:
- if x<x0-width/2:
- includePoints.append((x1,y1))
- else:
- includePoints.append((x0-width/2,y0-height/2))
- includePoints.append((x1,y1))
- # drawLines(includePoints)
- return includePoints
- # 获取点到长方形指定点的最短距离
- # x0,y0为矩形中点,xy为起始点,x1,y1为终点
- def getMovePath2(x0,y0,width,height,x,y,x1,y1):
- includePoints = [(x,y)]
- bottom = y0-height/2
- right = x0+width/2
- top = y0+height/2
- left = x0-width/2
- points = [(x,y),(left,bottom),(left,top),(right,bottom),(right,top),(x0,y0),(x1,y1)]
- drawPoints(points)
- # 终点在模块最上边
- if y1 == top:
- if y>=top:
- includePoints.append((x1,y1))
- elif x<=left:
- includePoints.append((left,top))
- includePoints.append((x1,y1))
- elif x>=right:
- includePoints.append((right,top))
- includePoints.append((x1,y1))
- elif x+x1<=2*x0:
- includePoints.append((left,bottom))
- includePoints.append((left,top))
- includePoints.append((x1,y1))
- else:
- includePoints.append((right,bottom))
- includePoints.append((right,top))
- includePoints.append((x1,y1))
- elif y1 == bottom:
- if y<=bottom:
- includePoints.append((x1,y1))
- elif x<=left:
- includePoints.append((left,bottom))
- includePoints.append((x1,y1))
- elif x>=right:
- includePoints.append((right,bottom))
- includePoints.append((x1,y1))
- elif x+x1<=2*x0:
- includePoints.append((left,top))
- includePoints.append((left,bottom))
- includePoints.append((x1,y1))
- else:
- includePoints.append((right,top))
- includePoints.append((right,bottom))
- includePoints.append((x1,y1))
- elif x1 == left:
- if x<=left:
- includePoints.append((x1,y1))
- elif y>=top:
- includePoints.append((left,top))
- includePoints.append((x1,y1))
- elif y<=bottom:
- includePoints.append((left,bottom))
- includePoints.append((x1,y1))
- elif y+y1<=2*y0:
- includePoints.append((right,bottom))
- includePoints.append((left,bottom))
- includePoints.append((x1,y1))
- else:
- includePoints.append((right,top))
- includePoints.append((left,top))
- includePoints.append((x1,y1))
- else:
- if x>=right:
- includePoints.append((x1,y1))
- elif y>=top:
- includePoints.append((right,top))
- includePoints.append((x1,y1))
- elif y<=bottom:
- includePoints.append((right,bottom))
- includePoints.append((x1,y1))
- elif y+y1<=2*y0:
- includePoints.append((left,bottom))
- includePoints.append((right,bottom))
- includePoints.append((x1,y1))
- else:
- includePoints.append((left,top))
- includePoints.append((right,top))
- includePoints.append((x1,y1))
- drawLines(includePoints)
- return includePoints
- # 获取模块可用点到另一个模块使用点间的距离
- def getModuleMovePath(x10,y10,width1,height1,x1,y1,x20,y20,width2,height2,x2,y2):
- includePoints1 = getMovePath(x10,y10,width1,height1,x2,y2,x1,y1)
- includePoints2 = getMovePath(x20,y20,width2,height2,x1,y1,x2,y2)
- includePoints = []
- for item in range(len(includePoints1)):
- if(item>0):
- includePoints.append(includePoints1[item])
- for item in range(len(includePoints2)):
- if(item>0):
- includePoints.insert(0,includePoints2[item])
- print(includePoints)
- drawLines(includePoints)
- return includePoints
- # getDoorBeginAndModuleUsePointDistince(3300,1500,900,600,10,3000)
- # getDoorBeginAndModuleUsePointDistince(3300,2800,900,600,1000,3000)
- # getDoorBeginAndModuleUsePointDistince(3300,2800,900,600,3100,3800)
- # getDoorBeginAndModuleUsePointDistince(3300,2800,900,600,4000,900)
- # getMovePath(3300,2800,900,600,0,0,3500,2500)
- # getMovePath(3300,2800,900,600,0,0,2850,2700)
- # getMovePath(3300,2800,900,600,3000,0,2850,2700)
- # getMovePath(3300,2800,900,600,3300,0,3500,3100)
- # getMovePath2(800,700,600,400,2000,2000,800,900)
- # getMovePath2(800,700,600,400,300,800,800,900)
- # getMovePath2(800,700,600,400,2000,0,800,900)
- # getMovePath2(800,700,600,400,1000,100,800,900)
- # getMovePath2(800,700,600,400,600,100,800,900)
- # getModuleMovePath(800,700,600,400,900,500,1500,1100,400,800,1700,800)
- def testAppend():
- score = []
- for i in range(100):
- s = []
- s.append(i)
- score.append(s)
- print(score)
- def testRange():
- for i in range(3,10,2):
- print(i)
- testRange()
- # testAppend()
|