如何在 OpenCASCADE 中导出带颜色的 STEP 文件
OCCUtils 提供了导出带颜色元素的 STEP 的简便方法:
make_colored_step.cpp
#include <occutils/ExtendedSTEP.hxx>
#include <occutils/Primitive.hxx>
TopoDS_Shape cube = Primitive::MakeCube(5 /* mm */);
STEP::ExtendedSTEPExporter stepExporter;
stepExporter.AddShapeWithColor(cube, Quantity_NOC_RED);
stepExporter.Write("ColoredCube.step");生成的 STEP 文件如下所示:

你也可以添加无颜色的形状:
add_shape_no_color.cpp
stepExporter.AddShape(myShape);不使用 OCCUtils 也可以做到,但可能会让你的生活很痛苦,因为要让它正确工作相当困难。
这是一个最小示例:
minimal_colored_step.cpp
// 获取全局应用程序
Handle(TDocStd_Application) application = XCAFApp_Application::GetApplication();
// 创建文档
Handle(TDocStd_Document) document;
application->NewDocument("MDTV-XCAF", document);
// 获取形状和颜色工具
Handle(XCAFDoc_ShapeTool) shapeTool = XCAFDoc_DocumentTool::ShapeTool(document->Main());
Handle(XCAFDoc_ColorTool) colorTool = XCAFDoc_DocumentTool::ColorTool(document->Main());
// 创建形状
// 重要:不要使用 shapeTool->AddShape(TopoDS_Shape)!
// 这不会保留颜色!
TDF_Label partLabel = shapeTool->NewShape();
shapeTool->SetShape(partLabel, filletedBody);
//TDF_Label redColor = colorTool->AddColor();
colorTool->SetColor(partLabel, Quantity_NOC_RED, XCAFDoc_ColorGen);
STEPCAFControl_Writer writer;
writer.SetColorMode(true);
writer.Perform(document, "ColoredShape.step");你可以使用哪些颜色?
要么像这样使用 RGB 值(从 0.0 到 1.0)自己定义 Quantity_Color:
color_rgb.cpp
Quantity_Color red(1.0 /* R */, 0.0 /* G */, 0.0 /* B */, Quantity_TypeOfColor::Quantity_TOC_RGB);或者你可以在 HSL 颜色空间中做同样的事情:
color_hsl.cpp
Quantity_Color brownishOrange(1.0 /* H */, 0.5 /* L */, 0.5 /* S */, Quantity_TypeOfColor::Quantity_TOC_HLS);或者你可以使用 Quantity_NameOfColor 枚举中的预定义颜色 - 请参见OpenCASCADE 中所有可用标准颜色概述查看所有可用颜色的表格。
Check out similar posts by category:
C/C++, OpenCASCADE
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow