Error while doing a Boolean Fuse of volumes
Hi, I have written a Python code, to create a 3D network of hollow tubes. The tube is created by defining an outer cylinder, then subtracting an inner cylinder from it. Then I define a set of points in 3D space, these points define the centres of the opposite faces of these tubes. I connect all these points together to create a 3D network. Everything works good, and the tubes mesh well. However, when I try to fuse them together to create a single volume, I'm facing an error.
The error says that
Error : Invalid boundary mesh (segment-facet intersection) on surface 219, intersection (-8.34386,123.856,-104.195)
Error : No elements in volume 1
Can anyone help me fix this ? This is the code.
import gmsh
import numpy
import itertools
rt3 = numpy.sqrt(3)
outfile = "simple-holow-dmnd.msh"
lc = 3.0
side_length = 100.0
sphere_rad = 3.0
cyl_rad_out = 15.0
cyl_rad_in = 10.0
model = gmsh.model
factory = model.occ
pi = numpy.pi
gmsh.initialize()
gmsh.option.setNumber("Mesh.CharacteristicLengthMax", lc)
gmsh.option.setNumber("General.Terminal", 1)
model.add("ice")
def getLength(p1, p2):
return numpy.sqrt(numpy.square(p2[0] - p1[0]) +
numpy.square(p2[1] - p1[1]) +
numpy.square(p2[
2] - p1[2]))
def finish_and_write(filename='mesh.msh'):
factory.synchronize()
model.mesh.generate(3)
#gmsh.fltk.run()
gmsh.write(filename)
gmsh.finalize()
def copy_obj(obj):
return factory.copy([(3, obj)])[0][1]
def touch_sphere(cordinates_xyz, radius_):
return factory.addSphere(cordinates_xyz[0], cordinates_xyz[1],
cordinates_xyz[2], radius=radius_)
def touch_cyllinder(p1, p2, rad):
return factory.addCylinder(p1[0], p1[1], p1[2],
p2[0] - p1[0], p2[1] - p1[1]
, p2[2] - p1[2], rad)
def touch_tubez_cyl (p1,p2,rad_out,rad_in):
out_cyl = factory.addCylinder(p1[0], p1[1], p1[2],
p2[0] - p1[0], p2[1] - p1[1]
, p2[2] - p1[2], rad_out)
in_cyl = factory.addCylinder(p1[0], p1[1], p1[2],
p2[0] - p1[0], p2[1] - p1[1]
, p2[2] - p1[2], rad_in)
return factory.cut([(3,out_cyl)],[(3,in_cyl)])[0][0][1]
def rotate_obj(axis, axis_point, angle, object1, duplicata=1):
if duplicata == 1:
object1 = copy_obj(object1)
factory.rotate([(3, object1)], axis_point[0],
axis_point[1], axis_point[2],
axis[0], axis[1], axis[2],
angle)
return object1
def BoolUnion(obj1, obj2, dimens):
return model.occ.fuse([(dimens, obj1)], [(dimens, obj2)])
def BoolUnion2 (obj1, obj2, dimens):
return model.occ.fuse([(dimens, obj1)], [(dimens, obj2)], removeObject=False,
removeTool=False)
def BoolIntersectionKeepOriginal(obj1, obj2, dimens):
return model.occ.intersect([(dimens, obj1)],
[(dimens, obj2)], tag=-1, removeObject=False,
removeTool=False)
def BoolIntersectionDeleteOriginal(obj1, obj2, dimens):
return model.occ.intersect([(dimens, obj1)],
[(dimens, obj2)], tag=-1, removeObject=True,
removeTool=True)
def translate_obj(obj1, xyz, duplicata=0):
if duplicata == 1:
obj1 = copy_obj(obj1)
factory.translate([(3, obj1)], xyz[0], xyz[1], xyz[2])
# create a hexagon
def getLength(p1, p2):
return numpy.sqrt(numpy.square(p2[0] - p1[0]) +
numpy.square(p2[1] - p1[1]) +
numpy.square(p2[02] - p1[2]))
"""Deffining the points for the shape"""
p1 = (0,0,0)
p2 = (1,1,1)
p3 = (-1,-1,1)
p4 = (1,-1,-1)
p5 = (-1,1,-1)
p6 = (2,2,0)
p7 = (2,0,2)
p8 = (0,2,2)
p9 = (1,3,-1)
p10 = (3,1,-1)
p11 = (3,3,1)
p12 = (0,4,0)
p13 = (0,2,-2)
p14 = (-2,2,0)
p15 = (1,1,-3)
p16 = (2,4,-2)
p17 = (-1,3,-3)
p18 = (-2,0,-2)
p19 = (-1,3,1)
p20 = (1,5,1)
p21 = (-1,5,-1)
p22 = (-2,4,2)
p23 = (1,3,3)
p24 = (-1,1,3)
p25 = (-3,3,-1)
p26 = (-3,1,1)
plst1 = [p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,\
p12,p13,p14,p15,p16,p17,p18,p19,p20,\
p21,p22,p23,p24,p25,p26]
plst = []
for p in plst1:
plst.append(numpy.array(p)*side_length/rt3 )
points = []
cylis = []
sphz = []
""" Connecting the nearest points through a hollow-tube"""
for cc in itertools.combinations(plst,2):
p1,p2 = cc[0],cc[1]
if side_length*0.9 <= getLength(p1,p2) <= side_length*1.1 :
cyl = touch_tubez_cyl(p1,p2,cyl_rad_out,cyl_rad_in)
cylis.append( (3,cyl) )
"""Fusing all the tubes to make a single volume """
vol = factory.fuse(cylis[:len(cylis)//2],cylis[len(cylis)//2:])
finish_and_write(outfile)
This is the full code. Gitlab.py