Lines in Python

Hi!
i wonder if someone knows how to define a line using Python script with out drawing it.
In other word i need to define a line for may calculation. But I do not like to add it into the graphic environment of my file.

// Mazdak

You can make “virtual” geometry directly using the RhinoCommon API, which isn’t automatically added to the Rhino document (like with the rhinoscriptsyntax package). Here’s an example:

import Rhino as rc

ptA = rc.Geometry.Point3d(0,0,0)
ptB = rc.Geometry.Point3d(10,10,10)
lineAB = rc.Geometry.Line(ptA,ptB)

print lineAB.Length

Thank you for your help!
maybe i had to describe my problem better;
what i need i a guid or object_id. because i need to use those lines in another function " rs.CurveContourPoints" to get the intersection point a line and a curve.

//

In that case, you’re probably going to have to the delete the line after you’re done with it. I might well be wrong, but I don’t think one can create geometry using rhinoscriptsyntax without adding it to the Rhino document (unless you’re in the Grasshopper Python editor).

I might be missing your point, but from the way I understand your issue, you want to have some geometry data that you generate in your python program to use later in your program but don’t want to actually add it to your model and/or display it. For some reason you want to refer to it later using a guid instead of the variable name you’ve given it in your program.

I believe the way to do that is for you to generate and assign a guid in your program. On the other hand, it would seem to me that it would be much more straightforward to just use the variable name.

If you want to give it a Rhino guid you will need to make it an actual Rhino object. You could hide it so it doesn’t display, of course.

you have understood me right.
i can draw(add) those lines to Rhino and then hide or delete them.
but i am talking about more than 3000 lines.
so being able not drawing them would save me plenty of time.

3000 lines is nothing. Just make sure you call rs.EnableRedraw(enable=False) before making your lines, and rs.EnableRedraw(enable=True) when you’re done.

2 Likes

Well, instead of using rhinoscriptsyntax for that you might want to also use RhinoCommon for generating the curve contour points… that way you don’t have to add the lines to the document and delete them afterwards. Below is the RhinoCommon code for rs.CurveContourPoints():

def CurveContourPoints(curve_id, start_point, end_point, interval=None):
    """Returns the 3D point locations calculated by contouring a curve object.
    Parameters:
      curve_id (guid): identifier of a curve object.
      start_point (point): 3D starting point of a center line.
      end_point (point): 3D ending point of a center line.
      interval (number, optional): The distance between contour curves. If omitted,
      the interval will be equal to the diagonal distance of the object's
      bounding box divided by 50.
    Returns:
      list(point, ....): A list of 3D points, one for each contour
    Example:
      import rhinoscriptsyntax as rs
      obj = rs.GetObject("Select curve", rs.filter.curve)
      start_point = rs.GetPoint("Base point of center line")
      end_point = rs.GetPoint("Endpoint of center line", start_point)
      contour = rs.CurveContourPoints(obj, start_point, end_point)
      if contour: rs.AddPoints(contour)
    See Also:
      AddSrfContourCrvs
    """
    curve = rhutil.coercecurve(curve_id, -1, True)
    start_point = rhutil.coerce3dpoint(start_point, True)
    end_point = rhutil.coerce3dpoint(end_point, True)
    if start_point.DistanceTo(end_point)<Rhino.RhinoMath.ZeroTolerance:
        raise Exception("start and end point are too close to define a line")
    if not interval:
        bbox = curve.GetBoundingBox(True)
        diagonal = bbox.Max - bbox.Min
        interval = diagonal.Length / 50.0
    rc = curve.DivideAsContour( start_point, end_point, interval )
    return list(rc)
1 Like