C++ 2010
#include <NXOpen/Annotations.hxx>
#include <NXOpen/Annotations_AnnotationManager.hxx>
#include <NXOpen/Annotations_CenterlineCollection.hxx>
#include <NXOpen/SelectNXObjectList.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/NXObjectManager.hxx>
//选择过滤
Selection::SelectionAction action = Selection::SelectionActionClearAndEnableSpecific;
std::vector<Selection::MaskTriple> maskArray(1);
maskArray[0] = Selection::MaskTriple( UF_solid_type, UF_all_subtype, UF_UI_SEL_FEATURE_CYLINDRICAL_FACE);//圆柱面
selection0->GetProperties()->SetSelectionFilter("SelectionFilter", action, maskArray);
//获取UI选取的圆柱面
std::vector<TaggedObject *> faces = selection0->GetSelectedObjects(); //得到圆柱面
//创建圆柱面中心线
Face *face2 (dynamic_cast<Face *>(NXObjectManager::Get(faces[0]->Tag())));
NXOpen::Annotations::Centerline3d *line1(NULL);
NXOpen::Annotations::Centerline3dBuilder*line1Builder;
line1Builder=workPart->Annotations()->Centerlines()->CreateCenterline3dBuilder(line1);
line1Builder->Face()->Add(face2);
line1Builder->Settings()->SetExtension(0.0);
//法1
NXOpen::NXObject*nXObject1;
nXObject1=line1Builder->Commit();
//法2
//定义NXObject容器得到中心线
std::vector<NXObject *> nxobjects;
nxobjects = line1Builder->GetCommittedObjects();
line1Builder->Destroy();
//sprintf (msg,"%d",nxobjects.size());
//theSession->ListingWindow()->WriteLine( msg);
//中心线改色
DisplayModification *displayModification1;
displayModification1 = theSession->DisplayManager()->NewDisplayModification();
displayModification1->SetApplyToAllFaces(false);
displayModification1->SetApplyToOwningParts(false);
displayModification1->SetNewWidth(DisplayableObject::ObjectWidthOne);
displayModification1->SetNewColor(186); //设置色
std::vector<NXOpen::DisplayableObject *> objects2(1);
//法1
//objects2[0]=dynamic_cast<NXOpen::DisplayableObject *>(nXObject1);
//法2
for ( int i=0 ; i < nxobjects.size(); i++)
{
objects2[i]=dynamic_cast<NXOpen::DisplayableObject *>(nxobjects[i]);
}
displayModification1->Apply(objects2);
delete displayModification1;