Tolerances

In this example, we demonstrate how geometry tolerances can affect meshing.

from qtcad.builder import Builder
from qtcad.builder import Mask, Polygon

First we create a mask with three shapes: a large base rectangle and two smaller rectangles that touch each other.

base = Polygon.box(10, 10).centered()
insert_left = Polygon.box(4, 4).centered().translated(-2, 0)
insert_right = insert_left.translated(4, 0).auto_name()

mask = Mask("shapes", shapes=[base, insert_left, insert_right])

Next we define a function that creates a mesh with a given geometry tolerance and a gap between the two smaller rectangles.

def test_tolerances(tol, gap):
    builder = (
        Builder()
        .set_geometry_tolerance(tol)
        .add_mask(mask)
        .use_shape("base")
        .add_surface()
        .use_shape("insert_left")
        .add_surface()
        .use_shape("insert_right")
        .copy_shape("insert_right_gapped", translate=(gap, 0))
        .add_surface()
        .mesh(2, show_gmsh_output=True)
    )

    entities = set(builder.get_all_entities(0))

    print("Points:", entities)
    return builder, entities

We can now create two meshes, one with tight tolerances and one with loose tolerances. We visualize the meshes and print the points in each case.

In the case where the tolerance is smaller than the gap between the two smaller rectangles, the two rectangles are treated as separate entities and the mesh reflects that.

builder, tight_points = test_tolerances(tol=1e-4, gap=1e-2)
builder.view(
    surface_labels=True,
    save="figs/tolerances_tight.svg",  # caption: With tight tolerances the two touching lines are separated by an invisible gap;
)
tolerances

Fig. 4 With tight tolerances the two touching lines are separated by an invisible gap

Points: {(0, 1), (0, 7), (0, 4), (0, 10), (0, 3), (0, 9), (0, 6), (0, 12), (0, 2), (0, 5), (0, 8), (0, 11)}

<qtcad.builder.builder.Builder object at 0x7fe14b3bae90>

In the case where the tolerance is larger than the gap, the two rectangles are merged into a single entity and again the mesh reflects that.

builder, loose_points = test_tolerances(tol=1e-1, gap=1e-2)
builder.view(
    surface_labels=True,
    save="figs/tolerances_loose.svg",  # caption: With loose tolerances the two touching lines are merged into one.;
)
tolerances

Fig. 5 With loose tolerances the two touching lines are merged into one.

Points: {(0, 1), (0, 7), (0, 4), (0, 10), (0, 3), (0, 9), (0, 6), (0, 2), (0, 5), (0, 8)}

<qtcad.builder.builder.Builder object at 0x7fe14a2d4f50>

Finally, we can see the difference in points between the two meshes. The tighter mesh has two extra points:

print(tight_points - loose_points)
{(0, 12), (0, 11)}

Total running time of the script: (0 minutes 5.018 seconds)

Gallery generated by Sphinx-Gallery