+ Reply to Thread
Results 1 to 3 of 3

Thread: Class Examples in Rhino.Python - Trying to learn.

  1. #1

    Class Examples in Rhino.Python - Trying to learn.

    I've been learning Python and Rhino for the past couple of months, and want to get a better understanding of how classes are used within Rhino.Python. I've gone through the Rhino.Python Primer, (which was extremely helpful) but it never really covers how classes work within the context of Rhino. Similarly, the Python Docs on classes give me a pretty good understanding of how classes work, but I'm still at a loss for how to best use them.


    Could anyone point me in a direction that would help me understand how classes are used within the context of Rhino.Python? Or provide a code example which shows a good usage scenario?



    An example of what I'm currently trying to do: I have a set functions which create and modify "wooden rods" of various lengths and diameters floating in space. I want to know the location, direction, diameter, length of each of them... and I want to be able to move and sort them based on location or direction and have all of the other data move and sort with them. Right now, this is achieved by a messy combination of lists that are getting passed around and interpreted. I have a vision of creating "dowel" objects which contain all of that data in a clean package. Is this what OOP is used for?

  2. #2
    OOP is basically used to create abstractions of what you're working with so that
    it is easier to understand. It can also help you to not repeat yourself.

    Whether or not classes are valuable in your situation depends on the specifics of what
    you're doing. If you can imagine an abstract object that would nicely wrap up what your doing
    (such as a DowellCollection object), then maybe it would help. But it might also be a lot of work
    to wrap up something that is more simply represented with functions and existing data types.

    You can just create classes from scratch. There is no major differenc between making
    classes generally and making classes for Rhino.Python. You're basically just
    using Rhino.Python as library of data types and functions that you can draw
    upon to develop your class.

    There's a lot of different ways to design a class, and a lot of different data
    structures you can use in your class design. If you post more details about your functions,
    perhaps people can suggest some good solutions for you.

    Here's a simple example of writing a possible Dowel class


    Python Code:
      class Dowel(object):
          def __init__(self, point1, point2, radius=1.0):
              # here I've created a LineCurve object using two points as input.
              # we will assume that both point1 and point2 are Rhino.Geometry.Point3d
              # objects
              # the LineCurve is a class from the Rhino.Python library
              self.curve = Rhino.Geometry.LineCurve(point1, point2)
              # build some other useful info form the two initial points.
              self.vector = point2.Subtract(point1)
              self.basePlane = Rhino.Geometry.Plane(point1, self.vector)
              self.profile = Rhino.Geometry.Circle(self.basePlane, radius)
       
          def getAngle(self, otherDowell):
              # here we assume that `otherDowell` is another Dowell object
              angle = self.vector.VectorAngle(self.vector, otherDowell.vector)
              return angle
    Last edited by BenjaminGolder; 06-10-2012 at 03:28 PM.

  3. #3
    Thanks! This is very helpful.

    I ended up solving my current problem in a slightly different way, but I'll keep thinking about classes as I continue.

    I guess what I meant by Rhino.Python specific classes was making class objects and then transforming them using the existing rhinoscriptsyntax functions - like making a "board" or "dowel" object, and then translating, rotating, performing a boolean operation while keeping some other non-physical properties (direction, original location, time of creation, material, etc.) intact and tagging along.

    Here's a (modified from your example) class that I just sketched up to illustrate this:

    Python Code:
      import rhinoscriptsyntax as rs
       
      class Dowel(object):
          def __init__(self, line, radius=1.0, material):
              #This (maybe?) creates a dowel object based on a input line
              #and permanently stores the creation time and material with the geometry
              self.pointStart = rs.CurveStartPoint(line)
              self.pointEnd = rs.CurveEndPoint(line)
              self.vector = self.pointStart.Subtract(self.pointEnd)
              self.surface = rs.AddPipe(line, 0, radius, 0, 1)
              self.creation = time.strftime("%y%m%d_%Hh%Mm")
              self.material = material
       
      #Now I make a bunch of dowel objects with different properties:
      firstDowel = Dowel(line1, rad1, birch)
      secondDowel = Dowel(line2, rad2, maple)
      thirdDowel = Dowel(line3, rad3, oak)
       
      dowels = [firstDowel, secondDowel, thirdDowel]
       
      #This is the part I'm not totally clear on - how do modify those initial
      #properties using rhinoscriptsyntax functions, while keeping the
      #creation time and material intact?
      for dowel in Dowels:
          dowel.line = rs.MoveObject(dowel.line, [10,0,0])
         
         
      #Now, hopefully use the newly moved dowel to make a hole at
      #a new location in a board?
      boardWithHole = rs.BooleanDifference(board, firstDowel.surface)
       
      #And print the time that the dowel was created
      print firstDowel.creation
    Last edited by nhfoley; 06-16-2012 at 06:11 AM.

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts