import sys
sys.path.append('..')
from OCC.BRepPrimAPI import BRepPrimAPI_MakeTorus, BRepPrimAPI_MakeBox, BRepPrimAPI_MakeSphere
from OCC.gp import gp_Vec
from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer, NORMAL
from OCC.GProp import GProp_GProps
from OCC.BRepGProp import brepgprop_VolumeProperties
from core_geometry_utils import translate_shp
# create 3 toruses
# be careful to set copy to True or all the shapes will share the same mesh
torus_shp = BRepPrimAPI_MakeTorus(20, 5).Shape()
box_shp = translate_shp(BRepPrimAPI_MakeBox(10, 20, 3).Shape(), gp_Vec(60, 0, 0))
sphere_shp = translate_shp(BRepPrimAPI_MakeSphere(20.).Shape(), gp_Vec(-60, 0, 0))
# use the NORMAL.CLIENT_SIDE in order to clearly see faces
# in case the NORMAL.SERVER_SIDE option is used, vertex normals lead to
# a smooth rendering
my_renderer = JupyterRenderer()
#create and register a callback
# this callback will compute and display shape volume each time a
# shape is selectd
def compute_volume(shp):
props = GProp_GProps()
brepgprop_VolumeProperties(shp, props)
# Get inertia properties
mass = props.Mass()
cog = props.CentreOfMass()
matrix_of_inertia = props.MatrixOfInertia()
# Display inertia properties
cog_x, cog_y, cog_z = cog.Coord()
#print("Center of mass: x = %f;y = %f;z = %f;" % (cog_x, cog_y, cog_z))
html_value = "Shape volumes = %s<br>" % mass
html_value += "Center of mass: x = %f;y = %f;z = %f" % (cog_x, cog_y, cog_z)
my_renderer.html.value = html_value
my_renderer.register_select_callback(compute_volume)
my_renderer.DisplayShape(torus_shp, shape_color="blue")
my_renderer.DisplayShape(box_shp, shape_color="red")
my_renderer.DisplayShape(sphere_shp, shape_color="green")
my_renderer.Display()