I must be missing something obvious but I cant seem to figure it out.
I would like to split a line into two curves then delete the shorter of the two. When it comes time to delete the polylinecurve I get an error saying it needs a Rhino object. When looking at the documentation it seems like it can take a GUID, Rhino object, ObjRef. How do I go about getting to one of the supported data types ? What is a ObjRef ? Is there no way to just delete that polylinecurve object directly ?
Thanks !
con_crvs = rs.GetObjects("Select Curves to connect to",4)
for cc in con_crvs:
cc_obj = rs.coercecurve(cc)
if R.Geometry.Intersect.Intersection.CurveCurve(off_crv,cc_obj,0.01,0.01).Count > 0:
for inter_eve in R.Geometry.Intersect.Intersection.CurveCurve(off_crv,cc_obj,0.01,0.01):
inter_param = cc_obj.ClosestPoint(inter_eve.PointA)
cc_split = cc_obj.Split(inter_param[1])
len_l = []
for ccs in cc_split:
len_l.append(ccs.GetLength())
print max(len_l)
print min(len_l)
for cca in cc_split:
#print cca.GetLength()
if round(cca.GetLength(),2) == round(max(len_l),2):
target_crv = cca
if round(cca.GetLength(),2) == round(min(len_l),2):
doc.Objects.Delete(cca)
Hey,
When you’re working with Rhino, there are different “types” of objects. Some are just mathematical descriptions of shapes (like the curves you get from Split) and some are actual objects in the Rhino document that you can see and manipulate.
The Delete method expects something that’s in the Rhino document, not just a mathematical description. That’s why you’re running into the error. You can’t delete something that’s not actually “there” in the document.
So to fix this you wuold typically add the longest curve to the Rhino document, replacing the original curve you split. This way youre not adding and then deleting you’re just swapping out the old curve for the new longer one.
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
con_crvs = rs.GetObjects("Select Curves to connect to", 4)
off_crv_guid = rs.GetObject("Select the offset curve", 4)
off_crv = rs.coercecurve(off_crv_guid)
for cc in con_crvs:
cc_obj = rs.coercecurve(cc)
intersections = Rhino.Geometry.Intersect.Intersection.CurveCurve(cc_obj, off_crv, 0.01, 0.01)
if intersections.Count > 0:
for inter_eve in intersections:
rc, inter_param = cc_obj.ClosestPoint(inter_eve.PointA)
cc_split = cc_obj.Split(inter_param)
len_l = []
for ccs in cc_split:
len_l.append(ccs.GetLength())
max_len = max(len_l)
for ccs in cc_split:
if round(ccs.GetLength(), 2) == round(max_len, 2):
guid = sc.doc.Objects.AddCurve(ccs)
sc.doc.Objects.Delete(cc, True)
This is another possible solution, imo more elegant
if intersections.Count > 0:
for inter_eve in intersections:
rc, inter_param = cc_obj.ClosestPoint(inter_eve.PointA)
cc_split = cc_obj.Split(inter_param)
# Find the longest curve in a Pythonic worthy way
longest_curve = max(cc_split, key=lambda c: c.GetLength())
# Replace the original curve with the longest one
sc.doc.Objects.Replace(cc, longest_curve)
I’d suggest you read the full documentation in order to understand how everything connects, anyway i hope that helps clarify your doubts,
Farouk