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;
        }
    }
}
相关推荐
asdzx672 小时前
C#:通过模板快速生成 Word 文档
开发语言·c#·word
游乐码2 小时前
c#StringBuilder
开发语言·c#
CSharp精选营3 小时前
都是微软亲儿子,WPF凭啥干不掉WinForm?这3个场景说明白了
c#·wpf·跨平台·winform
View1213817 小时前
在 .NET 中使用 Moonshot Kimi + AgentFramework:从 SDK 到 Agent 的完整实践
c#·agent·kimi
FlDmr4i2818 小时前
.NET 10 & C# 14 New Features 新增功能介绍-扩展成员Extension Members
开发语言·c#·.net
QJtDK1R5a20 小时前
C# 14 中的新增功能
开发语言·c#
雨浓YN1 天前
WebApi 通讯-DeepSeek API调用文档
c#
yuan199971 天前
C# 断点续传下载文件工具设计与实现
开发语言·c#
雨浓YN1 天前
WebApi 通讯-自写Demo技术文档
c#
唐青枫1 天前
C#.NET TPL Dataflow 深入解析:数据流管道、背压控制与实战取舍
c#·.net