How to create a new assembly from ZeroDoc environment

How to create a new assembly from ZeroDoc environment

JBerns
Advisor Advisor
189 Views
5 Replies
Message 1 of 6

How to create a new assembly from ZeroDoc environment

JBerns
Advisor
Advisor

 

Community,

 

I am trying to create a new assembly from the ZeroDoc environment by means of an add-in.

 

With the help of @Curtis_Waguespack , @JelteDeJong , @chandra , @ryan.rittenhouse , and others on the forum, I have a working add-in that adds the ribbon tab, panel, and button to the ZeroDoc ribbon.

JBerns_0-1753891341760.png

 

The challenge is getting the button to create the new assembly. I trust this is possible.

 

I have searched many examples for code, but I have not been able to get the pieces of code to work.

Below is a portion of the code. The full VB file for the button is attached.

    'This is the code that does the real work when your command is executed.
    Sub Run_CommandCode()

        Debugger.Break()
        MsgBox("This command creates a new assembly from the ZeroDoc environment.")

        ' Get the Inventor Application object
        Dim oApp As Inventor.Application
        oApp = ThisApplication ' If running within Inventor (VBA macro)

        Dim invApp As Inventor.Application
        Try
           invApp = GetObject(, "Inventor.Application")
           '            invApp = ThisApplication ' If running within Inventor (VBA macro)
        Catch ex As Exception
           invApp = CreateObject("Inventor.Application")
           invApp.Visible = True
        End Try

        If invApp Is Nothing Then
           System.Windows.Forms.MessageBox.Show("Inventor is not running or could not be launched.", CStr(vbCritical))
           Exit Sub
        End If

        ' Define the document type as an Assembly Document
        Dim oDocType As Inventor.DocumentTypeEnum
        oDocType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject

        ' Get the default template file for an assembly
        ' This will use the default template set in Inventor's options.
        Dim sTemplateFile As String
        sTemplateFile = invApp.FileManager.GetTemplateFile(oDocType)

        ' Create a new assembly document
        Dim oAsmDoc As Inventor.AssemblyDocument
        oAsmDoc = invApp.Documents.Add(oDocType, sTemplateFile, True) ' True makes it visible and active

    End Sub

 

I have tried code such as the following to get the Inventor Application:

'Public g_Inventor As Inventor.Application = GetObject(, "Inventor.Application")
'Public Property g_Inventor As Inventor.Application
'Public g_Inventor As Inventor.Application = 'System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")

However, none of these lines get the Inventor Application.

 

I tried to understand the button examples on these posts, but I can't convert C# to VB code.

Getting inventor application object in addins 

Setting up api in visual basic using c 

 

I would welcome your suggestions to get the "new assembly" button code working.

 

Once this button is working, I plan to expand to a Windows User Form from which the user could select the assembly type to create. The selection will determine which assembly template to use when creating the new assembly.

 

Thank you for your time and attention. I look forward to your replies.

 

 

Regards,
Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Accepted solutions (1)
190 Views
5 Replies
Replies (5)
Message 2 of 6

JBerns
Advisor
Advisor
Accepted solution

Ironic - I find the solution after I post to the forum. 🙄

 

I discovered the StandardAddInServer.vb file gets the Inventor application and stores it globally:

Public Module Globals

    ' Inventor application object.
    Public g_inventorApplication As Inventor.Application

    'Unique ID for this add-in.  
    Public Const g_simpleAddInClientID As String = "d49b875a-c5e2-46af-8a59-a071d61d5b72"
    Public Const g_addInClientID As String = "{" & g_simpleAddInClientID & "}"

 

I just needed to reference the module name in the call to the object:

        If Globals.g_inventorApplication Is Nothing Then
            System.Windows.Forms.MessageBox.Show("Inventor is not running or could not be launched.", CStr(vbCritical))
            Exit Sub
        End If

        ' Define the document type as an Assembly Document
        Dim oDocType As Inventor.DocumentTypeEnum
        oDocType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject

        ' Get the default template file for an assembly
        ' This will use the default template set in Inventor's options.
        Dim sTemplateFile As String
        sTemplateFile = Globals.g_inventorApplication.FileManager.GetTemplateFile(oDocType)

        ' Create a new assembly document
        Dim oAsmDoc As Inventor.AssemblyDocument
        oAsmDoc = Globals.g_inventorApplication.Documents.Add(oDocType, sTemplateFile, True) ' True makes it visible and active

 

Is this best practice?

 

The button is working to create a new assembly from the ZeroDoc environment. Yeah!

 

I hope this can help others developing add-ins.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
Message 3 of 6

Curtis_Waguespack
Consultant
Consultant

Hi @JBerns , glad you got it figured out. And yeah, I think as yo u have it will work fine, but you could do it like this as well if it's preferred to use what you had in your original code.

 

Curtis_Waguespack_2-1753917002439.png

 

 

EESignature

Message 4 of 6

JBerns
Advisor
Advisor

Thanks, Curtis. That is easier to read.

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 5 of 6

chris
Advisor
Advisor

What is meant by "zero doc environment"?

0 Likes
Message 6 of 6

JBerns
Advisor
Advisor

All Inventor documents (part, assembly, etc.) are closed. Optionally, Home screen could be open.

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes