Example #1
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            toggle = !toggle;
            if (toggle)
            {
                string layername = "Heating Network";

                // Get all of the objects on the layer. If layername is bogus, you will
                // just get an empty list back
                Rhino.DocObjects.RhinoObject[] rhobjs = doc.Objects.FindByLayer(layername);
                if (rhobjs == null || rhobjs.Length < 1)
                {
                    return(Result.Cancel);
                }

                var curves = new Curve[rhobjs.Length];

                for (int i = 0; i < rhobjs.Length; i++)
                {
                    GeometryBase geom = rhobjs[i].Geometry;
                    Curve        x    = geom as Curve;
                    if (x != null && x.IsValid)
                    {
                        curves[i] = x;
                    }
                }

                OptionDouble tol = new OptionDouble(RhinoDoc.ActiveDoc.ModelAbsoluteTolerance, true, 0.0);

                CurvesTopology crvTopology = new CurvesTopology(curves, tol.CurrentValue);

                ids = CurvesTopologyPreview.Mark(crvTopology, Color.LightBlue, Color.LightCoral, Color.GreenYellow);

                return(Result.Success);
            }
            else
            {
                EndOperations(ids);
                return(Result.Success);
            }
        }
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            SearchMode sm = SearchMode.CurveLength;

            Curve[]      curves;
            OptionToggle tog = new OptionToggle(false, "Hide", "Show");
            OptionDouble tol = new OptionDouble(RhinoDoc.ActiveDoc.ModelAbsoluteTolerance, true, 0.0);

            using (CurvesGetter getLines = new CurvesGetter("Select curves meeting at endpoints. Press Enter when done"))
            {
                for ( ; ;)
                {
                    getLines.ClearCommandOptions();
                    getLines.EnableClearObjectsOnEntry(false);
                    int showInt = getLines.AddOptionToggle("Topology", ref tog);
                    int tolInt  = getLines.AddOptionDouble("Tolerance", ref tol);
                    int modeInt = GetterExtension.AddEnumOptionList(getLines, sm);

                    if (getLines.Curves(1, 0, out curves))
                    {
                        break;
                    }
                    else
                    {
                        if (getLines.Result() == GetResult.Option)
                        {
                            if (getLines.Option().Index == modeInt)
                            {
                                sm = GetterExtension.RetrieveEnumOptionValue <SearchMode>
                                         (getLines.Option().CurrentListOptionIndex);
                            }
                            continue;
                        }
                        else
                        {
                            RhinoApp.WriteLine("Less than three lines were selected");
                            return(Result.Cancel);
                        }
                    }
                }
            }
            CurvesTopology crvTopology = new CurvesTopology(curves, tol.CurrentValue);

            Guid[] ids = null;
            if (tog.CurrentValue)
            {
                ids = CurvesTopologyPreview.Mark(crvTopology, Color.LightBlue, Color.LightCoral);
            }

            int walkFromIndex;

            using (var getStart = new TrackingPointGetter("Select the start point of the walk on the curves", crvTopology))
            {
                if (getStart.GetPointOnTopology(out walkFromIndex) != Result.Success)
                {
                    EndOperations(ids);
                    return(Result.Cancel);
                }
            }

            Result wasSuccessful = Result.Cancel;

            for (; ;)
            {
                int      walkToIndex;
                double[] distances;
                using (var getEnd = new TrackingPointGetter("Select the end point", crvTopology, walkFromIndex, sm))
                {
                    if (getEnd.GetPointOnTopology(out walkToIndex) != Result.Success)
                    {
                        break;
                    }
                    distances = getEnd.DistanceCache;
                }

                if (walkFromIndex == walkToIndex)
                {
                    RhinoApp.WriteLine("Start and end points are equal");
                    EndOperations(ids);
                    return(Result.Nothing);
                }

                PathMethod pathSearch = PathMethod.FromMode(sm, crvTopology, distances);

                int[]  nIndices, eIndices;
                bool[] eDirs;
                double totLength;
                Curve  c =
                    pathSearch.Cross(walkFromIndex, walkToIndex, out nIndices, out eIndices, out eDirs, out totLength);

                if (c != null && c.IsValid)
                {
                    if (tog.CurrentValue)
                    {
                        RhinoApp.WriteLine("Vertices: {0}", FormatNumbers(nIndices));
                        RhinoApp.WriteLine("Edges: {0}", FormatNumbers(eIndices));
                    }

                    var  a = RhinoDoc.ActiveDoc.CreateDefaultAttributes();
                    Guid g = RhinoDoc.ActiveDoc.Objects.AddCurve(c, a);

                    var obj = RhinoDoc.ActiveDoc.Objects.Find(g);
                    if (obj != null)
                    {
                        obj.Select(true);
                        wasSuccessful = Result.Success;
                        walkFromIndex = walkToIndex;
                    }
                    else
                    {
                        RhinoApp.WriteLine("An error occurred while adding the new polycurve.");
                        wasSuccessful = Result.Failure;
                        break;
                    }
                }
                else
                {
                    RhinoApp.WriteLine("No path was found. Nodes are isolated.");
                    wasSuccessful = Result.Nothing;
                }
            }

            EndOperations(ids);
            return(wasSuccessful);
        }
 private static void EndOperations(Guid[] ids)
 {
     CurvesTopologyPreview.Unmark(ids);
     RhinoDoc.ActiveDoc.Views.Redraw();
 }