当前位置:网站首页>UG NX二次开发(C#)-外部模式-导出dwg格式的文件
UG NX二次开发(C#)-外部模式-导出dwg格式的文件
2022-08-02 07:17:00 【GimiGimmy】
前言
UG NX是大型CAD软件,也提供了二维制图功能,当我们想采用外部模式导出标准视图的二维制图时,可以通过UG NX提供的二次开发函数来完成。下面介绍下导出二维制图的二次开发方法。
建立一个新的工程
在VS中根据UG NX的编程模板建立一个工程,命名为ExportDwgFile
点击确定,进入下一个页面。
点击Next,在下面的页面中选择exe
点击Next,进入下一个页面。选择Automatically,when the application completes。
这样一个工程就建好了,如下图所示。
UG NX录制代码
打开UG NX1984,新建一个模型,如下图所示。
在“开发人员”菜单下,选择“录制”。
在文件-》导出-》导出AutoCAD DXF/DWG文件,弹出如下图所示对话框
设置输出的dwg文件名称和地址。
如果有其他设置,自己设置,我这里直接点击完成,就生成了一个dwg文件。
点击“停止录制”。
将导出dwg格式文件的录制代码复制到cs文件中

并将其设置为一个方法,ExportDwgFunction。
public static void ExportDwgFunction()
{
NXOpen.Session theSession = NXOpen.Session.GetSession();
NXOpen.Part workPart = theSession.Parts.Work;
NXOpen.Part displayPart = theSession.Parts.Display;
// ----------------------------------------------
// 菜单:文件(F)->导出(E)->AutoCAD DXF/DWG...
// ----------------------------------------------
NXOpen.Session.UndoMarkId markId1;
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "起点");
NXOpen.DxfdwgCreator dxfdwgCreator1;
dxfdwgCreator1 = theSession.DexManager.CreateDxfdwgCreator();
dxfdwgCreator1.ExportData = NXOpen.DxfdwgCreator.ExportDataOption.Drawing;
dxfdwgCreator1.AutoCADRevision = NXOpen.DxfdwgCreator.AutoCADRevisionOptions.R2004;
dxfdwgCreator1.ViewEditMode = true;
dxfdwgCreator1.FlattenAssembly = true;
dxfdwgCreator1.ExportScaleValue = 1.0;
dxfdwgCreator1.FlattenAssembly = false;
dxfdwgCreator1.OutputFileType = NXOpen.DxfdwgCreator.OutputFileTypeOption.Dwg;
dxfdwgCreator1.InputFile = "C:\\temp\\_model1.prt";
dxfdwgCreator1.ExportData = NXOpen.DxfdwgCreator.ExportDataOption.Modeling;
theSession.SetUndoMarkName(markId1, "导出 AutoCAD DXF/DWG 文件 对话框");
NXOpen.Session.UndoMarkId markId2;
markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "导出 AutoCAD DXF/DWG 文件");
theSession.DeleteUndoMark(markId2, null);
NXOpen.Session.UndoMarkId markId3;
markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "导出 AutoCAD DXF/DWG 文件");
dxfdwgCreator1.OutputFile = "D:\\_model1.dwg";
dxfdwgCreator1.WidthFactorMode = NXOpen.DxfdwgCreator.WidthfactorMethodOptions.AutomaticCalculation;
dxfdwgCreator1.LayerMask = "1-256";
dxfdwgCreator1.DrawingList = "_ALL_";
dxfdwgCreator1.ViewList = "Trimetric";
dxfdwgCreator1.ProcessHoldFlag = true;
NXOpen.NXObject nXObject1;
nXObject1 = dxfdwgCreator1.Commit();
theSession.DeleteUndoMark(markId3, null);
dxfdwgCreator1.Destroy();
// ----------------------------------------------
// 菜单:工具(T)->操作记录(J)->停止录制(S)
// ----------------------------------------------
}
修改录制代码
在这里,代码是不能直接用的,需要修改代码。
因为是一个方法,其实带参数的,我们设置为两个参数:
1)工作部件workpart的文件字符串
2)导出的文件字符串
即:
public static void ExportDwgFunction(string partFile, string dwgFile)
{
在代码中,修改如下:
public static void ExportDwgFunction(string partFile, string dwgFile)
{
NXOpen.Session theSession = NXOpen.Session.GetSession();
NXOpen.Part workPart = theSession.Parts.Work;
NXOpen.Part displayPart = theSession.Parts.Display;
// ----------------------------------------------
// 菜单:文件(F)->导出(E)->AutoCAD DXF/DWG...
// ----------------------------------------------
NXOpen.Session.UndoMarkId markId1;
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "起点");
NXOpen.DxfdwgCreator dxfdwgCreator1;
dxfdwgCreator1 = theSession.DexManager.CreateDxfdwgCreator();
dxfdwgCreator1.ExportData = NXOpen.DxfdwgCreator.ExportDataOption.Drawing;
dxfdwgCreator1.AutoCADRevision = NXOpen.DxfdwgCreator.AutoCADRevisionOptions.R2004;
dxfdwgCreator1.ViewEditMode = true;
dxfdwgCreator1.FlattenAssembly = true;
dxfdwgCreator1.ExportScaleValue = 1.0;
dxfdwgCreator1.FlattenAssembly = false;
dxfdwgCreator1.OutputFileType = NXOpen.DxfdwgCreator.OutputFileTypeOption.Dwg;
dxfdwgCreator1.InputFile = partFile;
//dxfdwgCreator1.InputFile = "C:\\temp\\_model1.prt";
dxfdwgCreator1.ExportData = NXOpen.DxfdwgCreator.ExportDataOption.Modeling;
theSession.SetUndoMarkName(markId1, "导出 AutoCAD DXF/DWG 文件 对话框");
NXOpen.Session.UndoMarkId markId2;
markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "导出 AutoCAD DXF/DWG 文件");
theSession.DeleteUndoMark(markId2, null);
NXOpen.Session.UndoMarkId markId3;
markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "导出 AutoCAD DXF/DWG 文件");
dxfdwgCreator1.OutputFile = dwgFile;
//dxfdwgCreator1.OutputFile = "D:\\_model1.dwg";
dxfdwgCreator1.WidthFactorMode = NXOpen.DxfdwgCreator.WidthfactorMethodOptions.AutomaticCalculation;
dxfdwgCreator1.LayerMask = "1-256";
dxfdwgCreator1.DrawingList = "_ALL_";
dxfdwgCreator1.ViewList = "Trimetric";
dxfdwgCreator1.ProcessHoldFlag = true;
NXOpen.NXObject nXObject1;
nXObject1 = dxfdwgCreator1.Commit();
theSession.DeleteUndoMark(markId3, null);
dxfdwgCreator1.Destroy();
// ----------------------------------------------
// 菜单:工具(T)->操作记录(J)->停止录制(S)
// ----------------------------------------------
}
这样就完成了导出dwg文件方法的编写
调用方法
在 public static int Main(string[] args)中添加如下代码
public static int Main(string[] args)
{
int retValue = 0;
try
{
theProgram = new Program();
//TODO: Add your application code here
string partFile = args[0];
string dwgFile = args[1];
Tag workPartTag;
UFPart.LoadStatus loadStatus;
theUFSession.Part.Open(partFile,out workPartTag,out loadStatus);
Part workPart = theSession.Parts.Work;
ExportDwgFunction(partFile,dwgFile);
theUFSession.Part.CloseAll();
theProgram.Dispose();
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----
}
return retValue;
}
生成exe
点击生成,生成exe。
测试
打开cmd,调用程序和输入参数
结果生如下:

