Rhino Python Lecture 2016 | DAY5

Contents

  1. User-defined functions
  2. Practice 1
    1. The Golden Spiral – User-defined functions
  3. Class Object
  4. Practice 2
    1. Develop Architecture Class 1
    2. Develop Architecture Class 2

1. User-defined functions

We can define and edit original function in python. It is called User-defined functions. Defining it, you use same algorithm repeatly.We can edit also number and type of argument and return values.
userFunc

import rhinoscriptsyntax as rs

def UserDefFunc1(a, b):
    
    #Add two values
    c = a + b
    
    #return as single value
    return c

#Call UserDefFunc1 and Store return value
f1 =  UserDefFunc1(5, 6)
#Print value
print f1

def UserDefFunc2(a, b):
    
    #Add two values
    c = a + b
    
    #Subtract two values
    d = a - b
    
    #return as array
    return c, d

#Call UserDefFunc2 and Store return value
f2 =  UserDefFunc2(5, 6)
#Print value
print f2[0]
print f2[1]

2. Practice 1

Please send your program data to Shuta by the end of the day
Submission is closed.

The Golden Spiral – User-defined functions

Please convert Python code to User-defined functions.
gs_userFunc

  • Argument value
    • Start Number of Fibonacci terms
    • End Number of Fibonacci terms
    • X value of start point
    • Y value of start point
    • Z value of start point
  • Return value
    • Rectangle objects
    • Arc objects
  • Algorithm of drawing The Golden Spiral
    import rhinoscriptsyntax as rs
    
    roopNum = 10
    originPt = [0.0, 0.0, 0.0]
    
    for i in range(roopNum): 
        
        if i == 0 :
            fn1 = 0  #the initial term of Fibonacci number
            fn  = 1  #the first term of Fibonacci number
        else:
            fn = fn1 + fn0 #Fibonacci progression
        
        #Print Fibonacci Num
        print  fn
        
        #Replacement of data for originPt
        if not i == 0:
            if i%4 == 0:
                originPt[0] -= fn0
            elif i%4 == 1:
                originPt[1] -= fn0
            elif i%4 == 2:
                originPt[0] += fn0
            elif i%4 == 3:
                originPt[1] += fn0
        
        #Drawing Recrangle
        rec = rs.AddRectangle(originPt, fn, fn)
        rec = rs.RotateObject(rec, originPt, 90.0*i)
        
        #Drawing Arc
        arc = rs.AddArc(originPt, fn, 90.0)
        arc = rs.RotateObject(arc, originPt, 90.0*i)
        
        fn0 = fn1 #Replacement of data for next step (f0)
        fn1 = fn #Replacement of data for next step (f1)
    

3. Class Object

Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A feature of objects is that an object’s procedures can access and often modify the data fields of the object with which they are associated (objects have a notion of “this” or “self”). In OOP, computer programs are designed by making them out of objects that interact with one another. There is significant diversity of OOP languages, but the most popular ones are class-based, meaning that objects are instances of classes, which typically also determine their type.

Many of the most widely used programming languages are multi-paradigm programming languages that support object-oriented programming to a greater or lesser degree, typically in combination with imperative, procedural programming.

Wikipedia

class

ex)Architecture() Class

class_archi

Architecture Class have 4 variables and 2 functions.If we call them, objects return self data at any time. So They behave in bottom-up, not in top-down.

class Architecture:
    
    #Class initializer
    def __init__(self, _name, _architect, _year, _structure):
        #Class variable
        self.name = _name
        self.architect = _architect
        self.year = _year
        self.structure = _structure
    
    #Class function 1
    def ageDifference(self, yourArchi):
        ageDif = self.year - yourArchi.year
        if(ageDif > 0):
            print self.name, "was built after", ageDif, "year(s) after", yourArchi.name, "was."
        elif(ageDif < 0):
            print self.name, "was built after", -ageDif, "year(s) before", yourArchi.name, "was."
        else:
            print self.name, "and", yourArchi.name , "are built in same year."
            
    #Class function 2
    def compareArchitect(self, yourArchi):
        if(self.architect == yourArchi.architect):
            print "The architects of", self.name, "and", yourArchi.name, "are same."
        else:
            print "The architects of", self.name, "and", yourArchi.name, "are not same."

#Create "Architecture" class object 
archi1 = Architecture("Villa Savoye", "Le Corbusier", 1931, "RC")
archi2 = Architecture("Farnsworth House", "Mies van der Rohe", 1950, "S")
archi3 = Architecture("Barcelona Pavilion", "Mies van der Rohe", 1929, "S")

#Call "Architecture" class variable
print archi1.name
print archi2.year
print archi3.structure 

#Call "Architecture" class functions
archi1.ageDifference(archi2)
archi2.compareArchitect(archi1)
archi3.compareArchitect(archi3)

4.Practice 2

Please send your program data to Shuta by the end of the day
Submission is closed.

Develop Architecture Class 1

Please define new class function “compareStructure()” in Architecture() Class.

  • class function: compareStructure()
    • Argument value: Architecture Class object
    • Console output: Display whether two Architecture are the same structure or not.

Develop Architecture Class 2

Please define new class value “self.country” and class function “checkContinent()” in Architecture() Class.
class_archi2

  • class value: self.country:
    • country’s name
  • class function: checkContinent()
    • Argument value: None
    • Console output: Determine continent from self.country, and display result