pygmsh_cut.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. # Example usage with pygmsh package:
  2. # https://github.com/nschloe/pygmsh
  3. import pygmsh # pip install pygmsh
  4. from vedo import TetMesh, SphereCutter, Plotter
  5. with pygmsh.occ.Geometry() as geom:
  6. geom.characteristic_length_min = 0.1
  7. geom.characteristic_length_max = 0.1
  8. rectangle = geom.add_rectangle([-1.0, -1.0, 0.0], 2.0, 2.0)
  9. disk1 = geom.add_disk([-1.2, 0.0, 0.0], 0.5)
  10. disk2 = geom.add_disk([+1.2, 0.0, 0.0], 0.5)
  11. disk3 = geom.add_disk([0.0, -0.9, 0.0], 0.5)
  12. disk4 = geom.add_disk([0.0, +0.9, 0.0], 0.5)
  13. flat = geom.boolean_difference(
  14. geom.boolean_union([rectangle, disk1, disk2]),
  15. geom.boolean_union([disk3, disk4]),
  16. )
  17. geom.extrude(flat, [0, 0, 0.3])
  18. msh = geom.generate_mesh()
  19. lines, triangles, tetras, vertices = msh.cells
  20. vmsh = TetMesh([msh.points, tetras.data]).tomesh(fill=True)
  21. plt = Plotter(axes=1, interactive=False)
  22. plt.show(
  23. vmsh,
  24. "Drag the sphere,\nright-click&drag to zoom",
  25. )
  26. cutter = SphereCutter(vmsh)
  27. plt.add(cutter)
  28. plt.interactive()
  29. plt.close()