Commits

Ed Blake  committed 0eaf1b1

Initial revision to enable modules and variables in output file.

  • Participants
  • Parent commits 1370554

Comments (0)

Files changed (5)

 *.dxf
 *.komodo*
 *.egg-info
+.DS_Store
 build
 dist
 .komodo*

File py2scad/base.py

     """Wrapper for Openscad program."""
 
     def __init__(self, fn=None, fa=None, fs=None):
+        self.module_list = []
         self.objlist = []
         # Global facet settings
         self.fn = fn
         self.fa = fa
         self.fs = fs
 
+    def module(self, obj, **kwargs):
+        """Do something terribly clever to parameterize an object..."""
+        pass
+
     def add(self, obj):
+        """Add a scad object to this program container."""
         if type(obj) == list:
             self.objlist.extend(obj)
         else:
         if not self.fs == None:
             rtn_str = '%s$fs = %d;\n'%(rtn_str, self.fs)
 
+        for mod in self.module_list:
+            rtn_str = "{0}{1}\n\n".format(rtn_str, mod)
+
         for obj in self.objlist:
-            rtn_str = '%s%s\n'%(rtn_str,obj)
+            rtn_str = '%s%s\n\n'%(rtn_str,obj)
 
         return rtn_str
 
         rtn_str = SCAD_Object.__str__(self, tab_level=tab_level)
         rtn_str = '%s {\n'%(rtn_str,)
         for obj in self.obj:
-            rtn_str = '%s%s\n'%(rtn_str,obj.__str__(tab_level=tab_level+1))
+            try:
+                rtn_str = '%s%s\n'%(rtn_str,obj.__str__(tab_level=tab_level+1))
+            except: # Assume obj is str, otherwise it is converted...
+                rtn_str = "{0}{1}\n".format(rtn_str, (" "*(TAB_WIDTH*tab_level+1))+obj)
         rtn_str = '%s%s}'%(rtn_str,tab_str,)
 
         return rtn_str

File py2scad/primitives.py

 
 class Cube(base.SCAD_Object):
 
-    def __init__(self, size=1.0, center=True, mod=''):
-        base.SCAD_Object.__init__(self,center=center,mod=mod)
-        try:
-            self.size = float(size)
-        except:
-            self.size = utility.float_list3(size)
+    def __init__(self, size=1.0, center=True, *args, **kwargs):
+        base.SCAD_Object.__init__(self, center=center, *args, **kwargs)
+        self.size = size
 
     def cmd_str(self,tab_level=0):
         size_str = utility.val_to_str(self.size)
         center_str = self.center_str()
-        return 'cube(size=%s,center=%s);'%(size_str,center_str)
+        return 'cube(size=%s,center=%s);'%(size_str, center_str)
 
 class Sphere(base.SCAD_Object):
 
-    def __init__(self, r=1.0, center=True,mod=''):
-        base.SCAD_Object.__init__(self,center=center,mod=mod)
+    def __init__(self, r=1.0, center=True, *args, **kwargs):
+        base.SCAD_Object.__init__(self, center=center, *args, **kwargs)
         self.r = float(r)
 
     def cmd_str(self,tab_level=0):
 
 class Cylinder(base.SCAD_Object):
 
-    def __init__(self, h=1.0, r1=1.0, r2=None, center=True, mod=''):
-        base.SCAD_Object.__init__(self,center=center,mod=mod)
+    def __init__(self, h=1.0, r1=1.0, r2=None, center=True, *args, **kwargs):
+        base.SCAD_Object.__init__(self, center=center, *args, **kwargs)
         self.h = float(h)
         self.r1 = float(r1)
         # r2 is optional
 
 class Polyhedron(base.SCAD_Object):
 
-    def __init__(self, points, faces, center=True, mod=''):
-        base.SCAD_Object.__init__(self,center=center,mod=mod)
+    def __init__(self, points, faces, center=True, *args, **kwargs):
+        base.SCAD_Object.__init__(self, center=center, *args, **kwargs)
         self.points = [utility.float_list3(x) for x in points]
         self.faces = [utility.float_list(x) for x in faces]
 
 
 class Import_STL(base.SCAD_Object):
 
-    def __init__(self, filename, convexity=5,mod=''):
-        base.SCAD_Object.__init__(self,mod=mod)
+    def __init__(self, filename, convexity=5, *args, **kwargs):
+        base.SCAD_Object.__init__(self, *args, **kwargs)
         self.filename = filename
         self.convexity = convexity
 
 
 class Circle(base.SCAD_Object):
 
-    def __init__(self,r=1,mod=''):
-        base.SCAD_Object.__init__(self,mod=mod)
+    def __init__(self, r=1, *args, **kwargs):
+        base.SCAD_Object.__init__(self, *args, **kwargs)
         self.r = float(r)
 
     def cmd_str(self,tab_level=0):
 
 class Square(base.SCAD_Object):
 
