{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "sys.path.append('..')\n", "\n", "from OCC.BRepPrimAPI import BRepPrimAPI_MakeTorus, BRepPrimAPI_MakeBox, BRepPrimAPI_MakeSphere\n", "from OCC.gp import gp_Vec\n", "from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer, NORMAL\n", "from OCC.GProp import GProp_GProps\n", "from OCC.BRepGProp import brepgprop_VolumeProperties\n", "\n", "from core_geometry_utils import translate_shp" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create 3 toruses\n", "# be careful to set copy to True or all the shapes will share the same mesh\n", "torus_shp = BRepPrimAPI_MakeTorus(20, 5).Shape()\n", "box_shp = translate_shp(BRepPrimAPI_MakeBox(10, 20, 3).Shape(), gp_Vec(60, 0, 0))\n", "sphere_shp = translate_shp(BRepPrimAPI_MakeSphere(20.).Shape(), gp_Vec(-60, 0, 0))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# use the NORMAL.CLIENT_SIDE in order to clearly see faces\n", "# in case the NORMAL.SERVER_SIDE option is used, vertex normals lead to\n", "# a smooth rendering\n", "my_renderer = JupyterRenderer()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#create and register a callback\n", "# this callback will compute and display shape volume each time a\n", "# shape is selectd\n", "def compute_volume(shp):\n", " props = GProp_GProps()\n", " brepgprop_VolumeProperties(shp, props)\n", " # Get inertia properties\n", " mass = props.Mass()\n", " cog = props.CentreOfMass()\n", " matrix_of_inertia = props.MatrixOfInertia()\n", " # Display inertia properties\n", " cog_x, cog_y, cog_z = cog.Coord()\n", " #print(\"Center of mass: x = %f;y = %f;z = %f;\" % (cog_x, cog_y, cog_z))\n", " html_value = \"Shape volumes = %s
\" % mass\n", " html_value += \"Center of mass: x = %f;y = %f;z = %f\" % (cog_x, cog_y, cog_z)\n", " my_renderer.html.value = html_value\n", "my_renderer.register_select_callback(compute_volume)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_renderer.DisplayShape(torus_shp, shape_color=\"blue\")\n", "my_renderer.DisplayShape(box_shp, shape_color=\"red\")\n", "my_renderer.DisplayShape(sphere_shp, shape_color=\"green\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_renderer.Display()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }