Exemplo n.º 1
0
    def AddCurveToScene(self, shp, edge_color, deflection):
        """ shp is either a TopoDS_Wire or a TopodS_Edge.
        """
        if is_edge(shp):
            pnts = discretize_edge(shp, deflection)
        elif is_wire(shp):
            pnts = discretize_wire(shp, deflection)
        np_edge_vertices = np.array(pnts, 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)

        # and to the dict of shapes, to have a mapping between meshes and shapes
        edge_id = "%s" % uuid.uuid4().hex
        self._shapes[edge_id] = shp

        edge_line = Line(geometry=edge_geometry,
                         material=edge_material,
                         name=edge_id)

        # and to the dict of shapes, to have a mapping between meshes and shapes
        edge_id = "%s" % uuid.uuid4().hex
        self._shapes[edge_id] = shp

        return edge_line
Exemplo n.º 2
0
 def DisplayShape(self,
                  shape,
                  vertex_shader=None,
                  fragment_shader=None,
                  export_edges=False,
                  color=(0.65, 0.65, 0.7),
                  specular_color=(0.2, 0.2, 0.2),
                  shininess=0.9,
                  transparency=0.,
                  line_color=(0, 0., 0.),
                  line_width=2.,
                  mesh_quality=1.):
     """ Adds a shape to the rendering buffer. This class computes the x3d file
     """
     # if the shape is an edge or a wire, use the related functions
     if is_edge(shape):
         print("X3D exporter, discretize an edge")
         pnts = discretize_edge(shape)
         edge_hash = "edg%s" % uuid.uuid4().hex
         line_set = ExportEdgeToILS(pnts)
         x3dfile_content = indexed_lineset_to_x3d_string([line_set], ils_id=edge_hash)
         edge_full_path = os.path.join(self._path, edge_hash + '.x3d')
         with open(edge_full_path, "w") as edge_file:
             edge_file.write(x3dfile_content)
         # store this edge hash
         self._x3d_edges[edge_hash] = [color, line_width]
         return self._x3d_shapes, self._x3d_edges
     elif is_wire(shape):
         print("X3D exporter, discretize a wire")
         pnts = discretize_wire(shape)
         wire_hash = "wir%s" % uuid.uuid4().hex
         line_set = ExportEdgeToILS(pnts)
         x3dfile_content = indexed_lineset_to_x3d_string([line_set], ils_id=wire_hash)
         wire_full_path = os.path.join(self._path, wire_hash + '.x3d')
         with open(wire_full_path, "w") as wire_file:
             wire_file.write(x3dfile_content)
         # store this edge hash
         self._x3d_edges[wire_hash] = [color, line_width]
         return self._x3d_shapes, self._x3d_edges
     shape_uuid = uuid.uuid4().hex
     shape_hash = "shp%s" % shape_uuid
     x3d_exporter = X3DExporter(shape, vertex_shader, fragment_shader,
                                export_edges, color,
                                specular_color, shininess, transparency,
                                line_color, line_width, mesh_quality)
     x3d_exporter.compute()
     x3d_filename = os.path.join(self._path, "%s.x3d" % shape_hash)
     # the x3d filename is computed from the shape hash
     shape_id = len(self._x3d_shapes)
     x3d_exporter.write_to_file(x3d_filename, shape_id)
     ## TODO : orientation and translation
     # get shape translation and orientation
     # trans = shape.Location().Transformation().TranslationPart().Coord()  # vector
     # v = gp_Vec()
     # angle = shape.Location().Transformation().GetRotation().GetVectorAndAngle(v)
     # ori = (v.X(), v.Y(), v.Z(), angle)  # angles
     # fill the shape dictionnary with shape hash, translation and orientation
     #  self._x3d_shapes[shape_hash] = [trans, ori]
     self._x3d_shapes[shape_hash] = [export_edges, color, specular_color, shininess, transparency, line_color, line_width]
     return self._x3d_shapes, self._x3d_edges
