Command Manager issue

Command Manager issue

NachoShaw
Advisor Advisor
623 Views
2 Replies
Message 1 of 3

Command Manager issue

NachoShaw
Advisor
Advisor

Hi

 

I seem to be having an issue with using the Command Manager. Whenever the commandmanager is executed, inventor crashes. When i place a dummy MsgBox("Test") directly above the commandmanager, it pops up so, i know that everything is working up to that point

. Even when i place a MsgBox(SymbolName), i get the correct symbol name displayed.

 

whats even more strange is if i delete the code and pasted the exact same code back, it works for a bit then crashes. It worked last night just fine and today, it doesn't work....

 

Any ideas? Here is my code.

Public Sub InsertSymbol(SymbolName As String)
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument

On Error GoTo NodeError

    Set oSheet = oDrawDoc.ActiveSheet
    oSheet.Activate

    ThisApplication.ActiveView.Fit
    
    Dim S As String
    S = SymbolName
    
' Get the sketched symbol named "?".
Dim oNode As BrowserNode
Set oNode = GetSymbolNode(oDrawDoc.BrowserPanes.ActivePane.TopNode, S)

' Select the symbol.
Dim oSelectSet As SelectSet
Set oSelectSet = oDrawDoc.SelectSet
oSelectSet.Clear
oNode.DoSelect


DoEvents


' Execute the Insert Drawing Sketch Symbol command.
ThisApplication.CommandManager.ControlDefinitions.Item("DrawingSketchSymbolInsertCtxCmd").Execute
Exit Sub

NodeError:
MsgBox ("There is an issue with the current Sketch Symbol. It either no longer exists or it has been renamed." & _
    vbCrLf & vbCrLf & _
    "Please check and try again"), vbOKOnly, "Browser Issue"

End Sub

Private Function GetSymbolNode(ByVal TopNode As BrowserNode, ByVal SymbolName As String) As BrowserNode
' Iterate through the children of the input node.
Dim oNode As BrowserNode
For Each oNode In TopNode.BrowserNodes
' Look for the "Sketched Symbols node".
If oNode.BrowserNodeDefinition.Label = "Sketched Symbols" Then
' Find the specified node.
Dim oSymbolNode As BrowserNode
For Each oSymbolNode In oNode.BrowserNodes
If UCase(oSymbolNode.BrowserNodeDefinition.Label) = UCase(SymbolName) Then
' Assign the node as the output value and exit the function.
Set GetSymbolNode = oSymbolNode
Exit Function
End If
Next
End If

' Check if the node has been found and exit the function.
If Not GetSymbolNode Is Nothing Then
Exit Function
Else
' Recursively call this function to traverse the browser tree.
Set GetSymbolNode = GetSymbolNode(oNode, SymbolName)
End If
Next
End Function

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
624 Views
2 Replies
Replies (2)
Message 2 of 3

frederic.vandenplas
Collaborator
Collaborator

@NachoShaw This is how i did it in vba, review the code attached, there is also a vba form that you can import attached here.

It's a better approach then through the nodes.

You can call directly drawingresources, sketched symbols and so on.

 

Option Explicit

Public Sub CmdOK_click()
 On Error GoTo ErrorHandler

   Dim oDestinationDocument As DrawingDocument
   Set oDestinationDocument = ThisApplication.ActiveDocument
  
    Dim oSourceDocument As DrawingDocument
    
    Set oSourceDocument = ThisApplication.Documents.Open("fullfilename to template.idw", False)
    
    Dim oSourceSketchedSymbolDef As SketchedSymbolDefinition
    
    ' Get the transaction manager from the application
    Dim oTxnMgr As TransactionManager
    Set oTxnMgr = ThisApplication.TransactionManager
    
 ' Start a regular transaction
    Dim oTxn As Transaction
    Set oTxn = oTxnMgr.StartTransaction(oDestinationDocument, "Copy Sketched Symbols")
   
        
   Dim i As Integer
 
    For i = 0 To Me.LstStickers.ListCount - 1
    
    If Me.LstStickers.Selected(i) = True Then
    
        Set oSourceSketchedSymbolDef = oSourceDocument.SketchedSymbolDefinitions.Item(i + 1)
        Set oSourceSketchedSymbolDef = oSourceSketchedSymbolDef.CopyTo(oDestinationDocument, True)
    End If
    
    Next i
    
    oDestinationDocument.Update
    oDestinationDocument.Update2
    oTxn.End
    
    oSourceDocument.Close
    
    Unload Me
    Exit Sub
ErrorHandler:
     If Err.Number = 91 Then
        MsgBox "No document open."
        Exit Sub
     Else
        MsgBox "Err.Number"
     End If
End Sub



Public Sub UserForm_Initialize()
  On Error GoTo ErrorHandler
     
    If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then
    MsgBox "Open a drawing before running this macro!", vbExclamation, "No drawing open"
    Exit Sub
    End If
  
    Dim oSourceDocument As DrawingDocument
    Dim oSourceSketchedSymbolDef As SketchedSymbolDefinition

    Set oSourceDocument = ThisApplication.Documents.Open("fullfilename to template.idw", False)
    
    For Each oSourceSketchedSymbolDef In oSourceDocument.SketchedSymbolDefinitions
        
        Call Me.LstStickers.AddItem(oSourceSketchedSymbolDef.Name)
    
    Next
       
    oSourceDocument.Close (True)
   Exit Sub
ErrorHandler:
     If Err.Number = 91 Then
        MsgBox "No document open."
        Exit Sub
     Else
        MsgBox "Err.Number
     End If
        oSourceDocument.Close (True)
     End Sub
Private Sub cmdCancel_Click()
Unload Me
End Sub
If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes
Message 3 of 3

NachoShaw
Advisor
Advisor

Hi

 

Thanks for the reply & code snippet. I need to get the sketch symbol attached to the mouse pointer for positioning. The code method of AddItem doesn't allow that and I cannot rely on a mouse click first, then insert the Symbol, then reposition as its much more labour intensive. (I know it doesn't see it but when you have a lot to do, repetitive clicking does cause people hand pain etc etc)

 

There isn't any reason why my code shouldn't work, its the actual Execute that crashes. Whats more weird is that it worked until it crashes, then once it has crashed it doesn't work again ever unless I re-paste the exact same code.

 

Maybe if the API provided better insert options like attaching members to the the mouse pointer for inserting, we wouldn't have to fudge it and make workarounds.....

 

 

Cheers

 

Nacho

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes