Inventor VBA insert sketched block into part

Inventor VBA insert sketched block into part

frank_schalla
Advocate Advocate
1,698 Views
2 Replies
Message 1 of 3

Inventor VBA insert sketched block into part

frank_schalla
Advocate
Advocate

i try to solve the following stuff.

I open via VBA a Sketch Block template part from a new (other) part.

I copy this to the calling part (the part i call the function).

It's pasted into the sketched block definition fine 🙂

I think now about that it will be a smoother workflow when the user after the copy action get the new block on the cursor or in the smallest solution get a copy at 0.0 of the sketch

Is there a way to start the insert block command inside the part sketch enviroment via VBA ?

***************************

Private Sub Cmd_Insert_Block_Click()

Dim oCompDef As PartComponentDefinition
    Set oCompDef = Block_Template.ComponentDefinition

' Set a reference to the definition named by selection
Dim oSketchBlockDef As SketchBlockDefinition
    Set oSketchBlockDef = oCompDef.SketchBlockDefinitions.Item(CmB_Blocks.Text)
    
    'Check if block exist
Dim targetdocoCompDef As PartComponentDefinition
    Set targetdocoCompDef = targetdoc.ComponentDefinition
Dim targetdocoSketchBlockDef As SketchBlockDefinition
Dim Blockda As Integer
    Blockda = 0
    For Each targetdocoSketchBlockDef In targetdocoCompDef.SketchBlockDefinitions
        If targetdocoSketchBlockDef.Name = CmB_Blocks.Text Then
        Blockda = 1
        End If
    Next
    If Blockda = 0 Then
        Call oSketchBlockDef.CopyTo(targetdoc, True)
        Me.LB_LastAction.Caption = "Last Action : " & "Block inserted = " & CmB_Blocks.Text
    Else
        Me.LB_LastAction.Caption = "Last Action : " & "Existing Block ignored"
    End If
targetdoc.BrowserPanes.ActivePane.TopNode.BrowserNodes.Item(1).Expanded = True
    
    If CB_Multi.Value = 0 Then
        Me.Hide
    End If
If Me.Visible = False Then Block_Template.Close True

End Sub

0 Likes
Accepted solutions (1)
1,699 Views
2 Replies
Replies (2)
Message 2 of 3

Vladimir.Ananyev
Alumni
Alumni
Accepted solution

You may insert a sketch block to any position on the sketch using API (here x = 10 cm, y = 20 cm):

Dim oPosition As Point2d
Set oPosition = ThisApplication.TransientGeometry.CreatePoint2d(10, 20)
' Insert the sketch block definition
Call oSketch.SketchBlocks.AddByDefinition(oSketchBlockDef, oPosition)

 

It is also possible to invoke the Inventor command that allows you interactively insert the preselected block to the edited planar sketch:

' Get the command manager.
Dim oCmdMgr As CommandManager
Set oCmdMgr = ThisApplication.CommandManager
' Get control definition for the place the sketch block
Dim oControlDef As ControlDefinition
Set oControlDef = oCmdMgr.ControlDefinitions.Item("SketchRigidSetPlaceInstanceCmd")
' Execute the command.
Call oControlDef.Execute

cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 3

frank_schalla
Advocate
Advocate

Ok Thanks for that.

I found the solution a few days ago but was to busy to post it.

Attached you find the solution and the full workflow in a video sample