コード例 #1
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // Check the selected dot
            GetObject go = new GetObject();

            // Create a new dictionary of strings, with string keys.
            //
            Dictionary <double, double> sizeAngle = new Dictionary <double, double>();
            List <double> holeSizeList            = new List <double>();

            go.GroupSelect     = true;
            go.SubObjectSelect = false;
            go.EnableClearObjectsOnEntry(false);
            go.EnableUnselectObjectsOnExit(false);
            go.DeselectAllBeforePostSelect = false;
            go.EnableSelPrevious(true);
            go.EnablePreSelect(true, false);
            go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve;

            go.SetCommandPrompt("Select all the circles:");
            GetResult result = go.GetMultiple(1, -1);

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

            RhinoApp.WriteLine("Object selection counter = {0}", go.ObjectCount);

            List <RhinoObject> rhinoObjectList = new List <RhinoObject>();
            List <ArcCurve>    arcCurveList    = new List <ArcCurve>();

            // Loop through all the objects to find Text
            for (int i = 0; i < go.ObjectCount; i++)
            {
                RhinoObject rhinoObject = go.Object(i).Object();

                if (rhinoObject.ObjectType == ObjectType.Curve)
                {
                    ArcCurve curve = rhinoObject.Geometry as ArcCurve;

                    if (curve != null)
                    {
                        if (curve.IsCircle() == true)
                        {
                            if (!holeSizeList.Exists(element => element == curve.Radius))
                            {
                                holeSizeList.Add(curve.Radius);
                            }

                            arcCurveList.Add(curve);
                            // rhinoObjectList.Add(rhinoObject);
                        }
                    }
                }
            }

            holeSizeList.Sort();

            if (holeSizeList.Count < 1)
            {
                return(Result.Failure);
            }

            double maxHole = holeSizeList.Max();
            double minHole = holeSizeList.Min();

            foreach (double size in holeSizeList)
            {
                double angle;
                if ((maxHole - minHole) != 0)
                {
                    angle = 180 * ((size - minHole) / (maxHole - minHole));
                }
                else
                {
                    angle = 0;
                }

                sizeAngle.Add(size, angle);
            }

            // Create a new layer
            string layerName = "FormTool";

            // Does a layer with the same name already exist?
            int layerIndex = doc.Layers.Find(layerName, true);

            // If layer does not exist
            if (layerIndex == -1)
            {
                // Add a new layer to the document
                layerIndex = doc.Layers.Add(layerName, System.Drawing.Color.Black);
            }

            doc.Layers.SetCurrentLayerIndex(layerIndex, true);

            foreach (ArcCurve ac in arcCurveList)
            {
                double angle = 0;

                sizeAngle.TryGetValue(ac.Radius, out angle);

                drawFormTool(ac.Arc.Center.X, ac.Arc.Center.Y, angle * Math.PI / 180);
            }

            doc.Views.Redraw();

            return(Result.Success);
        }
コード例 #2
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            var gc = new GetObject();

            gc.SetCommandPrompt("Select some FANCY curves");
            gc.GeometryFilter = ObjectType.Curve;
            gc.GetMultiple(1, 0);

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

            int linecount     = 0;
            int arccount      = 0;
            int circlecount   = 0;
            int polylinecount = 0;


            //create a collection of curves

            // var curves = new List<Curve>(gc.ObjectCount); OR

            var curves = new CurveList();

            for (var i = 0; i < gc.ObjectCount; i++)
            {
                var curve = gc.Object(i).Curve();
                if (null != curve)
                {
                    curves.Add(curve);
                }
                // ADD THE CURVE TO THE CURVES LIST, expanding dynamically through the loop

                LineCurve line_curve = curve as LineCurve; //check if curve is a line, 'as' is simplest form of casting
                if (line_curve != null)                    // so long as the selection is not null, the our curve is a line
                {
                    linecount++;                           // then we can increase our linecount
                }
                PolylineCurve polyline_curve = curve as PolylineCurve;
                if (polyline_curve != null)
                {
                    polylinecount++;
                }

                ArcCurve arc_curve = curve as ArcCurve;
                if (arc_curve != null)
                {
                    if (arc_curve.IsCircle())
                    {
                        circlecount++;
                    }
                    else
                    {
                        arccount++;
                    }
                }

                curve.Domain = new Interval(0, 1);

                //this will force all curves to have the Domain from 0 to 1

                doc.Objects.AddPoint(curve.PointAtStart);
                doc.Objects.AddPoint(curve.PointAtEnd);
                doc.Objects.AddPoint(curve.PointAt(0.75));

                //add points along the domain...


                var format = string.Format("F{0}", doc.DistanceDisplayPrecision);

                string crvinfo = string.Format("The curve {0} has the length {1} and domain: {2} to {3}. Degree {4}. Points at 0,0.75 and 1 of domain.",
                                               i,
                                               curve.GetLength().ToString(format),
                                               curve.Domain.T0.ToString(format),
                                               curve.Domain.T1.ToString(format),
                                               curve.Degree.ToString(format));

                RhinoApp.WriteLine(crvinfo);
            }

            doc.Views.Redraw();

            string s = string.Format("{0} line(s), {1} polyline(s), {2} circle(s), {3} arc(s) and {4} curve(s) selected in total",
                                     linecount.ToString(), polylinecount.ToString(), circlecount.ToString(), arccount.ToString(), curves.Count.ToString());

            RhinoApp.WriteLine(s);

            return(Result.Success);
        }
