NXOpen 属性工具(工作部件和实体加属性二合为一)

C++

//HPP文件

//==============================================================================

#ifndef ATTRIBUTE_TEST_H_INCLUDED
#define ATTRIBUTE_TEST_H_INCLUDED

//------------------------------------------------------------------------------
//These includes are needed for the following template code
//------------------------------------------------------------------------------
#include <uf_defs.h>
#include <uf_ui_types.h>
#include <iostream>
#include <NXOpen/Session.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/Callback.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/BlockStyler_UIBlock.hxx>
#include <NXOpen/BlockStyler_BlockDialog.hxx>
#include <NXOpen/BlockStyler_PropertyList.hxx>
#include <NXOpen/BlockStyler_Group.hxx>
#include <NXOpen/BlockStyler_Enumeration.hxx>
#include <NXOpen/BlockStyler_SelectObject.hxx>
#include <NXOpen/BlockStyler_StringBlock.hxx>

//用户头文件
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/AttributeManager.hxx>
#include <NXOpen/AttributePropertiesBaseBuilder.hxx>
#include <NXOpen/AttributePropertiesBuilder.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/NXObjectManager.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/BodyCollection.hxx>
#include <NXOpen/BodyDumbRule.hxx>
#include <uf_ui.h>
#include <uf.h>

//------------------------------------------------------------------------------
//Bit Option for Property: SnapPointTypesEnabled
//------------------------------------------------------------------------------
#define SnapPointTypesEnabled_UserDefined (1 << 0);
#define SnapPointTypesEnabled_Inferred (1 << 1);
#define SnapPointTypesEnabled_ScreenPosition (1 << 2);
#define SnapPointTypesEnabled_EndPoint (1 << 3);
#define SnapPointTypesEnabled_MidPoint (1 << 4);
#define SnapPointTypesEnabled_ControlPoint (1 << 5);
#define SnapPointTypesEnabled_Intersection (1 << 6);
#define SnapPointTypesEnabled_ArcCenter (1 << 7);
#define SnapPointTypesEnabled_QuadrantPoint (1 << 8);
#define SnapPointTypesEnabled_ExistingPoint (1 << 9);
#define SnapPointTypesEnabled_PointonCurve (1 <<10);
#define SnapPointTypesEnabled_PointonSurface (1 <<11);
#define SnapPointTypesEnabled_PointConstructor (1 <<12);
#define SnapPointTypesEnabled_TwocurveIntersection (1 <<13);
#define SnapPointTypesEnabled_TangentPoint (1 <<14);
#define SnapPointTypesEnabled_Poles (1 <<15);
#define SnapPointTypesEnabled_BoundedGridPoint (1 <<16);
//------------------------------------------------------------------------------
//Bit Option for Property: SnapPointTypesOnByDefault
//------------------------------------------------------------------------------
#define SnapPointTypesOnByDefault_EndPoint (1 << 3);
#define SnapPointTypesOnByDefault_MidPoint (1 << 4);
#define SnapPointTypesOnByDefault_ControlPoint (1 << 5);
#define SnapPointTypesOnByDefault_Intersection (1 << 6);
#define SnapPointTypesOnByDefault_ArcCenter (1 << 7);
#define SnapPointTypesOnByDefault_QuadrantPoint (1 << 8);
#define SnapPointTypesOnByDefault_ExistingPoint (1 << 9);
#define SnapPointTypesOnByDefault_PointonCurve (1 <<10);
#define SnapPointTypesOnByDefault_PointonSurface (1 <<11);
#define SnapPointTypesOnByDefault_PointConstructor (1 <<12);
#define SnapPointTypesOnByDefault_BoundedGridPoint (1 <<16);
//------------------------------------------------------------------------------
// Namespaces needed for following template
//------------------------------------------------------------------------------
using namespace std;
using namespace NXOpen;
using namespace NXOpen::BlockStyler;

class DllExport Attribute_test
{
// class members
public:
static Session *theSession;
static UI *theUI;
Attribute_test();
~Attribute_test();
int Show();

//----------------------- BlockStyler Callback Prototypes ---------------------
// The following member function prototypes define the callbacks
// specified in your BlockStyler dialog. The empty implementation
// of these prototypes is provided in the Attribute_test.cpp file.
// You are REQUIRED to write the implementation for these functions.
//------------------------------------------------------------------------------
void initialize_cb();
void dialogShown_cb();
int apply_cb();
int ok_cb();
int update_cb(NXOpen::BlockStyler::UIBlock* block);
PropertyList* GetBlockProperties(const char *blockID);

//用户代码
void workPartSetAttr(); //定义工作部件创建属性函数
void bodySetAttr(); //定义实体创建属性函数
void nowdate(); //自动获取日期

private:
const char* theDlxFileName;
NXOpen::BlockStyler::BlockDialog* theDialog;
NXOpen::BlockStyler::Group* group01;// Block type: Group
NXOpen::BlockStyler::Enumeration* enum0;// Block type: Enumeration
NXOpen::BlockStyler::Group* group02;// Block type: Group
NXOpen::BlockStyler::SelectObject* selection0;// Block type: Selection
NXOpen::BlockStyler::Group* group03;// Block type: Group
NXOpen::BlockStyler::StringBlock* stringPartName;// Block type: String
NXOpen::BlockStyler::StringBlock* stringDesigner;// Block type: String
NXOpen::BlockStyler::StringBlock* stringDate;// Block type: String
NXOpen::BlockStyler::StringBlock* stringContact;// Block type: String

};
#endif //ATTRIBUTE_TEST_H_INCLUDED

//CPP文件

//------------------------------------------------------------------------------
//These includes are needed for the following template code
//------------------------------------------------------------------------------
#include "Attribute_test.hpp"
using namespace NXOpen;
using namespace NXOpen::BlockStyler;

//------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
Session *(Attribute_test::theSession) = NULL;
UI *(Attribute_test::theUI) = NULL;
//------------------------------------------------------------------------------
// Constructor for NX Styler class
//------------------------------------------------------------------------------
Attribute_test::Attribute_test()
{
try
{
// Initialize the NX Open C++ API environment
Attribute_test::theSession = NXOpen::Session::GetSession();
Attribute_test::theUI = UI::GetUI();
theDlxFileName = "Attribute_test.dlx";
theDialog = Attribute_test::theUI->CreateDialog(theDlxFileName);
// Registration of callback functions
theDialog->AddApplyHandler(make_callback(this, &Attribute_test::apply_cb));
theDialog->AddOkHandler(make_callback(this, &Attribute_test::ok_cb));
theDialog->AddUpdateHandler(make_callback(this, &Attribute_test::update_cb));
theDialog->AddInitializeHandler(make_callback(this, &Attribute_test::initialize_cb));
theDialog->AddDialogShownHandler(make_callback(this, &Attribute_test::dialogShown_cb));
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
throw;
}
}

