UG NX是大型CAD软件,也提供了二维制图功能,当我们想采用外部模式导出标准视图的二维制图时,可以通过UG NX提供的二次开发函数来完成。下面介绍下导出二维制图的二次开发方法。
在VS中根据UG NX的编程模板建立一个工程,命名为ExportDwgFile
点击确定,进入下一个页面。
点击Next,在下面的页面中选择exe
点击Next,进入下一个页面。选择Automatically,when the application completes。
这样一个工程就建好了,如下图所示。
打开UG NX1984,新建一个模型,如下图所示。
在“开发人员”菜单下,选择“录制”。
在文件-》导出-》导出AutoCAD DXF/DWG文件,弹出如下图所示对话框
设置输出的dwg文件名称和地址。
如果有其他设置,自己设置,我这里直接点击完成,就生成了一个dwg文件。
点击“停止录制”。
并将其设置为一个方法,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。
打开cmd,调用程序和输入参数
结果生如下:
说明生成成功。