Пример #1
0
        private void button_Create_Click(object sender, EventArgs e)
        {
            string MainBlock = (_FBD_Path.SelectedPath + "\\Empty_FC.xml");
            int    arrLenght = dataGridView1.Rows.Count - 1;

            Mechanism[] Mechanisms = new Mechanism[arrLenght];
            progressBar1.Value = 0;


            for (int i = 0; i < arrLenght; i++)
            {
                Mechanisms[i].name = dataGridView1.Rows[i].Cells[0].Value.ToString();
                Mechanisms[i].type = dataGridView1.Rows[i].Cells[1].Value.ToString();
            }


            XDocument doc = XDocument.Load(MainBlock);

            doc.Root.Element("SW.Blocks.FC").Element("ObjectList").DescendantsAndSelf("SW.Blocks.CompileUnit").Remove();
            //Console.WriteLine(doc);

            progressBar1.PerformStep();

            int id = Mechanisms.Length * 10 + 90;

            for (int i = 0; i < Mechanisms.Length; i++)
            {
                XDocument doc_to_Extract  = XDocument.Load(_FBD_Path.SelectedPath + "\\Template_FC.xml");// + Mechanisms[i].type);"SW.Blocks.CompileUnit"                                                                                              //XElement ele_NewFragment = doc_to_Extract.Root.Element("SW.Blocks.FC").Element("ObjectList").Descendants().Where(z => z.Elements("SW.Blocks.CompileUnit").Any (x => (string)x.Attribute("Name") == a ));
                XElement  ele_NewFragment = doc_to_Extract.Root.Element("SW.Blocks.FC").Element("ObjectList").Elements("SW.Blocks.CompileUnit").Where(d => d.Descendants().Any(s => (string)s.Attribute("Name") == Mechanisms[i].type)).FirstOrDefault();
                //Console.WriteLine(ele_NewFragment+"\n");

                //Extract ID's in order to update them
                IEnumerable <XElement> extract_ID = from z in ele_NewFragment.DescendantsAndSelf()
                                                    where (string)z.Attribute("ID") != null
                                                    select z;
                foreach (XElement Xid in extract_ID)
                {
                    Xid.Attribute("ID").Value = id.ToString();
                    id--;
                }

                // Update name of instance DB according to array of mechanism names

                XNamespace SIE          = "http://www.siemens.com/automation/Openness/SW/NetworkSource/FlgNet/v1";
                XElement   extract_Name = ele_NewFragment.Element("AttributeList").Element("NetworkSource").Element(SIE + "FlgNet").Element(SIE + "Parts").Element(SIE + "Call").Element(SIE + "CallInfo").Element(SIE + "Instance").Element(SIE + "Component");
                extract_Name.Attribute("Name").Value = Mechanisms[i].name;
                //Console.WriteLine(extract_Name);
                doc.Root.Element("SW.Blocks.FC").Element("ObjectList").Elements().Where(x => (string)x.Attribute("CompositionName") == "Title").FirstOrDefault().AddBeforeSelf(ele_NewFragment);
            }

            doc.Save(@"C:\Users\V14\Desktop\Sample\Generated.xml");


            progressBar1.PerformStep();


            for (int i = 0; i < Mechanisms.Length; i++)
            {
                CreateInstDB(Mechanisms[i].name, Mechanisms[i].type, i + 5);
            }

            progressBar1.PerformStep();

            for (int i = 0; i < Mechanisms.Length; i++)
            {
                string file = _FBD_Path.SelectedPath + "\\" + Mechanisms[i].name + ".xml";

                FindPlc();
                ImportBlocks(_controller, file);
            }

            progressBar1.PerformStep();

            string path_FC = _FBD_Path.SelectedPath + "\\" + "Generated" + ".xml";

            FindPlc();
            ImportBlocks(_controller, path_FC);
            progressBar1.PerformStep();

            MessageBox.Show("Done!");
        }