Beispiel #1
0
        /// <summary>
        /// Destroys the NormalSwitcherControl and resets the GUI-elements.
        /// </summary>
        /// <param name="sender">closeToolStripMenuItem</param>
        /// <param name="e">Standard EventArgs</param>
        private void CloseFile(object sender, EventArgs e)
        {
            if (currentFile != "")
            {
                if (changed && (MessageBox.Show("Do you want to save your changes?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
                {
                    if ((triangleList.Count == 0) && (MessageBox.Show("You need at least one triangle to save!", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel))
                    {
                        throw new Exception();
                    }
                    else if (triangleList.Count > 0)
                    {
                        this.SaveAs(sender, e);
                    }
                }

                splitContainer2.Panel2.Controls.Clear();
                visualization          = null;
                originTrackBar.Visible = false;
                history.Clear();
                currentSelection.Clear();
                currentFile        = "";
                allButton.Enabled  = false;
                undoButton.Enabled = false;

                triangleList = backupList = null;
                parser       = new STLParser();
                changed      = false;
                triVertices  = new float[9];
                corners      = new float[9];

                tabControl1.TabPages.Clear();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Initializes the NormalSwitcherForm and opens the file given by <paramref name="file"/>
        /// </summary>
        /// <param name="file">Path of the file to be displayed</param>
        internal NormalSwitcherForm(string file)
        {
            InitializeComponent();

            BindEvents();

            StreamReader reader = new StreamReader(file);

            try {
                parser.Parse(reader);
                triangleList = parser.TriangleList;
                backupList   = triangleList.Copy();
                SetOrigin();
                originTrackBar.Visible = true;
                InitVisualization();

                currentFile       = file;
                allButton.Enabled = true;

                InitializePages();
            } catch (Exception exception) {
                MessageBox.Show(exception.Message, "Error");
            } finally {
                reader.Close();
            }
        }
        /// <summary>
        /// Returns a copy of the TriangleList.
        /// </summary>
        /// <returns>Copy of the TriangleList</returns>
        internal TriangleList Copy()
        {
            TriangleList tri = new TriangleList();

            for (int i = 0; i < this.Count; i++)
            {
                tri.AddTriangle(this[i].Copy());
            }
            tri.Finish();
            return(tri);
        }
Beispiel #4
0
        /// <summary>
        /// Sets all triangles back to the values from the STL-file.
        /// </summary>
        /// <param name="sender">resetToolStripMenuItem or resetButton</param>
        /// <param name="e">Standard EventArgs</param>
        private void Reset(object sender, EventArgs e)
        {
            triangleList = null;
            triangleList = backupList.Copy();
            currentSelection.Clear();
            history.Clear();

            undoButton.Enabled = false;
            changed            = false;
            visualization.SetColorArray();
            visualization.SetPickingColors();
            SetOrigin();
            SetPanelsChanged();
            (tabControl1.SelectedTab as Page).UpdateTab();
        }
        /// <summary>
        /// Writes the data (normal vectors and vertices of the triangles) to the chosen file in binary-mode.
        /// </summary>
        /// <param name="filename">Path of the file to be saved</param>
        /// <param name="normalArray">The normal vectors</param>
        /// <param name="vertexArray">The vertices</param>
        internal static void WriteToBinary(string filename, TriangleList triangleList)
        {
            FileStream   fs = new FileStream(filename, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);

            try {
                byte   abc       = 0;
                byte[] headerArr = new byte[80];
                GetHeader().CopyTo(headerArr, 0);

                for (int c = 0; c < 80; c++)
                {
                    bw.Write(headerArr[c]);
                }

                bw.Write((UInt32)(triangleList.Count));

                for (int i = 0; i < triangleList.Count; i++)
                {
                    // Normal vector
                    for (int j = 0; j < 3; j++)
                    {
                        bw.Write(triangleList[i][3][j]);
                    }

                    // Next three are vertices
                    for (int k = 0; k < 3; k++)
                    {
                        for (int h = 0; h < 3; h++)
                        {
                            bw.Write(triangleList[i][k][h]);
                        }
                    }

                    // Last two bytes are only to fill up to 50 bytes
                    bw.Write(abc);
                    bw.Write(abc);
                }
            } catch (Exception exception) {
                MessageBox.Show(exception.Message, "Error");
            } finally {
                fs.Close();
                bw.Close();
            }
        }
        /// <summary>
        /// Writes the data (normal vectors and vertices of the triangles) to the chosen file in ASCII-mode.
        /// </summary>
        /// <param name="filename">Path of the file to be saved</param>
        /// <param name="normalArray">The normal vectors</param>
        /// <param name="vertexArray">The vertices</param>
        internal static void WriteToASCII(string filename, TriangleList triangleList)
        {
            StreamWriter sw = new StreamWriter(filename);

            try {
                sw.WriteLine("solid ");
                for (int i = 0; i < triangleList.Count; i++)
                {
                    sw.WriteLine("  facet normal " + triangleList[i][3][0] + " " + triangleList[i][3][1] + " " + triangleList[i][3][2] + " ");
                    sw.WriteLine("    outer loop");
                    for (int j = 0; j < 3; j++)
                    {
                        sw.WriteLine("      vertex " + triangleList[i][j][0] + " " + triangleList[i][j][1] + " " + triangleList[i][j][2] + " ");
                    }
                    sw.WriteLine("    endloop");
                    sw.WriteLine("  endfacet");
                }
                sw.WriteLine("endsolid ");
            } catch (Exception exception) {
                MessageBox.Show(exception.Message, "Error");
            } finally {
                sw.Close();
            }
        }
Beispiel #7
0
        /// <summary>
        /// Closes a previously opened file and opens a new one.
        /// Parses the file and initializes the arrays and GUI-elements.
        /// </summary>
        /// <param name="sender">openToolStripMenuItem</param>
        /// <param name="e">Standard EventArgs</param>
        private void OpenFile(object sender, EventArgs e)
        {
            try {
                CloseFile(sender, e);

                OpenFileDialog ofd = new OpenFileDialog();
                ofd.CheckFileExists = true;
                ofd.DefaultExt      = "stl";
                ofd.Filter          = "STL Files (*.stl)|*.stl";
                ofd.Multiselect     = false;
                ofd.Title           = "Open STL-file";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    StreamReader reader = new StreamReader(ofd.FileName);
                    try {
                        parser.Parse(reader);

                        triangleList = parser.TriangleList;
                        backupList   = triangleList.Copy();
                        SetOrigin();
                        originTrackBar.Visible = true;
                        InitVisualization();

                        currentFile       = ofd.FileName;
                        allButton.Enabled = true;

                        InitializePages();
                    } catch (Exception exception) {
                        MessageBox.Show(exception.Message, "Error");
                    } finally {
                        reader.Close();
                    }
                }
                ofd.Dispose();
            } catch { }
        }