//------------------------------------------------------------------------------
// Destructor for NX Styler class
//------------------------------------------------------------------------------
Attribute_test::~Attribute_test()
{
if (theDialog != NULL)
{
delete theDialog;
theDialog = NULL;
}
}
//------------------------------- DIALOG LAUNCHING ---------------------------------
//
// Before invoking this application one needs to open any part/empty part in NX
// because of the behavior of the blocks.
//
// Make sure the dlx file is in one of the following locations:
// 1.) From where NX session is launched
// 2.) $UGII_USER_DIR/application
// 3.) For released applications, using UGII_CUSTOM_DIRECTORY_FILE is highly
// recommended. This variable is set to a full directory path to a file
// containing a list of root directories for all custom applications.
// e.g., UGII_CUSTOM_DIRECTORY_FILE=$UGII_ROOT_DIRmenuscustom_dirs.dat
//
// You can create the dialog using one of the following way:
//
// 1. USER EXIT
//
// 1) Create the Shared Library -- Refer "Block UI Styler programmer's guide"
// 2) Invoke the Shared Library through File->Execute->NX Open menu.
//
//------------------------------------------------------------------------------
extern "C" DllExport void ufusr(char *param, int *retcod, int param_len)
{
Attribute_test *theAttribute_test = NULL;
try
{
theAttribute_test = new Attribute_test();
// The following method shows the dialog immediately
theAttribute_test->Show();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
Attribute_test::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
if(theAttribute_test != NULL)
{
delete theAttribute_test;
theAttribute_test = NULL;
}
}

//------------------------------------------------------------------------------
// This method specifies how a shared image is unloaded from memory
// within NX. This method gives you the capability to unload an
// internal NX Open application or user exit from NX. Specify any
// one of the three constants as a return value to determine the type
// of unload to perform:
//
//
// Immediately : unload the library as soon as the automation program has completed
// Explicitly : unload the library from the "Unload Shared Image" dialog
// AtTermination : unload the library when the NX session terminates
//
//
// NOTE: A program which associates NX Open applications with the menubar
// MUST NOT use this option since it will UNLOAD your NX Open application image
// from the menubar.
//------------------------------------------------------------------------------
extern "C" DllExport int ufusr_ask_unload()
{
//return (int)Session::LibraryUnloadOptionExplicitly;
return (int)Session::LibraryUnloadOptionImmediately;
//return (int)Session::LibraryUnloadOptionAtTermination;
}

//------------------------------------------------------------------------------
// Following method cleanup any housekeeping chores that may be needed.
// This method is automatically called by NX.
//------------------------------------------------------------------------------
extern "C" DllExport void ufusr_cleanup(void)
{
try
{
//---- Enter your callback code here -----
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
Attribute_test::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

int Attribute_test::Show()
{
try
{
theDialog->Show();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
Attribute_test::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return 0;
}

//------------------------------------------------------------------------------
//---------------------Block UI Styler Callback Functions--------------------------
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//Callback Name: initialize_cb
//------------------------------------------------------------------------------
void Attribute_test::initialize_cb()
{
try
{
group01 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group01"));
enum0 = dynamic_cast<NXOpen::BlockStyler::Enumeration*>(theDialog->TopBlock()->FindBlock("enum0"));
group02 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group02"));
selection0 = dynamic_cast<NXOpen::BlockStyler::SelectObject*>(theDialog->TopBlock()->FindBlock("selection0"));
group03 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group03"));
stringPartName = dynamic_cast<NXOpen::BlockStyler::StringBlock*>(theDialog->TopBlock()->FindBlock("stringPartName"));
stringDesigner = dynamic_cast<NXOpen::BlockStyler::StringBlock*>(theDialog->TopBlock()->FindBlock("stringDesigner"));
stringDate = dynamic_cast<NXOpen::BlockStyler::StringBlock*>(theDialog->TopBlock()->FindBlock("stringDate"));
stringContact = dynamic_cast<NXOpen::BlockStyler::StringBlock*>(theDialog->TopBlock()->FindBlock("stringContact"));

//用户代码
//设置过虑(注释处改动即可使用)
Selection::SelectionAction action = Selection::SelectionActionClearAndEnableSpecific;
std::vector<Selection::MaskTriple> maskArray(1);
maskArray[0] = Selection::MaskTriple( UF_solid_type, UF_solid_body_subtype/*此处控制选择的类型,右键点转到定义查开类型*/, UF_UI_SEL_FEATURE_MAX_FEATURE /*此处控制选择的类型,在头文件uf_ui_types.可以查到*/ ); // Bodies
selection0/*此处blockID需按实际改*/->GetProperties()->SetSelectionFilter("SelectionFilter",action,maskArray);

//获取枚举值
PropertyList *enum0Props = enum0->GetProperties();
int EnumValue = enum0Props->GetEnum("Value");
delete enum0Props;
enum0Props = NULL;

enum0->GetProperties()->SetEnum("Value", 0 );//设置枚举初此值为0

//判断枚举控制group02是否显示
if ( EnumValue == 0 )
{

group02->SetShow(false);

}
else if ( EnumValue == 1 )
{

group02->SetShow(true);

}

nowdate();//自动获取日期

}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
Attribute_test::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//------------------------------------------------------------------------------
//Callback Name: dialogShown_cb
//This callback is executed just before the dialog launch. Thus any value set
//here will take precedence and dialog will be launched showing that value.
//------------------------------------------------------------------------------
void Attribute_test::dialogShown_cb()
{
try
{
//用户代码
enum0->GetProperties()->SetEnum("Value", 0 );//设置枚举初此值为0
nowdate();//自动获取日期


}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
Attribute_test::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//------------------------------------------------------------------------------
//Callback Name: apply_cb
//------------------------------------------------------------------------------
int Attribute_test::apply_cb()
{
int errorCode = 0;
try
{
//---- Enter your callback code here -----
//用户代码

//获取枚举值
PropertyList *enum0Props = enum0->GetProperties();
int EnumValue = enum0Props->GetEnum("Value");
delete enum0Props;
enum0Props = NULL;

//判断枚举0在工作部件创建属性,1在实体创建属性
if ( EnumValue == 0 )
{

workPartSetAttr();

}
else if ( EnumValue == 1 )
{

bodySetAttr();

}

}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
errorCode = 1;
Attribute_test::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return errorCode;
}

//------------------------------------------------------------------------------
//Callback Name: update_cb
//------------------------------------------------------------------------------
int Attribute_test::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
try
{
if(block == enum0)
{
//---------Enter your code here-----------

//获取枚举值
PropertyList *enum0Props = enum0->GetProperties();
int EnumValue = enum0Props->GetEnum("Value");
delete enum0Props;
enum0Props = NULL;

//判断枚举控制group02是否显示
if ( EnumValue == 0 )
{

group02->SetShow(false);

}
else if ( EnumValue == 1 )
{

group02->SetShow(true);

}


}
else if(block == selection0)
{
//---------Enter your code here-----------
}
else if(block == stringPartName)
{
//---------Enter your code here-----------
}
else if(block == stringDesigner)
{
//---------Enter your code here-----------
}
else if(block == stringDate)
{
//---------Enter your code here-----------
}
else if(block == stringContact)
{
//---------Enter your code here-----------
}
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
Attribute_test::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return 0;
}

//------------------------------------------------------------------------------
//Callback Name: ok_cb
//------------------------------------------------------------------------------
int Attribute_test::ok_cb()
{
int errorCode = 0;
try
{
errorCode = apply_cb();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
errorCode = 1;
Attribute_test::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return errorCode;
}

//------------------------------------------------------------------------------
//Function Name: GetBlockProperties
//Description: Returns the propertylist of the specified BlockID
//------------------------------------------------------------------------------
PropertyList* Attribute_test::GetBlockProperties(const char *blockID)
{
return theDialog->GetBlockProperties(blockID);
}


//工作部件创建属性
void Attribute_test::workPartSetAttr()
{
try
{
//用户代码
Session *theSession = Session::GetSession(); //获取绘画窗口
Part *workPart(theSession->Parts()->Work()); //获取工作部件
Part *displayPart(theSession->Parts()->Display()); //获取显示部件

//获取UI工件名称
PropertyList *stringPartNameProps = stringPartName->GetProperties();
NXString thestringPartName = stringPartNameProps->GetString("Value");
delete stringPartNameProps;
stringPartNameProps = NULL;
//获取UI设计
PropertyList *stringDesignerProps = stringDesigner->GetProperties();
NXString thestringDesigner = stringDesignerProps->GetString("Value");
delete stringDesignerProps;
stringDesignerProps = NULL;
//获取UI日期
PropertyList *stringDateProps = stringDate->GetProperties();
NXString thestringDate = stringDateProps->GetString("Value");
delete stringDateProps;
stringDateProps = NULL;
//获取UI QQ
PropertyList *stringContactProps = stringContact->GetProperties();
NXString thestringContact = stringContactProps->GetString("Value");
delete stringContactProps;
stringContactProps = NULL;

//定义objects1为workPart
std::vector<NXObject *> objects1(1);
objects1[0] = workPart;

//NXOpen加属性
AttributePropertiesBuilder *attributePropertiesBuilder1;
attributePropertiesBuilder1 = theSession->AttributeManager()->CreateAttributePropertiesBuilder(workPart,objects1, AttributePropertiesBuilder::OperationTypeNone);

attributePropertiesBuilder1->SetTitle("PartName");//创建属性标题
attributePropertiesBuilder1->SetStringValue(thestringPartName);//创建字符属性内容

NXObject *nXObject1;
nXObject1 = attributePropertiesBuilder1->Commit();//创建第一个属性

attributePropertiesBuilder1->SetTitle("Design");//创建属性标题
attributePropertiesBuilder1->SetStringValue(thestringDesigner);//创建字符属性内容

NXObject *nXObject2;
nXObject2 = attributePropertiesBuilder1->Commit();//创建第二个属性

attributePropertiesBuilder1->SetTitle("日期");//创建属性标题
attributePropertiesBuilder1->SetStringValue(thestringDate);//创建字符属性内容

NXObject *nXObject3;
nXObject3 = attributePropertiesBuilder1->Commit();//创建第三个属性

attributePropertiesBuilder1->SetTitle("QQ");//创建属性标题
attributePropertiesBuilder1->SetStringValue(thestringContact);//创建字符属性内容

NXObject *nXObject4;
nXObject4 = attributePropertiesBuilder1->Commit();//创建第四个属性

attributePropertiesBuilder1->Destroy();//释放attributePropertiesBuilder1


}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
Attribute_test::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}


//实体创建属性
void Attribute_test::bodySetAttr()
{
try
{
//用户代码
Session *theSession = Session::GetSession(); //获取绘画窗口
Part *workPart(theSession->Parts()->Work()); //获取工作部件
Part *displayPart(theSession->Parts()->Display()); //获取显示部件

//获取UI工件名称
PropertyList *stringPartNameProps = stringPartName->GetProperties();
NXString thestringPartName = stringPartNameProps->GetString("Value");
delete stringPartNameProps;
stringPartNameProps = NULL;
//获取UI设计
PropertyList *stringDesignerProps = stringDesigner->GetProperties();
NXString thestringDesigner = stringDesignerProps->GetString("Value");
delete stringDesignerProps;
stringDesignerProps = NULL;
//获取UI日期
PropertyList *stringDateProps = stringDate->GetProperties();
NXString thestringDate = stringDateProps->GetString("Value");
delete stringDateProps;
stringDateProps = NULL;
//获取UI QQ
PropertyList *stringContactProps = stringContact->GetProperties();
NXString thestringContact = stringContactProps->GetString("Value");
delete stringContactProps;
stringContactProps = NULL;

//定义objects2为Body

//获取UI选择的体
PropertyList* selection01Props = selection0->GetProperties();
std::vector<NXOpen::TaggedObject *> theselection = selection01Props->GetTaggedObjectVector
("SelectedObjects");
delete selection01Props;
selection01Props = NULL;

Body *body1(dynamic_cast<Body *>(NXObjectManager::Get(theselection[0]->Tag())));
std::vector<NXObject *> objects2(1);
objects2[0] = body1;

//NXOpen加属性
AttributePropertiesBuilder *attributePropertiesBuilder1;
attributePropertiesBuilder1 = theSession->AttributeManager()->CreateAttributePropertiesBuilder(workPart,objects2, AttributePropertiesBuilder::OperationTypeNone);


attributePropertiesBuilder1->SetTitle("PartName");//创建属性标题
attributePropertiesBuilder1->SetStringValue(thestringPartName);//创建字符属性内容

NXObject *nXObject1;
nXObject1 = attributePropertiesBuilder1->Commit();//创建第一个属性

attributePropertiesBuilder1->SetTitle("Design");//创建属性标题
attributePropertiesBuilder1->SetStringValue(thestringDesigner);//创建字符属性内容

NXObject *nXObject2;
nXObject2 = attributePropertiesBuilder1->Commit();//创建第二个属性

attributePropertiesBuilder1->SetTitle("日期");//创建属性标题
attributePropertiesBuilder1->SetStringValue(thestringDate);//创建字符属性内容

NXObject *nXObject3;
nXObject3 = attributePropertiesBuilder1->Commit();//创建第三个属性

attributePropertiesBuilder1->SetTitle("QQ");//创建属性标题
attributePropertiesBuilder1->SetStringValue(thestringContact);//创建字符属性内容

NXObject *nXObject4;
nXObject4 = attributePropertiesBuilder1->Commit();//创建第四个属性

attributePropertiesBuilder1->Destroy();//释放attributePropertiesBuilder1


}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
Attribute_test::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}


//自动获取日期
void Attribute_test::nowdate()
{
try
{
UF_initialize(); //入口

//获取日期
UF_system_info_t info; //定义info
UF_ask_system_info(&info); //获取系统日期时间


//格式化日期

string formatdate = info.date_buf; //转换获得的日期和时间为字符串
char msg[256]; //定义数簇
sprintf(msg,"%s-%s-%s",formatdate.substr(0,4),formatdate.substr(5,2),formatdate.substr(8,2)); //格式化日期


//设置日期
PropertyList *stringDateProps = stringDate->GetProperties();
stringDateProps->SetString("Value",msg);
delete stringDateProps;
stringDateProps = NULL;

UF_free_system_info(&info);

UF_terminate(); //入口终止

}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
Attribute_test::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

怡宁塑胶模具设计
原文地址:https://www.cnblogs.com/hqsalanhuang/p/14223994.html