我最终让我的绘图程序创建了一个非常基本的 Autocad 脚本。我提到了这个关于从等高线图中提取 x,y 数据以编写 Autocad 脚本的问题。以下是相关功能:
def make_autocad_script(outfile_name, contour):
'''
Creates an Autocad script which contains polylines for each contour.
Args
outfile_name: the name of the Autocad script file.
contour: the contour plot that needs to be exported to Autocad.
'''
with open(outfile_name, 'w', newline='') as outfile:
writer = csv.writer(outfile, delimiter=',', )
# each collection is associated with a contour level
for collection in contour.collections:
# If the contour level is never reached, then the collection will be an empty list.
if collection:
# Set color for contour level
outfile.write('COLOR {}\n'.format(random.randint(1,100)))
# Each continuous contour line in a collection is a path.
for path in collection.get_paths():
vertices = path.vertices
# pline is an autocad command for polyline. It interprets
# the next (x,y) pairs as coordinates of a line until
# it sees a blank line.
outfile.write('pline\n')
writer.writerows(vertices)
outfile.write('\n')
我发送我需要make_autocad_script
的绘图,然后在 Autocad 中导入脚本outfile
。contour
这会将每个轮廓绘制为随机颜色,但可以用您想要的任何颜色替换。