コード例 #1
0
ファイル: DocumentForm.cs プロジェクト: jjacksons/GLUE
        private void importFile(String filename)
        {
            String path = "";
            bool multifile = false;
            bool multiimport = false;
            if (filename.Trim().Length == 0)
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "Gridlabd GLM files (*.glm)|*.glm";
                DialogResult results = dlg.ShowDialog(this);
                if (results != DialogResult.OK) return;
                filename = dlg.FileName;
                path = filename.Remove(filename.IndexOf(dlg.SafeFileName));
            }

            if (this.isDirty())
            {
                DialogResult result = MessageBox.Show(this, "This file is already edited. Do you want to merge?", Program.AppName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (result == DialogResult.Cancel) return;
                if (result == DialogResult.No)
                {
                    this.Save();
                    this.Close();
                }
            }

            int FileLineCount = 0;
            string line;
            List<ModuleItems.Module> objects = new List<ModuleItems.Module>();
            ModuleItems.Module temp = null;
            System.IO.StreamReader file = new System.IO.StreamReader(filename);
            String prevline = "";

            while ((line = file.ReadLine()) != null)
            {

                line = Regex.Replace(line, "\t", "");
                line = Regex.Replace(line, "//.*", "").Trim();
                FileLineCount++;
                if (line.Trim() == string.Empty) continue;
                prevline = line.Trim();
                if (line.Trim() == "{") line = prevline + " {";
                if (line.IndexOf("#") >= 0 && !multifile)
                {
                    DialogResult result = MessageBox.Show(this, "This file used external files. Do you want to import?", Program.AppName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    multifile = true;
                    if (result == DialogResult.Cancel) return;
                    multiimport = result == System.Windows.Forms.DialogResult.Yes;
                }
                if (line.IndexOf("#include") >= 0 && multiimport) importFile(path + Regex.Replace(line, ".*\"(.*)\"", "$1"));
                if (line.IndexOf("object") >= 0)
                {
                    String type = Regex.Replace(line, ".*object (.*){", "$1").Trim();

                    if (type.IndexOf(":") >= 0) type = Regex.Replace(line, ".*object (.*):.*{", "$1").Trim();
                    temp = (ModuleItems.Module)Model.CreateObject(type, new UnitPoint(), null);
                    if (temp == null)
                    {
                        ModuleItems.unknown.unknown temp2 = Model.CreateObject("unknown", new UnitPoint(), null) as ModuleItems.unknown.unknown;
                        temp2.Type = type;
                        temp = temp2;
                    }
                    if (type.IndexOf(":") >= 0) foreach (ModuleItems.Property p in temp.Properties) if (p.name == "name") p.value = Regex.Replace(line, ".*object .*:(.*){", "$1").Trim();
                    continue;
                }
                if (line.IndexOf("{") >= 0 && line.IndexOf("clock") >= 0)
                {
                    //only clocks so far
                    temp = new ModuleItems.clock();
                    continue;
                }
                if ((line.Trim() == "}" || line.Trim() == "};") && temp != null)
                {
                    objects.Add(temp);
                    temp = null;
                    continue;
                }
                if (temp != null)
                {
                    line = Regex.Replace(line, "'(.*) (.*)'", "'$1@@@@$2'").Trim();
                    line = Regex.Replace(line, "(.*) (.*) .*;", "$1 $2;").Trim();
                    bool solved = false;
                    foreach (ModuleItems.Property p in temp.Properties) if (p.name == Regex.Replace(line, "(.*) .*", "$1").Trim()) { p.value = Regex.Replace(Regex.Replace(line, ".* (.*);", "$1").Trim(), "@@@@", " "); solved = true; }
                    if (!solved) temp.Properties.Add(new ModuleItems.Property(Regex.Replace(line, "(.*) .*", "$1").Trim(), Regex.Replace(Regex.Replace(line, ".* (.*);", "$1").Trim(), "@@@@", " "), ""));
                    continue;
                }
            }
            file.Close();
            SetHint("Completed glm file import. Processing...");
            this.Invalidate(true);
            UnitPoint start = new UnitPoint(0, 0);
            while (objects.Count > 0)
            {

                ModuleItems.Module m = objects[0];
                objects.Remove(m);
                m.FromPoint = m.StartPoint = m.ToPoint = m.EndPoint = start;
                if (m.tofrom)
                {
                    m.FromPoint = new UnitPoint(start.X + 1,start.Y + 1);
                    m.EndPoint = new UnitPoint(start.X + 1, start.Y);
                    m.ToPoint = new UnitPoint(start.X , start.Y - 1);
                }
                if (m.child)
                {
                    m.FromPoint = new UnitPoint(start.X + 1, start.Y + 1);
                    m.EndPoint = new UnitPoint(start.X + 1, start.Y);
                }
                Model.AddObject(Model.ActiveLayer, m);

                Trace(m,objects);
                start.X++;
                start.X++;
                start.X++;
                if (start.X > 50)
                {
                    start.X = 0;
                    start.Y += 3;
                }
            }

            Invalidate(true);
            SetHint("");
        }
コード例 #2
0
ファイル: Clock.cs プロジェクト: jjacksons/GLUE
 public clock(clock old)
 {
     m_p1 = old.FromPoint; m_p2 = old.StartPoint; m_p3 = old.EndPoint; m_p4 = old.ToPoint; setupProperties(old.Properties);
 }