PickContext for Brep SubObject

Hi @dale, I’m trying to use a PickContext to highlight the face of a brep as the mouse moves over it in a custom GetPoint-derived class. The PickContext seems to work for general objects but it doesn’t seem to select subobjects like faces even though I’ve set SubObjectSelectionEnabled on the context. Below is a sample command and I’ve attached a sample model. When you run the TestPickObjects command and move over the surface or text dots they are found and highlighted but when I move over a brep it doesn’t seem to find the face. Any idea on what I am leaving out?

Thanks, Larry

using System.Collections.Generic;
using Rhino;
using Rhino.Commands;
using Rhino.DocObjects;
using Rhino.Input;
using Rhino.Input.Custom;
using Rhino.Geometry;

namespace TestCommands
{
	public class PickCompartmentTestCommand : Command
	{
		/// <summary>
		/// Get the name of the command
		/// </summary>
		public override string EnglishName
		{
			get { return "TestPickObjects"; }
		}

		/// <summary>
		/// Logic to execute the command
		/// </summary>
		/// <param name="doc"></param>
		/// <param name="mode"></param>
		/// <returns></returns>
		protected override Result RunCommand(RhinoDoc doc, RunMode mode)
		{
			GetObjectsWithHighlight getObj = new GetObjectsWithHighlight(doc);
			for (; ; )
			{
				var result = getObj.Get();
				if (result == GetResult.Cancel || result == GetResult.Nothing) return Result.Cancel;
				if (result == GetResult.Point) break;
			}
			if (getObj.Result() == GetResult.Point)
			{
			}
			return Result.Success;
		}

	}
	public class GetObjectsWithHighlight : GetPoint
	{
		RhinoDoc _rhinoDoc = null;
		List<ObjRef> _highlightedObjs = new List<ObjRef>();
		public GetObjectsWithHighlight(RhinoDoc rhinoDoc)
		{
			_rhinoDoc = rhinoDoc;
		}
		/// <summary>
		/// Override dynamic drawing to highlight compartments as you move near them.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnDynamicDraw(GetPointDrawEventArgs e)
		{
			base.OnDynamicDraw(e); // call base class method

			if (e.Viewport.Id == View().ActiveViewport.Id)
			{
				HighlightObjects(e, _highlightedObjs, false);
				_highlightedObjs.Clear();

				var clientPt = e.Viewport.WorldToClient(e.CurrentPoint);
				var pickedObjs = PickObjects(new System.Drawing.Point((int)clientPt.X, (int)clientPt.Y));

				HighlightObjects(e, pickedObjs, true);
				_highlightedObjs.AddRange(pickedObjs);
			}
		}
		protected void HighlightObjects(GetPointDrawEventArgs e, IList<ObjRef> objects, bool highlight)
		{
			foreach (var objRef in objects)
			{
				if (objRef.Brep() != null && !objRef.Brep().IsSurface)
				{
					if (objRef.GeometryComponentIndex.ComponentIndexType == ComponentIndexType.BrepFace)
						objRef.Object().HighlightSubObject(objRef.GeometryComponentIndex, highlight);
					else
						e.Display.DrawDot(e.CurrentPoint, "No Face Found", System.Drawing.Color.Yellow, System.Drawing.Color.Black);
				}
				else
					objRef.Object().Highlight(highlight);
			}
		}
		/// <summary>
		/// Internal helper method to figure out the selected objects
		/// </summary>
		/// <param name="screenPt"></param>
		/// <returns>The collection of picked object refs</returns>
		protected IList<ObjRef> PickObjects(System.Drawing.Point screenPt)
		{
			PickContext pickContext = new PickContext();
			pickContext.View = this.View();
			//pickContext.PickMode = PickMode.Shaded;
			pickContext.PickMode = PickMode.Wireframe;
			pickContext.SubObjectSelectionEnabled = true;
			pickContext.PickStyle = PickStyle.PointPick;

			var pickTransform = View().ActiveViewport.GetPickTransform(screenPt);
			pickContext.SetPickTransform(pickTransform);
			
			View().ActiveViewport.GetFrustumLine(screenPt.X, screenPt.Y, out Line worldLine);
			pickContext.PickLine = worldLine;
			pickContext.UpdateClippingPlanes();

			return _rhinoDoc.Objects.PickObjects(pickContext);
		}
	}
}

TestPickModel.3dm (168.6 KB)

Hi @LarryL,

As you know, Rhino does not have any mouse-over highlighting. So anything you fudge will be just that.

I’m not sure there is enough available in PickContext to do what you want. But I’ll try to remember to have a look at this later this week.

– Dale

Thanks in advance for taking a look Dale. Most everything I write is fudged so this won’t be any different. :slight_smile: