Is there a way to move the start and end point of the line segment to a new location? I see that there are From and To properties and I can change them to a new point but the line segment does not update. I’m sure this is more complicated than it appears to be.
A RhinoCommon polyline is basically just a list of points. If you have the points which correspond to the segment in question (that you want to move), you should be able to simply change the corresponding point values in the list of points to their new locations.
To what @Willem and @Helvetosaur already mentioned, I would add that to move a point, you basically need another point, a location that you want to move the point to or towards.
Now, points are usually moved by adding them to a directional vector. You can get that vector by subtracting the initial point from the target point. When you now add the initial point to that specific vector, you’ll end up with a point at the target location. If you don’t want to move the point all the way, you can for instance unitise the directional vector - meaning scale it to magnitude 1 -, and multiply it by a desired distance value, before adding it to the initial point. You can also scale vector by multiplying it by a scalar value.
A RhinoCommon polyline is basically just a list of points. If you have the points which correspond to the segment in question (that you want to move), you should be able to simply change the corresponding point values in the list of points to their new locations.
So far I’ve been able to get the polylline, and do see that it has a list of 3D points. I just set up the macro to change the point location of the first point in the list. It changes it clearly when referencing the code window but nothing happens after that. This is where I am stuck. Code changes the value of pline[0] but doesn’t physically move the point. Do I need a move command mixed in to move the point to a new location, as suggested by others in the post? I need a strong nudge in the right direction.
Here is my code so far:
import rhinoscriptsyntax as rs
import Rhino
from scriptcontext import doc
# let user pick a curve
picked_crv = rs.GetCurveObject("Please select a curve for moving endpoint",True,False)
# new endpoint
newPt = Rhino.Geometry.Point3d(12, 0, 0)
# coerce GUID to get the curve geometry
rg_crv = rs.coercecurve(picked_crv[0])
pline = Rhino.Geometry.PolylineCurve.ToPolyline(rg_crv)
pline[0] = newPt
id2 = doc.Objects.Add(rg_crv)
Thank you. I see the point does move now. Thank you.
Eric
Example Code:
import rhinoscriptsyntax as rs
import Rhino
from scriptcontext import doc
import scriptcontext
ply_thick = .75
def movePoints(pNum,name):
#Move base line to Ply Thickness
picked_crv = rs.ObjectsByName(name,False)
# coerce GUID to get the curve geometry
rg_crv = rs.coercecurve(picked_crv[0])
#Get PolylineCurve
pCurve = rg_crv.ToPolyline(scriptcontext.doc.ModelAbsoluteTolerance,scriptcontext.doc.ModelAngleToleranceRadians,0,0)
#Get Polyline
pLine = Rhino.Geometry.PolylineCurve.ToPolyline(pCurve)
#Cycle through pNum and move points (Ex: pline[3] = Rhino.Geometry.Point3d(pline[3].X,ply_thick,pline[3].Z))
for i in pNum:
pLine[i[0]] = Rhino.Geometry.Point3d( float(i[1][0]) , float(i[1][1]) , float(i[1][2]))
#Replace Original with New One (uses same id)
scriptcontext.doc.Objects.Replace(picked_crv[0],pLine)
#Refresh Screen
rs.Redraw()
#Move Base Line to Ply Thickness
#Define a list of new point locations (Ex: [point2,[90,0,0]]
pNum = list()
pNum.append( [2,[0.000,ply_thick,0.000]] )
pNum.append( [3,[5.354,ply_thick,0.000]] )
pNum.append( [6,[16.866,ply_thick,0.000]] )
pNum.append( [7,[20.720,ply_thick,0.000]] )
#pNum = [[2,[0.000,ply_thick,0.000]],[3,[5.354,ply_thick,0.000]],[6,[16.866,ply_thick,0.000]],[7,[20.720,ply_thick,0.000]]]
movePoints(pNum,'Side')
You can also replace the original with the new one (keeping the same ID) by using sc.doc.Objects.Replace() - check the API help for the options on this.
I seem to have discovered that if I have a RhinoCam toolpath assigned to a curve and I replace that curve with a modified curve that the tool path sticks and only needs to be regenerated. If that is the case this is a game changer for me. Can you verify that this is the case.
That’s probably the case, as all geometry in a Rhino document is referenced by ID, so if you modify a curve but its ID remains the same, the toolpaths should remain referenced as well.
Thanks for verifying. I did put together a little script to test this out and it did work. Game changer for me as I said. Most of what I have been working on is preparing geometry to be machined with RhinoCam. I can now assign a toolpath to the object, save it in the file and modify it without losing the toolpath connection. Thanks for revealing that function to me. I appreciate it.