This is my code using your sample.
It goes through the ui.selectentity code, but doesn't stop to get the face and returns out of the code.
def drawBolt(design, numBushingID, numBushingOD, numBushingLength, numClearLength, numHeadDiam, numHeadHeight😞
ui = None
try:
# Create a new component by creating an occurrence.
occs = design.rootComponent.occurrences
mat = adsk.core.Matrix3D.create()
newOcc = occs.addNewComponent(mat)
newComp = adsk.fusion.Component.cast(newOcc.component)
ui = adsk.core.UserInterface
# Everthing here expects all distances to be in centimeters,
# so convert for the bolt creation.
sizeUnits = 2.54
numClearDiam = (numBushingID + .125) * sizeUnits
numBushingID = numBushingID * sizeUnits
numPressDiam = (numBushingOD - .0005) * sizeUnits
numBushingOD = numBushingOD * sizeUnits
numBushingLength = numBushingLength * sizeUnits
numCboreDiam = (numHeadDiam + .01) * sizeUnits
numHeadDiam = numHeadDiam * sizeUnits
numHeadHeight = numHeadHeight * sizeUnits
numClearLength = numClearLength * sizeUnits
try:
returnValue :adsk.core.Selections = ui.selectEntity(
'Select Face', 'PlanarFaces')
except:
return <<<<< executes to here and exits
xyPlane :adsk.fusion.BRepFace = returnValue.entity
xyPlane :adsk.fusion.Sketch = sketches.add(xyPlane)
pnt :adsk.core.Point3D = returnValue.point
mat :adsk.core.Matrix3D = xyPlane.transform
mat.invert()
pnt.transformBy(mat)
# create the head
headSketch = sketches.add(xyPlane)
headCircle = headSketch.sketchCurves.sketchCircles.addByCenterRadius(center, numHeadDiam / 2)
headSketch.sketchDimensions.addDiameterDimension(headCircle, adsk.core.Point3D.create(1, 1, 0))
headSketch.name = 'Head Diameter Sketch'
extrudes = newComp.features.extrudeFeatures
headProf = headSketch.profiles[0]
headExtInput = extrudes.createInput(headProf, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
distance1 = adsk.core.ValueInput.createByReal(numHeadHeight)
headExtInput.setDistanceExtent(False, distance1)
headExt = extrudes.add(headExtInput)
headExt.name = 'Head Height'
# Create the OD body
bushingOdSketch = sketches.add(xyPlane)
bushingOdCircle = bushingOdSketch.sketchCurves.sketchCircles.addByCenterRadius(center, numBushingOD / 2)
bushingOdSketch.sketchDimensions.addDiameterDimension(bushingOdCircle, adsk.core.Point3D.create(1, 0, 0))
bushingOdSketch.name = 'O.D. Sketch'
bushingOdProf = bushingOdSketch.profiles[0]
bushingOdExtInput = extrudes.createInput(bushingOdProf, adsk.fusion.FeatureOperations.JoinFeatureOperation)
distance2 = adsk.core.ValueInput.createByReal(numBushingLength)
bushingOdExtInput.setDistanceExtent(False, distance2)
bushingOdExt = extrudes.add(bushingOdExtInput)
bushingOdExt.name = 'Bushing Length'
# Create the ID cut
bushingIdSketch = sketches.add(xyPlane)
bushingIdCircle = bushingIdSketch.sketchCurves.sketchCircles.addByCenterRadius(center, numBushingID / 2)
bushingIdSketch.sketchDimensions.addDiameterDimension(bushingIdCircle, adsk.core.Point3D.create(1, 1, 0))
bushingIdSketch.name = 'I.D. Sketch'
bushingIdProf = bushingIdSketch.profiles[0]
bushingIdExtInput = extrudes.createInput(bushingIdProf, adsk.fusion.FeatureOperations.CutFeatureOperation)
distance3 = adsk.core.ValueInput.createByReal(numBushingLength)
bushingIdExtInput.setDistanceExtent(False, distance3)
bushingIdExtInput.participantBodies = [bushingOdExt.bodies.item(0)]
bushingIdExt = extrudes.add(bushingIdExtInput)
bushingIdExt.name = 'Bushing Length'
# Create the bushing fillet
bushingEnds = bushingOdExt.endFaces.item(0)
filletEdge = bushingEnds.edges.item(0)
edgeCol = adsk.core.ObjectCollection.create()
edgeCol.add(filletEdge)
filletFeats = newComp.features.filletFeatures
filletInput = filletFeats.createInput()
filletSize = adsk.core.ValueInput.createByReal(.254)
filletInput.addConstantRadiusEdgeSet(edgeCol, filletSize, True)
newFilletFeats = filletFeats.add(filletInput)
newFilletFeats.name = 'Fillet'
# Name the body
fc = bushingOdExt.faces[0]
bd = fc.body
bd.name = _inputCatalogNumber.text + ' Bushing'
fusionMaterials = _app.materialLibraries.itemByName('Fusion 360 Appearance Library')
appear = fusionMaterials.appearances.itemByName('Powder Coat (Blue)')
bd.appearance = appear
# Group everything used to create the bushing body in the timeline.
bushingTimelineGroups = design.timeline.timelineGroups
bushingOccIndex = newOcc.timelineObject.index
filletFeatsIndex = newFilletFeats.timelineObject.index
bushingTimelineGroup = bushingTimelineGroups.add(bushingOccIndex, filletFeatsIndex)
bushingTimelineGroup.name = _inputCatalogNumber.text + ' Bushing'
# Create a clearance length extrude. Used in parameter expressions
clearanceLengthSketch = sketches.add(xyPlane)
clearanceLengthCircle = clearanceLengthSketch.sketchCurves.sketchCircles.addByCenterRadius(center, .01)
clearanceLengthSketch.name = 'Clearance Length Sketch'
clearanceLengthProf = clearanceLengthSketch.profiles[0]
clearanceLengthExtInput = extrudes.createInput(clearanceLengthProf, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
distance3 = adsk.core.ValueInput.createByReal(numClearLength * -1)
clearanceLengthExtInput.setDistanceExtent(False, distance3)
clearanceLengthExt = extrudes.add(clearanceLengthExtInput)
clearanceLengthExt.name = 'Clearance Length'
clearanceLengthExt.isSuppressed = True
# Create the bushing press fit
cutBushingSketch = sketches.add(xyPlane)
cutBushingCircle = cutBushingSketch.sketchCurves.sketchCircles.addByCenterRadius(center, numPressDiam / 2)
cutBushingSketch.sketchDimensions.addDiameterDimension(cutBushingCircle, adsk.core.Point3D.create(0, 1, 0))
cutBushingSketch.name = 'Bushing Press Fit Sketch'
cutBushingProf = cutBushingSketch.profiles[0]
cutBushingExtInput = extrudes.createInput(cutBushingProf, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
distance4 = adsk.core.ValueInput.createByReal(numBushingLength)
cutBushingExtInput.setDistanceExtent(False, distance4)
cutBushingExt = extrudes.add(cutBushingExtInput)
cutBushingExt.name = 'Bushing Press Fit'
# Create the head clearance
cutHeadSketch = sketches.add(xyPlane)
cutHeadCircle = cutHeadSketch.sketchCurves.sketchCircles.addByCenterRadius(center, numCboreDiam / 2)
cutHeadSketch.sketchDimensions.addDiameterDimension(cutHeadCircle, adsk.core.Point3D.create(-1, 1, 0))
cutHeadSketch.name = 'Head Clearance Sketch'
cutHeadProf = cutHeadSketch.profiles[0]
cutHeadExtInput = extrudes.createInput(cutHeadProf, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
distance5 = adsk.core.ValueInput.createByReal(numHeadHeight)
cutHeadExtInput.setDistanceExtent(False, distance5)
cutHeadExt = extrudes.add(cutHeadExtInput)
cutHeadExt.name = 'Head Clearance Length'
# Create the bushing clearance hole
cutBushingClearSketch = sketches.add(xyPlane)
cutBushingClearCircle = cutBushingClearSketch.sketchCurves.sketchCircles.addByCenterRadius(center, numClearDiam / 2)
cutBushingClearSketch.sketchDimensions.addDiameterDimension(cutBushingClearCircle, adsk.core.Point3D.create(-1, 0, 0))
cutBushingClearSketch.name = 'Bushing Clearance Sketch'
cutBushingClearProf = cutBushingClearSketch.profiles[0]
cutBushingClearExtInput = extrudes.createInput(cutBushingClearProf, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
distance6 = adsk.core.ValueInput.createByReal(numClearLength * -1)
cutBushingClearExtInput.setDistanceExtent(False, distance6)
cutBushingClearExt = extrudes.add(cutBushingClearExtInput)
cutBushingClearExt.name = 'Bushing Clearance Length'
# Create cut body with combine
cutBody = cutBushingExt.bodies[0]
bodyCollection = adsk.core.ObjectCollection.create()
bodyCollection.add(cutHeadExt.bodies[0])
bodyCollection.add(cutBushingClearExt.bodies[0])
model = design.activeComponent
features = model.features
combineFeat = features.combineFeatures
combineInput = combineFeat.createInput(cutBody, bodyCollection)
combineFeatur = combineFeat.add(combineInput)
# Name the cut body
fc = combineFeatur.faces[0]
bd = fc.body
bd.name = 'Bushing Cut Body'
fusionMaterials = _app.materialLibraries.itemByName('Fusion 360 Appearance Library')
appear = fusionMaterials.appearances.itemByName('Powder Coat (Green)')
bd.appearance = appear
# Group everything used to create the bushing cut body in the timeline.
cutTimelineGroups = design.timeline.timelineGroups
cutOccIndex = clearanceLengthSketch.timelineObject.index
combineFeatIndex = combineFeatur.timelineObject.index
cutTimelineGroup = cutTimelineGroups.add(cutOccIndex, combineFeatIndex)
cutTimelineGroup.name = _inputCatalogNumber.text + ' Cut Body'
# Add an attribute to the component with all of the input values. This might
# be used in the future to be able to edit the bushing.
bushingValues = {}
bushingValues['bushingID'] = str(numBushingID)
bushingValues['bushingOD'] = str(numBushingOD)
bushingValues['bushingLength'] = str(numBushingLength)
bushingValues['headDiameter'] = str(numHeadDiam)
bushingValues['headHeight'] = str(numHeadHeight)
bushingValues['catalogNumber'] = _inputCatalogNumber
attrib = newComp.attributes.add('CreateShbu', 'Values', str(bushingValues))
newComp.name = _inputCatalogNumber.text + ' Guide Bushing'
return newComp
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))