Only enable grips object when select RhinoObject

public class SampleCsRectangleGrips : CustomObjectGrips
    {
        private readonly List<SampleCsRectangleGrip> m_sample_cs_rectangle_grips = new List<SampleCsRectangleGrip>();

        public bool CreateGrips(LineCurve polylineCurve, Point3d? endPoint)
        {
            if (GripCount > 0)
                return false;

            m_sample_cs_rectangle_grips.Add(new SampleCsRectangleGrip()
            {
                OriginalLocation = polylineCurve.PointAt(polylineCurve.Domain.Mid)
            });
            if (endPoint != default)
            {
                m_sample_cs_rectangle_grips.Add(new SampleCsRectangleGrip()
                {
                    OriginalLocation = endPoint.Value
                });
            }

            foreach (var item in m_sample_cs_rectangle_grips)
            {
                AddGrip(item);
            }

            return true;
        }
}
}
   internal class SampleCsRectangleGripsEnabler
    {
        public static IDictionary<Guid, SampleCsRectangleGrips> map = new Dictionary<Guid, SampleCsRectangleGrips>();
        /// <summary>
        /// Delegate function used to turn custom grips on
        /// </summary>
        public void TurnOnGrips(RhinoObject rhObject, Point3d? point)
        {
            if (rhObject == null)
                return;

            var polyline_curve = rhObject.Geometry as LineCurve;
            if (polyline_curve == null)
                return;

            //if (!SampleCsRectangleHelper.IsRectangle(polyline_curve))
            //    return;

            var rectangle_grips = new SampleCsRectangleGrips();
            if (!rectangle_grips.CreateGrips(polyline_curve, point))
                return;
            map.Add(rhObject.Id, rectangle_grips);
            //rhObject.EnableCustomGrips(rectangle_grips);
        }
    }
RhinoDoc.SelectObjects += (x, y) =>
            {
                    var curveObject = y.RhinoObjects.OfType<CurveObject>().FirstOrDefault();
                    if (curveObject != null)
                    {
                        curveObject.EnableCustomGrips(SampleCsRectangleGripsEnabler.map[curveObject.Id]);
                  }
           }
 RhinoDoc.DeselectAllObjects += (x, y) =>
            {
// In order to hide grip objects
                    foreach (var item in RhinoDoc.Objects)
                    {
                        item.GripsOn = true;
                        item.GripsOn = false;
                    }
            };
RhinoDoc.DeselectObjects += (x, e) =>
            {
// In order to hide grip objects
                foreach (var item in e.RhinoObjects)
                {
                    item.GripsOn = true;
                    item.GripsOn = false;
                }
            };

Simply draw a few lines in Rhino, then select back and forth to switch between different lines, and Rhino crashes with no error message.

If I use RhinoObject.EnableCustomGrips(null), then use RhinoObject.EnableCustomGrips(something) again, it will prompt “Attempt to write to protected memory” and then crash

Can someone offer some help? I changed the name yesterday, here is the link to my historical question:

HI @ken_zhang,

We’ve replied to these two issues. Is there something else you are looking for?

I have not tried repeating the crash you’ve reported. I hope to have time later this week.

– Dale

As far as the question in this thread is concerned, how can I make Grips only show up when I select an object?

Maybe my calling method is wrong, but I want to know what is the correct way.

I sent those two links to prove that I am not a newbie, I just changed the username that was automatically generated before.

Hi @ken_zhang,

Here is an example of turning on an object’s grips.

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var go = new GetObject { SubObjectSelect = false, GroupSelect = true };
  go.SetCommandPrompt("Select objects for control point display");
  go.GetMultiple(1, 0);
  if (go.CommandResult() != Result.Success)
    return go.CommandResult();

  foreach (var objref in go.Objects())
  {
    var rhObj = objref.Object();
    if (null != rhObj)
      rhObj.GripsOn = true;
  }

  doc.Views.Redraw();

  return Result.Success;

Hope this helps.

– Dale

i would love to do that in C++ but why objref.Object() are constant by design ?
what i just want is to turn on Grips on a CRhinoObject provided by a CRhinoGetObject ?
i really need that simple stuff lol !! arf

	CRhinoGetObject go;
	go.SetCommandPrompt(L"Select Object:");
	go.GetObjects(1, 1);
	go.Object(0).Object()->m_grips->m_owner_object->EnableGrips(1);// give back a null ptr
	const CRhinoObject* g = go.Object(0).Object();

in fact a simple cast have solved my issue.

A sample if anyone needs.

cmdSampleEnableGrips.cpp

– Dale