DomCR
DomCR

Reputation: 583

Select by name HelixToolkit

INTRODUCTION

I'm implementing a 3D viewer in a WPF, in the viewer I need to be able to select the object and identify them by their name.

PROBLEM

I can select objects with the mouse without problem but I cannot get the object name, to test the app I setup a simple scene with a teapot, my goal is to be able to retrieve the object's name.

xaml file:

<Grid>
    <h:HelixViewport3D Name="h_viewport" MouseDown="mdown_select">
        <h:DefaultLights/>
        <h:Teapot x:Name="h_teapot"/>
    </h:HelixViewport3D>
</Grid>

Selection method:

private void mdown_select(object sender, MouseButtonEventArgs e)
        {
            Point mousePt = e.GetPosition(this);
            PointHitTestParameters ptParams = new PointHitTestParameters(mousePt);

            //Test for a result in the Viewport3D
            VisualTreeHelper.HitTest(h_viewport, null, HTResult, ptParams);
        }

        public HitTestResultBehavior HTResult(System.Windows.Media.HitTestResult rawresult)
        {
            RayHitTestResult rayResult = rawresult as RayHitTestResult;

            if (rayResult != null)
            {
                RayMeshGeometry3DHitTestResult rayMeshResult = rayResult as RayMeshGeometry3DHitTestResult;

                if (rayMeshResult != null)
                {
                    GeometryModel3D hitGeometry = rayMeshResult.ModelHit as GeometryModel3D;
                    //Trying to find the name
                    string name = hitGeometry.GetName();

                    //It returns true, I've found the teapot but not the name
                    if (hitGeometry.Equals(h_teapot.Content))
                        //No name here
                        System.Diagnostics.Debug.WriteLine("teapot found: " + hitGeometry.GetName());
                }
            }

            return HitTestResultBehavior.Stop;
        }

QUESTION

Is the name hidden somewhere in the selected object, or is only a reference for the menu class? Another possible way that I can achieve this?

Upvotes: 1

Views: 846

Answers (1)

DomCR
DomCR

Reputation: 583

It seems that helix toolkit only saves the attached properties, so to find the name of the teapot I had to add the name using an helix property h:AttachedProperties.Name="myteapot".

<Grid>
    <h:HelixViewport3D Name="h_viewport" MouseDown="mdown_select" h:AttachedProperties.Name="myteapot">
        <h:DefaultLights/>
        <h:Teapot x:Name="h_teapot"/>
    </h:HelixViewport3D>
</Grid>

And find the object in the code by getting the visualmesh

RayMeshGeometry3DHitTestResult rayMeshResult = rayResult as RayMeshGeometry3DHitTestResult;
string name = rayMeshResult.VisualHit.GetName(); //returns "myteapot"

Upvotes: 1

Related Questions