Help with VBA define flat pattern alignment

Help with VBA define flat pattern alignment

nathan_m_gardner
Enthusiast Enthusiast
100 Views
0 Replies
Message 1 of 1

Help with VBA define flat pattern alignment

nathan_m_gardner
Enthusiast
Enthusiast

I am playing around with Inventor 2023, I tried to use ChatGPT and Gemini but I keep running into the same issue. Unable to align sheet base on edge selected in the .IAM model. 

 

Is this even possible to do?

 

Public Sub AlignFlatPattern_RemoteControl()
    Dim oApp As Application: Set oApp = ThisApplication
    
    ' 1. Pick the edge from the assembly
    Dim oSelectedEdge As Object
    Set oSelectedEdge = oApp.CommandManager.Pick(kPartEdgeFilter, "Select a LONG STRAIGHT edge on the Flat Face")
    If oSelectedEdge Is Nothing Then Exit Sub

    ' 2. Identify the Part and open it
    Dim oPartDoc As PartDocument
    If TypeOf oSelectedEdge Is EdgeProxy Then
        Set oPartDoc = oSelectedEdge.ContainingOccurrence.Definition.Document
    Else
        Set oPartDoc = oSelectedEdge.Parent.Document
    End If

    ' ACTIVATE is required for SendKeys and UI Commands
    Dim oFullDoc As PartDocument
    Set oFullDoc = oApp.Documents.Open(oPartDoc.FullFileName, True)
    oFullDoc.Activate

    ' 3. Enter Flat Pattern Environment
    Dim oSMDef As SheetMetalComponentDefinition: Set oSMDef = oFullDoc.ComponentDefinition
    If Not oSMDef.HasFlatPattern Then oSMDef.Unfold
    
    ' Late binding the Flat Pattern to avoid 438 errors
    Dim oFlatPattern As Object: Set oFlatPattern = oSMDef.FlatPattern
    oFlatPattern.Edit

    ' 4. Find the Native Edge (The one inside the IPT file)
    Dim oNativeEdge As Object
    If TypeOf oSelectedEdge Is EdgeProxy Then
        Set oNativeEdge = oSelectedEdge.NativeObject
    Else
        Set oNativeEdge = oSelectedEdge
    End If

    ' 5. THE WORKAROUND: Select and "Press the Button"
    oFullDoc.SelectSet.Clear
    oFullDoc.SelectSet.Select oNativeEdge
    
    ' Get the internal command for "Edit Flat Pattern Definition"
    Dim oCtrlDef As ControlDefinition
    Set oCtrlDef = oApp.CommandManager.ControlDefinitions.Item("SheetMetalFlatPatternEditDefinitionCmd")
    
    ' Execute the command (this opens the dialog box)
    oCtrlDef.Execute
    
    ' 6. Automate the Dialog Box
    ' We send "Enter" to click 'OK' on the dialog that just popped up
    DoEvents
    SendKeys "{ENTER}"
    DoEvents

    ' 7. Exit, Save, and Close
    oFlatPattern.ExitEdit
    oFullDoc.Save
    oFullDoc.Close
    
    MsgBox "Flat Pattern aligned successfully."
End Sub

 

0 Likes
101 Views
0 Replies
Replies (0)