Python Advanced Intro #01 | Object-Oriented Programing
Contents
What’s OOP?
Outline
Object-Oriented Programing (OOP) is one of the method for programing. The one we used in previous tutorial is Procedural Programing (PP). OOP is the method to define “object” which has several “properties” and “behaviors” in contrast to PP is the method to make workflow with variables and functions.
These two codes are examples showing difference between OOP and PP.
PP
import rhinoscriptsyntax as rs verts = [] verts.append(rs.AddPoint(pt.X-width/2,pt.Y-height/2,0)) verts.append(rs.AddPoint(pt.X+width/2,pt.Y-height/2,0)) verts.append(rs.AddPoint(pt.X+width/2,pt.Y+height/2,0)) verts.append(rs.AddPoint(pt.X-width/2,pt.Y+height/2,0)) verts.append(rs.AddPoint(pt.X-width/2,pt.Y-height/2,0)) a = rs.AddPolyline(verts)
OOP
import rhinoscriptsyntax as rs class Rect: def __init__(self, pt, width, height): self.center = pt self.w = width self.h = height def geometry(self): verts = [] verts.append(rs.AddPoint(self.center.X-self.w/2,self.center.Y-self.h/2,0)) verts.append(rs.AddPoint(self.center.X+self.w/2,self.center.Y-self.h/2,0)) verts.append(rs.AddPoint(self.center.X+self.w/2,self.center.Y+self.h/2,0)) verts.append(rs.AddPoint(self.center.X-self.w/2,self.center.Y+self.h/2,0)) verts.append(rs.AddPoint(self.center.X-self.w/2,self.center.Y-self.h/2,0)) return rs.AddPolyline(verts) a = Rect(pt,w,h)
Class
Class is used for defining “objects”. Class is consisted of 3 elements – property variable, method, constructor. Property variables are used for defining properties of the object, method for behavior, and constructor is for initialization.
class Rect: //constructor def __init__(self, pt, width, height): //property variables self.center = pt self.w = width self.h = height //methods def geometry(self): verts = [] verts.append(rs.AddPoint(self.center.X-self.w/2,self.center.Y-self.h/2,0)) verts.append(rs.AddPoint(self.center.X+self.w/2,self.center.Y-self.h/2,0)) verts.append(rs.AddPoint(self.center.X+self.w/2,self.center.Y+self.h/2,0)) verts.append(rs.AddPoint(self.center.X-self.w/2,self.center.Y+self.h/2,0)) verts.append(rs.AddPoint(self.center.X-self.w/2,self.center.Y-self.h/2,0)) return rs.AddPolyline(verts)
Practice
Component system
Thinking about component based structures, it has several geometric properties and shapes.
The Component which is shown at the right, has center point and vectors to the connected point as geometric properties and skeleton lines and pipe based shape as shape.
- Property
- center point
- target vectors
- Behavior
- generate skeleton
- generate pipe geometries
import ghpythonlib.components as gh class component: def __init__(self, center, targets, range): self.center = center self.vectors = [] for pt in targets: dist = gh.Distance(center,pt) if dist!=0 and dist<range: vec = gh.Vector2Pt(center,pt) self.vectors.append(vec) def skeleton(self): lines = [] for vec in self.vectors: length = gh.VectorLength(vec) line = gh.LineSDL(self.center, vec, length*0.5) lines.append(line) return lines def geometry(self,radius): geos = [] sph = gh.Sphere(gh.XYPlane(self.center),radius*1.2) geos.append(sph) for vec in self.vectors: length = gh.VectorLength(vec) line = gh.LineSDL(self.center, vec, length*0.5) pipe = gh.Pipe(line,radius,1) geos.append(pipe) geo = gh.SolidUnion(geos) return geo a = component(compPt, Pts , range)