cancel
Showing results for 
Search instead for 
Did you mean: 

Menuscript modificaton for History Pallette

Pioneer
Pioneer

I am currently modifying all the FILE_OPEN functions to add some attributes when the button is pushed. I have everything done except if you open from the history pallette. 

 

How do I modifythe behavior for opening from the history pallette? 

 

Thanks in advance,

 

Ron

NX8.5

Windows 7 64-bit

15 REPLIES

Re: Menuscript modificaton for History Pallette

Siemens Phenom Siemens Phenom
Siemens Phenom

 

If you want to do this whenever a part is opened, no matter how that happens, then you should register a function with UF_add_callback_function() with a reason of "UF_open_part_reason".

 

Then you will just need to use a ufsta entry point for the code that registers the function, and put the .DLL in a valid "startup" folder so NX will see it at launch time.

 

One more thing:  Set the unload option to unload at termination - that is a must if you are registering a function like this.

 

Regards,

 

Steve

 

 

Re: Menuscript modificaton for History Pallette

Pioneer
Pioneer

Thanks for the reply Steve. 

 

I do not know how to "register a function". Can you elaborate? Or is there some documentation that I can reference?

 

Also, the code I'm using to add attributes when opening is a .vb journal. Does it need to be a .dll? If so, what do I need to to do to convert it?

 

Thanks,

Ron

Re: Menuscript modificaton for History Pallette

Siemens Phenom Siemens Phenom
Siemens Phenom

Ron,

 

I'm sorry that my note was not clear - let me try to add a little detail.  When you "register a function" in NX, you are specifying that you want a certain piece of code - your function - to run in response to certain things that happen in the software.

 

