try (destroyDialog objProcessor) catch()

global objProcessor

rollout objProcessor "Object Processor" width:300
(
    button btnStep1 "1) Convert to Poly" width:280 align:#center
    button btnStep2 "2) Weld Vertices" width:280 align:#center
    button btnStep3 "3) Attach Objects" width:280 align:#center
    button btnStep4 "4) Apply Chamfer" width:280 align:#center
    
    progressbar pbProgress height:10 color:green
    
    on btnStep1 pressed do (
        if selection.count == 0 do (
            messageBox "Select objects first!" title:"Error"
            return false
        )
        
        pbProgress.value = 0
        local successCount = 0
        local selArray = selection as array
        local total = selArray.count
        
        for i = 1 to total where isValidNode selArray[i] do (
            pbProgress.value = 100.0 * i / total
            try (
                local obj = selArray[i]
                local objName = obj.name
                
                -- Reset XForm
                resetXForm obj
                collapseStack obj
                
                -- Convert to Poly
                if classOf obj.baseObject != Editable_Poly do (
                    convertToPoly obj
                )
                
                -- Center Pivot
                centerPivot obj
                
                successCount += 1
                format "[Success] % converted to poly\n" objName
            ) catch (
                format "[Error] %: %\n" objName (getCurrentException())
            )
        )
        
        if successCount > 0 then (
            messageBox ("Converted " + successCount as string + " objects") title:"Complete"
        ) else (
            messageBox "Conversion failed!" title:"Error"
        )
        pbProgress.value = 0
    )
    
    on btnStep2 pressed do (
        if selection.count == 0 do (
            messageBox "Select objects first!" title:"Error"
            return false
        )
        
        pbProgress.value = 0
        local successCount = 0
        local selArray = selection as array
        local total = selArray.count
        
        for i = 1 to total where isValidNode selArray[i] do (
            pbProgress.value = 100.0 * i / total
            try (
                local obj = selArray[i]
                local objName = obj.name
                
                -- Use Vertex Weld modifier for reliable welding
                weldMod = Vertex_Weld()
                weldMod.threshold = 0.1
                addModifier obj weldMod
                collapseStack obj
                
                successCount += 1
                format "[Success] Welded vertices for %\n" objName
            ) catch (
                format "[Error] %: %\n" objName (getCurrentException())
            )
        )
        
        if successCount > 0 then (
            messageBox ("Welded " + successCount as string + " objects") title:"Complete"
        ) else (
            messageBox "Welding failed!" title:"Warning"
        )
        pbProgress.value = 0
    )
    
    on btnStep3 pressed do (
        if selection.count < 2 do (
            messageBox "Select at least 2 objects!" title:"Error"
            return false
        )
        
        pbProgress.value = 0
        local selArray = selection as array
        local baseObj = selArray[1]
        local baseObjName = baseObj.name
        local attachedCount = 0
        local failedObjects = #()
        local total = selArray.count - 1
        
        for i = 2 to selArray.count do (
            pbProgress.value = 100.0 * (i-1) / total
            if isValidNode selArray[i] do (
                local targetName = selArray[i].name
                
                try (
                    polyOp.attach baseObj selArray[i]
                    attachedCount += 1
                    format "[Success] % attached to %\n" targetName baseObjName
                ) catch (
                    format "[Error] %: %\n" targetName (getCurrentException())
                    append failedObjects targetName
                )
            )
        )
        
        if isValidNode baseObj do centerPivot baseObj
        
        if attachedCount > 0 then (
            messageBox ("Attached " + attachedCount as string + " objects") title:"Complete"
        ) else (
            messageBox "No objects were attached!" title:"Warning"
        )
        
        if failedObjects.count > 0 then (
            messageBox ("Failed to attach:\n" + (joinString failedObjects "\n")) title:"Warning"
        )
        
        pbProgress.value = 0
        try (select baseObj) catch ()
    )
    
    on btnStep4 pressed do (
        if selection.count == 0 do (
            messageBox "Select objects first!" title:"Error"
            return false
        )
        
        pbProgress.value = 0
        local successCount = 0
        local selArray = selection as array
        local total = selArray.count
        
        for i = 1 to total where isValidNode selArray[i] do (
            pbProgress.value = 100.0 * i / total
            try (
                local obj = selArray[i]
                local objName = obj.name
                
                -- Apply Chamfer
                chamferMod = Chamfer()
                chamferMod.segments = 2
                chamferMod.amount = 2.0
                addModifier obj chamferMod
                
                successCount += 1
                format "[Success] Chamfer applied to %\n" objName
            ) catch (
                format "[Error] %: %\n" objName (getCurrentException())
            )
        )
        
        if successCount > 0 then (
            messageBox ("Applied chamfer to " + successCount as string + " objects") title:"Complete"
        ) else (
            messageBox "Chamfer application failed!" title:"Warning"
        )
        pbProgress.value = 0
    )
)

createDialog objProcessor style:#(#style_titlebar, #style_border, #style_sysmenu)