python绘制CAD断面
引用的python第三方库
pyautocad模块,官方文档讲解的并不多,最主要部分是连接CAD软件的讲解,示例代码极少。pyautocad是依赖于cad运行的第三方库。意味着运行代码运行时CAD必须处打开状态,否则程序就会BUG.
pyautocad 库由俄罗斯工程师 Roman Haritonov 开发,用于简化使用 Python 语言书写 AutoCAD ActiveX Automation 脚本。主要参考资料如下:
• PiPy: pyautocad 0.2.0 。
• GitHub: reclosedev/pyautocad 。
• Documentation: pyautocad’s documentation 。
math模块,python内置的标准库。主要提供了很多关于数学方面的函数。
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
绘制断面需要什么数据
1.从CASS提取所需的高程点 2.其他数据整理代码都可以实现,只需要填入相应的参数,处理完成后可以得到一个二维数组(第一点到个点之间的距离,高程*************)。
代码实现
"""主函数"""
import Data_collation
import Art_CAD
def main():
pass
if __name__
== "__main__":
main
()
"""数据分析模块"""
import math
def Read_Data():
"""
排序,把点从河流的上游到下游,从左到右的方式进行排序
原理主要根据X或Y的坐标大小进行分析
:return: 返回正确排序的高程点
"""
datas_list
= []
x_y
= input("请输入需要按X轴或Y轴排序(输入其他字符则按Y轴处理):")
x_y
= x_y
.upper
()
if x_y
== "X":
x_y
= 2
else:
x_y
= 1
with open(r
"C:\Users\n\Desktop\test\lxh.txt", "r", encoding
="utf-8")as f
:
datas
= f
.readlines
()
for data
in datas
:
data
= data
.strip
("\n")
data
= data
.split
(",")
data
[2] = float(data
[2])
data
[3] = float(data
[3])
data
= tuple(data
)
datas_list
.append
(data
)
T_F
= input("请输入排序方式:T或F")
T_F
= T_F
.upper
()
if T_F
== "T":
new_datas_list
= sorted(datas_list
, key
=lambda xy
: xy
[x_y
], reverse
=False)
else:
new_datas_list
= sorted(datas_list
, key
=lambda xy
: xy
[x_y
], reverse
=True)
return new_datas_list
def Calc_Distance(distance_datas
):
"""
计算各点与第一点的距离
:param distance_datas: x,y的原始坐标
:return: None
"""
y1
, x1
= distance_datas
[0][2], distance_datas
[0][3]
for xy
in distance_datas
:
y
, x
= xy
[2], xy
[3]
distance
= round(math
.sqrt
((x1
- x
) ** 2 + (y
- y1
) ** 2), 2)
with open(r
"C:\Users\n\Desktop\test\S6666.txt", "a")as f
:
f
.write
("%s,%s,%s,%s\n" % (xy
[0], distance
, xy
[4], xy
[1]))
"""绘图模块"""
"""
图层函数暂时还没有内容,断面图的整体框架已经出来了。
"""
from pyautocad
import Autocad
, aDouble
, APoint
pyacad
= Autocad
(create_if_not_exists
=True)
height_list
= []
def Read_Starandtxt():
"""
读取标准的断面数据文件*.dat
:return: 含高程点数据的列表
"""
with open(r
"C:\Users\n\Desktop\test\aa.txt", "r")as f
:
point_data
= f
.readlines
()
for height_datas
in point_data
:
height_datas
= height_datas
.strip
("\n")
height_datas
= height_datas
.split
(",")
height_list
.append
(float(height_datas
[2]))
return point_data
def Handle1(textString
, Height
, Distance
):
"""
辅助注记函数,具体请可以参考pyautocad文档
:param textString: 注记的文集信息
:param Height: 高程
:param Distance: 距离
:return:
"""
insertPnt
= APoint
(0, 0)
height
= 2.5
textObj
= pyacad
.model
.AddText
(textString
, insertPnt
, height
)
AlignNum
= 13
textObj
.Alignment
= AlignNum
insertPnt
= APoint
(Distance
, Height
)
textObj
.TextAlignmentPoint
= insertPnt
BasePoint
= insertPnt
textObj
.ScaleEntity
(BasePoint
, 0.2)
def Point_And_Line():
"""
画断面线和点
:return: 没有返回值
"""
Point_List
= []
datas
= Read_Starandtxt
()
for point
in datas
:
point
= point
.strip
("\n")
point
= point
.split
(",")
distance
, height
= float(point
[1]), float(point
[2])
pyacad
.ActiveDocument
.SendCommand
(
"point" + chr(13) + "{}".format(distance
) + "," + "{}".format(height
) + chr(13))
Point_List
.append
(distance
)
Point_List
.append
(height
)
Point_List
.append
(0)
Point_List
= aDouble
(Point_List
)
pyacad
.model
.AddPolyLine
(Point_List
)
def Notes():
"""
注记信息(距离,高程,代码)
:return: 没有返回值
"""
datas
= Read_Starandtxt
()
for point
in datas
:
point
= point
.strip
("\n")
point
= point
.split
(",")
textString
= point
[3]
Distance
= float(point
[1])
Height
= float(point
[2])
Handle1
(textString
, Height
- 0.8, Distance
)
Handle1
(Height
, Height
, Distance
)
Handle1
(Distance
, Height
+ 0.8, Distance
)
def Outer_Frame():
"""
画断面图的外围方框
:return: 没有返回值
"""
max_height
= max(height_list
)
min_heighe
= min(height_list
)
point1
= [0, min_heighe
- 3, 0]
point2
= [0, max_height
+ 73, 0]
point4
= [300, max_height
+ 73, 0]
point3
= [300, min_heighe
- 3, 0]
point5
= [150, max_height
+ 73, 0]
point6
= [150, min_heighe
- 3, 0]
frame_line
= point1
+ point2
+ point4
+ point3
+ point1
center_line
= point5
+ point6
frame_line
= aDouble
(frame_line
)
pyacad
.model
.AddPolyLine
(frame_line
)
center_line
= aDouble
(center_line
)
pyacad
.model
.AddPolyLine
(center_line
)
def Ruler():
"""
画高程标尺
:return: 没有返回值
"""
max_height
= max(height_list
)
min_heighe
= min(height_list
)
line_start
= (30, min_heighe
- 3, 0, 31.5, min_heighe
- 3, 0)
line_end
= (30, max_height
+ 3, 0, 31.5, max_height
+ 3, 0)
line_start
= aDouble
(line_start
)
pyacad
.model
.AddPolyLine
(line_start
)
line_end
= aDouble
(line_end
)
pyacad
.model
.AddPolyLine
(line_end
)
longitudinal_line
= (30, min_heighe
- 3, 0, 30, max_height
+ 3, 0)
longitudinal_line
= aDouble
(longitudinal_line
)
pyacad
.model
.AddPolyLine
(longitudinal_line
)
TwoLine_to_point
= int(min_heighe
- 2)
while True:
line
= (30, TwoLine_to_point
, 0, 30.8, TwoLine_to_point
, 0)
line
= aDouble
(line
)
pyacad
.model
.AddPolyLine
(line
)
insertPnt
= APoint
(30.8, TwoLine_to_point
- 0.2)
height
= 2.5
textObj
= pyacad
.model
.AddText
(TwoLine_to_point
, insertPnt
, height
)
BasePoint
= insertPnt
textObj
.ScaleEntity
(BasePoint
, 0.2)
TwoLine_to_point
+= 1
if TwoLine_to_point
>= max_height
+ 3:
break
def water_line():
"""
画水压线
:return: 没有返回值
"""
water_line_xy
= []
datas
= Read_Starandtxt
()
for point
in datas
:
point
= point
.strip
("\n")
point
= point
.split
(",")
if point
[3] == "水边":
water_line_xy
.append
(float(point
[1]))
water_line_xy
.append
(float(point
[2]))
water_line_xy
.append
(0)
midpoint
= (water_line_xy
[0] + water_line_xy
[3]) / 2
height
= water_line_xy
[1]
water_line_xy
= aDouble
(water_line_xy
)
pyacad
.model
.AddPolyLine
(water_line_xy
)
Handle1
("2020年12月8日", height
, midpoint
)
def Layer():
"""
增添图层,指定样式,美化断面图。
:return:
"""
pass
如有疑问或需数据请联系作者可留言博客或+ qq:1769190045
文章内容只供参考学习阅读,不可他用(特别商用)。侵权必追究其法律责任
————————部分内容参考他人博客文章————————