Example #1
0
        void RhinoApp_KeyboardEvent(int key)
        {
            //Debug.WriteLine(key);

            //myKeyPressed = key == 16;

            //return;
            if (key == 17) // ctrl key pressed
            {
                Rhino.RhinoApp.WriteLine("Fire");

                System.Drawing.Point myloc = System.Windows.Forms.Cursor.Position;
                Rhino.UI.MouseCursor.SetToolTip("heywassup");
                //Rhino.RhinoApp.WriteLine(myloc.X.ToString()+","+myloc.Y.ToString());
                Rhino.Display.RhinoView  myViewport       = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView;
                System.Drawing.Rectangle view_screen_rect = myViewport.ScreenRectangle;

                double XCoor = myloc.X - view_screen_rect.Left;
                double YCoor = myloc.Y - view_screen_rect.Top;
                //Rhino.RhinoApp.WriteLine(XCoor + "," + YCoor);


                Rhino.Display.RhinoViewport viewport          = myViewport.ActiveViewport;
                System.Drawing.Point        view_client_point = new System.Drawing.Point();
                view_client_point.X = (int)XCoor;
                view_client_point.Y = (int)YCoor;

                Rhino.Geometry.Line myLine = new Rhino.Geometry.Line();
                bool gotline = viewport.GetFrustumLine(view_client_point.X, view_client_point.Y, out myLine);
                if (gotline == true)
                {
                    List <Guid> myGuids        = myInterop.allGuids;
                    List <Guid> myHoveredGuids = new List <Guid>();

                    foreach (Guid guid in myGuids)
                    {
                        Rhino.DocObjects.RhinoObject foundObject = Rhino.RhinoDoc.ActiveDoc.Objects.Find(guid);
                        Rhino.Geometry.Curve[]       myCrvs;
                        Rhino.Geometry.Point3d[]     myPts;
                        bool cbx = Rhino.Geometry.Intersect.Intersection.CurveBrep(myLine.ToNurbsCurve(), foundObject.Geometry.GetBoundingBox(true).ToBrep(), 0.01, out myCrvs, out myPts);
                        if (myPts.Length > 0)
                        {
                            myHoveredGuids.Add(foundObject.Id);
                        }
                    }

                    if (myHoveredGuids.Count > 0)
                    {
                        List <double> allDists = new List <double>();
                        foreach (Guid guid in myHoveredGuids)
                        {
                            Rhino.DocObjects.RhinoObject foundObject = Rhino.RhinoDoc.ActiveDoc.Objects.Find(guid);
                            Rhino.Geometry.Point3d       myCe        = foundObject.Geometry.GetBoundingBox(true).Center;
                            double dist = myLine.To.DistanceTo(myCe);
                            allDists.Add(dist);
                        }
                        var sorted = allDists
                                     .Select((x, i) => new KeyValuePair <double, int>(x, i))
                                     .OrderBy(x => x.Key)
                                     .ToList();

                        List <double> B   = sorted.Select(x => x.Key).ToList();
                        List <int>    idx = sorted.Select(x => x.Value).ToList();
                        Rhino.RhinoApp.WriteLine(myHoveredGuids[idx[0]].ToString());
                        RhinoApp.Idle += OnIdleHover;
                    }
                    else
                    {
                        Rhino.RhinoApp.WriteLine("C'est la dech");
                    }
                }
                else
                {
                    Rhino.RhinoApp.WriteLine("no keys");
                }
            }
        }