forked from CADacombs/rhinopython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxCurve_splitWithSurfaceEdges.py
More file actions
552 lines (405 loc) · 16.9 KB
/
xCurve_splitWithSurfaceEdges.py
File metadata and controls
552 lines (405 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
"""
200521: Created, starting with another script.
200611, 24, 25: Bug fix.
200729: Renamed a function.
"""
import Rhino
import Rhino.DocObjects as rd
import Rhino.Geometry as rg
import Rhino.Input as ri
import scriptcontext as sc
from System import Guid
class Opts():
keys = []
values = {}
names = {}
riOpts = {}
riAddOpts = {}
stickyKeys = {}
def addOptionDouble(key, names, riOpts):
return lambda getObj: ri.Custom.GetBaseClass.AddOptionDouble(
getObj, englishName=names[key], numberValue=riOpts[key])
def addOptionInteger(key, names, riOpts):
return lambda getObj: ri.Custom.GetBaseClass.AddOptionInteger(
getObj, englishName=names[key], intValue=riOpts[key])
def addOptionList(key, names, listValues, values):
return lambda getObj: ri.Custom.GetBaseClass.AddOptionList(
getObj,
englishOptionName=names[key],
listValues=listValues,
listCurrentIndex=values[key])
def addOptionToggle(key, names, riOpts):
return lambda getObj: ri.Custom.GetBaseClass.AddOptionToggle(
getObj, englishName=names[key], toggleValue=riOpts[key])
key = 'fTolerance'; keys.append(key)
values[key] = 1.0 * sc.doc.ModelAbsoluteTolerance
riOpts[key] = ri.Custom.OptionDouble(values[key])
riAddOpts[key] = addOptionDouble(key, names, riOpts)
stickyKeys[key] = '{}({})({})'.format(key, __file__, sc.doc.Name)
key = 'bEcho'; keys.append(key)
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bDebug'; keys.append(key)
values[key] = False
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
for key in keys:
if key not in names:
names[key] = key[1:]
# Load sticky.
for key in stickyKeys:
if stickyKeys[key] in sc.sticky:
if key in riOpts:
riOpts[key].CurrentValue = values[key] = sc.sticky[stickyKeys[key]]
else:
values[key] = sc.sticky[stickyKeys[key]]
@classmethod
def setValues(cls):
for key in cls.keys:
if key in cls.riOpts:
cls.values[key] = cls.riOpts[key].CurrentValue
@classmethod
def saveSticky(cls):
for key in cls.stickyKeys:
if key in cls.riOpts:
sc.sticky[cls.stickyKeys[key]] = cls.riOpts[key].CurrentValue
else:
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
def getInput_Curves():
"""
Get wires with optional input.
"""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select wire curves")
go.SetCommandPromptDefault("Enter for all normal wires")
go.GeometryFilter = rd.ObjectType.Curve
go.GeometryAttributeFilter = ri.Custom.GeometryAttributeFilter.WireCurve
go.AcceptNothing(True)
go.AcceptNumber(enable=True, acceptZero=True)
go.AlreadySelectedObjectSelect = True
go.DeselectAllBeforePostSelect = False # So objects won't be deselected on repeats of While loop.
go.EnableClearObjectsOnEntry(False)
go.EnableUnselectObjectsOnExit(False)
idxs_Opts = {}
while True:
key = 'fTolerance'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bEcho'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bDebug'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
res = go.GetMultiple(minimumNumber=1, maximumNumber=0)
if res == ri.GetResult.Cancel:
return
elif res == ri.GetResult.Nothing:
rdCrvs = []
rgEdges = []
settings = rd.ObjectEnumeratorSettings()
settings.NormalObjects = True
settings.LockedObjects = False
for rdObj in sc.doc.Objects.GetObjectList(settings):
if rdObj.ObjectType == rd.ObjectType.Curve:
rdCrvs.append(rdObj)
return tuple([rdCrvs + rgEdges] + [Opts.values[key] for key in Opts.keys])
elif res == ri.GetResult.Object:
objrefs = go.Objects()
go.Dispose()
return tuple([objrefs] + [Opts.values[key] for key in Opts.keys])
# An option was selected or a number was entered.
if res == ri.GetResult.Number:
Opts.riOpts['fTolerance'].CurrentValue = go.Number()
if Opts.riOpts['fTolerance'].CurrentValue < 0.0:
Opts.riOpts['fTolerance'].CurrentValue = Opts.riOpts['fTolerance'].InitialValue
Opts.setValues()
Opts.saveSticky()
go.ClearCommandOptions()
def getInput_Face():
"""Get Brepface with optional input."""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select face")
go.GeometryFilter = rd.ObjectType.Surface
go.AcceptNumber(enable=True, acceptZero=True)
idxs_Opts = {}
while True:
key = 'fTolerance'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bEcho'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bDebug'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
res = go.Get()
if res == ri.GetResult.Object:
objref = go.Object(0)
go.Dispose()
return tuple([objref] + [Opts.values[key] for key in Opts.keys])
elif res == ri.GetResult.Cancel:
return
# An option was selected or a number was entered.
if res == ri.GetResult.Number:
Opts.riOpts['fTolerance'].CurrentValue = go.Number()
if Opts.riOpts['fTolerance'].CurrentValue < 0.0:
Opts.riOpts['fTolerance'].CurrentValue = Opts.riOpts['fTolerance'].InitialValue
Opts.setValues()
Opts.saveSticky()
go.ClearCommandOptions()
def formatDistance(fDistance):
if fDistance is None:
return "(No deviation was provided.)"
if fDistance == 0.0:
return "Exactly zero"
if fDistance < 10.0**(-(sc.doc.DistanceDisplayPrecision-3)):
return "{:.1e}".format(fDistance)
else:
return "{:.{}f}".format(fDistance, sc.doc.ModelDistanceDisplayPrecision)
def splitCurvesWithSurfaceEdges(rgCrvs_In, rgSrf_In, fTolerance=None, bDebug=False):
"""
Parameters:
rgCrvs_In
rgSrf_In: If rg.BrepFace, then adjacent edges are used, otherwise surface domain isocurves are used.
fTolerance
bDebug
Returns:
rgCrvs_Out_SrfInterior
rgCrvs_Out_SrfBoundary
rgCrvs_Out_SrfExterior
Intersect.Intersection.CurveCurve sometimes returns intersections at the exact ends of curves.
In this case, the entire curve will be the result of Curve.Split.
"""
if fTolerance is None:
fTolerance = sc.doc.ModelAbsoluteTolerance
rgEdges = [] # Face edges or Surface domain extent curves.
if isinstance(rgSrf_In, rg.BrepFace):
idxs_Edges = rgSrf_In.AdjacentEdges()
for idxE in idxs_Edges:
rgE = rgSrf_In.Brep.Edges[idxE]
rgEdges.append(rgE)
elif isinstance(rgSrf_In, rg.Surface):
# SENW == 0,1,2,3 for IsSingular.
# South.
if not rgSrf_In.IsSingular(0):
rgC = rgSrf_In.IsoCurve(0, rgSrf_In.Domain(1).T0)
rgEdges.append(rgC)
# East.
if not rgSrf_In.IsSingular(1):
rgC = rgSrf_In.IsoCurve(1, rgSrf_In.Domain(0).T1)
rgEdges.append(rgC)
# North.
if not rgSrf_In.IsSingular(2):
rgC = rgSrf_In.IsoCurve(0, rgSrf_In.Domain(1).T1)
rgEdges.append(rgC)
# West.
if not rgSrf_In.IsSingular(3):
rgC = rgSrf_In.IsoCurve(1, rgSrf_In.Domain(1).T0)
rgEdges.append(rgC)
else:
raise ValueError("{} passed to splitCurvesWithSurfaceEdges".format(
rgSrf_In.GetType().Name))
rgCrvs_Out_SrfInterior = []
rgCrvs_Out_SrfBoundary = []
rgCrvs_Out_SrfExterior = []
for rgC_toSplit in rgCrvs_In:
t_Pts = []
doms_Overlaps = []
for rgEdge in rgEdges:
intrscts = rg.Intersect.Intersection.CurveCurve(
curveA=rgC_toSplit,
curveB=rgEdge,
tolerance=fTolerance,
overlapTolerance=0.1*fTolerance)
if bDebug: print "Intersection count: {}".format(intrscts.Count)
for intsct in intrscts:
if intsct.IsPoint:
#print intsct.ParameterA
t_Pts.append(intsct.ParameterA)
if intsct.IsOverlap:
doms_Overlaps.append(intsct.OverlapA)
#print intsct.OverlapA.T0, intsct.OverlapA.T1
t_Pts.append(intsct.OverlapA.T0)
t_Pts.append(intsct.OverlapA.T1)
t_Pts = sorted(set(t_Pts))
# TODO: Remove near-duplicates.
rgCs_fromSplit = rgC_toSplit.Split(t=t_Pts)
# For debugging.
if len(rgCs_fromSplit) == 1:
pass
#print "1 curve resulted from split."
#c = rgCs_fromSplit[0]
#sc.doc.Objects.AddCurve(c)
#continue
for c in rgCs_fromSplit:
fLength = c.GetLength()
if fLength == 0.0:
if bDebug: print "Zero length curve ignored."
c.Dispose()
continue
elif fLength < fTolerance:
if bDebug: print "Short ({}) curve ignored. Domain: [{},{}]".format(
formatDistance(fLength),
c.Domain.T0,
c.Domain.T1)
c.Dispose()
continue
for dom in doms_Overlaps:
if c.Domain.IncludesParameter(dom.Mid):
rgCrvs_Out_SrfBoundary.append(c)
break # To next curve from split.
else:
# Curve is not part of Intersection overlaps but may still be along boundary.
pt_MidCrv = c.PointAt(c.Domain.Mid)
#sc.doc.Objects.AddPoint(pt_MidCrv)
b, u, v = rgSrf_In.ClosestPoint(pt_MidCrv) # if rgSrf_In is BrepFace, UnderlyingSurface is still used.
if not b:
raise ValueError("ClosestPoint failed.")
ptAtUV = rgSrf_In.PointAt(u,v)
#sc.doc.Objects.AddPoint(ptAtUV)
# Check whether Closest point on surfaces is beyond tolerance from the test point.
dist = ptAtUV.DistanceTo(pt_MidCrv)
if dist > fTolerance:
rgCrvs_Out_SrfExterior.append(c)
continue
# Point is either interior or along an edge.
if isinstance(rgSrf_In, rg.BrepFace):
# TODO: Check distance from edge?
ptFaceRel = rgSrf_In.IsPointOnFace(u,v)
if ptFaceRel == rg.PointFaceRelation.Interior:
rgCrvs_Out_SrfInterior.append(c)
else:
rgCrvs_Out_SrfBoundary.append(c)
else:
# rgSrf_In is a Surface but not a BrepFace.
# TODO: Check distance from surface side?
if (
rgSrf_In.Domain(0).IncludesParameter(u) and
rgSrf_In.Domain(1).IncludesParameter(v)
):
rgCrvs_Out_SrfInterior.append(c)
else:
rgCrvs_Out_SrfBoundary.append(c)
return rgCrvs_Out_SrfInterior, rgCrvs_Out_SrfBoundary, rgCrvs_Out_SrfExterior
def processCurveObjects(rhCrvObjs, rhSrf, **kwargs):
"""
"""
def getOpt(key): return kwargs[key] if key in kwargs else Opts.values[key]
fTolerance = getOpt('fTolerance')
bEcho = getOpt('bEcho')
bDebug = getOpt('bDebug')
def getCurve(rhObj):
if isinstance(rhObj, rd.CurveObject):
return rhObj, rhObj.CurveGeometry
if isinstance(rhObj, rg.Curve):
return None, rhObj
if isinstance(rhObj, rg.GeometryBase):
rdObj = None
rgObj = rhObj
elif isinstance(rhObj, rd.ObjRef):
rdObj = rhObj.Object()
rgObj = rhObj.Geometry()
elif isinstance(rhObj, Guid):
rdObj = sc.doc.Objects.FindId(rhObj) if Rhino.RhinoApp.ExeVersion >= 6 else sc.doc.Objects.Find(rhObj)
rgObj = rdObj.Geometry
else:
return
if isinstance(rgObj, rg.Curve):
return rdObj, rgObj
def getSurface(rhSrf):
"""
Parameters:
rhFace
Returns:
rg.BrepFace or rg.Surface
"""
if isinstance(rhSrf, rg.BrepFace):
return rhSrf
if isinstance(rhSrf, rg.Surface):
return rhSrf
if isinstance(rhSrf, rg.Brep):
rgBrep = rhSrf
if rgBrep.Faces.Count == 1:
return rgBrep.Faces[0]
else:
return
if isinstance(rhSrf, rd.ObjRef):
objref = rhSrf
compIdxType = objref.GeometryComponentIndex.ComponentIndexType
if compIdxType == rg.ComponentIndexType.BrepFace:
return objref.Face()
if compIdxType == rg.ComponentIndexType.InvalidType:
rgBrep = objref.Brep()
if rgBrep.Faces.Count == 1:
return rgBrep.Faces[0]
else:
return
if isinstance(rhSrf, Guid):
rdObj = sc.doc.Objects.FindId(rhSrf) if Rhino.RhinoApp.ExeVersion >= 6 else sc.doc.Objects.Find(rhSrf)
if isinstance(rdObj, rd.BrepObject):
brep = rdObj.Geometry
if rhSrf.Faces.Count == 1:
return rhSrf.Faces[0]
else:
return
srf = None
if isinstance(geom, rg.BrepFace):
srf = geom.UnderlyingSurface()
elif isinstance(geom, rg.Surface):
srf = geom
return srf
rdCrvs_In = []
rgCrvs_In = []
for o in rhCrvObjs:
rdCrv_In, rgCrv_In = getCurve(o)
if rdCrv_In:
rdCrvs_In.append(rdCrv_In)
rgCrvs_In.append(rgCrv_In)
rgSrf_In = getSurface(rhSrf)
gCrvs_Out = []
iCt_Split_AllCrv_In = 0
iCt_CrvsSplit_NoError = 0
for i, rgCrv in enumerate(rgCrvs_In):
attrs = rdCrvs_In[i].Attributes
rc = splitCurvesWithSurfaceEdges(
[rgCrv],
rgSrf_In=rgSrf_In,
fTolerance=fTolerance)
if not rc: continue
(
cs_Interior,
cs_Boundary,
cs_Exterior
) = rc
iCt_Added_ThisCrv_In = 0
crvs_toAdd = cs_Interior + cs_Boundary + cs_Exterior
if not crvs_toAdd: continue
for c in crvs_toAdd:
gC = sc.doc.Objects.AddCurve(c, attributes=attrs)
if gC != Guid.Empty:
gCrvs_Out.append(gC)
iCt_Added_ThisCrv_In += 1
if iCt_Added_ThisCrv_In == len(crvs_toAdd):
sc.doc.Objects.Delete(objectId=rdCrvs_In[i].Id, quiet=False)
iCt_Split_AllCrv_In += iCt_Added_ThisCrv_In
iCt_CrvsSplit_NoError += 1
else:
print "Added {} out of {} curves. Original curve was not removed.".format(
iCt_Added_ThisCrv_In, len(crvs_toAdd))
if iCt_CrvsSplit_NoError:
print "Split {} curves into {} curves.".format(
iCt_CrvsSplit_NoError, iCt_Split_AllCrv_In)
else:
print "No curves were split."
return gCrvs_Out
def main():
rc = getInput_Curves()
if rc is None: return
rhObjects_Wires = rc[0]
rc = getInput_Face()
if rc is None: return
objrefs_Face = rc[0]
#sc.doc.Objects.UnselectAll()
Rhino.RhinoApp.SetCommandPrompt("Working ...")
gCrvs_Ret = processCurveObjects(
rhCrvObjs=rhObjects_Wires,
rhSrf=objrefs_Face,
)
if gCrvs_Ret:
sc.doc.Objects.UnselectAll()
for gC in gCrvs_Ret:
sc.doc.Objects.Select(objectId=gC)
sc.doc.Views.Redraw()
if __name__ == '__main__': main()