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;
        }
    }
}
相关推荐
czhc11400756639 小时前
C# 428 线程、异步
开发语言·c#
唐青枫10 小时前
C#.NET ThreadLocal 深入解析:线程独享数据、性能收益与实战边界
c#·.net
烟话616 小时前
实际内存条,虚拟内存,堆,栈
c#
归途醉染17 小时前
Swifter.Json
c#·json·swifter.json
伽蓝_游戏17 小时前
第一章:解构游戏资源
游戏·unity·性能优化·c#·游戏引擎·游戏程序·assetbundle
星辰徐哥18 小时前
Unity C#入门:Visual Studio与Unity的关联配置
unity·c#·visual studio
星辰_mya19 小时前
分布式系统里的“快递中转站”——消息队列(MQ)
c#·linq
qq_4312807020 小时前
工作经验总结:半导体上位机软件开发与互联网开发的不同
c#·.net
Metaphor69221 小时前
使用 Python 查找并替换 Word 文档中的文本
python·c#·word
chen_22721 小时前
kanzi插件之节点树可视化
c#·kanzi