MES系统使用C#实现SolidWorks图纸批量转换PDF在线查看

embedded/2024/9/24 11:16:43/

通过 eDrawings API 批量将 SOLIDWORKS 文件导出为 PDF(无需 SOLIDWORKS 软件)。

这个用 C# 开发的控制台应用程序允许通过 SOLIDWORKS eDrawings 的免费版本及其 API 将 SOLIDWORKS、DXF、DWG 文件导出为 PDF。使用此工具无需安装 SOLIDWORKS。

运行工具
此应用程序可以从命令行运行,并需要两个必填参数和一个可选参数,如下所述:
示例命令
exportpdf.exe "C:\SOLIDWORKS Drawings" "test.slddrw""C:\PDFs"

using System;
using System.Collections.Generic;

using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using eDrawings.Interop;
using eDrawings.Interop.EModelViewControl;

namespace ExportPdf
{

    static class Module1
    {

        private static EModelViewControl m_Ctrl;

        private static List<string> m_Files;
        private static string m_OutDir;

        public static void Main()
        {

            try
            {
                ExtractInputParameters();

                var eDrwCtrl = new EDrawingsHost();

                eDrwCtrl.ControlLoaded += OnEdrawingsControlLoaded;

                var winForm = new Form();
                winForm.Controls.Add(eDrwCtrl);
                eDrwCtrl.Dock = DockStyle.Fill;
                winForm.ShowIcon = false;
                winForm.ShowInTaskbar = false;
                winForm.WindowState = FormWindowState.Minimized;
                winForm.ShowDialog();
            }

            catch (Exception ex)
            {
                PrintError(ex.Message);
            }

        }

        private static void ExtractInputParameters()
        {

            string[] args = Environment.GetCommandLineArgs();
            string input = args[1];
            string filter = args[2];
            m_OutDir = "";

            if (args.Length > 3)
            {
                m_OutDir = args[3];
            }

            if (!string.IsNullOrEmpty(m_OutDir))
            {
                if (!Directory.Exists(m_OutDir))
                {
                    Directory.CreateDirectory(m_OutDir);
                }
            }

            if (Directory.Exists(input))
            {
                m_Files = Directory.GetFiles(input, filter, SearchOption.AllDirectories).ToList();
            }
            else if (File.Exists(input))
            {
                m_Files = new List<string>();
                m_Files.Add(input);
            }
            else
            {
                throw new Exception("Specify input file or directory");
            }

        }

        public static void OnEdrawingsControlLoaded(EModelViewControl ctrl)
        {

            Console.WriteLine(string.Format("Starting job. Exporting {0} file(s)", m_Files.Count));

            m_Ctrl = ctrl;

            global::ExportPdf.Module1.m_Ctrl.OnFinishedLoadingDocument += OnDocumentLoaded;
            global::ExportPdf.Module1.m_Ctrl.OnFailedLoadingDocument += OnDocumentLoadFailed;
            global::ExportPdf.Module1.m_Ctrl.OnFinishedPrintingDocument += OnDocumentPrinted;
            global::ExportPdf.Module1.m_Ctrl.OnFailedPrintingDocument += OnPrintFailed;

            PrintNext();

        }

        public static void PrintNext()
        {

            if (m_Files.Any())
            {

                string filePath;
                filePath = m_Files.First();
                m_Files.RemoveAt(0);

                m_Ctrl.CloseActiveDoc("");
                m_Ctrl.OpenDoc(filePath, false, false, false, "");
            }

            else
            {
                Console.WriteLine("Completed");
                Environment.Exit(0);
            }

        }

        public static void OnDocumentLoaded(string fileName)
        {

            MessageBox.Show("3");
            const string PRINTER_NAME = "Microsoft Print to PDF";
            const int AUTO_SOURCE = 7;

            Console.WriteLine(string.Format("Opened {0}", fileName));
            m_Ctrl.SetPageSetupOptions(EMVPrintOrientation.eLandscape, (int)PaperKind.A4, 100, 100, 1, AUTO_SOURCE, PRINTER_NAME, 0, 0, 0, 0);

            string pdfFileName = Path.GetFileNameWithoutExtension(fileName) + ".pdf";
            string outDir;

            if (!string.IsNullOrEmpty(m_OutDir))
            {
                outDir = m_OutDir;
            }
            else
            {
                outDir = Path.GetDirectoryName(fileName);
            }

            string pdfFilePath;
            pdfFilePath = Path.Combine(outDir, pdfFileName);

            Console.WriteLine(string.Format("Exporting {0} to {1}", fileName, pdfFilePath));

            m_Ctrl.Print5(false, fileName, false, false, true, EMVPrintType.eOneToOne, 1, 0, 0, true, 1, 1, pdfFilePath);

        }

        public static void OnDocumentLoadFailed(string fileName, int errorCode, string errorString)
        {
            PrintError(string.Format("Failed to load {0}: {1}", fileName, errorString));
            PrintNext();
        }

        public static void OnDocumentPrinted(string printJobName)
        {
            Console.WriteLine(string.Format("'{0}' export completed", printJobName));
            PrintNext();
        }

        public static void OnPrintFailed(string printJobName)
        {
            PrintError(string.Format("Failed to export '{0}'", printJobName));
            PrintNext();
        }

        public static void PrintError(string msg)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(msg);
            Console.ResetColor();
        }

    }
}

