目录[-]

在建模的过程中,可能出现排水管线方向错误导致出错问题,所以在前期进行管线方向检查可以避免后期的错误出现,但是如果一个个点击操作,则效率低下,故采用python根据CAD写出的pl线数据进行计算,生成箭头多段线文本使用CAD插件画回图纸,可以直观查看管线方向是否错误。

arrow1

原理:以线段中间为起点,通过三角函数垂直与线段的角度进行位置平移,而后通过平移生成多段线的所有坐标点,最后形成一个多段线文本,将所有箭头合并后输出,画回CAD软件。

代码如下:

#该代码为自动生成多段线每一段封闭箭头并移动到管网线旁
#作者:冯桂和,qq:317882351

import os
import math

vertical_len=3  #垂直移动3
parallel_len=4  #平行移动
arrowlen=2      #箭头长度2
arrlen=5        #箭头线长度5

#读数转弧度公式
def degree(angle):
    arcnum=angle/180*3.14159265
    return arcnum

jiantouPL=""

#箭头计算公式
def jiantou(x1,y1,x2,y2):
    xxx1=x1
    yyy1=y1
    xxx2=x2
    yyy2=y2
    xm=(x1+x2)/2
    ym=(y1+y2)/2
    x2=xm
    y2=ym
    xc=x2-x1
    yc=y2-y1
    if x1==x2:
        xc=xc+0.0001
    xyc=yc/xc
    arc=math.atan(xyc)
    #移动基点位置(箭头尖点)
    if x1<x2:
        arcc1=arc                   #往垂直方向移动
        x2=x2-math.sin(arcc1)*vertical_len
        y2=y2+math.cos(arcc1)*vertical_len
        arcc2=arc-degree(90)        #往线段方向移动
        x2=x2-math.sin(arcc2)*parallel_len
        y2=y2+math.cos(arcc2)*parallel_len
    elif x1>x2:
        arcc1=arc+degree(180)
        x2=x2+math.sin(arcc1)*vertical_len
        y2=y2-math.cos(arcc1)*vertical_len
        arcc2=arc-degree(90)        
        x2=x2+math.sin(arcc2)*parallel_len
        y2=y2-math.cos(arcc2)*parallel_len
    else:
        arcc1=arc
        x2=x2+math.sin(arcc1)*vertical_len
        y2=y2+math.cos(arcc1)*vertical_len
        if yyy1>yyy2:
            x2=x2-2*math.sin(arcc1)*vertical_len
            y2=y2+math.cos(arcc1)*vertical_len
        arcc2=arc-degree(90)
        x2=x2-math.sin(arcc2)*parallel_len
        y2=y2+math.cos(arcc2)*parallel_len
    #箭头尖点两边线的辅助距离,xx/yy外侧线,xx2/yy2内侧线
    if xxx1<xxx2:
        arcc=degree(90)+arc-degree(45)+degree(20)
        xx=math.sin(arcc)*arrowlen
        yy=math.cos(arcc)*arrowlen
    elif xxx1>xxx2:
        arcc=degree(90)+arc+degree(135)+degree(20)
        xx=math.sin(arcc)*arrowlen
        yy=math.cos(arcc)*arrowlen
    if xxx1==xxx2:
        arcc=degree(90)+arc+degree(135)+degree(180)+degree(20)
        xx=math.sin(arcc)*arrowlen
        yy=math.cos(arcc)*arrowlen
    if xxx1<xxx2:
        arcc=degree(90)+arc-degree(45)-degree(20)
        xx2=math.sin(arcc)*arrowlen
        yy2=math.cos(arcc)*arrowlen
        if yyy1<yyy2:
            arcc=degree(90)+arc-degree(45)-degree(20)
            xx2=math.sin(arcc)*arrowlen
            yy2=math.cos(arcc)*arrowlen
    elif xxx1>xxx2:
        arcc=degree(90)+arc+degree(135)-degree(20)
        xx2=math.sin(arcc)*arrowlen
        yy2=math.cos(arcc)*arrowlen
        if yyy1<yyy2:
            arcc=degree(90)+arc+degree(135)-degree(20)
            xx2=math.sin(arcc)*arrowlen
            yy2=math.cos(arcc)*arrowlen
    if xxx1==xxx2:
        arcc=degree(90)+arc+degree(135)+degree(180)-degree(20)
        xx2=math.sin(arcc)*arrowlen
        yy2=math.cos(arcc)*arrowlen
    #箭头三角形上的五个点生成
    xo1=x2-xx
    yo1=y2+yy
    xo2=x2
    yo2=y2
    xo3=x2-yy2
    yo3=y2-xx2
    xo4m=(xo1+xo3)/2
    yo4m=(yo1+yo3)/2
    xo4mc=(xo4m+xo3)/2
    yo4mc=(yo4m+yo3)/2
    xo4mf=(xo4m+xo1)/2
    yo4mf=(yo4m+yo1)/2
    xo4=(xo4m+xo4mc)/2
    yo4=(yo4m+yo4mc)/2
    xo0=(xo4m+xo4mf)/2
    yo0=(yo4m+yo4mf)/2
    #箭头长平行线延伸点内侧生成
    if xxx1<=xxx2:
        arcc2=arc-degree(90)
        xo5=xo4+math.sin(arcc2)*arrlen
        yo5=yo4-math.cos(arcc2)*arrlen
    elif xxx1>xxx2:
        arcc2=arc-degree(90)
        xo5=xo4-math.sin(arcc2)*arrlen
        yo5=yo4+math.cos(arcc2)*arrlen
    #箭头长平行线延伸点外侧生成
    if xxx1<=xxx2:
        arcc2=arc-degree(90)
        xo6=xo0+math.sin(arcc2)*arrlen
        yo6=yo0-math.cos(arcc2)*arrlen
    elif xxx1>xxx2:
        arcc2=arc-degree(90)
        xo6=xo0-math.sin(arcc2)*arrlen
        yo6=yo0+math.cos(arcc2)*arrlen
    #PL写入格式生成
    jiantoutxt=""
    jiantoutxt=jiantoutxt+"8"+"\n"
    jiantoutxt=jiantoutxt+str(xo0)+"  "+str(yo0)+"\n"
    jiantoutxt=jiantoutxt+str(xo1)+"  "+str(yo1)+"\n"
    jiantoutxt=jiantoutxt+str(xo2)+"  "+str(yo2)+"\n"
    jiantoutxt=jiantoutxt+str(xo3)+"  "+str(yo3)+"\n"
    jiantoutxt=jiantoutxt+str(xo4)+"  "+str(yo4)+"\n"
    jiantoutxt=jiantoutxt+str(xo5)+"  "+str(yo5)+"\n"
    jiantoutxt=jiantoutxt+str(xo6)+"  "+str(yo6)+"\n"
    jiantoutxt=jiantoutxt+str(xo0)+"  "+str(yo0)+"\n"
    # print(xo1,",",yo1,"\n",xo2,",",yo2,"\n",xo3,",",yo3,'\n')
    return jiantoutxt

