Commits

iorodeo committed b8333dd

Updated viewer to take a configuration file to set color, opacity, etc of different objects.

Comments (0)

Files changed (4)

 *.swp
 *.scad
 *.stl
+*.pkl
 *.dxf

examples/create_stl_w_param.py

+"""
+Create parts, save scad files and then use openscad form the command
+line to convert the scad files to stl files. 
+"""
+import os
+import pickle
+from py2scad import *
+
+c0 = Cylinder(h=1,r1=1,r2=1)
+c0 = Translate(c0,v=[5,0,0])
+
+c1 = Cube(size=[3,3,0.3])
+c1 = Translate(c1,v=[-5,0,0])
+
+c2 = Sphere(r=2)
+c2 = Translate(c2,v=[5,5,0])
+
+print 'writng scad files'
+
+prog0 = SCAD_Prog()
+prog0.fn = 40
+prog0.add(c0)
+prog0.write('test0.scad')
+
+
+prog1 = SCAD_Prog()
+prog1.fn = 40
+prog1.add(c1)
+prog1.write('test1.scad')
+
+prog2 = SCAD_Prog()
+prog2.fn = 40
+prog2.add(c2)
+prog2.write('test2.scad')
+
+print 'writing stl files'
+
+os.system('openscad -s test0.stl test0.scad')
+os.system('openscad -s test1.stl test1.scad')
+os.system('openscad -s test2.stl test2.scad')
+
+obj_list = [ 
+        {
+            'filename'   : 'test0.stl',
+            'parameters' : { 
+                'color'          : (0.5,0.5,0.5), 
+                'opacity'        : 1.0, 
+                'specular'       : 1.0, 
+                'diffuse'        : 0.8, 
+                'specular_power' : 1.0,
+                },
+            },
+        { 
+            'filename'   : 'test1.stl',
+            'parameters' : {
+                'color'          : (0.4, 0.4, 0.9), 
+                'opacity'        : 0.1, 
+                'specular'       : 1.0, 
+                'diffuse'        : 0.8, 
+                'specular_power' : 0.6,
+                },
+            },
+        {
+            'filename'   : 'test2.stl',
+            'parameters' : {
+                'color'          : (0.9, 0.9, 0.9), 
+                'opacity'        : 1.0,
+                'specular'       : 0.5,
+                'diffuse'        : 0.5,
+                'specular_power' : 0.7,
+                }
+            }
+        ]
+
+config = {
+        'background' : (0.4, 0.4, 0.4),
+        'size'       : (600,600),
+        'objects'    : obj_list,
+        }
+
+with open('testconfig.pkl','w') as fid:
+    pickle.dump(config,fid)
+

examples/view_stl.py

 # Author: William Dickson 12/21/04
 #
 # --------------------------------------------------------------
-import vtk, sys
+import vtk
+import sys
+import optparse
+import pickle
 
-stl_files = sys.argv[1:]
+# Default parameters 
+background = (0.1, 0.2, 0.4)
+window_sz = (600,600)
 
-actor_list = []
-for f in stl_files:
+# Set up options parser
+usage = ''
+
+parser = optparse.OptionParser(usage=usage)
+parser.add_option('-c','--config', 
+        help='plotting configuration file',
+        action='store', 
+        type='string', 
+        dest='configfile')
+
+options, args = parser.parse_args()
+
+# Read stl file names given on the command line
+stl_files = []
+for f in args:
+    stl_files.append((f,None))
+
+# Read configuration file if it exists
+if not options.configfile == None:
+    have_config = True
+    with open(options.configfile,'r') as fid:
+        config = pickle.load(fid)
+
+    # Get background color and window size
+    try:
+        background = config['background']
+    except KeyError:
+        pass
+
+    try:
+        window_sz = config['size']
+    except KeyError:
+        pass
+
+    # Read in list of stl objects and parameters
+    if config.has_key('objects'):
+        for obj in config['objects']:
+            stl_files.append((obj['filename'],obj['parameters']))
+          
+
+# Create the Renderer, RenderWindow, and RenderWindowInteractor
+ren = vtk.vtkRenderer()
+ren_win = vtk.vtkRenderWindow()
+ren_win.AddRenderer(ren)
+iren = vtk.vtkRenderWindowInteractor()
+iren.SetRenderWindow(ren_win)
+
+# Set the background color and window size
+ren.SetBackground(*background)
+ren_win.SetSize(*window_sz)
+
+# Create Actors for stl objects
+for f, prm in stl_files:
 
     # Create the reader and read a data file.  
     sr = vtk.vtkSTLReader()
     sr.SetFileName(f)
 
-    # Connect the mapper and actor 
-    stlMapper = vtk.vtkPolyDataMapper()
-    stlMapper.SetInput(sr.GetOutput())
-    stlActor = vtk.vtkLODActor()
-    stlActor.SetMapper(stlMapper)
-    actor_list.append(stlActor)
+    # Setup actors
+    stl_mapper = vtk.vtkPolyDataMapper()
+    stl_mapper.ScalarVisibilityOff()
+    stl_mapper.SetInput(sr.GetOutput())
 
-# Create the Renderer, RenderWindow, and RenderWindowInteractor
-ren = vtk.vtkRenderer()
-renWin = vtk.vtkRenderWindow()
-renWin.AddRenderer(ren)
-iren = vtk.vtkRenderWindowInteractor()
-iren.SetRenderWindow(renWin)
+    stl_actor = vtk.vtkActor()
+    stl_actor.SetMapper(stl_mapper)
+    prop = stl_actor.GetProperty()
 
-# Add the actors to the render; set the background and size
-for a in actor_list:
-    ren.AddActor(a)
+    if not prm == None:
+        print 'setting parameters'
+        try:
+            prop.SetColor(*prm['color'])
+        except KeyError:
+            pass
+        try: 
+            prop.SetOpacity(prm['opacity'])
+        except KeyError:
+            pass
+        try:
+            prop.SetSpecular(prm['specular'])
+        except KeyError:
+            pass
+        try:
+            prop.SetDiffuse(prm['diffuse'])
+        except KeyError:
+            pass
+        try:
+            prop.SetSpecularPower(prm['specular_power'])
+        except KeyError:
+            pass
+        try:
+            prop.SetAmbient(prm['ambient'])
+        except KeyError:
+            pass
 
-ren.SetBackground(0.1, 0.2, 0.4)
-renWin.SetSize(600, 600)
+    print 'done'
+
+    prop.SetInterpolationToGouraud()
+    stl_mapper.Update()
+    ren.AddActor(stl_actor)
+
 
 # Render
 iren.Initialize()
 ren.ResetCameraClippingRange()
-renWin.Render()
+ren_win.Render()
 iren.Start()
 
 class SCAD_Prog(object):
 
-    def __init__(self,objlist = [],fn=None,fa=None,fs=None):
-        self.objlist = objlist 
+    def __init__(self,fn=None,fa=None,fs=None):
+        self.objlist = []
         self.fn = fn
         self.fa = fa
         self.fs = fs