cancel
Showing results for 
Search instead for 
Did you mean: 

Using user defined functions in journaling

Valued Contributor
Valued Contributor

Hello,

 

Within our company we do a lot with journals, instead of using the GUI. Our projects are one offs, so we can not really standardize the complete process. Instead we use blocks of functionality which the user pieces together to form the complete process.

 

I would like to make user defined functions and store these in a .dll which loads at start up of NX. The user can then call these user defined functions in his/her journals.

 

I managed to load a .dll at runtime during the execution of a journal. Looping through all the types works, but trying to access a specific type trows an error "Class system.type cannot be indexed because it has not default property"

Option Strict Off 'Late binding is not allowed with strict on
Imports System
Imports NXOpen
Imports System.Windows.Forms
Imports NXOpenUI
Imports System.Reflection
'Imports GustoMSCClass

Module NXJournal
Sub Main (ByVal args() As String) 

Dim theSession As Session = Session.GetSession()
Dim theLW As NXOpen.ListingWindow = theSession.ListingWindow()
theLW.Open()
        Dim i As Integer
        For i = 0 To 100
            theLW.WriteLine(TimeOfDay.ToString)
        Next
theLW.WriteLine(TimeOfDay.ToString.Substring(12,8))


				'Dim MyGustoMSCFunction As New GustoMSCClass.Class1
				'MyGustoMSCFunction.DisplayMessgeBox("Function call of TestClass during startup")

        'The following code dynamically loads the library
        Dim MyDLL = System.Reflection.Assembly.LoadFile("H:\NX100_USER_DIR\Startup\test\GustoMSCClass.dll")

        For Each element As System.Type In MyDLL.GetExportedTypes()
        		theLW.WriteFullline(element.FullName)
            Dim Myc = System.Activator.CreateInstance(element)
            Myc.DisplayMessageBox(element.tostring)
        Next
        
        theLW.writefullline(mydll.fullname)
        
        Dim MyElement As System.Type = MyDLL.GetType("GustoMSCClass.Class1")
        Dim InstanceOfClass1 = System.Activator.CreateInstance(MyElement)
        InstanceOfClass1.DisplayMessageBox("working with late binding")      

End Sub
End Module

 

Running the same code as a .dll does however work...

 

Could somebody help me out, or point me in the right direction?

 

Thanks in advance

Frederik

 

 

 

 

The GustoMSCClass:

 

Option Strict On
Option Explicit On
Imports System.Windows.forms

Public Class Class1

    Public Sub DisplayMessageBox(ByRef Name As String)
        MessageBox.Show(Name)
    End Sub

End Class

 

3 REPLIES

Re: Using user defined functions in journaling

Valued Contributor
Valued Contributor

Hello,

 

Update from my side:

When I change

Dim MyElement As System.Type = MyDLL.GetType("GustoMSCClass.Class1")

to

Dim MyElement As System.Type = MyDLL.GetType()

The journal version works. However, when running the compiled version of the same code I get the error:

"System.MissingMethodException: No parameterless constructor defined for this object"

 

Regards,

Frederik

Re: Using user defined functions in journaling

Siemens Phenom Siemens Phenom
Siemens Phenom

The set of DLLs that you can reference from a journal is quite limited. It includes NXOpen.dll, NXOpen.UF.dll, NXOpenUI.dll, Snap.dll (if you have the authoring license), System.Windows.Forms, System.Windows.Drawing, and a few others. You can't call functions that reside in a DLL you wrote yourself, even if you do it by reflection.

山田
yamada

Re: Using user defined functions in journaling

Valued Contributor
Valued Contributor

Hello Yamada,

 

Thank  you for your answer. I'll just have to add my user defined functions at the end of each journal.

 

One thing has me wondering though. When I run the journal below, it actually works. So it seems that the dll gets loaded by reflectioin at some point since the following line of code makes the messagebox show on screen.

Myc.DisplayMessageBox(element.tostring)

 

regards,

Frederik

 

My journal:

Option Strict Off 'Late binding is not allowed with strict on
Imports System
Imports NXOpen

Module NXJournal
    Sub Main(ByVal args() As String)

        Dim theSession As Session = Session.GetSession()
        Dim theLW As NXOpen.ListingWindow = theSession.ListingWindow()
        theLW.Open()

        'The following code dynamically loads the library
        Dim MyDLL = System.Reflection.Assembly.LoadFile("H:\NX100_USER_DIR\Startup\test\GustoMSCClass.dll")

        For Each element As System.Type In MyDLL.GetExportedTypes()
            theLW.WriteFullline(element.FullName)
            Dim Myc = System.Activator.CreateInstance(element)
            Myc.DisplayMessageBox(element.tostring)
        Next

        theLW.writefullline(mydll.fullname)

    End Sub
End Module

Source for GustoMSCClass.dll

Option Strict On
Option Explicit On
Imports System.Windows.forms

Public Class Class1

    Public Sub New()
        MessageBox.Show("New instance of Class1 has been created")
    End Sub


    Public Sub DisplayMessageBox(ByRef Name As String)
        MessageBox.Show(Name)
    End Sub

End Class