Bug in reading STL files.

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.

Jim

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"

fscanf(file,"%*s");

// skip the keywords "endfacet"

fscanf(file,"%*s");

//NEW CODE START
//skip any 'endsolid' and 'solid' delimiters

fgetpos(file, &pos);

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

if( C==EOF ) {

break;

}

else if( C=='e' ) {

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

C = getc(file);

if( C==EOF )

break;

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

}

else {

fsetpos(file, &pos);

}

//NEW CODE END
}

cout

fclose(file);

return ReadMesh;

}

Roman Lygin's picture

(OCCPATCH)

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.

Roman
---
opencascade.blogspot.com - 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.

Thanks.
Roman
---
opencascade.blogspot.com - the Open CASCADE blog

J. Tonnison's picture
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 ;-).

Roman
---
opencascade.blogspot.com - 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.

P G's picture

Hi
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?
regards
-PG

Patrik Mueller's picture

Take a look at the samples:

ImportExport.cpp

It uses StlAPI_Writer...

Greets,

Patrik