Is it possible to use "AddVBFile" in Straight VB Code File?

Is it possible to use "AddVBFile" in Straight VB Code File?

ColdWatermelonJuice
Explorer Explorer
1,210 Views
7 Replies
Message 1 of 8

Is it possible to use "AddVBFile" in Straight VB Code File?

ColdWatermelonJuice
Explorer
Explorer

Hi all,

 

I am trying to call a sub, which is declared in a class from a separate VB file, and this sub also calls another sub from a module declared in another separate VB file. 

 

To summarise, I have a total of three VB files, and all of them are stored under the iLogic external rule directory.

 

The code is shown below.

 

File: Code.vb

AddVbFile "ExternalClass.vb"

Sub Main()

    ClassA.MSGC()

End Sub

 

File: ExternalClass.vb

AddVbFile "ExternalModule.VB"

Public Class ClassA

    Public Shared Sub MSGC()
        System.Windows.Forms.MessageBox.Show("Call from ClassA")
        ModuleB.MSGM()
    End Sub

End Class

 

File: ExternalModule.vb

Public Module ModuleB

    Public Sub MSGM()
        System.Windows.Forms.MessageBox.Show("Call from ModuleB")
    End Sub

End Module

 

After I ran the Code.vb file. I got the following error message.

Rule Compile Errors in Code, in Part1

Error on Line 1 : Declaration expected.
Error on Line 7 : 'ModuleB' is not declared. It may be inaccessible due to its protection level.

 

I think Line 1 and Line 7 refer to lines in ExternalClass.vb.

My induction is that straight vb code does not support AddVBFile as it's for ilogic only.

My programming knowledge is very limited, and I do not know how to set up a code that is equivalent to AddVBFile.

 

Much appreciated for your help.

0 Likes
Accepted solutions (1)
1,211 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @ColdWatermelonJuice.  Those AddVbFile & AddVbRule tools will only work properly from an iLogic rule that is not set to 'Straight VB Code'.  So, you can not chain several of them together end to end like that.  You can have one iLogic rule that uses multiple of those tools though.  And the iLogic rule using one (or more) of those tools may not need to be set to Straight VB Code to use those reference tools.  When more than one layer of references may be needed, you may need to look into creating your own external DLL file that you can reference from an iLogic rule with an AddReference and/or Imports tool, within the 'Header' of the rule.  To do that you will likely have to have something like Microsoft Visual Studio installed and a decent understanding of vb.net.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

Michael.Navara
Advisor
Advisor
Accepted solution

I agree with @WCrihfield If you need this complex references, create your own DLL file with all of them.

But because AddVbFile directive is solved at compile time, you can combine this two files from "main" rule.

 

Code.iLogicVb

'AddVbFile "C:\Full\Path\To\ModuleB.vb"
'AddVbFile "C:\Full\Path\To\ClassA.vb"
AddVbFile "IC_11884562\ClassA.vb"
AddVbFile "IC_11884562\ModuleB.vb"

Sub Main()
    ClassA.MSGC()
End Sub

ClassA.vb

Public Class ClassA
    Public Shared Sub MSGC()
        System.Windows.Forms.MessageBox.Show("Call from ClassA")
        ModuleB.MSGM()
    End Sub
End Class

ModuleB.vb

Public Module ModuleB
    Public Sub MSGM()
        System.Windows.Forms.MessageBox.Show("Call from ModuleB")
    End Sub
End Module

 

Screenshot from iLogic external rules settings

MichaelNavara_0-1681195811815.png

 

 

Message 4 of 8

ColdWatermelonJuice
Explorer
Explorer

Hi @WCrihfield, Thank you very much for your reply. I have read a few of your posts about API and iLogic, and they have been extremely helpful as I am just starting to learn programming. I have discovered that there are many things that can be accomplished using APIs. I am trying to set up things in a "universal" way so that other people can easily add features on top of my code in the future (at least, I hope so😅). 

0 Likes
Message 5 of 8

ColdWatermelonJuice
Explorer
Explorer

Hi @Michael.Navara, Thank you for your guidance. I can confirm that the code is now working as expected.😄

0 Likes
Message 6 of 8

Marco_PeronXTBPH
Participant
Participant

Hi everyone, I'm compiling a program with a structure very similar to the one indicated in this topic where I have several classes called within the main rule and some modules that contain "universal" functions that must be called both in the main rule and in the classes.
My main problem is that from the modules I don't know how to recall and use the ilogic logger.
In classes I don't have this problem as I pass the logger itself through the class constructor, but for modules I haven't found a valid method.
Any ideas on how this could be done?

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor

Hi @Marco_PeronXTBPH.  When I want to use the iLogic rule logger in a referenced Class in an external file that I am referencing into an iLogic rule, I just declare the 'Logger' variable again within that Class.  Then you do not even need to set a value to that variable, because of how it works.  The following lines of code are an example of how to access and use those types of things, as long as your references are set up properly.

Private iLogicAuto As Autodesk.iLogic.Interfaces.IiLogicAutomation
Private Logger As Autodesk.iLogic.Interfaces.IRuleLogger

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 8

Marco_PeronXTBPH
Participant
Participant

Hi @WCrihfield 

 

Main Rule

AddReference "System.Data"
AddReference "System.Xml"
AddReference "Microsoft.Office.Interop.Excel.dll"
AddVbFile "...\mLog.vb"
AddVbFile "...\cMagnum.vb"

Class ThisRule
   '...
   Private oMagnum As cMagnum
   '...
   Sub Main

      StartLog
      oMagnum = New cMagnum(Logger, arrayRiga, transcodifica)
      '...
      '...

   End Sub
End Class

 

mLog.vb

Imports Autodesk.iLogic.Core
Imports Autodesk.iLogic.Interfaces
Imports Autodesk.Inventor
Imports System

Public Module mLog

    Private iLogicAuto As Autodesk.iLogic.Interfaces.IiLogicAutomation
    Private Logger As Autodesk.iLogic.Interfaces.IRuleLogger
    'Public logCtrl As LogControl
    'Public Log As IRuleLogger

    Public Sub StartLog()'(startMarker As String)
		Logger.Debug("TEST MODULO")
        'Log.Debug(startMarker)
		'Log.Trace("**Class cLog - StartLog")
        'Log.Info(" Log regola lanciata alle " & DateTime.Now)
    End Sub

    Public Sub EndLog(startMarker As String, nomeRegola As String, Optional percorsoLogCompleto As String = "...\Log", _
                            Optional percorsoLogPuntuale As String = "...\Log", Optional fullLog As Boolean = True, _
                            Optional puntualLog As Boolean = True)
       '...
       '...
       logCtrl.SaveLogAs(tempfile)
       '...
       '...
   End Sub
End Module

 

cMagnum.vb

Imports System.IO
Imports System.Data
Imports System.Xml

Public Class cMagnum
   Dim Log As IRuleLogger
   '...
   '...

	Public Sub New(log As IRuleLogger, datiConfigurazione(,) As Object,configPath As String )
		Me.Log = log
                '...
                '...
	End Sub

    '...
    '...

	Private ReadOnly Property calcolo_LT As Integer
		Get
			value = L - 2 * AMT
			Log.Trace("Valore di LT: " & value)
			Return value
		End Get
	End Property

    '...
    '...

End Class

 

The logger works perfectly on the class cMagnum, but not in the module mLog.

I get the error: "Object reference not set to an object instance."

 

This is only an example but the main concepet is that i need to create some "module" that can be recalled inside other classes as isn't possible to call a class inside another class as far as i know, and obviously i need to use the logger inside them.

 

I hope I have been as clear as possible.

0 Likes