-    def __init__(self,size=[1,1],center=True, mod=''):
-        base.SCAD_Object.__init__(self,center=center,mod=mod)
+    def __init__(self, size=[1,1], center=True, *args, **kwargs):
+        base.SCAD_Object.__init__(self, center=center, *args, **kwargs)
         self.size = utility.float_list2(size)
 
     def cmd_str(self,tab_level=0):
 
 class Polygon(base.SCAD_Object):
 
-    def __init__(self,points,paths,mod=''):
-        base.SCAD_Object.__init__(self,mod=mod)
+    def __init__(self, points, paths, *args, **kwargs):
+        base.SCAD_Object.__init__(self, *args, **kwargs)
         self.points = [utility.float_list2(p) for p in points]
         self.paths = [utility.float_list(p) for p in paths]
 

File py2scad/transforms.py

 import base
 import utility
 
+# Assembly creation ----------------------------------------------------------
+
+class Assembly(base.SCAD_CMP_Object):
+    """Create a reusable assembly (module) object."""
+    def __init__(self, obj, name, parameters=[], *args, **kwargs):
+        base.SCAD_CMP_Object.__init__(self, obj, **kwargs)
+        self.name = name
+        # Filter out any non-string arguments (unicode removed from py3)
+        self.args = tuple([arg for arg in args if type(arg) == str])
+
+    def cmd_str(self,tab_level=0):
+        """Outputs the module signature."""
+        return 'module {0}({1})'.format(self.name, ', '.join(arg for arg in self.args))
+
+    def __call__(self, *args):
+        """Returns a string calling this module with provided arguments."""
+        if len(args) > len(self.args):
+            raise TypeError("{0}() takes exactly {1} argument(s) ({2} given)".format(self.name, len(self.args), len(args)))
+        return "{0}({1});".format(self.name, ', '.join(arg for arg in args))
+
 # 3D transformations ---------------------------------------------------------
 
 class Scale(base.SCAD_CMP_Object):
     """Scale contained object along local x,y,z."""
-    def __init__(self,obj,v=[1.0,1.0,1.0], mod=''):
-        base.SCAD_CMP_Object.__init__(self, obj, mod=mod)
+    def __init__(self,obj,v=[1.0,1.0,1.0], *args, **kwargs):
+        base.SCAD_CMP_Object.__init__(self, obj, *args, **kwargs)
         self.v = utility.float_list3(v)
 
     def cmd_str(self,tab_level=0):
 class Rotate(base.SCAD_CMP_Object):
     """Rotate contained objects."""
 
-    def __init__(self, obj, v=[1.0,0.0,0.0], a=None, mod=''):
-        base.SCAD_CMP_Object.__init__(self,obj,mod=mod)
+    def __init__(self, obj, v=[1.0,0.0,0.0], a=None, *args, **kwargs):
+        base.SCAD_CMP_Object.__init__(self, obj, *args, **kwargs)
         self.v = utility.float_list3(v)
         self.a = a
 
 
 class AnimRotate(base.SCAD_CMP_Object):
 
-    def __init__(self,obj,a=0.0,v=[0.0,0.0,0.0],mod=''):
-        base.SCAD_CMP_Object.__init__(self,obj,mod=mod)
+    def __init__(self,obj,a=0.0,v=[0.0,0.0,0.0], *args, **kwargs):
+        base.SCAD_CMP_Object.__init__(self, obj, *args, **kwargs)
         self.a = a
         self.v = v
 
 
 class Translate(base.SCAD_CMP_Object):
 
-    def __init__(self,obj,v=[0.0,0.0,0.0],mod=''):
-        base.SCAD_CMP_Object.__init__(self,obj,mod=mod)
+    def __init__(self,obj,v=[0.0,0.0,0.0], *args, **kwargs):
+        base.SCAD_CMP_Object.__init__(self, obj, *args, **kwargs)
         self.v = utility.float_list3(v)
 
     def cmd_str(self,tab_level=0):
 
 class AnimTranslate(base.SCAD_CMP_Object):
 
-    def __init__(self,obj,v=[0.0,0.0,0.0],mod=''):
-        base.SCAD_CMP_Object.__init__(self,obj,mod=mod)
+    def __init__(self,obj,v=[0.0,0.0,0.0], *args, **kwargs):
+        base.SCAD_CMP_Object.__init__(self, obj, *args, **kwargs)
         self.v = v
 
     def cmd_str(self,tab_level=0):
 
 class Mirror(base.SCAD_CMP_Object):
 
