Example #1
0
        /// <summary>
        /// Update a tag named prefix + index
        /// Create one if it doesn't exist, using tag prefix + (index-1) as template.
        /// </summary>
        /// <param name="prefix">Start of tag name. Typically ends with '_'</param>
        /// <param name="index">Index added to end of name</param>
        /// <param name="plc_tag">PLC tag to connect to</param>
        public void AddIndexedTag(string prefix, int index, string plc_tag, DataType type = null)
        {
            string     tag_name = prefix + index.ToString();
            XmlElement tag      = tag_list.SelectSingleNode("Hmi.Tag.Tag[AttributeList/Name/text()='" + tag_name + "']") as XmlElement;

            if (tag == null)
            {
                string     template_name = prefix + (index - 1).ToString();
                XmlElement template      = tag_list.SelectSingleNode("Hmi.Tag.Tag[AttributeList/Name/text()='" + template_name + "']") as XmlElement;
                if (template == null)
                {
                    throw new Exception("No tag " + template_name + " to use as template for tag " + tag_name);
                }
                tag = template.CloneNode(true) as XmlElement;
                tag_list.InsertAfter(tag, template);
                XMLUtil.ReplaceID(tag, idset);

                // Change name
                XmlElement name_elem = tag.SelectSingleNode("AttributeList/Name") as XmlElement;
                name_elem.InnerText = tag_name;
            }

            // Erase all type information and let the import figure it out
            XmlElement attr_list   = tag.SelectSingleNode("AttributeList") as XmlElement;
            XmlElement length_elem = attr_list.SelectSingleNode("Length") as XmlElement;

            if (length_elem != null)
            {
                attr_list.RemoveChild(length_elem);
            }
            if (type != null)
            {
                XmlElement coding_elem = attr_list.SelectSingleNode("Coding") as XmlElement;
                if (coding_elem != null)
                {
                    if (type is REAL || type is LREAL)
                    {
                        coding_elem.InnerText = "IEEE754Float";
                    }
                    else
                    {
                        coding_elem.InnerText = "Binary";
                    }
                }
            }


            XmlElement link_list     = tag.SelectSingleNode("LinkList") as XmlElement;
            XmlElement hmi_type_elem = link_list.SelectSingleNode("HmiDataType") as XmlElement;

            if (hmi_type_elem != null)
            {
                link_list.RemoveChild(hmi_type_elem);
            }
            XmlElement type_elem = link_list.SelectSingleNode("DataType") as XmlElement;

            if (type_elem != null)
            {
                link_list.RemoveChild(type_elem);
            }


            // Set PLC tag
            XmlElement controller_tag_elem = tag.SelectSingleNode("LinkList/ControllerTag/Name") as XmlElement;

            if (controller_tag_elem == null)
            {
                throw new Exception("No PLC tag for HMI tag " + tag_name);
            }
            controller_tag_elem.InnerText = plc_tag;
        }
Example #2
0
        public static FileInfo buildFile(string table_name, IEnumerable <Constant> constants)
        {
            XmlWriterSettings settings = new XmlWriterSettings {
                ConformanceLevel = ConformanceLevel.Document,
                Encoding         = Encoding.UTF8,
                Indent           = true
            };
            FileInfo file;

            using (Stream stream = TempFile.Open("AlarmConst", "xml", out file))
            {
                int       id = 0;
                XmlWriter w  = XmlWriter.Create(stream, settings);
                w.WriteStartDocument();
                w.WriteStartElement("Document");

                w.WriteStartElement("DocumentInfo");
                w.WriteEndElement(); // DocumentInfo
                w.WriteStartElement("SW.Tags.PlcTagTable");
                w.WriteAttributeString("ID", (id++).ToString());
                w.WriteStartElement("AttributeList");
                XMLUtil.SimpleValue(w, "Name", table_name);
                w.WriteEndElement(); // AttributeList



                w.WriteStartElement("ObjectList");
                foreach (Constant c in constants)
                {
                    w.WriteStartElement("SW.Tags.PlcUserConstant");
                    w.WriteAttributeString("ID", (id++).ToString());
                    w.WriteAttributeString("CompositionName", "UserConstants");
                    w.WriteStartElement("AttributeList");
                    XMLUtil.SimpleValue(w, "DataTypeName", "Int");
                    XMLUtil.SimpleValue(w, "Name", c.Name);
                    XMLUtil.SimpleValue(w, "Value", c.Value.ToString());
                    w.WriteEndElement(); // AttributeList



                    w.WriteStartElement("ObjectList");
                    w.WriteStartElement("MultilingualText");
                    w.WriteAttributeString("ID", (id++).ToString());
                    w.WriteAttributeString("CompositionName", "Comment");

                    w.WriteStartElement("ObjectList");
                    w.WriteStartElement("MultilingualTextItem");
                    w.WriteAttributeString("ID", (id++).ToString());
                    w.WriteAttributeString("CompositionName", "Items");

                    w.WriteStartElement("AttributeList");
                    XMLUtil.SimpleValue(w, "Culture", "sv-SE");
                    XMLUtil.SimpleValue(w, "Text", c.Comment);

                    w.WriteEndElement(); // AttributeList
                    w.WriteEndElement(); // MultilingualTextItem
                    w.WriteEndElement(); // ObjectList
                    w.WriteEndElement(); // MultilingualText
                    w.WriteEndElement(); // ObjectList

                    w.WriteEndElement(); // SW.Tags.PlcUserConstant
                }


                w.WriteEndElement(); // ObjectList
                w.WriteEndElement(); // SW.Tags.PlcUserConstant
                w.WriteEndElement(); // Document
                //w.Close();
            }
            return(file);
        }