Combine two step files, keeping colors and assembly structure unchanged

frankpian

CAD practitioner
I found an demo with chatgpt, but he has no color and the assembly structure is not correct.
If someone can share a demo with me, I'd really appreciate it !

screenshot-20230512-183902.png

C++:
#include <iostream>
#include <fstream>
#include <BRep_Builder.hxx>
#include <STEPControl_Reader.hxx>
#include <STEPControl_Writer.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Compound.hxx>
#include <gp_Trsf.hxx>
#include <gp_Ax1.hxx>
#include <BRepBuilderAPI_Transform.hxx>

int main(int argc, char* argv[])
{
    const char* input_file1 = "22.step";
    const char* input_file2 = "23.step";
    const char* output_file = "test.step";

    STEPControl_Reader reader1, reader2;
    IFSelect_ReturnStatus status1 = reader1.ReadFile(input_file1);
    IFSelect_ReturnStatus status2 = reader2.ReadFile(input_file2);

    if (status1 != IFSelect_RetDone || status2 != IFSelect_RetDone) {
        std::cerr << "Error reading input STEP files" << std::endl;
        return 1;
    }

    reader1.TransferRoots();
    reader2.TransferRoots();

    TopoDS_Shape shape1 = reader1.OneShape();
    TopoDS_Shape shape2 = reader2.OneShape();

    gp_Trsf trsf;
    trsf.SetTranslation(gp_Vec(100, 0, 0)); 
    trsf.SetRotation(gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), M_PI / 4);

    BRepBuilderAPI_Transform transform(shape2, trsf);
    shape2 = transform.Shape();

    BRep_Builder builder;
    TopoDS_Compound compound;
    builder.MakeCompound(compound);
    builder.Add(compound, shape1);
    builder.Add(compound, shape2);

    STEPControl_Writer writer;
    writer.Transfer(compound, STEPControl_AsIs);
    writer.Write(output_file);

    std::cout << "Merged STEP file written to: " << output_file << std::endl;
    return 0;
}
 

Quaoar

Administrator
Staff member
I got another result but the assemblies still look badly positioned to each other:

1684159754488.png
 

Quaoar

Administrator
Staff member
This one is a commercial software package, so I do not have a reusable piece of code to share. The idea is pretty straightforward though: you load your file to another XDE document and then copy the roots from this second document to the first one. You will also have to transfer all labels with colors, and this mechanics can be a little laborious.
 
Top