Fun with OpenSCAD and SolidPython

I have been thinking for a long time to get hands on with OpenSCAD. It is a programmers Solid 3D CAD modeller. Part of my python readings, I was looking for different fields or area of interest where python can be used and went through something called SolidPython. It  is a python frontend for solid modelling that compiles to OpenSCAD. Cool I quickly installed both the packages.

sudo apt-get install openscad

sudo pip install solidpython

I played around an example code and got a cube working. The code is here I will explain how I created the model
end of the blog

#! /usr/bin/env python
# -*- coding: UTF-8 -*-
from __future__ import division
import os, sys, re

from solid import *
from solid.utils import *
from solid import screw_thread

def assembly():
    c = cube(10)
    return  c

if __name__ == '__main__':
    out_dir = sys.argv[1] if len(sys.argv) > 1 else os.curdir
    file_out = os.path.join( out_dir, 'cube.scad')

    a = assembly()
    print("%(__file__)s: SCAD file written to: \n%(file_out)s"%vars())
    scad_render_to_file( a, file_out, include_orig_code=True)

I will explain how I created the cube model as shown above.
Save the python code and execute it as follows

$ python SCAD file written to:

This will generate cube.scad file.

Now run the OpenSCAD and open the cube.scad and type ‘F5′ or in the menu Design->Compile.
You will get this output.

Python readings – Library – CSV

import csv
csvlist = []
for i in range(0, 3):
   name = raw_input("Enter name of the Animal:")
   place = raw_input("Enter place of origin:")
   csvlist.append([name, place])

print csvlist

print "Creating Zoo CSV file"
   with open("Zoo.csv","wt") as csvfile:
     csvwriter = csv.writer(csvfile)
     csvwriter.writerow(["Animal", "Place"])
     for row in csvlist:
   print "Some Exception, unable to write, quitting..."
   with open("Zoo.csv","r") as csvfile:
   csvreader = csv.reader(csvfile)
   for name, animal in csvreader:
      print name, animal
   print "Some Exception unable to read, quitting..."
Enter name of the Animal:Tiger
Enter place of origin:India
Enter name of the Animal:Kangaroo
Enter place of origin:Australia
Enter name of the Animal:Elephant
Enter place of origin:Africa
[['Tiger', 'India'], ['Kangaroo', 'Australia'], ['Elephant', 'Africa']]
Creating Zoo CSV file
Animal Place
Tiger India
Kangaroo Australia
Elephant Africa

Python readings – Library – Json

import json 

fruit = dict(apple=1, berry=3, cherry=10, date=12, elderberry=22)

days = ("Montag", "Dienstag","Mittwoch","Donnerstag",
 "Freitag", "Samstag","Sonntag")

mix = [ fruit, days ]

with open("output.json","w") as fhandle:
 json.dump(mix, fhandle)

with open("output.json","rb") as fhandle:
 newmix = json.load(fhandle)

newfruits, newdays = newmix

print type(mix), type(newmix)
print type(days), type(newdays)
print type(fruit), type(newfruits)

print newdays
print newfruits
<type 'list'> <type 'list'>
<type 'tuple'> <type 'tuple'>
<type 'dict'> <type 'dict'>
('Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag')
{'date': 12, 'cherry': 10, 'berry': 3, 'elderberry': 22, 'apple': 1}
[{"date": 12, "cherry": 10, "berry": 3, "elderberry": 22, "apple": 1},
["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"]]
I found a command json_pp which seems to pretty print the json.
$json_pp < output.json
 "cherry" : 10,
 "apple" : 1,
 "date" : 12,
 "elderberry" : 22,
 "berry" : 3