Esempio n. 1
0
        /// <summary>
        /// Generates an OpenSCAD file, along with auxilliary .STL files,
        /// that can be compiled into 3D-printable geometry for this
        /// telescope structure.
        /// </summary>
        public void GenerateSCAD()
        {
            Debug.Log("Exporting structure...");

            Directory.CreateDirectory("scad");
            Directory.CreateDirectory("scad/stls");

            // Retract the structure completely
            foreach (TelescopeSegment segment in segments)
            {
                segment.ExtendImmediate(0);
                segment.SetSmallestExtension(0.1f);
            }

            Vector3 minBounds = WorldSpaceMin - new Vector3(0.2f, 0.2f, 0.2f);

            List <string>         junctureScads = new List <string>();
            List <List <string> > shellSTLs     = new List <List <string> >();

            // First output .scad files for all junctures
            foreach (TelescopeJuncture junct in junctures)
            {
                string scad = junct.WriteSTLOfJuncture(minBounds, junct.name);
                junctureScads.Add(scad);
            }

            // Now output .stl files for all segments
            foreach (TelescopeSegment segment in segments)
            {
                List <string> segSTLs = STLWriter.WriteSTLOfShells(segment, minBounds);
                shellSTLs.Add(segSTLs);
            }

            string fname = "scad/" + name + ".scad";

            Debug.Log("Writing OpenSCAD file of telescope to " + fname);

            // Finally output a .scad file that combines all of the things we just exported
            using (StreamWriter file = new StreamWriter(fname))
            {
                file.WriteLine("union() {");

                // Import all scad files
                foreach (string scadFile in junctureScads)
                {
                    file.WriteLine("    include <" + scadFile + ">;");
                }

                for (int i = 0; i < segments.Count; i++)
                {
                    List <string> segmentSTLs = shellSTLs[i];

                    float radius = Mathf.Min(segments[i].InnermostRadius * 0.5f, segments[i].OutermostHeight * 0.5f);

                    List <string> cylinder = SCADOfCylinder(segments[i].BasePointAtCenter,
                                                            segments[i].BasePointOnSurface, radius, minBounds);

                    if (Constants.ADD_SUPPORT_CHANNELS)
                    {
                        file.WriteLine("    difference() {");
                    }

                    file.WriteLine("        union() {");
                    // Write out all of the STLs for the segments of shells
                    foreach (string stlFile in segmentSTLs)
                    {
                        file.WriteLine("            import(\"" + stlFile + "\", convexity=10);");
                    }
                    file.WriteLine("        }");

                    if (Constants.ADD_SUPPORT_CHANNELS)
                    {
                        // Subtract a cylinder to provide a channel to remove support
                        foreach (string cylLine in cylinder)
                        {
                            file.WriteLine("        " + cylLine);
                        }
                        file.WriteLine("    }");
                    }
                }

                file.WriteLine("}");
            }
        }
Esempio n. 2
0
        public void WriteShell()
        {
            string name = this.name + ".stl";

            STLWriter.WriteSTLOfMesh(mFilter.mesh, name);
        }
Esempio n. 3
0
        // Write a set of STLs for a juncture, and an OpenSCAD file that
        // compiles them all into one fused part.
        public string WriteSTLOfJuncture(Vector3 minOffset, string objName)
        {
            List <string> unionSTLs = new List <string>();
            List <string> diffSTLs  = new List <string>();
            string        bulbSTL   = "";

            // If there is a parent segment, write that
            if (parentSegment)
            {
                TelescopeShell last = parentSegment.LastShell;

                // Export them in world coordinates so that the full
                // object can just be assembled without moving pieces around
                STLWriter.VectorTransform f =
                    (v => last.transform.rotation * v + last.transform.position - minOffset);

                if (parentSegment.NumShells > 1 && parentSegment.Reversed)
                {
                    Mesh inner = last.GenerateInnerVolume(parentSegment.shells[parentSegment.NumShells - 2].getParameters(),
                                                          -Constants.WALL_THICKNESS, extraRings: Constants.CUTS_PER_CYLINDER / 2);

                    string parentVolumeSTL = objName + "-parentInner.stl";
                    STLWriter.WriteSTLOfMesh(inner, "scad/" + parentVolumeSTL, f);

                    diffSTLs.Add(parentVolumeSTL);
                }
            }

            // Write each child segment as well
            int childNum = 0;

            foreach (TelescopeSegment childSegment in childSegments)
            {
                TelescopeShell first = childSegment.FirstShell;

                STLWriter.VectorTransform f =
                    (v => first.transform.rotation * v + first.transform.position - minOffset);

                // Write a mesh for the inner volume contained in the shell,
                // so that it can be subtracted out from the juncture
                if (childSegment.NumShells > 1 && !childSegment.Reversed)
                {
                    Mesh inner = first.GenerateInnerVolume(childSegment.shells[1].getParameters(),
                                                           -Constants.WALL_THICKNESS, extraRings: Constants.CUTS_PER_CYLINDER / 2);

                    string childVolumeSTL = objName + "-childInner" + childNum + ".stl";
                    STLWriter.WriteSTLOfMesh(inner, "scad/" + childVolumeSTL, f);

                    diffSTLs.Add(childVolumeSTL);
                }
                childNum++;
            }

            // Write this piece
            if (junctureType != JunctureType.None)
            {
                STLWriter.VectorTransform f =
                    (v => transform.rotation * v + transform.position - minOffset);

                bulbSTL = objName + ".stl";
                STLWriter.WriteSTLOfMesh(mFilter.mesh, "scad/" + bulbSTL, f);
            }

            string scadFile = objName + ".scad";

            STLWriter.WriteSCADOfJunctureSTLs(bulbSTL, unionSTLs, diffSTLs, "scad/" + scadFile);
            return(scadFile);
        }