PickContext Bug?

The following code does is: When GetPoint() is used, if the Point2d() returned is visually located on some RhinoObject, it returns the ObjRef for those RhinoObject. And then I’m going to try to get these points on the RhinoObject.

Please try the following code :

    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
        GetPoint getPointAction = new GetPoint();
        getPointAction.SetCommandPrompt("Please select the location");

        if (getPointAction.Get() == GetResult.Point)
        {
            //PickContext  Setting
            System.Drawing.Point pt2d = getPointAction.Point2d();
            PickContext pickContext = new PickContext();
            pickContext.View = doc.Views.ActiveView;
            pickContext.PickStyle = PickStyle.PointPick;
            pickContext.PickMode = PickMode.Shaded;

            Rhino.Geometry.Line pickLine;
            doc.Views.ActiveView.ActiveViewport.GetFrustumLine(pt2d.X, pt2d.Y, out pickLine);
            pickContext.PickLine = pickLine;
            pickContext.UpdateClippingPlanes();
            pickContext.PickGroupsEnabled = true;

            //Get the ObjRef[]
            ObjRef[] pickList = doc.Objects.PickObjects(pickContext);

            foreach( ObjRef objRef in pickList )
            {
                RhinoApp.WriteLine($"pickList_id:  {objRef.ObjectId}");
            }

            doc.Objects.AddLine(pickLine);
        }
        return Result.Success;
    }

Use this file:
PickContext_Test.3dm (102.1 KB)

If you click the Brep in GetPoint, it will return the wrong ObjRef;

But if you click Extrusion, it will return correctly ObjRef;

And the pickLine is correctly generated.

Did I use PickContext incorrectly or something else?

Hi @Zhang,

Any reason you are not just using GetObject?

– Dale

Thank you Dale. The problem has been solved. GetObject is useful.

Here is my code, If anyone needs it.

        //add my rhino-object one by one 
        bool go_on = true;
        do
        {
            //get the location
            Rhino.Input.Custom.GetObject myGet = new Rhino.Input.Custom.GetObject();

            myGet.AcceptPoint(true);
            myGet.AcceptNothing(true);
            myGet.EnableHighlight(false);
            myGet.AcceptCustomMessage(true);
            myGet.DisablePreSelect();
            myGet.SetCommandPrompt("Select object surface point to put it");
            Rhino.Geometry.Point3d location;
            myGet.Get();

            if (myGet.Result() == GetResult.Nothing)
            {
                go_on = false;
            }

            else if (myGet.Result() != GetResult.Cancel )
            {
                if (myGet.Object(0).SelectionPoint() != Point3d.Unset)
                {
                    //add my rhino-object
                    Box myBox = new Box(Plane.WorldXY, new Interval(0, 1000), new Interval(0, 1000), new Interval(0, 1000));
                    BoundingBox bbox = myBox.BoundingBox;

                    location = myGet.Object(0).SelectionPoint();
                    Rhino.Geometry.Point3d center_bottom = new Point3d(bbox.Center.X, bbox.Center.Y, bbox.Min.Z);
                    Rhino.Geometry.Transform move = Rhino.Geometry.Transform.Translation(new Rhino.Geometry.Vector3d(location - center_bottom));

                    myBox.Transform(move);
                    doc.Objects.AddBox(myBox);
                }
            }

        } while (go_on);


        return Result.Success;