Show / Hide Table of Contents

Cut Triangles

This example shows how to get all the triangles of a body that is cut by a plane. First, we create the body. In this case a sphere, but it could be any shape. We also create a plane that is supposed to cut the sphere. This plane can not be seen in the graphical view, so we also have to create a graphical plane (a surface rectangle) just to be able to see the graphical representation of the mathematical plane.

MeshFace mf = spherePart.Mesh[DetailLevels.Medium].Bodies[0].Faces[0]; gives us the face of the sphere, i.e. the surface, with all the triangles. We also print a message in the logger with how many triangles there are in the sphere.

A triangle consists of three points, so to be able to see if a triangle is cut by the plane, we check if one of the triangles sides intersects with the plane. If so, the triangle must be cut by the plane. if (p.Intersect(vec1, vec2, out intPoint) || p.Intersect(vec2, vec3, out intPoint) || p.Intersect(vec3, vec1, out intPoint)) is where we check if one of the triangles sides are cut by the plane. If they are, we will draw the outlining of the triangle (with temporary graphics) and store its vertices in a list where we can pick them up later.

When you run this AddIn, you will see the sphere, cut by a plane, with all the cut triangles drawn on it.

Compiling the Code

Note

You need to add System.Drawing as a reference for this example to work!

Example

Project.UndoContext.BeginUndoStep("CutTriangles");
try
{
    Station station = Station.ActiveStation;

    // Create a sphere and add it to the station.
    Part spherePart = new Part();
    Body sphereBody = Body.CreateSolidSphere(new Vector3(1.5, 0.5, 1.5), 1);
    spherePart.Bodies.Add(sphereBody);
    spherePart.Name = "Sphere";
    station.GraphicComponents.Add(spherePart);

    // Create a plane.
    Matrix4 matrix_origo = new Matrix4(new Vector3(Axis.X), 45.0);
    Plane p = new Plane(matrix_origo, Axis.Z);

    // Create a graphical plane.
    Part planePart = new Part();
    station.GraphicComponents.Add(planePart);
    Body planeBody = Body.CreateSurfaceRectangle(matrix_origo, 3, 3);
    planeBody.Color = Color.RosyBrown;
    planePart.Bodies.Add(planeBody);
    planeBody.Opacity = 0.5;
    planePart.Name = "Plane";

    // Get the face of the sphere, and print how many triangles there are.
    MeshFace mf = spherePart.Mesh.Closest(DetailLevels.Medium).Bodies[0].Faces[0];
    Logger.AddMessage(
        new LogMessage("Number of triangles in sphere: " + mf.TriangleIndices.Count / 3));

    // Create a list to store the triangles in.
    List<Vector3> vecList = new List<Vector3>();

    // This is where the intersection point between the triangles sides and the plane will be stored.
    Vector3 intPoint;

    // Go through all the triangles in the sphere.
    for (int i = 0; i < mf.TriangleIndices.Count; i += 3)
    {
        // Get the indicies of the triangle vertices.
        int i0 = mf.TriangleIndices[i + 0];
        int i1 = mf.TriangleIndices[i + 1];
        int i2 = mf.TriangleIndices[i + 2];

        // Get the vectors of the triangles vertices.
        Vector3 vec1 = mf.Vertices[i0];
        Vector3 vec2 = mf.Vertices[i1];
        Vector3 vec3 = mf.Vertices[i2];

        // Check if the triangle is cut by the plane.
        // If one of the triangles sides intersect with the plane, the triangle is cut.
        if (p.Intersect(vec1, vec2, out intPoint) || p.Intersect(vec2, vec3, out intPoint)
            || p.Intersect(vec3, vec1, out intPoint))
        {
            // Draw the triangle
            station.TemporaryGraphics.DrawLine(vec1, vec2, 1, Color.PowderBlue);
            station.TemporaryGraphics.DrawLine(vec2, vec3, 1, Color.PowderBlue);
            station.TemporaryGraphics.DrawLine(vec3, vec1, 1, Color.PowderBlue);

            // Add the triangle to the list of cut triangles.
            vecList.Add(vec1);
            vecList.Add(vec2);
            vecList.Add(vec3);
        }
    }
    // Print a message of how many triangles where cut.
    Logger.AddMessage(new LogMessage("Number of triangles cut by the plane: " + vecList.Count / 3));
}
catch
{
    Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
    throw;
}
finally
{
    Project.UndoContext.EndUndoStep();
}

See Also

  • Plane
  • MeshFace
  • RobotStudio Community
In this article
Back to top Copyright © 2024 ABB