コード例 #3
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // Check the selected dot
            GetObject go = new GetObject();

            // Create a new dictionary of strings, with string keys.
            //
            Dictionary <int, int> sizeAngle    = new Dictionary <int, int>();
            List <int>            holeSizeList = new List <int>();

            //Setting up for hole selection
            go.GroupSelect     = true;
            go.SubObjectSelect = false;
            go.EnableClearObjectsOnEntry(false);
            go.EnableUnselectObjectsOnExit(false);
            go.DeselectAllBeforePostSelect = false;
            go.EnableSelPrevious(true);
            go.EnablePreSelect(true, false);
            go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve;

            go.SetCommandPrompt("Select all the circles:");
            GetResult result = go.GetMultiple(1, -1);

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

            RhinoApp.WriteLine("Object selection counter = {0}", go.ObjectCount);

            List <RhinoObject> rhinoObjectList = new List <RhinoObject>();
            List <ArcCurve>    arcCurveList    = new List <ArcCurve>();

            // Loop through all the objects to find Curve
            for (int i = 0; i < go.ObjectCount; i++)
            {
                RhinoObject rhinoObject = go.Object(i).Object();

                if (rhinoObject.ObjectType == ObjectType.Curve)
                {
                    ArcCurve curve = rhinoObject.Geometry as ArcCurve;

                    if (curve != null)
                    {
                        if (curve.IsCircle() == true)
                        {
                            if (!holeSizeList.Exists(element => element == curve.Radius))
                            {
                                holeSizeList.Add(Convert.ToInt32(curve.Radius)); //add unique hole sizes
                            }

                            arcCurveList.Add(curve);
                        }
                    }
                }
            }

            holeSizeList.Sort();

            int maxHole = Convert.ToInt32(holeSizeList.Max());           //get the maximum hole size in the list
            int minHole = Convert.ToInt32(holeSizeList.Min());           //get the minimum hole size in the list

            double maximumRotation = (360 - (360 / holeSizeList.Count)); //equation to calculate the maximum rotation
            int    indexCOunt      = 0;

            foreach (int size in holeSizeList) //for each hole size in the list, calculate the angle of rotation
            {
                int angle;
                if ((maxHole - minHole) != 0)
                {
                    angle = Convert.ToInt32(indexCOunt * (360 / holeSizeList.Count));
                    indexCOunt++;
                }
                else
                {
                    angle = 0;
                }

                sizeAngle.Add(size, angle); //assign the angle for each hole size
            }

            // Create a new layer
            string layerName = "CaveTool";

            // Does a layer with the same name already exist?
            int layerIndex = doc.Layers.Find(layerName, true);

            // If layer does not exist
            if (layerIndex == -1)
            {
                // Add a new layer to the document
                layerIndex = doc.Layers.Add(layerName, System.Drawing.Color.Black);
            }

            doc.Layers.SetCurrentLayerIndex(layerIndex, true);

            String location = getCaveToolLocation(); //get the location of the cave tool (request from user)

            if (location != null)
            {
                //for each hole found (selected by user)
                //start drawing the cave tool
                foreach (ArcCurve ac in arcCurveList)
                {
                    int angle = 0;
                    //pass the hole size and get the angle  specific to the hole size
                    sizeAngle.TryGetValue(Convert.ToInt32(ac.Radius), out angle);
                    //draw the cave tool
                    drawCaveImageTool(ac.Arc.Center.X, ac.Arc.Center.Y, angle, layerIndex, location);
                }
            }
            return(Result.Success);
        }
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // Check the selected dot
            GetObject go = new GetObject();

            // Create a new dictionary of strings, with string keys.
            //
            Dictionary <double, double> sizeAngle = new Dictionary <double, double>();
            List <double> holeSizeList            = new List <double>();

            go.GroupSelect     = true;
            go.SubObjectSelect = false;
            go.EnableClearObjectsOnEntry(false);
            go.EnableUnselectObjectsOnExit(false);
            go.DeselectAllBeforePostSelect = false;
            go.EnableSelPrevious(true);
            go.EnablePreSelect(true, false);
            go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve;

            go.SetCommandPrompt("Select all the circles:");
            GetResult result = go.GetMultiple(1, -1);

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

            RhinoApp.WriteLine("Object selection counter = {0}", go.ObjectCount);

            List <ArcCurve> arcCurveList = new List <ArcCurve>();

            // Loop through all the objects to find Curve
            for (int i = 0; i < go.ObjectCount; i++)
            {
                RhinoObject rhinoObject = go.Object(i).Object();

                if (rhinoObject.ObjectType == ObjectType.Curve)
                {
                    ArcCurve curve = rhinoObject.Geometry as ArcCurve;

                    if (curve != null)
                    {
                        if (curve.IsCircle() == true)
                        {
                            if (!holeSizeList.Exists(element => element == curve.Radius))
                            {
                                holeSizeList.Add(curve.Radius);
                            }

                            arcCurveList.Add(curve);
                        }
                    }
                }
            }

            holeSizeList.Sort();

            double maxHole = holeSizeList.Max();
            double minHole = holeSizeList.Min();

            foreach (double size in holeSizeList)
            {
                double angle;
                if ((maxHole - minHole) != 0)
                {
                    angle = 180 * ((size - minHole) / (maxHole - minHole));
                }
                else
                {
                    angle = 0;
                }

                sizeAngle.Add(size, angle);
            }


            // Open file dialog
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = doc.Path;
            openFileDialog.Filter           = "3dm files (*.3dm)|*.3dm| dxf files (*.dxf)|*.dxf|All files (*.*)|*.*";
            openFileDialog.FilterIndex      = 1;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.Multiselect      = false;
            openFileDialog.Title            = "CAD file containing geometry";

            DialogResult dr = openFileDialog.ShowDialog();
            uint         firstSN, lastSN;
            string       sScript;

            List <RhinoObject> imported = new List <RhinoObject>();

            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                string file = openFileDialog.FileName;
                // Import the file
                try
                {
                    sScript = String.Format("! _-Import \"{0}\" _Enter", file);
                    firstSN = RhinoObject.NextRuntimeSerialNumber;
                    RhinoApp.RunScript(sScript, false);
                    lastSN = RhinoObject.NextRuntimeSerialNumber;
                    List <Guid> ids = new List <Guid>();

                    foreach (RhinoObject obj in doc.Objects)
                    {
                        if (obj.RuntimeSerialNumber >= firstSN && obj.RuntimeSerialNumber < lastSN)
                        {
                            imported.Add(obj);
                            ids.Add(obj.Id);
                        }
                    }

                    foreach (ArcCurve ac in arcCurveList)
                    {
                        double angle = 0;

                        sizeAngle.TryGetValue(ac.Radius, out angle);

                        Transform translation = Transform.Translation(ac.Arc.Center.X, ac.Arc.Center.Y, ac.Arc.Center.Z);
                        Transform rotate      = Transform.Rotation(angle * Math.PI / 180, new Point3d(0, 0, 0));

                        List <Guid> rotatedIds = new List <Guid>();
                        foreach (var objRef in imported)
                        {
                            rotatedIds.Add(doc.Objects.Transform(objRef, rotate, false));
                        }

                        foreach (var id in rotatedIds)
                        {
                            RhinoObject objectRot;

                            objectRot = doc.Objects.Find(id);
                            doc.Objects.Transform(objectRot, translation, true);
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Probably related to Windows file system permissions.
                    MessageBox.Show(ex.Message);
                }
            }



            // Export the whole lot
            //string command = string.Format("-_Export \"" + Path.GetDirectoryName(doc.Path) + @"\" + labelName + "\"  Scheme \"R12 Lines & Arcs\" Enter");
            // Export the selected curves
            //RhinoApp.RunScript(command, true);

            return(Result.Success);
        }