Exemplo n.º 1
0
        /// <summary>
        /// Popu[ file choose dialog and next call sutable import method.
        /// </summary>
        public void Import()
        {
            var label     = LeanLocalization.GetTranslation("Export").Text;
            var fileNames = StandaloneFileBrowser.OpenFilePanel(label, "", "stl", false);

            if (fileNames.Length == 0)
            {
                return;
            }

            if (Path.GetExtension(fileNames[0]).ToLower() == ".stl")
            {
                if (fileNames[0].Contains("file:"))
                {
                    fileNames[0] = fileNames[0].Substring(7);
                }
                fileNames[0] = Decode(fileNames[0]);

                try
                {
                    var meshes = STLImporter.Import(fileNames[0]);
                    var brick  = BrickBuilder.Instansiate(meshes);
                }
                catch (Exception e)
                {
                    Debug.Log(e.Message);

                    Dialog.ShowInfo(
                        LeanLocalization.GetTranslation("ImportFailed").Text,
                        DialogButtonType.ok);
                }
            }
        }
Exemplo n.º 2
0
 public void ImportBinaryTest()
 {
     Mesh[] meshes = STLImporter.Import(string.Format("{0}/Cylinder_BINARY_RH.stl", TEST_MODELS));
     Assert.IsTrue(meshes != null);
     Assert.AreEqual(1, meshes.Length);
     Assert.AreEqual(240, meshes[0].triangles.Length);
     Assert.AreEqual(240, meshes[0].vertexCount);
 }
Exemplo n.º 3
0
 public void ImportBinaryWithHeadersTest()
 {
     Mesh[] meshes = STLImporter.Import(string.Format("{0}/CubedShape_BINARY_H.stl", TEST_MODELS));
     Assert.IsTrue(meshes != null);
     Assert.AreEqual(1, meshes.Length);
     Assert.AreEqual(204, meshes[0].triangles.Length);
     Assert.AreEqual(204, meshes[0].vertexCount);
 }
Exemplo n.º 4
0
        public void ExportMultipleTest()
        {
            GameObject a = GameObject.CreatePrimitive(PrimitiveType.Cube);
            GameObject b = GameObject.CreatePrimitive(PrimitiveType.Cube);

            a.transform.position      = Vector3.right;
            b.transform.position      = new Vector3(3f, 5f, 2.4f);
            b.transform.localRotation = Quaternion.Euler(new Vector3(45f, 45f, 10f));

            if (!Directory.Exists(TEMP_FILE_DIR))
            {
                Directory.CreateDirectory(TEMP_FILE_DIR);
            }

            string temp_model_path = string.Format("{0}/multiple.stl", TEMP_FILE_DIR);

            STLExporter.Export(temp_model_path, new GameObject[] { a, b }, FileType.Binary);

            // Comparing binary files isn't great
            // Assert.IsTrue(CompareFiles(string.Format("{0}/CompositeCubes_BINARY.stl", TEST_MODELS), temp_model_path));
            Mesh[] expected = STLImporter.Import(string.Format("{0}/CompositeCubes_BINARY.stl", TEST_MODELS));
            Mesh[] results  = STLImporter.Import(temp_model_path);

            Assert.IsTrue(expected != null);
            Assert.IsTrue(results != null);

            Assert.IsTrue(expected.Length == 1);
            Assert.IsTrue(results.Length == 1);

            Assert.AreEqual(expected[0].vertexCount, results[0].vertexCount);
            Assert.AreEqual(expected[0].triangles, results[0].triangles);

            // Can't use Assert.AreEqual(positions, normals, uvs) because Vec3 comparison is subject to floating point inaccuracy
            for (int i = 0; i < expected[0].vertexCount; i++)
            {
                Assert.Less(Vector3.Distance(expected[0].vertices[i], results[0].vertices[i]), .00001f);
                Assert.Less(Vector3.Distance(expected[0].normals[i], results[0].normals[i]), .00001f);
            }

            GameObject.DestroyImmediate(a);
            GameObject.DestroyImmediate(b);

            Directory.Delete(TEMP_FILE_DIR, true);
        }