主要代码模块

  1. Main方法:

    • 程序的入口点,捕获可能出现的异常。
    • 创建EDrawingsHost实例eDrwCtrl,并设置其ControlLoaded事件处理程序为OnEdrawingsControlLoaded
    • 创建一个Form,将eDrwCtrl添加到Form的控件中,并设置一些属性后以对话框形式显示。
  2. ExtractInputParameters方法:

    • 从命令行参数中提取输入参数,包括输入文件或文件夹路径、筛选器和可选的输出文件夹路径。
    • 如果输出文件夹不存在,则创建该文件夹。
    • 如果输入是一个目录,则获取该目录下符合筛选条件的所有文件路径存入m_Files;如果输入是一个文件,则将该文件路径存入m_Files
  3. OnEdrawingsControlLoaded方法:

    • 当 eDrawings 控件加载完成时触发。
    • 打印开始任务的信息,并设置m_Ctrl的各种事件处理程序。
    • 调用PrintNext方法开始处理第一个文件。
  4. PrintNext方法:

    • 如果m_Files中有文件,则取出第一个文件路径,关闭当前打开的文档,打开这个文件。
    • 如果m_Files为空,则打印任务完成信息并退出程序。
  5. OnDocumentLoaded方法:

    • 当文档加载成功时触发。
    • 设置页面打印选项,确定输出 PDF 文件的名称和路径。
    • 使用m_Ctrl的打印方法将当前文档打印为 PDF 文件。
  6. OnDocumentLoadFailed方法:

    • 当文档加载失败时触发。
    • 打印错误信息,并调用PrintNext方法继续处理下一个文件。
  7. OnDocumentPrinted方法:

    • 当文档打印成功时触发。
    • 打印文档导出完成的信息,并调用PrintNext方法继续处理下一个文件。
  8. OnPrintFailed方法:

    • 当文档打印失败时触发。
    • 打印错误信息,并调用PrintNext方法继续处理下一个文件。
  9. PrintError方法:

    • 将错误信息以红色字体打印到控制台,然后恢复控制台颜色。

样例代码下载地址:https://download.csdn.net/download/bjhtgy/89789940


http://www.ppmy.cn/embedded/116064.html

相关文章

案例分析-Stream List 中取出值最大的前 5 个和最小的 5 个值

List<Aaa> 中取出 value 最大的前 5 个和最小的 5 个值 要从 List<Aaa> 中取出 value 最大的前 5 个和最小的 5 个值&#xff0c;我们可以使用 Java 8 的流&#xff08;Stream&#xff09;API 来实现。 代码示例&#xff1a; import java.math.BigDecimal; impo…

桶排序和计数排序(非比较排序算法)

桶排序 桶排序是一种基于分配的排序算法&#xff0c;特别适合用来排序均匀分布的数据。它的基本思想是将输入的数据分到有限数量的桶里&#xff0c;然后对每个桶内的数据分别进行排序&#xff0c;最后再将各个桶内的数据合并得到最终的排序结果。(通常用于浮点数&#xff0c;因…

基于vue框架的宠物托管系统设计与实现is203(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。

系统程序文件列表 项目功能&#xff1a;用户,宠物种类,商家,咨询商家,用户宠物,宠物托管,宠物状况,宠物用品,用品分类,商家公告,结束托管,账单信息,延长托管 开题报告内容 基于Vue框架的宠物托管系统设计与实现开题报告 一、引言 随着现代生活节奏的加快&#xff0c;越来越…

3. 什么是连接池?为什么使用数据库连接池?

连接池&#xff08;Connection Pool&#xff09; 是一种数据库连接管理技术&#xff0c;用于在应用程序和数据库之间管理数据库连接。连接池通过预先创建和维护一定数量的数据库连接&#xff0c;将这些连接放入一个“池”中&#xff0c;供应用程序重复使用。这种方法避免了频繁…

uniapp map设置高度为100%后,会拉伸父容器的高度

推荐学习文档 golang应用级os框架&#xff0c;欢迎stargolang应用级os框架使用案例&#xff0c;欢迎star案例&#xff1a;基于golang开发的一款超有个性的旅游计划app经历golang实战大纲golang优秀开发常用开源库汇总想学习更多golang知识&#xff0c;这里有免费的golang学习笔…

数模方法论-无约束问题求解

一、基本概念 无约束问题在数学建模中是指优化过程中没有任何限制条件的情况。这种问题旨在寻找一个决策变量集合&#xff0c;使得某个目标函数&#xff08;如成本、效益或其他需要优化的量&#xff09;达到最大或最小值。具体来说&#xff0c;无约束问题通常可以表示为&#x…

Centos 7 搭建Samba

笔记&#xff1a; 环境&#xff1a;VMware Centos 7&#xff08;网络请选择桥接模式&#xff0c;不要用NAT&#xff09; 遇到一个问题就是yum 安装404&#xff0c;解决办法在下面&#xff08;没有遇到可以无视这句话&#xff09; # 安装Samba软件 yum -y install samba# 创建…

深度学习:(五)初识神经网络

&#xff08;一&#xff09;神经网络的层数 除去输入层&#xff0c;但包括输出层&#xff0c;每一层都有自己的参数。 输入层称为第零层。 &#xff08;二&#xff09;最简单的神经网络&#xff08;逻辑回归&#xff09; 下图中的小圆圈&#xff0c;代表了一种运算。且一个小…