Exemplo n.º 3
0
 def DisplayShape(self,
                  shape,
                  vertex_shader=None,
                  fragment_shader=None,
                  export_edges=False,
                  color=(0.65, 0.65, 0.7),
                  specular_color=(0.2, 0.2, 0.2),
                  shininess=0.9,
                  transparency=0.,
                  line_color=(0, 0., 0.),
                  line_width=2.,
                  mesh_quality=1.):
     """ Adds a shape to the rendering buffer. This class computes the x3d file
     """
     # if the shape is an edge or a wire, use the related functions
     if is_edge(shape):
         print("X3D exporter, discretize an edge")
         pnts = discretize_edge(shape)
         edge_hash = "edg%s" % uuid.uuid4().hex
         line_set = ExportEdgeToILS(pnts)
         x3dfile_content = indexed_lineset_to_x3d_string([line_set], ils_id=edge_hash)
         edge_full_path = os.path.join(self._path, edge_hash + '.x3d')
         with open(edge_full_path, "w") as edge_file:
             edge_file.write(x3dfile_content)
         # store this edge hash
         self._x3d_edges[edge_hash] = [color, line_width]
         return self._x3d_shapes, self._x3d_edges
     elif is_wire(shape):
         print("X3D exporter, discretize a wire")
         pnts = discretize_wire(shape)
         wire_hash = "wir%s" % uuid.uuid4().hex
         line_set = ExportEdgeToILS(pnts)
         x3dfile_content = indexed_lineset_to_x3d_string([line_set], ils_id=wire_hash)
         wire_full_path = os.path.join(self._path, wire_hash + '.x3d')
         with open(wire_full_path, "w") as wire_file:
             wire_file.write(x3dfile_content)
         # store this edge hash
         self._x3d_edges[wire_hash] = [color, line_width]
         return self._x3d_shapes, self._x3d_edges
     shape_uuid = uuid.uuid4().hex
     shape_hash = "shp%s" % shape_uuid
     x3d_exporter = X3DExporter(shape, vertex_shader, fragment_shader,
                                export_edges, color,
                                specular_color, shininess, transparency,
                                line_color, line_width, mesh_quality)
     x3d_exporter.compute()
     x3d_filename = os.path.join(self._path, "%s.x3d" % shape_hash)
     # the x3d filename is computed from the shape hash
     shape_id = len(self._x3d_shapes)
     x3d_exporter.write_to_file(x3d_filename, shape_id)
     ## TODO : orientation and translation
     # get shape translation and orientation
     # trans = shape.Location().Transformation().TranslationPart().Coord()  # vector
     # v = gp_Vec()
     # angle = shape.Location().Transformation().GetRotation().GetVectorAndAngle(v)
     # ori = (v.X(), v.Y(), v.Z(), angle)  # angles
     # fill the shape dictionnary with shape hash, translation and orientation
     #  self._x3d_shapes[shape_hash] = [trans, ori]
     self._x3d_shapes[shape_hash] = [export_edges, color, specular_color, shininess, transparency, line_color, line_width]
     return self._x3d_shapes, self._x3d_edges
