本文通过举四个例子来告诉大家在NX二次开发过程中会经常用到UFUN和NXOpen结合去开发,在UFUN中我们得到的是Tag_t对象,在NXOpen中得到的是TaggedObject对象,这两个是需要进行转换的。
本文主要知识点为:TaggedObject->tag_t() , NXOpen::NXObjectManager::Get(BodyTag1) , feature1->JournalIdentifier()的用法。
NX11+VS2013
#include <uf.h>
#include <uf_modl.h>
#include <uf_obj.h>
#include <NXOpen\NXObjectManager.hxx>
#include <NXOpen/Features_BlockFeatureBuilder.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/BodyCollection.hxx>
#include <NXOpen/DisplayManager.hxx>
#include <NXOpen/PartCollection.hxx>
NXOpen::Session *theSession = NXOpen::Session::GetSession();
NXOpen::Part *workPart(theSession->Parts()->Work());
NXOpen::Part *displayPart(theSession->Parts()->Display());
UF_initialize();
//方法1:UFUN方式创建块,设置块颜色。
//UFUN方式创建块
UF_FEATURE_SIGN Sign = UF_NULLSIGN;
double Corner_pt[3] = { 0.0, 0.0, 0.0 };
char *Edge_len[3] = { "100", "100", "100" };
tag_t BlkTag = NULL_TAG;
UF_MODL_create_block1(Sign, Corner_pt, Edge_len, &BlkTag);
//UFUN方式特征找体
tag_t BodyTag = NULL_TAG;
UF_MODL_ask_feat_body(BlkTag, &BodyTag);
//UFUN方式设置颜色
UF_OBJ_set_color(BodyTag, 186);
//方法2:NXOpen方式创建块,设置块颜色。
//NXOpen方式创建块
NXOpen::Features::Feature *nullNXOpen_Features_Feature(NULL);
NXOpen::Features::BlockFeatureBuilder *blockFeatureBuilder1;
blockFeatureBuilder1 = workPart->Features()->CreateBlockFeatureBuilder(nullNXOpen_Features_Feature);
std::vector<NXOpen::Body *> targetBodies1(1);
NXOpen::Body *nullNXOpen_Body(NULL);
targetBodies1[0] = nullNXOpen_Body;
blockFeatureBuilder1->BooleanOption()->SetTargetBodies(targetBodies1);
blockFeatureBuilder1->SetType(NXOpen::Features::BlockFeatureBuilder::TypesOriginAndEdgeLengths);//设置创建类型
NXOpen::Point3d originPoint1(100.0, 100.0, 100.0);
blockFeatureBuilder1->SetOriginAndLengths(originPoint1, "100", "100", "100");//设置原点
blockFeatureBuilder1->SetBooleanOperationAndTarget(NXOpen::Features::Feature::BooleanTypeCreate, nullNXOpen_Body);//设置布尔
NXOpen::Features::Feature *feature1;
feature1 = blockFeatureBuilder1->CommitFeature();
//NXOpen方式设置颜色
NXOpen::DisplayModification *displayModification1;
displayModification1 = theSession->DisplayManager()->NewDisplayModification();
displayModification1->SetNewColor(108);//设置颜色
std::vector<NXOpen::DisplayableObject *> objects1(1);
NXOpen::Body *body1(dynamic_cast<NXOpen::Body *>(workPart->Bodies()->FindObject(feature1->JournalIdentifier())));
objects1[0] = body1;
displayModification1->Apply(objects1);
//方法3:UFUN创建块,NXOpen设置颜色
//UFUN方式创建块
UF_FEATURE_SIGN Sign1 = UF_NULLSIGN;
double Corner_pt1[3] = { 200.0, 200.0, 200.0 };
char *Edge_len1[3] = { "100", "100", "100" };
tag_t BlkTag1 = NULL_TAG;
UF_MODL_create_block1(Sign1, Corner_pt1, Edge_len1, &BlkTag1);
//UFUN方式特征找体
tag_t BodyTag1 = NULL_TAG;
UF_MODL_ask_feat_body(BlkTag1, &BodyTag1);
//NXOpen方式设置颜色
NXOpen::DisplayModification *displayModification2;
displayModification2 = theSession->DisplayManager()->NewDisplayModification();
displayModification2->SetNewColor(128);//设置颜色
std::vector<NXOpen::DisplayableObject *> objects2(1);
NXOpen::Body *body2(dynamic_cast<NXOpen::Body *>(NXOpen::NXObjectManager::Get(BodyTag1)));
objects2[0] = body2;
displayModification2->Apply(objects2);
//方法4:NXOpen创建块,UFUN设置颜色
//NXOpen方式创建块
NXOpen::Features::Feature *nullNXOpen_Features_Feature1(NULL);
NXOpen::Features::BlockFeatureBuilder *blockFeatureBuilder2;
blockFeatureBuilder2 = workPart->Features()->CreateBlockFeatureBuilder(nullNXOpen_Features_Feature1);
std::vector<NXOpen::Body *> targetBodies2(1);
NXOpen::Body *nullNXOpen_Body1(NULL);
targetBodies2[0] = nullNXOpen_Body1;
blockFeatureBuilder2->BooleanOption()->SetTargetBodies(targetBodies2);
blockFeatureBuilder2->SetType(NXOpen::Features::BlockFeatureBuilder::TypesOriginAndEdgeLengths);//设置创建类型
NXOpen::Point3d originPoint2(300.0, 300.0, 300.0);
blockFeatureBuilder2->SetOriginAndLengths(originPoint2, "100", "100", "100");//设置原点
blockFeatureBuilder2->SetBooleanOperationAndTarget(NXOpen::Features::Feature::BooleanTypeCreate, nullNXOpen_Body1);//设置布尔
NXOpen::Features::Feature *feature2;
feature2 = blockFeatureBuilder2->CommitFeature();
//UFUN方式特征找体
tag_t BodyTag2 = NULL_TAG;
UF_MODL_ask_feat_body(feature2->Tag(), &BodyTag2);
//UFUN方式设置颜色
UF_OBJ_set_color(BodyTag2, 125);
UF_terminate();