# Mesh cube with spherical hole

**URL:** <https://community.freefem.org/t/mesh-cube-with-spherical-hole/2999>\
**Category:** General Discussion\
**Created:** [February 26, 2024, 11:10am UTC](https://community.freefem.org/t/mesh-cube-with-spherical-hole/2999 "2024-02-26T11:10:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![smyrback](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@smyrback](https://community.freefem.org/u/smyrback)\
**Post date:** [February 26, 2024, 11:10am UTC](https://community.freefem.org/t/mesh-cube-with-spherical-hole/2999/1 "2024-02-26T11:10:52Z")

</div>

Hi,

I’ve been looking at the example of generating a cube with a sphere inside from here: [Mesh Generation](https://doc.freefem.org/documentation/mesh-generation.html#the-command-savesurfacemesh).

The code is:

"  
load “msh3”  
load “TetGen”  
load “medit”  
include “MeshSurface.idp”

// Parameters  
real hs = 0.1; //mesh size on sphere  
int[int] N = [20, 20, 20];  
real [int,int] B = [[-1, 1], [-1, 1], [-1, 1]];  
int [int,int] L = [[1, 2], [3, 4], [5, 6]];

// Meshes  
meshS ThH = SurfaceHex(N, B, L, 1);  
meshS ThS = Sphere(0.5, hs, 7, 1);  
meshS ThHS = ThH + ThS;  
medit(“Hex-Sphere”, ThHS);

real voltet = (hs^3)/6.;  
cout \<\< "voltet = " \<\< voltet \<\< endl;

real[int] domain = [0, 0, 0, 1, voltet, 0, 0, 0.7, 2, voltet];  
mesh3 Th = tetg(ThHS, switch=“pqaAAYYQ”, nbofregions=2, regionlist=domain);  
medit(“Cube with ball”, Th);  
"

I want to make the sphere mesh disappear, so that I’m left with a 3D cube mesh without the sphere in the middle. I tried altering the code to the following, but I am unable to save the mesh to an .msh file which I need, I get the error “UNABLE TO OPEN :file\_name.msh” … PB Write error !.

What am I doing wrong?

Here is the modified code:

"  
load “msh3”  
load “TetGen”  
load “medit”  
include “MeshSurface.idp”

// Parameters  
real hs = 0.1; //mesh size on sphere  
int[int] N = [20, 20, 20];  
real [int,int] B = [[-1, 1], [-1, 1], [-1, 1]];  
int [int,int] L = [[1, 2], [3, 4], [5, 6]];

// Meshes  
meshS ThH = SurfaceHex(N, B, L, 1);  
meshS ThS = Sphere(0.5, hs, 7, 1);  
meshS ThHS = ThH + ThS;

medit(“Hex-Sphere”, ThHS);

real voltet = (hs^3)/6.;  
cout \<\< "voltet = " \<\< voltet \<\< endl;

real[int] hole = [0., 0., 0.]  
real[int] domain = [0, 0, 0, 1, voltet];  
mesh3 Th = tetg(ThHS, switch=“pqaAAYYQ”, nbofholes=1, holelist=hole, nbofregions=1, regionlist=domain);  
medit(“Cube with ball”, Th);  
savemesh(Th, “mesh.msh”);  
"

---

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [February 26, 2024, 12:48pm UTC](https://community.freefem.org/t/mesh-cube-with-spherical-hole/2999/2 "2024-02-26T12:48:25Z")

</div>

I don’t know about the issue of the sphere, but I noticed that saving a 3d mesh as a .msh file gives such error message. You should try a .mesh file

---

<div class="post-metadata">

**Author:** ![smyrback](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@smyrback](https://community.freefem.org/u/smyrback)\
**Post date:** [February 27, 2024, 12:33pm UTC](https://community.freefem.org/t/mesh-cube-with-spherical-hole/2999/3 "2024-02-27T12:33:17Z")

</div>

I see. Do you know any way to convert between the two formats, because the library I’m using needs a .msh format.

---

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [February 27, 2024, 3:54pm UTC](https://community.freefem.org/t/mesh-cube-with-spherical-hole/2999/4 "2024-02-27T15:54:36Z")

</div>

Maybe you can find some utility on the web to make the conversion. Otherwise it looks not difficulty to write a script to do it, since the data are the same, only the tag lines differ. You can do the conversion by hand if you have only a few meshes to convert.
