3D part design with OpenSCAD # 114: centering an stl file.

2 min read
By Bob
3D part design with OpenSCAD # 114: centering an stl file.

Sometimes you get an stl file that someone else has designed and it is not centered or you want to center two files and it is difficult to get them to align, as far as I am aware OpenSCAD doesn't have a simple way to exactly center an imported stl file so I made a module that will show a cube that is dead center that you can align the stl with then save it and it will be exactly center.

If you enable the bounding box output in the render summary it will give you the exact size of the stl file:

Press the render button or F6 and now you can copy and paste or manually enter the x,y,and z size values to show a box that is the size of the bounding box:

Then you can use the move stl sliders to move the stl to the center of the box:

Once the min and max values are the same and have opposite values that are 1/2 the bounding box size the stl is exactly center.

With the bounding box being a different color it is easy to see if any part of the stl is outside of the box.

here is the code:

//
/*[Path to STL] */
 Path_to_STL_File="/home/none4/Desktop/";

/*[Translate STL] */
 Move_X= 0;//[-400:.01:400]
 Move_Y= 0;//[-400:.01:400]
 Move_Z= 0;//[-400:.01:400]

/*[Bounding box] */
 Show_box=false;
 BoundingBox_x=10;//[-400:.01:400]
 BoundingBox_y=10;//[-400:.01:400]
 BoundingBox_z=10;//[0:.01:400]
  
module CenterSTL(){  
 translate([Move_X,Move_Y,Move_Z])
  color("white")
 import(Path_to_STL_File);
 if(Show_box)
  color("red",.3)
%cube([BoundingBox_x,BoundingBox_y,  BoundingBox_z],center=true);
}

CenterSTL();