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;
        }
    }
}
相关推荐
H1785350909619 小时前
SolidWorks_基于草图的实体特征6_边界凸台技巧
3d建模·solidworks
wearegogog12320 小时前
C# .NET 文件比较工具 WinForms
开发语言·c#·.net
糖不吃21 小时前
WPF值转换器
c#
Popeye-lxw1 天前
由罗技 K380 键盘 FN 键模式切换引发的血案
c#
FL16238631291 天前
C# OpenCvSharp 基于霍夫变换直线检测的文本图像倾斜校正文本图像倾斜校
开发语言·c#
aini_lovee1 天前
C# 快递单打印系统(万能套打系统)
开发语言·c#
白菜上路1 天前
C# Serilog.AspNetCore基本使用
c#·serilog
小白不白1111 天前
C# WinForm 与 VP 二次开发
开发语言·c#
SunnyDays10111 天前
如何使用 C# 自动调整 Excel 行高和列宽
开发语言·c#·excel
itgather1 天前
OfficeExcel — Word / Excel DLL 验证台功能介绍
c#·word·excel