Exemplo n.º 4
0
    def DisplayShape(self,
                     shape,
                     vertex_shader=None,
                     fragment_shader=None,
                     export_edges=False,
                     color=(0.65, 0.65, 0.7),
                     specular_color=(0.2, 0.2, 0.2),
                     shininess=0.9,
                     transparency=0.,
                     line_color=(0, 0., 0.),
                     line_width=2.,
                     mesh_quality=1.):
        """ Adds a shape to the rendering buffer. This class computes the x3d file
        """
        # if the shape is an edge or a wire, use the related functions
        if is_edge(shape):
            print("X3D exporter, discretize an edge")
            pnts = discretize_edge(shape)
            edge_hash = "edg%s" % uuid.uuid4().hex
            line_set = export_edge_to_indexed_lineset(pnts)
            x3dfile_content = indexed_lineset_to_x3d_string([line_set],
                                                            ils_id=edge_hash)
            edge_full_path = os.path.join(self._path, edge_hash + '.x3d')
            with open(edge_full_path, "w") as edge_file:
                edge_file.write(x3dfile_content)
            # store this edge hash
            self._x3d_edges[edge_hash] = [color, line_width]
            return self._x3d_shapes, self._x3d_edges

        if is_wire(shape):
            print("X3D exporter, discretize a wire")
            pnts = discretize_wire(shape)
            wire_hash = "wir%s" % uuid.uuid4().hex
            line_set = export_edge_to_indexed_lineset(pnts)
            x3dfile_content = indexed_lineset_to_x3d_string([line_set],
                                                            ils_id=wire_hash)
            wire_full_path = os.path.join(self._path, wire_hash + '.x3d')
            with open(wire_full_path, "w") as wire_file:
                wire_file.write(x3dfile_content)
            # store this edge hash
            self._x3d_edges[wire_hash] = [color, line_width]
            return self._x3d_shapes, self._x3d_edges

        shape_uuid = uuid.uuid4().hex
        shape_hash = "shp%s" % shape_uuid
        x3d_exporter = X3DExporter(shape, vertex_shader, fragment_shader,
                                   export_edges, color, specular_color,
                                   shininess, transparency, line_color,
                                   line_width, mesh_quality)
        x3d_exporter.compute()
        x3d_filename = os.path.join(self._path, "%s.x3d" % shape_hash)
        # the x3d filename is computed from the shape hash
        shape_id = len(self._x3d_shapes)
        x3d_exporter.write_to_file(x3d_filename, shape_id)

        self._x3d_shapes[shape_hash] = [
            export_edges, color, specular_color, shininess, transparency,
            line_color, line_width
        ]
        return self._x3d_shapes, self._x3d_edges
Exemplo n.º 5
0
 def drawShape(self,
                  shape,
                  color=(0.65, 0.65, 0.7),
                  transparency=0.,
                  line_width=1.,
                 ):
     # if the shape is an edge or a wire, use the related functions
     shininess=0.9
     specular_color=(0.2, 0.2, 0.2)
     shape_precision, wire_precision = self.precision
     if is_edge(shape):
         print("discretize an edge")
         pnts = discretize_edge(shape, wire_precision)
         edge_hash = "exp_%s_edge" % str(self.shapeNum).zfill(3)
         self.shapeNum += 1
         str_to_write = export_edgedata_to_json(edge_hash, pnts)
         edge_full_path = os.path.join(self._path, edge_hash + '.json')
         with open(edge_full_path, "w") as edge_file:
             edge_file.write(str_to_write)
         # store this edge hash
         self.stringList.append("\tzdeskXCurve(slidePath+'%s.json', %s, %g);\n"
                                     % (edge_hash, color_to_hex(color), line_width))
         print("%s, %i segments" % (edge_hash ,len(pnts)-1))
     elif is_wire(shape):
         print("discretize a wire")
         pnts = discretize_wire(shape, wire_precision)
         wire_hash = "exp_%s_wire" % str(self.shapeNum).zfill(3)
         self.shapeNum += 1
         str_to_write = export_edgedata_to_json(wire_hash, pnts)
         wire_full_path = os.path.join(self._path, wire_hash + '.json')
         with open(wire_full_path, "w") as wire_file:
             wire_file.write(str_to_write)
         # store this edge hash
         self.stringList.append("\tzdeskXCurve(slidePath+'%s.json', %s, %g);\n"
                                     % (wire_hash, color_to_hex(color), line_width))
         print("%s, %i segments" % (wire_hash ,len(pnts)-1))
     else: #solid or shell
         print("tesselate a shape")
         shape_uuid = uuid.uuid4().hex
         shape_hash = "exp_%s_shape" % str(self.shapeNum).zfill(3)
         self.shapeNum += 1
         # tesselate
         tess = ShapeTesselator(shape)
         tess.Compute(compute_edges=False,
                      mesh_quality=shape_precision,
                      parallel=True)
         # export to 3JS
         shape_full_path = os.path.join(self._path, shape_hash + '.json')
         with open(shape_full_path, 'w') as json_file:
             json_file.write(tess.ExportShapeToThreejsJSONString(shape_uuid))
         self.stringList.append("\tzdeskXShape(slidePath+'%s.json', %s, %s, %g, %g);\n"
                                     % (shape_hash, color_to_hex(color), color_to_hex(specular_color), shininess, 1 - transparency))
         print("%s, %i triangles\n" % (shape_hash, tess.ObjGetTriangleCount()))
Exemplo n.º 6
0
 def drawShape(self,  shape):
     shape_precision, wire_precision = self.precision
     if is_edge(shape):
         pass
     elif is_wire(shape):
         pass
     else: #solid or shell
         print("export shape %s to STL start", str(self.shapeNum).zfill(3))
         shape_hash = "exp_%s_shape" % str(self.shapeNum).zfill(3)
         shape_full_path = os.path.join(self._path, shape_hash + '.stl')
         write_stl_file(shape, shape_full_path, "ascii", shape_precision/4, 0.5/4)
         print("export shape %s to STL done", str(self.shapeNum).zfill(3))
         self.shapeNum += 1
 def DisplayShape(
         self,
         shp,  # the TopoDS_Shape to be displayed
         shape_color=default_shape_color,  # the default
         render_edges=False,
         edge_color=default_edge_color,
         compute_uv_coords=False,
         quality=1.0,
         transparency=False,
         opacity=1.,
         topo_level='default',
         update=False):
     """ Displays a topods_shape in the renderer instance.
     shp: the TopoDS_Shape to render
     shape_color: the shape color, in html corm, eg '#abe000'
     render_edges: optional, False by default. If True, compute and dislay all
                   edges as a linear interpolation of segments.
     edge_color: optional, black by default. The color used for edge rendering,
                 in html form eg '#ff00ee'
     compute_uv_coords: optional, false by default. If True, compute texture
                        coordinates (required if the shape has to be textured)
     quality: optional, 1.0 by default. If set to something lower than 1.0,
                   mesh will be more precise. If set to something higher than 1.0,
                   mesh will be less precise, i.e. lower numer of triangles.
     transparency: optional, False by default (opaque).
     opacity: optional, float, by default to 1 (opaque). if transparency is set to True,
              0. is fully opaque, 1. is fully transparent.
     topo_level: "default" by default. The value should be either "compound", "shape", "vertex".
     update: optional, False by default. If True, render all the shapes.
     """
     if is_wire(shp) or is_edge(shp):
         self.AddCurveToScene(shp, shape_color)
     if topo_level != "default":
         t = TopologyExplorer(shp)
         map_type_and_methods = {
             "Solid": t.solids,
             "Face": t.faces,
             "Shell": t.shells,
             "Compound": t.compounds,
             "Compsolid": t.comp_solids
         }
         for subshape in map_type_and_methods[topo_level]():
             self.AddShapeToScene(subshape, shape_color, render_edges,
                                  edge_color, compute_uv_coords, quality,
                                  transparency, opacity)
     else:
         self.AddShapeToScene(shp, shape_color, render_edges, edge_color,
                              compute_uv_coords, quality, transparency,
                              opacity)
     if update:
         self.Display()
Exemplo n.º 8
0
    def AddCurveToScene(self, shp, color):
        """ shp is either a TopoDS_Wire or a TopodS_Edge.
        """
        if is_edge(shp):
            pnts = discretize_edge(shp)
        elif is_wire(shp):
            pnts = discretize_wire(shp)
        np_edge_vertices = np.array(pnts, 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=color, linewidth=1)
        edge_lines = Line(geometry=edge_geometry, material=edge_material)

        # Add geometries to pickable or non pickable objects
        self._displayed_pickable_objects.add(edge_lines)
Exemplo n.º 9
0
    def AddCurveToScene(self, shp, color):
        """ shp is either a TopoDS_Wire or a TopodS_Edge.
        """
        if is_edge(shp):
            pnts = discretize_edge(shp)
        elif is_wire(shp):
            pnts = discretize_wire(shp)
        np_edge_vertices = np.array(pnts, 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=color, linewidth=2, fog=True)
        edge_lines = LineSegments(geometry=edge_geometry, material=edge_material)

        # Add geometries to pickable or non pickable objects
        self._displayed_pickable_objects.add(edge_lines)
