Insert Component VB.NET

Insert Component VB.NET

Nejc.Leban
Advocate Advocate
192 Views
3 Replies
Message 1 of 4

Insert Component VB.NET

Nejc.Leban
Advocate
Advocate

Hello!

 

I'm creating an addin for Inventor and I'm currently stuck at placing/inserting a component into my assembly.

I quickly figured out how to add a component in iLogic with:

Dim componentA = Components.Add("a:1", "a.ipt", position := Nothing, grounded := False, visible := True, appearance := Nothing)

I would like to convert this into vb.net for my addin. I found some documentation about IManagedComponents, but can't figure out how to initialize the managedComponents. I tried doing it like this, but it's not working:

 

Imports Autodesk.iLogic.Interfaces
Imports Inventor

Module BuildConveyor
    Dim managedComps As IManagedComponents
    Public Sub PlaceComponent(ByVal m_inventorApplication As Inventor.Application)
        Try
            Dim oOcc = managedComps.Add("", "S1325-03-000-00.iam", , , , )
        Catch ex As Exception
            MsgBox($"Error placing component: {ex.Message}")
        End Try
    End Sub
End Module

I get the error:
Error placing component: Object reference not set to an instance of an object.

I'm assuming something's missing after the Dim managedComps As IManagedComponents, maybe something like managedComps = m_inventorApplication.ActiveDocument.Component

I don't know.
If anyone has any idea how to make it work, I'd greatly appreciate it.
Thanks.

0 Likes
Accepted solutions (1)
193 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Nejc.Leban.  The objects and methods mentioned in your original post are unique to iLogic, which is another Inventor add-in.  You would most likely need to use the Inventor API way of doing those types of tasks instead, so that your add-in does not become dependent on that other add-in.  The Inventor API way starts with getting the  AssemblyDocument from the Application.ActiveDocument property.  Using its ComponentDefinition property to get the AssemblyComponentDefinition object.  Using its Occurrences property to get the ComponentOccurrences collection object.  Then using one of its many Add... methods to add an occurrence into the assembly.  Inventor API methods usually require a bit more code, when compared to similar uniquely iLogic methods, because Inventor API methods usually ask for the actual objects involved, instead of just the names of the objects involved.  This usually requires a bit more code ahead of time for you to acquire those objects beforehand, so you can supply them to the method.

 

The alternative, using uniquely iLogic resources may still be possible, but the iLogic UI usually does not even load into Inventor until after the user has either created or opened the first Inventor document after starting a new session of Inventor, while many other add-ins have already fully loaded all their resources before that point.  Accessing uniquely iLogic resources from outside of iLogic rules usually starts by getting the iLogic ApplicationAddIn object, then using its Automation property to get the 'Object' representing its API entry way, which is comparable to the IiLogicAutomation Interface.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

lauri_barnhart
Autodesk
Autodesk

Hi @Nejc.Leban, thank you for your question.

 

Did the information provided by @WCrihfield help?

 

If yes, please mark the reply as the accepted solution.

 

This helps other community user find a solution to similar problems.

 

In case you still need help, please provide an update here so we can help you best moving forward.

 

Thanks!


Lauri | Community Manager
0 Likes
Message 4 of 4

JelteDeJong
Mentor
Mentor

Here is example code:

Module BuildConveyor
    Public Sub PlaceComponentInActiveDocument(ByVal m_inventorApplication As Inventor.Application, fileName As String)

        If (Not System.IO.File.Exists(fileName)) Then
            Throw New Exception()
        End If

        Dim doc As AssemblyDocument = m_inventorApplication.ActiveDocument
        Dim def As AssemblyComponentDefinition = doc.ComponentDefinition
        Dim occs As ComponentOccurrences = def.Occurrences

        Dim orign = m_inventorApplication.TransientGeometry.CreateMatrix()

        occs.Add(fileName, orign)
    End Sub
End Module

 

Jelte de Jong
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.

EESignature


Blog: hjalte.nl - github.com

0 Likes