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;
        }
    }
}
相关推荐
月巴月巴白勺合鸟月半16 小时前
质本洁来还洁去,强于污淖陷文本
c#
Xin_ye1008617 小时前
C# 零基础到精通教程 - 第八章:面向对象编程(进阶)——继承与多态
开发语言·c#
asdzx6719 小时前
使用 C# 打印 Excel 文档(详细教程)
c#·excel
伽蓝_游戏20 小时前
第四章:AssetBundle 核心机制与文件结构
unity·c#·游戏引擎·游戏程序
2501_9307077821 小时前
使用C#代码拆分 PowerPoint 演示文稿
开发语言·c#·powerpoint
SenChien21 小时前
C#学习笔记-入门篇
笔记·学习·c#·rider
诙_21 小时前
由C++速通C#
开发语言·c#
Xin_ye1008621 小时前
C# 零基础到精通教程 - 第九章:面向对象编程(高级)——接口、委托与事件
开发语言·c#
步步为营DotNet21 小时前
深入.NET 11:C# 14 在边缘计算数据处理的优化与实践
c#·.net·边缘计算
weixin_4280053021 小时前
C#调用 AI学习从0开始-第1阶段(基础与工具)-第6天流式输出
开发语言·学习·c#·流式输出stream