Exemplo n.º 5
0
 private void FileOpen_Click(object sender, EventArgs e)
 {
     if (openFileDialog1.ShowDialog() == DialogResult.OK)
     {
         STLImporter importer = new STLImporter(model);
         try
         {
             importer.Import(openFileDialog1.FileName);
             SetupScaleAndPosition();
         }
         catch (Exception ex)
         {
             MessageBox.Show($"Не могу открыть файл!\n{openFileDialog1.FileName}\n{ex.Message}", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
             model.Clear();
             scale    = 1f;
             position = Vector3.Zero;
         }
         glControl1.Invalidate();
     }
 }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            Console.Title = "FrameCAM";

            OpenFileDialog oDialog = new OpenFileDialog();

            oDialog.Filter = "STL Files |*.stl|All Files |*.*";

            if (oDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            Console.Write("Import -> ");

            STLImporter SImport = new STLImporter(oDialog.FileName);

            Console.WriteLine("OK");

            List <Triangle> usedTris = new List <Triangle>();
            List <Triangle> usedNail = new List <Triangle>();


            for (int i = 0; i < SImport.AllTriangles.Length; i++)
            {
                if (CheckNailPos(SImport.AllTriangles[i]))
                {
                    usedNail.Add(SImport.AllTriangles[i]);
                }

                if (!CheckYCoord(SImport.AllTriangles[i]))
                {
                    usedTris.Add(SImport.AllTriangles[i]);
                }
            }

            if (usedTris.Count % 2 != 0)
            {
                Console.WriteLine("Invalid Geometry.");
                Console.ReadLine();
                return;
            }

            Console.WriteLine("Detected " + usedTris.Count / 2 + " Wood Pieces!");

            List <Vector3> beamPos = new List <Vector3>();
            List <Vector3> beamSiz = new List <Vector3>();

            Console.WriteLine("Calculating Beam Placements");
            for (int i = 0; i < usedTris.Count; i++)
            {
                float ZSize = 0;
                float ZPos  = getZValue(usedTris[i], out ZSize);

                float XSize = 0;
                float XPos  = getXValue(usedTris[i], out XSize);

                bool ZW = (Math.Abs(ZSize - 0.0381f) < 0.01f);
                bool XW = (Math.Abs(XSize - 0.0381f) < 0.01f);

                if (ZW && XW)
                {
                    throw new Exception("Invalid Geometry!");
                }
                if (!ZW && !XW)
                {
                    throw new Exception("Invalid Geometry!");
                }


                if (!isPresent(beamPos, XPos, ZPos))
                {
                    beamPos.Add(new Vector3(XPos, ZPos, 0));
                    beamSiz.Add(new Vector3(XSize, ZSize, 0));
                }
            }

            List <Nailable> nailPos = new List <Nailable>();

            Console.WriteLine("Calculating Nailer");

            for (int i = 0; i < usedNail.Count; i++)
            {
                nailPos.Add(ComputeNail(usedNail[i]));
            }

            nailPos.Sort(delegate(Nailable x, Nailable y) { return(x.nailOrder.CompareTo(y.nailOrder)); });

            Console.WriteLine("CAM Processing OK\nPlease select a path to save gcode file");

            SaveFileDialog sDialog = new SaveFileDialog();

            sDialog.Filter = "Text Files |*.txt| All Files|*.*";

            if (sDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            List <string> GInstr = new List <string>();

            Vector3 GoUpPos = new Vector3(0, 0, 0);

            for (int i = 0; i < beamPos.Count; i++)
            {
                beamPos[i] = beamPos[i] + BeamOffset;
            }

            for (int i = 0; i < beamPos.Count; i++)
            {
                bool  SW  = Math.Abs(beamSiz[i].x - 0.0381f) < 0.01f;
                float ROT = SW ? 1.57f : 0f;

                //release previous piece and spawn the new piece
                GInstr.Add("Instructions.Add(new GCode(false, " + (SW ? beamSiz[i].y : beamSiz[i].x) + "f)); //Release Previous Piece");

                //go up incase of last piece
                GInstr.Add("Instructions.Add(new GCode(" + GoUpPos.y + "f, " + GoUpPos.x + "f, 0f, " + GoUpPos.z + "f)); //Go Up");

                //Get The Piece
                GInstr.Add("Instructions.Add(new GCode(0, 2, 0, 0)); //Go To Piece Pickup");
                GInstr.Add("Instructions.Add(new GCode(0, 2, 0.62f, 0)); //Lower To Piece");

                //Pickup the piece
                GInstr.Add("Instructions.Add(new GCode(true, 12345f)); //Pickup Piece");
                GInstr.Add("Instructions.Add(new GCode(0, 2, 0, 0)); //Go up");

                //goto pos
                GInstr.Add("Instructions.Add(new GCode(" + beamPos[i].y + "f, " + beamPos[i].x + "f, 0f, " + ROT + "f)); //Goto Piece Pos");

                //Lower Piece
                GInstr.Add("Instructions.Add(new GCode(" + beamPos[i].y + "f, " + beamPos[i].x + "f, 0.49f, " + ROT + "f)); //Lower the piece");
                GoUpPos = new Vector3(beamPos[i].x, beamPos[i].y, ROT);

                //loop end repeat ->
            }

            //release last piece and go home
            GInstr.Add("Instructions.Add(new GCode(false, " + "0" + "f)); //Release Previous Piece");
            GInstr.Add("Instructions.Add(new GCode(" + GoUpPos.y + "f, " + GoUpPos.x + "f, 0f, " + GoUpPos.z + "f)); //Go Up");
            GInstr.Add("Instructions.Add(new GCode(0, 0, 0, 0)); //go home");


            WriteNailTargets(nailPos, GInstr);



            File.WriteAllLines(sDialog.FileName, GInstr.ToArray());
            Console.WriteLine("File Written!");
            Console.ReadLine();
        }
Exemplo n.º 7
0
        private void Form1_Load(object sender, EventArgs e)
        {
            #region StartupWindow
            StartupForm StartForm = new StartupForm(Environment.GetCommandLineArgs());
            StartForm.ShowDialog();


            if (StartupForm.ApplicationTerminated)
            {
                this.Close();
                Application.Exit();
                return;
            }
            #endregion

            #region WindowSettings
            this.Size = new Size(StartupForm.W, StartupForm.H);
            int WindowWidth  = StartupForm.W - ClientSize.Width;
            int WindowHeight = StartupForm.H - ClientSize.Height;
            this.Size = new Size(StartupForm.W + WindowWidth, StartupForm.H + WindowHeight);
            this.SetStyle(ControlStyles.StandardDoubleClick, false);
            FBCaching        = StartupForm.NormalsInterpolated;
            this.MouseWheel += Form1_MouseWheel;
            #endregion

            #region FullscreenSettings
            if (StartupForm.FullscreenSelected)
            {
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.WindowState     = FormWindowState.Maximized;
                StartupForm.W        = this.Width;
                StartupForm.H        = this.Height;
            }
            #endregion



            #region VertexBufferInitialization
            STLImporter Importer = new STLImporter(StartupForm.FilePath);

            float[] vertexpoints = new float[Importer.AllTriangles.Length * 3 * 3];
            float[] normalBuffer = new float[Importer.AllTriangles.Length * 3];
            for (int i = 0; i < Importer.AllTriangles.Length; i++)
            {
                vertexpoints[i * 9]     = Importer.AllTriangles[i].vertex1.x;
                vertexpoints[i * 9 + 1] = Importer.AllTriangles[i].vertex1.y;
                vertexpoints[i * 9 + 2] = Importer.AllTriangles[i].vertex1.z;
                vertexpoints[i * 9 + 3] = Importer.AllTriangles[i].vertex2.x;
                vertexpoints[i * 9 + 4] = Importer.AllTriangles[i].vertex2.y;
                vertexpoints[i * 9 + 5] = Importer.AllTriangles[i].vertex2.z;
                vertexpoints[i * 9 + 6] = Importer.AllTriangles[i].vertex3.x;
                vertexpoints[i * 9 + 7] = Importer.AllTriangles[i].vertex3.y;
                vertexpoints[i * 9 + 8] = Importer.AllTriangles[i].vertex3.z;
                normalBuffer[i * 3]     = Importer.AllTriangles[i].normals.x;
                normalBuffer[i * 3 + 1] = Importer.AllTriangles[i].normals.y;
                normalBuffer[i * 3 + 2] = Importer.AllTriangles[i].normals.z;
            }
            #endregion

            ModelCenter = CalculateCenterOfModel(ref vertexpoints, out DistanceCenter);
            //   vertexpoints = STLImporter.AverageUpFaceNormalsAndOutputVertexBuffer(Importer.AllTriangles, 45);

            NormalBuffer = new GLBuffer(normalBuffer, 3, MemoryLocation.Heap);
            VertexBuffer = new GLBuffer(vertexpoints, 3, MemoryLocation.Heap);

            NormalBuffer = new GLBuffer(renderX.PrimitiveTypes.CubeNormals(), 3, MemoryLocation.Heap);
            VertexBuffer = new GLBuffer(renderX.PrimitiveTypes.Cube(), 5, MemoryLocation.Heap);


            //GL._DebugBlit
            StandardShader = new Shader(null, BasicShader, GLRenderMode.TriangleFlat);
            //StandardShader.SetOverrideAttributeCopy(true);
            // Skybox = new GLCubemap("skybox_data");


            DisplayTexture = new Shader(CubeShader, TextureShader, GLRenderMode.Triangle);
            //  DisplayTexture.SetOverrideAttributeCopy(false);
            //  DisplayTexture.SetOverrideAttributeCount(3);
            // GL.BlitInto

            texture2d    = new GLTexture("container2.png", MemoryLocation.Heap, DuringLoad.Flip);
            TEXTURE_ADDR = (int *)texture2d.GetAddress();

            textureHeight         = texture2d.Height;
            textureWidthMinusOne  = texture2d.Width - 1;
            textureHeightMinusOne = texture2d.Height - 1;


            #region MemoryAddresses
            nbAddr       = (float *)NormalBuffer.GetAddress();
            vertexpoints = null;

            #endregion

            #region CubeObject
            CubeVBO    = new GLBuffer(renderX.PrimitiveTypes.Cube(), 5, MemoryLocation.Heap);
            cubeShader = new Shader(CubeVS, CubeFS, GLRenderMode.TriangleFlat);
            cubeShader.SetOverrideAttributeCount(0);
            #endregion

            #region 3D_Grid
            float[] vpoints = new float[] {
                0, 0, 0, 9, 0, 0,
                0, 0, 1, 9, 0, 1,
                0, 0, 2, 9, 0, 2,
                0, 0, 3, 9, 0, 3,
                0, 0, 4, 9, 0, 4,
                0, 0, 5, 9, 0, 5,
                0, 0, 6, 9, 0, 6,
                0, 0, 7, 9, 0, 7,
                0, 0, 8, 9, 0, 8,
                0, 0, 9, 9, 0, 9,

                0, 0, 0, 0, 0, 9,
                1, 0, 0, 1, 0, 9,
                2, 0, 0, 2, 0, 9,
                3, 0, 0, 3, 0, 9,
                4, 0, 0, 4, 0, 9,
                5, 0, 0, 5, 0, 9,
                6, 0, 0, 6, 0, 9,
                7, 0, 0, 7, 0, 9,
                8, 0, 0, 8, 0, 9,
                9, 0, 0, 9, 0, 9
            };

            LineBuffer = new GLBuffer(vpoints, 3, MemoryLocation.Heap);
            LineShader = new Shader(GridShaderVS, GridShaderFS, GLRenderMode.Line, GLExtraAttributeData.XYZ_CameraSpace);

            #endregion

            #region CameraIndicator
            CameraIndicator = new Shader(CIVS, null, GLRenderMode.TriangleGouraud);
            CameraIndicator.SetOverrideCameraTransform(true);
            CameraIndicator.SetOverrideAttributeCopy(true);

            //Inner Part
            CICVertex  = new GLBuffer(renderX.PrimitiveTypes.CMI(), 3, MemoryLocation.Heap);
            CICNormals = new GLBuffer(renderX.PrimitiveTypes.CMINormals(), 3, MemoryLocation.Heap);

            //Outer Part
            CIPVertex  = new GLBuffer(renderX.PrimitiveTypes.CMS(), 3, MemoryLocation.Heap);
            CIPNormals = new GLBuffer(renderX.PrimitiveTypes.CMSNormals(), 3, MemoryLocation.Heap);

            CIC_Normals = (float *)CICNormals.GetAddress();
            CIP_Normals = (float *)CIPNormals.GetAddress();
            #endregion

            #region Post-Processing
            SSRShader = new Shader(null, SSR_Fragment, GLRenderMode.Triangle, GLExtraAttributeData.XYZ_XY_Both);

            SSRShaderPost = new Shader(SSR_Pass);
            #endregion

            #region MiniGLIndicator
            MiniGL = new renderX(130, 128);
            MiniGL.SetMatrixData(90, 10);
            MiniGL.SelectShader(CameraIndicator);
            MiniGL.SetFaceCulling(true, false);
            MiniGL.InitializeClickBuffer();
            MiniGL.SetClickBufferWrite(true);
            #endregion

            #region renderX_Initialization
            GL = new renderX(StartupForm.W, StartupForm.H, this.Handle);
            GL.SelectBuffer(VertexBuffer);
            GL.SelectShader(StandardShader);
            GL.SetMatrixData(90, 10);


            #endregion

            #region GLSettings
            cachedBuffer = new GLCachedBuffer(GL);

            GL.SetWireFrameOFFSET(-0.1f);
            GL.SetFaceCulling(true, false);

            #endregion

            //  GL.SetLinkedWireframe(true, 255, 255, 255);
            //  GL.SetViewportScaling(1920, 1080, InterpolationMethod.NearestNeighbour);
            GL.SetDebugWireFrameColor(255, 255, 255);
            // GL.SetLineAntiAliasing(true);
            GL.SetLineAntiAliasing(true);
            GL.SetLineThickness(1);
            // GL.SetLineThickness(3);
            //   GL.SetWireFrameOFFSET(-5000f);

            //    GL.SetLineAntiAliasing(true);

            //  GL.SetLinkedWireframe(true, 0, 0, 255);
            GL.InitializeVignetteBuffer(VignetteShader);

            //  cameraPosition = new Vector3(-1.16f, 18f, -33.5f);
            //   cameraRotation = new Vector3(0,0,90);

            cameraPosition = new Vector3(0, 0, -50);
            cameraRotation = new Vector3(0, 0, 0);


            #region RenderThreadStart
            RT              = new RenderThread(144);
            RT.RenderFrame += RT_RenderFrame;
            RT.Start();
            #endregion
        }