Exemple #1
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            Rhino.DocObjects.ObjectType filter = Rhino.DocObjects.ObjectType.Mesh;
            Rhino.DocObjects.ObjRef     objref = null;
            Rhino.Commands.Result       rc     = Rhino.Input.RhinoGet.GetOneObject("Select mesh to contour", false, filter, out objref);
            if (rc != Rhino.Commands.Result.Success || objref == null)
            {
                return(rc);
            }

            Rhino.Geometry.Mesh mesh = objref.Geometry() as Rhino.Geometry.Mesh;
            if (null == mesh)
            {
                return(Rhino.Commands.Result.Failure);
            }

            Rhino.Geometry.BoundingBox bbox     = mesh.GetBoundingBox(false);
            Rhino.Geometry.Point3d     start_pt = bbox.Corner(true, true, true);
            Rhino.Geometry.Point3d     end_pt   = bbox.Corner(false, true, true);
            double interval = start_pt.DistanceTo(end_pt) / 10;

            Rhino.Geometry.Curve[] curves = Rhino.Geometry.Mesh.CreateContourCurves(mesh, start_pt, end_pt, interval);
            if (null != curves && curves.Length > 0)
            {
                for (int i = 0; i < curves.Length; i++)
                {
                    doc.Objects.AddCurve(curves[i]);
                }
                doc.Views.Redraw();
            }

            return(Result.Success);
        }
