protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            OptionDouble  amountOption = new OptionDouble(amount, 0.001, 300);
            OptionInteger triesOption  = new OptionInteger(tries, 1, 1000);

            GetObject go = new GetObject();

            go.SetCommandPrompt("Select meshes to Randomize");
            go.AddOptionDouble("Amount", ref amountOption);
            go.AddOptionInteger("Tries", ref triesOption);
            go.GeometryFilter = Rhino.DocObjects.ObjectType.Mesh;

            for (;;)
            {
                GetResult res = go.GetMultiple(1, 0);

                if (res == GetResult.Option)
                {
                    tries  = triesOption.CurrentValue;
                    amount = amountOption.CurrentValue;
                    continue;
                }

                if (go.CommandResult() != Result.Success)
                {
                    return(go.CommandResult());
                }

                break;
            }

            if (go.ObjectCount < 1)
            {
                return(Result.Failure);
            }

            foreach (var obj in go.Objects())
            {
                var rhinoMesh = obj.Mesh();

                if (rhinoMesh == null || !rhinoMesh.IsValid)
                {
                    continue;
                }

                var mesh = GopherUtil.ConvertToD3Mesh(obj.Mesh());

                g3.DMesh3 outputMeshRandom;
                GopherUtil.RandomizeMesh(mesh, out outputMeshRandom, amount, tries);

                var rhinoOutputMeshRandom = GopherUtil.ConvertToRhinoMesh(outputMeshRandom);
                doc.Objects.Replace(obj, rhinoOutputMeshRandom);
            }

            doc.Views.Redraw();

            return(Result.Success);
        }
Exemplo n.º 2
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            const ObjectType selFilter      = ObjectType.PolysrfFilter | ObjectType.Extrusion;
            bool             value          = false;
            double           preShrinkValue = ZERO_CM;

            OptionToggle shrinkToDimensions = new OptionToggle(value, "Off", "On");
            OptionDouble preShrink          = new OptionDouble(preShrinkValue, 0.0, 1.0);
            ObjRef       boxObjRef          = null;

            GetObject go = new GetObject();

            go.SetCommandPrompt("Select the box to net-ify");
            go.AddOptionToggle("Constrain", ref shrinkToDimensions);
            go.AddOptionDouble("PreShrink", ref preShrink, "Preshrink");
            go.GeometryFilter = selFilter;

            go.EnableClearObjectsOnEntry(true);
            go.EnableUnselectObjectsOnExit(true);

            // TODO: clean up this hot mess
            for (;;)
            {
                GetResult getObjResult = go.Get();
                if (getObjResult == GetResult.Object)
                {
                    boxObjRef = go.Object(0);
                    boxObjRef.Object().Select(on: true, syncHighlight: true);
                    go.EnablePreSelect(false, true);
                    go.AcceptNothing(true);
                    continue;
                }

                else if (getObjResult == GetResult.Cancel)
                {
                    return(Result.Cancel);
                }

                // Case where user presses enter
                else if (getObjResult == GetResult.Nothing)
                {
                    if (boxObjRef != null)
                    {
                        preShrinkValue = preShrink.CurrentValue;
                        if (preShrinkValue != ZERO_CM)
                        {
                            boxObjRef = shrinkBoxObj(boxObjRef, preShrinkValue);
                        }
                        drawNetFromObjRef(boxObjRef, doc, shrinkToDimensions.CurrentValue);
                        return(Result.Success);
                    }
                }
            }
        }
        protected override Result RunCommand(Rhino.RhinoDoc doc, RunMode mode)
        {
            // TODO: start here modifying the behaviour of your command.
            // ---
            RhinoApp.WriteLine("The {0} command will select the curve with same length", EnglishName);
            double tolerance = 0.001;

            Rhino.Commands.Result rc = Rhino.Commands.Result.Failure;

            using (GetObject getObjectAction = new GetObject())
            {
                getObjectAction.GeometryFilter = ObjectType.Curve;
                getObjectAction.SetCommandPrompt("Please select curves");
                var dblOption = new OptionDouble(tolerance, true, 0);
                getObjectAction.AddOptionDouble("SelectionTolerance", ref dblOption);
                getObjectAction.EnablePreSelect(true, true);
                var    result    = getObjectAction.Get();
                double refLength = 0.0;
                if (result == GetResult.Object)
                {
                    var value = getObjectAction.Object(0);
                    var crv   = value.Curve();
                    if (crv != null)
                    {
                        refLength = crv.GetLength();
                        var objects = doc.Objects.FindByObjectType(ObjectType.Curve);
                        foreach (var obj in objects)
                        {
                            var _curve = (new ObjRef(obj)).Curve();
                            if (Rhino.RhinoMath.EpsilonEquals(_curve.GetLength(), refLength, dblOption.CurrentValue))
                            {
                                obj.Select(true);
                            }
                        }
                        rc = Rhino.Commands.Result.Success;
                    }
                }
            }
            doc.Views.Redraw();
            return(rc);
        }
Exemplo n.º 4
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            bool bHavePreselectedObjects = false;

            const ObjectType geometryFilter = ObjectType.MeshFace;

            OptionDouble  minEdgeLengthOption     = new OptionDouble(minEdgeLength, 0.001, 200);
            OptionDouble  maxEdgeLengthOption     = new OptionDouble(maxEdgeLength, 0.001, 200);
            OptionDouble  constriantAngleOption   = new OptionDouble(constriantAngle, 0.001, 360);
            OptionInteger smoothStepsOptions      = new OptionInteger(smoothSteps, 0, 100);
            OptionDouble  smoothSpeedOption       = new OptionDouble(smoothSpeed, 0.01, 1.0);
            OptionDouble  projectAmountOption     = new OptionDouble(projectedAmount, 0.01, 1.0);
            OptionDouble  projectedDistanceOption = new OptionDouble(projectedDistance, 0.01, 100000.0);

            GetObject go = new GetObject();

            go.SetCommandPrompt("Select mesh faces to project onto another mesh");
            go.GeometryFilter = geometryFilter;

            go.AddOptionDouble("ConstraintAngle", ref constriantAngleOption);
            go.AddOptionDouble("MinEdge", ref minEdgeLengthOption);
            go.AddOptionDouble("MaxEdge", ref maxEdgeLengthOption);
            go.AddOptionInteger("SmoothSteps", ref smoothStepsOptions);
            go.AddOptionDouble("SmoothSpeed", ref smoothSpeedOption);

            go.GroupSelect     = true;
            go.SubObjectSelect = true;

            for (; ;)
            {
                GetResult faceres = go.GetMultiple(1, 0);

                if (faceres == GetResult.Option)
                {
                    go.EnablePreSelect(false, true);
                    continue;
                }

                else if (go.CommandResult() != Result.Success)
                {
                    return(go.CommandResult());
                }

                if (go.ObjectsWerePreselected)
                {
                    bHavePreselectedObjects = true;
                    go.EnablePreSelect(false, true);
                    continue;
                }

                break;
            }

            minEdgeLength   = minEdgeLengthOption.CurrentValue;
            maxEdgeLength   = maxEdgeLengthOption.CurrentValue;
            constriantAngle = constriantAngleOption.CurrentValue;
            smoothSteps     = smoothStepsOptions.CurrentValue;
            smoothSpeed     = smoothSpeedOption.CurrentValue;

            //System.Collections.Generic.List<System.Guid> meshes = new System.Collections.Generic.List<System.Guid>();

            System.Guid rhinoMesh = System.Guid.Empty;

            System.Collections.Generic.List <int> removeFaces = new System.Collections.Generic.List <int>();

            g3.DMesh3 projectFaces = new g3.DMesh3(true, false, false, false);

            Rhino.Geometry.Mesh rhinoInputMesh = new Rhino.Geometry.Mesh();

            for (int i = 0; i < go.ObjectCount; i++)
            {
                ObjRef obj = go.Object(i);

                if (rhinoMesh == System.Guid.Empty)
                {
                    rhinoMesh      = obj.ObjectId;
                    rhinoInputMesh = obj.Mesh();

                    for (int j = 0; j < rhinoInputMesh.Vertices.Count; j++)
                    {
                        var vertex = new g3.NewVertexInfo();
                        vertex.n = new g3.Vector3f(rhinoInputMesh.Normals[j].X, rhinoInputMesh.Normals[j].Y, rhinoInputMesh.Normals[j].Z);
                        vertex.v = new g3.Vector3d(rhinoInputMesh.Vertices[j].X, rhinoInputMesh.Vertices[j].Y, rhinoInputMesh.Vertices[j].Z);
                        projectFaces.AppendVertex(vertex);
                    }
                }

                var m = rhinoInputMesh;

                if (rhinoMesh != obj.ObjectId)
                {
                    continue;
                }

                removeFaces.Add(obj.GeometryComponentIndex.Index);

                var mf = rhinoInputMesh.Faces[obj.GeometryComponentIndex.Index];


                if (mf.IsQuad)
                {
                    double dist1 = m.Vertices[mf.A].DistanceTo(m.Vertices[mf.C]);
                    double dist2 = m.Vertices[mf.B].DistanceTo(m.Vertices[mf.D]);
                    if (dist1 > dist2)
                    {
                        projectFaces.AppendTriangle(mf.A, mf.B, mf.D);
                        projectFaces.AppendTriangle(mf.B, mf.C, mf.D);
                    }
                    else
                    {
                        projectFaces.AppendTriangle(mf.A, mf.B, mf.C);
                        projectFaces.AppendTriangle(mf.A, mf.C, mf.D);
                    }
                }
                else
                {
                    projectFaces.AppendTriangle(mf.A, mf.B, mf.C);
                }
            }

            if (rhinoInputMesh == null)
            {
                return(Result.Failure);
            }

            removeFaces.Sort();
            removeFaces.Reverse();

            foreach (var removeFace in removeFaces)
            {
                rhinoInputMesh.Faces.RemoveAt(removeFace);
            }

            rhinoInputMesh.Compact();

            GetObject goProjected = new GetObject();

            goProjected.EnablePreSelect(false, true);
            goProjected.SetCommandPrompt("Select mesh to project to");
            goProjected.GeometryFilter = ObjectType.Mesh;
            goProjected.AddOptionDouble("ConstraintAngle", ref constriantAngleOption);
            goProjected.AddOptionDouble("MinEdge", ref minEdgeLengthOption);
            goProjected.AddOptionDouble("MaxEdge", ref maxEdgeLengthOption);
            goProjected.AddOptionInteger("SmoothSteps", ref smoothStepsOptions);
            goProjected.AddOptionDouble("SmoothSpeed", ref smoothSpeedOption);
            goProjected.AddOptionDouble("ProjectAmount", ref projectAmountOption);
            goProjected.AddOptionDouble("ProjectDistance", ref projectedDistanceOption);

            goProjected.GroupSelect     = true;
            goProjected.SubObjectSelect = false;
            goProjected.EnableClearObjectsOnEntry(false);
            goProjected.EnableUnselectObjectsOnExit(false);


            for (; ;)
            {
                GetResult resProject = goProjected.Get();

                if (resProject == GetResult.Option)
                {
                    continue;
                }
                else if (goProjected.CommandResult() != Result.Success)
                {
                    return(goProjected.CommandResult());
                }

                break;
            }


            minEdgeLength     = minEdgeLengthOption.CurrentValue;
            maxEdgeLength     = maxEdgeLengthOption.CurrentValue;
            constriantAngle   = constriantAngleOption.CurrentValue;
            smoothSteps       = smoothStepsOptions.CurrentValue;
            smoothSpeed       = smoothSpeedOption.CurrentValue;
            projectedAmount   = projectAmountOption.CurrentValue;
            projectedDistance = projectedDistanceOption.CurrentValue;

            if (bHavePreselectedObjects)
            {
                // Normally, pre-selected objects will remain selected, when a
                // command finishes, and post-selected objects will be unselected.
                // This this way of picking, it is possible to have a combination
                // of pre-selected and post-selected. So, to make sure everything
                // "looks the same", lets unselect everything before finishing
                // the command.
                for (int i = 0; i < go.ObjectCount; i++)
                {
                    RhinoObject rhinoObject = go.Object(i).Object();
                    if (null != rhinoObject)
                    {
                        rhinoObject.Select(false);
                    }
                }
                doc.Views.Redraw();
            }

            bool result = false;

            if (goProjected.ObjectCount < 1)
            {
                return(Result.Failure);
            }


            var rhinoMeshProject = goProjected.Object(0).Mesh();

            if (rhinoMeshProject == null || !rhinoMeshProject.IsValid)
            {
                return(Result.Failure);
            }

            var meshProjected = GopherUtil.ConvertToD3Mesh(rhinoMeshProject);

            var res = GopherUtil.RemeshMesh(projectFaces, (float)minEdgeLength, (float)maxEdgeLength, (float)constriantAngle, (float)smoothSpeed, smoothSteps, meshProjected, (float)projectedAmount, (float)projectedDistance);

            var newRhinoMesh = GopherUtil.ConvertToRhinoMesh(res);

            if (newRhinoMesh != null && newRhinoMesh.IsValid)
            {
                newRhinoMesh.Append(rhinoInputMesh);

                result |= doc.Objects.Replace(rhinoMesh, newRhinoMesh);
            }

            doc.Views.Redraw();

            return(Result.Success);
        }
