NX二次开发 批量导出图纸 合并DWG

该代码示例展示了如何使用NXOpen接口处理工作包文件,实现CAD图纸的自动导出和合并。程序首先读取输入文件,配置转换参数,然后调用UGTO2D和DXFDWG工具将3D模型转换为2DDWG格式,并最终使用AutoCAD进行合并。在过程中,程序会检查和处理可能出现的错误,并清理临时文件。

NXOpen入口

   class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //Console.ReadKey(true);
                Console.BackgroundColor = ConsoleColor.DarkGray;
                Console.ForegroundColor = ConsoleColor.DarkMagenta;
                Console.Clear();
                Console.Title = "导出CAD图纸";

                string filePath = "";
                foreach (string arg in args)
                {
                    filePath = filePath + arg + " ";
                }
                filePath = filePath.Trim();

                CombineSheetManager manager = string.IsNullOrWhiteSpace(filePath)
                    ? new CombineSheetManager()
                    : new CombineSheetManager(filePath);

               

                manager.Commit();
            }
            catch (System.Exception ex)
            {
                if (!string.IsNullOrWhiteSpace(ex.Source))
                {
                    Console.WriteLine(ex.Source);
                }
                Console.WriteLine(ex);
                
            }
            Console.ReadKey();
        }
    }

主要类:导出 合并

    public class CombineSheetManager : BaseCombineManager, IEnumerable<CombineSheet>
    {
        private readonly List<CombineSheet> mList;
        public CombineSheetManager(string filePath)
        {
            var lines = ReadAllLines(filePath).ToList();
            if (lines.Count < 3)
            {
                throw new System.Exception($"工作包文件格式错误。\r\n{filePath}");
            }

            this.BaseDirectory = lines[0];
            this.InputPart = lines[1];
            this.OutputFile = lines[2];

            this.mList = lines.Skip(3).Select(p => new CombineSheet(this, p)).ToList();
            if (this.mList.Count == 0)
            {
                throw new System.Exception($"工作包文件没有指定导出图纸。\r\n{filePath}");
            }
            //赋序号
            int i = 0;
            foreach (var combineSheet in this.mList)
            {
                combineSheet.Id = ++i;
            }
            //查找Root目录
            var rootDir = this.BaseDirectory.GetSubFolder("NXBIN");
            if (rootDir == null || !rootDir.Exists)
            {
                rootDir = this.BaseDirectory.GetSubFolder("UGII");
                if (rootDir == null || !rootDir.Exists)
                {
                    throw new System.Exception($"当前{this.BaseDirectory}程序安装不完整。");
                }
            }
            this.RootDirectory = rootDir;
            //配置表
            var ugTo2dDef = this.CurrentDirectory.GetFile("ugto2d.def");
            this.SettingTable = new SettingTable(ugTo2dDef);
        }

        public CombineSheetManager():this(OpenPackage())
        {
        }

        private static string OpenPackage()
        {
            Console.WriteLine("请输入工作包文件路径:");

           return Console.ReadLine();

            //var openFile = new System.Windows.Forms.OpenFileDialog();
            //openFile.Multiselect = false;
            //openFile.Filter = "Package Files(*.package)|*.package|All Files(*.*)|*.*";
            //openFile.CheckPathExists = true;
            //var result = openFile.ShowDialog();
            //string fileName = "";
            //if (result == System.Windows.Forms.DialogResult.OK)
            //{
            //    fileName = openFile.FileName;
            //}
            //openFile.Dispose();
            //return fileName;
        }

        public FolderPathInfo BaseDirectory { get; }

        public FilePathInfo InputPart { get; set; }

        public FilePathInfo OutputFile { get; set; }

        public FolderPathInfo RootDirectory { get; }

        public SettingTable SettingTable { get; }

        private IEnumerable<string> ReadAllLines(string filePath)
        {
            bool isDeleteFile = true;
            if (strin
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值