You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Open CASCADE合并STEP文件:保留层级、定位导出及示例代码

使用Open CASCADE实现STEP文件合并操作

1. 合并STEP文件并保留层级结构与关联信息

要保留STEP的装配层级(组件、子组件结构)和关联信息(颜色、属性、几何约束),需基于Open CASCADE的XCAF框架处理,它专门用于管理带附加数据的CAD文档。

核心思路

  • 用STEPControl_Reader读取每个STEP文件,获取包含拓扑结构和所有附属数据的完整TDocStd_Document对象
  • 创建目标文档,通过XCAFDoc_ShapeTool、XCAFDoc_ColorTool等工具,将源文档的装配结构、形状、颜色、属性逐一复制到目标文档
  • 确保复制过程中保留原有的父子组件关系和所有关联元数据

示例代码

#include <STEPControl_Reader.hxx>
#include <TDocStd_Document.hxx>
#include <XCAFDoc_DocumentTool.hxx>
#include <XCAFDoc_ShapeTool.hxx>
#include <XCAFDoc_ColorTool.hxx>
#include <XCAFDoc_LayerTool.hxx>
#include <TopoDS_Shape.hxx>
#include <STEPControl_Writer.hxx>
#include <TCollection_ExtendedString.hxx>
#include <TDocStd_Application.hxx>

// 复制源文档的所有内容到目标文档
void CopyDocumentContent(const Handle(TDocStd_Document)& srcDoc, Handle(TDocStd_Document)& targetDoc)
{
    Handle(XCAFDoc_ShapeTool) srcShapeTool = XCAFDoc_DocumentTool::ShapeTool(srcDoc->Main());
    Handle(XCAFDoc_ShapeTool) targetShapeTool = XCAFDoc_DocumentTool::ShapeTool(targetDoc->Main());
    
    Handle(XCAFDoc_ColorTool) srcColorTool = XCAFDoc_DocumentTool::ColorTool(srcDoc->Main());
    Handle(XCAFDoc_ColorTool) targetColorTool = XCAFDoc_DocumentTool::ColorTool(targetDoc->Main());
    
    Handle(XCAFDoc_LayerTool) srcLayerTool = XCAFDoc_DocumentTool::LayerTool(srcDoc->Main());
    Handle(XCAFDoc_LayerTool) targetLayerTool = XCAFDoc_DocumentTool::LayerTool(targetDoc->Main());

    TopoDS_Shape srcRoot = srcShapeTool->GetShape();
    if (!srcRoot.IsNull())
    {
        // 复制形状及层级结构
        targetShapeTool->AddShape(srcRoot, Standard_True);
        
        // 复制颜色信息
        XCAFDoc_ColorType colorTypes[] = {XCAFDoc_ColorSurf, XCAFDoc_ColorCurv, XCAFDoc_ColorPoint};
        for (int i = 0; i < 3; ++i)
        {
            XCAFDoc_ColorType ct = colorTypes[i];
            TColStd_IndexedMapOfInteger srcColors;
            srcColorTool->GetColors(ct, srcColors);
            for (int j = 1; j <= srcColors.Size(); ++j)
            {
                Standard_Integer colorID = srcColors(j);
                Quantity_Color color;
                if (srcColorTool->GetColor(colorID, color, ct))
                {
                    targetColorTool->SetColor(srcShapeTool->GetShape(colorID), color, ct);
                }
            }
        }
        
        // 复制图层信息
        TColStd_IndexedMapOfInteger srcLayers;
        srcLayerTool->GetLayers(srcLayers);
        for (int j = 1; j <= srcLayers.Size(); ++j)
        {
            Standard_Integer layerID = srcLayers(j);
            TCollection_ExtendedString layerName;
            if (srcLayerTool->GetLayerName(layerID, layerName))
            {
                Standard_Integer targetLayerID = targetLayerTool->AddLayer(layerName);
                TColStd_IndexedMapOfInteger layerShapes;
                srcLayerTool->GetShapes(layerID, layerShapes);
                for (int k = 1; k <= layerShapes.Size(); ++k)
                {
                    targetLayerTool->AddShape(targetLayerID, srcShapeTool->GetShape(layerShapes(k)));
                }
            }
        }
    }
}

// 合并多个STEP文件并保留层级与关联信息
void MergeStepFilesWithStructure(const std::vector<std::string>& stepPaths, const std::string& outputPath)
{
    Handle(TDocStd_Application) app = new TDocStd_Application();
    app->Initialize();
    
    Handle(TDocStd_Document) targetDoc;
    app->NewDocument("MDTV-XCAF", targetDoc);
    
    for (const std::string& path : stepPaths)
    {
        STEPControl_Reader reader;
        IFSelect_ReturnStatus status = reader.ReadFile(path.c_str());
        if (status != IFSelect_RetDone)
        {
            continue;
        }
        
        Handle(TDocStd_Document) srcDoc;
        app->NewDocument("MDTV-XCAF", srcDoc);
        reader.TransferRoots(srcDoc->Main());
        
        CopyDocumentContent(srcDoc, targetDoc);
    }
    
    STEPControl_Writer writer;
    writer.Transfer(targetDoc->Main(), STEPControl_AsIs);
    writer.Write(outputPath.c_str());
    
    app->Close(targetDoc);
}

