8 Replies Latest reply on Apr 12, 2012 2:26 PM by ConditionMonitoring

    can you help with entry SetLevel  method

    ConditionMonitoring

      I would like to create a new processbook with some text entries at level =1 and for each text entry I want to have some display entries at level =2

       

      Please find the code below, it is very odd that setlabel works fine but not for SetLevel. Can you help with this?

       

       

       
      Public Sub
          CreatePIW()
          Dim newwindow As New PBObjLib.Display
          Dim oBook As PBObjLib.ProcBook
          Dim oEntry As PBObjLib.Entry
          Dim oEntries As PBObjLib.Entries
          Dim directory As String
          Dim i As Short
          Dim j As Short
          Dim ItemtextList As New List(Of String)
          Dim itemlist As New List(Of String)
          ItemtextList.Add("item1")
          ItemtextList.Add("item2")
          directory = "C:\Documents and Settings\D15544\Desktop\testmacro\testbook.piw"
          itemlist.Add("content1")
          itemlist.Add("content2")
          For i = 0 To 1
              MsgBox("create new item text page for " & ItemtextList.Item(i))
              app.ProcBooks.Open(directory)
              oBook = app.ActiveProcBook
              oEntry = oBook.Entries.Add(ItemtextList.Item(i))
              For j = 0 To 1
                  MsgBox("create new contents on each text page for content " & (j + 1))
                  oEntry = oBook.Entries.InsertAfter(oEntry, itemlist.Item(j))
                  oEntry.SetLabel("changed")
                  oEntry.SetLevel(2)
                  oEntry.DefineAction(1, itemlist.Item(j), directory)
                  MsgBox("entry and window is linked")
              Next j
              j = 0
          Next i
      End sub
      

       

        • Re: can you help with entry SetLevel  method
          mhamel

          @Tom: Changing the level can only work when used in outline mode. You will need to change the viewing mode of PI ProcessBook using the RunMode property of your Application object. I have added the change of RunMode property in your code snippet.

           
          Public Sub CreatePIW()
               'Declaration
               '...
               
               ItemtextList.Add("item1")
               ItemtextList.Add("item2")
               directory = "C:\Documents and Settings\D15544\Desktop\testmacro\testbook.piw"
               itemlist.Add("content1")
               itemlist.Add("content2")
               
               For i = 0 To 1
                    MsgBox("create new item text page for " & ItemtextList.Item(i))
                    
                    'Here!!!
                    'Add here the change in Outline mode.
                    app.RunMode = False
                    'Here!!!
                    
                    app.ProcBooks.Open(directory)          
                    oBook = app.ActiveProcBook          
                    oEntry = oBook.Entries.Add(ItemtextList.Item(i))          
                    For j = 0 To 1
                         MsgBox("create new contents on each text page for content " & (j + 1))
                         oEntry = oBook.Entries.InsertAfter(oEntry, itemlist.Item(j))
                         oEntry.SetLabel("changed")
                         oEntry.SetLevel(2)
                         oEntry.DefineAction(1, itemlist.Item(j), directory)
                         MsgBox("entry and window is linked")
                    Next j
                    
                    j = 0
               Next i
          End Sub
          

           

           

          Let me know if that helped you.

           

           

            • Re: can you help with entry SetLevel  method
              ConditionMonitoring

              When using app.RunMode = False, it still displays as book mode. But I am now in build mode rather than run mode. The setlevel method is still not working, I wonder how to change the current display mode from book mode to outline mode?

                • Re: can you help with entry SetLevel  method
                  mhamel

                  @Tom: The only other reason why the SetLevel method fails, that I can think of, is because it does not exist a level 1 entry parent of this level 2 entry. To be able to move an entry to level 2 to 9, it must be placed under a level 1 entry.

                   

                  Let me know if that answered your question.

                    • Re: can you help with entry SetLevel  method
                      andreas

                      Tom - your tesbook.piw is empty - I guess. Can you try if it works if your testbook.piw contains already one entry (for example a text entry) at level 1?

                        • Re: can you help with entry SetLevel  method
                          andreas

                          After getting some more iterations, I finally ended up with the below example:

                           
                                  Dim oBook As PBObjLib.ProcBook
                                  Dim oEntry As PBObjLib.Entry
                                  Dim directory As String
                                  Dim i As Short
                                  Dim j As Short
                                  directory = "C:\Book1.piw"
                          
                                  app.ProcBooks.Open(directory)
                                  oBook = app.ActiveProcBook
                          
                                  For i = 5 To 1 Step -1
                                      oEntry = CType(oBook.Entries.Add("Item" + i.ToString + " " + Now().ToString()), PBObjLib.Entry)
                                  Next
                          
                                  Dim message As String
                                  message = ""
                                  For i = 1 To 5
                                      message = message + i.ToString() + ": " + oBook.Entries.Item(i).Label + System.Environment.NewLine
                                  Next
                                  MessageBox.Show(message)
                          
                                  For i = 2 To 5
                                      oBook.Entries.Item(i).SetLevel(i)
                                  Next
                          

                           

                           

                          hope that helps illustrating how you could set the level in an empty PIW.

                            • Re: can you help with entry SetLevel  method
                              ConditionMonitoring

                              When i am creating my entries i am using the code:

                               

                              oEntry = oBook.Entries.Add(entryname)

                               

                              which is within a series of For each loops. When each entry is added it is added above the previous entry instead of below the previous entry. Do you know if there is a way to ensure every subsequent addition of an entry is after the one before?

                                • Re: can you help with entry SetLevel  method
                                  dhollebeek

                                  I believe there is an AddAfter method, however, there are known bugs in the entry add methods. Have u tried adding your entries in reverse order?

                                    • Re: can you help with entry SetLevel  method
                                      ConditionMonitoring

                                      @Mathieu: Yes there needed to be a parent entry to change the level.

                                       

                                      @Andreas: Thanks for the help but i am using a series of classes within classes so try and avoid using forloops if its possible to use a foreachloop.

                                       

                                      @everyone else for reference: This is my code to sort out my levels but since im using a class structure if might be a bit confusing to follow at first. This is the method I used if.

                                       

                                      1. Populate a book with all required entries (in the correct order-using a series of for loops)

                                       

                                      2. then this code to sort out the levels:

                                       
                                      For Each PBunit As Unit In PBstation.unitlist
                                      
                                                     For Each PBEntry As Entry In oBook.Entries
                                      
                                                         If PBEntry.Label = PBunit.name Then
                                      
                                                             'if this code isnt run then the levels will be shifted into the wrong place  'when on the second run of the loop
                                      
                                                             If Not PBEntry.Level = 1 Then
                                      
                                                                 PBEntry.SetLevel(1)
                                      
                                                                 Exit For
                                      
                                                             End If
                                      
                                                         End If
                                      
                                                     Next
                                      
                                                     For Each PBmtype As modeltype In PBunit.modeltypelist
                                      
                                                         For Each PBEntry As Entry In oBook.Entries
                                      
                                                             If PBEntry.Label = PBmtype.name Then
                                      
                                                                 PBEntry.SetLevel(2)
                                      
                                                                 Exit For
                                      
                                                             End If
                                      
                                                         Next
                                      
                                                         For Each PBm As Model In PBmtype.modellist
                                      
                                                             For Each PBEntry As Entry In oBook.Entries
                                      
                                                                 If PBEntry.Label = PBm.name Then
                                      
                                                                     PBEntry.SetLevel(3)
                                      
                                        ‘this lowest level will have a display associated with it
                                      
                                                                     PBEntry.DefineAction(1, PBEntry.Label, PBm.ClusterSetName)
                                      
                                                                     Exit For
                                      
                                                                 End If
                                      
                                                             Next
                                      
                                                         Next
                                      
                                                     Next
                                      
                                                 Next
                                      

                                       

                                       

                                      Hope this is helpful if you need help using the SetLevel Method and entries.

                                       

                                      Thanks for all the help guys