Source

py2scad / py2scad / base.py

"""
Copyright 2010  IO Rodeo Inc. 

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from utility import TAB_WIDTH

class SCAD_Prog(object):

    def __init__(self,objlist = [],fn=None,fa=None,fs=None):
        self.objlist = objlist 
        self.fn = fn
        self.fa = fa
        self.fs = fs

    def add(self,obj):
        if type(obj) == list:
            self.objlist.extend(obj)
        else:
            self.objlist.append(obj)

    def __str__(self):
        rtn_str = ''
        if not self.fn == None:
            rtn_str = '%s$fn = %d;\n'%(rtn_str,self.fn)
        if not self.fa == None:
            rtn_str = '%s$fa = %d;\n'%(rtn_str,self.fa)
        if not self.fs == None:
            rtn_str = '%s$fs = %d;\n'%(rtn_str,self.fs)

        for obj in self.objlist:
            rtn_str = '%s%s\n'%(rtn_str,obj)

        return rtn_str

    def write(self,filename):
        fid = open(filename,'w')
        fid.write(get_header_str(filename))
        fid.write('%s'%(self,))
        fid.close()

class SCAD_Object(object):

    def __init__(self,center=True,mod=''):
        self.type = None
        self.center = center
        self.cmp = False
        self.mod = mod

    def center_str(self):
        return str(self.center).lower()

    def is_cmp(self):
        return self.cmp

    def cmd_str(self,tab_level=0):
        return 'Empty SCAD_Object'

    def __str__(self,tab_level=0):
        tab_str = ' '*TAB_WIDTH*tab_level
        mod_str = self.mod
        return '%s%s%s'%(tab_str, mod_str, self.cmd_str(tab_level=tab_level))

    def write(self,filename,fn=None):
        fid = open(filename,'w')
        if not fn == None:
            fid.write('$fn=%d;\n'%(fn,))
        fid.write('%s'%(self,))
        fid.close()

class SCAD_CMP_Object(SCAD_Object):

    def __init__(self,obj,center=True,mod=''):
        SCAD_Object.__init__(self,center=center,mod=mod)
        self.cmp = True 
        #self.obj = obj
        if type(obj) == list:
            self.obj = obj 
        else:
            self.obj = [obj]

    def cmd_str(self,tab_level=0):
        return 'Empty SCAD_CMP_Object'

    def __str__(self, tab_level=0):
        tab_str = ' '*TAB_WIDTH*tab_level
        mod_str = self.mod
        rtn_str = '%s%s%s'%(tab_str, mod_str, self.cmd_str())
        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))
        rtn_str = '%s%s}'%(rtn_str,tab_str,)

        return rtn_str

def get_header_str(filename):
    str0 = '//' + '='*70 + '\n'
    str1 = '// %s\n'%(filename,)
    str2 = """//
// Autogenerated using py2scad. Hand editing this file is not advisable 
// as all modifications will be lost when the program which generated 
// this file is re-run. 
// 
"""
    return str0 + str1 + str2 + str0 + '\n'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.