How do I trigger a ivb macro in Inventor with c# ?

How do I trigger a ivb macro in Inventor with c# ?

mattias-bergson
Participant Participant
596 Views
3 Replies
Message 1 of 4

How do I trigger a ivb macro in Inventor with c# ?

mattias-bergson
Participant
Participant

I am currently working on a project which requires the job processor to open an idw file with inventor and trigger a couple macros. The .ivb ilogic file has multiple macros saved.

 

My problem is that I don't know how to trigger the ivb macros with inventor.

 

Currently I have this c# code:

 

 

public static void ExecuteMacros(string idwPath)
        {
            Inventor.Application invInstance = null;
            try
            {
                invInstance = LaunchInventor(true);

                Inventor.Document document = invInstance.Documents.Open(idwPath);


                if (document is Inventor.DrawingDocument)
                {
                   
                    // Execute Macros

                }



            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (invInstance != null)
                {
                    invInstance.Quit();
                }
            }

        }

 

 

How can I trigger the macros? The .ivb macros I want to trigger don't require any arguments.

 

0 Likes
597 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @mattias-bergson.  You can access Most (if not all) of the Inventor VBA stuff through Inventor's API.  I do not code in C#, but I have a couple examples of regular iLogic/vb.net code you can look at as guide.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

mattias-bergson
Participant
Participant

Hello

 

Thanks for the reply.

 

What would you do if you had to open the ivb and .idw file and trigger the two macros in VB.net? (for example "macro1" and "macro2"?)

 

I have looked at the .txt files you provided but I don't see any macro being triggered

0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor

Hi @mattias-bergson.  Those two text files were just exploratory type codes to get you familiar with where to find the things you will need to find.  Here is an example code more like what you asked for in your second response.  It tries to open an IDW type drawing file.  Then tries to open an IVB file for the VBA project.  Then it tries to find your two macros and run them.  You will need to know the names of the Modules that you put them into, and the name of the main Sub or Function within that Module, in order to be able to run them.  And since I wasn't sure whether your macros were Subs or Functions, I left more code in there than may be necessary.  You will need to edit the drawing file's path & name, the VBA project file's path & name, the module names, and maybe even the macro names in order to make this work for you.

'iLogic rule way to run a macro:
'oResult = iLogicVb.RunMacro(sProjectName, sModuleName, sMethodName, {oInputArg1, oInputArg2})

'Inventor API way to run a VBA macro:
'open an IDW drawing file:
Dim sDrawingFile As String = "C:\Temp\MyDrawing.idw"
Dim oDDoc As DrawingDocument = ThisApplication.Documents.Open(sDrawingFile, True) 'True = Visible
'open the VBA project, then find the object it represents
Dim sVBAProjectFile As String = "C:\Temp\MyVBAProject.ivb"
Dim oVBAProjects As InventorVBAProjects = ThisApplication.VBAProjects
oVBAProjects.Open(sVBAProjectFile)
Dim oMyProj As InventorVBAProject = Nothing
For Each oProj As InventorVBAProject In oVBAProjects
	If oProj.Name = "MyVBAProject" Then
		oMyProj = oProj
		Exit For
	End If
Next
'find the first Module, then the Sub or Function routine within it, by name (the macro)
Dim oModule1 As InventorVBAComponent = oMyProj.InventorVBAComponents.Item("Module1")
Dim oRoutine1 As InventorVBAMember = oModule1.InventorVBAMembers.Item("macro1")
Dim oFunctionReturnValue1 As Object = Nothing
If oRoutine1.MemberType = MemberTypeEnum.kFunctionMember Then
	oRoutine1.Execute(oFunctionReturnValue1)
Else
	oRoutine1.Execute 'run the macro
End If

Dim oModule2 As InventorVBAComponent = oMyProj.InventorVBAComponents.Item("Module2")
Dim oRoutine2 As InventorVBAMember = oModule2.InventorVBAMembers.Item("macro2")
Dim oFunctionReturnValue2 As Object = Nothing
If oRoutine2.MemberType = MemberTypeEnum.kFunctionMember Then
	oRoutine2.Execute(oFunctionReturnValue2)
Else
	oRoutine2.Execute
End If
'update, save, and close the drawing
oDDoc.Update2(True) 'True = accept errors & continue
'oDDoc.Save2(True) 'True = save dependents
'oDDoc.Close(True) 'True = skip save

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes