Re: [rp-ml] algorithm&implementation for STL slicin

From: Wesley Brooks <wesbrooks_at_gmail.com>
Date: Thu Nov 02 2006 - 14:00:35 EET

Here's something that may help yourself and others on this list. It's
a very simple script to read in 3DS format files and export the
contained geometric data (these files can contain many separate
geometries, cameras, lights, etc.) and export each seperate piece of
geometry into STL format.

The data in the 3DS files may not be great for RP purposes, as some
are created for visualisation purposes only, and so may still not be
complete surfaces.

To use this code follow these instructions:

Go to http://code.enthought.com/enthon/, download their version of
python which comes pre-compiled with a load of other tools, including
vtk which is used in this example.

Install python, preferably on the route C drive on your machine. For
example mine is installed in C:\Python23.

Copy and paste the following lines into a text file and save it as a .py file.

Open a DOS window by clicking start, run, and typing CMD.

Change directory to the directory you saved the .py file. For example
to change to 'C:\Documents and Settings' you would type 'cd
C:\Documents and Settings'.

Type the name of the saved file and hit return. AT this stage the dos
window should run the program and you'll see progress messages come up
on the screen. When the export to STL is complete the script will open
up a viewing window showing the imported 3DS file.

If you would like to know more about the Python scripting language
goto www.python.org, and www.vtk.org for information on the
Visualisation Toolkit. These are open-source languages/code libraries
and there is a large amount of online documentation and loads of
mailing lists online.

For OBJ files have a look at the documentation for vtkOBJReader /
vtkOBJExporter.

Cheers,

Wesley Brooks

##### START OF PASTED PYTHON SCRIPT #####

import time
import vtk
from vtk.util.colors import *

# Full path and name for the 3DS file to use. Best to put the 3DS file
# in it's own directory as there are no checks to make sure files are
# not overwritten.
inputFileName = 'C:/Some Folder/Folder within folder/futuramaship.3ds'

print "Importing 3DS File."
print time.asctime()

importer = vtk.vtk3DSImporter()
importer.ComputeNormalsOn()
importer.SetFileName(inputFileName)
importer.Read()

print "Imported 3DS File."
print time.asctime()

renWin = importer.GetRenderWindow()
ren = importer.GetRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)

print "Spliting imported data."
print time.asctime()

actors = ren.GetActors()
num = actors.GetNumberOfItems()
actors.InitTraversal()

print "Number of actors:", num
print time.asctime()

for actorID in xrange(num):
    print "Exporting data for %i part to STL." %actorID
    print time.asctime()
    # The following line of code creates a new ending for the file name
    # for each of the parts created. the %i adds the interger value
    # supplied outside the speach marks after the % character.
    nameEnd = "_SplitFile_%i.stl" %actorID
    # The following line of code creates a file name for the new part.
    # The inputFileName is a string as set on line 8. The [:-4] on the
    # end returns a slice of the string, in this case all but the last
    # four characters, .3DS in this files case.
    outputFileName = inputFileName[:-4] + nameEnd
    actor = actors.GetNextActor()
    mapper = actor.GetMapper()
    polydata = mapper.GetInput()
    # The following three lines of code ensure the geometry is represented
    # by triangles only.
    triangleFilter = vtk.vtkTriangleFilter()
    triangleFilter.SetInput(polydata)
    polydata = triangleFilter.GetOutput()
    # The following three lines ensures the computer is aware that coincident
    # points are 'cleaned up' and treated as one point. This helps ensure the
    # best quality STL file.
    cleaner = vtk.vtkCleanPolyData()
    cleaner.SetInput(polydata)
    cleaner.SetAbsoluteTolerance(0.)
    polydata = cleaner.GetOutput()
    # The following five lines output the data to a binary stl file. To write
    # to an readable format file remove, or comment out line 67 with a #
    # character.
    writer = vtk.vtkSTLWriter()
    writer.SetFileName(outputFileName)
    writer.SetFileTypeToBinary()
    writer.SetInput(polydata)
    writer.Write()

print "Completed export, veiwing 3DS File."
print time.asctime()