Exemplo n.º 5
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            //look at this for a summary of all the GET commands.

            // https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Input_Custom_GetBaseClass.htm.

            /*System.Object
             * Rhino.Input.Custom.GetBaseClass
             *  Rhino.Input.Custom.GetInteger
             *  Rhino.Input.Custom.GetNumber
             *  Rhino.Input.Custom.GetObject
             *  Rhino.Input.Custom.GetOption
             *  Rhino.Input.Custom.GetPoint
             *  Rhino.Input.Custom.GetString
             *
             * */
            var gc = new GetObject();

            gc.SetCommandPrompt("Select objects");
            gc.GeometryFilter = ObjectType.AnyObject;

            OptionInteger intOption  = new OptionInteger(1, 1, 99);
            OptionDouble  dblOption  = new OptionDouble(2.2, 0, 99);
            OptionToggle  boolOption = new OptionToggle(true, "off", "on");

            gc.AddOptionInteger("Integer", ref intOption);
            gc.AddOptionDouble("Double", ref dblOption);
            gc.AddOptionToggle("Boolean", ref boolOption);

            int listIndex = 0;

            string[] listValues = new string[] { "Bran", "Bob", "Arya", "Sansa", "Rickon" };

            int optList = gc.AddOptionList("List", listValues, listIndex);

            while (true)
            {
                GetResult res = gc.GetMultiple(1, 0);

                //we can check if the user selected something
                if (gc.CommandResult() != Result.Success)
                {
                    return(gc.CommandResult());
                }

                if (res == Rhino.Input.GetResult.Object)
                {
                    RhinoApp.WriteLine("Command line option values are");
                    RhinoApp.WriteLine("Integer = {0}", intOption.CurrentValue);
                    RhinoApp.WriteLine("Double = {0}", dblOption.CurrentValue);
                    RhinoApp.WriteLine("Boolean = {0}", boolOption.CurrentValue);
                    RhinoApp.WriteLine("List = {0}", listValues[listIndex]);
                }
                else if (res == Rhino.Input.GetResult.Option)
                {
                    if (gc.OptionIndex() == optList)
                    {
                        listIndex = gc.Option().CurrentListOptionIndex; //this updates the list option to whatever index the user has picked
                    }
                    continue;
                }

                break;
            }

            return(Result.Success);
        }
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            const ObjectType geometryFilter = ObjectType.Mesh;

            OptionDouble  minEdgeLengthOption   = new OptionDouble(minEdgeLength, 0.001, 200);
            OptionDouble  maxEdgeLengthOption   = new OptionDouble(maxEdgeLength, 0.001, 200);
            OptionDouble  constriantAngleOption = new OptionDouble(constriantAngle, 0.001, 360);
            OptionInteger smoothStepsOptions    = new OptionInteger(smoothSteps, 0, 10000);
            OptionDouble  smoothSpeedOption     = new OptionDouble(smoothSpeed, 0.01, 1.0);

            GetObject go = new GetObject();

            go.SetCommandPrompt("Select meshes to remesh");
            go.GeometryFilter = geometryFilter;
            go.AddOptionDouble("ConstraintAngle", ref constriantAngleOption);
            go.AddOptionDouble("MinEdge", ref minEdgeLengthOption);
            go.AddOptionDouble("MaxEdge", ref maxEdgeLengthOption);
            go.AddOptionInteger("SmoothSteps", ref smoothStepsOptions);
            go.AddOptionDouble("SmoothSpeed", ref smoothSpeedOption);

            go.GroupSelect     = true;
            go.SubObjectSelect = false;
            go.EnableClearObjectsOnEntry(false);
            go.EnableUnselectObjectsOnExit(false);
            go.DeselectAllBeforePostSelect = false;

            bool bHavePreselectedObjects = false;

            for (;;)
            {
                GetResult res = go.GetMultiple(1, 0);

                if (res == GetResult.Option)
                {
                    go.EnablePreSelect(false, true);
                    continue;
                }

                else if (go.CommandResult() != Result.Success)
                {
                    return(go.CommandResult());
                }

                if (go.ObjectsWerePreselected)
                {
                    bHavePreselectedObjects = true;
                    go.EnablePreSelect(false, true);
                    continue;
                }

                break;
            }

            minEdgeLength   = minEdgeLengthOption.CurrentValue;
            maxEdgeLength   = maxEdgeLengthOption.CurrentValue;
            constriantAngle = constriantAngleOption.CurrentValue;
            smoothSteps     = smoothStepsOptions.CurrentValue;
            smoothSpeed     = smoothSpeedOption.CurrentValue;

            if (bHavePreselectedObjects)
            {
                // Normally, pre-selected objects will remain selected, when a
                // command finishes, and post-selected objects will be unselected.
                // This this way of picking, it is possible to have a combination
                // of pre-selected and post-selected. So, to make sure everything
                // "looks the same", lets unselect everything before finishing
                // the command.
                for (int i = 0; i < go.ObjectCount; i++)
                {
                    RhinoObject rhinoObject = go.Object(i).Object();
                    if (null != rhinoObject)
                    {
                        rhinoObject.Select(false);
                    }
                }
                doc.Views.Redraw();
            }

            bool result = false;

            foreach (var obj in go.Objects())
            {
                var rhinoMesh = obj.Mesh();

                if (rhinoMesh == null || !rhinoMesh.IsValid)
                {
                    continue;
                }

                var mesh = GopherUtil.ConvertToD3Mesh(obj.Mesh());

                var res = GopherUtil.RemeshMesh(mesh, (float)minEdgeLength, (float)maxEdgeLength, (float)constriantAngle, (float)smoothSpeed, smoothSteps);

                var newRhinoMesh = GopherUtil.ConvertToRhinoMesh(mesh);

                if (newRhinoMesh != null && newRhinoMesh.IsValid)
                {
                    doc.Objects.Replace(obj, newRhinoMesh);
                }

                if (newRhinoMesh != null && newRhinoMesh.IsValid)
                {
                    result |= doc.Objects.Replace(obj, newRhinoMesh);
                }
            }

            doc.Views.Redraw();

            return(Result.Success);
        }
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            const ObjectType geometryFilter = ObjectType.Mesh;

            OptionDouble  minEdgeLengthOption     = new OptionDouble(minEdgeLength, 0.001, 200);
            OptionDouble  maxEdgeLengthOption     = new OptionDouble(maxEdgeLength, 0.001, 200);
            OptionDouble  constriantAngleOption   = new OptionDouble(constriantAngle, 0.001, 360);
            OptionInteger smoothStepsOptions      = new OptionInteger(smoothSteps, 0, 1000);
            OptionDouble  smoothSpeedOption       = new OptionDouble(smoothSpeed, 0.01, 1.0);
            OptionDouble  projectAmountOption     = new OptionDouble(projectedAmount, 0.01, 1.0);
            OptionDouble  projectedDistanceOption = new OptionDouble(projectedDistance, 0.01, 100000.0);

            GetObject go = new GetObject();

            go.SetCommandPrompt("Select mesh to remesh");
            go.GeometryFilter = geometryFilter;
            go.AddOptionDouble("ConstraintAngle", ref constriantAngleOption);
            go.AddOptionDouble("MinEdge", ref minEdgeLengthOption);
            go.AddOptionDouble("MaxEdge", ref maxEdgeLengthOption);
            go.AddOptionInteger("SmoothSteps", ref smoothStepsOptions);
            go.AddOptionDouble("SmoothSpeed", ref smoothSpeedOption);
            go.AddOptionDouble("ProjectAmount", ref projectAmountOption);
            go.AddOptionDouble("ProjectDistance", ref projectedDistanceOption);

            go.GroupSelect     = true;
            go.SubObjectSelect = false;
            go.EnableClearObjectsOnEntry(false);
            go.EnableUnselectObjectsOnExit(false);
            go.DeselectAllBeforePostSelect = false;

            bool bHavePreselectedObjects = false;

            for (;;)
            {
                GetResult res = go.Get();

                if (res == GetResult.Option)
                {
                    go.EnablePreSelect(false, true);
                    continue;
                }

                else if (go.CommandResult() != Result.Success)
                {
                    return(go.CommandResult());
                }

                if (go.ObjectsWerePreselected)
                {
                    bHavePreselectedObjects = true;
                    go.EnablePreSelect(false, true);
                    continue;
                }

                break;
            }

            GetObject goProjected = new GetObject();

            goProjected.EnablePreSelect(false, true);
            goProjected.SetCommandPrompt("Select mesh to project to");
            goProjected.GeometryFilter = geometryFilter;
            goProjected.AddOptionDouble("ConstraintAngle", ref constriantAngleOption);
            goProjected.AddOptionDouble("MinEdge", ref minEdgeLengthOption);
            goProjected.AddOptionDouble("MaxEdge", ref maxEdgeLengthOption);
            goProjected.AddOptionInteger("SmoothSteps", ref smoothStepsOptions);
            goProjected.AddOptionDouble("SmoothSpeed", ref smoothSpeedOption);
            goProjected.AddOptionDouble("ProjectAmount", ref projectAmountOption);
            goProjected.AddOptionDouble("ProjectDistance", ref projectedDistanceOption);

            goProjected.GroupSelect     = true;
            goProjected.SubObjectSelect = false;
            goProjected.EnableClearObjectsOnEntry(false);
            goProjected.EnableUnselectObjectsOnExit(false);


            for (;;)
            {
                GetResult res = goProjected.Get();

                if (res == GetResult.Option)
                {
                    continue;
                }

                else if (goProjected.CommandResult() != Result.Success)
                {
                    return(goProjected.CommandResult());
                }

                break;
            }


            minEdgeLength     = minEdgeLengthOption.CurrentValue;
            maxEdgeLength     = maxEdgeLengthOption.CurrentValue;
            constriantAngle   = constriantAngleOption.CurrentValue;
            smoothSteps       = smoothStepsOptions.CurrentValue;
            smoothSpeed       = smoothSpeedOption.CurrentValue;
            projectedAmount   = projectAmountOption.CurrentValue;
            projectedDistance = projectedDistanceOption.CurrentValue;

            if (bHavePreselectedObjects)
            {
                // Normally, pre-selected objects will remain selected, when a
                // command finishes, and post-selected objects will be unselected.
                // This this way of picking, it is possible to have a combination
                // of pre-selected and post-selected. So, to make sure everything
                // "looks the same", lets unselect everything before finishing
                // the command.
                for (int i = 0; i < go.ObjectCount; i++)
                {
                    RhinoObject rhinoObject = go.Object(i).Object();
                    if (null != rhinoObject)
                    {
                        rhinoObject.Select(false);
                    }
                }
                doc.Views.Redraw();
            }

            bool result = false;

            if (goProjected.ObjectCount < 1)
            {
                return(Result.Failure);
            }

            foreach (var obj in go.Objects())
            {
                var rhinoMesh = obj.Mesh();

                if (rhinoMesh == null || !rhinoMesh.IsValid)
                {
                    continue;
                }

                var mesh = GopherUtil.ConvertToD3Mesh(obj.Mesh());


                var rhinoMeshProject = goProjected.Object(0).Mesh();

                if (rhinoMeshProject == null || !rhinoMeshProject.IsValid)
                {
                    continue;
                }

                var meshProjected = GopherUtil.ConvertToD3Mesh(rhinoMeshProject);

                var mesh2 = new DMesh3(mesh);
                var mesh3 = new DMesh3(mesh);

                var res3 = GopherUtil.RemeshMesh(mesh3, (float)minEdgeLength, (float)maxEdgeLength, (float)constriantAngle, (float)smoothSpeed, smoothSteps, meshProjected, (float)projectedAmount, (float)projectedDistance);

                var watch = System.Diagnostics.Stopwatch.StartNew();
                // the code that you want to measure comes here

                var res = GopherUtil.RemeshMesh(mesh, (float)minEdgeLength, (float)maxEdgeLength, (float)constriantAngle, (float)smoothSpeed, smoothSteps, meshProjected, (float)projectedAmount, (float)projectedDistance);

                watch.Stop();

                var watch2 = System.Diagnostics.Stopwatch.StartNew();

                var res2 = GopherUtil.RemeshMeshNew(mesh2, (float)minEdgeLength, (float)maxEdgeLength, (float)constriantAngle, (float)smoothSpeed, smoothSteps, meshProjected, (float)projectedAmount, (float)projectedDistance);

                watch2.Stop();


                Rhino.RhinoApp.WriteLine("Time 1: {0} Time 2: {1}", watch.ElapsedMilliseconds, watch2.ElapsedMilliseconds);

                var newRhinoMesh = GopherUtil.ConvertToRhinoMesh(res);

                var newRhinoMesh2 = GopherUtil.ConvertToRhinoMesh(mesh2);

                newRhinoMesh2.Translate(new Rhino.Geometry.Vector3d(newRhinoMesh2.GetBoundingBox(true).Diagonal.X, 0, 0));

                doc.Objects.Add(newRhinoMesh2);

                if (newRhinoMesh != null && newRhinoMesh.IsValid)
                {
                    doc.Objects.Replace(obj, newRhinoMesh);
                }

                if (newRhinoMesh != null && newRhinoMesh.IsValid)
                {
                    result |= doc.Objects.Replace(obj, newRhinoMesh);
                }
            }

            doc.Views.Redraw();

            return(Result.Success);
        }
