生成PDMS管口方位图 python方案
思路:通过python3的ezdxf模块,将pdms导出的管嘴信息生成.dxf图,网上已有初步方案(见参考),本文章主要增加以实际案例绘制管嘴部分内容。
一、代码
import math # 导入math模块
import ezdxf # 导入ezdxf模块
# 定义点的函数
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def getx(self):
return self.x
def gety(self):
return self.y
def getAxis(self):
return (self.x, self.y)
# 定义直线函数
class Getlen:
def __init__(self, p1, p2):
self.x = p1.getx() - p2.getx()
self.y = p1.gety() - p2.gety()
# 用math.sqrt()求平方根
self.len = math.sqrt((self.x ** 2) + (self.y ** 2))
# 定义得到直线长度的函数
def getlen(self):
return self.len
# 通过矩形中心点p, 与x轴弧度,获取矩形四点坐标
def recLoc(p, angle, height, bottom):
# assert angle <= 35, 'angle out of index'
# angle *= (10 / 180) * math.pi # 弧度
# xo = np.cos(angle)
# yo = np.sin(angle)
xo = math.cos(angle)
yo = math.sin(angle)
y1 = p.x + height / 2 * yo
x1 = p.y - height / 2 * xo
y2 = p.x - height / 2 * yo
x2 = p.y + height / 2 * xo
return [
Point(y1 - bottom / 2 * xo, x1 - bottom / 2 * yo),
Point(y2 - bottom / 2 * xo, x2 - bottom / 2 * yo),
Point(y2 + bottom / 2 * xo, x2 + bottom / 2 * yo),
Point(y1 + bottom / 2 * xo, x1 + bottom / 2 * yo)
]
# 管口朝向转为角度,包含('W','E 30 S')两种格式
def getNozzAngle(s):
arr = s.split(' ')
phasJson = {
'E': (0, 1),
'N': (90, 2),
'W': (180, 3),
'S': (270, 4)}
try:
angle = float(phasJson[arr[0]][0])
cursor = 1 if phasJson[arr[2]][1] > phasJson[arr[0]][1] else -1
if arr[0] == 'E' and arr[2] == 'S':
cursor = -1
angle += cursor * float(arr[1])
return (angle if angle > 0 else 360 + angle, True)
except (KeyError, IndexError) as e:
return (phasJson[arr[0]][0], False)
# *求圆和直线之间的交点
# *直线方程:y = kx + b
# *圆的方程:(x - m)² + (x - n)² = r²
# *x1, y1 = 线坐标1, x2, y2 = 线坐标2, m, n = 圆坐标, r = 半径
def getPointBeCircleAndLine(k, b, m, n, r):
aX = 1 + k * k
bX = 2 * k *