怡宁塑胶模具设计

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

VS2015 NX12

 

#include <uf_defs.h>
#include <uf_ui_types.h>
#include <iostream>
#include <NXOpen/Session.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/Callback.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/BlockStyler_UIBlock.hxx>
#include <NXOpen/BlockStyler_BlockDialog.hxx>
#include <NXOpen/BlockStyler_PropertyList.hxx>
#include <NXOpen/BlockStyler_Group.hxx>
#include <NXOpen/BlockStyler_Enumeration.hxx>
#include <NXOpen/BlockStyler_SelectObject.hxx>


//用户头文件
#include <NXOpen/ListingWindow.hxx> //打印信息
#include <NXOpen/NXException.hxx> //异常处理
#include <uf_obj.h>
#include <uf_ui.h>
#include <uf.h>
#include <NXOpen/DisplayManager.hxx>
#include <NXOpen/DisplayModification.hxx>
#include <NXOpen/DisplayableObject.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/NXObjectManager.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/BodyCollection.hxx>
#include <NXOpen/Face.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Assemblies_Component.hxx>
#include <NXOpen/Assemblies_ComponentAssembly.hxx>
#include <sstream>
#include <fstream>
#include <uf_ui.h>
#include <uf.h>
#include <uf_obj.h>
#include <uf_assem.h>
#include <uf_part.h>

 

//用户代码(定义函数)
Part *workPart;
BasePart *basedisplaypart;//定义顶层显示部件为全局变量
std::vector<int> basePartvectorTag;
Assemblies::Component *rootcomponent;

bool isComponent;

void selectFacefindPart();//选面找部件

void CycleComponent();//遍历装配

std::vector<NXOpen::TaggedObject*> theselection0;
//Face *SelectFacesprototype1;
std::vector<int> SelectFacesprototypeTag;

void setTransparent();//设置透明
void setNoTransparent();//设置不透明

 

void Transparent::initialize_cb()
{
try
{
group = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group"));
enum0 = dynamic_cast<NXOpen::BlockStyler::Enumeration*>(theDialog->TopBlock()->FindBlock("enum0"));
group0 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group0"));
selection0 = dynamic_cast<NXOpen::BlockStyler::SelectObject*>(theDialog->TopBlock()->FindBlock("selection0"));

//用户代码
//设置过虑(注释处改动即可使用)
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_ANY_FACE); //Faces
selection0/*此处blockID需按实际改*/->GetProperties()->SetSelectionFilter("SelectionFilter", action, maskArray);

//获取顶层部件名
basedisplaypart = theSession->Parts()->BaseDisplay();
basePartvectorTag.push_back(basedisplaypart->Tag());

theSession->ListingWindow()->Open();

theselection0.clear();


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

 

//------------------------------------------------------------------------------
//Callback Name: update_cb
//------------------------------------------------------------------------------
int Transparent::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
try
{
if (block == enum0)
{
//---------Enter your code here-----------
//获取类型UI界面
PropertyList *enum0Props = enum0->GetProperties();
int EnumValue = enum0Props->GetEnum("Value");
delete enum0Props;
enum0Props = NULL;

//判断
if (EnumValue == 0)
{
selectFacefindPart();//选面找部件
setTransparent();//设置透明
}
else if (EnumValue == 1)
{
selectFacefindPart();//选面找部件
setNoTransparent();//设置不透明
}


}
else if (block == selection0)
{
//---------Enter your code here-----------

//获取选择对象
PropertyList *selection0Props = selection0->GetProperties();
theselection0 = selection0Props->GetTaggedObjectVector("SelectedObjects");
delete selection0Props;
selection0Props = NULL;

//获取类型UI界面
PropertyList *enum0Props = enum0->GetProperties();
int EnumValue = enum0Props->GetEnum("Value");
delete enum0Props;
enum0Props = NULL;

//判断
if (EnumValue == 0)
{
selectFacefindPart();//选面找部件
setTransparent();//设置透明
}
else if (EnumValue == 1)
{
selectFacefindPart();//选面找部件
setNoTransparent();//设置不透明
}


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

//设置透明
void Transparent::setTransparent()
{
try
{
//---- Enter your callback code here -----
UF_initialize();
SelectFacesprototypeTag.clear();
if (theselection0.size() >= 1 && isComponent == true)
{
for (int i = 0; i<theselection0.size(); i++)
{
tag_t Facesprototype = UF_ASSEM_ask_prototype_of_occ(theselection0[i]->Tag());//如果是装配选的部件面,找面的原型
SelectFacesprototypeTag.push_back(Facesprototype);
}

//设置图层透明度
DisplayModification *displayModification1;
displayModification1 = theSession->DisplayManager()->NewDisplayModification();
displayModification1->SetApplyToAllFaces(true);
displayModification1->SetApplyToOwningParts(true);
displayModification1->SetNewTranslucency(100); //设置透明度

for (int i = 0; i < SelectFacesprototypeTag.size(); i++)
{
std::vector<DisplayableObject *> objects1(1);
Face *face1(dynamic_cast<Face *>(NXObjectManager::Get(SelectFacesprototypeTag[i])));
objects1[0] = face1;
displayModification1->Apply(objects1);
}

delete displayModification1;

}
else if (theselection0.size() >= 1 && isComponent == false)
{
//设置图层透明度
DisplayModification *displayModification1;
displayModification1 = theSession->DisplayManager()->NewDisplayModification();
displayModification1->SetApplyToAllFaces(true);
displayModification1->SetApplyToOwningParts(true);
displayModification1->SetNewTranslucency(100); //设置透明度

for (int i = 0; i < theselection0.size(); i++)
{
std::vector<DisplayableObject *> objects1(1);
Face *face1(dynamic_cast<Face *>(NXObjectManager::Get(theselection0[i]->Tag())));
objects1[0] = face1;
displayModification1->Apply(objects1);
}

delete displayModification1;
}
else if (theselection0.size() == 0)
{
return;
}
UF_terminate();

 

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
Transparent::theUI->NXMessageBox()->Show("设置透明", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}
//设置不透明
void Transparent::setNoTransparent()
{
try
{
//---- Enter your callback code here -----
UF_initialize();
SelectFacesprototypeTag.clear();
if (theselection0.size() >= 1 && isComponent == true)
{
for (int i = 0; i<theselection0.size(); i++)
{
tag_t Facesprototype = UF_ASSEM_ask_prototype_of_occ(theselection0[i]->Tag());//如果是装配选的部件面,找面的原型
SelectFacesprototypeTag.push_back(Facesprototype);
}

//设置图层透明度
DisplayModification *displayModification1;
displayModification1 = theSession->DisplayManager()->NewDisplayModification();
displayModification1->SetApplyToAllFaces(false);
displayModification1->SetApplyToOwningParts(false);
displayModification1->SetNewTranslucency(0); //设置透明度

for (int i = 0; i < SelectFacesprototypeTag.size(); i++)
{
std::vector<DisplayableObject *> objects1(1);
Face *face1(dynamic_cast<Face *>(NXObjectManager::Get(SelectFacesprototypeTag[i])));
objects1[0] = face1;
displayModification1->Apply(objects1);
}

delete displayModification1;

}
else if (theselection0.size() >= 1 && isComponent == false)
{
//设置图层透明度
DisplayModification *displayModification1;
displayModification1 = theSession->DisplayManager()->NewDisplayModification();
displayModification1->SetApplyToAllFaces(false);
displayModification1->SetApplyToOwningParts(false);
displayModification1->SetNewTranslucency(0); //设置透明度

for (int i = 0; i < theselection0.size(); i++)
{
std::vector<DisplayableObject *> objects1(1);
Face *face1(dynamic_cast<Face *>(NXObjectManager::Get(theselection0[i]->Tag())));
objects1[0] = face1;
displayModification1->Apply(objects1);
}

delete displayModification1;
}
else if (theselection0.size() == 0)
{
return;
}
UF_terminate();


}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
Transparent::theUI->NXMessageBox()->Show("设置不透明", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

 

 

posted on 2020-12-23 10:50  怡宁塑胶模具设计  阅读(739)  评论(1编辑  收藏  举报