c# solidworks 创建装配体工程图+bom

csharp 复制代码
using System;
using System.IO;
using SolidWorks.Interop.sldworks;
using View = SolidWorks.Interop.sldworks.View;

namespace tools
{
    /// <summary>装配体工程图视图创建(斜二测等)。</summary>
    public static class asm_drawing_views
    {
        /// <summary>斜二测(Dimetric / 二等角三角法)视角;依次尝试中文/英文模型视图名。</summary>
        public static View? CreateDimetricView(DrawingDoc drawingDoc, string modelPath, double x, double y)
        {
            string[] viewNames =
            {
                "*斜二测",
                "*斜二测视",
                "*二等角三角法",
                "*Dimetric",
            };

            foreach (string viewName in viewNames)
            {
                View? view = drawingDoc.CreateDrawViewFromModelView3(modelPath, viewName, x, y, 0);
                if (view != null)
                {
                    Console.WriteLine($"已创建斜二测视图({viewName})模型={Path.GetFileName(modelPath)}");
                    return view;
                }
            }

            Console.WriteLine(
                $"警告:斜二测视图创建失败,模型={Path.GetFileName(modelPath)},已尝试:*斜二测 / *斜二测视 / *二等角三角法 / *Dimetric");
            return null;
        }

        /// <summary>1008 装配体工程图主视图:*等轴侧(中文 SW 标准名)。</summary>
        public static View? CreateAssemblyMainView(DrawingDoc drawingDoc, string modelPath, double x, double y)
        {
            string[] viewNames =
            {
                "*等轴侧",
                "*等轴测",
                "*Isometric",
            };

            foreach (string viewName in viewNames)
            {
                View? view = drawingDoc.CreateDrawViewFromModelView3(modelPath, viewName, x, y, 0);
                if (view != null)
                {
                    Console.WriteLine($"已创建装配体主视图({viewName})模型={Path.GetFileName(modelPath)}");
                    return view;
                }
            }

            Console.WriteLine(
                $"错误:装配体主视图创建失败,模型={Path.GetFileName(modelPath)},已尝试:*等轴侧 / *等轴测 / *Isometric");
            return null;
        }

        private const double AssemblyMainViewScaleMultiplier = 1.5;

        /// <summary>将视图比例设为图纸比例的 <see cref="AssemblyMainViewScaleMultiplier"/> 倍(模型显示更大)。</summary>
        public static void ApplyDocumentScaleMultiplier(View view)
        {
            double before = view.ScaleDecimal;
            // ScaleDecimal ≤1 为小数比例(1:5→0.2);>1 为比例分母(1:5→5)
            if (before > 1.0)
            {
                view.ScaleDecimal = before / AssemblyMainViewScaleMultiplier;
            }
            else if (before > 0)
            {
                view.ScaleDecimal = before * AssemblyMainViewScaleMultiplier;
            }

            Console.WriteLine(
                $"视图 {view.Name} 比例 ScaleDecimal: {before} -> {view.ScaleDecimal}(文档比例 {AssemblyMainViewScaleMultiplier} 倍)");
        }
    }
}
相关推荐
hez20101 天前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
雨落倾城夏未凉6 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫7 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫8 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m6259 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户91721561902119 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠9 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫11 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech12 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf13 天前
C#摸鱼实录——IoC与DI案例详解
c#