获得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);
            }
        }
    }
}
相关推荐
饮水桶_T1 小时前
JAVA IO流笔记
开发语言·数据结构·算法
Felven3 小时前
C. Brr Brrr Patapim
c语言·开发语言
路溪非溪5 小时前
python标准库使用总结
开发语言·python
SomeOtherTime6 小时前
Rust 面向对象介绍(AI回答)
开发语言·python·rust
zh_xuan7 小时前
kotlin常见面试题
开发语言·kotlin·面试题
草莓熊Lotso8 小时前
【C++ 基于微服务的即时通讯系统】文件存储子服务实战:UUID生成、文件IO封装与全接口单元测试
开发语言·网络·数据库·c++·微服务·架构·单元测试
huaqianzkh9 小时前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
沐晓时光20 小时前
C语言入门,内存函数知识讲解
c语言·开发语言
David猪大卫1 天前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
用户3721574261351 天前
C# 实现 Excel 单元格保护:锁定公式并允许编辑指定区域
c#·ai编程·编程语言