+ Reply to Thread
Results 1 to 8 of 8

Thread: Data from "Line" Objects

  1. #1

    Data from "Line" Objects

    I'm trying to use LineFitFromPoints, but I'm not sure how to use what it is returning.


    Some example code:

    p1 = (0,0,10)
    p2 = (0,1,0)
    p3 =(0,0,-10)
    points = [p1, p2, p3]
    testLine = rs.LineFitFromPoints(points)
    print testLine
    #test = rs.IsLine(testLine)
    #print test


    This prints 6 numbers. If I unhash the last two lines, I get the familiar error "parameter (testLine) must be a Guid". If I try to access the numbers as a list (testLine[0], for example) I get an error that says " Line object is unscriptable".

    I want to have access to the points of testLine, or be able to treat testLine as a curve and extract the endpoints.


    Any help? I'm going to try to look at the code of the LineFitFromPoints function now...
    Last edited by nhfoley; 06-02-2012 at 07:06 PM.

  2. #2
    Super Moderator Mitch's Avatar
    Join Date
    May 2010
    Location
    Switzerland
    Posts
    313
    The 6 numbers are correct, those are the coordinates of the end points of the line (2 xyz triples). The function returns a Rhino.Geometry.Line object, IsLine fails because it's looking for an existing line object in the document that has a GUID, but the line produced by LineFitFromPoints hasn't been added to the document yet.

    To add the line object to the document rhinoscriptsyntax:
    Python Code:
      import rhinoscriptsyntax as rs
      p1 = (0,0,10)
      p2 = (0,1,0)
      p3 =(0,0,-10)
      points = [p1, p2, p3]
      testLine = rs.LineFitFromPoints(points)
      rs.AddLine(testLine.From, testLine.To)

    or with a bit of RhinoCommon:
    Python Code:
      import rhinoscriptsyntax as rs
      import scriptcontext
      p1 = (0,0,10)
      p2 = (0,1,0)
      p3 =(0,0,-10)
      points = [p1, p2, p3]
      testLine = rs.LineFitFromPoints(points)
      scriptcontext.doc.Objects.AddLine(testLine)

    To extract the endpoints:
    Python Code:
      import rhinoscriptsyntax as rs
      p1 = (0,0,10)
      p2 = (0,1,0)
      p3 =(0,0,-10)
      points = [p1, p2, p3]
      testLine = rs.LineFitFromPoints(points)
      rs.AddPoint(testLine.From)
      rs.AddPoint(testLine.To)

    HTH, --Mitch

  3. #3
    Thanks! That is very helpful.

    I see now that the example in the help file uses the line.From and line.To syntax, but I didn't catch the significance. It's also somewhat confusing that the help file states that the LineFitFromPoints function returns "A list containing the starting and ending points of the fit line if successful." which sounds like the output should be directly accessible as points, or at least values.
    Last edited by nhfoley; 06-02-2012 at 08:55 PM.

  4. #4
    Super Moderator Mitch's Avatar
    Join Date
    May 2010
    Location
    Switzerland
    Posts
    313
    Yes, I agree this is confusing. The Help is translated from the vbRhinoscript help file and the vbRhinoscript version of this method returns two points - because vb rhinoscript isn't object oriented. All vbRhinoscript methods that return lines return an array of two points. There aren't many methods in Python/RhinoCommon that actually return a Rhino.Geometry.Line object; checking, there are also a few inconsistencies. For example PlanePlaneIntersection (which results in a line) returns a tuple of 2 point3D objects, not a RG line object.

    This is sometimes what makes Python for Rhino a bit more difficult to figure out, in accommodating both the classic vb rhinoscripting methods and the more complex OOP Python/RhinoCommon ones, occasionally the object oriented stuff floats to the top and you need to understand the basics of how it works. The editor debugger is your best friend here, with a breakpoint, you can see what type of object the method is returning. If you then go into the Rhino tree index at the right, you can find Rhino.Geometry.Line object and see that it has a From and a To property (I would have thought they would call it Start and End, though) which are point3D's. Unfortunately, there isn't much more in the way of help/explanations for this stuff yet.

    --Mitch
    Last edited by Mitch; 06-03-2012 at 08:26 AM. Reason: Clarity

  5. #5
    Great to know. This will definitely help me solve future issues.

  6. #6
    A simple way to know the type of a returned object is the Python type() builtin function.
    It returns a Type, which can tell you exactly what you are dealing with (among a lot of other things).

    A simple way to use it is just print(type(obj))

    I hope this is helpful,

    - Giulio
    _________________
    giulio@mcneel.com

  7. #7
    Senior Member Steve Baer's Avatar
    Join Date
    Apr 2010
    Location
    Seattle
    Posts
    1,472
    Blog Entries
    19
    All of the above comments are good points, but I do think that the function IsLine "should" return true if the input is an instance of a Line. I also think that it may be worth getting things like indexing operators working for lines in python. I'll look into this next week. This week I'm on vacation, so comments like this is all you're gonna see from me
    -Steve

  8. #8
    Hi guys,
    I am struggling exactly with the same on rhino python script. When things return the actual geometry or when its only the Guid. Is there a way to someone put some guidance together to make things clear? Or is there some clear documentation that explains the differences you could point us the direction to? I am finding it very confusing....
    Thanks a lot,
    buzz

+ 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