NXOpen 刻字

VS2015 NX12

//关键代码

void TheLettering::CreateText() //创建文字
{
try
{
//用户代码

//判断如果字已经创建则转到编辑字
if (isTextCreated)
{
GetUI(); //获取UI值
redefineText();
return;
}

GetUI(); //获取UI值

//();//获取原WCS

if (isFacesComponent==true)
{
//切换工作部件
Assemblies::Component *componentPart(dynamic_cast<NXOpen::Assemblies::Component *>(NXObjectManager::Get(componentFacePartTAG[0])));//组件
PartLoadStatus *partLoadStatus1;
theSession->Parts()->SetWorkComponent(componentPart, &partLoadStatus1);
workPart = theSession->Parts()->Work();
delete partLoadStatus1;
}

Features::Text *nullFeatures_Text(NULL);
Features::TextBuilder *textBuilder1;
textBuilder1 = workPart->Features()->CreateTextBuilder(nullFeatures_Text);
textBuilder1->FrameOnPath()->AnchorPosition()->Expression()->SetRightHandSide("50");
textBuilder1->SetScript(Features::TextBuilder::ScriptOptionsWestern);
textBuilder1->PlanarFrame()->SetAnchorLocation(GeometricUtilities::RectangularFrameBuilder::AnchorLocationTypeBottomCenter);
textBuilder1->PlanarFrame()->Length()->SetRightHandSide("5");

//设置字体的高度
textBuilder1->PlanarFrame()->Height()->SetRightHandSide(thefonthigh);

//设置长宽比例
textBuilder1->PlanarFrame()->SetWScale(100.0);
textBuilder1->PlanarFrame()->Shear()->SetRightHandSide("0");
textBuilder1->FrameOnPath()->AnchorPosition()->Expression()->SetRightHandSide("50");
textBuilder1->FrameOnPath()->Offset()->SetRightHandSide("0");
textBuilder1->FrameOnPath()->Length()->SetRightHandSide("1");
textBuilder1->FrameOnPath()->Height()->SetRightHandSide("2");

//设置字体
textBuilder1->SelectFont(thefont, Features::TextBuilder::ScriptOptionsWestern);

//设置要刻的文本
textBuilder1->SetTextString(theEngrave[0]);

Vector3d xDirection1(manip0->XAxis());
Vector3d yDirection1(manip0->YAxis());

Xform *xform1;
xform1 = workPart->Xforms()->CreateXform(manip0->Origin(), xDirection1, yDirection1, SmartObject::UpdateOptionWithinModeling, 1.0);

CartesianCoordinateSystem *cartesianCoordinateSystem1;
cartesianCoordinateSystem1 = workPart->CoordinateSystems()->CreateCoordinateSystem(xform1, SmartObject::UpdateOptionWithinModeling);
textBuilder1->PlanarFrame()->SetCoordinateSystem(cartesianCoordinateSystem1);
textBuilder1->PlanarFrame()->UpdateOnCoordinateSystem();

NXObject *textnXObject1;
textnXObject1 = textBuilder1->Commit();

textBuilder1->Destroy();

isTextCreated = true;


}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
TheLettering::theUI->NXMessageBox()->Show("创建文字", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}



//编辑文字
void TheLettering::redefineText()
{
try
{
Features::TextBuilder *textBuilder2;
textBuilder2 = workPart->Features()->CreateTextBuilder(text1);
textBuilder2->FrameOnPath()->AnchorPosition()->Expression()->SetRightHandSide("50");
textBuilder2->SetScript(Features::TextBuilder::ScriptOptionsWestern);
textBuilder2->PlanarFrame()->SetAnchorLocation(GeometricUtilities::RectangularFrameBuilder::AnchorLocationTypeBottomCenter);
textBuilder2->PlanarFrame()->Length()->SetRightHandSide("5");

//设置字体的高度
textBuilder2->PlanarFrame()->Height()->SetRightHandSide(thefonthigh);

//设置长宽比例
textBuilder2->PlanarFrame()->SetWScale(100.0);
textBuilder2->PlanarFrame()->Shear()->SetRightHandSide("0");
textBuilder2->FrameOnPath()->AnchorPosition()->Expression()->SetRightHandSide("50");
textBuilder2->FrameOnPath()->Offset()->SetRightHandSide("0");
textBuilder2->FrameOnPath()->Length()->SetRightHandSide("1");
textBuilder2->FrameOnPath()->Height()->SetRightHandSide("2");

//设置字体
textBuilder2->SelectFont(thefont, Features::TextBuilder::ScriptOptionsWestern);

//设置要刻的文本
textBuilder2->SetTextString(theEngrave[0]);

Vector3d xDirection1(manip0->XAxis());
Vector3d yDirection1(manip0->YAxis());

Xform *xform1;
xform1 = workPart->Xforms()->CreateXform(manip0->Origin(), xDirection1, yDirection1, SmartObject::UpdateOptionWithinModeling, 1.0);

CartesianCoordinateSystem *cartesianCoordinateSystem2;
cartesianCoordinateSystem2 = workPart->CoordinateSystems()->CreateCoordinateSystem(xform1, SmartObject::UpdateOptionWithinModeling);
textBuilder2->PlanarFrame()->SetCoordinateSystem(cartesianCoordinateSystem2);
textBuilder2->PlanarFrame()->UpdateOnCoordinateSystem();

markId1 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityVisible, "text");
NXObject *nXObject1;
nXObject1 = textBuilder2->Commit();
textBuilder2->Destroy();

theSession->UpdateManager()->DoUpdate(markId1);
theSession->DeleteUndoMark(markId1, NULL);

 }

catch(exception& ex)
{
//---- Enter your exception handling code here -----
TheLettering::theUI->NXMessageBox()->Show("编辑文字", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

void TheLettering::CreateExtrudeBuilder() //创建拉伸
{
try
{
enumTextTypeValue = enumTextType->GetProperties()->GetEnum("Value");

if (isExtrudeCreated)
{
//GetUI(); //获取UI值
EditExtrudeBuilder();
return;
}
//创建拉伸
Features::Feature *nullFeatures_Feature(NULL);
Features::ExtrudeBuilder *extrudeBuilder1;
extrudeBuilder1 = workPart->Features()->CreateExtrudeBuilder(nullFeatures_Feature);

//设置参数
//设置公差
extrudeBuilder1->SetDistanceTolerance(0.01);

//设置平面
Section *section1;
section1 = workPart->Sections()->CreateSection();
extrudeBuilder1->SetSection(section1);

//NXOpen::Features::Feature *text = dynamic_cast<Features::Text *>(nXObject1);
std::vector<Features::Feature *> Rulefeatures(1);
Rulefeatures[0] = text1;

CurveFeatureRule *curveFeatureRule1;
curveFeatureRule1= workPart->ScRuleFactory()->CreateRuleCurveFeature(Rulefeatures);

std::vector<SelectionIntentRule *> rules1(1);
rules1[0] = curveFeatureRule1;
NXObject *nullNXObject(NULL);
Point3d helpPoint1(0.0, 0.0, 0.0);
section1->AddToSection(rules1, nullNXObject, nullNXObject, nullNXObject, helpPoint1, Section::ModeCreate, false);

//设置方向
//获取方位器上的方向及原点
UF_initialize();
Point3d theOrigin = manip0->Origin();
Vector3d xDirection = manip0->XAxis();
Vector3d yDirection = manip0->YAxis();
double yuandian[3] = { theOrigin.X, theOrigin.Y, theOrigin.Z }; //方位器原点Point3d类型转double型
double ufun_X[3] = { xDirection.X, xDirection.Y, xDirection.Z }; //方位器X轴Vector3d类型转double型
double ufun_Y[3] = { yDirection.X, yDirection.Y, yDirection.Z }; //方位器Y轴Vector3d类型转double型
double ufun_Z[3];
UF_VEC3_cross( ufun_X, ufun_Y, ufun_Z ); //通过叉乘求Z轴
Vector3d zDirection1 ( ufun_Z[0], ufun_Z[1], ufun_Z[2] ); //Z轴double型转Vector3d 正向
Vector3d zDirection2 ( ufun_Z[0], ufun_Z[1], -ufun_Z[2] ); //Z轴double型转Vector3d 负向
UF_terminate();
//设置拉伸方向
Direction *direction1;
direction1 = workPart->Directions()->CreateDirection(manip0->Origin(), zDirection1, SmartObject::UpdateOptionWithinModeling);

//深度转Double型
ExtrudeDepth = atof(theDepth.GetLocaleText());

extrudeBuilder1->Limits()->StartExtend()->Value()->SetValue(0.00);//设置拉伸起始点
extrudeBuilder1->Limits()->EndExtend()->Value()->SetValue(ExtrudeDepth);//设置拉伸起终点

extrudeBuilder1->SetDirection(direction1);

//设置拉伸起始点和终点

ExtrudeFeature1 = extrudeBuilder1->CommitFeature();
extrudeBuilder1->Destroy();

isExtrudeCreated = true ;

visibility(); //设置显示
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
TheLettering::theUI->NXMessageBox()->Show("创建拉伸", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

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