pyautocad获得所选圆弧的弧长总和

python 复制代码
# total_arc_length_calculator.py
from pyautocad import Autocad, APoint
import math
import time

def get_selected_arcs(acad, doc):
    """
    获取用户选择的圆弧对象
    
    Args:
        acad: AutoCAD应用程序对象
        doc: AutoCAD文档对象
    
    Returns:
        list: 选中的圆弧对象列表
    """
    try:
        # 提示用户选择对象
        print("请选择圆弧对象...")
        
        # 使用时间戳创建唯一的选择集名称
        selection_set_name = f"ArcSelection_{int(time.time())}"
        selection_set = doc.SelectionSets.Add(selection_set_name)
        selection_set.SelectOnScreen()
        
        # 筛选出圆弧对象
        arcs = []
        if selection_set.Count > 0:
            for i in range(selection_set.Count):
                try:
                    entity = selection_set.Item(i)
                    if entity.ObjectName == "AcDbArc":
                        arcs.append(entity)
                except Exception as e:
                    print(f"检查对象时出错: {e}")
                    continue
            
            print(f"共选择 {selection_set.Count} 个对象,其中 {len(arcs)} 个为圆弧")
        else:
            print("未选择任何对象")
        
        # 清理选择集
        selection_set.Delete()
        
        return arcs
        
    except Exception as e:
        print(f"选择对象时出错: {e}")
        return []

def calculate_arc_length(arc):
    """
    计算单个圆弧的弧长
    
    Args:
        arc: AutoCAD Arc对象
    
    Returns:
        float: 圆弧的弧长
    """
    try:
        # 获取基本属性
        radius = arc.Radius
        start_angle = arc.StartAngle
        end_angle = arc.EndAngle
        
        # 计算弧长
        if end_angle >= start_angle:
            angle_diff = end_angle - start_angle
        else:
            angle_diff = (2 * math.pi - start_angle) + end_angle
            
        arc_length = radius * angle_diff
        return arc_length
    except Exception as e:
        print(f"计算圆弧弧长时出错: {e}")
        return 0

def calculate_total_arc_length(arcs):
    """
    计算所有圆弧的弧长总和
    
    Args:
        arcs: 圆弧对象列表
    
    Returns:
        float: 所有圆弧的弧长总和
    """
    total_length = 0
    for i, arc in enumerate(arcs, 1):
        length = calculate_arc_length(arc)
        total_length += length
        print(f"第 {i} 个圆弧弧长: {length:.2f}")
    
    return total_length

def main():
    """
    主函数 - 计算选中圆弧的弧长总和
    """
    # 连接到正在运行的 AutoCAD
    try:
        acad = Autocad(create_if_not_exists=True)
        doc = acad.doc
        print(f"成功连接到 AutoCAD 文档: {doc.Name}")
    except Exception as e:
        print("无法连接到 AutoCAD:", e)
        return

    try:
        # 获取用户选择的圆弧
        selected_arcs = get_selected_arcs(acad, doc)
        
        if not selected_arcs:
            print("没有选择有效的圆弧对象")
            return
        
        # 计算所有圆弧的弧长总和
        total_length = calculate_total_arc_length(selected_arcs)
        
        print(f"\n总共选择了 {len(selected_arcs)} 个圆弧")
        print(f"所有圆弧的弧长总和为: {total_length:.2f}")
        
    except Exception as e:
        print(f"处理过程中发生错误: {e}")

if __name__ == "__main__":
    main()
相关推荐
高级程序源4 小时前
django校园寻物通失物招领平台32261-计算机课程设计、毕业设计
vue.js·python·mysql·小程序·django·flask·课程设计
WeiXin_DZbishe4 小时前
基于springboot宠物领养系统-计算机毕设 附源码【课程设计】94217
vue.js·spring boot·python·django·php·课程设计
kimnoic5 小时前
Python数据库学习心得:SQLite、MySQL、PostgreSQL优缺点
开发语言·python
imperialeast5 小时前
WinAXP 2.4.0.828正式版
windows
snhna5 小时前
向量数据库深度实战:从 HNSW 到 pgvector、Milvus 选型与 RAG 落地
数据库·人工智能·python·postgresql·milvus
必须得开心呀6 小时前
python编写带界面的软件,需要安装哪些插件
开发语言·python
Wx-bishekaifayuan6 小时前
express皖美特色农产品网售系统53118-计算机课程设计、毕业设计
人工智能·spring boot·后端·python·课程设计·express·旅游
青少儿编程课堂6 小时前
迪杰斯特拉(Dijkstra)算法:单源最短路与赛场路线规划
c++·python·算法·bfs·信息学竞赛
计算机编程指导师6 小时前
【计算机毕设推荐】基于Hadoop+Spark的公共交通运营数据分析与可视化系统源码 毕业设计 选题推荐 毕设选题 数据分析 机器学习
大数据·hadoop·python·计算机·数据分析·spark·交通
Wx-bishekaifayuan6 小时前
springboot汉中旅游服务平台41442-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游