通过对话框选择体以及分割该体的面,实现体的分割,相关资源请访问下载我的资源分享(上传需要时间审核)
NXOpen::Session* theSession = NXOpen::Session::GetSession();
NXOpen::Part* workPart(theSession->Parts()->Work());
NXOpen::Part* displayPart(theSession->Parts()->Display());
//根据对话框选择体和分割面
std::vector<NXOpen::TaggedObject*>bodysSelected = bodySelect0->GetSelectedObjects();
std::vector<NXOpen::TaggedObject*>facesSelected = face_select0->GetSelectedObjects();
NXOpen::Body* body1(dynamic_cast<NXOpen::Body*>(bodysSelected[0]));
NXOpen::Face* face1(dynamic_cast<NXOpen::Face*>(facesSelected[0]));
//根据选择的面创建基准平面
NXOpen::Features::Feature* nullNXOpen_Features_Feature(NULL);
NXOpen::Features::DatumPlaneBuilder* datumPlaneBuilder1;
datumPlaneBuilder1 = workPart->Features()->CreateDatumPlaneBuilder(nullNXOpen_Features_Feature);
NXOpen::Plane* plane1;
plane1 = datumPlaneBuilder1->GetPlane();
plane1->SetUpdateOption(NXOpen::SmartObject::UpdateOptionWithinModeling);
plane1->SetMethod(NXOpen::PlaneTypes::MethodTypeDistance);
std::vector<NXOpen::NXObject*> geom1(1);
geom1[0] = face1;
plane1->SetGeometry(geom1);
plane1->SetFlip(false);
plane1->SetReverseSide(false);
NXOpen::Expression* expression3;
expression3 = plane1->Expression();
expression3->SetRightHandSide("0");
plane1->SetAlternate(NXOpen::PlaneTypes::AlternateTypeOne);
plane1->Evaluate();
NXOpen::Features::Feature* feature1;
feature1 = datumPlaneBuilder1->CommitFeature();
NXOpen::Features::DatumPlaneFeature* datumPlaneFeature1(dynamic_cast<NXOpen::Features::DatumPlaneFeature*>(feature1));
NXOpen::DatumPlane* datumPlane1;
datumPlane1 = datumPlaneFeature1->DatumPlane();
//体分割操作
NXOpen::Features::SplitBody* nullNXOpen_Features_SplitBody(NULL);
NXOpen::Features::SplitBodyBuilder* splitBodyBuilder1;
splitBodyBuilder1 = workPart->Features()->CreateSplitBodyBuilderUsingCollector(nullNXOpen_Features_SplitBody);
std::vector<NXOpen::Body*> bodies1(1);
bodies1[0] = body1;
NXOpen::BodyDumbRule* bodyDumbRule1;
bodyDumbRule1 = workPart->ScRuleFactory()->CreateRuleBodyDumb(bodies1);
NXOpen::ScCollector* scCollector1;
scCollector1 = workPart->ScCollectors()->CreateCollector();
std::vector<NXOpen::SelectionIntentRule*> rules1(1);
rules1[0] = bodyDumbRule1;
scCollector1->ReplaceRules(rules1, false);
splitBodyBuilder1->SetTargetBodyCollector(scCollector1);
std::vector<NXOpen::DatumPlane*> faces1(1);
faces1[0] = datumPlane1;
NXOpen::FaceDumbRule* faceDumbRule1;
faceDumbRule1 = workPart->ScRuleFactory()->CreateRuleFaceDatum(faces1);
std::vector<NXOpen::SelectionIntentRule*> rules2(1);
rules2[0] = faceDumbRule1;
splitBodyBuilder1->BooleanTool()->FacePlaneTool()->ToolFaces()->FaceCollector()->ReplaceRules(rules2, false);
NXOpen::Features::Feature* splitFeature = splitBodyBuilder1->CommitFeature();
std::vector< NXOpen::Body* >splitBody = splitFeature->GetBodies();
//移除参数
NXOpen::Features::RemoveParametersBuilder* removeParametersBuilder1;
removeParametersBuilder1 = workPart->Features()->CreateRemoveParametersBuilder();
for (int i=0; i< splitBody.size(); i++)
{
bool added1;
added1 = removeParametersBuilder1->Objects()->Add(splitBody[i]);
}
NXOpen::NXObject* nXObject11;
nXObject11 = removeParametersBuilder1->Commit();
//删除创建的基准平面
std::vector<NXOpen::TaggedObject*> objects1(1);
objects1[0] = datumPlane1;
int nErrs1;
nErrs1 = theSession->UpdateManager()->AddObjectsToDeleteList(objects1);
NXOpen::Session::UndoMarkId id1;
id1 = theSession->NewestVisibleUndoMark();
int nErrs2;
nErrs2 = theSession->UpdateManager()->DoUpdate(id1);