说明生成成功。
边栏推荐
- Introduction to Totem Pole and Push-Pull Circuits
- (2022牛客多校五)C-Bit Transmission(思维)
- OC-范畴
- PLSQL Developer安装和配置
- Splunk Field Caculated 计算字段
- 【CV】OpenVINO安装教程
- gdalinfo: error while loading shared libraries: libgdal.so.30: cannot open shared object file: No su
- 主流定时任务解决方案全横评
- 责任链模式(Chain Of Responsibility)
- LeetCode 2312. 卖木头块
猜你喜欢

Install Metasploitable2 on VMware

Aided by training and learning by battle | The new version of the Offensive and Defense World Platform is officially launched!

MySQL-FlinkCDC-Hudi enters the lake in real time

spark架构

【CV】OpenVINO安装教程

LeetCode 2360. 图中的最长环

2022年防止网络攻击的15个网络安全实践,你学会了吗?

MySQL - slow query log

Xilinx Constraint Study Notes - Timing Constraints

主流定时任务解决方案全横评
随机推荐
MySQL-Multiversion Concurrency Control
MySQL-Execution Process + Cache + Storage Engine
Debian 10 dhcp relay (dhcp 中继) dhcp 固定分配
WebForm DropDownList分别绑定年月
MySQL-锁机制
Splunk Field Caculated Calculated Field
OC-NSString
pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies
From cloud computing to function computing
Please tell me, how to write Flink SQL and JDBC sink into mysql library and want to create an auto-incrementing primary key
OC-NSNumber and NSValue are generally used for boxing and unboxing
CSRF-跨站请求伪造-相关知识
PLSQL Developer安装和配置
带手续费买卖股票的最大利益[找DP的状态定义到底缺什么?]
概率论与数理统计
Understand Chisel language. 31. Chisel advanced communication state machine (3) - Ready-Valid interface: definition, timing and implementation in Chisel
OC-NSNumber和NSValue一般用来装箱拆箱
(2022 Nioke Duo School 5) C-Bit Transmission (Thinking)
Agile, DevOps and Embedded Systems Testing
2022年防止网络攻击的15个网络安全实践,你学会了吗?