Introduction

StereoLithography(StL) is something that is widely used in CAD/CAM, RapidPrototyping etc. The concept is that any surface or solid is exported to StL format by data exchange packages after they are modeled in commercial CAD packages. This data consists of triangulated facets that approximate the surface of the solid. No topological or geometric information is exported. The surface is subdivided into small triangles. The approximation uses chordal deflection for curved surfaces to smoothen the surface. More the smoothening required, more number of smaller triangles are generated by subdivision resulting in larger size of data file. Now, along with triangles, their facet normals are also generated. The data is written both in ASCII and binary formats. The data in ASCII is written like this:

The facet normal tells the three components of the facet normal followed by three vertices of one triangle, enclosed by the statements.

facet normal 0.000000e+000 -1.000000e+000 0.000000e+000

and

endfacet

Thus all the triangles are written one after the other. This data can be then used as input for generating Rapid Prototype models as well as for NC toolpath generation. The Normal data helps to compute tool offsets etc.

Here, for displaying the data in the OpenGL viewer, I have written the code for reading the data and displaying each triangle using glBegin(GL_TRIANGLES) and the normals for lighting. The viewer and geometry coding is same as my earlier project CadSurf. You can read as many StL (ASCII only) files into the viewer zoom, pan, rotate the views, select the objects, change attributes like color and material. The viewer provides object oriented context menus. I.e., when you click the right mouse button in the view with no active selection, you get a context menu for setting the viewer attributes, whereas with StL object selected, you get menu for the object attributes. Some sample StL data is also provided in the Data folder of the project.

Comments and Discussions

Hmm... if you helped with the c# code, you may not be doing the homework for the people asking for it. People have their own strenghts, being able to wrap this code may not be their core strengths but the surely have other strengths like being able to embed this code in an application having other great functionality beyond rendering stl.

Personally, I consider it rude that you assumed you would be doing people's homeworks by providing a c# version of a code you have already provided.

Oh, I am sorry for you if you think I sounded rude. But its not any less rude if someone cannot show up any effort and just bluntly ask for code here. I am indeed not here to do anybody's homework and the way the help has been asked here really sounds like asking for free favour so that they simply copy-paste and complete their project. Sorry, it doesn't work like that. If some one is seeking genuine help, if they are stuck at something after trying hard, they must ask specific questions related to the difficulty. They also must have the courtesy to explain what they tried so far. I am taking the pain to answer your question hoping that people like you and the other asker will understand the real use of CodeProject. This is a place to learn from someone's work and share your work so that others can learn. This is indeed no place to ask someone to write free code for you. Take help from the thousands of tutorials posted here, learn the C# language by putting some efforts and then port the posted code yourself. And thank someone for posting their code here for your use. That will teach you a lot and will be useful for you in the future. I hope I didn't sound rude here. All that I said is for your benefit, take it or leave it, upto you!

The root cause of the problem is that you are using a latest compiler that has more enforcement of the standards compared to the old compiler. This code should build without any major changes upto VS compilers lower than version 2005. For the code to build on VS2010 you need to replace all #include<standardlibrary.h> to #include "standardlibrary" e.g. #include "iostream" instead of iostream.h and #include "cstdlib" instead of stdlib.h. Other than that you also need to change some MFC callback signatures. In the compilers before version 2008 both iostream.h and iostream were available for backward compatibility but from 2008 and up the deprecated headers are no longer supplied. Hope this helps.

The tool seems to be very interesting. I have built the application on MS VS2010 and tried to run the same. After running the viewer is seen only for a fraction of second and collapsing. Could you please suggest any possible solution for the same.

Does it crash when you load some StL file? If so can you please set a break point at the StLReader methods and see what is happening? I am not sure what is happening. It was built using Visual Studio 6 long back and still works fine if it is built with it.

