获得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);
            }
        }
    }
}
相关推荐
大圣编程2 小时前
Java 多维数组详解
java·开发语言
殳翰5 小时前
下服务器端开发流程及相关工具介绍(C++)
开发语言·c++
落寞的星星5 小时前
这个对象就包含了已经转换好的DFA和各种词法分析器运转所需要的参数。下一步,我们就可以用ScannerInfo对象创建出Scanner对象,请看下面的代码:
开发语言·c#
nothing&nowhere6 小时前
用 Python 做问卷数据清洗:无效样本检测与处理实战
开发语言·python·数据清洗·数据处理·问卷星·问卷星脚本·刷问卷
2601_961593426 小时前
Rust 开发环境配置繁琐?RustRover 开箱即用搞定编码调试
开发语言·后端·macos·rust
HZZD_HZZD7 小时前
DL/T 645-2026新国标深度解读与智能电表协议适配实战:从帧结构变化到Java采集器升级的全链路改造方案
java·开发语言
多加点辣也没关系9 小时前
JavaScript|第4章:类型转换
开发语言·javascript
聪慧的水蜜桃9 小时前
【YFIOs】用C#开发硬件之设备上云
开发语言·c#
yqcoder9 小时前
httpOnly 是什么,又有什么用?
开发语言·前端·javascript
wuqingshun31415910 小时前
重写equals而不重写hashCode,会出什么问题?
java·开发语言