Search code examples
c++opencascade

Translate (Move) the center of TopoDS_Shape to origin


I am working on a step file reader where I load the file and look at its features. What I want to achieve is to move the step file towards the origin meaning that I want the center of the part to be on the origin.

By the center of the part I mean the center of the bounding box around the part. However I am having a bit of problem understanding how to do it in OpenCascade. Here is the piece of code that I think should do the trick for me

STEPControl_Reader reader;
IFSelect_ReturnStatus stat = reader.ReadFile(inputFilename.c_str());
reader.NbRootsForTransfer();
reader.TransferRoots();
Handle(TColStd_HSequenceOfTransient) list = reader.GiveList();
reader.TransferList(list)                                                                                                                                                                          
TopoDS_Shape Old_Original_Solid = reader.OneShape();                                                                                                                                                                   
// Translate this shape to center                                                                                                                                                                                      
gp_Trsf trsf;
// TODO: How to fill this? How do I get the bounding box for Old_Original_Solid                                                                                                                                                                                                   
gp_Vec translation;                                                                                                                                                                                                     
trsf.SetTranslation(translation);                                                                                                                                                                                      
BRepBuilderAPI_Transform aBRepTrsf (Old_Original_Solid, aTrsf, Standard_False);                                                                                                                                        
TopoDS_Shape Original_Solid = aBRepTrsf.Shape();  

I have removed all the checks and logs from this code. My question specifically is if my approach (the last 5 lines) is correct at all and if it is how should I fill the translation vector.

Thank you.


Solution

  • I figured out the way so here is the answer

    Bnd_Box box;
    BRepBndLib::Add(Old_Original_Solid, box);
    Standard_Real theXmin, theYmin, theZmin, theXmax, theYmax, theZmax;
    box.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
                                                                                                                                                        
    // Translate this shape to center
    gp_Trsf trsf;                                                                                                                                                                                                 
    trsf.SetTranslation(gp_Vec(-(theXmax + theXmin)*0.5, -(theYmax + theYmin)*0.5, -(theZmax + theZmin)*0.5));
    

    Bnd_Box figures out the bounding box for a shape that is passed to it, then we get the max and min values of the bounding box in each direction and move towards the center.