Exemple #2
0
        public static void curves(int state, Rhino.DocObjects.RhinoObject geo)
        {
            //Verify geometery is being added, not transformed.
            string G00_Path    = utils.file_structure.getPathFor("G00");
            int    verifyState = Convert.ToInt32(System.IO.File.ReadAllText(G00_Path));

            if (verifyState == 3)
            {
                state = 3;
            }
            else
            {
                System.IO.File.WriteAllText(G00_Path, state.ToString());
            }

            //Grab reference point from docBox.
            Rhino.Geometry.Point3d refPoint = utils.properties.getRefPoint();
            double refPointX = refPoint.X;
            double refPointY = refPoint.Y;

            //Determine and sanitize cache file for new geometry.
            string G10_Path = utils.file_structure.getPathFor("G10");

            if (System.IO.File.Exists(G10_Path) == false)
            {
                System.IO.File.WriteAllText(G10_Path, "");
                //System.Threading.Thread.Sleep(500);
            }

            //Parse curve geometry from RhinoObject.
            Guid incomingGuid = geo.Id;

            Rhino.Geometry.Curve newCurve = null;

            int layerIndex = geo.Attributes.LayerIndex;

            Rhino.DocObjects.Layer incomingLayer = Rhino.RhinoDoc.ActiveDoc.Layers[layerIndex];
            string incomingLayerName             = incomingLayer.Name;

            Rhino.DocObjects.ObjRef oRef = new Rhino.DocObjects.ObjRef(incomingGuid);

            //Verify object is a curve, otherwise quit.
            if (oRef.Geometry().ObjectType.ToString().ToLower().Contains("curve"))
            {
                newCurve = oRef.Curve();
            }
            else
            {
                //System.IO.File.AppendAllText(G10_Path, "X|" + Environment.NewLine);
                return;
            }

            //Run curve geometry through the coin sorter.
            //All data is cached in the same file, but keyed at the beginning by type.
            //0 - linear curve
            //1 - linear polyline
            //2 - arc
            //3 - circle
            //4 - ellipse
            //5 - arbitrary curve
            //6 - complex polycurve

            //New type key:
            //0 - linear curve
            //1 - linear polyline
            //2 - any non-linear curve
            if (newCurve.Degree == 1)
            {
                if (newCurve.SpanCount == 1)
                {
                    //Treat as linear curve.
                    //utils.debug.ping(0, "Linear curve added!");

                    Rhino.Geometry.Point3d startPoint = newCurve.PointAtStart;
                    Rhino.Geometry.Point3d endPoint   = newCurve.PointAtEnd;

                    string x1 = (startPoint.X - refPointX).ToString();
                    string y1 = (startPoint.Y - refPointY).ToString();
                    string x2 = (endPoint.X - refPointX).ToString();
                    string y2 = (endPoint.Y - refPointY).ToString();

                    //utils.debug.alert(G10_Path);
                    //utils.debug.alert(incomingGuid.ToString());
                    //utils.debug.alert(incomingLayerName);

                    try
                    {
                        System.IO.File.AppendAllText(G10_Path, "0|" + incomingGuid.ToString() + "|" + incomingLayerName + "|1|" + x1 + "," + y1 + "," + x2 + "," + y2 + Environment.NewLine);
                    }
                    catch (Exception e)
                    {
                        utils.debug.alert(e.ToString());
                    }

                    //utils.debug.alert("Pausing!!");
                }
                else if (newCurve.SpanCount > 1)
                {
                    //Treat as linear polyline.
                    //utils.debug.ping(0, "Linear polyline added!");

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

                    int spanCount = newCurve.SpanCount;
                    for (int i = 0; i < spanCount; i++)
                    {
                        double activeParameter             = newCurve.SpanDomain(i).Min;
                        Rhino.Geometry.Point3d activePoint = newCurve.PointAt(activeParameter);

                        double activeX = activePoint.X - refPointX;
                        coords.Add(activeX.ToString());

                        double activeY = activePoint.Y - refPointY;
                        coords.Add(activeY.ToString());

                        if (i == spanCount - 1)
                        {
                            //On reaching final span, also record endpoint.
                            double finalParameter             = newCurve.SpanDomain(i).Max;
                            Rhino.Geometry.Point3d finalPoint = newCurve.PointAt(finalParameter);

                            double finalX = finalPoint.X - refPointX;
                            coords.Add(finalX.ToString());

                            double finalY = finalPoint.Y - refPointY;
                            coords.Add(finalY.ToString());
                        }
                    }

                    string coordInfo = string.Join(",", coords);

                    System.IO.File.AppendAllText(G10_Path, "1|" + incomingGuid.ToString() + "|" + incomingLayerName + "|" + newCurve.SpanCount.ToString() + "|" + coordInfo + Environment.NewLine);
                }
            }
            else if (newCurve.Degree > 1)
            {
                //Generate degree 3 approximation of incoming curve. (Low tolerance makes it more than "good enough")
                Rhino.Geometry.BezierCurve[] bCurveSpans = Rhino.Geometry.BezierCurve.CreateCubicBeziers(newCurve, 0.01, 0.01);
                int bCurveCount = bCurveSpans.Length;

                List <string> bPointData  = new List <string>();
                List <double> bPointCache = new List <double>();

                //Cache point info.
                for (int i = 0; i < bCurveCount; i++)
                {
                    //Starting point caching process.
                    if (i == 0)
                    {
                        //Parse anchor point data.
                        Rhino.Geometry.Point3d activeAnchor = bCurveSpans[i].GetControlVertex3d(0);
                        double activeAnchorX = activeAnchor.X - refPointX;
                        double activeAnchorY = activeAnchor.Y - refPointY;
                        bPointCache.Add(activeAnchorX);
                        bPointCache.Add(activeAnchorY);

                        //Parse left direction data. For starting point, this is the same as the anchor.
                        bPointCache.Add(activeAnchorX);
                        bPointCache.Add(activeAnchorY);

                        //Parse right direction data.
                        Rhino.Geometry.Point3d activeRightDirection = bCurveSpans[i].GetControlVertex3d(1);
                        double activeRightDirectionX = activeRightDirection.X - refPointX;
                        double activeRightDirectionY = activeRightDirection.Y - refPointY;
                        bPointCache.Add(activeRightDirectionX);
                        bPointCache.Add(activeRightDirectionY);

                        //Push packaged point data to final list.
                        string bPoint = string.Join(":", bPointCache);
                        bPointData.Add(bPoint);
                        bPointCache.Clear();
                    }
                    //Intermediate point caching process.
                    else
                    {
                        //Parse anchor point data.
                        Rhino.Geometry.Point3d activeAnchor = bCurveSpans[i].GetControlVertex3d(0);
                        double activeAnchorX = activeAnchor.X - refPointX;
                        double activeAnchorY = activeAnchor.Y - refPointY;
                        bPointCache.Add(activeAnchorX);
                        bPointCache.Add(activeAnchorY);

                        //Parse left direction data.
                        Rhino.Geometry.Point3d activeLeftDirection = bCurveSpans[i - 1].GetControlVertex3d(2);
                        double activeLeftDirectionX = activeLeftDirection.X - refPointX;
                        double activeLeftDirectionY = activeLeftDirection.Y - refPointY;
                        bPointCache.Add(activeLeftDirectionX);
                        bPointCache.Add(activeLeftDirectionY);

                        //Parse right direction data.
                        Rhino.Geometry.Point3d activeRightDirection = bCurveSpans[i].GetControlVertex3d(1);
                        double activeRightDirectionX = activeRightDirection.X - refPointX;
                        double activeRightDirectionY = activeRightDirection.Y - refPointY;
                        bPointCache.Add(activeRightDirectionX);
                        bPointCache.Add(activeRightDirectionY);

                        //Push packaged point data to final list.
                        string bPoint = string.Join(":", bPointCache);
                        bPointData.Add(bPoint);
                        bPointCache.Clear();
                    }
                }

                //Cache endpoint info after loop finishes.
                //Parse anchor point data.
                Rhino.Geometry.Point3d lastActiveAnchor = bCurveSpans[bCurveCount - 1].GetControlVertex3d(3);
                double lastActiveAnchorX = lastActiveAnchor.X - refPointX;
                double lastActiveAnchorY = lastActiveAnchor.Y - refPointY;
                bPointCache.Add(lastActiveAnchorX);
                bPointCache.Add(lastActiveAnchorY);

                //Parse left direction data.
                Rhino.Geometry.Point3d lastActiveLeftDirection = bCurveSpans[bCurveCount - 1].GetControlVertex3d(2);
                double lastActiveLeftDirectionX = lastActiveLeftDirection.X - refPointX;
                double lastActiveLeftDirectionY = lastActiveLeftDirection.Y - refPointY;
                bPointCache.Add(lastActiveLeftDirectionX);
                bPointCache.Add(lastActiveLeftDirectionY);

                //Parse right direction data. For the ending point, this is the same as the anchor.
                bPointCache.Add(lastActiveAnchorX);
                bPointCache.Add(lastActiveAnchorY);

                //Push packaged point data to final list.
                string lastBPoint = string.Join(":", bPointCache);
                bPointData.Add(lastBPoint);
                bPointCache.Clear();

                //Finalize and record data.
                string coordInfo = string.Join(";", bPointData);

                System.IO.File.AppendAllText(G10_Path, "2|" + incomingGuid.ToString() + "|" + incomingLayerName + "|" + newCurve.SpanCount.ToString() + "|" + coordInfo + Environment.NewLine);
            }
        }