2. 加载多个STEP文件并按坐标定位后合并导出

此需求需在加载每个STEP形状后,通过几何平移变换将其定位到指定X/Y/Z坐标,再添加到目标装配结构中,同时保留原有的层级和关联信息。

核心思路

  • 读取每个STEP文件后获取根形状
  • 创建gp_Trsf变换对象,设置平移参数
  • 对根形状应用变换得到定位后的新形状
  • 将定位后的形状添加到目标文档,同时复制原有的关联信息

示例代码

#include <gp_Trsf.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <gp_Vec.hxx>
#include <Transfer_FinderProcess.hxx>

// 对形状应用平移变换
TopoDS_Shape TranslateShape(const TopoDS_Shape& shape, double x, double y, double z)
{
    gp_Trsf trsf;
    trsf.SetTranslation(gp_Vec(x, y, z));
    BRepBuilderAPI_Transform transform(trsf);
    transform.Perform(shape);
    return transform.Shape();
}

// 加载多个STEP文件并按坐标定位后合并
void MergeStepFilesWithPositioning(const std::vector<std::pair<std::string, gp_Vec>>& stepWithPos, const std::string& outputPath)
{
    Handle(TDocStd_Application) app = new TDocStd_Application();
    app->Initialize();
    
    Handle(TDocStd_Document) targetDoc;
    app->NewDocument("MDTV-XCAF", targetDoc);
    Handle(XCAFDoc_ShapeTool) targetShapeTool = XCAFDoc_DocumentTool::ShapeTool(targetDoc->Main());
    Handle(XCAFDoc_ColorTool) targetColorTool = XCAFDoc_DocumentTool::ColorTool(targetDoc->Main());
    
    for (const auto& entry : stepWithPos)
    {
        const std::string& path = entry.first;
        const gp_Vec& pos = entry.second;
        
        STEPControl_Reader reader;
        IFSelect_ReturnStatus status = reader.ReadFile(path.c_str());
        if (status != IFSelect_RetDone)
        {
            continue;
        }
        
        TopoDS_Shape srcShape = reader.OneShape();
        if (srcShape.IsNull())
        {
            continue;
        }
        
        TopoDS_Shape translatedShape = TranslateShape(srcShape, pos.X(), pos.Y(), pos.Z());
        
        targetShapeTool->AddShape(translatedShape, Standard_True);
        
        // 复制颜色信息(完整复制可参考第一部分的颜色复制逻辑)
        Handle(XCAFDoc_ColorTool) srcColorTool = XCAFDoc_DocumentTool::ColorTool(reader.Model()->Document()->Main());
        TColStd_IndexedMapOfInteger colorIDs;
        srcColorTool->GetColors(XCAFDoc_ColorSurf, colorIDs);
        for (int i = 1; i <= colorIDs.Size(); ++i)
        {
            Quantity_Color color;
            if (srcColorTool->GetColor(colorIDs(i), color, XCAFDoc_ColorSurf))
            {
                targetColorTool->SetColor(translatedShape, color, XCAFDoc_ColorSurf);
            }
        }
    }
    
    STEPControl_Writer writer;
    writer.Transfer(targetDoc->Main(), STEPControl_AsIs);
    writer.Write(outputPath.c_str());
    
    app->Close(targetDoc);
}

// 调用示例
int main()
{
    // 合并两个STEP文件并保留结构
    std::vector<std::string> stepFiles = {"part1.stp", "part2.stp"};
    MergeStepFilesWithStructure(stepFiles, "merged_with_structure.stp");
    
    // 加载两个STEP并分别定位到(10,0,0)和(0,20,0)
    std::vector<std::pair<std::string, gp_Vec>> stepWithPositions = {
        {"part1.stp", gp_Vec(10, 0, 0)},
        {"part2.stp", gp_Vec(0, 20, 0)}
    };
    MergeStepFilesWithPositioning(stepWithPositions, "merged_with_positioning.stp");
    
    return 0;
}

注意事项

  • 编译时需链接Open CASCADE核心库(如TKSTEP, TKXCAF, TKBRep, TKernel等)
  • 若需复制更多关联信息(如属性、约束),可扩展CopyDocumentContent函数,加入XCAFDoc_AttributeTool的处理逻辑
  • 复杂装配建议使用XCAFDoc_ShapeTool::AddComponent维护组件间的装配关系

内容的提问来源于stack exchange,提问作者Nils P.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 10:52:33