仿照这个代码修改,将shujuf1.txt替换为shujuf2.txt,画出有四条竖向折痕。代码:# _*_ coding: utf-8 _*_
import ezdxf # type: ignore
import matplotlib.font_manager as fm # type: ignore
import matplotlib.pyplot as plt # type: ignore
import numpy as np # type: ignore
import pandas as pd
import ezdxf
import numpy as np
import ezdxf
import numpy as np
# 读取节点坐标文件并放大400倍
file_path = r"D:\曲纹折纸\文献\Codes\Codes\shujuf1.txt"
points = []
line_count = 0
scale_factor = 400 # 缩放因子
with open(file_path, 'r') as f:
for line in f:
line_count += 1
stripped_line = line.strip()
if not stripped_line:
print(f"跳过第 {line_count} 行(空行)")
continue
parts = stripped_line.split(',')
if len(parts) < 2:
print(f"警告:第 {line_count} 行格式错误 - '{line.strip()}'")
continue
try:
# 读取坐标并放大400倍
x = float(parts[0]) * scale_factor
y = float(parts[1]) * scale_factor
points.append((x, y))
except ValueError as e:
print(f"错误处理第 {line_count} 行: {e} - '{line.strip()}'")
# 验证数据完整性
if not points:
raise ValueError("未读取到有效坐标数据")
if len(points) % 3 != 0:
print(f"警告:节点总数 {len(points)} 不能被3整除,可能影响分组")
# 将节点分为三组(左、中、右)
num_points = len(points) // 3
left = points[:num_points]
middle = points[num_points:2*num_points]
right = points[2*num_points:]
print(f"成功读取 {len(points)} 个坐标点")
print(f"缩放因子: {scale_factor}x")
print(f"左边界: {len(left)} 点, 中间: {len(middle)} 点, 右边界: {len(right)} 点")
# 创建DXF文档
doc = ezdxf.new(dxfversion='R2010')
msp = doc.modelspace()
# 添加图层
doc.layers.add('VERTICAL', color=1) # 红色竖边
doc.layers.add('HORIZONTAL', color=2) # 黄色水平线
# 绘制三条竖边
for edge, layer_name in zip([left, middle, right], ['VERTICAL']*3):
msp.add_lwpolyline(
points=edge,
dxfattribs={'layer': layer_name, 'closed': False}
)
# 绘制水平折痕线
for i in range(min(len(left), len(middle), len(right))):
if i < len(left) and i < len(middle):
msp.add_line(
start=left[i],
end=middle[i],
dxfattribs={'layer': 'HORIZONTAL'}
)
if i < len(middle) and i < len(right):
msp.add_line(
start=middle[i],
end=right[i],
dxfattribs={'layer': 'HORIZONTAL'}
)
# 保存DXF文件
output_path = r"D:\曲纹折纸\文献\Codes\Codes\output_scaled.dxf"
doc.saveas(output_path)
print(f"缩放后的DXF文件已生成: {output_path}")
shujuf1:0.000000,0.100000
0.050000,0.100000
0.099999,0.100000
0.149995,0.100002
0.199979,0.100008
0.249939,0.100029
0.299859,0.100079
0.349720,0.100184
0.399494,0.100379
0.449153,0.100715
0.498659,0.101255
0.547972,0.102082
0.597043,0.103296
0.645816,0.105018
0.694224,0.107389
0.742188,0.110572
0.789614,0.114749
0.836387,0.120124
0.882365,0.126915
0.927376,0.135350
0.971205,0.145667
1.013592,0.158094
1.054218,0.172842
1.092701,0.190087
1.128588,0.209945
1.161360,0.232443
1.190435,0.257486
1.215189,0.284819
1.234993,0.313987
1.249269,0.344301
1.257569,0.374819
0.000000,0.000000
0.050000,0.000000
0.100000,0.000000
0.150000,0.000002
0.200000,0.000008
0.250000,0.000029
0.300000,0.000079
0.350000,0.000184
0.399999,0.000381
0.449998,0.000719
0.499995,0.001264
0.549988,0.002102
0.599973,0.003339
0.649942,0.005103
0.699882,0.007549
0.749772,0.010860
0.799579,0.015247
0.849252,0.020955
0.898716,0.028260
0.947860,0.037471
0.996530,0.048927
1.044510,0.062993
1.091509,0.080055
1.137137,0.100503
1.180886,0.124711
1.222107,0.153009
1.259991,0.185640
1.293555,0.222700
1.321641,0.264067
1.342944,0.309302
1.356067,0.357549
0.000000,-0.100000
0.050000,-0.100000
0.100001,-0.100000
0.150005,-0.099998
0.200021,-0.099992
0.250061,-0.099971
0.300140,-0.099921
0.350280,-0.099815
0.400505,-0.099618
0.450844,-0.099278
0.501332,-0.098727
0.552005,-0.097877
0.602903,-0.096618
0.654068,-0.094812
0.705540,-0.092291
0.757356,-0.088852
0.809544,-0.084255
0.862118,-0.078214
0.915066,-0.070394
0.968344,-0.060409
1.021854,-0.047814
1.075428,-0.032107
1.128800,-0.012732
1.181573,0.010918
1.233183,0.039476
1.282854,0.073575
1.329548,0.113794
1.371921,0.160582
1.408290,0.214146
1.436619,0.274302
1.454564,0.340278
shujuf2:0.000000,0.100000
0.050000,0.100000
0.099999,0.100000
0.149995,0.100002
0.199979,0.100008
0.249939,0.100029
0.299859,0.100079
0.349720,0.100184
0.399494,0.100379
0.449153,0.100715
0.498659,0.101255
0.547972,0.102082
0.597043,0.103296
0.645816,0.105018
0.694224,0.107389
0.742188,0.110572
0.789614,0.114749
0.836387,0.120124
0.882365,0.126915
0.927376,0.135350
0.971205,0.145667
1.013592,0.158094
1.054218,0.172842
1.092701,0.190087
1.128588,0.209945
1.161360,0.232443
1.190435,0.257486
1.215189,0.284819
1.234993,0.313987
1.249269,0.344301
1.257569,0.374819
0.000000,0.000000
0.050000,0.000000
0.100000,0.000000
0.150000,0.000002
0.200000,0.000008
0.250000,0.000029
0.300000,0.000079
0.350000,0.000184
0.399999,0.000381
0.449998,0.000719
0.499995,0.001264
0.549988,0.002102
0.599973,0.003339
0.649942,0.005103
0.699882,0.007549
0.749772,0.010860
0.799579,0.015247
0.849252,0.020955
0.898716,0.028260
0.947860,0.037471
0.996530,0.048927
1.044510,0.062993
1.091509,0.080055
1.137137,0.100503
1.180886,0.124711
1.222107,0.153009
1.259991,0.185640
1.293555,0.222700
1.321641,0.264067
1.342944,0.309302
1.356067,0.357549
0.000000,-0.100000
0.050000,-0.100000
0.100001,-0.100000
0.150005,-0.099998
0.200021,-0.099992
0.250061,-0.099971
0.300140,-0.099921
0.350280,-0.099815
0.400505,-0.099618
0.450844,-0.099278
0.501332,-0.098727
0.552005,-0.097877
0.602903,-0.096618
0.654068,-0.094812
0.705540,-0.092291
0.757356,-0.088852
0.809544,-0.084255
0.862118,-0.078214
0.915066,-0.070394
0.968344,-0.060409
1.021854,-0.047814
1.075428,-0.032107
1.128800,-0.012732
1.181573,0.010918
1.233183,0.039476
1.282854,0.073575
1.329548,0.113794
1.371921,0.160582
1.408290,0.214146
1.436619,0.274302
1.454564,0.340278
-0.000000,-0.200000
0.050000,-0.200000
0.100003,-0.200000
0.150016,-0.199998
0.200055,-0.199992
0.250142,-0.199971
0.300311,-0.199920
0.350603,-0.199815
0.401068,-0.199616
0.451765,-0.199274
0.502763,-0.198717
0.554138,-0.197855
0.605974,-0.196571
0.658362,-0.194720
0.711396,-0.192119
0.765169,-0.188547
0.819772,-0.183731
0.875282,-0.177343
0.931753,-0.168992
0.989203,-0.158209
1.047593,-0.144444
1.106798,-0.127059
1.166578,-0.105321
1.226526,-0.078408
1.286019,-0.045426
1.344148,-0.005439
1.399641,0.042471
1.450789,0.099101
1.495372,0.164988
1.530620,0.240188
1.553061,0.323007
0.000000,-0.300000
0.050000,-0.300000
0.100004,-0.300000
0.150021,-0.299998
0.200076,-0.299992
0.250203,-0.299971
0.300452,-0.299920
0.350883,-0.299814
0.401573,-0.299615
0.452612,-0.299270
0.504101,-0.298708
0.556158,-0.297834
0.608910,-0.296528
0.662497,-0.294634
0.717067,-0.291958
0.772772,-0.288257
0.829763,-0.283230
0.888182,-0.276508
0.948149,-0.267639
1.009746,-0.256076
1.072992,-0.241165
1.137808,-0.222130
1.203980,-0.198063
1.271093,-0.167928
1.338468,-0.130568
1.405062,-0.084744
1.469377,-0.029202
1.529337,0.037212
1.582189,0.115361
1.624392,0.205449
1.651744,0.306830
最新发布