1 本文通过举四个例子来告诉大家在NX二次开发过程中会经常用到UFUN和NXOpen结合去开发,在UFUN中我们得到的是Tag_t对象,在NXOpen中得到的是TaggedObject对象,这两个是需要进行转换的。本文主要知识点为:TaggedObject->tag_t() , NXOpen::NXObjectManager::Get(BodyTag1) , feature1->JournalIdentifier()的用法。 2 3 NX11+VS2013 4 5 #include <uf.h> 6 #include <uf_modl.h> 7 #include <uf_obj.h> 8 #include <NXOpenNXObjectManager.hxx> 9 #include <NXOpen/Features_BlockFeatureBuilder.hxx> 10 #include <NXOpen/Features_FeatureCollection.hxx> 11 #include <NXOpen/NXException.hxx> 12 #include <NXOpen/Body.hxx> 13 #include <NXOpen/BodyCollection.hxx> 14 #include <NXOpen/DisplayManager.hxx> 15 #include <NXOpen/PartCollection.hxx> 16 17 NXOpen::Session *theSession = NXOpen::Session::GetSession(); 18 NXOpen::Part *workPart(theSession->Parts()->Work()); 19 NXOpen::Part *displayPart(theSession->Parts()->Display()); 20 21 UF_initialize(); 22 23 //方法1:UFUN方式创建块,设置块颜色。 24 //UFUN方式创建块 25 UF_FEATURE_SIGN Sign = UF_NULLSIGN; 26 double Corner_pt[3] = { 0.0, 0.0, 0.0 }; 27 char *Edge_len[3] = { "100", "100", "100" }; 28 tag_t BlkTag = NULL_TAG; 29 UF_MODL_create_block1(Sign, Corner_pt, Edge_len, &BlkTag); 30 31 //UFUN方式特征找体 32 tag_t BodyTag = NULL_TAG; 33 UF_MODL_ask_feat_body(BlkTag, &BodyTag); 34 35 //UFUN方式设置颜色 36 UF_OBJ_set_color(BodyTag, 186); 37 38 39 40 41 42 //方法2:NXOpen方式创建块,设置块颜色。 43 //NXOpen方式创建块 44 NXOpen::Features::Feature *nullNXOpen_Features_Feature(NULL); 45 NXOpen::Features::BlockFeatureBuilder *blockFeatureBuilder1; 46 blockFeatureBuilder1 = workPart->Features()->CreateBlockFeatureBuilder(nullNXOpen_Features_Feature); 47 48 std::vector<NXOpen::Body *> targetBodies1(1); 49 NXOpen::Body *nullNXOpen_Body(NULL); 50 targetBodies1[0] = nullNXOpen_Body; 51 blockFeatureBuilder1->BooleanOption()->SetTargetBodies(targetBodies1); 52 53 blockFeatureBuilder1->SetType(NXOpen::Features::BlockFeatureBuilder::TypesOriginAndEdgeLengths);//设置创建类型 54 55 NXOpen::Point3d originPoint1(100.0, 100.0, 100.0); 56 blockFeatureBuilder1->SetOriginAndLengths(originPoint1, "100", "100", "100");//设置原点 57 58 blockFeatureBuilder1->SetBooleanOperationAndTarget(NXOpen::Features::Feature::BooleanTypeCreate, nullNXOpen_Body);//设置布尔 59 60 NXOpen::Features::Feature *feature1; 61 feature1 = blockFeatureBuilder1->CommitFeature(); 62 63 //NXOpen方式设置颜色 64 NXOpen::DisplayModification *displayModification1; 65 displayModification1 = theSession->DisplayManager()->NewDisplayModification(); 66 67 displayModification1->SetNewColor(108);//设置颜色 68 69 std::vector<NXOpen::DisplayableObject *> objects1(1); 70 NXOpen::Body *body1(dynamic_cast<NXOpen::Body *>(workPart->Bodies()->FindObject(feature1->JournalIdentifier()))); 71 objects1[0] = body1; 72 displayModification1->Apply(objects1); 73 74 75 76 77 78 //方法3:UFUN创建块,NXOpen设置颜色 79 //UFUN方式创建块 80 UF_FEATURE_SIGN Sign1 = UF_NULLSIGN; 81 double Corner_pt1[3] = { 200.0, 200.0, 200.0 }; 82 char *Edge_len1[3] = { "100", "100", "100" }; 83 tag_t BlkTag1 = NULL_TAG; 84 UF_MODL_create_block1(Sign1, Corner_pt1, Edge_len1, &BlkTag1); 85 86 //UFUN方式特征找体 87 tag_t BodyTag1 = NULL_TAG; 88 UF_MODL_ask_feat_body(BlkTag1, &BodyTag1); 89 90 //NXOpen方式设置颜色 91 NXOpen::DisplayModification *displayModification2; 92 displayModification2 = theSession->DisplayManager()->NewDisplayModification(); 93 94 displayModification2->SetNewColor(128);//设置颜色 95 96 std::vector<NXOpen::DisplayableObject *> objects2(1); 97 NXOpen::Body *body2(dynamic_cast<NXOpen::Body *>(NXOpen::NXObjectManager::Get(BodyTag1))); 98 objects2[0] = body2; 99 displayModification2->Apply(objects2); 100 101 102 103 104 105 //方法4:NXOpen创建块,UFUN设置颜色 106 //NXOpen方式创建块 107 NXOpen::Features::Feature *nullNXOpen_Features_Feature1(NULL); 108 NXOpen::Features::BlockFeatureBuilder *blockFeatureBuilder2; 109 blockFeatureBuilder2 = workPart->Features()->CreateBlockFeatureBuilder(nullNXOpen_Features_Feature1); 110 111 std::vector<NXOpen::Body *> targetBodies2(1); 112 NXOpen::Body *nullNXOpen_Body1(NULL); 113 targetBodies2[0] = nullNXOpen_Body1; 114 blockFeatureBuilder2->BooleanOption()->SetTargetBodies(targetBodies2); 115 116 blockFeatureBuilder2->SetType(NXOpen::Features::BlockFeatureBuilder::TypesOriginAndEdgeLengths);//设置创建类型 117 118 NXOpen::Point3d originPoint2(300.0, 300.0, 300.0); 119 blockFeatureBuilder2->SetOriginAndLengths(originPoint2, "100", "100", "100");//设置原点 120 121 blockFeatureBuilder2->SetBooleanOperationAndTarget(NXOpen::Features::Feature::BooleanTypeCreate, nullNXOpen_Body1);//设置布尔 122 123 NXOpen::Features::Feature *feature2; 124 feature2 = blockFeatureBuilder2->CommitFeature(); 125 126 //UFUN方式特征找体 127 tag_t BodyTag2 = NULL_TAG; 128 UF_MODL_ask_feat_body(feature2->Tag(), &BodyTag2); 129 130 //UFUN方式设置颜色 131 UF_OBJ_set_color(BodyTag2, 125); 132 133 134 UF_terminate();