Exemplo n.º 10
0
 def DisplayShape(self,
                  shp,  # the TopoDS_Shape to be displayed
                  shape_color=default_shape_color,  # the default
                  render_edges=False,
                  edge_color=default_edge_color,
                  compute_uv_coords=False,
                  quality=1.0,
                  transparency=False,
                  opacity=1.,
                  topo_level='default',
                  update=False):
     """ Displays a topods_shape in the renderer instance.
     shp: the TopoDS_Shape to render
     shape_color: the shape color, in html corm, eg '#abe000'
     render_edges: optional, False by default. If True, compute and dislay all
                   edges as a linear interpolation of segments.
     edge_color: optional, black by default. The color used for edge rendering,
                 in html form eg '#ff00ee'
     compute_uv_coords: optional, false by default. If True, compute texture
                        coordinates (required if the shape has to be textured)
     quality: optional, 1.0 by default. If set to something lower than 1.0,
                   mesh will be more precise. If set to something higher than 1.0,
                   mesh will be less precise, i.e. lower numer of triangles.
     transparency: optional, False by default (opaque).
     opacity: optional, float, by default to 1 (opaque). if transparency is set to True,
              0. is fully opaque, 1. is fully transparent.
     topo_level: "default" by default. The value should be either "compound", "shape", "vertex".
     update: optional, False by default. If True, render all the shapes.
     """
     if is_wire(shp) or is_edge(shp):
         self.AddCurveToScene(shp, shape_color)
     if topo_level != "default":
         t = TopologyExplorer(shp)
         map_type_and_methods = {"Solid": t.solids, "Face": t.faces, "Shell": t.shells,
                                 "Compound": t.compounds, "Compsolid": t.comp_solids}
         for subshape in map_type_and_methods[topo_level]():
             self.AddShapeToScene(subshape, shape_color, render_edges, edge_color, compute_uv_coords, quality,
                                  transparency, opacity)
     else:
         self.AddShapeToScene(shp, shape_color, render_edges, edge_color, compute_uv_coords, quality,
                              transparency, opacity)
     if update:
         self.Display()
Exemplo n.º 11
0
 def DisplayShape(self,
                  shape,
                  export_edges=False,
                  color=(0.65, 0.65, 0.7),
                  specular_color=(0.2, 0.2, 0.2),
                  shininess=0.9,
                  transparency=0.,
                  line_color=(0, 0., 0.),
                  line_width=2.,
                  mesh_quality=1.):
     # if the shape is an edge or a wire, use the related functions
     if is_edge(shape):
         print("discretize an edge")
         pnts = discretize_edge(shape)
         edge_hash = "edg%s" % uuid.uuid4().hex
         str_to_write = export_edgedata_to_json(edge_hash, pnts)
         edge_full_path = os.path.join(self._path, edge_hash + '.json')
         with open(edge_full_path, "w") as edge_file:
             edge_file.write(str_to_write)
         # store this edge hash
         self._3js_edges[edge_hash] = [color, line_width]
         return self._3js_shapes, self._3js_edges
     elif is_wire(shape):
         print("discretize a wire")
         pnts = discretize_wire(shape)
         wire_hash = "wir%s" % uuid.uuid4().hex
         str_to_write = export_edgedata_to_json(wire_hash, pnts)
         wire_full_path = os.path.join(self._path, wire_hash + '.json')
         with open(wire_full_path, "w") as wire_file:
             wire_file.write(str_to_write)
         # store this edge hash
         self._3js_edges[wire_hash] = [color, line_width]
         return self._3js_shapes, self._3js_edges
     shape_uuid = uuid.uuid4().hex
     shape_hash = "shp%s" % shape_uuid
     # tesselate
     tess = Tesselator(shape)
     tess.Compute(compute_edges=export_edges,
                  mesh_quality=mesh_quality,
                  uv_coords=False,
                  parallel=True)
     # update spinning cursor
     sys.stdout.write("\r%s mesh shape %s, %i triangles     " % (next(self.spinning_cursor),
                                                                 shape_hash,
                                                                 tess.ObjGetTriangleCount()))
     sys.stdout.flush()
     # export to 3JS
     shape_full_path = os.path.join(self._path, shape_hash + '.json')
     # add this shape to the shape dict, sotres everything related to it
     self._3js_shapes[shape_hash] = [export_edges, color, specular_color, shininess, transparency, line_color, line_width]
     # generate the mesh
     #tess.ExportShapeToThreejs(shape_hash, shape_full_path)
     # and also to JSON
     with open(shape_full_path, 'w') as json_file:
         json_file.write(tess.ExportShapeToThreejsJSONString(shape_uuid))
     # draw edges if necessary
     if export_edges:
         # export each edge to a single json
         # get number of edges
         nbr_edges = tess.ObjGetEdgeCount()
         for i_edge in range(nbr_edges):
             # after that, the file can be appended
             str_to_write = ''
             edge_point_set = []
             nbr_vertices = tess.ObjEdgeGetVertexCount(i_edge)
             for i_vert in range(nbr_vertices):
                 edge_point_set.append(tess.GetEdgeVertex(i_edge, i_vert))
             # write to file
             edge_hash = "edg%s" % uuid.uuid4().hex
             str_to_write += export_edgedata_to_json(edge_hash, edge_point_set)
             # create the file
             edge_full_path = os.path.join(self._path, edge_hash + '.json')
             with open(edge_full_path, "w") as edge_file:
                 edge_file.write(str_to_write)
             # store this edge hash, with black color
             self._3js_edges[hash] = [(0, 0, 0), line_width]
     return self._3js_shapes, self._3js_edges