Exemplo n.º 8
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // TODO: start here modifying the behaviour of your command.
            // ---
            //RhinoApp.WriteLine("The {0} command will add a line right now.", EnglishName);



            GetObject getBreps = new GetObject();

            getBreps.SetCommandPrompt("Pick the breps to create a PolyhedralFrame");
            getBreps.GeometryFilter = Rhino.DocObjects.ObjectType.Brep;
            // set up options

            OptionDouble dblOptionCollapse = new Rhino.Input.Custom.OptionDouble(0.1, true, 0.0);
            OptionDouble dblOptionPlanar   = new Rhino.Input.Custom.OptionDouble(0.1, true, 0.0);
            OptionToggle togOptionReplace  = new OptionToggle(true, "Keep", "Replace");

            getBreps.AddOptionDouble("PointCollapseLimit", ref dblOptionCollapse);
            getBreps.AddOptionDouble("PlanarityMaxDeviation", ref dblOptionPlanar);

            getBreps.AddOptionToggle("ReplaceGeo", ref togOptionReplace);
            getBreps.AcceptNothing(false);

            while (true)
            {
                var r = getBreps.GetMultiple(1, 0);
                if (r == GetResult.Cancel)
                {
                    return(getBreps.CommandResult());
                }
                else if (r == GetResult.Object)
                {
                    break;
                }
            }

            List <Guid> ids = new List <Guid>();

            for (int i = 0; i < getBreps.ObjectCount; i++)
            {
                ids.Add(getBreps.Object(i).ObjectId);
            }

            var foam           = new PFoam();
            var deleteOriginal = false;

            try
            {
                foam.ProcessBFaces(Util.DecomposeG(ids), dblOptionCollapse.CurrentValue, dblOptionPlanar.CurrentValue);
                doc.Objects.UnselectAll();
                if (foam.Faces.Count > 2)
                {
                    foam.MakeCells();
                    Point3d newCentroid = foam.Centroid;
                    if (!togOptionReplace.CurrentValue)
                    {
                        Rhino.Input.RhinoGet.GetPoint("Place the PolyFrame", true, out newCentroid);
                    }
                    else
                    {
                        deleteOriginal = true;
                    }
                    //Rhino.RhinoDoc.ActiveDoc.Objects.Delete(ids, true);


                    foam.Offset(newCentroid - foam.Centroid);
                    foam.Show(true, true, false);
                    foam.ShowCells();


                    if (!foam.SaveToDocument())
                    {
                        foam.Hide();
                        return(Result.Cancel);
                    }


                    foam.Hide();

                    if (deleteOriginal)
                    {
                        Rhino.RhinoDoc.ActiveDoc.Objects.Delete(ids, true);
                    }
                    doc.Views.Redraw();
                    Rhino.RhinoApp.WriteLine($"Constructed a PolyFrame object with {foam.Cells.Count} cells, {foam.Faces.Count} half-faces, {foam.Edges.Count} half-edges and {foam.Vertices.Count} vertices.");

                    return(Result.Success);
                }
                else
                {
                    Rhino.RhinoApp.WriteLine("Not enough faces!");
                    return(Result.Failure);
                }
            }
            catch (Exception pfE)
            {
                RhinoApp.WriteLine(pfE.Message + " Press <Esc> to continue.");
                foam.Hide();
                return(Result.Failure);
            }
        }
