获得solidworks 3d零件的包围框 长宽高 boundingbox c#

csharp 复制代码
using System;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;

namespace tools
{
    /// <summary>
    /// 零件尺寸工具类 - 用于获取零件的长宽高尺寸
    /// </summary>
    public class PartDimensionHelper
    {
        /// <summary>
        /// 获取零件的边界框尺寸(长、宽、高)
        /// </summary>
        /// <param name="partDoc">零件文档对象</param>
        /// <returns>包含长、宽、高的元组 (length, width, height),单位为毫米</returns>
        public static (double length, double width, double height) GetPartDimensions(PartDoc partDoc)
        {
            try
            {
                if (partDoc == null)
                {
                    Console.WriteLine("错误:零件文档为空");
                    return (0, 0, 0);
                }

                // 使用 GetPartBox 方法获取边界框
                // 参数 true 表示返回用户单位(通常是米),false 表示系统单位
                object boxObj = partDoc.GetPartBox(true);
                
                if (boxObj == null)
                {
                    Console.WriteLine("警告:无法获取零件边界框");
                    return (0, 0, 0);
                }

                double[] boxArray = (double[])boxObj;
                
                if (boxArray == null || boxArray.Length < 6)
                {
                    Console.WriteLine("警告:边界框数据格式不正确");
                    return (0, 0, 0);
                }

                // boxArray 包含 [Xmin, Ymin, Zmin, Xmax, Ymax, Zmax]
                // 计算长宽高(单位已经是用户单位,通常是米,需要转换为毫米)
                double length = Math.Abs(boxArray[3] - boxArray[0]) * 1000.0; // X方向
                double width = Math.Abs(boxArray[4] - boxArray[1]) * 1000.0;  // Y方向
                double height = Math.Abs(boxArray[5] - boxArray[2]) * 1000.0; // Z方向

                return (Math.Round(length, 2), Math.Round(width, 2), Math.Round(height, 2));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"获取零件尺寸时出错:{ex.Message}");
                return (0, 0, 0);
            }
        }

        /// <summary>
        /// 根据零件名称获取零件文档并返回其尺寸
        /// </summary>
        /// <param name="swApp">SolidWorks应用程序对象</param>
        /// <param name="partName">零件名称(不含路径和扩展名)</param>
        /// <returns>包含长、宽、高的元组 (length, width, height),单位为毫米</returns>
        public static (double length, double width, double height) GetPartDimensionsByName(SldWorks swApp, string partName)
        {
            try
            {
                if (swApp == null || string.IsNullOrEmpty(partName))
                {
                    Console.WriteLine("错误:参数无效");
                    return (0, 0, 0);
                }

                // 尝试查找已打开的文档
                ModelDoc2 modelDoc = null;
                
                // 遍历所有打开的文档查找匹配的零件
                object[] docs = (object[])swApp.GetDocuments();
                foreach (object docObj in docs)
                {
                    ModelDoc2 doc = (ModelDoc2)docObj;
                    if (doc != null && doc.GetType() == (int)swDocumentTypes_e.swDocPART)
                    {
                        string docTitle = System.IO.Path.GetFileNameWithoutExtension(doc.GetTitle());
                        if (docTitle.Equals(partName, StringComparison.OrdinalIgnoreCase))
                        {
                            modelDoc = doc;
                            break;
                        }
                    }
                }

                // 如果没有找到已打开的文档,尝试从文件系统查找
                if (modelDoc == null)
                {
                    // 这里可以添加从文件系统查找零件的逻辑
                    Console.WriteLine($"警告:未找到已打开的零件 '{partName}'");
                    return (0, 0, 0);
                }

                PartDoc partDoc = (PartDoc)modelDoc;
                return GetPartDimensions(partDoc);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"根据名称获取零件尺寸时出错:{ex.Message}");
                return (0, 0, 0);
            }
        }
    }
}
相关推荐
郝学胜-神的一滴15 分钟前
C++11 工程级应用 09:告别无谓拷贝,解锁高性能移动语义
开发语言·数据结构·c++·vscode·软件工程·visual studio
MC皮蛋侠客1 小时前
OPC UA 系列(一):标准全景与 Python 最小闭环——让第一条设备数据流动起来
开发语言·python·opcua
SamChan902 小时前
PDF翻译后的格式完整性校验:用Python自动比对译文与原文档的表格与段落结构
开发语言·python·ai·pdf·机器翻译
君顾12 小时前
外卖CPS系统开发实战指南:从架构设计到部署全流程解析
java·开发语言·外卖
是立不是利2 小时前
前端交互基石:深入剖析JavaScript三级联动背后的设计哲学
开发语言·前端·javascript
老刘的望远镜3 小时前
AgentScope Java 从零(03):Agent 的记性默认全开,我在第 50 轮翻了车
java·开发语言·javascript
Littlehero_1213 小时前
QT自定义控件之电气接线图(源码开源)
开发语言·qt
中科三方3 小时前
DNS拨测到底在拨测什么?
开发语言·github·php·dns·云拨测
软件黑马王子3 小时前
20.Resources资源加载:基本实现
开发语言·前端框架·c#