Exercise 1: Python Batch Program

Exercise 1: Python Batch Program

Drawing Tiles in Rhino Python

This exercise is the beginning to learn how to use Rhino Python with 2-Dimensional objects. The code uses basic commands from Python and also uses commands inside Rhino. Some Rhino commands like array cannot be used in python, so instead “rs.command ()” is used to activate the command from inside Rhino.

The code begins with overall commands to clear the file.

#this links python 
import rhinoscriptsyntax as rs 
#clears the file 
rs.DeleteObjects(rs.AllObjects())

Continuing on, the tile is created and transformed.

#add first curve
rs.AddCurve (((0,0,0), (4,4,0), (8,0,0)))
#name the first curve
CurveA = rs.FirstObject()
#copy first curve
rs.SelectObject(rs.AllObjects())
rs.CopyObject(rs.SelectedObjects(), (0,0,0))

Later on the code becomes more difficult and commands are used inside Rhino itself instead of inside Python

#group all curves 
rs.SelectObjects(rs.AllObjects()) 
rs.Command("group enter")

 

Python code for Exercise 1:

#this links python
import rhinoscriptsyntax as rs

#clears the file
rs.DeleteObjects(rs.AllObjects())

#add first curve
rs.AddCurve (((0,0,0), (4,4,0), (8,0,0)))

#name the first curve
CurveA = rs.FirstObject()

#copy first curve
rs.SelectObject(rs.AllObjects())
rs.CopyObject(rs.SelectedObjects(), (0,0,0))
#name next curve
CurveB = rs.FirstObject()
if CurveB: rs.SelectObject(CurveB)
#rotate curve
rs.UnselectObject(CurveA)
rs.RotateObject(rs.SelectedObjects(), (0,0,0), 90)

#mirror curves
rs.MirrorObject(rs.SelectedObjects(), (4,0,0), (4,4,0), copy = True)
rs.UnselectObject(rs.AllObjects())
rs.SelectObject(CurveA)
rs.MirrorObject(rs.SelectedObjects(), (0,4,0), (4,4,0), copy = True)

#join all curves
rs.SelectObjects(rs.AllObjects())
rs.Command("join enter")

#copy, rotate, and move curves
rs.RotateObject(rs.SelectedObjects(), (4,4,0), 90, copy = True)
Group2 = rs.FirstObject()
if Group2: rs.SelectObject(Group2)
rs.UnselectAllObjects()
rs.SelectObject(Group2)
rs.MoveObject(rs.SelectedObjects(), (0,8,0))

#copy and move curves to make tile
rs.SelectObjects(rs.AllObjects())
rs.CopyObjects(rs.SelectedObjects(), (8,8,0))

#group all curves
rs.SelectObjects(rs.AllObjects())
rs.Command("group enter")

#color tile
rs.Command("-properties o c o 120,200,240 enter enter enter")

#array tile
rs.SelectObjects(rs.AllObjects())
rs.Command("array 10 10 1 16 16 enter")

rs.UnselectAllObjects()

Leave a Reply

Your email address will not be published. Required fields are marked *