Exemple #3
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            Rhino.DocObjects.ObjectType filter = Rhino.DocObjects.ObjectType.Hatch;
            Rhino.DocObjects.ObjRef     objref = null;
            Rhino.Commands.Result       rc     = Rhino.Input.RhinoGet.GetOneObject("Select hatch to explode", false, filter, out objref);
            if (rc != Rhino.Commands.Result.Success || objref == null)
            {
                return(rc);
            }

            Rhino.Geometry.Hatch hatch = (Rhino.Geometry.Hatch)objref.Geometry();
            if (null == hatch)
            {
                return(Rhino.Commands.Result.Failure);
            }

            Rhino.Geometry.GeometryBase[] hatch_geom = hatch.Explode();
            if (null != hatch_geom)
            {
                for (int i = 0; i < hatch_geom.Length; i++)
                {
                    Rhino.Geometry.GeometryBase geom = hatch_geom[i];
                    if (null != geom)
                    {
                        switch (geom.ObjectType)
                        {
                        case Rhino.DocObjects.ObjectType.Point:
                        {
                            Rhino.Geometry.Point point = (Rhino.Geometry.Point)geom;
                            if (null != point)
                            {
                                doc.Objects.AddPoint(point.Location);
                            }
                        }
                        break;

                        case Rhino.DocObjects.ObjectType.Curve:
                        {
                            Rhino.Geometry.Curve curve = (Rhino.Geometry.Curve)geom;
                            if (null != curve)
                            {
                                doc.Objects.AddCurve(curve);
                            }
                        }
                        break;

                        case Rhino.DocObjects.ObjectType.Brep:
                        {
                            Rhino.Geometry.Brep brep = (Rhino.Geometry.Brep)geom;
                            if (null != brep)
                            {
                                doc.Objects.AddBrep(brep);
                            }
                        }
                        break;
                        }
                    }
                }
            }

            return(Rhino.Commands.Result.Success);
        }