当前位置: 首页 > news >正文

opencascade15解析导出为step格式

#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 文件。逐行注释已经说明了代码的作用。

http://www.lryc.cn/news/305588.html

相关文章:

  • 【软件设计模式之模板方法模式】
  • Spring Boot项目怎么对System.setProperty(key, value)设置的属性进行读取加解密
  • Linux理解
  • 常用芯片学习——YC688语音芯片
  • C语言:指针的进阶讲解
  • 基于SSM的车位租赁系统(有报告)。Javaee项目。ssm项目。
  • Java pyhon C C++ R JS 主流语言的区别-03
  • 5 buuctf解题
  • 微服务三十五关
  • 第一个 Angular 项目 - 添加服务
  • 红日靶场3
  • B树的介绍
  • 《The Art of InnoDB》第二部分|第4章:深入结构-磁盘结构-撕裂的页面(doublewrite buffer)
  • 提示工程(Prompt Engineering)、微调(Fine-tuning) 和 嵌入(Embedding)
  • 【Flink精讲】Flink 内存管理
  • 正则化概念及使用
  • 让程序员设计B端界面,好比武大郎招聘:向我看齐。不忍直视!
  • 使用python构建Android,探索跨平台应用开发Kivy框架
  • 08 Redis之集群的搭建和复制原理+哨兵机制+CAP定理+Raft算法
  • *MYSQL--索引--内部原理
  • docker安装kafka和kafka-console-ui
  • Linux:gitlab创建组,创建用户,创建项目
  • 相机选型介绍
  • SQL创建数据库
  • 读书笔记-增强型分析:AI驱动的数据分析、业务决策与案例实践
  • NXP实战笔记(十):S32K3xx基于RTD-SDK在S32DS上配置CAN通信
  • 纳斯达克大屏-投放需要知道的几个条件-大舍传媒
  • python-可视化篇-简单-条形图输出主要省份GDP排名情况
  • Sora - 探索AI视频模型的无限可能-官方报告解读与思考
  • 算法提升——LeetCode第385场周赛总结