Select triangles inside a mesh

The attached project shows how to select the triangles inside a mesh.

It creates a derived MyMesh object, overrides 2 methods of the ViewportLayout (ProcessSelectionVisibleOnly, GetVisibleEntitiesFromBackBuffer) and listens to the viewportLayout1.SelectionChanged event to compile the selected triangles data.

 Capture.PNG

 

Note:

In builds greater than 9.0.265 the ProcessSelectionVisibleOnly of the MyViewportLayout must be replaced with this one:

 

        public override void ProcessSelectionVisibleOnly(Rectangle selectionBox, bool firstOnly, bool invert, SelectionChangedEventArgs eventArgs, bool selectableOnly = true, bool temporarySelection = false)
        {
            processTriangles = true;
            base.ProcessSelectionVisibleOnly(selectionBox, firstOnly, invert, eventArgs, selectableOnly, temporarySelection);
            processTriangles = false;
        }

 

Note 2:

By using the following class it's possible to hilight the triangles dynamically when moving the mouse.

 

class MyViewportLayout : ViewportLayout
{
    private bool processTriangles = false;

    public override void ProcessSelectionVisibleOnly(Rectangle selectionBox, bool firstOnly, bool invert, SelectionChangedEventArgs eventArgs, bool selectableOnly = true, bool temporarySelection = false)
    {
        processTriangles = true;
        base.ProcessSelectionVisibleOnly(selectionBox, firstOnly, invert, eventArgs, selectableOnly, temporarySelection);
        processTriangles = false;
    }

    protected override int[] GetVisibleEntitiesFromBackBuffer(Viewport viewport, byte[] rgbValues, int stride, int bpp, Rectangle selectionBox,
        bool firstOnly)
    {
        // Need to pass the selectionEntityMode

        if (!processTriangles)

            return base.GetVisibleEntitiesFromBackBuffer(viewport, rgbValues, stride, bpp, selectionBox, firstOnly);

        // Reads the visible triangles from the back buffer and selects them
        for (int i = 0; i < Entities.Count; i++)
        {
            if (Entities[i] is MyMesh)
            {
                MyMesh myMesh = Entities[i] as MyMesh;
                int[] indices = base.GetVisibleEntitiesFromBackBuffer(viewport, rgbValues, stride, bpp, selectionBox, firstOnly);

                // Add the previously selected triangles to the list
                if (addTrianglesToSelection)
                {
                    var l = new List<int>(myMesh.SelectedTriangles);
                    l.AddRange(indices);
                    indices = l.ToArray();
                }

                // Select the triangles
                myMesh.SelectTriangles(indices);
                break;
            }
        }

        return new int[0];
    }

    private bool addTrianglesToSelection = true;


    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);

        if (ActionMode != actionType.None)
            return;
        processTriangles = true;
        addTrianglesToSelection = true;
        GetEntityUnderMouseCursor(e.Location);

        processTriangles = false;

        for (int i = 0; i < Entities.Count; i++)
        {
            var m = ((MyMesh)Entities[i]);
            if (m.needsCompileSelected)

                m.CompileSelected(renderContext);
        }

        addTrianglesToSelection = false;

        Invalidate();

    }
}
Was this article helpful?
1 out of 1 found this helpful
Have more questions? Submit a request

Comments

5 comments
  • I created a femmesh with several Quad4 element

    can you show me how to select each element in the mesh

  • Please have a look at the FemDemo sample. You can click on PickFace and see the information of the clicked element.

    We don't have a visual feedback of the selected element, though, but we can evaluate to support it in the future.

  • Thank Luca. I hope this function will be added soon. In finite element analysis, it is necessary to investigate stress-strain at an interested location to see what happen in there.

    By the way, looks like now I can only use PickFace and PickVertex to see the information of shell and node element. How about beam or truss element. Is there any way to know the name of those elements.

    PickFace

  • Do you mean the element index of Beam and Truss elements?

  • Alberto,

    Yes, this is what I mean. 

    Your software is excellent. I'm glad that I found it for my work. 

    However, right now the result is only shown by contour color. It is not enough.  Sometimes, the program users want to know more about a particular element or a location where the stress and strain are too larger. Therefore, allowing to select a random element or node to its information is important.

    I hope that this function will be soon supported in the future.

    Ken,

Please sign in to leave a comment.