A try except statement will work. The message box you're seeing is the result of the try failing and a message box is displayed in the except with the current error. If it's not working, there must be a small problem in your code.
Here's a modified version of your program with a try except around the creation code and the display of a custom message. If you don't want a message at all you can remove the messageBox line.
import adsk.core, adsk.fusion, traceback
draft = 5
def surfNormal(app,ui,ent):
# ui = adsk.core.UserInterface.cast(None)
try:
# app: adsk.fusion.Application = adsk.core.Application.get()
# ui = app.userInterface
des: adsk.fusion.Design = app.activeProduct
root: adsk.fusion.Component = des.rootComponent
face: adsk.fusion.BRepFace = ent.entity
click: adsk.core.Point3D = ent.point
# get SurfaceEvaluator
eva: adsk.core.SurfaceEvaluator = face.evaluator
# get Normal
normal: adsk.core.Vector3D
_, normal = eva.getNormalAtPoint(click)
normal.normalize()
return normal
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def selectEnt(msg: str,filtterStr: str) -> adsk.core.Selection:
try:
app = adsk.core.Application.get()
ui = app.userInterface
sel = ui.selectEntity(msg, filtterStr)
return sel
except:
return None
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
# Get active design
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
# Get root component in this design
rootComp = design.rootComponent
i=1
while i<=10:
dialogResult = ui.messageBox('Select the face(s) parallel to the base plane to draft from. Click "Yes" to add a face, "No to finish selections"','Draft Selection', adsk.core.MessageBoxButtonTypes.YesNoButtonType, adsk.core.MessageBoxIconTypes.QuestionIconType)
if dialogResult == adsk.core.DialogResults.DialogYes:
#select entity (ent = ui.selectEntity("Select a face to draft from.","Faces"))
ent = selectEnt("Select a face to draft from.","Faces",)
#get surface normal
norm = surfNormal(app,ui,ent);print(int(norm.x),int(norm.y),int(norm.z))
norm = str( int(norm.x) ) + str( int(norm.y) ) + str( int(norm.z) )
#get the face from the selection
face1 = adsk.fusion.BRepFace.cast(ent.entity)
try:
# Get all faces which connect to the first face
connectedFaces = [];
for edge in face1.edges:
for face in edge.faces:
if face1 != face:
connectedFaces.append(face)
# Create draft feature
drafts = rootComp.features.draftFeatures
draftFeatInput = drafts.createInput(connectedFaces, face1, True)
#set the draft direction based on the normal
# draftFeatInput.isDirectionFlipped = False
if norm == "001":
draftFeatInput.isDirectionFlipped = False
else:
draftFeatInput.isDirectionFlipped = True
#set the value of the angle
angle = adsk.core.ValueInput.createByString(str(int(draft)) + " deg")
angle2 = adsk.core.ValueInput.createByString(str(int(draft*-1)) + " deg")
#set for single angle
draftFeatInput.setTwoAngles(angle,angle2)
# draftFeatInput.setSingleAngle(False, angle)
#add the draft
drafts.add(draftFeatInput)
except:
ui.messageBox('Failed with my custom message.')
return
elif dialogResult == adsk.core.DialogResults.DialogNo:
break
i += 1
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian EkinsInventor and Fusion 360 API Expert
Website/Blog:
https://EkinsSolutions.com