Shared Function

Shared Function

gabriele.tittonel
Contributor Contributor
849 Views
9 Replies
Message 1 of 10

Shared Function

gabriele.tittonel
Contributor
Contributor

Hello everyone!

 

I have few functions that are used in several external rules. At the moment, I've copied the function code on each rule, therefore I have the structure:

Sub Main()
...
End Sub

Function MyFunc()
...
End Function

 

Is there a way to store these functions in one file and use them without having the code on the rule itself?

Here an example of a function that I use in different rules:

Function ParameterCreate(Name As String, Optional Value As Object = "", Optional Unit As String = "ul")

'	This rule works on every kind of Inventor files: Part, Assembly and Drawing
'	If a parameter doens't exist, then it will be created.
'	If a parameter exists, then it won't be changed.
'
'	How to use:
'
'		ParameterCreate("txtPara", "Hello World!")
'		ParameterCreate("mmPara", 45, "mm")
'		ParameterCreate("booPara", True)
'		ParameterCreate("emptyPara")
'		ParameterCreate("ulPara", 15)

	Try 	' Check if parameter exists.
		
		Parameter(Name) = Parameter(Name)
		
	Catch 	' Parameter not found, so create it.
		
		If TypeName(Value) = "Boolean" 	Then Unit = "BOOLEAN"
		If TypeName(Value) = "String" 	Then Unit = "TEXT" 
			
		Dim oDoc As Document = ThisApplication.ActiveDocument
'		Dim oDoc As Document = ThisDoc.Document
		Dim oParam As UserParameters
		
		If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
			oParam = oDoc.Parameters.UserParameters							' Drawing.
		Else
			oParam = oDoc.ComponentDefinition.Parameters.UserParameters		' Part/Assembly.
		End If

		Select Case Unit 
			Case "BOOLEAN", "TEXT" 	: oParam.AddByValue(Name, Value, Unit)
			Case Else 				: oParam.AddByExpression(Name, Value, Unit)
		End Select
		
	End Try

End Function



0 Likes
Accepted solutions (1)
850 Views
9 Replies
Replies (9)
Message 2 of 10

davidt162003
Advocate
Advocate

The best method for this would be to create an external exe using something like visual studio or visual studio code.

the syntax is basically the same once you get the inventor App object, which can be difficult if you dont use the right framework.

    Private Sub _TryAndOpenInventor()
        Try
            _App = Marshal.GetActiveObject(InventorAppName)
            NotifyDriverReport("Inventor Already Open")
        Catch
            Try
                Dim pInventorAppType = Type.GetTypeFromProgID(InventorAppName)
                _App = Activator.CreateInstance(pInventorAppType)
                NotifyDriverReport("Inventor opened new")
                _InventorWasOpenedFresh = True
            Catch
            End Try
        End Try

        If _App Is Nothing Then
            NotifyDriverReport("Open Inventor Failed")
            RaiseEvent OpenAppFailed()
        Else
            RaiseEvent OpenAppSuceeded()
            NotifyDriverReport("Open Inventor Succeeded")
        End If
    End Sub
HII
0 Likes
Message 3 of 10

FINET_Laurent
Advisor
Advisor

Hi @gabriele.tittonel,

 

I am not aware of any easy way out. I've faced the same issue few years ago.

My point of view is that it is a bad idea to copy/paste 50 times since it takes 1 sec to do so, but you might have to update the same function 50 times in 50 different rules.. Thus praying you don't forget one... 

 

I would recommand using a custom add-in instead. You could then create function classes that you could reach from anywhere. 

 

Here is some interesting links :

My First Inventor Plug-in Overview (autodesk.com)

Inventor 2022 Help | Creating an Add-In | Autodesk

 

Kind regards,

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 4 of 10

WCrihfield
Mentor
Mentor

Hi @gabriele.tittonel.  Have you read through this online help page yet.  It mentions ways to reference other iLogic rules as a source for using the code resources in them to add to the functionality of your current iLogic rule.  If you do not have a history of programming using vb.net though, it can be complicated to adapt to writing the code necessary for those special types of external rules that you can reference that way though, because they need to be laid out differently, and much more strictly than your other normal external iLogic rules.  The iLogic add-in and its rule editor dialog do a lot more for you behind the scenes, to help make things simpler than most folks realize, which can actually be awkward for those who do have a history of writing code in vb.net.  Those external rule that you intent to use that way, must have their 'Straight VB Code' option turned on, which changes a lot of things.  Then they must specify the Class (or similar block), before you can add a Sub Main or other Methods or Properties inside that Class, which you normally do not need to do in iLogic.  You will also loose a lot of the externally referenced sources that would normally be included automatically in the background of other iLogic rules, so you will likely need to include several lines of code within the 'Header' them which use 'AddReference' and 'Imports' statements.  It can be a lot to learn and may take a lot of trial & error testing, if you are not experienced with how vb.net coding outside of Visual Studio works.  Even though I have Visual Studio Community 2019 installed, I rarely ever really need to use if for anything.

https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-32B66838-22E4-4A0A-B5BB-862350C76B36 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 10

cidhelp
Advocate
Advocate

Hi @gabriele.tittonel ,

 

you can share functions with iLogic.

First you have to create an external rule with the option "Straight VB-Code" on. In this rule, create a class and your functions. Keep in mind, that staight VB-Code needs arguments for the Inventor Application or Document-objects.

