C++
HPP 关键代码
//用户代码
#include "ExtrudewithPreview.hpp"
#include "NXOpen/Body.hxx"
#include "NXOpen/Direction.hxx"
#include "NXOpen/DisplayableObject.hxx"
#include "NXOpen/DisplayModification.hxx"
#include "NXOpen/DisplayManager.hxx"
#include "NXOpen/Expression.hxx"
#include "NXOpen/Facet_FacetedBodyCollection.hxx"
#include "NXOpen/Features_BodyFeature.hxx"
#include "NXOpen/Features_Extrude.hxx"
#include "NXOpen/Features_ExtrudeBuilder.hxx"
#include "NXOpen/Features_Feature.hxx"
#include "NXOpen/Features_FeatureCollection.hxx"
#include "NXOpen/GeometricUtilities_Extend.hxx"
#include "NXOpen/GeometricUtilities_Limits.hxx"
#include "NXOpen/ModelingView.hxx"
#include "NXOpen/ModelingViewCollection.hxx"
#include "NXOpen/Part.hxx"
#include "NXOpen/PartCollection.hxx"
#include "NXOpen/Update.hxx"
#include "NXOpen/View.hxx"
#include "NXOpen/ViewCollection.hxx"
#include "NXOpen/Preferences_SessionVisualizationVisual.hxx"
#include <sstream>
#include <iomanip>
Part* workPart;
Part* displayPart;
std::vector<NXOpen::TaggedObject *> vectCol;
NXOpen::Direction* extrudeDirection;
std::vector<NXOpen::TaggedObject *> tagObjArray;
NXOpen::Features::Feature* feature1;
bool isFeatureCreated;
bool preview;
NXOpen::Session::UndoMarkId markId1;
double startValueOfExt;
double endValueOfExt;
NXOpen::Direction* directionToSet;
bool value;
bool Translucency1;
void createExtrude();
void redefineFeature();
void visibility();
void showOrCreate(int transparencyValue);
void setBoolean(bool value);
NXString doubleToNXString(double value);
NXString intToNXString(int value);
CPP 关键代码
void ExtrudewithPreview::initialize_cb()
{
try
{
SectionGroup = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("SectionGroup"));
SectionToExtrude = dynamic_cast<NXOpen::BlockStyler::SectionBuilder*>(theDialog->TopBlock()->FindBlock("SectionToExtrude"));
DirectionGroup = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("DirectionGroup"));
DirectionToExtrude = dynamic_cast<NXOpen::BlockStyler::SpecifyVector*>(theDialog->TopBlock()->FindBlock("DirectionToExtrude"));
LimitsGroup = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("LimitsGroup"));
StartLimit = dynamic_cast<NXOpen::BlockStyler::DoubleBlock*>(theDialog->TopBlock()->FindBlock("StartLimit"));
EndLimit = dynamic_cast<NXOpen::BlockStyler::DoubleBlock*>(theDialog->TopBlock()->FindBlock("EndLimit"));
PreviewGroup = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("PreviewGroup"));
PreviewToggle = dynamic_cast<NXOpen::BlockStyler::Toggle*>(theDialog->TopBlock()->FindBlock("PreviewToggle"));
ResultButton = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("ResultButton"));
//用户代码
NXOpen::BlockStyler::PropertyList* Endlimitproplist = EndLimit->GetProperties();
Endlimitproplist->SetDouble("Value", 36.0);
delete Endlimitproplist;
Endlimitproplist = NULL;
workPart = ExtrudewithPreview::theSession->Parts()->Work();
displayPart = ExtrudewithPreview::theSession->Parts()->Display();
feature1 = NULL;
isFeatureCreated = false;
preview = true;
value = true;
tagObjArray.clear();
vectCol.clear();
Translucency1 = ExtrudewithPreview::theUI->VisualizationVisualPreferences()->Translucency();
ExtrudewithPreview::theUI->VisualizationVisualPreferences()->SetTranslucency(true);
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
ExtrudewithPreview::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}
//------------------------------------------------------------------------------
//Callback Name: dialogShown_cb
//This callback is executed just before the dialog launch. Thus any value set
//here will take precedence and dialog will be launched showing that value.
//------------------------------------------------------------------------------
void ExtrudewithPreview::dialogShown_cb()
{
try
{
//---- Enter your callback code here -----
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
ExtrudewithPreview::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}
//------------------------------------------------------------------------------
//Callback Name: apply_cb
//------------------------------------------------------------------------------
int ExtrudewithPreview::apply_cb()
{
int errorCode = 0;
try
{
//---- Enter your callback code here -----
//用户代码
showOrCreate(0);
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
errorCode = 1;
ExtrudewithPreview::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return errorCode;
}
//------------------------------------------------------------------------------
//Callback Name: update_cb
//------------------------------------------------------------------------------
int ExtrudewithPreview::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
try
{
if(block == SectionToExtrude)
{
//---------Enter your code here-----------
NXOpen::BlockStyler::PropertyList* SectionToExtrudeproplist = SectionToExtrude->GetProperties();
tagObjArray = SectionToExtrudeproplist->GetTaggedObjectVector("SelectedObjects");
delete SectionToExtrudeproplist;
NXOpen::BlockStyler::PropertyList* PreviewToggleproplist = PreviewToggle->GetProperties();
preview = PreviewToggleproplist->GetLogical("Value");
delete PreviewToggleproplist;
createExtrude();
}
else if(block == DirectionToExtrude)
{
//---------Enter your code here-----------
NXOpen::BlockStyler::PropertyList* DirectionToExtrudeproplist = DirectionToExtrude->GetProperties();
vectCol = DirectionToExtrudeproplist->GetTaggedObjectVector("SelectedObjects");
delete DirectionToExtrudeproplist;
if (vectCol.size() > 0)
{
extrudeDirection = dynamic_cast<NXOpen::Direction* >(vectCol[0]);
directionToSet = extrudeDirection;
}
NXOpen::BlockStyler::PropertyList* PreviewToggleproplist = PreviewToggle->GetProperties();
preview = PreviewToggleproplist->GetLogical("Value");
delete PreviewToggleproplist;
createExtrude();
}
else if(block == StartLimit)
{
//---------Enter your code here-----------
NXOpen::BlockStyler::PropertyList* PreviewToggleproplist = PreviewToggle->GetProperties();
preview = PreviewToggleproplist->GetLogical("Value");
delete PreviewToggleproplist;
NXOpen::BlockStyler::PropertyList* StartLimitproplist = StartLimit->GetProperties();
startValueOfExt = StartLimitproplist->GetDouble("Value");
delete StartLimitproplist;
createExtrude();
}
else if(block == EndLimit)
{
//用户代码
NXOpen::BlockStyler::PropertyList* PreviewToggleproplist = PreviewToggle->GetProperties();
preview = PreviewToggleproplist->GetLogical("Value");
delete PreviewToggleproplist;
NXOpen::BlockStyler::PropertyList* EndLimitproplist = EndLimit->GetProperties();
endValueOfExt = EndLimitproplist->GetDouble("Value");
delete EndLimitproplist;
createExtrude();
}
else if(block == PreviewToggle)
{
//用户代码
NXOpen::BlockStyler::PropertyList* PreviewToggleproplist = PreviewToggle->GetProperties();
preview = PreviewToggleproplist->GetLogical("Value");
delete PreviewToggleproplist;
createExtrude();
}
else if(block == ResultButton)
{
//用户代码
createExtrude();
if (value)
{
setBoolean(false);
NXOpen::BlockStyler::PropertyList* ResultButtonproplist = ResultButton->GetProperties();
ResultButtonproplist->SetString("Bitmap", "undo");
ResultButtonproplist->SetString("Label", "Undo result");
delete ResultButtonproplist;
showOrCreate(0);
}
else
{
setBoolean(true);
NXOpen::BlockStyler::PropertyList* ResultButtonproplist = ResultButton->GetProperties();
ResultButtonproplist->SetString("Bitmap", "general_preview");
ResultButtonproplist->SetString("Label", "Show Result");
delete ResultButtonproplist;
if (feature1 != NULL)
{
visibility();
}
}
value = !value;
}
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
ExtrudewithPreview::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return 0;
}
//------------------------------------------------------------------------------
//Function Name: GetBlockProperties
//Description: Returns the propertylist of the specified BlockID
//------------------------------------------------------------------------------
PropertyList* ExtrudewithPreview::GetBlockProperties(const char *blockID)
{
return theDialog->GetBlockProperties(blockID);
}
//创建拉伸
void ExtrudewithPreview::createExtrude()
{
if (tagObjArray.size() > 0 && vectCol.size() > 0)
{
if (isFeatureCreated)
{
redefineFeature();
return;
}
NXOpen::Features::Feature* nullFeatures_Feature = NULL;
NXOpen::Features::ExtrudeBuilder* extrudeBuilder1 = workPart->Features()->CreateExtrudeBuilder(nullFeatures_Feature);
extrudeBuilder1->SetDistanceTolerance(0.0254);
extrudeBuilder1->SetDirection(extrudeDirection);
NXOpen::BlockStyler::PropertyList* StartLimitproplist = StartLimit->GetProperties();
int starvalueofext = (int)StartLimitproplist->GetDouble("Value");
delete StartLimitproplist;
extrudeBuilder1->Limits()->StartExtend()->Value()->SetRightHandSide (intToNXString(starvalueofext));
NXOpen::BlockStyler::PropertyList* EndLimitproplist = EndLimit->GetProperties();
int endValueOfExt = (int)EndLimitproplist->GetDouble("Value");
delete EndLimitproplist;
extrudeBuilder1->Limits()->EndExtend()->Value()->SetRightHandSide (intToNXString(endValueOfExt));
extrudeBuilder1->SetSection (dynamic_cast<NXOpen::Section* >(tagObjArray[0]));
feature1 = extrudeBuilder1->CommitFeature();
extrudeBuilder1->Destroy();
workPart->FacetedBodies()->DeleteTemporaryFacesAndEdges();
isFeatureCreated = true;
visibility();
}
}
//编辑拉伸
void ExtrudewithPreview::redefineFeature()
{
NXOpen::Features::Extrude* extrude1 = dynamic_cast<NXOpen::Features::Extrude* >(feature1);
NXOpen::Features::ExtrudeBuilder* extrudeBuilder2 =
workPart->Features()->CreateExtrudeBuilder(extrude1);
extrudeBuilder2->SetDistanceTolerance(0.0254);
extrudeBuilder2->SetDirection(extrudeDirection);
NXOpen::BlockStyler::PropertyList* StartLimitproplist = StartLimit->GetProperties();
double starvalueofext = StartLimitproplist->GetDouble("Value");
delete StartLimitproplist;
extrudeBuilder2->Limits()->StartExtend()->Value()->SetRightHandSide(doubleToNXString(starvalueofext));
NXOpen::BlockStyler::PropertyList* EndLimitproplist = EndLimit->GetProperties();
double endValueOfExt = EndLimitproplist->GetDouble("Value");
delete EndLimitproplist;
extrudeBuilder2->Limits()->EndExtend()->Value()->SetRightHandSide(doubleToNXString(endValueOfExt));
extrudeBuilder2->SetSection(dynamic_cast<NXOpen::Section* >(tagObjArray[0]));
markId1 = ExtrudewithPreview::theSession->SetUndoMark(NXOpen::Session::MarkVisibilityVisible, "Start Extrude");
feature1 = extrudeBuilder2->CommitFeature();
extrudeBuilder2->Destroy();
workPart->FacetedBodies()->DeleteTemporaryFacesAndEdges();
ExtrudewithPreview::theSession->UpdateManager()->DoUpdate(markId1);
ExtrudewithPreview::theSession->DeleteUndoMark(markId1, NULL);
visibility();
}
//设置显示
void ExtrudewithPreview::visibility()
{
if (preview)
{
NXOpen::Features::BodyFeature* bodyfeat =
dynamic_cast<NXOpen::Features::BodyFeature* >(feature1);
std::vector<NXOpen::Body* > featureBodies = bodyfeat->GetBodies();
NXOpen::DisplayModification* displayModification1 =
ExtrudewithPreview::theSession->DisplayManager()->NewDisplayModification();
displayModification1->SetApplyToAllFaces(true);
displayModification1->SetNewTranslucency(80);
std::vector<NXOpen::DisplayableObject* > objects1(featureBodies.size());
for (unsigned int ii = 0; ii < featureBodies.size(); ++ii)
{
NXOpen::Body* body = featureBodies[ii];
objects1[ii] = dynamic_cast<NXOpen::DisplayableObject* > (body);
}
displayModification1->Apply(objects1);
workPart->ModelingViews()->WorkView()->SetRenderingStyle(NXOpen::View::RenderingStyleTypeShadedWithEdges);
}
else
{
NXOpen::Features::BodyFeature* bodyfeat =
dynamic_cast<NXOpen::Features::BodyFeature* >(feature1);
std::vector<NXOpen::Body* > featureBodies = bodyfeat->GetBodies();
NXOpen::DisplayModification* displayModification1 =
ExtrudewithPreview::theSession->DisplayManager()->NewDisplayModification();
displayModification1->SetApplyToAllFaces(true);
displayModification1->SetNewTranslucency(100);
std::vector<NXOpen::DisplayableObject* > objects1(featureBodies.size());
for (unsigned int ii = 0; ii < featureBodies.size(); ++ii)
{
objects1[ii] = featureBodies[ii];
}
displayModification1->Apply(objects1);
workPart->ModelingViews()->WorkView()->SetRenderingStyle(NXOpen::View::RenderingStyleTypeShaded);
}
}
//显示创建
void ExtrudewithPreview::showOrCreate(int transparencyValue)
{
if (feature1 != NULL)
{
NXOpen::Features::BodyFeature* bodyfeat = dynamic_cast<NXOpen::Features::BodyFeature* >(feature1);
std::vector<NXOpen::Body *> featureBodies = bodyfeat->GetBodies();
NXOpen::DisplayModification* displayModification1
= ExtrudewithPreview::theSession->DisplayManager()->NewDisplayModification();
displayModification1->SetApplyToAllFaces(true);
displayModification1->SetNewTranslucency(transparencyValue);
std::vector<NXOpen::DisplayableObject* > objects1(featureBodies.size());
for (unsigned int ii = 0; ii < featureBodies.size(); ++ii)
{
objects1[ii] = featureBodies[ii];
}
displayModification1->Apply(objects1);
delete displayModification1;
}
}
//获取布尔值
void ExtrudewithPreview::setBoolean(bool value)
{
NXOpen::BlockStyler::PropertyList* SectionToExtrudeproplist = SectionToExtrude->GetProperties();
SectionToExtrudeproplist->SetLogical("Enable", value);
delete SectionToExtrudeproplist;
SectionToExtrudeproplist = NULL;
NXOpen::BlockStyler::PropertyList* DirectionToExtrudeproplist = DirectionToExtrude->GetProperties();
DirectionToExtrudeproplist->SetLogical("Enable", value);
delete DirectionToExtrudeproplist;
DirectionToExtrudeproplist = NULL;
NXOpen::BlockStyler::PropertyList* StartLimitproplist = StartLimit->GetProperties();
StartLimitproplist->SetLogical("Enable", value);
delete StartLimitproplist;
StartLimitproplist = NULL;
NXOpen::BlockStyler::PropertyList* EndLimitproplist = EndLimit->GetProperties();
EndLimitproplist->SetLogical("Enable", value);
delete EndLimitproplist;
EndLimitproplist = NULL;
NXOpen::BlockStyler::PropertyList* PreviewToggleproplist = PreviewToggle->GetProperties();
PreviewToggleproplist->SetLogical("Enable", value);
delete PreviewToggleproplist;
PreviewToggleproplist = NULL;
}
//转换
NXString ExtrudewithPreview::doubleToNXString(double value)
{
stringstream tmpValue;
tmpValue << setprecision(16) << value;
return NXString(tmpValue.str());
}
NXString ExtrudewithPreview::intToNXString(int value)
{
stringstream tmpValue;
tmpValue << setprecision(16) << value;
return NXString(tmpValue.str());
}