solidworks导出展开 c# ExportFlatPatternView方法

好像没有工程图的时候是没法用这个的

这个方法不需要doc.Visible也能展开

cs 复制代码
using System;
using System.IO;
using System.Runtime.InteropServices;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Text;
namespace tools
{
    public class Exportdxf_flatview
    {

        static public string run(ModelDoc2 swModel, string thickness)
        {
            try
            {
                Console.OutputEncoding = Encoding.UTF8;
                // 后续逻辑不变...
        

                if (swModel == null || swModel.GetType() != (int)swDocumentTypes_e.swDocPART)
                {
                    Console.WriteLine("错误:请打开一个 SolidWorks 零件文档 (.sldprt)。");
                    return "";
                }

                string fullPath = swModel.GetPathName();

                if (string.IsNullOrEmpty(fullPath))
                {
                    Console.WriteLine("错误:文档尚未保存,请先保存文件。");
                    return "";
                }
                string? directory = Path.GetDirectoryName(fullPath);
                if (string.IsNullOrEmpty(directory))
                {
                    Console.WriteLine("错误:无法获取文件所在目录。");
                    return "";
                }
                PartDoc swPart = (PartDoc)swModel;
                string outputfile = directory + "\\" + "下料" + "\\" + thickness;
                if (!Directory.Exists(outputfile))
                {
                    Directory.CreateDirectory(outputfile);
                }
                string dxfFileName = directory + "\\" + "下料" + "\\" + thickness + "\\" + Path.GetFileNameWithoutExtension(fullPath) + ".dwg";
  
               var result=swPart.ExportFlatPatternView(dxfFileName,0);



                Console.WriteLine($"{result}!生成文档保存在:{dxfFileName}");
                return dxfFileName;

            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误: {ex.Message}");
                Console.WriteLine("提示:请确保 SolidWorks 正在运行。");

            }
            return "";


        }


    }
}
相关推荐
小码编匠14 小时前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫3 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech3 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf5 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6255 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech5 天前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
2601_962072556 天前
李梦娇常识4600问|题库|打印版
sql·华为od·华为·c#·华为云·.net·harmonyos
m0_547486666 天前
《C#语言程序设计与实践》 全套PPT课件
c语言·c#·c语言程序设计
叶帆6 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
IT方大同6 天前
(嵌入式操作系统)信号量
嵌入式硬件·c#