Maybe it’s a bug (insect)
Dunno, this works for me:
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
crvIDs=rs.GetObjects("Select two curves that intersect",4,preselect=True)
if crvIDs and len(crvIDs)==2:
tol=sc.doc.ModelAbsoluteTolerance
crvs=[rs.coercecurve(crvID) for crvID in crvIDs]
insec=Rhino.Geometry.Intersect.Intersection.CurveCurve(crvs[0],crvs[1],tol,tol)
if insec:
test=insec[0].PointA
rs.AddPoint(test)
Edit: fixed stupid typo…
–Mitch
No, Mitch, this doesn’t work here.
I am on 6.15RC latest update.
Me too… I think. (6.15.19148.13161, 28-May-19)
Sorry, there was a typo in the above - I added the following without testing…
Note to self: always test before you post!
if crvIDs and len(crvIDs==2):
it should read
if crvIDs and len(crvIDs)==2:
No probs, I saw and fixed it but still didn’t work for me.
Have you tried your code with more curves selected?
Yeah, actually it works only with two curves
My bad, I directly tried to apply to my test model.
Uh, as far as I know, CurveCurveIntersection is for only two curves…
Yes, you’re right.
This is my algorithm
def TST():
curves_ids = rs.GetObjects("get curves: ",rs.filter.curve)
if curves_ids == None: return
curves = [rs.coercecurve(id) for id in curves_ids if id is not None]
ptlst = []
for i in range(-1,len(curves)-1):
#print curves[i]
curve_0 = curves[i-1]
curve_1 = curves[i]
curve_2 = curves[i+1]
#if not curve_0 or not curve_1 or not curve_2: return
# Calculate the intersection
intersection_tolerance = tol
overlap_tolerance = 0.0
insect_event_01 = Rhino.Geometry.Intersect.Intersection.CurveCurve(curve_0, curve_1, intersection_tolerance, overlap_tolerance)
insect_event_12 = Rhino.Geometry.Intersect.Intersection.CurveCurve(curve_1, curve_2, intersection_tolerance, overlap_tolerance)
tmp_pt_lst = []
if insect_event_01.Count > 0:
for event in insect_event_01:
tmp_pt_lst.append(Rhino.Geometry.Point3d(event.PointA))
for pt in tmp_pt_lst:#list(set())
if pt is not Rhino.Geometry.Point3d.Unset:
ptlst.append(pt)
#print ptlst
#print len(ptlst)
#print list(set(ptlst))
#print len(list(set(ptlst)))
for pt in ptlst:
if pt is not None:
sc.doc.Objects.AddPoint(pt)
I want to get rid of this part:
for event in insect_event_01:
tmp_pt_lst.append(Rhino.Geometry.Point3d(event.PointA))
but insect_event_01[0].PointA
doesn’t work
Dunno, the following works for me if I have 3 intersecting curves:
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
def TST():
curves_ids = rs.GetObjects("get curves: ",rs.filter.curve)
if curves_ids == None: return
curves = [rs.coercecurve(id) for id in curves_ids if id is not None]
ptlst = []
for i in range(-1,len(curves)-1):
curve_0 = curves[i-1]
curve_1 = curves[i]
curve_2 = curves[i+1]
intersection_tolerance = sc.doc.ModelAbsoluteTolerance
overlap_tolerance = 0.0
insect_event_01 = Rhino.Geometry.Intersect.Intersection.CurveCurve(curve_0, curve_1, intersection_tolerance, overlap_tolerance)
insect_event_12 = Rhino.Geometry.Intersect.Intersection.CurveCurve(curve_1, curve_2, intersection_tolerance, overlap_tolerance)
tmp_pt_lst = []
if insect_event_01.Count > 0: tmp_pt_lst.append(insect_event_01[0].PointA)
if insect_event_12.Count > 0: tmp_pt_lst.append(insect_event_12[0].PointA)
for pt in tmp_pt_lst: rs.AddPoint(pt)
TST()
–Mitch
The problem appears when the order of selection has a possibility to create a couple of curves that do not intersect.
My algorithm works flawless if there are 2 and 3 curves.
If you add more, in my case it’s 7, it breaks:
You need to order the curves or select them in orderly manner, that’s why I created the other thread about ordering the curves. I’m now trying to make both algorithms work together.
Yes, but that’s a separate issue. I simply answered the part that insect_event_01[0].PointA
does work if the intersection actually exists. As far as ordering the curves goes, well, good luck…
Here is the sample i promised @ivelin.peychev:
import Rhino
import scriptcontext as sc
def splitCurvesWithEachOther():
# get tolerance from doc
dTol = sc.doc.ModelAbsoluteTolerance
# get curves
result, arrObjRefs = Rhino.Input.RhinoGet.GetMultipleObjects("Pick curves", False, Rhino.DocObjects.ObjectType.Curve)
if result != Rhino.Commands.Result.Success:
return result
# convert from objRef to Curve
arrCurves = [obj.Geometry() for obj in arrObjRefs]
# empty list for split parts
listCurveSplitParts = []
# iterate over all curves
for i in range(len(arrCurves)):
# empty list for intersection parameters on test curve
arrDIntersectionParams = []
# iterate over all curves again
for j in range(len(arrCurves)):
# intersecting a curve with itself only leads to cunfusion...
if i == j: continue
# do the intersection
arrXEvents = Rhino.Geometry.Intersect.Intersection.CurveCurve(arrCurves[i], arrCurves[j], dTol, 0.0)
# in your case, Ivelyn the curves only ever intersect their neighbour once
if arrXEvents.Count != 1: continue
# we got here so we can append to the intersection parameters
arrDIntersectionParams.append(arrXEvents[0].ParameterA)
# tets if we really found 2 parameters
if len(arrDIntersectionParams) == 2:
# do the splitting
arrCurveSplit = arrCurves[i].Split(arrDIntersectionParams)
# should be 3 parts
if arrCurveSplit.Count == 3:
# keep the part in the middle
listCurveSplitParts.append(arrCurveSplit[1])
# join it all up
arrCurveJoined = Rhino.Geometry.Curve.JoinCurves(listCurveSplitParts, dTol)
# test if it worked :)
if arrCurveJoined:
# finally add to doc
sc.doc.Objects.AddCurve(arrCurveJoined[0])
# and redraw for visual pleasure
objAdded = sc.doc.Objects.MostRecentObject()
objAdded.Select(True)
objAdded.CommitChanges()
sc.doc.Views.Redraw()
if __name__ == "__main__":
splitCurvesWithEachOther()
Let me know if this helped
PS: No need to order things, just box select away
Thanks for sharing that @lando.schumpich,
It certainly works, I’ll try to understand it.
Thanks for the comments