Exemplo n.º 12
0
    def DisplayShape(
        self,
        shp,
        shape_color=None,
        render_edges=False,
        edge_color=None,
        edge_deflection=0.05,
        vertex_color=None,
        quality=1.0,
        transparency=False,
        opacity=1.0,
        topo_level="default",
        update=False,
        selectable=True,
    ):
        """
        Displays a topods_shape in the renderer instance.

        :param shp: the TopoDS_Shape to render
        :param shape_color: the shape color, in html corm, eg '#abe000'
        :param render_edges: optional, False by default. If True, compute and dislay all
                      edges as a linear interpolation of segments.

        :param edge_color: optional, black by default. The color used for edge rendering,
                    in html form eg '#ff00ee'

        :param edge_deflection: optional, 0.05 by default
        :param vertex_color: optional
        :param quality: optional, 1.0 by default. If set to something lower than 1.0,
                      mesh will be more precise. If set to something higher than 1.0,
                      mesh will be less precise, i.e. lower numer of triangles.

        :param transparency: optional, False by default (opaque).
        :param opacity: optional, float, by default to 1 (opaque). if transparency is set to True,
                 0. is fully opaque, 1. is fully transparent.

        :param topo_level: "default" by default. The value should be either "compound", "shape", "vertex".
        :param update: optional, False by default. If True, render all the shapes.
        :param selectable: if True, can be doubleclicked from the 3d window
        """
        if edge_color is None:
            edge_color = self._default_edge_color
        if shape_color is None:
            shape_color = self._default_shape_color
        if vertex_color is None:
            vertex_color = self._default_vertex_color

        output = []  # a list of all geometries created from the shape
        # is it list of gp_Pnt ?
        from OCC.Core.gp import gp_Pnt
        from OCC.Extend.TopologyUtils import is_edge, is_wire

        if isinstance(shp, list) and isinstance(shp[0], gp_Pnt):
            result = self.AddVerticesToScene(shp, vertex_color)
            output.append(result)
        # or a 1d element such as edge or wire ?
        elif is_wire(shp) or is_edge(shp):
            result = self.AddCurveToScene(shp, edge_color, edge_deflection)
            output.append(result)
        elif topo_level != "default":
            from OCC.Extend.TopologyUtils import TopologyExplorer

            t = TopologyExplorer(shp)
            map_type_and_methods = {
                "Solid": t.solids,
                "Face": t.faces,
                "Shell": t.shells,
                "Compound": t.compounds,
                "Compsolid": t.comp_solids,
            }
            for subshape in map_type_and_methods[topo_level]():
                result = self.AddShapeToScene(
                    subshape,
                    shape_color,
                    render_edges,
                    edge_color,
                    vertex_color,
                    quality,
                    transparency,
                    opacity,
                )
                output.append(result)
        else:
            result = self.AddShapeToScene(
                shp,
                shape_color,
                render_edges,
                edge_color,
                vertex_color,
                quality,
                transparency,
                opacity,
            )
            output.append(result)

        if selectable:  # Add geometries to pickable or non pickable objects
            for elem in output:
                self._displayed_pickable_objects.add(elem)

        if update:
            self.Display()

        return output
