Thank You @JeromeBriot 💥
See updated script below with parameter control. Just add extrusionHeight and smallSideLength to your parameter table,
For for search metadata:
In summary, this script is designed to automate the process of extruding selected sketches, specifically those with a side length close to a predefined value. It simplifies the task of creating bodies and components from certain sketches.
#Description-This script is designed to automate the process of extruding selected sketches, specifically those with a side length close to a predefined value. It simplifies the task of creating 3D bodies from certain sketches.
import adsk.core
import adsk.fusion
import adsk.cam
import traceback
from math import fabs
def run(context):
ui = None
try:
# Get the Fusion 360 application and user interface
app = adsk.core.Application.get()
ui = app.userInterface
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
# Access the user parameters
userParams = design.userParameters
# Get the values of 'smallSideLength' and 'extrusionHeight' parameters
smallSideLengthParam = userParams.itemByName('smallSideLength')
extrusionHeightParam = userParams.itemByName('extrusionHeight')
# Check if the parameters exist
if smallSideLengthParam is not None and extrusionHeightParam is not None:
smallSideLength = smallSideLengthParam.value
extrusionHeight = extrusionHeightParam.value
else:
ui.messageBox('Parameters "smallSideLength" or "extrusionHeight" not found.')
return
# Prompt the user to select a sketch
selection = ui.selectEntity('Select a sketch', 'Sketches')
if selection.entity:
# Get the root component of the design
rootComp = design.rootComponent
extrudes = rootComp.features.extrudeFeatures
# Create a value input for extrusion distance
distance = adsk.core.ValueInput.createByString('extrusionHeight')
# Get the selected sketch
sketch = selection.entity
# Iterate through the profiles in the sketch
for profile in sketch.profiles:
# Get the bounding box coordinates of the profile
coord1 = profile.boundingBox.maxPoint.asArray()
coord2 = profile.boundingBox.minPoint.asArray()
# Calculate the lengths of the sides of the bounding box
l1 = coord1[0] - coord2[0]
l2 = coord1[1] - coord2[1]
# Check if the length of either side matches the small side length
if fabs(l1 - smallSideLength) < 0.01 or fabs(l2 - smallSideLength) < 0.01:
# Extrude the profile to create a new body
extrude = extrudes.addSimple(profile, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
body = extrude.bodies.item(0)
body.createComponent()
except:
# Handle exceptions and display an error message
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))