-    def __init__(self,obj,v=[1.0,0.0,0.0],mod=''):
-        base.SCAD_CMP_Object.__init__(self,obj,mod=mod)
+    def __init__(self,obj,v=[1.0,0.0,0.0], *args, **kwargs):
+        base.SCAD_CMP_Object.__init__(self, obj, *args, **kwargs)
         self.v = utility.float_list3(v)
 
     def cmd_str(self,tab_level=0):
     #######################################
 
     def __init__(self,obj):
-        base.SCAD_CMP_Object.__init__(self,obj,mod=mod)
+        base.SCAD_CMP_Object.__init__(self, obj, *args, **kwargs)
         pass
 
 class Color(base.SCAD_CMP_Object):
 
-    def __init__(self,obj,rgba=[0.5, 0.5, 0.5, 1.0],mod=''):
-        base.SCAD_CMP_Object.__init__(self,obj,mod=mod)
+    def __init__(self,obj,rgba=[0.5, 0.5, 0.5, 1.0], *args, **kwargs):
+        base.SCAD_CMP_Object.__init__(self, obj, *args, **kwargs)
         self.rgba = utility.float_list4(rgba)
         assert self.rgba_ok(), 'rgba values must be in [0,1]'
 
 
 class Linear_Extrude(base.SCAD_CMP_Object):
 
-    def __init__(self,obj,h=1, twist=0, center=True, mod='', convexity=5,slices=None):
-        base.SCAD_CMP_Object.__init__(self,obj,center=center,mod=mod)
+    def __init__(self,obj,h=1, twist=0, center=True, convexity=5,
+                 slices=None, *args, **kwargs):
+        base.SCAD_CMP_Object.__init__(self,obj,center=center,mod=mod, *args, **kwargs)
         self.h = float(h)
         self.twist = float(twist)
         self.convexity = int(convexity)
 
 class Linear_DXF_Extrude(base.SCAD_Object):
 
-    def __init__(self,filename,height=1.0,layer=None,center=True,convexity=10,twist=0,mod=''):
-        base.SCAD_Object.__init__(self,center=center,mod=mod)
+    def __init__(self, filename, height=1.0, layer=None, center=True,
+                 convexity=10, twist=0, *args, **kwargs):
+        base.SCAD_Object.__init__(self, center=center, *args, **kwargs)
         self.filename = filename
         self.height = float(height)
         self.layer = layer
 
 class Rotate_Extrude(base.SCAD_CMP_Object):
 
-    def __init__(self,obj,convexity=5,mod=''):
-        base.SCAD_CMP_Object.__init__(self,obj,mod=mod)
+    def __init__(self,obj,convexity=5, *args, **kwargs):
+        base.SCAD_CMP_Object.__init__(self, obj, *args, **kwargs)
         self.convexity = convexity
 
     def cmd_str(self,tab_level=0):
 
 class Projection(base.SCAD_CMP_Object):
 
-    def __init__(self,obj,cut=True,mod=''):
-        base.SCAD_CMP_Object.__init__(self,obj,mod=mod)
+    def __init__(self,obj,cut=True, *args, **kwargs):
+        base.SCAD_CMP_Object.__init__(self, obj, *args, **kwargs)
         self.cut = cut
 
     def cmd_str(self,tab_level=0):

File py2scad/utility.py

     _v = [float(x) for x in _v]
     return _v
 
-def val_to_str(x,tab_level=0):
-    tab_str = ''
-    if tab_level:
-        tab_str = ' '*TAB_WIDTH*tab_level
-    if type(x) == float:
-        x_str = '%s%f'%(tab_str,x,)
-    else:
-        x_str = '%s['%(tab_str,)
-        cnt = 0
-        for i,y in enumerate(x):
-            x_str = '%s%s'%(x_str,val_to_str(y))
-            if i < len(x)-1:
-                x_str = '%s, '%(x_str,)
-            cnt += 1
-            if cnt >= 8:
-                cnt = 0
-                x_str = '%s\n%s'%(x_str,tab_str)
-        x_str = '%s]'%(x_str,)
-    return x_str
+def val_to_str(val ,tab_level=0):
+    """Ensure misc values are nicely formatted."""
+    tab_str = '' + ' '*TAB_WIDTH*tab_level
+    if type(val) == str:
+        return tab_str + val
+    try: # For sequence types produce a comma seperated listing
+        iter(val) # prescribed way to check for iteration...
+        # Just because I like the fixed width numbers
+        str_val = list()
+        for item in val:
+            if type(item) != str: # Format as float, five decimals precision
+                item = "{0:0.5f}".format(item)
+            str_val.append(item)
+        return tab_str + '[' + ', '.join("{0}".format(item) for item in str_val) + ']'
+    except TypeError: # Format as float, five decimals precision
+        return tab_str + "{0:0.5f}".format(val)
+
 
 def write_obj_list(obj_list, filename, fn=100):
     fid = open(filename,'w')