Пример #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
Пример #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
Пример #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
Пример #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
Пример #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()))
Пример #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()
Пример #8
0
def measure_shape_mass_center_of_gravity(shape):
    """Returns the shape center of gravity
    Returns a gp_Pnt if requested (set as_Pnt to True)
    or a list of 3 coordinates, by default."""
    inertia_props = GProp_GProps()
    if is_edge(shape):
        brepgprop_LinearProperties(shape, inertia_props)
        mass_property = "Length"
    elif is_face(shape):
        brepgprop_SurfaceProperties(shape, inertia_props)
        mass_property = "Area"
    else:
        brepgprop_VolumeProperties(shape, inertia_props)
        mass_property = "Volume"
    cog = inertia_props.CentreOfMass()
    mass = inertia_props.Mass()
    return cog, mass, mass_property
Пример #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=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)
Пример #10
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)
Пример #11
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()
Пример #12
0
    def Write(self, path):
        from bcad.binterpreter.scl_context import SCLProjection
        p = SCLProjection(None)
        p.hlr_project(self.shapes)
        shapes = []
        for c in p.children:
            if c.shape != None:
                dump_topology_to_string(c.shape.get_shape())
                shapes.append(c.shape.get_shape())
        lines = []
        for s in shapes:
            lines.extend(self.collect_dumpable_shapes(s))
        #debug("Lines: %s"%(str(lines),))
        ctr = 0

        for l in lines:
            #debug ("l: %s"%(str(l),))
            if len(l)>1:
                s = l[0][:2]
                for p in l[1:]:
                    e = p[:2]
                    self.msp.add_line(s, e)
                    s = e
            # e = l[1][:2]
            # 
        self.doc.saveas(path)
        return
        
        for s in shapes:
            exp = TopExp_Explorer()
            exp.Init(s, TopAbs_EDGE)
            while exp.More():
                edge = exp.Value()
                if not is_edge(edge):
                    warning("Is not an edge.")
                if exp.Value().IsNull():
                    warning("TopoDS_Edge is null")

                curve_adaptor = BRepAdaptor_Curve(edge)
                first = curve_adaptor.FirstParameter()
                last = curve_adaptor.LastParameter()
                geom_curve = curve_adaptor.Curve()
                #edges.append(exp.Current())
                # first = topexp.FirstVertex(exp.Value())
                # last  = topexp.LastVertex(exp.Value())
                
                # # Take geometrical information from vertices.
                # pnt_first = BRep_Tool.Pnt(first)
                # pnt_last = BRep_Tool.Pnt(last)

                # a, b = BRep_Tool.Curve(exp.Value())
                exp.Next()
                if (geom_curve.GetType() == GeomAbs_Line):
                    debug("Is line")
                elif (geom_curve.GetType() == GeomAbs_Circle):
                    debug("Is circle")
                elif (geom_curve.GetType() == GeomAbs_Ellipse):
                    debug("Is ellipse")
                elif (geom_curve.GetType() == GeomAbs_Hyperbola):
                    debug("Is hyperbola")
                elif (geom_curve.GetType() == GeomAbs_Parabola):
                    debug("Is parabola")
                elif (geom_curve.GetType() == GeomAbs_BezierCurve):
                    debug("Is bezier")
                elif (geom_curve.GetType() == GeomAbs_BSplineCurve):
                    debug("Is bspline")
                elif (geom_curve.GetType() == GeomAbs_OffsetCurve):
                    debug("Is offset")
                elif (geom_curve.GetType() == GeomAbs_OtherCurve):
                    #debug("Is other")
                    continue
                    #self.msp.add_line()

                pts = discretize_edge(edge)
                debug("Curve[%i]: %s / %s"%(ctr, str(geom_curve.GetType()),str(edge)))
                ctr+=1
