Placing Sketch Block With Command / Control Definition

Placing Sketch Block With Command / Control Definition

matt_jlt
Collaborator Collaborator
675 Views
3 Replies
Message 1 of 4

Placing Sketch Block With Command / Control Definition

matt_jlt
Collaborator
Collaborator

I'm having trouble placing a sketch block using the control definition method and was hoping someone could help me out.

I can't even get the command to run, I have tried messing around with the post private event and parsing the block name as a string or filename etc. but nothing seems to work.

 

Any help is appreciated.

 

Thanks, Matt.

 

Dim oCM As CommandManager
Set oCM = ThisApplication.CommandManager

' Tried using post private event to see if it's required? 'Call oCM.PostPrivateEvent(kStringEvent, "MyBlockName") Dim oCtrlDef As ControlDefinition Set oCtrlDef = oCM.ControlDefinitions.Item("SketchRigidSetPlaceInstanceCmd") Call oCtrlDef.Execute
0 Likes
Accepted solutions (1)
676 Views
3 Replies
Replies (3)
Message 2 of 4

danijel.radenkovic
Collaborator
Collaborator

Hi,

Do you need a code or free tool?

If you need a tool to import sketch block, then can be very usefull this tool, provided by Avant.CAD.

Special thanks to Avant.CAD for this tool. I am using it everyday.Smiley Wink

 

https://apps.autodesk.com/INVNTOR/en/Detail/Index?id=appstore.exchange.autodesk.com%3aimportsketchbl...

 

Regards

Danijel

Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes
Message 3 of 4

matt_jlt
Collaborator
Collaborator

I need the code thanks.

0 Likes
Message 4 of 4

matt_jlt
Collaborator
Collaborator
Accepted solution

With the help from an old post, I worked it out. I ended up having to pre-select the sketch block via select set for it to work. Code below is working example on how to copy a sketch block from a template part file and insert it into the active part.

 

Final Code:

Public Sub InsertBlockFromTemplate()


    Dim oNVM As NameValueMap
    Set oNVM = ThisApplication.TransientObjects.CreateNameValueMap
    
    Dim oTemplate As PartDocument
    Set oTemplate = ThisApplication.Documents.OpenWithOptions("C:\btemplate.ipt", oNVM, False)
    
    Dim oTempCompDef As PartComponentDefinition
    Set oTempCompDef = oTemplate.ComponentDefinition
    
    Dim oTempSB As SketchBlockDefinition
    Set oTempSB = oTempCompDef.SketchBlockDefinitions.Item("Block Name")
        
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveEditDocument ' This makes the code work if in assembly edit environment
    
    If oDoc.DocumentType <> kPartDocumentObject Then
        MsgBox ("Active Edit Document Is Not A Part File")
    End If
        
    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oDoc.ComponentDefinition
    
    ' Copy sketch block definition to current part document & close template
    Call oTempSB.CopyTo(oDoc, True)
    Call oTemplate.Close
    
    ' Expand Block Browser Node
    oDoc.BrowserPanes.ActivePane.TopNode.BrowserNodes.Item(1).Expanded = True
        
    ' Define local sketch block definition
    Dim oSB As SketchBlockDefinition
    Set oSB = oCompDef.SketchBlockDefinitions.Item("Block Name")
    
    ' Need to preselect the block definition
    Call ThisApplication.activeDocument.SelectSet.Clear
    Call ThisApplication.activeDocument.SelectSet.Select(oSB)
    
    ' Define & Execute command
    Dim oCtrlDef As ControlDefinition
    Set oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("SketchRigidSetPlaceInstanceCmd")

    Call oCtrlDef.Execute
End Sub

 

 

Referenced Post with solution / code

http://forums.autodesk.com/t5/inventor-customization/inventor-vba-insert-sketched-block-into-part/m-...

 

Regards, Matt.

0 Likes