13 Replies Latest reply on Feb 7, 2014 5:13 PM by pingram

    Access to a BatchView-Control in Processbook from an Processbook-Add-In does not work

    FranzK

      Hi all,

       

      we want to access a BatchView control (BatchGroup) from an Processbook-Add-In. we also checked the workarround from known-issue 20984OSI8. But it doesn't help in our case.

       

      Code snippet:    
                      switch (pbDisplay.SelectedSymbols.Item(1).Type) {

                          case (int) PBObjLib.pbSYMBOLTYPE.pbSymbolBGroup:
                              string name = pbDisplay.SelectedSymbols.Item(1).Name;
                              foreach (PBObjLib.Symbol sym in pbDisplay.Symbols)
                                  if (sym.Name == name)
                                      pbBatchGroup = (BatchGroup) sym;
                              break;

                      }

       

      We get a crash with the following exception for line pbBatchGroup = (BatchGroup) sym;

       

      System.InvalidCastException: "Unable to cast object of type 'PBObjLib.SymbolClass' to type 'OSIsoft.ProcessBook.BatchView.BatchGroup'."

       

      Remarks:
      1) pbBatchGroup is of type OSIsoft.ProcessBook.BatchView.BatchGroup
      2) pbDisplay is of type PBObjLib.Display
      3) Iterating the symbols and checking the name against the selected symbol name is a workaround we got from OSI some years ago. the code did work with the processbook we used then. Maybe meanwhile there is another way to access the BatchGroup?
      4) Casting pbDisplay.SelectedSymbols.Item(1) to OSIsoft.ProcessBook.BatchView.BatchGroup crashed also with InvalidCastException
      5) Access the BatchGroup pbDisplay.Symbols.Item("BatchGroup1") and casting crashed also with InvalidCastException
      6) Using type library PIBTPB instead of the assembly OSIsoft.ProcessBook.BatchView.dll crashed also with InvalidCastException

       

      Assembly versions:
      OSIsoft.PBObjLib.dll: 3.3.2.2076
      OSIsoft.ProcessBook.BatchView.dll: 3.1.4.0

      Software:
      PI ProcessBook 2012 SP2
      PI BatchView 2012
      Windows 2008 R2
      Visual Studio 2010
      .NET Runtime 4.0

       

       

       

      thank you in advance

       

      best regards

       

      Franz

       

       

        • Re: Access to a BatchView-Control in Processbook from an Processbook-Add-In does not work

          Hello Franz,

           

          Can you please see if this other thread has the solution for your issue?

            • Re: Access to a BatchView-Control in Processbook from an Processbook-Add-In does not work
              FranzK

              Hi Gregor,

               

              i just checked all the post's in Group ProcessBook/ActiveView and Microsoft Office Development again. but nothing helps to solve my problem.

               

              the following post you might mention:

               

              vcampus.osisoft.com/.../387.aspx

               

              was generated by a former colleague of mine. the workaround ( Iterating the symbols and checking the name against the selected symbol name) was working back then. but now as we use a newer version of processbook, the workaround isn't working. maybe it's related to a problem of  the combination of processbook and batch-view plug in, as we have problems with this (we oppened a support call:Case # 535207).

               

              best regards

               

              Franz

                • Re: Access to a BatchView-Control in Processbook from an Processbook-Add-In does not work
                  FranzK

                  Hi all,

                   

                  the problem is not related to the support case i mentioned in my last post. If i could help with other information's you need, please ask.

                   

                  could already somebody reproduce my problem?

                   

                  with kind regards

                   

                  Franz

                    • Re: Access to a BatchView-Control in Processbook from an Processbook-Add-In does not work

                      Hi Franz,

                       

                      I was able to reproduce the issue and believe it's caused when mixing COM libraries with .NET libraries.

                       

                      The COM libraries (- btrend.tlb, pbobjlib.tlb and pbsymlib.tlb) are stored at %pihome%\ProcBook\.

                       

                      The corresponding .NET libraries can be found in %pihome%\Batch\(OSIsoft.ProcessBook.BatchView.dll) and %pihome%\ProcBook\PublicAssemblies\ (OSIsoft.PBObjLib.dll and OSIsoft.PBSymLib.dll).

                       

                      Reproducing the issue with the information that can be found within vCampus forum was challenging. E.g. on a 64-bit platform the project needs to be explicitly set to build for x86 platform. I plan to provide a step by step guide with another post at a later time. For now, can you please see if attached Visual Studio 2010 project works for you? The firing event is OnDisplayActivate. All symbols are browsed and if the symbol is of type BatchGroup, the selected BatchGroup objects background color is set to black.

                        • Re: Access to a BatchView-Control in Processbook from an Processbook-Add-In does not work
                          FranzK

                          Hi Gregor,

                           

                          thank you for the sample project. We compared it with our project. We did find out a strange behavior.

                           

                          the code works, if the casting is done within the OnDisplayActivate event.

                           

                          in our code the casting is done later with another event, and it doesn't work.

                           

                          we added the casting within the event OnDisplayActivate, and the casting with our event is also working then.

                           

                          do you know a reason for this behavior?

                           

                          with kind regards

                           

                          Franz

                            • Re: Access to a BatchView-Control in Processbook from an Processbook-Add-In does not work

                              Hello Franz,

                               

                              This appears indeed weird. What's the event that you are using?

                                • Re: Access to a BatchView-Control in Processbook from an Processbook-Add-In does not work
                                  FranzK

                                  Hi Gregor,

                                   

                                  we did test more and did find out, it's not related to the calling event.

                                   

                                  we did change your code like this:

                                   

                                  void OnDisplayActivate(Display aDisplay)

                                   

                                         {

                                   

                                             String sMsg = "empty String";

                                   

                                             // start of fix -------------------------------------------------

                                   

                                             // uncommenting this code fixes the Invalid cast exception

                                   

                                             //foreach (Symbol selSym in aDisplay.Symbols)

                                   

                                             //{

                                   

                                             //    if (selSym.Type == (int)PBObjLib.pbSYMBOLTYPE.pbSymbolBGroup)

                                   

                                             //    {

                                   

                                             //        BatchGroup dummy = (BatchGroup)selSym;

                                   

                                             //        dummy = null;

                                   

                                             //    }

                                   

                                             //}

                                   

                                             // end of fix ---------------------------------------------------

                                   

                                             // start of bad code ---------------------------------------------

                                   

                                             // uncommenting this leads to Invalid cast exception at line myBatchGroup = (BatchGroup)mySymbol;

                                   

                                             //Symbol selSym2 = aDisplay.Symbols.Item(1);

                                   

                                             // end of bad code ---------------------------------------------

                                   

                                             try

                                   

                                             {

                                   

                                                 foreach (Symbol mySymbol in aDisplay.Symbols)

                                   

                                                 {

                                   

                                                     if (mySymbol.Name.ToString() == "BatchGroup1")

                                   

                                                     {

                                   

                                                         BatchGroup myBatchGroup;

                                   

                                                         myBatchGroup = (BatchGroup)mySymbol;    // causes the crash

                                   

                                                         myBatchGroup.BackgroundColor = 1; }

                                   

                                                 }

                                   

                                             }

                                   

                                             catch (Exception ex)

                                   

                                             {

                                   

                                                 sMsg = ex.Message;

                                   

                                             }

                                   

                                             finally

                                   

                                             {

                                   

                                                 MessageBox.Show(sMsg, addInName + "DataRefresh");

                                   

                                             }

                                   

                                         }

                                   

                                  if you un-comment the "bad-code", it will crash.

                                   

                                  if you also un-comment the fix-code it will not crash.

                                   

                                  the code is without use, just for demonstration.

                                   

                                  it show's if you use "aDisplay.Symbols.Item(1)" before the for-each iteration we get a crash with the cast exception.

                                   

                                  it's possible to fix it with another iteration before.

                                   

                                  with kind regards

                                   

                                  Franz

                                    • Re: Access to a BatchView-Control in Processbook from an Processbook-Add-In does not work

                                      Hello Franz,

                                       

                                      Franz Krauter

                                      Symbol selSym2 = aDisplay.Symbols.Item(1);

                                       

                                      Your Display likely have more than just one Symbol. You can verify how many Symbols exist by reading Symbols.Item.Count. Also very likely not all of the Symbols are of type BatchGroup but above line of code assumes the first Symbol (Item(1)) is of type BatchGroup. But the first Item can also be of type PBTrend or PBValue. You should be able to avoid running into the exception by casting inside a foreach loop and after checking the Symbols type..

                                       

                                      Franz Krauter

                                      foreach (Symbol selSym in aDisplay.Symbols)

                                       

                                      This way you iterate through all Symbols where the amount of times you run through the loop equals Symbols.Item.Count

                                       

                                      By checking the actually selected symbol (selSym) for it's type before attempting to cast, you ensure your cast can succeed.

                                       

                                      Franz Krauter

                                      if (selSym.Type == (int)PBObjLib.pbSYMBOLTYPE.pbSymbolBGroup)

                                        • Re: Access to a BatchView-Control in Processbook from an Processbook-Add-In does not work
                                          FranzK

                                          Hi Gregor,

                                           

                                          the codesnippet i did send out of context to our complete code.

                                           

                                          as soon as we try to access an element via Index and name and don't use a loop we get an crash. the crash occurs even we add the loop later.

                                           

                                          i will be not available for the next few weeks. but hope with this hint you can reproduce the problem.

                                           

                                          regards

                                           

                                          Franz

                                            • Re: Access to a BatchView-Control in Processbook from an Processbook-Add-In does not work

                                              Hello Franz,

                                               

                                              Got it. I've tried 2 solutions now that should be equivalent but I am able to reproduce the issue when selecting the symbol by its index.

                                               

                                               

                                               
                                                      // Solution # 1
                                                      public void OnDisplayActivate(Display myDisplay)
                                                      {
                                                          String sMessage = "";
                                                          foreach (Symbol mySymbol in myDisplay.Symbols)
                                                          {
                                                              sMessage += mySymbol.Name + ", " + mySymbol.Type; 
                                                              if (mySymbol.Type == (int)PBObjLib.pbSYMBOLTYPE.pbSymbolBGroup)
                                                              {
                                                                  try
                                                                  {
                                                                      BatchGroup myBatchGroup = (BatchGroup)mySymbol;
                                                                      sMessage += ", " + "Cast to BatchGroup Succeeded";
                                                                  }
                                                                  catch (Exception ex)
                                                                  {
                                                                      sMessage += ", " + ex.Message;
                                                                  }
                                                              }
                                                              sMessage += "\r\n";
                                                          }
                                                          MessageBox.Show(sMessage, addInName);
                                                      }
                                              

                                               

                                               
                                                      // Solution # 2
                                                      public void OnDisplayActivate(Display myDisplay)
                                                      {
                                                          String sMessage = "";
                                                          Symbol mySymbol;
                                                          for (int i = 1; i <= myDisplay.Symbols.Count; i++)
                                                          {
                                                              mySymbol = myDisplay.Symbols.Item(i);
                                                              sMessage += mySymbol.Name + ", " + mySymbol.Type;
                                                              if (mySymbol.Type == (int)PBObjLib.pbSYMBOLTYPE.pbSymbolBGroup)
                                                              {
                                                                  try
                                                                  {
                                                                      BatchGroup myBatchGroup = (BatchGroup)mySymbol;
                                                                      sMessage += ", " + "Cast to BatchGroup Succeeded";
                                                                  }
                                                                  catch (Exception ex)
                                                                  {
                                                                      sMessage += ", " + ex.Message;
                                                                  }
                                                              }
                                                              sMessage += "\r\n";
                                                          }
                                                          MessageBox.Show(sMessage, addInName);
                                                      }
                                              

                                              While solution # 1 works

                                               

                                              7183.CastSucceeded.jpg

                                               

                                              solution # 2 fails

                                               

                                              0827.CastFailed.jpg

                                               

                                              I feel it's time now to hand this over to Technical Support. You'll be contacted by a Technical Support Engineer. I'll suggest working with me closely because I am able to reproduce your issue.

                                               

                                              My expectation is that we may end up with a new known issue with a low priority because there's an existing workaround: Use the foreach loop

                                               

                                              Please let us know if for some reason the workaround is not applicable in your case.