python +pyautocad 在CAD中 画一个线段带箭头
from pyautocad import Autocad, APoint, aDouble
import math
pyacad = Autocad(create_if_not_exists=True)
pyacad.prompt("Hello! Autocad from pyautocad.")
print(pyacad.doc.Name)
def cad_arrow(dotted_start, dotted_end):
'''
画箭头
:param x1: 起点x
:param y1: 起点x
:param x2: 终点x
:param y2: 终点y
'''
plineObj = draw_Line(dotted_start ,dotted_end)
segmentIndex = 0
startWidth =0
endWidth =0.8
plineObj.SetWidth(segmentIndex, startWidth, endWidth)
pyacad.ActiveDocument.preferences.LineweightDisplay = 1
pyacad.ActiveDocument.Application.ZoomAll()
pyacad.ActiveDocument.Application.Update()
def draw_Line(dotted_start ,dotted_end):
'''
画线
:param dotted_start: 点起点
:param dotted_end: 点终点
'''
pnts = [APoint(*dotted_start), APoint(*dotted_end)]
pnts = [j for i in pnts for j in i]
pnts = aDouble(pnts)
plineObj=pyacad.model.AddPolyLine(pnts)
return plineObj
def calculate_point_on_segment(x1, y1, x2, y2, d=2.5):
'''
起点/终点 距离2.5处的点
:param x1: 起点
:param y1: 起点
:param x2: 终点
:param y2: 终点
:param d: 起点/终点 距离2.5处
:return: 起点/终点 距离2.5处 点坐标
'''
dx = x2 - x1
dy = y2 - y1
L = math.sqrt(dx**2 + dy**2)
dx /= L
dy /= L
x1_s = x1 + d * dx
y1_s = y1 + d * dy
x2_e = x2 - d * dx
y2_e = y2 - d * dy
return [(x1_s,y1_s),(x2_e,y2_e)]
def draw_line_segments_arrows(dotted_start,dotted_end):
'''
画线段带双向箭头
:param dotted_start:
:param dotted_end:
'''
draw_Line(dotted_start ,dotted_end)
po= calculate_point_on_segment(*dotted_start, *dotted_end,d=2.5 )
cad_arrow(dotted_start, po[0])
cad_arrow(dotted_end,po[1])
dotted_start ,dotted_end=(1,1),(60, 50)
draw_line_segments_arrows(dotted_start,dotted_end)
dotted_start ,dotted_end=(44,88),(90, 100)
draw_line_segments_arrows(dotted_start,dotted_end)