怡宁塑胶模具设计

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

VS2015 NX12

 

关键代码

//找圆边(整圆)
void InsertPinVentingNew::findArcEdge()
{
try
{
workPart = theSession->Parts()->Work();

getOriginalWCS();//获取原WCS

UF_initialize();
CylFacesprototypeTag.clear();
edges.clear();
EdgesR1.clear();
bodyFaces.clear();
pinGTcylfaces1.clear();
cylfaceR1.clear();
arcedges1.clear();
arcedges2.clear();
//arcedges3.clear();
commoneFaces1.clear();
commoneFaces2.clear();


if (isComponent == true)
{
tag_t CylFacesprototype = UF_ASSEM_ask_prototype_of_occ(CylFaces[0]->Tag());//如果是装配选的部件面,找面的原型
CylFacesprototypeTag.push_back(CylFacesprototype);
}

//sprintf ( msg , "%d", CylFacesprototypeTag[0]);
//theSession->ListingWindow()->WriteLine(msg);

for (int i = 0; i < CylFaces.size(); i++)
{
if (isComponent == true)//如果是部件先的面需找面的原型
{
CylFacesprototype1 = (dynamic_cast<Face *>(NXObjectManager::Get(CylFacesprototypeTag[0]))); //选择的目标转换face
}
else if (isComponent == false)
{
CylFacesprototype1 = dynamic_cast<Face *>(CylFaces[i]);
}

edges = CylFacesprototype1->GetEdges();
//if ( face1->SolidFaceType() == Face::FaceTypeCylindrical)
//{
// theSession->ListingWindow()->WriteLine("圆柱面");
//}
}

//收集圆边和边的R
NXOpen::Edge::EdgeType edgetype;//2为圆边?
double arcR1[1];//圆边R
for (int i = 0; i < edges.size(); i++)
{
Edge *edge1 = edges[i];
edgetype = edge1->SolidEdgeType(); //获取类型

if (edgetype == 2)
{
arcedges1.push_back(edges[i]);//收集圆边
//获取边的信息
UF_EVAL_p_t evaluator1;
UF_EVAL_initialize(edge1->Tag(), &evaluator1);//使用UF_EVAL_ask_arc()函数需要先进行初始化

UF_EVAL_arc_t arcCoords1;
UF_EVAL_ask_arc(evaluator1, &arcCoords1);////arc结构体中有曲线的参数(圆中,半径,是否整圆等相关信息)

//圆边R
arcR1[0] = arcCoords1.radius;
EdgesR1.push_back(arcR1[0]);//收集圆边半径
edge1->Highlight();//高亮
}
}

//通过面类型和面的半径过滤出针挂台的面
if (isComponent == true)//如果是部件先的面需找面的原型
{
CylFacesprototype1 = (dynamic_cast<Face *>(NXObjectManager::Get(CylFacesprototypeTag[0]))); //选择的目标转换face
}
else if (isComponent == false)
{
CylFacesprototype1 = dynamic_cast<Face *>(CylFaces[0]);
}
//sprintf ( msg , "%d",CylFacesprototype1->Tag());
//theSession->ListingWindow()->WriteLine(msg);
Body *pinbody = CylFacesprototype1->GetBody();//面找体,找到针体
bodyFaces = pinbody->GetFaces();//获取到针体的所有面

int Type1;
double Point1[3];
double Dir1[3];
double Box1[6];
double Radius1[3];
double RadData1[3];
int NormDir1;
//数据转换
/*char edgeRmsg [256];*/
//sprintf ( edgeRmsg , "%.2f" , EdgesR1[0]);
//theSession->ListingWindow()->WriteLine(edgeRmsg);
for (int i = 0; i < bodyFaces.size(); i++)
{
Face *CylFace = bodyFaces[i];
if (CylFace->SolidFaceType() == Face::FaceTypeCylindrical)
{
//获取面参数
UF_MODL_ask_face_data(CylFace->Tag(), &Type1, Point1, Dir1, Box1, Radius1, RadData1, &NormDir1);
//数据转换
char GTfacesRmsg[256];
sprintf(GTfacesRmsg, "%.2f", Radius1[0]);
//theSession->ListingWindow()->WriteLine(GTfacesRmsg);
if (Radius1[0] > EdgesR1[0] + 0.01)
{
pinGTcylfaces1.push_back(bodyFaces[i]);
cylfaceR1.push_back(Radius1[0]);
//theSession->ListingWindow()->WriteLine(GTfacesRmsg);
//CylFace->Highlight();//高亮
}
}
}

//通过圆边到挂台柱面的距离收集针柱面底面圆边
double distance1;
double distance2;

sort(cylfaceR1.begin(), cylfaceR1.end());//排序,从小到大排
reverse(cylfaceR1.begin(), cylfaceR1.end());//reverse函数功能是逆序(或反转)

if (pinGTcylfaces1.size() > 0)
{

distance1 = cylfaceR1[0] - EdgesR1[0];
//数据转换
char Distance1msg[256];
sprintf(Distance1msg, "%.2f", distance1);
//theSession->ListingWindow()->WriteLine("挂台半径和针体半径差:");
//theSession->ListingWindow()->WriteLine(Distance1msg);

for (int i = 0; i < arcedges1.size(); i++)
{
Edge *edge2 = arcedges1[i];
for (int num = 0; num < pinGTcylfaces1.size(); num++)
{
Face *faceGTcyl1 = dynamic_cast<Face *>(pinGTcylfaces1[num]);
//sprintf ( msg , "%d",faceGTcyl1->Tag());
//theSession->ListingWindow()->WriteLine(msg);
NXObject *nullNXObject(NULL);
MeasureDistanceBuilder *measureDistanceBuilder1;
measureDistanceBuilder1 = workPart->MeasureManager()->CreateMeasureDistanceBuilder(nullNXObject);

measureDistanceBuilder1->SetMtype(MeasureDistanceBuilder::MeasureTypeMinimum);
measureDistanceBuilder1->Object1()->SetValue(edge2);

measureDistanceBuilder1->Object2()->SetValue(faceGTcyl1);

Unit *unit1(dynamic_cast<Unit *>(workPart->UnitCollection()->FindObject("MilliMeter")));
MeasureDistance *measureDistance1;
measureDistance1 = workPart->MeasureManager()->NewDistance(unit1, MeasureManager::MeasureTypeMinimum, edge2, faceGTcyl1);

Measure *measure1;
//measure1 = measureDistance1->CreateFeature();//只是获取数值,所以不创建特征

distance2 = measureDistance1->Value();//获取测量的值
//数据转换
char Distance2msg[256];
sprintf(Distance2msg, "%.2f", distance2);
//theSession->ListingWindow()->WriteLine(Distance2msg);
if (strcmp(Distance1msg, Distance2msg) == 0) // Radius[0] * 2 && atof(Distancemsg)> 0.00
{
arcedges2.push_back(arcedges1[i]);
//theSession->ListingWindow()->WriteLine("挂台柱面和针体距离:");
//theSession->ListingWindow()->WriteLine(Distance2msg);
//edge2->Highlight();
}
measureDistanceBuilder1->Destroy();
}
}

//找柱面和挂台面的共面
Edge *commonedge = dynamic_cast <Edge *> (arcedges2[0]);
commoneFaces1 = commonedge->GetFaces();
for (int i = 0; i < commoneFaces1.size(); i++)
{
Face *commonface = commoneFaces1[i];
if (commonface->SolidFaceType() == Face::FaceTypePlanar)
{
commoneFaces2.push_back(commoneFaces1[i]);
//commonface->Highlight();
}

}

//获取共面参数
int Type2;
double Point2[3];
double Box2[6];
double Radius2[3];
double RadData2[3];
int NormDir2;
if (commoneFaces2.size() > 0)
{
UF_MODL_ask_face_data(commoneFaces2[0]->Tag(), &Type2, Point2, commoneFacesDir2, Box2, Radius2, RadData2, &NormDir2);

commoneFacesx_axis[0] = Box2[0];
commoneFacesx_axis[1] = Box2[1];
commoneFacesx_axis[2] = Box2[2];
commoneFacesy_axis[0] = Box2[3];
commoneFacesy_axis[1] = Box2[4];
commoneFacesy_axis[2] = Box2[5];
}
else
{
theUI->NXMessageBox()->Show("提示", NXOpen::NXMessageBox::DialogTypeInformation, "必须选择针体柱面 并且 针体柱面与挂台与挂台之间不允许 倒角/倒圆角");
return;
}
//sprintf(msg, "面X:%.2f %.2f %.2f",commoneFacesx_axis[0],commoneFacesx_axis[1],commoneFacesx_axis[2]);
//theSession->ListingWindow()->WriteLine( msg);
//sprintf(msg, "面y:%.2f %.2f %.2f",commoneFacesy_axis[0],commoneFacesy_axis[1],commoneFacesy_axis[2]);
//theSession->ListingWindow()->WriteLine( msg);
//sprintf(msg, "dir:%.2f %.2f %.2f",commoneFacesDir2[0],commoneFacesDir2[1],commoneFacesDir2[2]);
/*theSession->ListingWindow()->WriteLine( msg); */

}
else
{
theUI->NXMessageBox()->Show("提示", NXOpen::NXMessageBox::DialogTypeInformation, "必须选择针体柱面 并且 针体柱面与挂台与挂台之间不允许 倒角/倒圆角");
return;
}

 

UF_terminate();

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
InsertPinVentingNew::theUI->NXMessageBox()->Show("找圆边(整圆)", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//创建镶针排气
void InsertPinVentingNew::createpinventing()
{
try
{
UF_initialize();
//for (int i = 0 ; i < arcedges2.size() ; i ++ )
//{
// sprintf ( msg , "%d",arcedges2[i]->Tag());
// theSession->ListingWindow()->WriteLine(msg);
//}

if (!arcedges2.size() == 0 && !commoneFaces2.size() == 0)
{
//sprintf ( msg , "%d",arcedges2[0]->Tag());
//theSession->ListingWindow()->WriteLine(msg);

Edge *edge2 = arcedges2[0];
//获取边的信息
UF_EVAL_p_t evaluator;
UF_EVAL_initialize(edge2->Tag(), &evaluator);//使用UF_EVAL_ask_arc()函数需要先进行初始化

UF_EVAL_ask_arc(evaluator, &arcCoords);//arc结构体中有曲线的参数(圆中,半径,是否整圆等相关信息)
//圆边中心
arcCenter[0] = arcCoords.center[0];
arcCenter[1] = arcCoords.center[1];
arcCenter[2] = arcCoords.center[2];
//圆边X轴
arcx_axis[0] = arcCoords.x_axis[0];
arcx_axis[1] = arcCoords.x_axis[1];
arcx_axis[2] = arcCoords.x_axis[2];
//圆边y轴
arcy_axis[0] = arcCoords.y_axis[0];
arcy_axis[1] = arcCoords.y_axis[1];
arcy_axis[2] = arcCoords.y_axis[2];
//圆边Z轴
UF_VEC3_cross(arcx_axis, arcy_axis, arcz_axis);
//圆边R
arcR[0] = arcCoords.radius;

//sprintf(msg, "边的中心:%.2f %.2f %.2f",arcCenter[0],arcCenter[1],arcCenter[2]);
//theSession->ListingWindow()->WriteLine( msg);
//sprintf(msg, "边半径:%.2f ",arcR [0]);
//theSession->ListingWindow()->WriteLine( msg);
//sprintf(msg, "边的X轴:%.2f %.2f %.2f",arcx_axis[0],arcx_axis[1],arcx_axis[2]);
//theSession->ListingWindow()->WriteLine( msg);
//sprintf(msg, "边的y轴:%.2f %.2f %.2f",arcy_axis[0],fabs(arcy_axis[1]),arcy_axis[2]);
//theSession->ListingWindow()->WriteLine( msg);
//sprintf(msg, "边的z轴:%.2f %.2f %.2f",arcz_axis[0],arcz_axis[1],arcz_axis[2]);
//theSession->ListingWindow()->WriteLine( msg);

//设置点和方向
Point3d origin1(arcCoords.center[0], arcCoords.center[1], arcCoords.center[2]);
Vector3d xDirection1(arcCoords.x_axis[0], arcCoords.x_axis[1], arcCoords.x_axis[2]);
Vector3d yDirection1(arcCoords.y_axis[0], fabs(arcCoords.y_axis[1]), arcCoords.y_axis[2]);//绝对值fabs
Vector3d zDirection1(commoneFacesDir2[0], commoneFacesDir2[1], commoneFacesDir2[2]);

//创建新WCS

Matrix3x3 matrix1;
matrix1.Xx = xDirection1.X;
matrix1.Xy = xDirection1.Y;
matrix1.Xz = xDirection1.Z;
matrix1.Yx = yDirection1.X;
matrix1.Yy = yDirection1.Y;
matrix1.Yz = yDirection1.Z;
matrix1.Zx = zDirection1.X;
matrix1.Zy = zDirection1.Y;
matrix1.Zz = zDirection1.Z;
workPart->WCS()->SetOriginAndMatrix(origin1, matrix1);

}
else
{
//theUI->NXMessageBox()->Show("提示", NXOpen::NXMessageBox::DialogTypeInformation, "请检查针体面与挂台是否有倒角或倒圆角");
return;
}


UF_terminate();

measureDistance();//测量距离
createCylinder1();//创建圆柱和方体
deletebodyparms();//移除参数
BooleanSubtract();//布尔减得出排气槽

retcoverWCS();//恢复WCS

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
InsertPinVentingNew::theUI->NXMessageBox()->Show("创建镶针排气", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//测量距离
void InsertPinVentingNew::measureDistance()
{
try
{
measureValue.clear();
double distance;

for (int i = 0; i < edges.size(); i++)
{
Edge *edge1 = edges[i];
NXObject *nullNXObject(NULL);
MeasureDistanceBuilder *measureDistanceBuilder1;
measureDistanceBuilder1 = workPart->MeasureManager()->CreateMeasureDistanceBuilder(nullNXObject);

measureDistanceBuilder1->SetMtype(MeasureDistanceBuilder::MeasureTypeMinimum);
measureDistanceBuilder1->Object1()->SetValue(edge1);

measureDistanceBuilder1->Object2()->SetValue(commoneFaces2[0]);

Unit *unit1(dynamic_cast<Unit *>(workPart->UnitCollection()->FindObject("MilliMeter")));
MeasureDistance *measureDistance1;
measureDistance1 = workPart->MeasureManager()->NewDistance(unit1, MeasureManager::MeasureTypeMinimum, edge1, commoneFaces2[0]);

Measure *measure1;
//measure1 = measureDistance1->CreateFeature();//只是获取数值,所以不创建特征

distance = measureDistance1->Value();//获取测量的值
if (distance > 0)
{
measureValue.push_back(distance);//获取的值塞到容器
}
measureDistanceBuilder1->Destroy();
}

sort(measureValue.begin(), measureValue.end());//排序,从小到大排

////打印测量到的距离
//for ( int i = 0 ; i < measureValue.size(); i++ )
//{
// sprintf ( msg , "%.2f",measureValue[i]);
// theSession->ListingWindow()->WriteLine(msg);
//}

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
InsertPinVentingNew::theUI->NXMessageBox()->Show("测量距离", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//创建圆柱和方体
void InsertPinVentingNew::createCylinder1()
{
try
{
//设置点和方向
Point3d origin1(arcCoords.center[0], arcCoords.center[1], arcCoords.center[2]);
Vector3d xDirection1(arcCoords.x_axis[0], arcCoords.x_axis[1], arcCoords.x_axis[2]);
Vector3d yDirection1(arcCoords.y_axis[0], fabs(arcCoords.y_axis[1]), arcCoords.y_axis[2]);//绝对值fabs
Vector3d zDirection1(commoneFacesDir2[0], commoneFacesDir2[1], commoneFacesDir2[2]);

//创建圆柱1
NXOpen::Features::Feature *null_cylinder_feature1(NULL);
Features::CylinderBuilder *cylinderBuilder1;
cylinderBuilder1 = workPart->Features()->CreateCylinderBuilder(null_cylinder_feature1);

char Diameter1[256];
sprintf(Diameter1, "%.2f", arcR[0] * 2 + 0.2);
cylinderBuilder1->Diameter()->SetRightHandSide(Diameter1);
char Height1[256];
sprintf(Height1, "%.2f", measureValue[0] * 2 + 100);
cylinderBuilder1->Height()->SetRightHandSide(Height1);

cylinderBuilder1->SetOrigin(origin1);

cylinderBuilder1->SetDirection(zDirection1);
cylinder_feature1 = cylinderBuilder1->CommitFeature(); //创建圆柱体1
cylinderBuilder1->Destroy();//释放cylinderBuilder1

//Features::Cylinder *cylinder1(dynamic_cast<Features::Cylinder *>(workPart->Features()->FindObject(cylinder_feature1->JournalIdentifier())));
Body *body1(dynamic_cast<Body *>(workPart->Bodies()->FindObject(cylinder_feature1->JournalIdentifier())));
vector<Face *> faces1;//定义面容器
vector<Face *> offsetfaces1;//定义平面容器
faces1 = body1->GetFaces();

for (int i = 0; i < faces1.size(); i++)
{
Face *face1 = faces1[i];
if (face1->SolidFaceType() == Face::FaceTypePlanar) //FaceTypePlanar平面 FaceTypeCylindrical柱面
{
offsetfaces1.push_back(faces1[i]);
}
}

char offsetHeight1[256];
sprintf(offsetHeight1, "-%.2f", measureValue[0] - expressionW1->Value());
for (int i = 0; i < offsetfaces1.size(); i++)
{
Face *face2 = offsetfaces1[i];

//偏置面
Features::Feature *offsetFace_Feature1(NULL);
Features::OffsetFaceBuilder *offsetFaceBuilder1;
offsetFaceBuilder1 = workPart->Features()->CreateOffsetFaceBuilder(offsetFace_Feature1);
offsetFaceBuilder1->Distance()->SetRightHandSide(offsetHeight1);
std::vector<Face *> boundaryFaces1(0);
FaceTangentRule *faceTangentRule1;
faceTangentRule1 = workPart->ScRuleFactory()->CreateRuleFaceTangent(face2, boundaryFaces1, 0.001);
std::vector<SelectionIntentRule *> rules1(1);
rules1[0] = faceTangentRule1;
offsetFaceBuilder1->FaceCollector()->ReplaceRules(rules1, false);
NXObject *nXObject1;
nXObject1 = offsetFaceBuilder1->Commit();
Expression *expression1(offsetFaceBuilder1->Distance());
offsetFaceBuilder1->Destroy();

}

//创建圆柱2
NXOpen::Features::Feature *null_cylinder_feature2(NULL);
Features::CylinderBuilder *cylinderBuilder2;
cylinderBuilder2 = workPart->Features()->CreateCylinderBuilder(null_cylinder_feature2);

char Diameter2[256];
sprintf(Diameter2, "%.2f", arcR[0] * 2 - expressionDdepth->Value() * 2);

cylinderBuilder2->Diameter()->SetRightHandSide(Diameter2);
cylinderBuilder2->Height()->SetRightHandSide(Height1);

cylinderBuilder2->SetOrigin(origin1);

cylinderBuilder2->SetDirection(zDirection1);
cylinder_feature2 = cylinderBuilder2->CommitFeature(); //创建圆柱体2
cylinderBuilder2->Destroy();//释放cylinderBuilder1

//布尔1
Features::BooleanFeature *nullFeatures_BooleanFeature1(NULL);
Features::BooleanBuilder *booleanBuilder1;
booleanBuilder1 = workPart->Features()->CreateBooleanBuilderUsingCollector(nullFeatures_BooleanFeature1);
ScCollector *scCollector1;
scCollector1 = booleanBuilder1->ToolBodyCollector();
GeometricUtilities::BooleanRegionSelect *booleanRegionSelect1;
booleanRegionSelect1 = booleanBuilder1->BooleanRegionSelect();
booleanBuilder1->SetTolerance(0.001);
booleanBuilder1->SetOperation(Features::Feature::BooleanTypeSubtract);//设置布尔减
//第一个体
bool added1;
added1 = booleanBuilder1->Targets()->Add(body1);
std::vector<TaggedObject *> targets1(1);
targets1[0] = body1;
booleanRegionSelect1->AssignTargets(targets1);
ScCollector *scCollector2;
scCollector2 = workPart->ScCollectors()->CreateCollector();
std::vector<Body *> bodies2(1);
//第二个体
Body *body2(dynamic_cast<Body *>(workPart->Bodies()->FindObject(cylinder_feature2->JournalIdentifier())));
bodies2[0] = body2;
BodyDumbRule *bodyDumbRule1;
bodyDumbRule1 = workPart->ScRuleFactory()->CreateRuleBodyDumb(bodies2);
std::vector<SelectionIntentRule *> rules1(1);
rules1[0] = bodyDumbRule1;
scCollector2->ReplaceRules(rules1, false);
booleanBuilder1->SetToolBodyCollector(scCollector2);
std::vector<TaggedObject *> targets2(1);
targets2[0] = body1;
booleanRegionSelect1->AssignTargets(targets2);
NXObject *nXObject1;
nXObject1 = booleanBuilder1->Commit();
booleanBuilder1->Destroy(); //释放

//创建圆柱3
NXOpen::Features::Feature *null_cylinder_feature3(NULL);
Features::CylinderBuilder *cylinderBuilder3;
cylinderBuilder3 = workPart->Features()->CreateCylinderBuilder(null_cylinder_feature3);

cylinderBuilder3->Diameter()->SetRightHandSide(Diameter1);
char Height3[256];
sprintf(Height3, "%.2f", (measureValue[0] - expressionW1->Value() - expressionW2->Value() / 2) * 2);//+ expressionW2->Value() - expressionW1->Value() - expressionW2->Value()
cylinderBuilder3->Height()->SetRightHandSide(Height3);

cylinderBuilder3->SetOrigin(origin1);

cylinderBuilder3->SetDirection(zDirection1);
cylinder_feature3 = cylinderBuilder3->CommitFeature(); //创建圆柱体3
cylinderBuilder3->Destroy();//释放cylinderBuilder1

Body *body3(dynamic_cast<Body *>(workPart->Bodies()->FindObject(cylinder_feature3->JournalIdentifier())));
vector<Face *> faces2;//定义面容器
vector<Face *> offsetfaces2;//定义平面容器
faces2 = body3->GetFaces();

for (int i = 0; i < faces2.size(); i++)
{
Face *face3 = faces2[i];
if (face3->SolidFaceType() == Face::FaceTypePlanar) //FaceTypePlanar平面 FaceTypeCylindrical柱面
{
offsetfaces2.push_back(faces2[i]);

}
}

char offsetHeight2[256];
sprintf(offsetHeight2, "-%.2f", measureValue[0] - expressionW1->Value() - expressionW2->Value());
for (int i = 0; i < offsetfaces2.size(); i++)
{
Face *face4 = offsetfaces2[i];

//偏置面
Features::Feature *offsetFace_Feature2(NULL);
Features::OffsetFaceBuilder *offsetFaceBuilder2;
offsetFaceBuilder2 = workPart->Features()->CreateOffsetFaceBuilder(offsetFace_Feature2);
offsetFaceBuilder2->Distance()->SetRightHandSide(offsetHeight2);
std::vector<Face *> boundaryFaces2(0);
FaceTangentRule *faceTangentRule2;
faceTangentRule2 = workPart->ScRuleFactory()->CreateRuleFaceTangent(face4, boundaryFaces2, 0.001);
std::vector<SelectionIntentRule *> rules2(1);
rules2[0] = faceTangentRule2;
offsetFaceBuilder2->FaceCollector()->ReplaceRules(rules2, false);
NXObject *nXObject2;
nXObject2 = offsetFaceBuilder2->Commit();
Expression *expression2(offsetFaceBuilder2->Distance());
offsetFaceBuilder2->Destroy();

}

//创建圆柱4
NXOpen::Features::Feature *null_cylinder_feature4(NULL);
Features::CylinderBuilder *cylinderBuilder4;
cylinderBuilder4 = workPart->Features()->CreateCylinderBuilder(null_cylinder_feature4);

char Diameter4[256];
sprintf(Diameter4, "%.2f", arcR[0] * 2 - expressionT->Value() * 2);

cylinderBuilder4->Diameter()->SetRightHandSide(Diameter4);
cylinderBuilder4->Height()->SetRightHandSide(Height1);

cylinderBuilder4->SetOrigin(origin1);

cylinderBuilder4->SetDirection(zDirection1);
cylinder_feature4 = cylinderBuilder4->CommitFeature(); //创建圆柱体4
cylinderBuilder4->Destroy();//释放cylinderBuilder1

//布尔2
Features::BooleanFeature *nullFeatures_BooleanFeature2(NULL);
Features::BooleanBuilder *booleanBuilder2;
booleanBuilder2 = workPart->Features()->CreateBooleanBuilderUsingCollector(nullFeatures_BooleanFeature2);
ScCollector *scCollector3;
scCollector3 = booleanBuilder2->ToolBodyCollector();
GeometricUtilities::BooleanRegionSelect *booleanRegionSelect2;
booleanRegionSelect2 = booleanBuilder2->BooleanRegionSelect();
booleanBuilder2->SetTolerance(0.001);
booleanBuilder2->SetOperation(Features::Feature::BooleanTypeSubtract);//设置布尔减
//第三个体
bool added2;
added2 = booleanBuilder2->Targets()->Add(body3);
std::vector<TaggedObject *> targets3(1);
targets3[0] = body3;
booleanRegionSelect2->AssignTargets(targets3);
ScCollector *scCollector4;
scCollector4 = workPart->ScCollectors()->CreateCollector();
//第四个体
std::vector<Body *> bodies4(1);
Body *body4(dynamic_cast<Body *>(workPart->Bodies()->FindObject(cylinder_feature4->JournalIdentifier())));
bodies4[0] = body4;
BodyDumbRule *bodyDumbRule4;
bodyDumbRule4 = workPart->ScRuleFactory()->CreateRuleBodyDumb(bodies4);
std::vector<SelectionIntentRule *> rules4(1);
rules4[0] = bodyDumbRule4;
scCollector4->ReplaceRules(rules4, false);
booleanBuilder2->SetToolBodyCollector(scCollector4);
std::vector<TaggedObject *> targets4(1);
targets4[0] = body3;
booleanRegionSelect2->AssignTargets(targets4);
NXObject *nXObject3;
nXObject3 = booleanBuilder2->Commit();
booleanBuilder2->Destroy(); //释放

//创建方体
NXOpen::Features::Feature *nullNXOpen_blockfeature1 = NULL; //创建feature
NXOpen::Features::BlockFeatureBuilder *blockFeatureBuilder1;
blockFeatureBuilder1 = workPart->Features()->CreateBlockFeatureBuilder(nullNXOpen_blockfeature1);

//设置创建立方体的类型
blockFeatureBuilder1->SetType(NXOpen::Features::BlockFeatureBuilder::TypesOriginAndEdgeLengths);

setBlockOrigin(blockFeatureBuilder1); //指定创建块的点

//blockFeatureBuilder1->SetOrientation( xDirection1, yDirection1 ); //设置块的方位

char blockLength1[256];
sprintf(blockLength1, "%.2f", arcR[0] * 4);
char blockWidth1[256];
sprintf(blockWidth1, "%.2f", arcR[0]);
char blockHeight1[256];
sprintf(blockHeight1, "%.2f", measureValue[0] - expressionW1->Value() - expressionW2->Value() + expressionT->Value());

blockFeatureBuilder1->SetLength(blockLength1);
blockFeatureBuilder1->SetWidth(blockWidth1);
blockFeatureBuilder1->SetHeight(blockHeight1);

//创建特征
blockfeature1 = blockFeatureBuilder1->CommitFeature();
blockFeatureBuilder1->Destroy();//释放blockFeatureBuilder1

//布尔3
Features::BooleanFeature *nullFeatures_BooleanFeature3(NULL);
Features::BooleanBuilder *booleanBuilder3;
booleanBuilder3 = workPart->Features()->CreateBooleanBuilderUsingCollector(nullFeatures_BooleanFeature3);
ScCollector *scCollector6;
scCollector6 = booleanBuilder3->ToolBodyCollector();
GeometricUtilities::BooleanRegionSelect *booleanRegionSelect3;
booleanRegionSelect3 = booleanBuilder3->BooleanRegionSelect();
booleanBuilder3->SetTolerance(0.001);
booleanBuilder3->SetOperation(Features::Feature::BooleanTypeUnite);//设置布尔加
//第三个体
bool added3;
added3 = booleanBuilder3->Targets()->Add(body3);
std::vector<TaggedObject *> targets5(1);
targets5[0] = body3;
booleanRegionSelect3->AssignTargets(targets5);
ScCollector *scCollector7;
scCollector7 = workPart->ScCollectors()->CreateCollector();
//第五个体
Body *body5(dynamic_cast<Body *>(workPart->Bodies()->FindObject(blockfeature1->JournalIdentifier())));
std::vector<Body *> bodies6(1);
bodies6[0] = body5;
BodyDumbRule *bodyDumbRule6;
bodyDumbRule6 = workPart->ScRuleFactory()->CreateRuleBodyDumb(bodies6);
std::vector<SelectionIntentRule *> rules6(1);
rules6[0] = bodyDumbRule6;
scCollector7->ReplaceRules(rules6, false);
booleanBuilder3->SetToolBodyCollector(scCollector7);
std::vector<TaggedObject *> targets6(1);
targets6[0] = body3;
booleanRegionSelect3->AssignTargets(targets6);
NXObject *nXObject6;
nXObject6 = booleanBuilder3->Commit();
booleanBuilder3->Destroy(); //释放

//布尔4
Features::BooleanFeature *nullFeatures_BooleanFeature4(NULL);
Features::BooleanBuilder *booleanBuilder4;
booleanBuilder4 = workPart->Features()->CreateBooleanBuilderUsingCollector(nullFeatures_BooleanFeature4);
ScCollector *scCollector8;
scCollector8 = booleanBuilder4->ToolBodyCollector();
GeometricUtilities::BooleanRegionSelect *booleanRegionSelect4;
booleanRegionSelect4 = booleanBuilder4->BooleanRegionSelect();
booleanBuilder4->SetTolerance(0.001);
booleanBuilder4->SetOperation(Features::Feature::BooleanTypeUnite);//设置布尔加
//第一个体
bool added4;
added4 = booleanBuilder4->Targets()->Add(body1);
std::vector<TaggedObject *> targets7(1);
targets7[0] = body1;
booleanRegionSelect4->AssignTargets(targets7);
ScCollector *scCollector9;
scCollector9 = workPart->ScCollectors()->CreateCollector();
//第三个体
std::vector<Body *> bodies8(1);
bodies8[0] = body3;
BodyDumbRule *bodyDumbRule8;
bodyDumbRule8 = workPart->ScRuleFactory()->CreateRuleBodyDumb(bodies8);
std::vector<SelectionIntentRule *> rules8(1);
rules8[0] = bodyDumbRule8;
scCollector9->ReplaceRules(rules8, false);
booleanBuilder4->SetToolBodyCollector(scCollector9);
std::vector<TaggedObject *> targets8(1);
targets8[0] = body1;
booleanRegionSelect4->AssignTargets(targets8);
NXObject *nXObject8;
nXObject8 = booleanBuilder4->Commit();
booleanBuilder4->Destroy(); //释放

visibility1();//设置显示

 

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

//设置方体的原点
void InsertPinVentingNew::setBlockOrigin(Features::BlockFeatureBuilder * bulider)
{
try
{
//---- Enter your callback code here -----

NXOpen::CartesianCoordinateSystem* WcsData = workPart->WCS()->CoordinateSystem();
Point3d WcsOrigin = workPart->WCS()->Origin();
NXOpen::Vector3d xDirection;
NXOpen::Vector3d yDirection;
WcsData->GetDirections(&xDirection, &yDirection);

//sprintf (msg, "WCS的原点:%.2f %.2f %.2f ", WcsOrigin.X , WcsOrigin.Y,WcsOrigin.Z);
//theSession->ListingWindow()->WriteLine(msg);

double yuandian[3] = { WcsOrigin.X , WcsOrigin.Y, WcsOrigin.Z }; //+10
double ufun_X[3] = { xDirection.X, xDirection.Y , xDirection.Z };
double ufun_Y[3] = { yDirection.X, yDirection.Y , yDirection.Z };
double ufun_Z[3];

UF_initialize();

UF_VEC3_cross(ufun_X, ufun_Y, ufun_Z);
//左下角点
double newpoint1[3], newpoint2[3], newpoint3[3];
UF_VEC3_affine_comb(yuandian, -arcR[0] * 2, ufun_X, newpoint1); // + arcR[0]*2
UF_VEC3_affine_comb(newpoint1, -arcR[0] * 2 + expressionT->Value(), ufun_Y, newpoint2);
UF_VEC3_affine_comb(newpoint2, -expressionT->Value(), ufun_Z, newpoint3);//+ measureValue[0] - expressionW1->Value()-expressionW2->Value()
Point3d newOrigin(newpoint3[0], newpoint3[1], newpoint3[2]);
bulider->SetOrigin(newOrigin);

UF_terminate();

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
InsertPinVentingNew::theUI->NXMessageBox()->Show("设置方体的原点", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//设置假体显示1
void InsertPinVentingNew::visibility1()
{
try
{
//设置镶件假体显示

NXOpen::Features::BodyFeature* bodyfeat = dynamic_cast<NXOpen::Features::BodyFeature* >(cylinder_feature1); //特征转换body
std::vector<NXOpen::Body* > featureBodies = bodyfeat->GetBodies();

NXOpen::DisplayModification* displayModification1 = theSession->DisplayManager()->NewDisplayModification();
displayModification1->SetApplyToAllFaces(true);
displayModification1->SetNewColor(1);
displayModification1->SetNewTranslucency(0);

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);

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
InsertPinVentingNew::theUI->NXMessageBox()->Show("设置假体显示1", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

 

//移除参数

void InsertPinVentingNew::deletebodyparms()
{
try
{
//---- Enter your callback code here -----

RemoveParametersbody1 = (dynamic_cast<Body *>(workPart->Bodies()->FindObject(cylinder_feature1->JournalIdentifier())));

//移除参数
Features::RemoveParametersBuilder *removeParametersBuilder1;
removeParametersBuilder1 = workPart->Features()->CreateRemoveParametersBuilder();
bool added1;
added1 = removeParametersBuilder1->Objects()->Add(RemoveParametersbody1);

NXObject *nXObject1;
nXObject1 = removeParametersBuilder1->Commit();
removeParametersBuilder1->Destroy();//释放

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
InsertPinVentingNew::theUI->NXMessageBox()->Show("移除参数和删除基准面", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//布尔减得出排气槽

void InsertPinVentingNew::BooleanSubtract()
{
try
{
//获取UI选取的圆柱面
std::vector<TaggedObject *> faces = selection0->GetSelectedObjects(); //得到圆柱面
Face *face1 = dynamic_cast<Face *>(faces[0]);
Body *pinbody = face1->GetBody();//面找体,找到针体

Features::BooleanFeature *nullFeatures_BooleanFeature1(NULL);
Features::BooleanBuilder *booleanBuilder1;
booleanBuilder1 = workPart->Features()->CreateBooleanBuilderUsingCollector(nullFeatures_BooleanFeature1);
ScCollector *scCollector1;
scCollector1 = booleanBuilder1->ToolBodyCollector();
GeometricUtilities::BooleanRegionSelect *booleanRegionSelect1;
booleanRegionSelect1 = booleanBuilder1->BooleanRegionSelect();
booleanBuilder1->SetTolerance(0.001);
booleanBuilder1->SetOperation(Features::Feature::BooleanTypeSubtract);//设置布尔减
//针体
bool added1;
added1 = booleanBuilder1->Targets()->Add(pinbody);
std::vector<TaggedObject *> targets1(1);
targets1[0] = pinbody;
booleanRegionSelect1->AssignTargets(targets1);
ScCollector *scCollector2;
scCollector2 = workPart->ScCollectors()->CreateCollector();
std::vector<Body *> bodies2(1);
//排气假体
bodies2[0] = RemoveParametersbody1;
BodyDumbRule *bodyDumbRule1;
bodyDumbRule1 = workPart->ScRuleFactory()->CreateRuleBodyDumb(bodies2);
std::vector<SelectionIntentRule *> rules1(1);
rules1[0] = bodyDumbRule1;
scCollector2->ReplaceRules(rules1, false);
booleanBuilder1->SetToolBodyCollector(scCollector2);
std::vector<TaggedObject *> targets2(1);
targets2[0] = pinbody;
booleanRegionSelect1->AssignTargets(targets2);
NXObject *nXObject1;
nXObject1 = booleanBuilder1->Commit();
booleanBuilder1->Destroy(); //释放


}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
InsertPinVentingNew::theUI->NXMessageBox()->Show("布尔减得出排气槽", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

 

posted on 2021-03-14 12:34  怡宁塑胶模具设计  阅读(399)  评论(0编辑  收藏  举报