Exemplo n.º 9
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            const ObjectType geometryFilter = ObjectType.Mesh;
            
            OptionDouble minEdgeLengthOption = new OptionDouble(minEdgeLength, 0.001, 200);
            OptionInteger maxTriangleCountOption = new OptionInteger(maxTriangleCount, 0, 100000000);
            
            GetObject go = new GetObject();
            go.SetCommandPrompt("Select meshes to reduce");
            go.GeometryFilter = geometryFilter;
            go.AddOptionDouble("MinEdge", ref minEdgeLengthOption);
            go.AddOptionInteger("MaxTriangle", ref maxTriangleCountOption);

            go.GroupSelect = true;
            go.SubObjectSelect = false;
            go.EnableClearObjectsOnEntry(false);
            go.EnableUnselectObjectsOnExit(false);
            go.DeselectAllBeforePostSelect = false;

            bool bHavePreselectedObjects = false;

            for (;;)
            {
                GetResult res = go.GetMultiple(1, 0);

                if (res == GetResult.Option)
                {
                    go.EnablePreSelect(false, true);
                    continue;
                }

                else if (go.CommandResult() != Result.Success)
                    return go.CommandResult();

                if (go.ObjectsWerePreselected)
                {
                    bHavePreselectedObjects = true;
                    go.EnablePreSelect(false, true);
                    continue;
                }

                break;
            }

            maxTriangleCount = maxTriangleCountOption.CurrentValue;
            minEdgeLength = minEdgeLengthOption.CurrentValue;

            if (bHavePreselectedObjects)
            {
                // Normally, pre-selected objects will remain selected, when a
                // command finishes, and post-selected objects will be unselected.
                // This this way of picking, it is possible to have a combination
                // of pre-selected and post-selected. So, to make sure everything
                // "looks the same", lets unselect everything before finishing
                // the command.
                for (int i = 0; i < go.ObjectCount; i++)
                {
                    RhinoObject rhinoObject = go.Object(i).Object();
                    if (null != rhinoObject)
                        rhinoObject.Select(false);
                }
                doc.Views.Redraw();
            }

            bool result = false;

            foreach (var obj in go.Objects())
            {
                var rhinoMesh = obj.Mesh();

                if (rhinoMesh == null || !rhinoMesh.IsValid)
                    continue;

                var mesh = GopherUtil.ConvertToD3Mesh(obj.Mesh());
                
                GopherUtil.ReduceMesh(mesh, (float)minEdgeLength, maxTriangleCount);

                double mine, maxe, avge;
                MeshQueries.EdgeLengthStats(mesh, out mine, out maxe, out avge);
                   RhinoApp.WriteLine("Edge length stats: {0} {1} {2}", mine, maxe, avge);

                var newRhinoMesh = GopherUtil.ConvertToRhinoMesh(mesh);

                if (newRhinoMesh != null && newRhinoMesh.IsValid)
                {
                    doc.Objects.Replace(obj, newRhinoMesh);
                }

                if (newRhinoMesh != null && newRhinoMesh.IsValid)
                {
                    result |= doc.Objects.Replace(obj, newRhinoMesh);
                }
            }

            doc.Views.Redraw();

            return Result.Success;
        }
