solidworks 导出dwg 带映射模板 c# 图纸比例一比一导出

https://help.solidworks.com/2023/english/api/swconst/filesaveasdxfoptions.htm

drw2dwg耗时: 618 ms

map文件:

复制代码
[Version]
SW990001

[Layers]
0 = 7, 0
注释 = 250, 0
尺寸 = 3, 6

[Entities]
草图直线 [16] = BYLAYER, BYLAYER, 0
尺寸 [1] = 3, BYLAYER, 尺寸
注释 [2] = BYLAYER, BYLAYER, 注释

[Colors]
0 -> 0
0 -> 0
0 -> 0
0 -> 0
0 -> 0
0 -> 0
0 -> 0
0 -> 0
16776960 -> 4
65535 -> 2
65280 -> 3
16776960 -> 4
8372223 -> 0
cs 复制代码
using System;
using System.IO;
using System.Runtime.InteropServices;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;

namespace tools
{
    public class drw2dwg
    {
        static public string run(ModelDoc2 swModel, SldWorks swApp )
        {
            
            string fullpath = swModel.GetPathName();
           
            string? directory = Path.GetDirectoryName(fullpath);
            if (string.IsNullOrEmpty(directory))
            {
                Console.WriteLine("错误:无法获取文件所在目录。");
                
            }
            
            string outputfile = directory + "\\" + "出图" + "\\" + "工程图";
            if (!Directory.Exists(outputfile))
            {
                Directory.CreateDirectory(outputfile);
            }
            string dwgFileName = directory + "\\" + "出图" + "\\" +  "工程图" + "\\" + Path.GetFileNameWithoutExtension(fullpath) + ".dwg";
            swApp.SetUserPreferenceIntegerValue((int)swUserPreferenceIntegerValue_e.swDxfOutputNoScale, 1);
            // 设置自定义映射文件
            if (swApp != null)
            {
                string pluginDir = Path.GetDirectoryName(typeof(drw2dwg).Assembly.Location);
              
                string mapFilePath = Path.Combine(pluginDir, "dwgmaping");
              
                if (File.Exists(mapFilePath))
                {
                    swApp.SetUserPreferenceStringListValue(
                        (int)swUserPreferenceStringListValue_e.swDxfMappingFiles, 
                        mapFilePath);
                    
                    int index = swApp.GetUserPreferenceIntegerValue(
                        (int)swUserPreferenceIntegerValue_e.swDxfMappingFileIndex);
                    
                    if (index == -1)
                    {
                        var set_result=swApp.SetUserPreferenceIntegerValue(
                            (int)swUserPreferenceIntegerValue_e.swDxfMappingFileIndex, 
                            0);
                    }
                }
                else
                {
                    Console.WriteLine($"错误:无法找到自定义映射文件。{mapFilePath}");

                }
            }
            
            int errors=0, warnings=0;
            var result = swModel.SaveAs4(
                dwgFileName, 
                (int)swSaveAsVersion_e.swSaveAsCurrentVersion, 
                (int)swSaveAsOptions_e.swSaveAsOptions_Silent, 
               ref errors, 
                ref warnings);
                
            Console.WriteLine($"{result},已创建工程图{dwgFileName}");
            return dwgFileName;
        }
    }
}
相关推荐
人工智能AI技术1 小时前
美团“问小团”同款架构:C# + ASP.NET Core 搭建本地生活 AI 搜索
人工智能·c#
!chen3 小时前
C# + ViewFaceCore 快速实现高精度人脸识别
开发语言·c#
她说彩礼65万3 小时前
WPF DataContext详解及用例
c#
她说彩礼65万3 小时前
WPF Dispatcher和DispatcherObject
c#
“抚琴”的人4 小时前
SqlSugar 文档
开发语言·数据库·c#·sqlsugar
人工智能AI技术4 小时前
从0到1:C# 调用 Claude 插件打通 Excel 与 PowerPoint 工作流
人工智能·c#
猹叉叉(学习版)5 小时前
【ASP.NET CORE】 10. 数据校验
笔记·后端·c#·asp.net·.netcore
人工智能AI技术5 小时前
C# 接入 Grok4.20 实战:在 .NET 8 中打造高可靠 AI 搜索服务
人工智能·c#
人工智能AI技术5 小时前
C# 版 WorldSim 客户端:在 Unity 中连接 OpenAI 世界模拟器训练机器人
人工智能·c#