NXopen 特征创建过程中的布尔运算

下面以所创建的块和圆柱之间的布尔运算来说明

// A code block
//1、模板文件添加头文件*
#include <NXOpen/Features_BlockFeatureBuilder.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/PointCollection.hxx>
#include <NXOpen/Features_CylinderBuilder.hxx>
#include <NXOpen/AxisCollection.hxx>
#include <NXOpen/GeometricUtilities_BooleanOperation.hxx>
//**2、函数声明**
//创建一个块通过原点和边长
NXOpen::Features::Feature* CreateBlockFeature(NXOpen::Point3d originPoint, double Length, double Width, double Height, NXOpen::GeometricUtilities::BooleanOperation::BooleanType type1, NXOpen::Body* Tbody, NXOpen::Part* workPart);
//NXOpen::Point3d originPoint 块原点
// double Length, double Width, double Height 块长、宽、高
// NXOpen::GeometricUtilities::BooleanOperation::BooleanType type1 布尔操作类型
// type1 为枚举变量  1 BooleanTypeCreate 仅创建;2 BooleanTypeUnite 并运算;3 BooleanTypeSubtract 减运算;4 BooleanTypeIntersect 交运算。
//NXOpen::Body* Tbody 布尔运算目标体,当 BooleanTypeCreate时 ,Tbody=NXOpen::Body* nullNXOpen_Body(NULL);
//创建圆柱通过轴、直径和高度
NXOpen::Features::Feature* CreateCylinderFeature(double CylinderDiameter, double CylinderHeight, double OriginPoint[3], double Direction[3], NXOpen::GeometricUtilities::BooleanOperation::BooleanType type1, NXOpen::Body* Tbody, NXOpen::Part* workPart);
// double CylinderDiameter 圆柱直径,double CylinderHeight 圆柱高度, double OriginPoint[3] 圆柱轴原点, double Direction[3] 圆柱轴方向。
//**3、函数定义**
NXOpen::Features::Feature* MyClass::CreateBlockFeature(NXOpen::Point3d originPoint, double Length, double Width, double Height, NXOpen::GeometricUtilities::BooleanOperation::BooleanType type1, NXOpen::Body* Tbody, NXOpen::Part* workPart)
{
	
	NXOpen::Features::Feature* nullNXOpen_Features_Feature(NULL);
	NXOpen::Features::BlockFeatureBuilder* blockFeatureBuilder1;
	blockFeatureBuilder1 = workPart->Features()->CreateBlockFeatureBuilder(nullNXOpen_Features_Feature);
	blockFeatureBuilder1->BooleanOption()->SetType(type1);

	std::vector<NXOpen::Body*> targetBodies(1);
	targetBodies[0] = Tbody;
	blockFeatureBuilder1->BooleanOption()->SetTargetBodies(targetBodies);


	blockFeatureBuilder1->SetType(NXOpen::Features::BlockFeatureBuilder::TypesOriginAndEdgeLengths);
	char Lengthchar[256];
	sprintf(Lengthchar, "%f", Length);

	char Widthchar[256];
	sprintf(Widthchar, "%f", Width);

	char Heightchar[256];
	sprintf(Heightchar, "%f", Height);

	blockFeatureBuilder1->SetOriginAndLengths(originPoint, Lengthchar, Widthchar, Heightchar);
	NXOpen::Features::Feature* blockfeature;
	blockfeature = blockFeatureBuilder1->CommitFeature();
	return blockfeature;
}
NXOpen::Features::Feature* MyClass::CreateCylinderFeature(double CylinderDiameter, double CylinderHeight, double OriginPoint[3], double Direction[3], NXOpen::GeometricUtilities::BooleanOperation::BooleanType type1, NXOpen::Body*Tbody, NXOpen::Part* workPart)
{
	char Diameterchar[256];
	sprintf(Diameterchar, "%f", CylinderDiameter);

	char Heightchar[256];
	sprintf(Heightchar, "%f", CylinderHeight);

	NXOpen::Features::Feature* nullNXOpen_Features_Feature(NULL);
	NXOpen::Features::CylinderBuilder* cylinderBuilder1;
	cylinderBuilder1 = workPart->Features()->CreateCylinderBuilder(nullNXOpen_Features_Feature);
	cylinderBuilder1->BooleanOption()->SetType(type1);

	std::vector<NXOpen::Body*> targetBodies1(1);
	targetBodies1[0] = Tbody;
	cylinderBuilder1->BooleanOption()->SetTargetBodies(targetBodies1);

	cylinderBuilder1->SetType(NXOpen::Features::CylinderBuilder::Types::TypesAxisDiameterAndHeight);
	NXOpen::Point3d AxisOriginPoint(OriginPoint[0], OriginPoint[1], OriginPoint[2]);
	NXOpen::Vector3d AxisDirection(Direction[0], Direction[1], Direction[2]);
	NXOpen::Axis* axis1;
	axis1 = workPart->Axes()->CreateAxis(AxisOriginPoint, AxisDirection, NXOpen::SmartObject::UpdateOptionWithinModeling);
	cylinderBuilder1->SetAxis(axis1);

	cylinderBuilder1->Diameter()->SetFormula(Diameterchar);
	cylinderBuilder1->Height()->SetFormula(Heightchar);
	NXOpen::Features::Feature* cyfeature;
	cyfeature = cylinderBuilder1->CommitFeature();
	cylinderBuilder1->Destroy();

	return cyfeature;
}
//**4、主程序**
NXOpen::Session* theSession = NXOpen::Session::GetSession();
NXOpen::Part* workPart(theSession->Parts()->Work());
NXOpen::Part* displayPart(theSession->Parts()->Display());
**//创建块特征**
double originX = 0.0;
double originY = 0.0;
double originZ = 0.0;
NXOpen::Point3d originPoint(originX, originY, originZ);
double Length = 200;
double Width = 100;
double Height = 100;
NXOpen::GeometricUtilities::BooleanOperation::BooleanType type1 = NXOpen::GeometricUtilities::BooleanOperation::BooleanTypeCreate;
**//所创建的第一个特征快特征布尔类型为BooleanTypeCreate,仅创建,此时布尔操作**目标体为空,即NXOpen::Body* nullNXOpen_Body(NULL);
NXOpen::Body* nullNXOpen_Body(NULL);
NXOpen::Body* Tbody1 = nullNXOpen_Body;
NXOpen::Features::Feature* blockfeature;
blockfeature = CreateBlockFeature(originPoint, Length, Width, Height, type1, Tbody1, workPart);
NXOpen::Body* Blockbody(dynamic_cast<NXOpen::Body*>(workPart->Bodies()->FindObject(blockfeature->JournalIdentifier())));**//获取所创建的块特征的体,作为下一个特征创建中布尔运算的目标体**
**//创建圆柱特征**
double CylinderDiameter = 100.0;
double CylinderHeight = 100.0;
double OriginPoint[3] = { 0.0,0.0,0.0 };
double Direction[3] = { 0.0,0.0,1.0 };
NXOpen::GeometricUtilities::BooleanOperation::BooleanType type2 = NXOpen::GeometricUtilities::BooleanOperation::BooleanTypeSubtract;
//布尔运算为BooleanTypeSubtract减运算,即从上面创建的块中减去块和圆柱相交的部分
NXOpen::Features::Feature* cyfeature;
cyfeature = CreateCylinderFeature(CylinderDiameter, CylinderHeight, OriginPoint, Direction, type2, Blockbody, workPart);
**//特别注意**当访问布尔运算结构体的时候,只能通过目标体所在特征,即块特征来获取
NXOpen::Body* Boolbody(dynamic_cast<NXOpen::Body*>(workPart->Bodies()->FindObject(blockfeature->JournalIdentifier())));

程序运行结果如下图:
在这里插入图片描述

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值