Exemplo n.º 10
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            double tolerance  = doc.ModelAbsoluteTolerance;
            var    opt_double = new OptionDouble(Radius, true, tolerance);

            // Select first surface to fillet
            var go0 = new GetObject();

            go0.SetCommandPrompt("Select first surface to fillet");
            go0.AddOptionDouble("Radius", ref opt_double, "Fillet radius");
            go0.GeometryFilter = ObjectType.Surface;
            go0.EnablePreSelect(false, true);
            for (;;)
            {
                GetResult res = go0.Get();
                if (res == GetResult.Option)
                {
                    continue;
                }
                else if (res != GetResult.Object)
                {
                    return(Result.Cancel);
                }
                break;
            }

            var      obj_ref0 = go0.Object(0);
            BrepFace face0    = obj_ref0.Face();

            if (null == face0)
            {
                return(Result.Failure);
            }

            double u0, v0;

            if (null == obj_ref0.SurfaceParameter(out u0, out v0))
            {
                var pt = obj_ref0.SelectionPoint();
                if (!pt.IsValid || !face0.ClosestPoint(pt, out u0, out v0))
                {
                    // Make surface selection scriptable
                    u0 = face0.Domain(0).Min;
                    v0 = face0.Domain(1).Min;
                }
            }

            // Juggle the pickpoint ever so slightly towards the middle of the domain
            // to get a better chance of getting a valid chord.
            if (u0 == face0.Domain(0).Min || u0 == face0.Domain(0).Max)
            {
                u0 = 0.99 * u0 + 0.01 * face0.Domain(0).Mid;
            }
            if (v0 == face0.Domain(1).Min || v0 == face0.Domain(1).Max)
            {
                v0 = 0.99 * v0 + 0.01 * face0.Domain(1).Mid;
            }

            // Select second surface to fillet
            var go1 = new GetObject();

            go1.SetCommandPrompt("Select second surface to fillet");
            go1.AddOptionDouble("Radius", ref opt_double, "Fillet radius");
            go1.GeometryFilter = ObjectType.Surface;
            go1.EnablePreSelect(false, true);
            go1.DeselectAllBeforePostSelect = false;
            for (;;)
            {
                GetResult res = go1.Get();
                if (res == GetResult.Option)
                {
                    continue;
                }
                else if (res != GetResult.Object)
                {
                    return(Result.Cancel);
                }
                break;
            }

            var      obj_ref1 = go1.Object(0);
            BrepFace face1    = obj_ref1.Face();

            if (null == face1)
            {
                return(Result.Failure);
            }

            double u1, v1;

            if (null == obj_ref1.SurfaceParameter(out u1, out v1))
            {
                var pt = obj_ref1.SelectionPoint();
                if (!pt.IsValid || !face1.ClosestPoint(pt, out u1, out v1))
                {
                    // Make surface selection scriptable
                    u1 = face1.Domain(0).Min;
                    v1 = face1.Domain(1).Min;
                }
            }

            // Juggle the pickpoint ever so slightly towards the middle of the domain
            // to get a better chance of getting a valid chord.
            if (u1 == face1.Domain(0).Min || u1 == face1.Domain(0).Max)
            {
                u1 = 0.99 * u0 + 0.01 * face1.Domain(0).Mid;
            }
            if (v1 == face1.Domain(1).Min || v1 == face1.Domain(1).Max)
            {
                v1 = 0.99 * v0 + 0.01 * face1.Domain(1).Mid;
            }

            var p0      = new Point2d(u0, v0);
            var p1      = new Point2d(u1, v1);
            var fillets = Surface.CreateRollingBallFillet(face0, p0, face1, p1, Radius, tolerance);

            foreach (var f in fillets)
            {
                doc.Objects.AddSurface(f);
            }

            doc.Views.Redraw();

            Radius = opt_double.CurrentValue;

            return(Result.Success);
        }
