7 Replies Latest reply on Nov 17, 2015 9:49 PM by vsinyuk

    How to use AF Subsitution Parameters in Windows Form application?

    vsinyuk

      Hello, I have a windows Form application where I load up all of the notifications in an AF Database, and rename them accordingly. I want to be able to replicate AF syntax when I rename these notifications. For instance I want to be able to pass along a string \%..\Element% and be able to bring back the relative value. Is there an easy way to do this?

       

      Thank you in advance for any comments or suggestions.

        • Re: How to use AF Subsitution Parameters in Windows Form application?
          cvillanua

          Hello Vitaliy,

           

          I will be moving this question to PI Developers Club. The PI Developers Club (PI DevClub) is an online offering that provides resources for developing applications and solutions on the PI System.

           

          In order to know more about this blog section have a look a the following link: PI Developers Club FAQ

           

          -Carlos.

          • Re: How to use AF Subsitution Parameters in Windows Form application?
            Marcos Vainer Loeff

            Hello Vitaliy,

             

            Please refer to the AFNameSubstitution.ResolveName() static method on the AF SDK Reference document.

             

            Let's suppose I want to update the name of an attribute through a concatenation of two strings: element's name and "Big Test". The sample code snippet would be:

             

                        AFElement element = AFObject.FindObject(@"\\MARC-PI2014\AFSDK Test\Analysis Tests") as AFElement;
                        AFAttribute testAttribute = element.Attributes.Add("Test attribute");
                        bool result = AFNameSubstitution.ResolveName(testAttribute, "%Element% Big Test", null);
                      
            

             

            If you access testAttribute.Name, you will see the object's Name property updated.

             

            Hope it helps!

              • Re: How to use AF Subsitution Parameters in Windows Form application?
                vsinyuk

                Hello Marcus,

                 

                Thank you for your reply. If I am understanding this correctly, what you are doing here is renaming the Attribute name with "Big Test" correct? If so, what I am actually trying to do is use the substitution parameters such as  %Element% to be able to rename a notification with the relative Element name, etc.. Would this process be able to do this?

                 

                Thank you.

                  • Re: How to use AF Subsitution Parameters in Windows Form application?
                    Marcos Vainer Loeff

                    Hi Vitaly,

                     

                    No, on the previous example, the ResolveName method will get the attribute’s element name and concatenate with “Big Test”. If the element’s name is “Hello”, the attribute’s name after running the ResolveName method would be “Hello Big Test”.

                     

                    If I understood correctly, you want to replace the notification name with the name of the target element for each notification. Is it true? In this case, I would just write a custom method to achieve this goal:

                     

                     public static void SubstituteParameters(AFObject afObject, string name)
                            {
                                if (afObject is AFNotification)
                                {
                                    if (name.Contains(@"%..\Element%") == true)
                                    {
                                        AFNotification notification = (AFNotification) afObject;
                                        AFElement targetElement = (AFElement)notification.Target;
                                        if (targetElement.Parent != null)
                                        {
                                            notification.Name = notification.Name.Replace(@"\%..\Element%", targetElement.Parent.Name);
                                        }
                                    }
                                }
                            }
                    

                     

                    The code snippet above is an example. You can change the logic according to your needs.

                     

                    Hope it helps!

                      • Re: How to use AF Subsitution Parameters in Windows Form application?
                        Marcos Vainer Loeff

                        I guess the code snippet below is considered a better practice:

                         

                          public static void SubstituteParameters(AFObject afObject, string name)
                                {
                                    IList<KeyValuePair<string, AFNameSubstitutionType>> substitutionList = AFNameSubstitution.GetSubstitutionList(AFNameSubstitution.Filters.None);
                                    var parentElementSubsitution = substitutionList.Where(m=>m.Value == AFNameSubstitutionType.ParentElement).FirstOrDefault();
                        
                        
                        
                        
                                    if (afObject is AFNotification)
                                    {
                                        if (name.Contains(parentElementSubsitution.Key) == true)
                                        {
                                            AFNotification notification = (AFNotification) afObject;
                                            AFElement targetElement = (AFElement)notification.Target;
                                            if (targetElement.Parent != null)
                                            {
                                                notification.Name = notification.Name.Replace(parentElementSubsitution.Key, targetElement.Parent.Name);
                                            }
                                        }
                                    }
                                }
                        
                        1 of 1 people found this helpful