#打开多段线文本PL.txt
filesPL = "PL.txt"
if os.path.exists(filesPL):
    T=open('PL.txt','r')        #打开文件并赋予变量
    txt=T.readlines()           
    a=[]                        #定义列表
    for w in txt:               #将txt内容写入列表
        w=w.replace('\n','')
        a.append(w)
    T.close()
    b=len(a)                    #列表长度
    sum1=0                      #行数标识符累加
    a1=[]
    a1b=[]
    a1c=[]                      
    while sum1<b:               #逐个坐标输出,XY之间用空格隔开
        x=int(a[sum1])
        for i in range(0,x):
            a1.append(a[sum1+i+1])
        a1c.append(int(a[sum1]))
        sum1=sum1+x+1
        a1b.append(sum1)
    a1_2=[[0]*2 for _ in range(len(a1))]
    for i in range(0,len(a1_2)):
        t=a1[i]
        position=t.index(" ")
        tt=list(t)
        ttt=tt[0:position]
        tf=""
        tf=tf.join(ttt)
        tfn=float(tf)
        a1_2[i][0]=tfn
        ttt=tt[position+1:]
        tf=""
        tf=tf.join(ttt)
        tfn=float(tf)
        a1_2[i][1]=tfn
    rangelen=0
    for i in range(0,len(a1c)):
        rangelen=rangelen+a1c[i]
    ii=a1c[0]-1
    x=1
    for i in range(0,rangelen-1):
        if i==ii:
            ii=ii+a1c[x]
            x=x+1
            # print(ii)
            continue
        jiantouPL=jiantouPL+jiantou(a1_2[i][0],a1_2[i][1],a1_2[i+1][0],a1_2[i+1][1])
        # jiantouPL=jiantouPL+"a"
    print("\n箭头文本已生成\n")
    with open("PL Arrow_v2.2.txt","w") as W:
        W.write(jiantouPL)
else:
    print("\nPL.txt文本不存在,请确保PL线文本已生成!\n")