一般针对录制的代码,新手可能不知道其具体的用法,有一些附加的功能我们用不上,所以可以进行适当的删减。下面是创建一个块的录制代码,我会在这里进行详细的解析。
1、原本录制代码
NXOpen::Session *theSession = NXOpen::Session::GetSession();
NXOpen::Part *workPart(theSession->Parts()->Work());
NXOpen::Part *displayPart(theSession->Parts()->Display());
// ----------------------------------------------
// Menu: 工具(T)->重复命令(R)->2 长方体
// ----------------------------------------------
// ----------------------------------------------
// Menu: 插入(S)->设计特征(E)->长方体(K)...
// ----------------------------------------------
NXOpen::Session::UndoMarkId markId1;
markId1 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityVisible, NXOpen::NXString("\345\274\200\345\247\213", NXOpen::NXString::UTF8));
NXOpen::Features::Feature *nullNXOpen_Features_Feature(NULL);
NXOpen::Features::BlockFeatureBuilder *blockFeatureBuilder1;
blockFeatureBuilder1 = workPart->Features()->CreateBlockFeatureBuilder(nullNXOpen_Features_Feature);
blockFeatureBuilder1->BooleanOption()->SetType(NXOpen::GeometricUtilities::BooleanOperation::BooleanTypeCreate);
std::vector<NXOpen::Body *> targetBodies1(1);
NXOpen::Body *nullNXOpen_Body(NULL);
targetBodies1[0] = nullNXOpen_Body;
blockFeatureBuilder1->BooleanOption()->SetTargetBodies(targetBodies1);
blockFeatureBuilder1->BooleanOption()->SetType(NXOpen::GeometricUtilities::BooleanOperation::BooleanTypeCreate);
std::vector<NXOpen::Body *> targetBodies2(1);
targetBodies2[0] = nullNXOpen_Body;
blockFeatureBuilder1->BooleanOption()->SetTargetBodies(targetBodies2);
theSession->SetUndoMarkName(markId1, NXOpen::NXString("\351\225\277\346\226\271\344\275\223 \345\257\271\350\257\235\346\241\206", NXOpen::NXString::UTF8));
NXOpen::Point3d coordinates1(0.0, 0.0, 0.0);
NXOpen::Point *point1;
point1 = workPart->Points()->CreatePoint(coordinates1);
NXOpen::Unit *unit1(dynamic_cast<NXOpen::Unit *>(workPart->UnitCollection()->FindObject("MilliMeter")));
NXOpen::Expression *expression1;
expression1 = workPart->Expressions()->CreateSystemExpressionWithUnits("0", unit1);
NXOpen::Session::UndoMarkId markId2;
markId2 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NXOpen::NXString("\351\225\277\346\226\271\344\275\223", NXOpen::NXString::UTF8));
theSession->DeleteUndoMark(markId2, NULL);
NXOpen::Session::UndoMarkId markId3;
markId3 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NXOpen::NXString("\351\225\277\346\226\271\344\275\223", NXOpen::NXString::UTF8));
blockFeatureBuilder1->SetType(NXOpen::Features::BlockFeatureBuilder::TypesOriginAndEdgeLengths);
NXOpen::Point *point2;
point2 = blockFeatureBuilder1->OriginPoint();
blockFeatureBuilder1->SetOriginPoint(point1);
NXOpen::Point3d originPoint1(0.0, 0.0, 0.0);
blockFeatureBuilder1->SetOriginAndLengths(originPoint1, "50", "50", "50");
blockFeatureBuilder1->SetBooleanOperationAndTarget(NXOpen::Features::Feature::BooleanTypeCreate, nullNXOpen_Body);
NXOpen::Features::Feature *feature1;
feature1 = blockFeatureBuilder1->CommitFeature();
theSession->DeleteUndoMark(markId3, NULL);
theSession->SetUndoMarkName(markId1, NXOpen::NXString("\351\225\277\346\226\271\344\275\223", NXOpen::NXString::UTF8));
blockFeatureBuilder1->Destroy();
workPart->Expressions()->Delete(expression1);
// ----------------------------------------------
// Menu: 工具(T)->操作记录(J)->停止录制(S)
// ----------------------------------------------
2、分析代码:
//UndoMarkId撤销有关,可以删去
NXOpen::Session::UndoMarkId markId1;
markId1 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityVisible, NXOpen::NXString("\345\274\200\345\247\213", NXOpen::NXString::UTF8));
//1、定义一个Feature的指针nullNXOpen_Features_Feature
NXOpen::Features::Feature *nullNXOpen_Features_Feature(NULL);
//2、创建一个BlockFeatureBuilder并进行实例化
NXOpen::Features::BlockFeatureBuilder *blockFeatureBuilder1;
blockFeatureBuilder1 = workPart->Features()->CreateBlockFeatureBuilder(nullNXOpen_Features_Feature);
//3、调用成员函数,及设置相关内容
//如果有布尔运算,成员targetBodies1,targetBodies2,没有删去
blockFeatureBuilder1->BooleanOption()->SetType(NXOpen::GeometricUtilities::BooleanOperation::BooleanTypeCreate);
std::vector<NXOpen::Body *> targetBodies1(1);
NXOpen::Body *nullNXOpen_Body(NULL);//无布尔运算
targetBodies1[0] = nullNXOpen_Body;
blockFeatureBuilder1->BooleanOption()->SetTargetBodies(targetBodies1);
blockFeatureBuilder1->BooleanOption()->SetType(NXOpen::GeometricUtilities::BooleanOperation::BooleanTypeCreate);
std::vector<NXOpen::Body *> targetBodies2(1);
targetBodies2[0] = nullNXOpen_Body;
blockFeatureBuilder1->BooleanOption()->SetTargetBodies(targetBodies2);
//UndoMarkId撤销有关,可以删去
theSession->SetUndoMarkName(markId1, NXOpen::NXString("\351\225\277\346\226\271\344\275\223 \345\257\271\350\257\235\346\241\206", NXOpen::NXString::UTF8));
//4、指定坐标点默认可以删去
NXOpen::Point3d coordinates1(0.0, 0.0, 0.0);
NXOpen::Point *point1;
point1 = workPart->Points()->CreatePoint(coordinates1);
//5、单位与表达式有关,这里没用可以选择删去,因为我们只是生成一个块,并无单位要求
NXOpen::Unit *unit1(dynamic_cast<NXOpen::Unit *>(workPart->UnitCollection()->FindObject("MilliMeter")));
NXOpen::Expression *expression1;
expression1 = workPart->Expressions()->CreateSystemExpressionWithUnits("0", unit1);
//UndoMarkId撤销有关,可以删去
NXOpen::Session::UndoMarkId markId2;
markId2 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NXOpen::NXString("\351\225\277\346\226\271\344\275\223", NXOpen::NXString::UTF8));
theSession->DeleteUndoMark(markId2, NULL);
NXOpen::Session::UndoMarkId markId3;
markId3 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NXOpen::NXString("\351\225\277\346\226\271\344\275\223", NXOpen::NXString::UTF8));
//6、设置blockFeatureBuilder1类型、指定点、原点、边长、布尔
blockFeatureBuilder1->SetType(NXOpen::Features::BlockFeatureBuilder::TypesOriginAndEdgeLengths);
NXOpen::Point *point2;
point2 = blockFeatureBuilder1->OriginPoint();
blockFeatureBuilder1->SetOriginPoint(point1);
NXOpen::Point3d originPoint1(0.0, 0.0, 0.0);
blockFeatureBuilder1->SetOriginAndLengths(originPoint1, "50", "50", "50");
blockFeatureBuilder1->SetBooleanOperationAndTarget(NXOpen::Features::Feature::BooleanTypeCreate, nullNXOpen_Body);
//7、创建块创建特征
NXOpen::Features::Feature *feature1;
feature1 = blockFeatureBuilder1->CommitFeature();
//UndoMarkId撤销有关,可以删去
theSession->DeleteUndoMark(markId3, NULL);
theSession->SetUndoMarkName(markId1, NXOpen::NXString("\351\225\277\346\226\271\344\275\223", NXOpen::NXString::UTF8));
//8、销毁指针
blockFeatureBuilder1->Destroy();
//有关表达式的销毁,由于没有用到删除
workPart->Expressions()->Delete(expression1);
3、修改后的代码
//1、定义一个Feature的指针nullNXOpen_Features_Feature
NXOpen::Features::Feature *nullNXOpen_Features_Feature(NULL);
//2、创建一个BlockFeatureBuilder并进行实例化
NXOpen::Features::BlockFeatureBuilder *blockFeatureBuilder1;
blockFeatureBuilder1 = workPart->Features()->CreateBlockFeatureBuilder(nullNXOpen_Features_Feature);
//3、调用成员函数,及设置相关内容
blockFeatureBuilder1->SetType(NXOpen::Features::BlockFeatureBuilder::TypesOriginAndEdgeLengths);
NXOpen::Point3d originPoint1(0.0, 0.0, 0.0);
blockFeatureBuilder1->SetOriginAndLengths(originPoint1, "50", "50", "50");
//4、创建块创建特征
NXOpen::Features::Feature *feature1;
feature1 = blockFeatureBuilder1->CommitFeature();
//5、销毁指针
blockFeatureBuilder1->Destroy();