Note
Go to the end to download the full example code.
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])
[01:52:40 PM] INFO Auto-named Polygon instance to 'base' masks.py:208
INFO Auto-named Polygon instance to 'insert_left' masks.py:208
INFO Auto-named Polygon instance to 'insert_right' masks.py:208
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;
)
Fig. 4 With tight tolerances the two touching lines are separated by an invisible gap
[01:52:41 PM] INFO Creating a surface from shape 0 with name 'base' from mask 'shapes' at z=0 builder.py:2386
INFO Creating new physical group with name 'base' builder.py:2482
INFO Creating a surface from shape 1 with name 'insert_left' from mask 'shapes' at z=0 builder.py:2386
INFO Fragmenting... fragmenter.py:234
INFO Creating new physical group with name 'insert_left' builder.py:2482
INFO Creating a surface from shape 3 with name 'insert_right_gapped' from mask 'shapes' at z=0 builder.py:2386
INFO Fragmenting... fragmenter.py:234
INFO Creating new physical group with name 'insert_right_gapped' builder.py:2482
INFO Preparing to mesh builder.py:1502
INFO Detecting appropriate random factor builder.py:3608
INFO Increased random factor from 1e-09 to 1.4802974253785605e-08 builder.py:3626
INFO Random factor is now appropriate builder.py:3632
INFO Meshing builder.py:1518
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)}
INFO Saving /home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/examples/operations/figs/tolerances_tight.svg builder.py:1863
<qtcad.builder.builder.Builder object at 0x7f026d8eae90>
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.;
)
Fig. 5 With loose tolerances the two touching lines are merged into one.
[01:52:43 PM] INFO Creating a surface from shape 0 with name 'base' from mask 'shapes' at z=0 builder.py:2386
INFO Creating new physical group with name 'base' builder.py:2482
INFO Creating a surface from shape 1 with name 'insert_left' from mask 'shapes' at z=0 builder.py:2386
INFO Fragmenting... fragmenter.py:234
INFO Creating new physical group with name 'insert_left' builder.py:2482
INFO Creating a surface from shape 3 with name 'insert_right_gapped' from mask 'shapes' at z=0 builder.py:2386
INFO Fragmenting... fragmenter.py:234
INFO Creating new physical group with name 'insert_right_gapped' builder.py:2482
INFO Preparing to mesh builder.py:1502
INFO Detecting appropriate random factor builder.py:3608
INFO Random factor is now appropriate builder.py:3632
INFO Meshing builder.py:1518
Points: {(0, 1), (0, 7), (0, 4), (0, 10), (0, 3), (0, 9), (0, 6), (0, 2), (0, 5), (0, 8)}
INFO Saving /home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/examples/operations/figs/tolerances_loose.svg builder.py:1863
<qtcad.builder.builder.Builder object at 0x7f026d8ea990>
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 4.612 seconds)