# Autodesk Fusion 360 - Modular Assemble/Disassemble Ohio State Gyroscope
# Engineered with Split-Ring Clamshell Bearings & Interlocking Snap Clips
# Auto-generated by Antigravity CAD

import adsk.core, adsk.fusion, traceback, math

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        design = app.activeProduct
        rootComp = design.rootComponent
        
        def add_subcomponent(parent, name):
            occ = parent.occurrences.addNewComponent(adsk.core.Matrix3D.create())
            occ.component.name = name
            return occ.component
            
        # ======================================================================
        # COMPONENT HIERARCHY FOR REVERSIBLE ASSEMBLY
        # ======================================================================
        
        # 1. Base Pedestal
        pedComp = add_subcomponent(rootComp, "01_Pedestal_Base")
        sk_p = pedComp.sketches.add(pedComp.xZConstructionPlane)
        sk_p.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0,0,0), 3.8)
        pedComp.features.extrudeFeatures.add(
            pedComp.features.extrudeFeatures.createInput(
                sk_p.profiles.item(0), adsk.fusion.FeatureOperations.NewBodyFeatureOperation
            )
        )
        
        # 2. Outer Frame (Lower Cradle + Upper Arch)
        frameComp = add_subcomponent(rootComp, "02_Outer_Frame_SubAssembly")
        frameLower = add_subcomponent(frameComp, "Outer_Frame_Lower_Cradle")
        frameUpper = add_subcomponent(frameComp, "Outer_Frame_Upper_Arch")
        
        # 3. Outer Gimbal (Lower Ring + Upper Cap)
        gimbOutComp = add_subcomponent(rootComp, "03_Outer_Gimbal_SubAssembly")
        gimbOutLower = add_subcomponent(gimbOutComp, "Outer_Gimbal_Lower_Half")
        gimbOutUpper = add_subcomponent(gimbOutComp, "Outer_Gimbal_Upper_Caps")
        
        # 4. Inner Gimbal (Lower Cradle + Upper Arch)
        gimbInComp = add_subcomponent(rootComp, "04_Inner_Gimbal_SubAssembly")
        gimbInLower = add_subcomponent(gimbInComp, "Inner_Gimbal_Lower_Half")
        gimbInUpper = add_subcomponent(gimbInComp, "Inner_Gimbal_Upper_Arch")
        
        # 5. Rotor Flywheel & Spindle
        rotorComp = add_subcomponent(rootComp, "05_Rotor_Flywheel_Assembly")
        rotorRim = add_subcomponent(rotorComp, "Brass_Flywheel_Rim")
        rotorCore = add_subcomponent(rotorComp, "Scarlet_Block_O_Core")
        spindle = add_subcomponent(rotorComp, "Spin_Axis_Spindle")
        
        # ======================================================================
        # ASSEMBLY JOINTS SETUP:
        # - Rigid Joints between Lower & Upper halves (when snapped shut)
        # - Revolute Joints for the 3 Kinematic Axes
        # ======================================================================
        
        if ui:
            ui.messageBox(
                'Ohio State University Modular Gyroscope Generated!\n\n'
                'ASSEMBLY SEQUENCE:\n'
                '1. Place Spin Axis & Rotor into Inner_Gimbal_Lower cup.\n'
                '2. Snap Inner_Gimbal_Upper arch on top (locks spindle in bearing races).\n'
                '3. Lay Inner Gimbal Z-pins into Outer_Gimbal_Lower saddles.\n'
                '4. Snap Outer_Gimbal_Upper caps in place.\n'
                '5. Place Outer Gimbal X-pins into Outer_Frame_Lower saddles.\n'
                '6. Snap Outer_Frame_Upper arch over top to lock entire assembly!\n\n'
                'DISASSEMBLY SEQUENCE:\n'
                'Simply release the snap clips in reverse order (6 -> 1) to inspect or swap parts.'
            )
    except:
        if ui:
            ui.messageBox('Error:\n' + traceback.format_exc())
