-
Notifications
You must be signed in to change notification settings - Fork 7
/
spb_Grips_linearizeBetween2.py
317 lines (231 loc) · 9.32 KB
/
spb_Grips_linearizeBetween2.py
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
"""
Select the 2 outer control points of linear control point region.
For surfaces, points must share a row or column.
Options:
Ends
Fixed: The 2 selected control points will not be moved with those between them.
MoveToAverageLine: the 2 selected control points will be calculated for the line
of projection and subsequently be projected onto it.
DistributeEvenly=Yes: All control points between will be evenly distributed between
selected control points.
"""
from __future__ import absolute_import, print_function, unicode_literals
"""
220922-23: Created, starting as a split from another script.
TODO:
Properly process periodic and other closed curves.
"""
import Rhino
import Rhino.Geometry as rg
import Rhino.Input as ri
import rhinoscriptsyntax as rs
import scriptcontext as sc
class Opts():
keys = []
values = {}
names = {}
riOpts = {}
listValues = {}
stickyKeys = {}
key = 'bFixedEnds'; keys.append(key)
values[key] = True
names[key] = 'Ends'
riOpts[key] = ri.Custom.OptionToggle(values[key], 'MoveToAverageLine', 'Fixed')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bDistributeEvenly'; keys.append(key)
values[key] = False
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bEcho'; keys.append(key)
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bDebug'; keys.append(key)
values[key] = False
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
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 addOption(cls, go, key):
idxOpt = None
if key in cls.riOpts:
if key[0] == 'b':
idxOpt = go.AddOptionToggle(
cls.names[key], cls.riOpts[key])[0]
elif key[0] == 'f':
idxOpt = go.AddOptionDouble(
cls.names[key], cls.riOpts[key])[0]
elif key[0] == 'i':
idxOpt = go.AddOptionInteger(
englishName=cls.names[key], intValue=cls.riOpts[key])[0]
elif key in cls.listValues:
idxOpt = go.AddOptionList(
englishOptionName=cls.names[key],
listValues=cls.listValues[key],
listCurrentIndex=cls.values[key])
else:
print("{} is not a valid key in Opts.".format(key))
return idxOpt
@classmethod
def setValue(cls, key, idxList=None):
#if key == 'fDistance':
# if cls.riOpts[key].CurrentValue <= 1e-9:
# print("Invalid input for Distance value.")
# cls.riOpts[key].CurrentValue = cls.values[key]
# return
if key in cls.riOpts:
cls.values[key] = cls.riOpts[key].CurrentValue
elif key in cls.listValues:
cls.values[key] = idxList
else:
return
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
def getInput():
"""
Get control point grips.
"""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select 2 outer control points of span to linearize")
go.GeometryFilter = Rhino.DocObjects.ObjectType.Grip
go.EnableUnselectObjectsOnExit(False) # Do not unselect object when an option selected, a number is entered, etc.
idxs_Opt = {}
def addOption(key): idxs_Opt[key] = Opts.addOption(go, key)
while True:
go.ClearCommandOptions()
idxs_Opt.clear()
addOption('bFixedEnds')
addOption('bDistributeEvenly')
addOption('bEcho')
addOption('bDebug')
res = go.GetMultiple(minimumNumber=2, maximumNumber=2)
if res == ri.GetResult.Cancel:
go.Dispose()
return
if res == ri.GetResult.Object:
objrefs = go.Objects()
go.Dispose()
grips = [o.Object() for o in objrefs]
return grips
go.DeselectAllBeforePostSelect = False # So objects won't be deselected on repeats of While loop.
go.EnablePreSelect(False, ignoreUnacceptablePreselectedObjects=True)
go.EnableClearObjectsOnEntry(False) # Do not clear objects in go on repeats of While loop.
for key in idxs_Opt:
if go.Option().Index == idxs_Opt[key]:
Opts.setValue(key, go.Option().CurrentListOptionIndex)
break
def _areNeighbors(gripA, gripB):
for directionR, directionS in ((1,0), (-1,0), (0,1), (0,-1)):
grip_Neighbor = gripA.NeighborGrip(directionR=1, directionS=0, directionT=0, wrap=True)
if grip_Neighbor is None: continue
if grip_Neighbor.Index == gripB.Index:
return True
return False
def _getIndicesBetweenGrips(gripA, gripB):
"""
Returns: list(int(ordered indices))
"""
for directionR, directionS in ((1,0), (-1,0), (0,1), (0,-1)):
idxs = []
grip_WIP = gripA
while True:
sc.escape_test()
grip_WIP = grip_WIP.NeighborGrip(
directionR=directionR,
directionS=directionS,
directionT=0,
wrap=True)
if grip_WIP is None:
# End of open direction.
break
if grip_WIP.Index == gripB.Index:
return idxs
if grip_WIP.Index == gripA.Index:
# This will occur when surface is closed.
break
idxs.append(grip_WIP.Index)
def distributeEvenly(grips_ToMove, ptA, ptB):
bSomePtTrans = False
fDists_From1stRef = []
for grip_ToMove in grips_ToMove:
fDists_From1stRef.append(
ptA.DistanceTo(grip_ToMove.CurrentLocation))
grips_ToMove_Sorted = []
for dist, grip in sorted(zip(fDists_From1stRef, grips_ToMove)):
grips_ToMove_Sorted.append(grip)
for i in range(len(grips_ToMove_Sorted)):
perunum = float(i+1)/float(len(grips_ToMove_Sorted)+1)
pt_Target = ptA + (ptB - ptA) * perunum
grip_ToMove = grips_ToMove_Sorted[i]
if grip_ToMove.CurrentLocation.DistanceTo(pt_Target) > Rhino.RhinoMath.ZeroTolerance:
grip_ToMove.CurrentLocation = pt_Target
bSomePtTrans = True
return bSomePtTrans
def linearizeGripsBetween2(gripA, gripB, bFixedEnds=True, bDistributeEvenly=False, bEcho=True, bDebug=False):
"""
"""
if gripA.OwnerId != gripB.OwnerId:
return False, "Grips must be from the same object."
if gripA.Index == gripB.Index:
return False, "The 2 grips are identical."
rdObj = rs.coercerhinoobject(gripA.OwnerId)
idxs_Between = _getIndicesBetweenGrips(gripA, gripB)
if idxs_Between is None:
return False, "The grips do not share a row or column."
elif len(idxs_Between) == 0:
return False, "The 2 grips are consecutive."
grips_All = rdObj.GetGrips()
bSomePtTrans = False
if bFixedEnds:
idxs_ToMove = idxs_Between
line = rg.Line(gripA.CurrentLocation, gripB.CurrentLocation)
else:
idxs_ToMove = [gripA.Index] + idxs_Between + [gripB.Index]
bSuccess, line = rg.Line.TryFitLineToPoints(
[grips_All[i].CurrentLocation for i in idxs_ToMove])
for iT in idxs_ToMove:
grip_ToMove = grips_All[iT]
pt_Target = line.ClosestPoint(
grip_ToMove.CurrentLocation, limitToFiniteSegment=False)
if grip_ToMove.CurrentLocation.DistanceTo(pt_Target) > Rhino.RhinoMath.ZeroTolerance:
grip_ToMove.CurrentLocation = pt_Target
bSomePtTrans = True
if bDistributeEvenly:
idxs_ToMove = idxs_Between
grips_ToMove = [grips_All[i] for i in idxs_ToMove]
rc = distributeEvenly(grips_ToMove, gripA.CurrentLocation, gripB.CurrentLocation)
bSomePtTrans = bSomePtTrans or rc
if not bSomePtTrans:
return False, "Grips are already at target locations."
rc = sc.doc.Objects.GripUpdate(rdObj, deleteOriginal=True)
return rc, None
def main():
grips = getInput()
if grips is None: return
bFixedEnds = Opts.values['bFixedEnds']
bDistributeEvenly = Opts.values['bDistributeEvenly']
bEcho = Opts.values['bEcho']
bDebug = Opts.values['bDebug']
if not bDebug: sc.doc.Views.RedrawEnabled = False
rc = linearizeGripsBetween2(
grips[0],
grips[1],
bFixedEnds=bFixedEnds,
bDistributeEvenly=bDistributeEvenly,
bEcho=bEcho,
bDebug=bDebug,
)
if rc is None: return
bSuccess, sLog = rc
if not bSuccess:
print(sLog)
sc.doc.Views.RedrawEnabled = True
if __name__ == '__main__': main()