See my sample iLogicVB-File (stored as MyVBFunctions.iLogicVB in an external rule directory):

Class MyVBFunctions

	Public Function GetUserName(oApp As Inventor.Application) As String
		Return oApp.UserName
	End Function
	
	Public Function WriteUserProperty(oDoc As Inventor.Document, PropName As String, PropVal As String) As Inventor.Property
		Dim oProp As Inventor.Property
		If UserPropExist(oDoc, PropName) Then
			oProp = oDoc.PropertySets("Inventor User Defined Properties").Item(PropName)
			oProp.Value = PropVal
		Else
			oProp = oDoc.PropertySets("Inventor User Defined Properties").Add(PropVal,PropName)
		End If
		Return oProp
	End Function
	
	Private Function UserPropExist(oDoc As Inventor.Document, PropName As String) As Boolean
		For Each oProp As Inventor.Property In oDoc.PropertySets("Inventor User Defined Properties")
			If oProp.Name.ToUpper = PropName.ToUpper Then
				Return True
			End If
		Next
		Return False
	End Function

End Class

To use these functions, create rules and add the iLogicVB file at the header using AddVBFile. Now you can create an object of the class and use the public functions.

See my sample rule header:

AddVbFile "MyVBFunctions.iLogicVb"

and the rule:

Dim MyFunc As New MyVBFunctions
sUser = MyFunc.GetUserName(ThisApplication)
oProp = MyFunc.WriteUserProperty(ThisDoc.Document, "Inv_UserName", sUser)

 

Message 6 of 10

gabriele.tittonel
Contributor
Contributor

Guys, thanks for your help!

 

I don't have experience writing in vb.net and I was indeed hoping for an "easy solution", which I didn't found anywhere online.

cidhelp's solution looks something I can use, finger crossed! 😀

@cidhelp : can I class my function like it is or do I need to modify it in some way?
0 Likes
Message 7 of 10

cidhelp
Advocate
Advocate
Accepted solution

Hi @gabriele.tittonel ,

 

some modifications are necessary. The rule including the function can look like this (see oDoc as first parameter):

Class MyVBFunctions

	Public Function CreateParameter(oDoc As Inventor.Document, ParameterName As String, Optional Value As Object = "", Optional Unit As String = "ul") As Inventor.Parameter
		Dim oParams As Inventor.UserParameters
		If oDoc.DocumentType = Inventor.DocumentTypeEnum.kDrawingDocumentObject Then
			oParams = oDoc.Parameters.UserParameters
		Else
			oParams = oDoc.ComponentDefinition.Parameters.UserParameters
		End If
		For Each oParam As Inventor.Parameter In oParams
			If oParam.Name = ParameterName Then Return Nothing
		Next

		If TypeName(Value) = "Boolean" 	Then Unit = "BOOLEAN"
		If TypeName(Value) = "String" 	Then Unit = "TEXT" 
		
		Select Case Unit 
			Case "BOOLEAN", "TEXT" 	: Return oParams.AddByValue(ParameterName, Value, Unit)
			Case Else 				: Return oParams.AddByExpression(ParameterName, Value, Unit)
		End Select

	End Function
End Class

The rule using the function can look like this:

AddVbFile "MyVBFunctions.iLogicVb"
Dim MyFunc As New MyVBFunctions
Dim oParam1 As Parameter
oParam1 = MyFunc.CreateParameter(ThisDoc.Document, "TestParam", 10, "mm")
If oParam1 is Nothing Then
	Logger.Info("TestParam always exists")
Else
	Logger.Info("TestParam created")
End If

 

 

Message 8 of 10

gabriele.tittonel
Contributor
Contributor
It works, thanks!

I guess we need to specify the "oDoc" otherwise the function doesn't know on which document the parameter must be created, correct?

Is it something I should put on every public function?
0 Likes
Message 9 of 10

gabriele.tittonel
Contributor
Contributor

Hi everyone!

 

Back again on this topic: I was trying to create a shared function that contains the "GoExcel" function, but it doesn't work... ☹️

 

I get the error: 'GoExcel' is not declared. It may be inaccessible due its protection level.

 

How can I solve it?

0 Likes
Message 10 of 10

WCrihfield
Mentor
Mentor

Hi @gabriele.tittonel.  The 'GoExcel' term is known as an iLogic 'Rule Object', and is mentioned at this following online help web page:

https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=4ccb78b0-c35c-4d38-943a-854117da6ced 

The 'GoExcel' term represents an instance of the IGoExcel Interface (Link in next line).

https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=af87c7b4-cf59-8cce-c207-cbd8a3b6632e 

Those are similar to local variables which have already been declared, had their Type set, and had their Value set, behind the scenes in every iLogic rule, when we are using the iLogic Rule Editor dialog.  Those terms will not be recognized outside of normal/simple iLogic rules, so, if you want to use those iLogic tools elsewhere (such as within a custom Class block of code), then you must 'pass' that reference into your Class, one way or another.  You can have a Public Property for that term within your Class block of code, then set its value from the iLogic rule that creates an instance of that Class, by setting it as the value of that Public Property of that Class.  Or, you can include asking for this reference in one of the 'Methods' of the Class, then pass it in that way, and set it at the value of a Private internal variable of the Class.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes