Insertion of Part into a [weldment] Assembly via Macro at the part level

Insertion of Part into a [weldment] Assembly via Macro at the part level

RNDinov8r
Collaborator Collaborator
352 Views
1 Reply
Message 1 of 2

Insertion of Part into a [weldment] Assembly via Macro at the part level

RNDinov8r
Collaborator
Collaborator

I am working on adding some macros that can run via a custom button when in the part environment. I have a similar system for inserting parts/assemblies directly into our specific drawing templates, so I figured slight modifications to code would accomplish this new behavior. I am gettin the appropriate weldment templates to open, however, I am gettin the error that I don't have a part open to insert (an error programmed in the macro).  When stepping through the debug, I can see I am capturing the current part name, so I am pretty sure the issue is how I am handling the insertion into the actual assembly. For that matter, I know the CommandManager.StartCommand (kInsertPartCommand) is not correct. direction would be appreciated....here's the code (there's other stuff but this is the section I am concenred with):

 

Case kAssemblyDocumentObject, kPartDocumentObject

    sCurrentFilename = ThisApplication.ActiveDocument.FullFileName
   
    If sCurrentFilename = "" Then
        MsgBox "The active file must first be saved"
        Exit Sub
    End If
   
    'if you want to use the default template then set UseDefaultTemplate = True
    'if you want to use a custom template set the path and filename of sTemplatePart and UseDefaultTemaplte = False
   
    UseDefaultTemplate = False
    
    Dim sTemplateAssembly As String
    
    If Units = "Metric" Then
        sTemplateAssembly = "K:\R2016 Inventor\R2016 Templates\_RND\Metric Weldment.iam"
    ElseIf Units = "Imperial" Then
        sTemplateAssembly = "K:\R2016 Inventor\R2016 Templates\_RND\Std Weldment.iam"
    End If
   
    Select Case UseDefaultTemplate
        Case True
            Set oNewDoc = oApp.Documents.Add(kAssemblyDocumentObject, oApp.FileManager.GetTemplateFile(kAssemblyDocumentObject), True)
        Case False
            Set oNewDoc = oApp.Documents.Add(kAssemblyDocumentObject, sTemplateAssembly, True)
    End Select
         
     
    'Post the filename to the private event queue.
    Call ThisApplication.CommandManager.PostPrivateEvent(kFileNameEvent, sCurrentFilename)  '<---Pretty sure this works
       
    'Start the insert part into the weldment assembly command.
    Call oApp.CommandManager.StartCommand(kInsertPartCommand)  '<----Pretty sure this is my issue

Case Else

ErrorSub:
    MsgBox "You must first have a Part or Assembly document open"

End Select

0 Likes
353 Views
1 Reply
Reply (1)
Message 2 of 2

wayne.brill
Collaborator
Collaborator

Hi,

 

Here is a VBA example that creates a new assembly and then runs the AssemblyPlaceComponentCmd command. It uses the Execute method of the ControlDefinition (I am not sure what this is - "oApp.CommandManager.StartCommand").

 

Also the data in the PostPrivateEvent would need to be the path of the file you want to insert.

 

Public Sub insertPart_Command_Test()
    'Debug.Print ThisApplication.CommandManager.ActiveCommand
    
    Dim oNewDoc As AssemblyDocument
    Set oNewDoc = ThisApplication.Documents.Add(kAssemblyDocumentObject, ThisApplication.FileManager.GetTemplateFile(kAssemblyDocumentObject), True)
    
    Dim sCurrentFilename As String
    sCurrentFilename = "C:\Users\brillw\Documents\Inventor\WB_2-18-16\cube.ipt"
    
    'Post the filename to the private event queue.
    Call ThisApplication.CommandManager.PostPrivateEvent(kFileNameEvent, sCurrentFilename)
       
    Dim oControlDef As ControlDefinition
    Set oControlDef = ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyPlaceComponentCmd")
    
    ' Call oApp.CommandManager.StartCommand(kInsertPartCommand)  '<----Pretty sure this is my issue
    Call oControlDef.Execute
   
End Sub

If you want to place the occurrence in the assembly without any user interaction you could use the  ComponentOccurrences.Add method. See this topic in the help to get started.

Assembly_API_Help.jpg

 

 

Thanks,

Wayne



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes