获得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);
            }
        }
    }
}
相关推荐
Scout-leaf1 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6251 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech1 天前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
LDR0062 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术2 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园2 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob2 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享2 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.2 天前
C语言--day30
c语言·开发语言
何以解忧,唯有..2 天前
Go语言循环语句详解:for、range与循环控制
开发语言·算法·golang