Exemplo n.º 13
0
 def DisplayShape(self,
                  shape,
                  export_edges=False,
                  color=(0.65, 0.65, 0.7),
                  specular_color=(0.2, 0.2, 0.2),
                  shininess=0.9,
                  transparency=0.,
                  line_color=(0, 0., 0.),
                  line_width=2.,
                  mesh_quality=1.):
     # if the shape is an edge or a wire, use the related functions
     if is_edge(shape):
         print("discretize an edge")
         pnts = discretize_edge(shape)
         edge_hash = "edg%s" % uuid.uuid4().hex
         str_to_write = export_edgedata_to_json(edge_hash, pnts)
         edge_full_path = os.path.join(self._path, edge_hash + '.json')
         with open(edge_full_path, "w") as edge_file:
             edge_file.write(str_to_write)
         # store this edge hash
         self._3js_edges[edge_hash] = [color, line_width]
         return self._3js_shapes, self._3js_edges
     elif is_wire(shape):
         print("discretize a wire")
         pnts = discretize_wire(shape)
         wire_hash = "wir%s" % uuid.uuid4().hex
         str_to_write = export_edgedata_to_json(wire_hash, pnts)
         wire_full_path = os.path.join(self._path, wire_hash + '.json')
         with open(wire_full_path, "w") as wire_file:
             wire_file.write(str_to_write)
         # store this edge hash
         self._3js_edges[wire_hash] = [color, line_width]
         return self._3js_shapes, self._3js_edges
     shape_uuid = uuid.uuid4().hex
     shape_hash = "shp%s" % shape_uuid
     # tesselate
     tess = Tesselator(shape)
     tess.Compute(compute_edges=export_edges,
                  mesh_quality=mesh_quality,
                  uv_coords=False,
                  parallel=True)
     # update spinning cursor
     sys.stdout.write("\r%s mesh shape %s, %i triangles     " % (next(self.spinning_cursor),
                                                                 shape_hash,
                                                                 tess.ObjGetTriangleCount()))
     sys.stdout.flush()
     # export to 3JS
     shape_full_path = os.path.join(self._path, shape_hash + '.json')
     # add this shape to the shape dict, sotres everything related to it
     self._3js_shapes[shape_hash] = [export_edges, color, specular_color, shininess, transparency, line_color, line_width]
     # generate the mesh
     #tess.ExportShapeToThreejs(shape_hash, shape_full_path)
     # and also to JSON
     with open(shape_full_path, 'w') as json_file:
         json_file.write(tess.ExportShapeToThreejsJSONString(shape_uuid))
     # draw edges if necessary
     if export_edges:
         # export each edge to a single json
         # get number of edges
         nbr_edges = tess.ObjGetEdgeCount()
         for i_edge in range(nbr_edges):
             # after that, the file can be appended
             str_to_write = ''
             edge_point_set = []
             nbr_vertices = tess.ObjEdgeGetVertexCount(i_edge)
             for i_vert in range(nbr_vertices):
                 edge_point_set.append(tess.GetEdgeVertex(i_edge, i_vert))
             # write to file
             edge_hash = "edg%s" % uuid.uuid4().hex
             str_to_write += export_edgedata_to_json(edge_hash, edge_point_set)
             # create the file
             edge_full_path = os.path.join(self._path, edge_hash + '.json')
             with open(edge_full_path, "w") as edge_file:
                 edge_file.write(str_to_write)
             # store this edge hash, with black color
             self._3js_edges[hash] = [(0, 0, 0), line_width]
     return self._3js_shapes, self._3js_edges