The code that gets registered does need to be in the form of a .DLL - .VB code (Journals) are automatically unloaded right after they execute, so while you can write the program in VB (or C, or ++, or C#), you must build it into a .DLL in order to use it this way.  To do that, you need the appropriate license, which would be dotnet_author if you are using VB, and on Windows you nee the Visual Studio from Microsoft.

 

Here is some additional information from the docs.  This appears in the Open C Reference Guide, in the "UF" chapter, under the entry for UF_add_callback_function():

 

Overview
Registers the callback to be called whenever the reason specified
occurs within NX.

Please note the following:

Once a routine is registered as a callback, the shared library it
resides in should not be unloaded until it is deregistered.

The callback function must call UF_initialize and UF_terminate, if
it makes any Open C function calls

The callback function receives three arguments:
argument 1 - The reason the callback has been called
argument 2 - A pointer to the part tag that caused the callback.
argument 3 - A user supplied pointer. Typically this will be a
structure pointer of data that the callback function
will need.

The callback will be called towards the end of the processing for that
particular reason. For example, the callback registered against
UF_create_part_reason will be called when a new part is created, after
almost all the process of creating this new part is completed.

The only exception to the above is UF_close_part_reason, for which
the callback will be called at the beginning of closing the part.

NOTE: For the new and open part reasons the callback is run at a time
when the newly created or opened part has not been set as the work part.
 
This is the list of reasons for which a callback can be registered:
 
Overview
These are the reasons against which call backs can be registered.

The registered callbacks will be called whenever the specific reason occurs
in interactive NX.

For all the reasons, except for UF_close_part_reason, the callbacks will be
called towards the end of that particular operation. For example, the
callbacks registered against UF_create_part_reason will be called when a
new part is created, after almost all the process of creating this new part
is completed.

An attempt to query the work or display part in a UF_save_part_reason_callback
may return unexpected results.

The callbacks registered against UF_close_part_reason will be called at the
beginning of part close.

The callbacks registered against UF_modified_part_reason will be called
when the part is modified for the first time after opening it or after a
save operation.

These callbacks give a handle to the user to do some extra processing, in
addition to the processing that NX does, during that particular operation.

In NX Manager mode, when a new part is being created it first loads the
template part (seed part), changes the template part to be the work part
and then renames the work part to the new part name given. So, the following callbacks are being called:
UF_modified_part_reason, UF_open_part_reason, UF_change_work_part_reason,
UF_rename_part_reason and UF_create_part_reason.

Data Members

UF_create_part_reason
Callback when a new part is created.

UF_open_part_reason
Callback when an existing part is opened.

UF_save_part_reason
Callback when a part is saved.

UF_save_as_part_reason
Callback when a part is saved-as.

UF_close_part_reason
Callback when a part is closed.

UF_modified_part_reason
Callback when a part is modified.

UF_rename_part_reason
Callback when a part is renamed.

UF_change_work_part_reason
Callback when there is a new work part.
The part returned is of the old work part.
Use UF_ASSEM_ask_work_part to get new work part.
 
Regards,
 
Steve
 

Re: Menuscript modificaton for History Pallette

Pioneer
Pioneer

Just when I thought I had it figured out. ha ha. I guess I'll be learning how to do somthing new yet again!

 

Will my 2010 Express edition of VB be able to do the trick?

 

Ron

Re: Menuscript modificaton for History Pallette

Siemens Phenom Siemens Phenom
Siemens Phenom

I think you should be able to build a .DLL for either NX8 or NX8.5 with that version, provided you have the dotnet_author license.

 

Here is a minimal program to demonstrate how this works.  You just need to run it once to make it register the callback, then everytime you open a part, the OnOpenPart() sub will run:

 

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module register_function_to_run_when_a_part_is_opened

    Dim theSession As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()

    Sub Main()

        Dim user_data As System.IntPtr = Nothing
        Dim function_id As System.IntPtr = Nothing

        ufs.UF.AddCallbackFunction(CallbackReason.OpenPartReason, _
            AddressOf OnOpenPart, user_data, function_id)

    End Sub

    Sub OnOpenPart(ByVal reason As CallbackReason, _
            ByVal application_data As IntPtr, ByVal user_data As IntPtr)

        Echo("You opened a part!")

    End Sub

    Sub Echo(ByVal output As String)

        theSession.ListingWindow.Open()
        theSession.ListingWindow.WriteLine(output)
        theSession.LogFile.WriteLine(output)

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.AtTermination
    End Function

End Module

Re: Menuscript modificaton for History Pallette

Pioneer
Pioneer

OK. I replaced the "Echo" sub function with my insert attributes function.

 

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module register_function_to_run_when_a_part_is_opened

    Dim theSession As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()

    Sub Main()

        Dim user_data As System.IntPtr = Nothing
        Dim function_id As System.IntPtr = Nothing

        ufs.UF.AddCallbackFunction(CallbackReason.OpenPartReason, _
            AddressOf OnOpenPart, user_data, function_id)

    End Sub

    Sub OnOpenPart(ByVal reason As CallbackReason, _
            ByVal application_data As IntPtr, ByVal user_data As IntPtr)

        Dim theSession As Session = Session.GetSession()

        Dim workPart As Part = theSession.Parts.Work

        Dim displayPart As Part = theSession.Parts.Display
        Dim session_UndoMarkId1 As Session.UndoMarkId
        session_UndoMarkId1 =
        theSession.SetUndoMark(Session.MarkVisibility.Visible, "Assign Attributes")
        Dim path As String = theSession.Parts.Work.FullPath
        theSession.Parts.Work.SetUserAttribute("MYFULLPATH", -1, path.ToString, Update.Option.Now)
        Dim win As System.Security.Principal.WindowsIdentity
        win = System.Security.Principal.WindowsIdentity.GetCurrent()
        Dim _UserName = win.Name.Substring(win.Name.IndexOf("\") + 1)
        theSession.Parts.Work.SetUserAttribute("USER_NAME", -1, _UserName.ToString, Update.Option.Now)
                Dim sysTime As NXObject.ComputationalTime
        theSession.Parts.Work.SetTimeUserAttribute("SystemTime", -1, sysTime, Update.Option.Now)

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.AtTermination
    End Function

End Module

 

Now after I build the .DLL do I go to FIle>Execute>NXOpen, select the .dll and run? Most likely I will want to run this when the part is saved also. Can I add these to this .dll or does need to be done separatly?

Re: Menuscript modificaton for History Pallette

Siemens Phenom Siemens Phenom
Siemens Phenom

 

"Now after I build the .DLL do I go to FIle>Execute>NXOpen, select the .dll and run?"

 

Yes, exactly.  Then for the rest of the session, it should run whenever you open a part.

 

"Most likely I will want to run this when the part is saved also. Can I add these to this .dll or does need to be done separatly?"

 

You can do it from the same .DLL.  Do you want to do the same thing?  If so, just register the same function but with a reason of "CallbackReason.SavePartReason".  If you want to do something different, add a different function for that reason.

Re: Menuscript modificaton for History Pallette

Pioneer
Pioneer

"Yes, exactly.  Then for the rest of the session, it should run whenever you open a part."

 

For the rest of the session? I won't have to run this every time I open a session, will I? 

 

My plan is to add this functionality to all users in our NX user group. We run a modified version of Rob Cohon's customized environment. I was assuming that I would add this to the start up/application folder like I did the menuscript files in the NXcustom library. Is this correct?

Re: Menuscript modificaton for History Pallette

Siemens Phenom Siemens Phenom
Siemens Phenom

 

Make a small change to the code:  Instead of a Sub, it needs to be a Startup function:

 

    'Sub Main() '<== Old
    Function Startup(ByVal args As String()) As Integer    ' <== New

        Dim user_data As System.IntPtr = Nothing
        Dim function_id As System.IntPtr = Nothing

        ufs.UF.AddCallbackFunction(CallbackReason.OpenPartReason, _
            AddressOf OnOpenPart, user_data, function_id)
        Return 0    ' <== New

    End Function       ' <== New
    'End Sub  '<== Old

 

Now define "USER_STARTUP" to point to the full path and name of the .DLL.  Then start NX.  It should load and run automatically to register the function, then the function should run at the right time(s).