Пример #1
0
        private static void StatusWindowThread(object obj)
        {
            try
            {
                StatusWindow _this = obj as StatusWindow;

                using (ThreadSafeStatusWindow wnd = new ThreadSafeStatusWindow())
                {
                    wnd.Show();

                    while (!_this.cancel)
                    {
                        lock (_this)
                        {
                            wnd.Text           = _this.caption;
                            wnd.lblStatus.Text = _this.status;
                        }

                        Thread.Sleep(30);
                        Application.DoEvents();
                    }
                }
            }
            catch (ThreadAbortException e)
            {
            }
        }
Пример #2
0
        private void btnGenerateData_Click(object sender, EventArgs e)
        {
            //if (this.chkGenerateVTKFiles.Checked && !this.chk3D.Checked)
            //{
            //    MessageBox.Show("Save Mesh as VTK file is not available for plain meshes", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}



            bool old = this.auto_update;

            this.OnChangeSensorParameters(null, null);
            this.auto_update = old;

            if ((this.s3r < this.s2r) && this.chkGenerateCompleteModel.Checked)
            {
                MessageBox.Show("Incorrect distances values", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Program.KIS.Clear();
            string mesh_path = this.edtPath.Text;

            this.btnGenerateData.Enabled = false;

            using (StatusWindow sw = new StatusWindow("Meshing process", "Preparing data"))
                try
                {
                    Directory.CreateDirectory(mesh_path);
                    if (this.chkCleanDir.Checked)
                    {
                        List <string> files = new List <string>();
                        files.AddRange(Directory.GetFiles(mesh_path, "*.txt", SearchOption.TopDirectoryOnly));
                        files.AddRange(Directory.GetFiles(mesh_path, "*.vtk", SearchOption.TopDirectoryOnly));
                        foreach (string f in files)
                        {
                            File.Delete(f);
                        }
                    }
                    string nodes_file, complete_simples_file, complete_mat_file, vtk_complete_file;
                    string electr_simples_file, electr_mat_file, vtk_electr_file;
                    string reco_simples_file, reco_mat_file, vtk_reco_file;
                    string vtk_shield_file, vtk_electrodes_file;
                    string distr_file, xml_filename;

                    XmlDocument doc = new XmlDocument();
                    doc.AppendChild(doc.CreateXmlDeclaration("1.0", string.Empty, "yes"));
                    XmlNode    xml_mesh        = doc.AppendChild(doc.CreateElement("mesh"));
                    XmlElement xml_format      = xml_mesh.AppendChild(doc.CreateElement("format")) as XmlElement;
                    XmlElement xml_is_complete = xml_mesh.AppendChild(doc.CreateElement("is_complete")) as XmlElement;
                    XmlElement xml_type        = xml_mesh.AppendChild(doc.CreateElement("type")) as XmlElement;
                    XmlElement xml_geometry    = xml_mesh.AppendChild(doc.CreateElement("geometry")) as XmlElement;
                    XmlElement xml_nodes       = xml_geometry.AppendChild(doc.CreateElement("nodes")) as XmlElement;
                    XmlElement xml_complete    = xml_geometry.AppendChild(doc.CreateElement("complete")) as XmlElement;
                    XmlElement xml_electrodes  = xml_geometry.AppendChild(doc.CreateElement("electrodes")) as XmlElement;
                    XmlElement xml_image       = xml_geometry.AppendChild(doc.CreateElement("image")) as XmlElement;
                    xml_complete.Attributes.Append(doc.CreateAttribute("radius"));
                    xml_electrodes.Attributes.Append(doc.CreateAttribute("radius"));
                    xml_image.Attributes.Append(doc.CreateAttribute("radius"));

                    xml_filename         = Path.Combine(mesh_path, "m_geometry.rwmg");
                    xml_format.InnerText = "rwmeshgen";

                    // plik węzłów, wspólny dla różnych siatek
                    nodes_file          = Path.Combine(mesh_path, "m_nodes.txt");
                    xml_nodes.InnerText = Path.GetFileName(nodes_file);

                    // complete, electr i reco - do matlaba
                    complete_simples_file = Path.Combine(mesh_path, "m_simples_complete.txt");
                    complete_mat_file     = Path.Combine(mesh_path, "m_material_complete.txt");
                    electr_simples_file   = Path.Combine(mesh_path, "m_simples_elect.txt");
                    electr_mat_file       = Path.Combine(mesh_path, "m_material_elect.txt");
                    reco_simples_file     = Path.Combine(mesh_path, "m_simples_reco.txt");
                    reco_mat_file         = Path.Combine(mesh_path, "m_material_reco.txt");

                    // complete, electr, reco - do vtk
                    vtk_complete_file = Path.Combine(mesh_path, "mesh_complete.vtk");
                    vtk_electr_file   = Path.Combine(mesh_path, "mesh_elec.vtk");
                    vtk_reco_file     = Path.Combine(mesh_path, "mesh_reco.vtk");

                    // shield, electrodes surface - do vtk
                    vtk_shield_file     = Path.Combine(mesh_path, "surf_shld.vtk");
                    vtk_electrodes_file = Path.Combine(mesh_path, "surf_elec.vtk");

                    // rozkład elektrod czujnika 2D
                    distr_file = Path.Combine(mesh_path, "m_electr.txt");

                    if (!this.chkGenerateMaterials.Checked)
                    {
                        complete_mat_file = null;
                        electr_mat_file   = null;
                        reco_mat_file     = null;
                    }

                    Program.KIS.HasMaterials = this.chkGenerateMaterials.Checked;
                    Program.KIS.IsComplete   = this.chkGenerateCompleteModel.Checked;

                    xml_is_complete.InnerText = this.chkGenerateCompleteModel.Checked.ToString();
                    if (this.chkGenerateCompleteModel.Checked)
                    {
                        // model kompletny
                        Program.KIS.ElectrodeSurfaceRadius   = this.outer_wall_distance;
                        Program.KIS.OuterScreenSurfaceRadius = this.shield_distance;

                        xml_complete.InnerText   = Path.GetFileName(complete_simples_file);
                        xml_electrodes.InnerText = Path.GetFileName(electr_simples_file);
                        xml_image.InnerText      = Path.GetFileName(reco_simples_file);

                        xml_complete.Attributes["radius"].Value   = this.shield_distance.ToString("N4", CultureInfo.InvariantCulture);
                        xml_electrodes.Attributes["radius"].Value = this.outer_wall_distance.ToString("N4", CultureInfo.InvariantCulture);
                        xml_image.Attributes["radius"].Value      = this.inner_wall_distance.ToString("N4", CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        // model winreco
                        Program.KIS.ElectrodeSurfaceRadius   = this.outer_wall_distance;
                        Program.KIS.OuterScreenSurfaceRadius = 0;

                        xml_electrodes.InnerText = Path.GetFileName(electr_simples_file);
                        xml_complete.Attributes["radius"].Value   = ((double)0).ToString("N4", CultureInfo.InvariantCulture);
                        xml_electrodes.Attributes["radius"].Value = this.outer_wall_distance.ToString("N4", CultureInfo.InvariantCulture);
                        xml_image.Attributes["radius"].Value      = ((double)0).ToString("N4", CultureInfo.InvariantCulture);
                    }


                    DateTime start_time = DateTime.Now;
                    this.SetupMeshParameters();

                    if (this.chk3D.Checked)
                    {
                        Program.KIS.Mode = TomoKisStudioXml.MeshType.Mode3D;

                        sw.SetStatus("Spatial mesh generation in progress...");
                        this.mesh_generator.GenerateSpatialMesh();

                        if (this.chkGenerateCompleteModel.Checked)
                        {
                            // model kompletny
                            sw.SetStatus("Saving spatial model data (complete mesh)...");
                            this.mesh_generator.SaveSpatialData(nodes_file, complete_simples_file, complete_mat_file);
                            Program.KIS.NodesFile             = nodes_file;
                            Program.KIS.CompleteSimplexesFile = complete_simples_file;
                            Program.KIS.CompleteMaterialsFile = complete_mat_file;

                            sw.SetStatus("Saving spatial model data (reconstruction mesh + electrodes mesh)...");
                            this.mesh_generator.SaveSpatialElectrodesData(String.Empty, electr_simples_file, electr_mat_file);
                            Program.KIS.ElectrodesSimplexesFile = electr_simples_file;
                            Program.KIS.ElectrodesMaterialsFile = electr_mat_file;

                            sw.SetStatus("Saving spatial model data (reconstruction mesh)...");
                            this.mesh_generator.SaveSpatialReconstructionData(String.Empty, reco_simples_file, reco_mat_file);
                            Program.KIS.ReconstructionSimplexesFile = reco_simples_file;
                            Program.KIS.ReconstructionMaterialsFile = reco_mat_file;
                        }
                        else
                        {
                            // model WinRECO
                            sw.SetStatus("Saving spatial model data (complete mesh)...");
                            //this.mesh_generator.SaveSpatialData(nodes_file, complete_simples_file, complete_mat_file);
                            //Program.Kis.NodesFile = nodes_file;
                            //Program.Kis.CompleteSimplexesFile = complete_simples_file;
                            //Program.Kis.CompleteMaterialsFile = complete_mat_file;

                            // 16.02.2010: pomysł Radka
                            this.mesh_generator.SaveSpatialElectrodesData(nodes_file, reco_simples_file, reco_mat_file);
                            Program.KIS.NodesFile = nodes_file;
                            Program.KIS.ReconstructionSimplexesFile = reco_simples_file;
                            Program.KIS.ReconstructionMaterialsFile = reco_mat_file;
                        }

                        if (this.chkGenerateVTKFiles.Checked)
                        {
                            if (this.chkGenerateCompleteModel.Checked)
                            {
                                // model complete
                                sw.SetStatus("Saving spatial model data (complete mesh) to VTK file...");
                                this.mesh_generator.SaveSpatialDataVTK(vtk_complete_file, vtk_shield_file, vtk_electrodes_file, this.chkGenerateMaterials.Checked);
                                sw.SetStatus("Saving spatial model data (reconstruction mesh + electrodes mesh) to VTK file...");
                                this.mesh_generator.SaveSpatialElectrodesDataVTK(vtk_electr_file, this.chkGenerateMaterials.Checked);
                                sw.SetStatus("Saving spatial model data (reconstruction mesh) to VTK file...");
                                this.mesh_generator.SaveSpatialReconstructionDataVTK(vtk_reco_file, this.chkGenerateMaterials.Checked);
                            }
                            else
                            {
                                // model winreco
                                //sw.SetStatus("Saving spatial model data (complete mesh) to VTK file...");
                                //this.mesh_generator.SaveSpatialDataVTK(vtk_complete_file, vtk_shield_file, vtk_electrodes_file, this.chkGenerateMaterials.Checked);

                                // 16.02.2010: pomysł Radka
                                sw.SetStatus("Saving spatial model data (reconstruction mesh + electrodes mesh) to VTK file...");
                                this.mesh_generator.SaveSpatialElectrodesDataVTK(vtk_reco_file, this.chkGenerateMaterials.Checked);
                            }
                        }
                    }
                    else
                    {
                        Program.KIS.Mode = TomoKisStudioXml.MeshType.Mode2D;

                        sw.SetStatus("Plain mesh generation in progress...");
                        this.mesh_generator.GeneratePlainMesh();


                        if (this.chkGeneratePlainElectrodes.Checked)
                        {
                            // generacja rozladu elektrod
                            this.mesh_generator.GeneratePlainElectrodesDistribution();
                            this.mesh_generator.SaveDistribution(distr_file);
                            Program.KIS.PlainDistribution = distr_file;
                        }

                        if (this.chkGenerateCompleteModel.Checked)
                        {
                            // model kompletny
                            sw.SetStatus("Saving plain model data (complete mesh)...");
                            this.mesh_generator.SavePlainData(nodes_file, complete_simples_file, complete_mat_file);
                            Program.KIS.NodesFile             = nodes_file;
                            Program.KIS.CompleteSimplexesFile = complete_simples_file;
                            Program.KIS.CompleteMaterialsFile = complete_mat_file;

                            sw.SetStatus("Saving plain model data (reconstruction mesh)...");
                            this.mesh_generator.SavePlainElectrodesData(String.Empty, electr_simples_file, electr_mat_file);
                            Program.KIS.ElectrodesSimplexesFile = electr_simples_file;
                            Program.KIS.ElectrodesMaterialsFile = electr_mat_file;

                            sw.SetStatus("Saving plain model data (reconstruction mesh)...");
                            this.mesh_generator.SavePlainReconstructionData(String.Empty, reco_simples_file, reco_mat_file);
                            Program.KIS.ReconstructionSimplexesFile = reco_simples_file;
                            Program.KIS.ReconstructionMaterialsFile = reco_mat_file;
                        }
                        else
                        {
                            // model winreco
                            //sw.SetStatus("Saving plain model data (complete mesh)...");
                            //this.mesh_generator.SavePlainData(nodes_file, complete_simples_file, complete_mat_file);
                            //Program.Kis.NodesFile = nodes_file;
                            //Program.Kis.CompleteSimplexesFile = complete_simples_file;
                            //Program.Kis.CompleteMaterialsFile = complete_mat_file;

                            // 16.02.2010: pomysł Radka
                            sw.SetStatus("Saving plain model data (reconstruction mesh)...");
                            this.mesh_generator.SavePlainElectrodesData(nodes_file, reco_simples_file, reco_mat_file);
                            Program.KIS.NodesFile = nodes_file;
                            Program.KIS.ReconstructionSimplexesFile = reco_simples_file;
                            Program.KIS.ReconstructionMaterialsFile = reco_mat_file;
                        }
                    }

                    xml_type.InnerText = Program.KIS.Mode.ToString(); // 2d czy 3d?
                    doc.Save(xml_filename);

                    if (!this.edtPath.Items.Contains(this.edtPath.Text))
                    {
                        this.edtPath.Items.Add(this.edtPath.Text);
                    }

                    sw.Close();
                    TimeSpan ts = DateTime.Now - start_time;

                    this.ShowMeshStatistics(ts);
                }
                catch (Exception ex)
                {
                    sw.Close();
                    Program.KIS.Clear();
                    MessageBox.Show("An exception has occured during mesh generation process:\n" + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            Program.KIS.ReturnCode = 1;
            Program.KIS.Save();

            this.btnGenerateData.Enabled = true;
        }