def __init__( self, size=(640, 480), compute_normals_mode=NORMAL.SERVER_SIDE, default_shape_color=format_color(166, 166, 166), # light grey default_edge_color=format_color(32, 32, 32), # dark grey default_vertex_color=format_color(8, 8, 8), # darker grey pick_color=format_color(232, 176, 36), # orange background_color="white", ): super().__init__( size, compute_normals_mode, default_shape_color, default_edge_color, default_vertex_color, pick_color, background_color, ) from ipywidgets import Dropdown self._toggle_geom_visibility_button = self.create_button( "Geom", "Toggle Geom Visibility", False, self.toggle_all_geom_visibility) self._toggle_mesh_visibility_button = self.create_button( "Mesh", "Toggle Mesh Visibility", False, self.toggle_mesh_visibility) self._controls.pop(0) self._controls.pop(0) fem_sets = ["None"] self._fem_sets_opts = Dropdown(options=fem_sets, value=fem_sets[0], tooltip="Select a set", disabled=False) self._fem_sets_opts.observe(self._on_changed_fem_set, "value") self._controls.insert(0, self._toggle_geom_visibility_button) self._controls.insert(1, self._toggle_mesh_visibility_button) self._controls.pop(-1) self._controls.pop(-1) self._controls.append(self._fem_sets_opts) # self._controls.append(p1) # self._controls.append(p2) self._refs = dict() self._fem_refs = dict()
def colour_webgl(self): from OCC.Display.WebGl.jupyter_renderer import format_color if self.colour is None: return None if self.colour[0] == -1 and self.colour[1] == -1 and self.colour[ 2] == -1: return None if self.colour[0] <= 1.0: colour = [int(x * 255) for x in self.colour] else: colour = [int(x) for x in self.colour] colour_formatted = format_color(*colour) return colour_formatted
def DisplayMesh(self, part: "Part", edge_color=None, vertex_color=None, vertex_width=2): from OCC.Core.BRep import BRep_Builder from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeVertex from OCC.Core.gp import gp_Pnt from OCC.Core.TopoDS import TopoDS_Compound # edge_color = format_color(*part.colour) if edge_color is None else edge_color rgb = randint(0, 255), randint(0, 255), randint(0, 255) edge_color = format_color(*rgb) if edge_color is None else edge_color vertex_color = self._default_vertex_color if vertex_color is None else vertex_color pmesh_id = "%s" % uuid.uuid4().hex BB = BRep_Builder() compound = TopoDS_Compound() BB.MakeCompound(compound) vertices_list = [] def togp(n_): return gp_Pnt(float(n_[0]), float(n_[1]), float(n_[2])) for vertex in map(togp, part.fem.nodes): vertex_to_add = BRepBuilderAPI_MakeVertex(vertex).Shape() BB.Add(compound, vertex_to_add) vertices_list.append([vertex.X(), vertex.Y(), vertex.Z()]) attributes = { "position": BufferAttribute(vertices_list, normalized=False) } mat = PointsMaterial(color=vertex_color, sizeAttenuation=False, size=vertex_width) geom = BufferGeometry(attributes=attributes) points_geom = Points(geometry=geom, material=mat, name=pmesh_id) lmesh_id = "%s" % uuid.uuid4().hex edges_nodes = list( chain.from_iterable( filter( None, [get_vertices_from_elem(el) for el in part.fem.elements]))) np_edge_vertices = np.array(edges_nodes, dtype=np.float32) np_edge_indices = np.arange(np_edge_vertices.shape[0], dtype=np.uint32) vertex_col = tuple([x / 255 for x in rgb]) edge_geometry = BufferGeometry( attributes={ "position": BufferAttribute(np_edge_vertices), "index": BufferAttribute(np_edge_indices), "color": BufferAttribute( [vertex_col for n in np_edge_vertices]), }) edge_material = LineBasicMaterial(vertexColors="VertexColors", linewidth=5) edge_geom = LineSegments( geometry=edge_geometry, material=edge_material, type="LinePieces", name=lmesh_id, ) output = [points_geom, edge_geom] for elem in output: self._shapes[elem.name] = compound self._refs[elem.name] = part self._displayed_pickable_objects.add(elem) self._fem_sets_opts.options = ["None"] + [ f"{part.fem.name}.{s.name}" for s in filter( lambda x: "internal" not in x.metadata.keys(), part.fem.sets) ] self._fem_refs[part.fem.name] = (part.fem, edge_geometry)
settings = ifcopenshell.geom.settings() settings.set(settings.USE_PYTHON_OPENCASCADE, True) # tells ifcopenshell to use pythonocc # In[ ]: ifc_file = ifcopenshell.open(ifc_filename) # In[ ]: products = ifc_file.by_type("IfcProduct") # traverse all IfcProducts # In[ ]: for product in products: if product.Representation is not None: # some IfcProducts don't have any 3d representation pdct_shape = ifcopenshell.geom.create_shape(settings, inst=product) r, g, b, alpha = pdct_shape.styles[0] # the shape color color = format_color(int(abs(r) * 255), int(abs(g) * 255), int(abs(b) * 255)) # below, the pdct_shape.geometry is a TopoDS_Shape, i.e. can be rendered using # any renderer (threejs, x3dom, jupyter, qt5 based etc.) my_renderer.DisplayShape(pdct_shape.geometry, shape_color=color, transparency=True, opacity=alpha) # In[ ]: my_renderer
# In[ ]: from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer, format_color from OCC.Extend.TopologyUtils import TopologyExplorer from OCC.Extend.DataExchange import read_step_file # In[ ]: shp = read_step_file( os.path.join('..', 'assets', 'models', 'RC_Buggy_2_front_suspension.stp')) # In[ ]: my_renderer = JupyterRenderer(size=(700, 700)) # In[ ]: # loop over subshapes so that each subshape is meshed/displayed t = TopologyExplorer(shp) for solid in t.solids(): # random colors, just for fun random_color = format_color(randint(0, 255), randint(0, 255), randint(0, 255)) my_renderer.DisplayShape(solid, shape_color=random_color, render_edges=True) # In[ ]: my_renderer.Display()
def DisplayMesh(self, part, edge_color=None, vertex_color=None, vertex_width=2): """ :param part: :param edge_color: :param vertex_color: :param vertex_width: :type part: ada.Part """ from itertools import chain from random import randint from OCC.Core.BRep import BRep_Builder from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeVertex from OCC.Core.gp import gp_Pnt from OCC.Core.TopoDS import TopoDS_Compound # edge_color = format_color(*part.colour) if edge_color is None else edge_color edge_color = (format_color(randint(0, 255), randint( 0, 255), randint(0, 255)) if edge_color is None else edge_color) vertex_color = self._default_vertex_color if vertex_color is None else vertex_color pmesh_id = "%s" % uuid.uuid4().hex BB = BRep_Builder() compound = TopoDS_Compound() BB.MakeCompound(compound) vertices_list = [] def togp(n_): return gp_Pnt(float(n_[0]), float(n_[1]), float(n_[2])) for vertex in map(togp, part.fem.nodes): vertex_to_add = BRepBuilderAPI_MakeVertex(vertex).Shape() BB.Add(compound, vertex_to_add) vertices_list.append([vertex.X(), vertex.Y(), vertex.Z()]) attributes = { "position": BufferAttribute(vertices_list, normalized=False) } mat = PointsMaterial(color=vertex_color, sizeAttenuation=False, size=vertex_width) geom = BufferGeometry(attributes=attributes) points_geom = Points(geometry=geom, material=mat, name=pmesh_id) def grab_nodes(el): """ :param el: :type el: ada.fem.Elem :return: """ if el.edges_seq is None: return None return [ part.fem.nodes.from_id(i).p for i in [el.nodes[e].id for ed_seq in el.edges_seq for e in ed_seq] ] lmesh_id = "%s" % uuid.uuid4().hex edges_nodes = list( chain.from_iterable( filter(None, map(grab_nodes, part.fem.elements)))) np_edge_vertices = np.array(edges_nodes, dtype=np.float32) np_edge_indices = np.arange(np_edge_vertices.shape[0], dtype=np.uint32) edge_geometry = BufferGeometry( attributes={ "position": BufferAttribute(np_edge_vertices), "index": BufferAttribute(np_edge_indices), }) edge_material = LineBasicMaterial(color=edge_color, linewidth=1) edge_geom = LineSegments( geometry=edge_geometry, material=edge_material, type="LinePieces", name=lmesh_id, ) output = [points_geom, edge_geom] for elem in output: self._shapes[elem.name] = compound self._refs[elem.name] = part self._displayed_pickable_objects.add(elem) self._fem_sets_opts.options = ["None"] + [ s.name for s in filter( lambda x: "internal" not in x.metadata.keys(), part.fem.sets) ]
def random_color(): from random import randint from OCC.Display.WebGl.jupyter_renderer import format_color return format_color(randint(0, 255), randint(0, 255), randint(0, 255))