Exemplo n.º 11
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            const ObjectType geometryFilter = ObjectType.MeshEdge;

            OptionDouble  minEdgeLengthOption   = new OptionDouble(minEdgeLength, 0.001, 200);
            OptionDouble  maxEdgeLengthOption   = new OptionDouble(maxEdgeLength, 0.001, 200);
            OptionDouble  constriantAngleOption = new OptionDouble(constriantAngle, 0.001, 360);
            OptionInteger smoothStepsOptions    = new OptionInteger(smoothSteps, 0, 1000);
            OptionDouble  smoothSpeedOption     = new OptionDouble(smoothSpeed, 0.01, 1.0);

            GetObject go = new GetObject();

            go.SetCommandPrompt("Select mesh edges to constrain during remesh");
            go.GeometryFilter = geometryFilter;

            go.AddOptionDouble("ConstraintAngle", ref constriantAngleOption);
            go.AddOptionDouble("MinEdge", ref minEdgeLengthOption);
            go.AddOptionDouble("MaxEdge", ref maxEdgeLengthOption);
            go.AddOptionInteger("SmoothSteps", ref smoothStepsOptions);
            go.AddOptionDouble("SmoothSpeed", ref smoothSpeedOption);

            go.GroupSelect     = true;
            go.SubObjectSelect = true;

            for (;;)
            {
                GetResult res = go.GetMultiple(1, 0);

                if (res == GetResult.Option)
                {
                    go.EnablePreSelect(false, true);
                    continue;
                }

                else if (go.CommandResult() != Result.Success)
                {
                    return(go.CommandResult());
                }

                break;
            }

            minEdgeLength   = minEdgeLengthOption.CurrentValue;
            maxEdgeLength   = maxEdgeLengthOption.CurrentValue;
            constriantAngle = constriantAngleOption.CurrentValue;
            smoothSteps     = smoothStepsOptions.CurrentValue;
            smoothSpeed     = smoothSpeedOption.CurrentValue;

            System.Collections.Generic.List <g3.Line3d>   constrain = new System.Collections.Generic.List <g3.Line3d>();
            System.Collections.Generic.List <System.Guid> meshes    = new System.Collections.Generic.List <System.Guid>();

            foreach (var obj in go.Objects())
            {
                if (!meshes.Contains(obj.ObjectId))
                {
                    meshes.Add(obj.ObjectId);
                }

                ObjRef objref = new ObjRef(obj.ObjectId);

                var mesh = objref.Mesh();

                var line = mesh.TopologyEdges.EdgeLine(obj.GeometryComponentIndex.Index);

                var dir = line.Direction;

                constrain.Add(new g3.Line3d(new Vector3d(line.FromX, line.FromY, line.FromZ), new Vector3d(dir.X, dir.Y, dir.Z)));
            }

            foreach (var guid in meshes)
            {
                var objref = new ObjRef(guid);

                var mesh    = GopherUtil.ConvertToD3Mesh(objref.Mesh());
                var res     = GopherUtil.RemeshMesh(mesh, (float)minEdgeLength, (float)maxEdgeLength, (float)constriantAngle, (float)smoothSpeed, smoothSteps, constrain);
                var newMesh = GopherUtil.ConvertToRhinoMesh(res);

                doc.Objects.Replace(objref, newMesh);
            }

            doc.Views.Redraw();

            return(Result.Success);
        }
