"""This file acts as the main module for this script.""" import traceback import adsk.core import adsk.fusion # Initialize the global variables for the Application and UserInterface objects. app = adsk.core.Application.get() ui = app.userInterface def run(_context: str): """This function is called by Fusion when the script is run.""" try: des = adsk.fusion.Design.cast(app.activeProduct) if not des: ui.messageBox('No active Fusion design', 'Error') return # Have a user coordinate system (UCS) selected. sel = ui.selectEntity('Select a UCS.', 'UserCoordinateSystems') if not sel: return ucs: adsk.fusion.UserCoordinateSystem = sel.entity # type: ignore # Have the user specify the length of the I-Beam. (length, isCancelled) = ui.inputBox('Enter the length of the I-Beam.', 'I-Beam Length', '20') if isCancelled: return # Get the component the UCS is in. comp = ucs.parentComponent # Create a sketch on the XY plane of the UCS. sk = comp.sketches.add(ucs.xYConstructionPlane) # Draw an I-beam shape in the sketch. The sizes are hard-coded here. lines = sk.sketchCurves.sketchLines width = 10 height = 15 thickness = 2 # Calculate points for an I-Beam centered on the origin of the sketch. p1 = adsk.core.Point3D.create(-width/2, height/2, 0) p2 = adsk.core.Point3D.create(width/2, height/2, 0) p3 = adsk.core.Point3D.create(width/2, height/2 - thickness, 0) p4 = adsk.core.Point3D.create(thickness/2, height/2 - thickness, 0) p5 = adsk.core.Point3D.create(thickness/2, thickness - height/2, 0) p6 = adsk.core.Point3D.create(width/2, thickness - height/2, 0) p7 = adsk.core.Point3D.create(width/2, -height/2, 0) p8 = adsk.core.Point3D.create(-width/2, -height/2, 0) p9 = adsk.core.Point3D.create(-width/2, -height/2 + thickness, 0) p10 = adsk.core.Point3D.create(-thickness/2, -height/2 + thickness, 0) p11 = adsk.core.Point3D.create(-thickness/2, height/2 - thickness, 0) p12 = adsk.core.Point3D.create(-width/2, height/2 - thickness, 0) # Draw the lines of the I-Beam shape. l1 = lines.addByTwoPoints(p1, p2) l2 = lines.addByTwoPoints(l1.endSketchPoint, p3) l3 = lines.addByTwoPoints(l2.endSketchPoint, p4) l4 = lines.addByTwoPoints(l3.endSketchPoint, p5) l5 = lines.addByTwoPoints(l4.endSketchPoint, p6) l6 = lines.addByTwoPoints(l5.endSketchPoint, p7) l7 = lines.addByTwoPoints(l6.endSketchPoint, p8) l8 = lines.addByTwoPoints(l7.endSketchPoint, p9) l9 = lines.addByTwoPoints(l8.endSketchPoint, p10) l10 = lines.addByTwoPoints(l9.endSketchPoint, p11) l11 = lines.addByTwoPoints(l10.endSketchPoint, p12) l12 = lines.addByTwoPoints(l11.endSketchPoint, l1.startSketchPoint) # Add a "center" line to use to position the center of the I-Beam. midline = lines.addByTwoPoints(adsk.core.Point3D.create(-thickness/2, 0, 0), adsk.core.Point3D.create(thickness/2, 0, 0)) midline.isConstruction = True # Constraint the line to the midpoints of the two long vertical lines. constraints = sk.geometricConstraints constraints.addMidPoint(midline.startSketchPoint, l10) constraints.addMidPoint(midline.endSketchPoint, l4) # Add geometric constraints to fully constrain the sketch. constraints.addHorizontal(l1) constraints.addPerpendicular(l12, l1) constraints.addPerpendicular(l3, l4) constraints.addPerpendicular(l4, l5) constraints.addPerpendicular(l5, l6) constraints.addEqual(l1, l7) constraints.addEqual(l12, l2) constraints.addEqual(l12, l6) constraints.addEqual(l11, l3) constraints.addCollinear(l3, l11) constraints.addCollinear(l5, l9) constraints.addCollinear(l2, l6) constraints.addCollinear(l8, l12) constraints.addParallel(l4, l10) dims = sk.sketchDimensions dims.addOffsetDimension(l1, l7, adsk.core.Point3D.create(-width/2 - 1.5, 0, 0)) dims.addOffsetDimension(l12, l2, adsk.core.Point3D.create(0, height/2 + 1, 0)) offsetDim = dims.addOffsetDimension(l4, l10, adsk.core.Point3D.create(0, 0, 0)) distDim = dims.addDistanceDimension(l12.startSketchPoint, l12.endSketchPoint, adsk.fusion.DimensionOrientations.VerticalDimensionOrientation, adsk.core.Point3D.create(-width/2 - 1, height/2 + 1, 0)) # type: ignore offsetDim.parameter.expression = distDim.parameter.name # Project the origin point of the UCS to the sketch and constrain the centerpoint of the midline to it. result = sk.project2([ucs.originConstructionPoint], True) originPnt: adsk.fusion.SketchPoint = result[0] # type: ignore constraints.addMidPoint(originPnt, midline) # Get the profile defined by the I-Beam sketch and extrude it to create a 3D body. prof = sk.profiles.item(0) extInput = comp.features.extrudeFeatures.addSimple(prof, adsk.core.ValueInput.createByString(length), adsk.fusion.FeatureOperations.NewBodyFeatureOperation) # type: ignore except: #pylint:disable=bare-except # Write the error message to the TEXT COMMANDS window. app.log(f'Failed:\n{traceback.format_exc()}')