NXOpen 遍历部件、填充树表创建节点、清理选择、高亮所选组件、改色、统针组件出现次数、节点和部件数据关联、判断组件加载状态

//循环遍历组装组件并填充树列表

void AssemblyViewer::PopulateTree(NXOpen::Assemblies::Component *component)
{
std::vector<Component *> childComponent = component->GetChildren();
int childComponentCount = childComponent.size();
for(int i=0; i<childComponentCount; ++i)
{
BlockStyler::Node *node = treeList->CreateNode(childComponent[i]->Name());
NXOpen::DataContainer *nodeData = node->GetNodeData();
nodeData->AddTaggedObject("Data",childComponent[i]);
treeNodes.push_back(node);
delete nodeData;
nodeData = NULL;

node->SetForegroundColor(198);
treeList->InsertNode(node,parentNode,NULL,treeList->NodeInsertOptionLast);
parentNode = node;
PopulateTree(childComponent[i]);
parentNode = node->ParentNode();
}
}

//执行部分清理并关闭高亮显示
void AssemblyViewer::PartCleanup()
{
try
{
NXOpen::PartCleanup *partCleanup1;
partCleanup1 = theSession->NewPartCleanup();
partCleanup1->SetTurnOffHighlighting(true);
partCleanup1->DoCleanup();
delete partCleanup1;
partCleanup1 = NULL;
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
AssemblyViewer::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//突出显示所选的组件
void AssemblyViewer::HighlightComponent(std::vector<NXOpen::TaggedObject *>&selObjectData, bool highlight)
{
try
{
//---- Enter your callback code here -----
int selObjectCount = selObjectData.size();

for(int i=0; i<selObjectCount; ++i)
{
NXOpen::Assemblies::Component *component = dynamic_cast<NXOpen::Assemblies::Component *> (selObjectData[i]);

if(!component)
continue;

highlight?component->Highlight():component->Unhighlight();

if(drawAll)
{
ComponentAssembly *owner =component->DirectOwner();//Gets the parent assembly of the component
Component *mapped = owner->MapComponentFromParent(component);//Gets other occurence of this component
ComponentAssembly *topOwner = theSession->Parts()->Display()->ComponentAssembly();//Gets the root part of the component

std::vector<Component *>mappedBack = topOwner->MapComponentsFromSubassembly(mapped);//Returns the component occurences

int mappedBackCount = mappedBack.size();
for(int i=0; i<mappedBackCount; ++i)
{
highlight?mappedBack[i]->Highlight():mappedBack[i]->Unhighlight();
}
}
}
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
AssemblyViewer::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//为组件设置所选的颜色
void AssemblyViewer::ApplyColor(std::vector<NXOpen::TaggedObject *>&selObjectData)
{
try
{
//---- Enter your callback code here -----
NXOpen::BlockStyler::PropertyList *selColorProps =selColor->GetProperties();
std::vector<int > colorValue = selColorProps->GetIntegerVector("Value");
delete selColorProps;
selColorProps = NULL;

DisplayModification *displayModification1 = theSession->DisplayManager()->NewDisplayModification();
displayModification1->SetApplyToAllFaces(false);
displayModification1->SetApplyToOwningParts(false);
displayModification1->SetNewColor(colorValue[0]);

std::vector<DisplayableObject *> objects1;
int selObjectCount = selObjectData.size();
for(int i=0; i<selObjectCount; ++i)
{
objects1.push_back(dynamic_cast<NXOpen::DisplayableObject *>(selObjectData[i]));
}
displayModification1->Apply(objects1);
objects1.clear();
delete displayModification1;
displayModification1 = NULL;

PartCleanup();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
AssemblyViewer::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//获取组件的出现次数
std::vector<NXOpen::TaggedObject *>AssemblyViewer::GetComponentOccurences(std::vector<NXOpen::Assemblies::Component *>&assemblyComponent)
{
std::vector<NXOpen::TaggedObject *>componentOccurences ;
try
{
int assemblyComponentCount = assemblyComponent.size();

for(int i=0; i<assemblyComponentCount; ++i)
{
ComponentAssembly *owner = assemblyComponent[i]->DirectOwner(); //Gets the parent assembly of the component
Component *mapped = owner->MapComponentFromParent(assemblyComponent[i]); //Gets other occurence of this component
ComponentAssembly *topOwner = theSession->Parts()->Display()->ComponentAssembly();//Gets the root part of the component

std::vector<Component *>mappedBack = topOwner->MapComponentsFromSubassembly(mapped);
for (std::vector<Component *>::iterator it = mappedBack.begin(); it != mappedBack.end(); ++it)
{
componentOccurences.push_back(*it);
}
}
}
catch(exception& ex)
{
AssemblyViewer::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return componentOccurences;
}

//从选择对象块中获取所选对象的标记
std::vector<NXOpen::TaggedObject *>AssemblyViewer::GetSelectedObjects()
{
std::vector<NXOpen::TaggedObject *>selCompTags ;
try
{
NXOpen::BlockStyler::PropertyList *selCompProps =selComponent->GetProperties();
selCompTags = selCompProps->GetTaggedObjectVector("SelectedObjects");
delete selCompProps;
selCompProps = NULL;
}
catch(exception& ex)
{
AssemblyViewer::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return selCompTags;
}

//判断装配组件Component是否加载状态
bool isComponentLoad(Assemblies::Component *component)
{
bool isbool = true;
Part *resCurrentProtype = dynamic_cast<Part *>(component->Prototype());

if(resCurrentProtype == NULL)
{
isbool = false;
}
return isbool;
}

//UF判断组件加载状态

int componentloaded =UF_PART_is_loaded(component1->DisplayName().GetLocaleText());//获取部件加载状态 0 =Part is not loaded ,1 =Part is fully loaded in session, 2 =Part is partially loaded in session, Other =Error Code
if (componentloaded == 2 )
{
} //判断结束

 //判断装配组件Component是否加载状态

Part *partLoaded = dynamic_cast<Part *>(component1->Prototype()); //component强制转换为part
if (partLoaded->IsFullyLoaded())
{
//完全加载

}
else if (partLoaded == NULL)
{
//没加载

}
else
{
//部份加载
}

//判断组件状态(抑制||空)
if (children[i]->IsSuppressed()||children[i]->Prototype()==NULL)
{
theSession->ListingWindow()->WriteLine(children[i]->DisplayName());//信息窗口写入
}
else
{
}

//从树列表中获取所选节点的标记
std::vector<NXOpen::TaggedObject *>AssemblyViewer::GetSelectedNodes()
{
std::vector<NXOpen::TaggedObject *>selNodeTags ;
try
{
std::vector<NXOpen::BlockStyler::Node *>selectedNodes = treeList->GetSelectedNodes();
int selectedNodesCount = selectedNodes.size();
for(int i=0; i<selectedNodesCount; ++i)
{
NXOpen::DataContainer *nodeData = selectedNodes[i]->GetNodeData();
selNodeTags.push_back(nodeData->GetTaggedObject("Data"));
}
}
catch(exception& ex)
{
AssemblyViewer::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return selNodeTags;
}

//根据IsSingleComponentSelection,选择是针对单个组件还是针对所有组件
void AssemblyViewer::ComponentSelection()
{
try
{
bool singleComponent = IsSingleComponentSelection();
std::vector<NXOpen::TaggedObject *>selObjectData = GetSelectedObjects();
std::vector<NXOpen::TaggedObject *>selNodeComps = GetSelectedNodes();

// Create a DisplayModification object to set the color
if (selObjectData.size()>0)
{
HighlightComponent(selObjectData,false);
drawAll = singleComponent?false:true;
HighlightComponent(selObjectData, true);
}
if (selNodeComps.size()>0)
{
HighlightComponent(selNodeComps,false);
drawAll = singleComponent?false:true;
HighlightComponent(selNodeComps, true);
}
}
catch(exception& ex)
{
AssemblyViewer::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//树列表和部件相关代码

treeList->InsertColumn(0, "Component Names", 240);
treeList->SetColumnResizePolicy(0, Tree::ColumnResizePolicyConstantWidth);

BasePart *basePart(theSession->Parts()->BaseWork());

//在树列表中创建节点,并将组件与节点数据关联起来。
if(basePart!=NULL)
{
NXOpen::Assemblies::Component *rootComponent = basePart->ComponentAssembly()->RootComponent();

if(rootComponent != NULL)
{
parentNode = treeList->CreateNode("ROOT");
NXOpen::DataContainer *nodeData = parentNode->GetNodeData();
nodeData->AddTaggedObject("Data",rootComponent);

delete nodeData;
nodeData = NULL;

parentNode->SetForegroundColor(198);
treeList->InsertNode(parentNode,NULL,NULL,treeList->NodeInsertOptionAlwaysFirst);
parentNode->Expand(Node::ExpandOptionExpand);
PopulateTree(rootComponent);
}
else
{
AssemblyViewer::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeInformation, "Opened part is not an assembly. Tree-list will be empty");
return;
}
}

怡宁塑胶模具设计
原文地址:https://www.cnblogs.com/hqsalanhuang/p/14305074.html