6 Replies Latest reply on May 26, 2016 10:09 PM by ahuynh

    Change PDI file depending on what is clicked on Available Modules tab?

    ahuynh

      Hello,

       

      I have various modules in the available modules tab and I want to be able to change the PI display depending on which module is clicked on. Currently, the same PDI persists after clicking on other modules but I want it to change. Is there a way to do this somehow in the VB code?

        • Re: Change PDI file depending on what is clicked on Available Modules tab?
          John Messinger

          Out of curiosity, what version of ProcessBook are you using? Do I understand the available modules tab to mean a Module Relative display (as in PI Module Database)?

            • Re: Change PDI file depending on what is clicked on Available Modules tab?
              ahuynh

              I'm using version 3.2.3.0. I'm not too sure if the available modules tab is a Module Relative display.

                • Re: Change PDI file depending on what is clicked on Available Modules tab?
                  John Messinger

                  OK, so definitely Module Database modules in this particular window. Typically this add-in is used to provide context to dynamic symbols such as Trends, Values, etc, when they are associated with the PI Module Database. This is the classic Module Relative Display functionality. Selecting a different module in the list would provide a new data context to said dynamic symbols. If you want to use this window as a means of display navigation, you will need to write some VBA code behind your display to capture the context change, and perform some action based on that change, in this case opening a pre-defined PDI file. Having said all this, I've programmatically used the newer Element Relative Display context handler in code to perform some custom actions, but not this particular handler. I'm pretty sure what you want to do can be done, and I will need to dig out my code from the project I did with the ERD and see if it can be adapted.

                  I assume that you have a string property defined in your Modules that contains the path of the PDI file you want to open when a specific module is clicked? If not, I would suggest that you will need to add such a property so that your custom code will know what file is to be opened.

                  I'll get back to you later today once I've had a chance to dig out and modify some code to see how much of this is actually possible.

                  • Re: Change PDI file depending on what is clicked on Available Modules tab?
                    John Messinger

                    Hi Andy,

                    So I've had a look through one of my older projects, and found some code that does some similar things. Firstly, you will need to declare a ContextHandler variable with events:

                    Dim WithEvents ctx As ContextHandler

                     

                    In your Display_Open() event handler, you will need to set a reference to the context:

                    Set ctx = Application.ContextHandlers.Item("ModuleContext")

                     

                    You will need to generate the event handler stub for the ContextChanged event:

                    Private Sub ctx_ContextChanged(FromDisplay As Display, FromContextHandler As ContextHandler)

                    End Sub

                     

                    Within this event handler, get a handle to the actual context that has been selected:

                    Dim selectedcontext As String

                    selectedcontext = ctx.CurrentContext(FromDisplay)

                     

                    As I don't currently have a PI Server with an accessible PI Module Database, I haven't been able to test what the string format is of the returned context. In any event, you should have a PI Module name that you can work with. From here, you will need some PI-SDK code to get a handle to the PIModule object represented by the context. Once you have this, you can read the PIProperty object that contains the file path to the PDI file you want to open, and then open this file as follows:

                    Application.Displays.Open "C:\PathToYourFile\TargetFile.pdi", True

                     

                    It should go without saying at this point that you will also need to include PI-SDK in your project to handle the connection to the PI server, and to reference the PI Module Database.

                     

                    If I can spin up an older version of PI a little later to test against, I will try and post a more complete code sample.

                    • Re: Change PDI file depending on what is clicked on Available Modules tab?
                      John Messinger

                      So here is a very simple, but useable code sample:

                      Option Explicit
                      
                      Dim WithEvents ctx As ContextHandler
                      Dim server As PISDK.server
                      Dim mdb As PIModuleDB
                      
                      Private Sub ctx_ContextChanged(FromDisplay As Display, FromContextHandler As ContextHandler)
                      
                          Dim module_path() As String
                          Dim mod_name As String
                          Dim module As PIModule
                          
                          module_path = Split(ctx.CurrentContext(FromDisplay), "\")
                          mod_name = module_path(UBound(module_path))
                          Set module = mdb.PIModules(mod_name)
                          Application.Displays.Open module.PIProperties("FileName").Value, True
                          
                      End Sub
                      
                      Private Sub Display_Open()
                      
                          Set ctx = Application.ContextHandlers.Item("ModuleContext")
                          Set server = PISDK.Servers.DefaultServer
                          Set mdb = server.PIModuleDB
                          
                      End Sub
                      

                       

                      Hope this helps.

                      2 of 2 people found this helpful