-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointOnLine.py
More file actions
125 lines (96 loc) · 4.18 KB
/
PointOnLine.py
File metadata and controls
125 lines (96 loc) · 4.18 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
import adsk.core, adsk.fusion, traceback
_app = None
_ui = None
_handlers = []
class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
adsk.terminate()
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class MyExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: adsk.core.CommandEventArgs):
global _app, _ui
_app = adsk.core.Application.get()
_ui = _app.userInterface
# Get the active design
design = _app.activeProduct
if not isinstance(design, adsk.fusion.Design):
_ui.messageBox('No active Fusion 360 design', 'Error')
return
# Get the active sketch the user is currently editing
sketch = _app.activeEditObject
if not isinstance(sketch, adsk.fusion.Sketch):
_ui.messageBox('No active sketch found', 'Error')
return
# Find the selected SketchFittedSpline
selectedSpline = None
for entity in _ui.activeSelections:
if isinstance(entity.entity, adsk.fusion.SketchFittedSpline):
selectedSpline = entity.entity
break
if not selectedSpline:
_ui.messageBox('No SketchFittedSpline selected', 'Error')
return
# Clear the selection before creating the point
_ui.activeSelections.clear()
# Get the slider value
positionRatio = positionInput.value
# Get the spline evaluator
evaluator = selectedSpline.geometry.evaluator
length = selectedSpline.length
targetLength = length * positionRatio
# Find the parameter at the selected length
success, param = evaluator.getParameterAtLength(0, targetLength)
if not success:
_ui.messageBox('Failed to find parameter at specified length', 'Error')
return
# Get the point at that parameter
success, point = evaluator.getPointAtParameter(param)
if not success:
_ui.messageBox('Failed to get point at parameter', 'Error')
return
# Add the calculated point to the sketch
sketchPoints = sketch.sketchPoints
sketchPoints.add(point)
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
global selectionInput, positionInput
try:
cmd = adsk.core.Command.cast(args.command)
onDestroy = MyCommandDestroyHandler()
cmd.destroy.add(onDestroy)
_handlers.append(onDestroy)
selectionInput = cmd.commandInputs.addSelectionInput('selection', 'Select', 'Basic select command input')
selectionInput.setSelectionLimits(1)
selectionInput.addSelectionFilter("SketchCurves")
inputs = cmd.commandInputs
# Add a slider to select position along the spline
positionInput = inputs.addFloatSpinnerCommandInput('spinnerFloat', 'Position', '', 0 , 1 , 0.01, 0.5)
onExecute = MyExecuteHandler()
cmd.execute.add(onExecute)
_handlers.append(onExecute)
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def run(context):
try:
global _app, _ui
_app = adsk.core.Application.get()
_ui = _app.userInterface
cmdDef = _ui.commandDefinitions.itemById('PointAtLengthRatio')
if not cmdDef:
cmdDef = _ui.commandDefinitions.addButtonDefinition('PointAtLengthRatio', 'Point At Length Ratio', 'Places a point on the selected curve at specified length ratio')
onCommandCreated = MyCommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
_handlers.append(onCommandCreated)
cmdDef.execute()
adsk.autoTerminate(False)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))