Пример #13
0
    def render(self, position=None, rotation=None, zoom=None):

        # Render all shapes
        for i, shape in enumerate(self.shapes):
            s = shape["shape"]
            # Assume that all are edges when first element is an edge
            if is_edge(s[0]):
                self._render_shape(i,
                                   edges=s,
                                   render_edges=True,
                                   edge_color=shape["color"],
                                   edge_width=3)
            elif is_vertex(s[0]):
                self._render_shape(i,
                                   vertices=s,
                                   render_edges=False,
                                   vertex_color=shape["color"],
                                   vertex_width=6)
            else:
                # shape has only 1 object, hence first=True
                self._render_shape(i,
                                   shape=s[0],
                                   render_edges=True,
                                   mesh_color=shape["color"])

        # Get the overall bounding box
        self.bb = BoundingBox([shape["shape"] for shape in self.shapes])

        bb_max = self.bb.max
        bb_diag = 2 * self.bb.diagonal

        # Set up camera
        camera_target = self.bb.center
        camera_position = self._scale(
            [1, 1, 1] if position is None else position)
        camera_zoom = 1.0 if zoom is None else zoom

        self.camera = CombinedCamera(position=camera_position,
                                     width=self.width,
                                     height=self.height,
                                     far=10 * bb_diag,
                                     orthoFar=10 * bb_diag)
        self.camera.up = (0.0, 0.0, 1.0)
        self.camera.lookAt(camera_target)
        self.camera.mode = 'orthographic'
        self.camera.position = camera_position
        if rotation is not None:
            self.camera.rotation = rotation

        # Set up lights in every of the 8 corners of the global bounding box
        key_lights = [
            DirectionalLight(color='white', position=position, intensity=0.12)
            for position in list(
                itertools.product((-bb_diag, bb_diag), (-bb_diag,
                                                        bb_diag), (-bb_diag,
                                                                   bb_diag)))
        ]
        ambient_light = AmbientLight(intensity=1.0)

        # Set up Helpers
        self.axes = Axes(bb_center=self.bb.center, length=bb_max * 1.1)
        self.grid = Grid(bb_center=self.bb.center,
                         maximum=bb_max,
                         colorCenterLine='#aaa',
                         colorGrid='#ddd')

        # Set up scene
        environment = self.axes.axes + key_lights + [
            ambient_light, self.grid.grid, self.camera
        ]
        self.scene = Scene(children=environment + [self.pickable_objects])

        # Set up Controllers
        self.controller = OrbitControls(controlling=self.camera,
                                        target=camera_target)

        self.picker = Picker(controlling=self.pickable_objects,
                             event='dblclick')
        self.picker.observe(self.pick)

        # Create Renderer instance
        self.renderer = Renderer(scene=self.scene,
                                 camera=self.camera,
                                 controls=[self.controller, self.picker],
                                 antialias=True,
                                 width=self.width,
                                 height=self.height)

        self.renderer.localClippingEnabled = True
        self.renderer.clippingPlanes = [
            Plane((1, 0, 0), self.grid.size / 2),
            Plane((0, 1, 0), self.grid.size / 2),
            Plane((0, 0, 1), self.grid.size / 2)
        ]

        # needs to be done after setup of camera
        self.grid.set_rotation((math.pi / 2.0, 0, 0, "XYZ"))
        self.grid.set_position((0, 0, 0))

        self.savestate = (self.camera.rotation, self.controller.target)

        # Workaround: Zoom forth and back to update frame. Sometimes necessary :(
        self.camera.zoom = camera_zoom + 0.01
        self._update()
        self.camera.zoom = camera_zoom
        self._update()

        return self.renderer
Пример #14
0
    def render(self, position=None, rotation=None, zoom=2.5):
        self.camera_initial_zoom = zoom

        start_render_time = self._start_timer()
        # Render all shapes
        for i, shape in enumerate(self.shapes):
            s = shape["shape"]
            # Assume that all are edges when first element is an edge
            if is_edge(s[0]):
                self._render_shape(i,
                                   edges=s,
                                   render_edges=True,
                                   edge_color=shape["color"],
                                   edge_width=3)
            elif is_vertex(s[0]):
                self._render_shape(i,
                                   vertices=s,
                                   render_edges=False,
                                   vertex_color=shape["color"],
                                   vertex_width=6)
            else:
                # shape has only 1 object, hence first=True
                self._render_shape(i,
                                   shape=s[0],
                                   render_edges=True,
                                   mesh_color=shape["color"])

        # Get the overall bounding box
        self.bb = BoundingBox([shape["shape"] for shape in self.shapes])

        bb_max = self.bb.max
        orbit_radius = 2 * self.bb.max_dist_from_center()

        # Set up camera
        camera_target = self.bb.center
        camera_up = (0.0, 0.0, 1.0)

        if rotation != (0, 0, 0):
            position = rotate(position, *rotation)

        camera_position = self._add(
            self.bb.center,
            self._scale(
                [1, 1, 1] if position is None else self._scale(position)))

        self.camera = CombinedCamera(
            position=camera_position,
            width=self.width,
            height=self.height
            #            far=10 * orbit_radius,
            #            orthoFar=10 * orbit_radius
        )
        self.camera.up = camera_up

        self.camera.mode = 'orthographic'
        self.camera.position = camera_position

        # Set up lights in every of the 8 corners of the global bounding box
        positions = list(
            itertools.product(*[(-orbit_radius, orbit_radius)] * 3))
        key_lights = [
            DirectionalLight(color='white', position=position, intensity=0.12)
            for position in positions
        ]
        ambient_light = AmbientLight(intensity=1.0)

        # Set up Helpers
        self.axes = Axes(bb_center=self.bb.center, length=bb_max * 1.1)
        self.grid = Grid(bb_center=self.bb.center,
                         maximum=bb_max,
                         colorCenterLine='#aaa',
                         colorGrid='#ddd')

        # Set up scene
        environment = self.axes.axes + key_lights + [
            ambient_light, self.grid.grid, self.camera
        ]
        self.scene = Scene(children=environment + [self.pickable_objects])

        # Set up Controllers
        self.controller = OrbitControls(controlling=self.camera,
                                        target=camera_target,
                                        target0=camera_target)

        # Update controller to instantiate camera position
        self.camera.zoom = zoom
        self._update()

        self.picker = Picker(controlling=self.pickable_objects,
                             event='dblclick')
        self.picker.observe(self.pick)

        # Create Renderer instance
        self.renderer = Renderer(scene=self.scene,
                                 camera=self.camera,
                                 controls=[self.controller, self.picker],
                                 antialias=True,
                                 width=self.width,
                                 height=self.height)

        self.renderer.localClippingEnabled = True
        self.renderer.clippingPlanes = [
            Plane((1, 0, 0), self.grid.size / 2),
            Plane((0, 1, 0), self.grid.size / 2),
            Plane((0, 0, 1), self.grid.size / 2)
        ]

        # needs to be done after setup of camera
        self.grid.set_rotation((math.pi / 2.0, 0, 0, "XYZ"))
        self.grid.set_position((0, 0, 0))

        self.savestate = (self.camera.rotation, self.controller.target)

        self._stop_timer("overall render time", start_render_time)

        return self.renderer
Пример #15
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
Пример #16
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
 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
Пример #18
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