# The following lines put the initial 3DS data into a viewing window.
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.Initialize()
renWin.Render()
iren.Start()

print "Script complete."
print time.asctime()

##### END OF PASTED PYTHON SCRIPT #####

On 31/10/06, Cristina Kadar <cristina_kadar@yahoo.com> wrote:
> Dear List,
>
> It was really nice to see so many helpful messages following my inquiry. I
> had been working at my application during the last 2 months and now a first
> working version is running in 2 labs. :).
>
> To answer some of your questions:
>
> Jeff : No, the task was not just find a solution (aka a commercial software
> which does that), but really do a specific program for the setups in the
> labs which has to be integrated with some other programs which are already
> running (here the limitation to Visual Basic; as a matter of fact, I agree
> with Greg - Vb is a poor choice...)
> G.Sachs: To me, STL seems still to be the standard format. We keep
> receiving all our models as .stl files and most of the programs I saw for
> designing/exporting/converting know it.
>
> The big problem is that is very hard to find complex STL files which are
> not faulty. Right now my program can slice the file, find the closed
> contours and hatch the inner areas, BUT cannot handle files with errors. And
> of course, there are some issues about size and time...
>
> I have been looking for online STLs databases - without much success. I
> have found a huge 3D Meshes database (mostly .3ds, .obj, .wrl):
> http://www-c.inria.fr/gamma/download/ , but after
> conversion I get faulty STLs most of the time... I am looking especially for
> STLs of Santa (or something connected to Christmas) and of objects with
> movable parts (as a helicopter, wind-meal, gear, etc). Anybody who could
> share? :)
>
> For correcting faulty STL files, we are opened to buy a powerful commercial
> tool. Has anybody worked with CopyCad from Delcam (I am still waiting for
> the trial version)? 3Data Design from DeskArts seems quite powerful, but a
> lot of "manual" work...
>
> Good luck with your work as well!
>
> Kind regards,
> Cristina
>
>
>
>
>
> ________________________________
> Get your email and see which of your friends are online - Right on the new
> Yahoo.com
>
>

On 31/10/06, Cristina Kadar <cristina_kadar@yahoo.com> wrote:
> Dear List,
>
> It was really nice to see so many helpful messages following my inquiry. I
> had been working at my application during the last 2 months and now a first
> working version is running in 2 labs. :).
>
> To answer some of your questions:
>
> Jeff : No, the task was not just find a solution (aka a commercial software
> which does that), but really do a specific program for the setups in the
> labs which has to be integrated with some other programs which are already
> running (here the limitation to Visual Basic; as a matter of fact, I agree
> with Greg - Vb is a poor choice...)
> G.Sachs: To me, STL seems still to be the standard format. We keep
> receiving all our models as .stl files and most of the programs I saw for
> designing/exporting/converting know it.
>
> The big problem is that is very hard to find complex STL files which are
> not faulty. Right now my program can slice the file, find the closed
> contours and hatch the inner areas, BUT cannot handle files with errors. And
> of course, there are some issues about size and time...
>
> I have been looking for online STLs databases - without much success. I
> have found a huge 3D Meshes database (mostly .3ds, .obj, .wrl):
> http://www-c.inria.fr/gamma/download/ , but after
> conversion I get faulty STLs most of the time... I am looking especially for
> STLs of Santa (or something connected to Christmas) and of objects with
> movable parts (as a helicopter, wind-meal, gear, etc). Anybody who could
> share? :)
>
> For correcting faulty STL files, we are opened to buy a powerful commercial
> tool. Has anybody worked with CopyCad from Delcam (I am still waiting for
> the trial version)? 3Data Design from DeskArts seems quite powerful, but a
> lot of "manual" work...
>
> Good luck with your work as well!
>
> Kind regards,
> Cristina
>
>
>
>
>
> ________________________________
> Get your email and see which of your friends are online - Right on the new
> Yahoo.com
>
>
Received on Thu Nov 02 13:12:11 2006

This archive was generated by hypermail 2.1.8 : Tue Jul 21 2009 - 10:27:52 EEST