Use ApprenticeServer API from an Inventor Addin

By Augusto Goncalves

We can create an ActiveX executable, which references the ApprenticeServer API, and which also exposes a suitable API of its own. We can then call into our ActiveX executable from an Inventor AddIn dll to request updates to any file references.

So – as a minimum, the API exposed by our ActiveX executable should allow us to open an Inventor document, update any file references, then save the updated document.

Here are the steps we need to perform in our server:-

(1) Create an Apprentice Server object

(2) Open an Inventor document via the Apprentice Server API

(3) Update relevant file references

(4) Save the modified document

(5) Destroy the Apprentice Server object.

The code sample sample below does exactly this (with the restriction that only Assembly documents can be updated). When an instance of the ActiveX component is initialized, an ApprenticeServerComponent is automatically created.

Then we call the ‘OpenAssembly()’ method passing in the path to an Assembly file.

Next we call the ‘Update()’ method – passing in a path to a referenced file that is to be updated, along with the new path.

Once all paths have been updated, we call the ‘Save()’ method to save our updated file references in the Assembly document.

The ApprenticeServerComponent object is automatically destroyed as our ActiveX server terminates.

To use our Server we firstly need to add a reference to it from our Inventor AddIn project (I’ve called it ‘FileRefUpdater’), then we just add code to call the API we have exposed.

‘ The path to our Assembly document
Dim
sAsmDocPathName As String = "………."

 

Dim oFileRefUpdater As Object

oFileRefUpdater = CreateObject("FileRefUpdater.clsFileRefUpdater") ‘ create our Server 

 

‘ and open the Assembly document

oFileRefUpdater.OpenAssembly(sAsmDocPathName)

 

Dim sOldPath As String

sOldPath = "……" ‘ the path to a file that is being moved

Dim sNewPath As String

sNewPath = "….." ‘ the updated location.

 

System.IO.File.Move(sOldPath, sNewPath) ‘ move the document  
‘ update the references in our Assembly  

oFileRefUpdater.Update(sOldPath, sNewPath)

oFileRefUpdater.Save() ‘ and save  

oFileRefUpdater = Nothing ‘ all done!


Comments

5 responses to “Use ApprenticeServer API from an Inventor Addin”

  1. Hi Augusto, I don’t understand how to create Active-X and link it with then ApprenticeServer. Can you explain this moment step-by-step?

  2. Maxim,
    Please check this training lesson for a more complete explanation: http://modthemachine.typepad.com/my_weblog/2013/02/inventor-api-training-lesson-1.html
    And below is a quick sample (in ActiveX/VBA)
    Public Sub ApprenticeOpen()
    ‘ Declare a variable for Apprentice. Notice that this uses the “New”
    ‘ keyword which will cause a new instance of Apprentice to be created.
    Dim invApprentice As New ApprenticeServerComponent
    ‘ Open a document using Apprentice.
    Dim invDoc As ApprenticeServerDocument
    Set invDoc = invApprentice.Open(“C:\Temp\Part1.ipt”)
    MsgBox “Opened: ” & invDoc.DisplayName
    ‘ Close the document and release all references.
    Set invDoc = Nothing
    invApprentice.Close
    Set in

  3. I dont understand this blogpost.
    You get a complete explanation of some generic sample code to update an assembly, and then the difficult stuff, like how to make that executable and link to it is as Maxim is asking is completely ignored like its common knowlegde.
    Anyone who is googling about using apprenticeserver in Inventor API can understand the sample code. We want to “Use ApprenticeServer API from an Inventor Addin”, so how do we do that? VB Example?

  4. Sajith Subramanian Avatar
    Sajith Subramanian

    Hi Arnold,
    If you are looking to use Apprentice directly within an Inventor process, it is not supported. Do take a look at the below link for more details / workarounds.
    http://adndevblog.typepad.com/manufacturing/2016/03/using-apprentice-from-vba-ilogic-or-an-add-in.html
    Regards,
    Sajith

  5. Hi Sajith,
    I have made an addin that exports thumbnail after save document event.
    Problem is that the thumbnail is being created in the background for few seconds during this event.
    So I need a separate thread that waits 5 seconds then run apprentice, export thumbnail, and close, without making user wait that time.
    Reading the title and first sentences of this blog suggest this is a solution to my problem
    “Use ApprenticeServer API from an Inventor Addin
    We can create an ActiveX executable, which references the ApprenticeServer API, and which also exposes a suitable API of its own. We can then call into our ActiveX executable from an Inventor AddIn dll to request updates to any file references.”
    But there is no further explanation how to do that.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading