6 Replies Latest reply on Nov 30, 2017 9:07 PM by Rick Davin

    How to get the count of points in an interface?

    shakith

      Hi,

       

      I am trying to get the count of points in an interface using the point source and location1 as the search inputs.

       

      Which is the best possible vb.net solution for doing this?

       

      Thanks

        • Re: How to get the count of points in an interface?
          pmartin

          Hi Shakith,

           

          I wrote this code in C# (which I'm more familiar with) using the AF SDK.  If necessary, I can convert it to VB since the languages are similar.

           

                      //get a list of point sources from the PI Data Archive
                      PIPointSources ptSources = pi.PointSources;
          
                      foreach (PIPointSource p in ptSources)
                      {
                          //Print out point source name and total count of points for that point source
                          Console.WriteLine($"Point Source: {p.Name} - {p.PointCount}");
          
                          //perform search for pts that have the desired point source
                          var pts = new PIPointList(PIPoint.FindPIPoints(pi, new[] { new PIPointQuery(PICommonPointAttributes.PointSource, AFSearchOperator.Equal, p.Name) }));
          
                          //load location1 attribute and group by it
                          pts.LoadAttributes(PICommonPointAttributes.Location1);
                          var ptsByLocation = pts.GroupBy(pt => {
                              return pt.IsAttributeLoaded(PICommonPointAttributes.Location1) ? pt.GetAttribute(PICommonPointAttributes.Location1) : null;
                          });
          
                          //loop through groups and print out how many belong to each group
                          foreach(var ptbyLocation in ptsByLocation)
                          {
                              Console.WriteLine($"\tLocation1: {ptbyLocation.Key} - {ptbyLocation.Count()}");
                          }
                      }
          
          5 of 5 people found this helpful
            • Re: How to get the count of points in an interface?
              Rick Davin

              Very nice.  Should translate easy enough.  The 2 biggest issues is the lambda in line 14 and line 15 (with the ? and : ). 

                • Re: How to get the count of points in an interface?
                  John Messinger

                  It is a very nice solution. Hopefully translating to VB.net shouldn't be too hard. VB.Net supports lambda expressions, and the statement on line 15 with the ternary operator can just be expanded out to a full If Then Else statement in VB.Net. Ah, the elegance of C#...

                    • Re: How to get the count of points in an interface?
                      Rick Davin

                      Yep.  Here's my taking a stab at the VB.NET:

                       

                       'get a list of point sources from the PI Data Archive
                          Dim ptSources As PIPointSources = pi.PointSources
                      
                          For Each p As PIPointSource In ptSources
                              'Print out point source name and total count of points for that point source 
                              Console.WriteLine($"Point Source: {p.Name} - {p.PointCount}")
                      
                              'perform search for pts that have the desired point source 
                              Dim query = New List(Of PIPointQuery)
                              query.Add(New PIPointQuery(PICommonPointAttributes.PointSource, AFSearchOperator.Equal, p.Name))
                      
                              'Load Location1 point attribute while we fetching PI points
                              Dim attributeNames = New List(Of String)
                              attributeNames.Add(PICommonPointAttributes.Location1)
                      
                              Dim pts = New PIPointList(PIPoint.FindPIPoints(pi, query, attributeNames))
                      
                              'Create a single line lambda function
                              Dim GetLocation1 As Func(Of PIPoint, String) = Function(pt) If(pt.IsAttributeLoaded(PICommonPointAttributes.Location1), pt.GetAttribute(PICommonPointAttributes.Location1), String.Empty)
                      
                              Dim ptsByLocation = pts.GroupBy(GetLocation1)
                      
                              'loop through groups and print out how many belong to each group 
                              For Each ptbyLocation In ptsByLocation
                                  Console.WriteLine($"{vbTab}Location1: {ptbyLocation.Key} - {ptbyLocation.Count()}")
                              Next
                      
                          Next
                      

                       

                      It's not exactly a 1-to-1 translation in its behavior.  I took liberty of loading the Location1 attribute during the FindPIPoints call.

                      3 of 3 people found this helpful
                • Re: How to get the count of points in an interface?
                  ksward

                  Also there is Health Points that can be used to obtain that same data down to the Scan Class or interface total.