UG NX二次开发(C++)-建模-将模型中的体导出为iges文件

文章目录

  • 1、前言
  • 2、录制导出iges的代码
  • 3、修改录制文件


1、前言

本文主要讲解下采用录制的方式导出part文件中的体到iges文件中,每个体导出一个相应名称的iges文件。

2、录制导出iges的代码

创建一个part文件,其有两个体,一个是片体,一个是实体,如下图所示:
UG NX二次开发(C++)-建模-将模型中的体导出为iges文件_第1张图片

采用录制功能,保存为C++语言格式,其导出片体的代码如下:

// NX 2008
// Journal created by Administrator on Mon Apr 17 20:54:08 2023 中国标准时间

//
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
// We are currently testing removal of using namespace NXOpen.
// Uncomment the below line if your journal does not compile.
// using namespace NXOpen;

extern "C" DllExport int ufusr_ask_unload()
{
    return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
}

extern "C" DllExport void ufusr(char *param, int *retCode, int paramLen)
{
    NXOpen::Session *theSession = NXOpen::Session::GetSession();
    NXOpen::Part *workPart(theSession->Parts()->Work());
    NXOpen::Part *displayPart(theSession->Parts()->Display());
    // ----------------------------------------------
    //   Menu: 文件(F)->导出(E)->IGES...
    // ----------------------------------------------
    NXOpen::Session::UndoMarkId markId1;
    markId1 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityVisible, NXOpen::NXString("\350\265\267\347\202\271", NXOpen::NXString::UTF8));
    
    NXOpen::IgesCreator *igesCreator1;
    igesCreator1 = theSession->DexManager()->CreateIgesCreator();
    
    igesCreator1->SetExportModelData(true);
    
    igesCreator1->SetExportDrawings(true);
    
    igesCreator1->SetMapTabCylToBSurf(true);
    
    igesCreator1->SetBcurveTol(0.050799999999999998);
    
    igesCreator1->SetIdenticalPointResolution(0.001);
    
    igesCreator1->SetMaxThreeDMdlSpace(10000.0);
    
    igesCreator1->ObjectTypes()->SetCurves(true);
    
    igesCreator1->ObjectTypes()->SetSurfaces(true);
    
    igesCreator1->ObjectTypes()->SetAnnotations(true);
    
    igesCreator1->ObjectTypes()->SetStructures(true);
    
    igesCreator1->ObjectTypes()->SetSolids(true);
    
    igesCreator1->SetMapRevolvedFacesTo(NXOpen::IgesCreator::MapRevolvedFacesOptionBSurfaces);
    
    igesCreator1->SetMapCrossHatchTo(NXOpen::IgesCreator::CrossHatchMapEnumSectionArea);
    
    igesCreator1->SetBcurveTol(0.050799999999999998);
    
    igesCreator1->SetInputFile("D:\\ProjectRelated\\06Code\\01NX\\02CSharp\\NX2007\\NX2007Trainning\\Trainning\\GeneralTrainning\\res\\part\\UIStyler01_BaseUI.prt");
    
    theSession->SetUndoMarkName(markId1, NXOpen::NXString("\345\257\274\345\207\272 IGES \346\226\207\344\273\266 \345\257\271\350\257\235\346\241\206", NXOpen::NXString::UTF8));
    
    igesCreator1->ExportSelectionBlock()->SetSelectionScope(NXOpen::ObjectSelector::ScopeSelectedObjects);
    
    NXOpen::Body *body1(dynamic_cast<NXOpen::Body *>(workPart->Bodies()->FindObject("EXTRUDE(3)")));
    bool added1;
    added1 = igesCreator1->ExportSelectionBlock()->SelectionComp()->Add(body1);
    
    NXOpen::Session::UndoMarkId markId2;
    markId2 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NXOpen::NXString("\345\257\274\345\207\272 IGES \346\226\207\344\273\266", NXOpen::NXString::UTF8));
    
    theSession->DeleteUndoMark(markId2, NULL);
    
    NXOpen::Session::UndoMarkId markId3;
    markId3 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NXOpen::NXString("\345\257\274\345\207\272 IGES \346\226\207\344\273\266", NXOpen::NXString::UTF8));
    
    igesCreator1->SetOutputFile("D:\\UIStyler01_BaseUI.igs");
    
    igesCreator1->SetFileSaveFlag(false);
    
    igesCreator1->SetLayerMask("1-256");
    
    igesCreator1->SetDrawingList("");
    
    igesCreator1->SetViewList("Top,Front,Right,Back,Bottom,Left,Isometric,Trimetric,User Defined");
    
    igesCreator1->SetProcessHoldFlag(true);
    
    NXOpen::NXObject *nXObject1;
    nXObject1 = igesCreator1->Commit();
    
    theSession->DeleteUndoMark(markId3, NULL);
    
    theSession->SetUndoMarkName(markId1, NXOpen::NXString("\345\257\274\345\207\272 IGES \346\226\207\344\273\266", NXOpen::NXString::UTF8));
    
    igesCreator1->Destroy();
    
    // ----------------------------------------------
    //   Menu: 工具(T)->操作记录(J)->停止录制(S)
    // ----------------------------------------------
}

