from pyautocad import Autocad, APoint
def create_variable_spaced_lines(start_point, end_point, distances, line_length):
# 连接AutoCAD实例
acad = Autocad(create_if_not_exists=True)
# 创建基线 baseline = acad.model.AddLine(start_point, end_point) # 计算基线向量和长度 base_vector = APoint(end_point.x - start_point.x, end_point.y - start_point.y) base_length = (base_vector.x ** 2 + base_vector.y ** 2) ** 0.5 # 计算单位方向向量 unit_vector = APoint(base_vector.x / base_length, base_vector.y / base_length) # 计算垂直方向向量(逆时针旋转90度) perp_vector = APoint(-unit_vector.y, unit_vector.x) # 处理输入的距离参数 if isinstance(distances, (int, float)): # 等距模式:生成等距点列表 spacing = float(distances) if spacing <= 0: raise ValueError("间距必须大于0") # 生成从0点距开始的等距点 dist_list = [] current = 0.0 while current <= base_length: dist_list.append(current) current += spacing elif isinstance(distances, list): # 不等距模式:直接使用输入的列表 dist_list = distances # 验证是否包含0点距 if 0 not in dist_list: print("警告: 不等距模式应包含0点距作为起点") else: raise TypeError("距离参数应为数字(等距)或列表(不等距)") # 对距离进行排序 sorted_distances = sorted(dist_list) # 创建垂直线和带圈数字标签 for idx, dist in enumerate(sorted_distances): if dist < 0 or dist > base_length: print(f"警告: 距离 {dist} 超出基线范围 (0-{base_length:.2f})") continue # 计算当前点坐标 current_point = APoint( start_point.x + unit_vector.x * dist, start_point.y + unit_vector.y * dist ) # 计算垂直线终点 end_perp = APoint( current_point.x + perp_vector.x * line_length, current_point.y + perp_vector.y * line_length ) # 创建垂直线 acad.model.AddLine(current_point, end_perp) # 计算文字位置(垂直线下方) text_offset = line_length * 0.2 # 文字偏移量 text_position = APoint( end_perp.x + perp_vector.x * text_offset, end_perp.y + perp_vector.y * text_offset ) # 创建带圈数字标签(从0开始) # 定义带圈数字Unicode映射 circled_numbers = { 0: "⓪", 1: "①", 2: "②", 3: "③", 4: "④", 5: "⑤", 6: "⑥", 7: "⑦", 8: "⑧", 9: "⑨", 10: "⑩", 11: "⑪", 12: "⑫", 13: "⑬", 14: "⑭", 15: "⑮", 16: "⑯", 17: "⑰", 18: "⑱", 19: "⑲", 20: "⑳", 21: "㉑", 22: "㉒", 23: "㉓", 24: "㉔", 25: "㉕", 26: "㉖", 27: "㉗", 28: "㉘", 29: "㉙", 30: "㉚", 31: "㉛", 32: "㉜", 33: "㉝", 34: "㉞", 35: "㉟", 36: "㊱", 37: "㊲", 38: "㊳", 39: "㊴", 40: "㊵", 41: "㊶", 42: "㊷", 43: "㊸", 44: "㊹", 45: "㊺", 46: "㊻", 47: "㊼", 48: "㊽", 49: "㊾", 50: "㊿" } # 获取带圈数字或使用普通数字加括号 if idx in circled_numbers: number_label = circled_numbers[idx] else: number_label = f"({idx})" # 创建文字对象(稍小的尺寸) text_height = line_length * 0.1 text = acad.model.AddText(number_label, text_position, text_height) # 调整文字对齐方式(居中) text.Alignment = 10 # acAlignmentCenter text.TextAlignmentPoint = text_position # 确保文字不加粗(使用常规样式) try: # 尝试设置文字样式为"Standard"(常规) text.StyleName = "Standard" except: # 如果失败,尝试设置字体为常规 try: text.FontFile = "txt" # 使用AutoCAD的常规字体 except: pass # 如果所有方法都失败,使用默认样式 print(f"在距离起点 {dist:.2f} 处创建垂直线和标记 {number_label}")
使用示例
if name == “main”:
start = APoint(0, 0) # 基线起点
end = APoint(1200, 0) # 基线终点
line_length = 80 # 垂直线长度
# 示例1: 等距模式 (输入单个距离值)
print(“\n等距模式示例:”)
create_variable_spaced_lines(start, end, 25, line_length)
# 示例2: 不等距模式 (输入包含0点距的列表)
print(“\n不等距模式示例:”)
distances = [655, 675, 700, 730, 760,790,820,850,880,910,940,970,1000,1030] # 包含0点距的不等距列表
create_variable_spaced_lines(start, end, distances, line_length)
D:\Anaconda\python.exe D:\水位检测\正射校正\cad画桩.py
File “D:\水位检测\正射校正\cad画桩.py”, line 129
print(“\n不等距模式示例:”)
^
IndentationError: unindent does not match any outer indentation level