-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChassis with Cross Section Input.py
540 lines (458 loc) · 24.5 KB
/
Chassis with Cross Section Input.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
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
# Author- Purushottam Nawale
# Description- CAD Customization of an EV Chassis
import adsk.core
import adsk.fusion
import adsk.cam
import traceback
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
design = adsk.fusion.Design.cast(app.activeProduct)
unitsMgr = design.unitsManager
rootComp = design.rootComponent
sketches = rootComp.sketches
planes = rootComp.constructionPlanes
features = rootComp.features
sweeps = rootComp.features.sweepFeatures
extrudes = rootComp.features.extrudeFeatures
# User Parameters
width='Width'
widthValue='152.0 cm'
newInputNumber=ui.inputBox("Enter Width of Chassis: ","User Parameter Width",widthValue)
Width=unitsMgr.evaluateExpression(newInputNumber[0],unitsMgr.defaultLengthUnits)
realValueInput1=adsk.core.ValueInput.createByReal(Width)
value1=design.userParameters.add(width,realValueInput1,unitsMgr.defaultLengthUnits,'')
Wheelbase='Wheelbase'
wheebaseValue='245 cm'
newInputNumber2=ui.inputBox("Enter WheelBase of Chassis: ","User Parameter Wheelbase",wheebaseValue)
WheelBase=unitsMgr.evaluateExpression(newInputNumber2[0],unitsMgr.defaultLengthUnits)
realValueInput2=adsk.core.ValueInput.createByReal(WheelBase)
value2=design.userParameters.add(Wheelbase,realValueInput2,unitsMgr.defaultLengthUnits,'')
CSWidth = 'csWidth'
csWidthValue='8.6 cm'
newInputNumber3=ui.inputBox("Enter Width of Rectangular Cross Section: ","User Parameter Width",csWidthValue)
csWidth=unitsMgr.evaluateExpression(newInputNumber3[0],unitsMgr.defaultLengthUnits)
realValueInput3 = adsk.core.ValueInput.createByReal(csWidth)
value3=design.userParameters.add(CSWidth, realValueInput3,unitsMgr.defaultLengthUnits, '')
CSHeight = 'csHeight'
csHeightValue='12.9 cm'
newInputNumber4=ui.inputBox("Enter Height of Rectangular Cross Section: ","User Parameter csHeight",csHeightValue)
csHeight=unitsMgr.evaluateExpression(newInputNumber4[0],unitsMgr.defaultLengthUnits)
realValueInput4 = adsk.core.ValueInput.createByReal(csHeight)
value4=design.userParameters.add(CSHeight, realValueInput4,unitsMgr.defaultLengthUnits, '')
thickness = 'Thickness'
csHeightValue='0.76 cm'
newInputNumber5=ui.inputBox("Enter the Thickness: ","User Parameter Thickness",csHeightValue)
Thickness=unitsMgr.evaluateExpression(newInputNumber5[0],unitsMgr.defaultLengthUnits)
realValueInput5 = adsk.core.ValueInput.createByReal(Thickness)
value5=design.userParameters.add(thickness, realValueInput5,unitsMgr.defaultLengthUnits, '')
ICSWidth = 'icsWidth'
icsWidthValue='5.5 cm'
newInputNumber6=ui.inputBox("Enter Width of Rectangular Cross Section: ","User Parameter Width",icsWidthValue)
icsWidth=unitsMgr.evaluateExpression(newInputNumber6[0],unitsMgr.defaultLengthUnits)
realValueInput6 = adsk.core.ValueInput.createByReal(icsWidth)
value6=design.userParameters.add(ICSWidth, realValueInput6,unitsMgr.defaultLengthUnits, '')
ICSHeight = 'icsHeight'
icsHeightValue= '8.2 cm'
newInputNumber7=ui.inputBox("Enter Height of Rectangular Cross Section: ","User Parameter csHeight",icsHeightValue)
icsHeight=unitsMgr.evaluateExpression(newInputNumber7[0],unitsMgr.defaultLengthUnits)
realValueInput7 = adsk.core.ValueInput.createByReal(icsHeight)
value7=design.userParameters.add(ICSHeight, realValueInput7,unitsMgr.defaultLengthUnits, '')
Width=Width-2*csWidth
w=csWidth-2*Thickness
h=csHeight-2*Thickness
w2=icsWidth-1
h2=icsHeight-1
xyPlane = rootComp.xYConstructionPlane
yzPlane = rootComp.yZConstructionPlane
xzPlane = rootComp.xZConstructionPlane
sketch1 = sketches.add(xyPlane)
sketch2 = sketches.add(yzPlane)
sketch3 = sketches.add(xzPlane)
lines1 = sketch1.sketchCurves.sketchLines
lines2 = sketch2.sketchCurves.sketchLines
lines3 = sketch3.sketchCurves.sketchLines
sketchPoints1 = sketch1.sketchPoints
sketchPoints2 = sketch2.sketchPoints
sketchPoints3 = sketch3.sketchPoints
pointA = adsk.core.Point3D.create(0, 0, 0)
pointB = adsk.core.Point3D.create(0, 62, 0)
pointC = adsk.core.Point3D.create(25, 62+(WheelBase*0.1069),-10)
pointD = adsk.core.Point3D.create(25, 62+(WheelBase*0.1069)+(WheelBase*0.7297), -10)
pointE = adsk.core.Point3D.create(-10, 62+(WheelBase*0.1069)+(WheelBase*0.7297)+(WheelBase*0.1633), -10)
pointF = adsk.core.Point3D.create(-10, 62+91+(WheelBase*0.1069)+(WheelBase*0.7297)+(WheelBase*0.1633), -10)
lineAB = lines2.addByTwoPoints(pointA, pointB)
lineBC = lines2.addByTwoPoints(pointB, pointC)
lineCD = lines2.addByTwoPoints(pointC, pointD)
lineDE = lines2.addByTwoPoints(pointD, pointE)
lineEF = lines2.addByTwoPoints(pointE, pointF)
arc1 = sketch2.sketchCurves.sketchArcs.addFillet(lineAB, lineAB.endSketchPoint.geometry, lineBC, lineBC.startSketchPoint.geometry, 30) # Last argument is the radius of arc
arc2 = sketch2.sketchCurves.sketchArcs.addFillet(lineBC, lineBC.endSketchPoint.geometry, lineCD, lineCD.startSketchPoint.geometry, 30)
arc3 = sketch2.sketchCurves.sketchArcs.addFillet(lineCD, lineCD.endSketchPoint.geometry, lineDE, lineDE.startSketchPoint.geometry, 30)
arc4 = sketch2.sketchCurves.sketchArcs.addFillet(lineDE, lineDE.endSketchPoint.geometry, lineEF, lineEF.startSketchPoint.geometry, 30)
# Create the rectangle 1 using the center points and cornerpoints
centerPoint = adsk.core.Point3D.create(0, 0, 0)
origin = sketchPoints3.add(centerPoint)
rectangles = sketch3.sketchCurves.sketchLines
cornerPoint1 = adsk.core.Point3D.create(w/2, h/2, 0)
cornerPoint2 = adsk.core.Point3D.create(csWidth/2, csHeight/2, 0)
rectangle1 = rectangles.addCenterPointRectangle(centerPoint, cornerPoint1)
rectangle2 = rectangles.addCenterPointRectangle(centerPoint, cornerPoint2)
prof = sketch3.profiles.item(1)
# Added all lines to the Collection, So Sweep follows all those lines
collection = adsk.core.ObjectCollection.create()
collection.add(lineAB)
collection.add(arc1)
collection.add(lineBC)
collection.add(arc2)
collection.add(lineCD)
collection.add(arc3)
collection.add(lineDE)
collection.add(arc4)
collection.add(lineEF)
path = features.createPath(collection)
# Create a sweep input
sweepInput = sweeps.createInput(prof, path, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
sweepInput.isDirectionFlipped = False
sweepInput.profileScaling = adsk.fusion.SweepProfileScalingOptions.SweepProfileScaleOption
# Create the sweep.
sweep=sweeps.add(sweepInput)
body1 = sweep.bodies.item(0)
# Get a face of the body
face1 = body1.faces.item(55)
# Create a Plane for a Mirror
planeInput = planes.createInput()
offsetDistance = adsk.core.ValueInput.createByReal(Width/2)
# offsetDistance = adsk.core.ValueInput.createByString(width)
planeInput.setByOffset(face1, offsetDistance)
plane = planes.add(planeInput)
plane.isLightBulbOn=False
# Create input entities for mirror feature
inputEntites = adsk.core.ObjectCollection.create()
inputEntites.add(body1)
# Create the input for mirror feature
mirrorFeatures = features.mirrorFeatures
mirrorInput = mirrorFeatures.createInput(inputEntites, plane)
# Create the mirror feature
mirrorFeature = mirrorFeatures.add(mirrorInput)
body2=mirrorFeature.bodies.item(0)
face2 = body2.faces.item(39)
# Create a construction plane by offset
planeInput21 = planes.createInput()
realValueInput3 = adsk.core.ValueInput.createByReal(csWidth/2)
planeInput21.setByOffset(yzPlane, realValueInput3)
offsetPlane21 = planes.add(planeInput21)
sketch21=sketches.add(offsetPlane21)
sketchPoints21 = sketch21.sketchPoints
rectangles21 = sketch21.sketchCurves.sketchLines
# Create the rectangle 1 using the center points and cornerpoints
centerPoint = adsk.core.Point3D.create(0, 45, 0)
cornerPoint1 = adsk.core.Point3D.create(h2/2, 45+w2/2, 0)
cornerPoint2 = adsk.core.Point3D.create(icsHeight/2, 45+icsWidth/2, 0)
rectangle1 = rectangles21.addCenterPointRectangle(centerPoint, cornerPoint1)
rectangle2 = rectangles21.addCenterPointRectangle(centerPoint, cornerPoint2)
# Create a construction plane by offset
planeInput22 = planes.createInput()
realValueInput3 = adsk.core.ValueInput.createByReal(csWidth/2-10)
planeInput22.setByOffset(yzPlane, realValueInput3)
offsetPlane22 = planes.add(planeInput22)
sketch22=sketches.add(offsetPlane22)
sketchPoints22 = sketch22.sketchPoints
rectangles22 = sketch22.sketchCurves.sketchLines
# Create the rectangle 2 using the center points and cornerpoints
centerPoint = adsk.core.Point3D.create(25.25, 62+(WheelBase*0.1959), 0)
cornerPoint1 = adsk.core.Point3D.create(25.25+w2/2, 62+h2/2+(WheelBase*0.1959), 0)
cornerPoint2 = adsk.core.Point3D.create(25.25+icsWidth/2, 62+icsHeight/2+(WheelBase*0.1959), 0)
rectangle1 = rectangles22.addCenterPointRectangle(centerPoint, cornerPoint1)
rectangle2 = rectangles22.addCenterPointRectangle(centerPoint, cornerPoint2)
# Create the rectangle 3 using the center points and cornerpoints
centerPoint = adsk.core.Point3D.create(25.25, 62+(WheelBase*0.1959)+(WheelBase*0.2857), 0)
cornerPoint1 = adsk.core.Point3D.create(25.25+w2/2, 62+h2/2+(WheelBase*0.1959)+(WheelBase*0.2857), 0)
cornerPoint2 = adsk.core.Point3D.create(25.25+icsWidth/2, 62+icsHeight/2+(WheelBase*0.1959)+(WheelBase*0.2857), 0)
rectangle1 = rectangles22.addCenterPointRectangle(centerPoint, cornerPoint1)
rectangle2 = rectangles22.addCenterPointRectangle(centerPoint, cornerPoint2)
# Create the rectangle 4 using the center points and cornerpoints
centerPoint = adsk.core.Point3D.create(25.25, 62+(WheelBase*0.1959)+(WheelBase*0.2857*2), 0)
cornerPoint1 = adsk.core.Point3D.create(25.25+w2/2, 62+h2/2+(WheelBase*0.1959)+(WheelBase*0.2857*2), 0)
cornerPoint2 = adsk.core.Point3D.create(25.25+icsWidth/2, 62+icsHeight/2+(WheelBase*0.1959)+(WheelBase*0.2857*2), 0)
rectangle1 = rectangles22.addCenterPointRectangle(centerPoint, cornerPoint1)
rectangle2 = rectangles22.addCenterPointRectangle(centerPoint, cornerPoint2)
# Create the rectangle 5 using the center points and cornerpoints
centerPoint = adsk.core.Point3D.create(-10, 62+(WheelBase*0.1959)+(WheelBase*0.2857*2)+(WheelBase*0.2326)+18, 0)
cornerPoint1 = adsk.core.Point3D.create(-10-h2/2,62+w2/2+(WheelBase*0.1959)+(WheelBase*0.2857*2)+(WheelBase*0.2326)+18 , 0)
cornerPoint2 = adsk.core.Point3D.create(-10-icsHeight/2, 62+icsWidth/2+(WheelBase*0.1959)+(WheelBase*0.2857*2)+(WheelBase*0.2326)+18, 0)
rectangle1 = rectangles22.addCenterPointRectangle(centerPoint, cornerPoint1)
rectangle2 = rectangles22.addCenterPointRectangle(centerPoint, cornerPoint2)
prof1 = sketch21.profiles.item(1)
prof2 = sketch22.profiles.item(1)
prof3 = sketch22.profiles.item(2)
prof4 = sketch22.profiles.item(5)
prof5 = sketch22.profiles.item(6)
collection2 = adsk.core.ObjectCollection.create()
collection2.add(prof1)
collection2.add(prof2)
collection2.add(prof3)
collection2.add(prof4)
collection2.add(prof5)
# Create an extrusion that goes from the profile to a specified entity.
extrudeInput = extrudes.createInput(collection2, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
# Create a to-entity extent definition
isChained = True
extent_toentity = adsk.fusion.ToEntityExtentDefinition.create(body2, isChained)
# Set the one side extent with the to-entity-extent-definition, and with a taper angle of 0 degree
extrudeInput.setOneSideExtent(extent_toentity, adsk.fusion.ExtentDirections.PositiveExtentDirection)
# Create the extrusion
extrude = extrudes.add(extrudeInput)
body3=extrude.bodies.item(0)
body4=extrude.bodies.item(1)
body5=extrude.bodies.item(2)
body6=extrude.bodies.item(3)
body7=extrude.bodies.item(4)
# Create a construction plane by offset
planeInput22 = planes.createInput()
offsetDistance22 = adsk.core.ValueInput.createByString('-34.3 cm')
planeInput22.setByOffset(yzPlane, offsetDistance22)
offsetPlane22 = planes.add(planeInput22)
sketch22=sketches.add(offsetPlane22)
sketchPoints22 = sketch22.sketchPoints
rectangles22 = sketch22.sketchCurves.sketchLines
# Create the rectangle 2 using the center points and cornerpoints
centerPoint = adsk.core.Point3D.create(-10, csWidth/2+62+91+(WheelBase*0.1069)+(WheelBase*0.7297)+(WheelBase*0.1633), 0)
cornerPoint1 = adsk.core.Point3D.create(-10-h/2,csWidth/2+w/2+62+91+(WheelBase*0.1069)+(WheelBase*0.7297)+(WheelBase*0.1633) , 0)
cornerPoint2 = adsk.core.Point3D.create(-10-csHeight/2, csWidth/2+csWidth/2+62+91+(WheelBase*0.1069)+(WheelBase*0.7297)+(WheelBase*0.1633), 0)
rectangle41 = rectangles22.addCenterPointRectangle(centerPoint, cornerPoint1)
rectangle42 = rectangles22.addCenterPointRectangle(centerPoint, cornerPoint2)
prof22 = sketch22.profiles.item(0)
distance22 = adsk.core.ValueInput.createByReal(Width+77.2)
extrude22 = extrudes.addSimple(prof22, distance22, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
body8=extrude22.bodies.item(0)
# Create the rectangle 3 using the center points and cornerpoints
centerPoint = adsk.core.Point3D.create(0, -csWidth/2, 0)
rectangles2 = sketch2.sketchCurves.sketchLines
cornerPoint1 = adsk.core.Point3D.create(-h/2, -0.75, 0) # z y -x
cornerPoint2 = adsk.core.Point3D.create(-csHeight/2, 0, 0)
rectangle1 = rectangles2.addCenterPointRectangle(centerPoint, cornerPoint1)
rectangle2 = rectangles2.addCenterPointRectangle(centerPoint, cornerPoint2)
pointA = adsk.core.Point3D.create(-15, -4.3, 0)
pointB = adsk.core.Point3D.create(Width+23.6, -4.3, 0)
pointC = adsk.core.Point3D.create(Width+33.6, 0, 0)
pointD = adsk.core.Point3D.create(-25, 0, 0)
lineAB = lines1.addByTwoPoints(pointA, pointB)
lineAD = lines1.addByTwoPoints(pointA, pointD)
lineBC = lines1.addByTwoPoints(pointB, pointC)
arc1 = sketch1.sketchCurves.sketchArcs.addFillet(lineAB, lineAB.endSketchPoint.geometry, lineBC, lineBC.startSketchPoint.geometry, 30) # Last argument is the radius of arc
arc2 = sketch1.sketchCurves.sketchArcs.addFillet(lineAB, lineAB.endSketchPoint.geometry, lineAD, lineAD.startSketchPoint.geometry, 30) # Last argument is the radius of arc
prof = sketch2.profiles.item(0)
# Added all lines to the Collection, So Sweep follows all those lines
collection = adsk.core.ObjectCollection.create()
collection.add(lineAD)
collection.add(arc2)
collection.add(lineAB)
collection.add(arc1)
collection.add(lineBC)
path = features.createPath(collection)
# Create a sweep input
sweepInput = sweeps.createInput(prof, path, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
sweepInput.isDirectionFlipped = False
sweepInput.profileScaling = adsk.fusion.SweepProfileScalingOptions.SweepProfileScaleOption
# Create the sweep.
sweep=sweeps.add(sweepInput)
body9=sweep.bodies.item(0)
ui.activeSelections.add(body1)
ui.activeSelections.add(body2)
ui.activeSelections.add(body3)
ui.activeSelections.add(body4)
ui.activeSelections.add(body5)
ui.activeSelections.add(body6)
ui.activeSelections.add(body7)
ui.activeSelections.add(body8)
ui.activeSelections.add(body9)
cmdDef = ui.commandDefinitions.itemById(commandId)
if not cmdDef:
cmdDef = ui.commandDefinitions.addButtonDefinition(commandId, commandName, commandDescription) # no resource folder is specified, the default one will be used
onCommandCreated = ApplyMaterialToSelectionCommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
# keep the handler referenced beyond this function
handlers.append(onCommandCreated)
inputs = adsk.core.NamedValues.create()
cmdDef.execute(inputs)
# prevent this module from being terminate when the script returns, because we are waiting for event handlers to fire
adsk.autoTerminate(False)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
import adsk.core, adsk.fusion, traceback
commandId = 'ApplyMaterialToSelectionCommand'
commandName = 'ApplyMaterialToSelection'
commandDescription = 'Apply a material to selected bodies or occurrences'
app = None
ui = None
# global set of event handlers to keep them referenced for the duration of the command
handlers = []
materialsMap = {}
app = adsk.core.Application.get()
if app:
ui = app.userInterface
class ApplyMaterialInputChangedHandler(adsk.core.InputChangedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
cmd = args.firingEvent.sender
inputs = cmd.commandInputs
materialListInput = None
filterInput = None
materialLibInput = None
global commandId
for inputI in inputs:
if inputI.id == commandId + '_materialList':
materialListInput = inputI
elif inputI.id == commandId + '_filter':
filterInput = inputI
elif inputI.id == commandId + '_materialLib':
materialLibInput = inputI
cmdInput = args.input
if cmdInput.id == commandId + '_materialLib' or cmdInput.id == commandId + '_filter':
materials = getMaterialsFromLib(materialLibInput.selectedItem.name, filterInput.value)
replaceItems(materialListInput, materials)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class ApplyMaterialToSelectionCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
command = args.firingEvent.sender
inputs = command.commandInputs
for input in inputs:
if input.id == commandId + '_selection':
selectionInput = input
elif input.id == commandId + '_materialList':
materialListInput = input
entities = getSelectedEntities(selectionInput)
if len(entities) == 0:
return
if not materialListInput.selectedItem:
if ui:
ui.messageBox('Material is not selected.')
return
material = getMaterial(materialListInput.selectedItem.name)
if not material:
return
applyMaterialToEntities(material, entities)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class ApplyMaterialToSelectionCommandDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
# when the command is done, terminate the script
# this will release all globals which will remove all event handlers
adsk.terminate()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class ApplyMaterialToSelectionCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
cmd = args.command
cmd.isRepeatable = False
onExecute = ApplyMaterialToSelectionCommandExecuteHandler()
cmd.execute.add(onExecute)
onDestroy = ApplyMaterialToSelectionCommandDestroyHandler()
cmd.destroy.add(onDestroy)
onInputChanged = ApplyMaterialInputChangedHandler()
cmd.inputChanged.add(onInputChanged)
# keep the handler referenced beyond this function
handlers.append(onExecute)
handlers.append(onDestroy)
handlers.append(onInputChanged)
# Define the inputs.
inputs = cmd.commandInputs
global commandId
selectionInput = inputs.addSelectionInput(commandId + '_selection', 'Select', 'Select bodies or occurrences')
selectionInput.setSelectionLimits(1)
selectionInput.selectionFilters = ['SolidBodies', 'Occurrences']
materialLibInput = inputs.addDropDownCommandInput(commandId + '_materialLib', 'Material Library', adsk.core.DropDownStyles.LabeledIconDropDownStyle)
listItems = materialLibInput.listItems
materialLibNames = getMaterialLibNames()
for materialName in materialLibNames :
listItems.add(materialName, False, '')
listItems[0].isSelected = True
materialListInput = inputs.addDropDownCommandInput(commandId + '_materialList', 'Material', adsk.core.DropDownStyles.TextListDropDownStyle)
materials = getMaterialsFromLib(materialLibNames[0], '')
listItems = materialListInput.listItems
for materialName in materials :
listItems.add(materialName, False, '')
listItems[0].isSelected = True
inputs.addStringValueInput(commandId + '_filter', 'Filter', '')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def getMaterial(materialName):
materialLibs = app.materialLibraries
material = None
for materialLib in materialLibs:
materials = materialLib.materials
try:
material = materials.itemByName(materialName)
except:
pass
if material:
break
return material
def getSelectedEntities(selectionInput):
entities = []
for x in range(0, selectionInput.selectionCount):
mySelection = selectionInput.selection(x)
selectedObj = mySelection.entity
if type(selectedObj) is adsk.fusion.BRepBody or type(selectedObj) is adsk.fusion.Component:
entities.append(selectedObj)
elif type(selectedObj) is adsk.fusion.Occurrence:
entities.append(selectedObj.component)
return entities
def applyMaterialToEntities(material, entities):
for entity in entities:
entity.material = material
def replaceItems(cmdInput, newItems):
cmdInput.listItems.clear()
if len(newItems) > 0:
for item in newItems:
cmdInput.listItems.add(item, False, '')
cmdInput.listItems[0].isSelected = True
def getMaterialLibNames():
materialLibs = app.materialLibraries
libNames = []
for materialLib in materialLibs:
if materialLib.materials.count > 0:
libNames.append(materialLib.name)
return libNames
def getMaterialsFromLib(libName, filterExp):
global materialsMap
materialList = None
if libName in materialsMap:
materialList = materialsMap[libName]
else:
materialLib = app.materialLibraries.itemByName(libName)
materials = materialLib.materials
materialNames = []
for material in materials:
materialNames.append(material.name)
materialsMap[libName] = materialNames
materialList = materialNames
if filterExp and len(filterExp) > 0:
filteredList = []
for materialName in materialList:
if materialName.lower().find(filterExp.lower()) >= 0:
filteredList.append(materialName)
return filteredList
else:
return materialList