For all issues regarding the Forums use, please, refer to the Forum Rules.

Our Solutions

Need professional assistance?
Consider our:

Support Offerings


Need to speed up your development?
Have a look at our:

Samples & Tools


Need some functionality extending standard OCCT capabilities?
Check out our:

Adv. Components

Related pages

Bug in reading STL files.

J. Tonnison's picture

There is a bug in the STL reader method named

Handle_StlMesh_Mesh RWStl::ReadAscii(const OSD_Path& aPath)

that is located in the file named RWStl.cxx.

The code as it stands now will not correctly read an ASCII STL file that contains more that one "solid." (See the STL file format documentation for a description of solids.) STL files with more than one solid are rare, but when they are encountered, OCC will create a BRep with a lot of garbage in it. Shown below is my fix.


Handle_StlMesh_Mesh RWStl::ReadAscii(const OSD_Path& aPath)


TCollection_AsciiString filename;

long ipos;

Standard_Integer nbLines = 0;

Standard_Integer nbTris = 0;

Standard_Integer iTri;

Standard_ShortReal x[4],y[4],z[4];

Standard_Integer i1,i2,i3;

fpos_t pos; //NEW CODE

char C; //NEW CODE

Handle(StlMesh_Mesh) ReadMesh;


// skip the keywords "endloop"


// skip the keywords "endfacet"


//skip any 'endsolid' and 'solid' delimiters

fgetpos(file, &pos);

C = getc(file);C = getc(file);

if( C==EOF ) {



else if( C=='e' ) {

while (getc(file) != '\n');

C = getc(file);

if( C==EOF )


while (getc(file) != '\n');


else {

fsetpos(file, &pos);





return ReadMesh;


Roman Lygin's picture


Good to have this improvement, thank you!
However I doubt the STL format is supposed to contain several solids. Descriptions that I found always speak (implicitly or explicitly) of a single solid expected. For example, the binary file header only specifies a number of facets in the file, it does not split them per solids as Ascii format would allow.

--- - the Open CASCADE blog

J. Tonnison's picture

I did consult the STL documentation -- all two pages of it -- and, indeed, more than one solid may be present in a file. Additionally, we attempted to import a STL file that, unknown to us, had several solids. When we displayed the BRep it looked totally crazy; this is because the extra lines "solid" and "endsolid" were read in as numerical data!

Roman Lygin's picture

J, could you send a link to your documentation? I could not find *the Standard*, and my assumption was it's more a de-facto conventional format rather than an industrial standard. For many years Open CASCADE has this interface, I have not encountered a single file with multiple solids.

--- - the Open CASCADE blog

J. Tonnison's picture

Look at

Roman Lygin's picture

Quod erat demonstrandum.

The Backus-Naur Form (BNF) on page 3 unambiguously defines that the ASCII STL file is a *single* solid ;-).

--- - the Open CASCADE blog

Charles Overy's picture

STL has been broadly adopted as a generic mesh interchange format. My unserstanding is that strictly speaking it should only have a single solid. However, we encounter (and generate) stl files with multiple solids on a daily basis. It is VERY common in additive fabrication ie. rapid prototyping, (the original industry for stl) to have multiple solids and most commercial applications now support this.
It would be a mistake not to make small accommodations, when possible to enable multiple solids to be read.

PG's picture

I am trying to read a binary file created using OCC itself.
Which API to use for creating a binary stl - StlAPI or StlAPI_Writer class?

Patrik Mueller's picture

Take a look at the samples:


It uses StlAPI_Writer...



Contact us

Attention: DO NOT use this form for all kind of request for the removal of your personal data. Please follow this link for such cases.