3、修改录制文件

首先,我们的需求是导出每个体,单个体作为一个对象,生成一个iges文件。那么,我们要遍历工作部件下的体对象,即:

	NXOpen::Session* theSession = NXOpen::Session::GetSession();
	NXOpen::Part* workPart(theSession->Parts()->Work());
	NXOpen::Part* displayPart(theSession->Parts()->Display());
	NXOpen::BodyCollection *bodyCollection = workPart->Bodies();

其中BodyCollection是workPart中的所有体的集合。

我们采用循环的方式将单个体输出iges文件,其代码为:

for (NXOpen::BodyCollection::iterator iter = bodyCollection->begin();iter!=bodyCollection->end();  iter++)
	{
		NXOpen::Body* body = *iter;

		NXOpen::IgesCreator* igesCreator1;
		igesCreator1 = theSession->DexManager()->CreateIgesCreator();

		igesCreator1->SetExportModelData(true);

		igesCreator1->SetExportDrawings(true);

		igesCreator1->SetMapTabCylToBSurf(true);

		igesCreator1->SetBcurveTol(0.050799999999999998);

		igesCreator1->SetIdenticalPointResolution(0.001);

		igesCreator1->SetMaxThreeDMdlSpace(10000.0);

		igesCreator1->ObjectTypes()->SetCurves(true);

		igesCreator1->ObjectTypes()->SetSurfaces(true);

		igesCreator1->ObjectTypes()->SetAnnotations(true);

		igesCreator1->ObjectTypes()->SetStructures(true);

		igesCreator1->ObjectTypes()->SetSolids(true);

		igesCreator1->SetMapRevolvedFacesTo(NXOpen::IgesCreator::MapRevolvedFacesOptionBSurfaces);

		igesCreator1->SetMapCrossHatchTo(NXOpen::IgesCreator::CrossHatchMapEnumSectionArea);

		igesCreator1->SetBcurveTol(0.050799999999999998);	

		igesCreator1->SetInputFile(workPart->FullPath());

		theSession->SetUndoMarkName(markId1, NXOpen::NXString("\345\257\274\345\207\272 IGES \346\226\207\344\273\266 \345\257\271\350\257\235\346\241\206", NXOpen::NXString::UTF8));

		igesCreator1->ExportSelectionBlock()->SetSelectionScope(NXOpen::ObjectSelector::ScopeSelectedObjects);

		bool added1;
		added1 = igesCreator1->ExportSelectionBlock()->SelectionComp()->Add(body);		

		NXOpen::Session::UndoMarkId markId2;
		markId2 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NXOpen::NXString("\345\257\274\345\207\272 IGES \346\226\207\344\273\266", NXOpen::NXString::UTF8));

		theSession->DeleteUndoMark(markId2, NULL);

		NXOpen::Session::UndoMarkId markId3;
		markId3 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NXOpen::NXString("\345\257\274\345\207\272 IGES \346\226\207\344\273\266", NXOpen::NXString::UTF8));
		   
		//输出的文件路径
		igesCreator1->SetOutputFile("D:\\" +  body->JournalIdentifier() + ".iges");

		igesCreator1->SetFileSaveFlag(false);

		igesCreator1->SetLayerMask("1-256");

		igesCreator1->SetDrawingList("");

		igesCreator1->SetViewList("Top,Front,Right,Back,Bottom,Left,Isometric,Trimetric,User Defined");

		igesCreator1->SetProcessHoldFlag(true);

		NXOpen::NXObject* nXObject1;
		nXObject1 = igesCreator1->Commit();

		theSession->DeleteUndoMark(markId3, NULL);

		theSession->SetUndoMarkName(markId1, NXOpen::NXString("\345\257\274\345\207\272 IGES \346\226\207\344\273\266", NXOpen::NXString::UTF8));

		igesCreator1->Destroy();
	} 	

当然了,头文件要加上:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include
#include 

4、生成的结果
结果如下图所示:
UG NX二次开发(C++)-建模-将模型中的体导出为iges文件_第2张图片

你可能感兴趣的:(NX二次开发(C++),c++,UG,NX二次开发,UG二次开发)