Exemplo n.º 14
0
    def ConvertShape(
        self,
        shape,
        export_edges=False,
        color=(0.65, 0.65, 0.7),
        specular_color=(0.2, 0.2, 0.2),
        shininess=0.9,
        transparency=0.0,
        line_color=(0, 0.0, 0.0),
        line_width=1.0,
        point_size=1.0,
        mesh_quality=1.0,
    ):
        # if the shape is an edge or a wire, use the related functions
        color = color_to_hex(color)
        specular_color = color_to_hex(specular_color)
        if is_edge(shape):
            print("discretize an edge")
            pnts = discretize_edge(shape)
            edge_hash = "edg%s" % uuid.uuid4().hex
            shape_content = export_edgedata_to_json(edge_hash, pnts)
            # store this edge hash
            self._3js_edges[edge_hash] = [color, line_width, shape_content]
            return self._3js_shapes, self._3js_edges, self._3js_vertex
        elif is_wire(shape):
            print("discretize a wire")
            pnts = discretize_wire(shape)
            wire_hash = "wir%s" % uuid.uuid4().hex
            shape_content = export_edgedata_to_json(wire_hash, pnts)
            # store this edge hash
            self._3js_edges[wire_hash] = [color, line_width, shape_content]
            return self._3js_shapes, self._3js_edges, self._3js_vertex
        # if shape is array of gp_Pnt
        elif isinstance(shape, list) and isinstance(shape[0], gp_Pnt):
            print("storage points")
            vertices_list = []  # will be passed to javascript
            BB = BRep_Builder()
            compound = TopoDS_Compound()
            BB.MakeCompound(compound)

            for vertex in shape:
                vertext_to_add = BRepBuilderAPI_MakeVertex(vertex).Shape()
                BB.Add(compound, vertext_to_add)
                vertices_list.append([vertex.X(), vertex.Y(), vertex.Z()])
            points_hash = "pnt%s" % uuid.uuid4().hex
            # store this vertex hash. Note: TopoDS_Compound did not save now
            self._3js_vertex[points_hash] = [color, point_size, vertices_list]
            return self._3js_shapes, self._3js_edges, self._3js_vertex

        # convert as TopoDS_Shape
        shape_uuid = uuid.uuid4().hex
        shape_hash = "shp%s" % shape_uuid
        # tesselate
        tess = ShapeTesselator(shape)
        tess.Compute(compute_edges=export_edges,
                     mesh_quality=mesh_quality,
                     parallel=True)
        # update spinning cursor
        sys.stdout.write("\r%s mesh shape %s, %i triangles     " % (next(
            self.spinning_cursor), shape_hash, tess.ObjGetTriangleCount()))
        sys.stdout.flush()
        # export to 3JS
        # generate the mesh
        shape_content = tess.ExportShapeToThreejsJSONString(shape_uuid)
        # add this shape to the shape dict, sotres everything related to it
        self._3js_shapes[shape_hash] = [
            export_edges,
            color,
            specular_color,
            shininess,
            transparency,
            line_color,
            line_width,
            shape_content,
        ]
        # draw edges if necessary
        if export_edges:
            # export each edge to a single json
            # get number of edges
            nbr_edges = tess.ObjGetEdgeCount()
            for i_edge in range(nbr_edges):
                # after that, the file can be appended
                edge_content = ""
                edge_point_set = []
                nbr_vertices = tess.ObjEdgeGetVertexCount(i_edge)
                for i_vert in range(nbr_vertices):
                    edge_point_set.append(tess.GetEdgeVertex(i_edge, i_vert))
                # write to file
                edge_hash = "edg%s" % uuid.uuid4().hex
                edge_content += export_edgedata_to_json(
                    edge_hash, edge_point_set)
                # store this edge hash, with black color
                self._3js_edges[edge_hash] = [
                    color_to_hex((0, 0, 0)),
                    line_width,
                    edge_content,
                ]
        return self._3js_shapes, self._3js_edges, self._3js_vertex