#include "DisplayScene.h" // 包含显示场景的头文件
#include "Viewer.h" // 包含查看器的头文件// OpenCascade 包含
#include <BRepPrimAPI_MakeCylinder.hxx> // 创建圆柱体
#include <BinXCAFDrivers.hxx> // 二进制XCAF驱动程序
#include <gp_Quaternion.hxx> // 四元数
#include <Interface_Static.hxx> // 静态接口
#include <STEPCAFControl_Writer.hxx> // STEP CAF 控制写入器
#include <TDF_ChildIterator.hxx> // 子迭代器
#include <TDataStd_Name.hxx> // 标准数据名称
#include <TDocStd_Application.hxx> // 文档标准应用
#include <TDocStd_Document.hxx> // 文档标准文件
#include <TopExp.hxx> // 顶部表达式
#include <TopoDS.hxx> // 拓扑数据结构
#include <XCAFDoc_ColorTool.hxx> // 颜色工具
#include <XCAFDoc_DocumentTool.hxx> // 文档工具
#include <XCAFDoc_ShapeTool.hxx> // 形状工具namespace
{// 写入带有元数据的STEP文件bool WriteStepWithMeta(const Handle(TDocStd_Document)& doc,const char* filename){STEPCAFControl_Writer Writer;// 为了使子形状名称起作用,我们必须打开OpenCascade的以下静态变量。Interface_Static::SetIVal("write.stepcaf.subshapes.name", 1);// 将XDE文档写入文件。try{if ( !Writer.Transfer(doc, STEPControl_AsIs) ) // 转移XDE文档{return false;}const IFSelect_ReturnStatus ret = Writer.Write(filename); // 将XDE文档写入文件if ( ret != IFSelect_RetDone ){return false;}}catch ( ... ){return false;}return true;}namespace ShapeReflection{// 创建表示车轮的简化形状的圆柱体原语,包括其边缘和与之相融的轮胎。TopoDS_Shape CreateWheel(const double OD,const double W){return BRepPrimAPI_MakeCylinder(gp_Ax2( gp_Pnt(-W/2, 0., 0.), gp::DX() ), OD/2, W);}// 创建表示轮轴的几何原语。TopoDS_Shape CreateAxle(const double D,const double L){return BRepPrimAPI_MakeCylinder(gp_Ax2( gp_Pnt(-L/2, 0., 0.), gp::DX() ), D/2, L);}// 创建车轮轴子组件形状。TopoDS_Shape CreateWheelAxle(const TopoDS_Shape& wheel,const TopoDS_Shape& axle,const double L){TopoDS_Compound compShape;BRep_Builder bbuilder;bbuilder.MakeCompound(compShape);gp_Trsf wright_T;wright_T.SetTranslationPart( gp_Vec(L/2, 0, 0) );gp_Quaternion qn(gp::DY(), M_PI);gp_Trsf wleft_TR;wleft_TR.SetRotation(qn);//gp_Trsf wleft_T = wright_T.Inverted() * wleft_TR;bbuilder.Add( compShape, wheel.Moved(wleft_T) );bbuilder.Add( compShape, wheel.Moved(wright_T) );bbuilder.Add( compShape, axle ); // 不带变换的轮轴。return compShape;}// 创建车轮轴。TopoDS_Shape CreateChassis(const TopoDS_Shape& wheelAxle,const double CL){TopoDS_Compound compShape;BRep_Builder bbuilder;bbuilder.MakeCompound(compShape);gp_Trsf wfront_T; wfront_T .SetTranslationPart( gp_Vec(0, CL/2, 0) );gp_Trsf wrear_T; wrear_T .SetTranslationPart( gp_Vec(0, -CL/2, 0) );bbuilder.Add( compShape, wheelAxle.Moved(wfront_T) );bbuilder.Add( compShape, wheelAxle.Moved(wrear_T) );return compShape;}}
}// 程序入口
int main(int argc, char** argv)
{Viewer vout(50, 50, 500, 500); // 创建查看器// 创建XDE文档Handle(TDocStd_Application) app = new TDocStd_Application;BinXCAFDrivers::DefineFormat(app);Handle(TDocStd_Document) doc;app->NewDocument("BinXCAF", doc);// 工具Handle(XCAFDoc_ShapeTool)ST = XCAFDoc_DocumentTool::ShapeTool( doc->Main() ); // 形状工具Handle(XCAFDoc_ColorTool)CT = XCAFDoc_DocumentTool::ColorTool( doc->Main() ); // 颜色工具// 准备装配文档// 参数const double OD = 500;const double W = 100;const double L = 1000;const double CL = 1000;// 创建一个轮子原型t_wheelPrototype wheelPrototype;wheelPrototype.shape = ::ShapeReflection::CreateWheel(OD, W);wheelPrototype.label = ST->AddShape(wheelPrototype.shape, false); // 将其添加到XDE文档// 创建一个轴原型t_prototype axlePrototype;axlePrototype.shape = ::ShapeReflection::CreateAxle(OD/10, L);axlePrototype.label = ST->AddShape(axlePrototype.shape, false); // 将其添加到XDE文档// 创建一个轮轴子组件。 注意,不会复制形状。// 此时,输出的STEP文件仅包含两个MANIFOLD_SOLID_BREP实体。t_prototype wheelAxlePrototype;wheelAxlePrototype.shape = ::ShapeReflection::CreateWheelAxle(wheelPrototype.shape, axlePrototype.shape, L);wheelAxlePrototype.label = ST->AddShape(wheelAxlePrototype.shape, true); // 将其添加到XDE文档// 主装配。// STEP文件仍然只包含几个实体。t_prototype chassisPrototype;chassisPrototype.shape = ::ShapeReflection::CreateChassis(wheelAxlePrototype.shape, CL);chassisPrototype.label = ST->AddShape(chassisPrototype.shape, true); // 将其添加到XDE文档// 将颜色分配给部件。CT->SetColor( wheelPrototype.label, Quantity_Color(1, 0, 0, Quantity_TOC_RGB), XCAFDoc_ColorGen );CT->SetColor( axlePrototype.label, Quantity_Color(0, 1, 0, Quantity_TOC_RGB), XCAFDoc_ColorGen );// 设置原型的名称TDataStd_Name::Set(wheelPrototype.label, "wheel");TDataStd_Name::Set(axlePrototype.label, "axle");TDataStd_Name::Set(wheelAxlePrototype.label, "wheel-axle");TDataStd_Name::Set(chassisPrototype.label, "chassis");// 设置子组件实例的名称。for ( TDF_ChildIterator cit(chassisPrototype.label); cit.More(); cit.Next() ){TDataStd_Name::Set(cit.Value(), "wheel-axle-ref");}// 通过面展开轮子形状。TopTools_IndexedMapOfShape wheelFaces;TopExp::MapShapes(wheelPrototype.shape, TopAbs_FACE, wheelFaces);//wheelPrototype.frontFace = TopoDS::Face( wheelFaces(2) );wheelPrototype.frontFaceLabel = ST->AddSubShape(wheelPrototype.label, wheelPrototype.frontFace);CT->SetColor( wheelPrototype.frontFaceLabel, Quantity_Color(0, 0, 1, Quantity_TOC_RGB), XCAFDoc_ColorSurf );// 为子形状设置名称。TDataStd_Name::Set(wheelPrototype.frontFaceLabel, "front-face");// 完成。// 显示场景DisplayScene cmd( doc, vout.GetContext() );if ( !cmd.Execute() ){std::cout << "Failed to visualize CAD model with `DisplayScene` command." << std::endl;return 1;}// 保存到文件if ( argc > 1 ){if ( !::WriteStepWithMeta(doc, argv[1]) ){std::cout << "Failed to write XDE document to a STEP file " << argv[1] << std::endl;return 1;}}// 保存以便调试PCDM_StoreStatus sstatus = app->SaveAs(doc, "E:/CAD/xinjiazi8_asm.xbf");if ( sstatus != PCDM_SS_OK ){app->Close(doc);std::cout << "Cannot write OCAF document." << std::endl;return 1;}// 进入消息循环vout.StartMessageLoop();app->Close(doc); // 在退出消息循环时关闭文档。return 0;
}
这段代码是一个完整的程序,它创建了一个包含车轮、轮轴和底盘的简单 CAD 模型,并将其保存为 STEP 文件。逐行注释已经说明了代码的作用。