Both sides offset using IronPython

Hi,I needed some help regarding implementation of both sides offset using IronPython scripting.At present I am using the sample offset code given in the documentation in which it asks only to click the side in which we want to offset thereby neglecting any additional options pertaining to BothSides offset as well as Cap option.

Well, the rhinoscriptsyntax functions (I assume that’s what you’re talking about here) are lower level than the Rhino functions, so to get the same kinds of options, you generally have to code them yourself. However, the rhinoscriptsyntax version does not ask you to click a side (the Rhino native function does) so I’m not sure if you’re talking about scripting the native Rhino command via rs.Command() or using Python rhinoscriptsyntax.

For offsetting both sides, I’ve found that the RhinoCommon method - the one that is even lower level than the rhinoscriptsyntax method - is even better, as it does not require a direction vector. All you need is to enter the distance both positive and negative and you will have a both-sides offset (getting the ends is another matter though). For the RhinoCommon you will have to dive down into “virtual” geometry though, it’s more complex. Following is a quickie sample which has no offset options, it will only do “sharp” and no ends:

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

def OffsetCurve2SidesSample():
    tol=sc.doc.ModelAbsoluteTolerance
    crvID=rs.GetObject("Select curve to offset 2 sides",4,preselect=True)
    if not crvID: return
    dist=rs.GetReal("Offset distance",minimum=tol)
    if dist == None: return
    
    plane = rs.CurvePlane(crvID)
    if not plane: plane = rs.ViewCPlane()
    crv=sc.doc.Objects.Find(crvID).Geometry
    
    trans=Rhino.Geometry.CurveOffsetCornerStyle.Sharp
    msg="Offset 2 sides failed!"
    #do first offset positive distance
    offset1=crv.Offset(plane,dist,tol,trans)
    if offset1:
        #do second offset negative distance
        offset2=crv.Offset(plane,-dist,tol,trans)
        if offset2:
            if len(offset1)== 1 and len(offset2)==1:
                offset1ID=sc.doc.Objects.AddCurve(offset1[0])
                offset2ID=sc.doc.Objects.AddCurve(offset2[0])
                msg="Successfully offset one curve both sides"
                sc.doc.Views.Redraw()
    print msg
OffsetCurve2SidesSample()

And below is a complete script from my library (which I use fairly often) which offsets multiple curves both sides and adds the ends - it uses the same basic code as above, but it’s somewhat more involved due to the options, especially adding the ends.

HTH, --Mitch

OffsetMulticrvs2SidesWEnds.py (5.3 KB)

4 Likes

Thanks Mitch!.It was really helpful.

Hi,can we add ends to this offset?