Drawings SDK: Importing from STL Format

Roman Martens

March 05, 2021

STL (Stereolithography) format is a simple, openly documented format for describing the surface of an object as a triangular mesh. Every triangle is represented by the unit normal and vertices (ordered by the right-hand rule) using a three-dimensional Cartesian coordinate system.

Use the STL import module to perform the import procedure:

STLImport_xx.yy_zz.tx

where:

  • xx — Major Drawings SDK version number.
  • yy — Minor Drawings SDK version number.
  • zz — Microsoft® Visual C++® compiler version number. For example, for Microsoft Visual Studio® 2015 version, the number is 14.

To import an .stl file using the STLImport module:

  1. Load the STLImport module.
  2. Create an OdStlImport object.
  3. Use one of the following functions:
    • OdStlImportRes import(const OdString &strFilePath);
    • OdStlImportRes import(const OdStreamBufPtr& pStreamBuf);
    • The difference between the functions is that the first function imports from a file while the second one imports from a stream.

When STL data is imported, you can get a shell as an array of vertices, facets (triangles), and facet normals using the following function:

OdStlImportRes getShell(OdGePoint3dArray &vertices,
                        OdUInt32Array &faces,
                        OdGeVector3dArray &normals,
                        OdUInt8 flags = 0) const

where flags can take the following values:

  • kFixNormals — Check the normals and correct them in the following cases:
    • Zero normals
    • Normals do not match with the normals calculated according to the right-hand rule
      If this flag is set, the array of normals contains the normals at faces.
  • kUnifyDuplicatedVertices — Unify duplicating vertices. If this flag is set, the array of vertices contains only non-repeating vertices.
  • kCalcNormalsAtVertices — Perform a primitive calculation of the normals at vertices. Normals at vertices can be used in smooth-shaded rendering modes.
    • If the kUnifyDuplicatedVertices flag is not set, the normals at a vertex are simply equal to the normals of faces that contain the vertex.
    • If the kUnifyDuplicatedVertices flag is set, the normal at a vertex is calculated by averaging the normals at faces that contain the vertex.
      If this flag is set, the array of normals contains the normals at vertices.

Note: This method of calculation is very rough and works well only for smooth shells. For example, for cubes, this method gives poor results. In such cases, more complex algorithms on the user side are required.

For more information about how to create a body (FacetModeler::Body) or a solid (OdDb3dSolidPtr) using the obtained arrays of facets and vertices, see the following:

Example of Importing from an .stl File

This example illustrates importing from an .stl file and getting the shell using the getShell() function.

STLFileImport::OdStlImportModulePtr stlModule = odrxDynamicLinker()->loadModule(OdStlImportModuleName);
STLFileImport::OdStlImportPtr stlImport = stlModule->create();

stlImport->import(fileName);

OdGePoint3dArray vertices;
OdUInt32Array faces;
OdGeVector3dArray normals;

OdUInt8 flags = 0;
SETBIT(flags, STLFileImport::OdStlImport::kUnifyDuplicatedVertices, true);
stlImport->getShell(vertices, faces, normals, flags);