OCC 创建简单几何

本文介绍了使用OpenCASCADETechnology(OCC)创建一个正方体和圆柱体,通过布尔运算在正方体内切除圆柱体,并以STL格式保存几何模型的过程,同时展示了如何计算新模型的体积、质心和惯性矩阵。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用 OCC 创建 正方体和圆柱体,并且通过布尔运算,切除正方体内的圆柱体,保存 stl 几何模型。

#include <iostream>
#include <iomanip>
#include "BRepPrimAPI_MakeCylinder.hxx"
#include "BRepPrimAPI_MakeBox.hxx"
#include "BRepAlgoAPI_Cut.hxx"
#include "BRepGProp.hxx"
#include "STEPControl_Writer.hxx"
#include "GProp_GProps.hxx"


int main(int argc, char* argv[])
{
	//创建 长 x 宽 x 高  100x100x50
	gp_Pnt lowerLeftCornerOfBox(-50.0, -50.0, 0.0);					  // 位置点
	BRepPrimAPI_MakeBox boxMaker(lowerLeftCornerOfBox, 100, 100, 50); // 长宽高
	TopoDS_Shape box = boxMaker.Shape();							  // 创建 box 形状


	//创建圆柱 radius 25.0, height 50.0 
	BRepPrimAPI_MakeCylinder cylinderMaker(25.0, 50.0);				  // 创建圆柱
	TopoDS_Shape cylinder = cylinderMaker.Shape();					  // 创建 Cylinder 形状

	// 剪切 box 里的圆柱部分
	BRepAlgoAPI_Cut cutMaker(box, cylinder);
	TopoDS_Shape boxWithHole = cutMaker.Shape();					  // 创建 布尔运算之后的几何

	//保存几何文件 STEP
	STEPControl_Writer writer;

	writer.Transfer(boxWithHole, STEPControl_AsIs);
	writer.Write("boxWithHole.stp");
	std::cout << "Created box with hole, file is written to boxWithHole.stp" << std::endl;

	// 计算新几何的体积
	GProp_GProps volumeProperties;
	BRepGProp::VolumeProperties(boxWithHole, volumeProperties);


	//计算体积
	std::cout << std::setprecision(14) << "Volume of the model is: " << volumeProperties.Mass() << std::endl;

	//计算质心
	std::cout << "Center of mass is: " << volumeProperties.CentreOfMass().X() << " " << volumeProperties.CentreOfMass().Y() << " " << volumeProperties.CentreOfMass().Z() << std::endl;

	//计算惯性矩阵
	gp_Mat inertiaMatrix = volumeProperties.MatrixOfInertia();
	std::cout << "Matrix of inertia: " << std::endl;
	for (int i = 1; i <= 3; ++i) {
		for (int j = 1; j <= 3; ++j) {
			std::cout << inertiaMatrix(i, j) << "\t";
		}
		std::cout << std::endl;
	}

	return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值