Exemplo n.º 12
0
        // loads or makes a polyframe and planarizes the faces
        // load a set of geometries
        // if lines => must have PolyFrame date attached
        // if breps and data exists => reload topology data and update from geometry
        // if no data exists => make topology without cells
        // planarize and dump to the file
        // add topology data if cells are present.

        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            GetObject getObjects = new GetObject();

            getObjects.SetCommandPrompt("Pick the breps or PolyFrame container to planarize ");
            getObjects.GeometryFilter =
                Rhino.DocObjects.ObjectType.Brep |
                Rhino.DocObjects.ObjectType.Curve |
                Rhino.DocObjects.ObjectType.Mesh;
            // set up options

            getObjects.GroupSelect = true;

            OptionDouble dblOptionCollapse = new OptionDouble(0.1, true, 0.0);

            OptionToggle togOptionReplace = new OptionToggle(true, "Keep", "Replace");

            getObjects.AddOptionDouble("PointCollapseLimit", ref dblOptionCollapse);

            getObjects.AddOptionToggle("ReplaceGeo", ref togOptionReplace);
            getObjects.AcceptNothing(false);



            var primal = new PFoam();
            var dual   = new PFoam();


            while (true)
            {
                var r = getObjects.GetMultiple(1, 0);
                if (r == GetResult.Cancel)
                {
                    return(getObjects.CommandResult());
                }
                else if (r == GetResult.Object)
                {
                    break;
                }
            }



            List <Guid> ids = new List <Guid>();

            for (int i = 0; i < getObjects.ObjectCount; i++)
            {
                ids.Add(getObjects.Object(i).ObjectId);
            }


            try
            {
                ContainerType container = ContainerType.Edge;
                bool          usePFTopo = false;
                var           selGeo    = getObjects.Objects().Select(x => x.Geometry());
                if (selGeo.All(x => x.ObjectType == Rhino.DocObjects.ObjectType.Brep))
                {
                    if (selGeo.Any(y => y.UserDictionary.TryGetString("Primal", out string primalJson)))
                    {
                        Rhino.Input.RhinoGet.GetBool("PolyFrame data detected. Use topology data from there?", false, "no", "yes", ref usePFTopo);
                    }

                    if (usePFTopo)
                    {
                        //guids = LoadData.LoadPrimalDual(out primal, out dual);

                        var brepGeo = selGeo.Cast <Brep>().ToList();
                        if (brepGeo.Any(x => x.Faces.Count > 1))
                        {
                            container = ContainerType.CellBrep;
                        }
                        else
                        {
                            container = ContainerType.FaceBrep;
                        }

                        primal = LoadData.LoadFromFaces(brepGeo, out dual, true);
                    }
                    else
                    {
                        primal.ProcessBFaces(Util.DecomposeG(ids), dblOptionCollapse.CurrentValue, double.MaxValue);
                        primal.SortPartsInFaces();
                    }
                }
                else if (selGeo.All(x => x.ObjectType == Rhino.DocObjects.ObjectType.Curve))
                {
                    if (selGeo.Any(y => y.UserDictionary.TryGetString("Primal", out string primalJson)))
                    {
                        primal = LoadData.LoadFromEdges(selGeo.Cast <Curve>().ToList(), out dual, true);
                        //guids = LoadData.LoadPrimalDual(out primal, out dual);
                        container = ContainerType.Edge;
                    }
                    else
                    {
                        RhinoApp.WriteLine("No PolyFrame data detected. Planarization of raw line/curve data is not supported ");
                        return(Result.Failure);
                    }
                }
                else if (selGeo.All(x => x.ObjectType == Rhino.DocObjects.ObjectType.Mesh))
                {
                    if (selGeo.Any(y => y.UserDictionary.TryGetString("Primal", out string primalJson)))
                    {
                        var meshGeos = selGeo.Cast <Mesh>().ToList();
                        if (meshGeos.Any(x => x.Ngons.Count > 1 ||
                                         (x.Ngons.Count == 0 && x.Faces.Count > 1) ||
                                         (x.Ngons.Count == 1 && x.Faces.Count > x.Ngons[0].FaceCount)))
                        {
                            container = ContainerType.CellMesh;
                        }
                        else
                        {
                            container = ContainerType.FaceMesh;
                        }
                        primal = LoadData.LoadFromMeshes(meshGeos, out dual, true);
                    }
                    else
                    {
                        RhinoApp.WriteLine("No PolyFrame data detected. Planarization of raw mesh data is not supported ");
                        return(Result.Failure);
                    }
                }
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            try
            {
                using (var go = new GetObject())
                {
                    var wwr = 0.6;
                    wwr = Settings.GetDouble(nameof(wwr), 0.6);
                    var optionWWR = new OptionDouble(wwr, 0.01, 0.98);

                    var optionSkipFaceExistingWindow = new OptionToggle(true, "No_CreateForAllFaces", "Yes");

                    go.SetCommandPrompt("Please select honeybee rooms for adding windows to");
                    go.GeometryFilter  = ObjectType.Brep;
                    go.GroupSelect     = false;
                    go.SubObjectSelect = false;
                    go.EnableClearObjectsOnEntry(false);
                    go.EnableUnselectObjectsOnExit(false);
                    go.DeselectAllBeforePostSelect = false;

                    while (true)
                    {
                        go.ClearCommandOptions();
                        go.AddOptionDouble("WindowWallRatio", ref optionWWR);
                        go.AddOptionToggle("SkipFacesWithWindows", ref optionSkipFaceExistingWindow);
                        var res = go.GetMultiple(1, 0);

                        if (res == Rhino.Input.GetResult.Option)
                        {
                            go.EnablePreSelect(false, true);
                            continue;
                        }
                        else if (res != Rhino.Input.GetResult.Object)
                        {
                            return(Result.Cancel);
                        }

                        if (go.ObjectsWerePreselected)
                        {
                            go.EnablePreSelect(false, true);
                            continue;
                        }

                        break;
                    }

                    if (go.CommandResult() != Result.Success)
                    {
                        throw new ArgumentException("Failed to execute command!");
                    }

                    if (go.ObjectCount == 0)
                    {
                        throw new ArgumentException("No object is selected!");
                    }

                    //get option values
                    Settings.SetDouble(nameof(wwr), optionWWR.CurrentValue);
                    var ifSkipFaceWithWindow = optionSkipFaceExistingWindow.CurrentValue;

                    //all selected room geometries
                    var solidBreps = go.Objects().Where(_ => _.Brep() != null).Where(_ => _.Brep().IsSolid);
                    var rooms      = solidBreps.Where(_ => _.IsRoom()).ToList();
                    if (solidBreps.Count() != rooms.Count())
                    {
                        doc.Objects.UnselectAll();
                        var nonRooms = solidBreps.Where(_ => !_.Brep().IsRoom());
                        foreach (var item in nonRooms)
                        {
                            doc.Objects.Select(item, true, true);
                        }

                        doc.Views.Redraw();
                        Rhino.UI.Dialogs.ShowMessage("These are not Honeybee rooms, please use MassToRoom to convert them first!", "Honeybee Rhino Plugin");
                        return(Result.Failure);
                    }


                    //Add Windows
                    AddApertureByWWR(doc, rooms, optionWWR.CurrentValue, ifSkipFaceWithWindow);

                    doc.Views.Redraw();
                    return(Result.Success);
                }
            }
            catch (Exception e)
            {
                RhinoApp.WriteLine($"ERROR: {e.Message}");
                return(Result.Failure);
            }
        }