I am sorry, I didn't get what you mean by CGi. Secondly, you won't be able to get any dimensions of features from the StL model as you get in CAD models. StL models are just triangles so you cannot programatically find dimensions between features like faces, fillets, chamfers etc. Finding volume may be practically possible but relatively very difficult.

Thanks for your immediate response about all previous queries.Now when i compile your project it is fine. But when i try to open STL file it is just flick but stl file is not showing and also show the debug assertion failed error(read stl click) occur. can you send me some clue or code for how to open stl file in my view. I am using your whole code as it is and try to open stl file.

Note: your path may vary as per the installation of SDK versions. Best way is to search for the other OpenGL files like gl.h and glu.h and place glut.h in the same GL folder and then the glut32.lib file in the Lib folder 2 levels above.

i paste the glut32.dll in system32 folder but the same following error occur again:fatal error C1083: Cannot open include file: 'GL/glut.h': No such file or directory c:\users\lab-temp\desktop\stlviewer_src\stlviewer\stdafx.h

Hi,In a STL file, there are many duplicated points. How can I remove them?In order to remove duplicated points we need to search the entire point list. I think you are using linked list to store the points.In that case the searching time will be high. I tried using KD tree but still the time needed to load the stl file is high. "Meshlab" quickly loading stl meshs and removing duplicate points. How can I efficiently load a STL mesh?

Removing duplicate elements in a list can be done by using the standard template library algorithms like using the unique method of the std::list class. For that you may also have to provide equality operator for the Point class for comparison. But once you remove the duplicates the individual triangle's self-contained vertex information would be lost. To keep account of that, you must store indexes to reference the vertex of each triangle, as each unique point will be vertex to more than one triangle. This will no longer be a simple data structure but a complicated topological data structure with vertex nodes and neighbouring triangle information etc. If you need to do any processing on the StL data, it is worth doing all this. However, the application shown here just builds the visualization data structure for the StL model.

Hi Sirjith sir jiI saw your application it is very good. But when i compile your project it is fine. But when i try to open STL file it is just flick and second document open but stl file is not showing. can you send me some clue or code for how to open stl file in my view. I am using your whole code as it is and try to open stl file. i,e, i am unable to open second stl file. Thanks Osho

How do you want the dimension? If the stl is of a surface how can you get the dimension? You can at the most calculate the bounding box by sorting vertices and finding the min-max extents in x, y and z directions.

Dear sir,I am getting that CL data file in ascii format.now a days as i said,making module for cam software,is Collabcad.its open source software,right now only for two dimensional i m getting that CL data file,how can i convert it into 3d view?I can work out that collabcad software to make 2d drawing and NC command it can reciev to generate 2d simulattion environment which ultimately; gives me CLdata for 2d.pl guide me how to go further.

First of all the StL format is only vertex and normal data and not curves or surface data. Then what are you planning to write into the iges file? What you can export as iges data is only Point and Triangular face and edge data but that won't smoothen your data. What is the objective in converting stl to iges. Conversion of Iges to Stl can be understood.

You may use the code freely in your project. To read the binary format file of stl data you will have to wite a separate algo.

I do have the code I downloaded from the net once upon a time. If you want I will send it... the website doesn't exist now so I can send it to your personal mail.

I am learning nor of your systems called STL Viewer ,but I am just a newbie about VC++ and opengl .so it's a little hard for me to read the program clearly . and I hope you can add more description to the program or some informationso that I can understand it easyier . If you could mail it to me I will really appreciate it .By the way , I am working in the Reverse Engineering business .Now I am studying how to cover the holes existing in the STL files . so I wonder if you can give me some programs or system related to it . Thank you in advance . I am looking forward to your reply !

sorry for not responding you. actually im having a stl file (diamond model) which i cant import through your code. mine is meshed with polygon. i dont know how to mesh the stl model with polygons and to display the height, weight, length etc?.

i have to display the dimension when the model is viewed in front, top, bottom and side. is there any tool available to purchase. pls help me