Пример #1
0
def simple_mesh():
    #
    # Create the shape
    #
    shape = BRepPrimAPI_MakeBox(200, 200, 200).Shape()
    theBox = BRepPrimAPI_MakeBox(200, 60, 60).Shape()
    theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100, 20, 20), 80).Shape()
    shape = BRepAlgoAPI_Fuse(theSphere, theBox).Shape()
    #
    # Mesh the shape
    #
    BRepMesh_IncrementalMesh(shape, 0.8)
    builder = BRep_Builder()
    comp = TopoDS_Compound()
    builder.MakeCompound(comp)

    bt = BRep_Tool()
    ex = TopExp_Explorer(shape, TopAbs_FACE)
    while ex.More():
        face = topods_Face(ex.Current())
        location = TopLoc_Location()
        facing = (bt.Triangulation(face, location)).GetObject()
        tab = facing.Nodes()
        tri = facing.Triangles()
        for i in range(1, facing.NbTriangles()+1):
            trian = tri.Value(i)
            index1, index2, index3 = trian.Get()
            for j in range(1, 4):
                if j == 1:
                    m = index1
                    n = index2
                elif j == 2:
                    n = index3
                elif j == 3:
                    m = index2
                me = BRepBuilderAPI_MakeEdge(tab.Value(m), tab.Value(n))
                if me.IsDone():
                    builder.Add(comp, me.Edge())
        ex.Next()
    display.EraseAll()
    display.DisplayShape(shape)
    display.DisplayShape(comp, update=True)
Пример #2
0
def occ_load_file(filename):
    """
    load in pythonocc a igs or step file

    :param filename: a filename with extension
    :return: a topods_shape
    """

    from OCC.STEPControl import STEPControl_Reader
    from OCC.IGESControl import IGESControl_Reader
    from OCC.BRep import BRep_Builder
    from OCC.TopoDS import TopoDS_Compound
    from OCC.IFSelect import IFSelect_RetDone, IFSelect_ItemsByEntity

    reader_switch = {
        'stp': STEPControl_Reader,
        'step': STEPControl_Reader,
        'igs': IGESControl_Reader,
        'iges': IGESControl_Reader
    }

    builder = BRep_Builder()
    comp = TopoDS_Compound()
    builder.MakeCompound(comp)

    reader = reader_switch[os.path.splitext(filename)[1][1:].lower()]()

    status = reader.ReadFile(filename)

    if status == IFSelect_RetDone:  # check status
        failsonly = False
        reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
        reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)

        reader.TransferRoots()
        nbs = reader.NbShapes()

        for i in range(1, nbs + 1):
            shape = reader.Shape(i)
            builder.Add(comp, shape)

    return comp
Пример #3
0
    def vstep(step_str):

        step = int(step_str)

        positions = dpos_data[nbobjs * step:nbobjs * step + nbobjs, 2:]

        builder = BRep_Builder()
        comp = TopoDS_Compound()
        builder.MakeCompound(comp)

        for _id in range(positions.shape[0]):

            q0, q1, q2, q3, q4, q5, q6 = [float(x) for x in positions[_id, :]]

            obj = obj_by_id[_id + 1]

            q = Quaternion((q3, q4, q5, q6))

            for shape_name, avatar in zip(io.instances()[obj], avatars(obj)):
                offset = get_offset(obj, shape_name)
                p = q.rotate(offset[0])
                r = q * Quaternion(offset[1])

                tr = gp_Trsf()
                qocc = gp_Quaternion(r[1], r[2], r[3], r[0])
                tr.SetRotation(qocc)
                xyz = gp_XYZ(q0 + p[0], q1 + p[1], q2 + p[2])
                vec = gp_Vec(xyz)
                tr.SetTranslationPart(vec)
                loc = TopLoc_Location(tr)

                display.Context.SetLocation(avatar, loc)

                moved_shape = BRepBuilderAPI_Transform(
                    avatar.GetObject().Shape(), tr, True).Shape()

                builder.Add(comp, moved_shape)

            display.Context.UpdateCurrentViewer()

        write_step((step_str, comp))
Пример #4
0
def discretize(shape, tol):
    """This method discretizes the OpenCascade shape.

    :param shape: Shape to discretize
    :type shape:
    :return: discretized face; profile coordinates; id of the surface the\
    coordinates belong to
    :rtype: OCC.TopoDS.TopoDS_Compound; numpy.ndarray; numpy.ndarray
    """
    BRepMesh_IncrementalMesh(shape, tol, False, 5)
    builder = BRep_Builder()
    comp = TopoDS_Compound()
    builder.MakeCompound(comp)

    bt = BRep_Tool()
    ex = TopExp_Explorer(shape, TopAbs_EDGE)
    edge_coords = np.zeros([0, 3])
    edge_ids = np.zeros([0], dtype=int)
    edge_id = 0
    while ex.More():
        edge = topods_Edge(ex.Current())
        location = TopLoc_Location()
        edging = (bt.Polygon3D(edge, location)).GetObject()
        tab = edging.Nodes()
        for i in range(1, edging.NbNodes() + 1):
            p = tab.Value(i)
            edge_coords = np.append(edge_coords,
                                    [[p.X(), p.Y(), p.Z()]],
                                    axis=0)
            edge_ids = np.append(edge_ids, edge_id)
            mv = BRepBuilderAPI_MakeVertex(p)
            if mv.IsDone():
                builder.Add(comp, mv.Vertex())
        edge_id += 1
        ex.Next()

    edge_coords = np.round(edge_coords, 8)
    return edge_coords, edge_ids
Пример #5
0
radius = 0.001
sphere_r001 = BRepPrimAPI_MakeSphere(point, radius)
sphere_r001_shape = sphere_r001.Shape()

from OCC.BRep import BRep_Builder
from OCC.TopoDS import TopoDS_Compound

builder = BRep_Builder()
comp = TopoDS_Compound()
builder.MakeCompound(comp)

from OCC.BRep import BRep_Builder
from OCC.TopoDS import TopoDS_Compound

builder.Add(comp, sphere_r1_shape)

radius = 1.0
point = gp_Pnt(4., 0., 0.)
sphere_r1_t = BRepPrimAPI_MakeSphere(point, radius)
sphere_r1_t_shape = sphere_r1_t.Shape()

builder.Add(comp, sphere_r1_t_shape)

# this cylinder is defined for the visualisation of the axis edge
# it may be used for contact also.
axis = BRepPrimAPI_MakeCylinder(gp_Ax2(gp_Pnt(0, -.05, 0), gp_Dir(0, 1, 0)),
                                .001, .1).Shape()

density = 7750.0
steel = Material(density=density)
Пример #6
0
    def write(self, mesh_points, filename, tolerance=None):
        """
		Writes a output file, called filename, copying all the structures from self.filename but
		the coordinates. mesh_points is a matrix that contains the new coordinates to
		write in the output file.

		:param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing
			the coordinates of the points of the mesh
		:param string filename: name of the output file.
		:param float tolerance: tolerance for the construction of the faces and wires
			in the write function. If not given it uses `self.tolerance`.
		"""
        self._check_filename_type(filename)
        self._check_extension(filename)
        self._check_infile_instantiation()

        self.outfile = filename

        if tolerance is not None:
            self.tolerance = tolerance

        # cycle on the faces to update the control points position
        # init some quantities
        faces_explorer = TopExp_Explorer(self.shape, TopAbs_FACE)
        n_faces = 0
        control_point_position = self._control_point_position

        compound_builder = BRep_Builder()
        compound = OCC.TopoDS.TopoDS_Compound()
        compound_builder.MakeCompound(compound)

        while faces_explorer.More():
            # similar to the parser method
            face = OCC.TopoDS.topods_Face(faces_explorer.Current())
            nurbs_converter = BRepBuilderAPI_NurbsConvert(face)
            nurbs_converter.Perform(face)
            nurbs_face = nurbs_converter.Shape()
            face_aux = OCC.TopoDS.topods_Face(nurbs_face)
            brep_face = BRep_Tool.Surface(OCC.TopoDS.topods_Face(nurbs_face))
            bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face)
            occ_face = bspline_face.GetObject()

            n_poles_u = occ_face.NbUPoles()
            n_poles_v = occ_face.NbVPoles()

            i = 0
            for pole_u_direction in range(n_poles_u):
                for pole_v_direction in range(n_poles_v):
                    control_point_coordinates = mesh_points[
                        i + control_point_position[n_faces], :]
                    point_xyz = gp_XYZ(*control_point_coordinates)

                    gp_point = gp_Pnt(point_xyz)
                    occ_face.SetPole(pole_u_direction + 1,
                                     pole_v_direction + 1, gp_point)
                    i += 1

            # construct the deformed wire for the trimmed surfaces
            wire_maker = BRepBuilderAPI_MakeWire()
            tol = ShapeFix_ShapeTolerance()
            brep = BRepBuilderAPI_MakeFace(occ_face.GetHandle(),
                                           self.tolerance).Face()
            brep_face = BRep_Tool.Surface(brep)

            # cycle on the edges
            edge_explorer = TopExp_Explorer(nurbs_face, TopAbs_EDGE)
            while edge_explorer.More():
                edge = OCC.TopoDS.topods_Edge(edge_explorer.Current())
                # edge in the (u,v) coordinates
                edge_uv_coordinates = BRep_Tool.CurveOnSurface(edge, face_aux)
                # evaluating the new edge: same (u,v) coordinates, but different (x,y,x) ones
                edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge(\
                 edge_uv_coordinates[0], brep_face)
                edge_phis_coordinates = edge_phis_coordinates_aux.Edge()
                tol.SetTolerance(edge_phis_coordinates, self.tolerance)
                wire_maker.Add(edge_phis_coordinates)
                edge_explorer.Next()

            # grouping the edges in a wire
            wire = wire_maker.Wire()

            # trimming the surfaces
            brep_surf = BRepBuilderAPI_MakeFace(occ_face.GetHandle(),
                                                wire).Shape()
            compound_builder.Add(compound, brep_surf)
            n_faces += 1
            faces_explorer.Next()
        self.write_shape_to_file(compound, self.outfile)
Пример #7
0
ifc_file = ifcopenshell.open("castest_01.ifc")

# Making compound
compound = TopoDS_Compound()
builder = BRep_Builder()
builder.MakeCompound(compound)

# Display the geometrical contents of the file using Python OpenCascade
products = ifc_file.by_type("IfcProduct")
for product in products:
    # Adding a element to compund maked
    if product.is_a("IfcOpeningElement"): continue
    if product.Representation:
        shape = ifcopenshell.geom.create_shape(settings, product).geometry
        display_shape = ifcopenshell.geom.utils.display_shape(shape)
        builder.Add(compound,shape)
        if product.is_a("IfcPlate"):
            # Plates are the transparent parts of the window assembly
            # in the IfcOpenHouse model
            ifcopenshell.geom.utils.set_shape_transparency(display_shape, 0.8)

# Saving compound
breptools_Write(compound,"castest_01.brep")

# Wait for user input and erase the display
raw_input()
occ_display.EraseAll()
'''
# Get a list of all walls in the file
walls = ifc_file.by_type("IfcWall")
anEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment.Value(),
                                         Handle_Geom_Surface(aCyl2))

threadingWire1 = BRepBuilderAPI_MakeWire(anEdge1OnSurf1.Edge(),
                                         anEdge2OnSurf1.Edge())
threadingWire2 = BRepBuilderAPI_MakeWire(anEdge1OnSurf2.Edge(),
                                         anEdge2OnSurf2.Edge())

# Compute the 3D representations of the edges/wires
breplib.BuildCurves3d(threadingWire1.Shape())
breplib.BuildCurves3d(threadingWire2.Shape())

# Create the surfaces of the threading
aTool = BRepOffsetAPI_ThruSections(True)
aTool.AddWire(threadingWire1.Wire())
aTool.AddWire(threadingWire2.Wire())
aTool.CheckCompatibility(False)
myThreading = aTool.Shape()

# Build the resulting compound
aRes = TopoDS_Compound()
aBuilder = BRep_Builder()
aBuilder.MakeCompound(aRes)
aBuilder.Add(aRes, myBody.Shape())
aBuilder.Add(aRes, myThreading)

display, start_display, add_menu, add_function_to_menu = init_display('wx')
display.DisplayColoredShape(aRes)

start_display()
Пример #9
0
    def write_shape(self, l_shells, filename, tol):
        """
        Method to recreate a TopoDS_Shape associated to a geometric shape
        after the modification of points of each Face. It
        returns a TopoDS_Shape (Shape).

        :param l_shells: the list of shells after initial parsing
        :param filename: the output filename
        :param tol: tolerance on the surface creation after modification
        :return: None

        """
        self.outfile = filename
        # global compound containing multiple shells
        global_compound_builder = BRep_Builder()
        global_comp = TopoDS_Compound()
        global_compound_builder.MakeCompound(global_comp)

        if self.check_topo == 0:
            # cycle on shells (multiple objects)
            shape_shells_explorer = TopExp_Explorer(
                self.shape.Oriented(TopAbs_FORWARD), TopAbs_SHELL)
            ishell = 0

            while shape_shells_explorer.More():
                per_shell = topods_Shell(shape_shells_explorer.Current())
                # a local compound containing a shell
                compound_builder = BRep_Builder()
                comp = TopoDS_Compound()
                compound_builder.MakeCompound(comp)

                # cycle on faces
                faces_explorer = TopExp_Explorer(
                    per_shell.Oriented(TopAbs_FORWARD), TopAbs_FACE)
                iface = 0
                while faces_explorer.More():
                    topoface = topods.Face(faces_explorer.Current())
                    newface = self.write_face(l_shells[ishell][iface][0],
                                              l_shells[ishell][iface][1],
                                              topoface, tol)

                    # add face to compound
                    compound_builder.Add(comp, newface)
                    iface += 1
                    faces_explorer.Next()

                new_shell = self.combine_faces(comp, 0.01)
                itype = TopoDS_Shape.ShapeType(new_shell)
                # add the new shell to the global compound
                global_compound_builder.Add(global_comp, new_shell)

                print("Shell {0} of type {1} Processed ".format(ishell, itype))
                print "=============================================="

                ishell += 1
                shape_shells_explorer.Next()

        else:
            # cycle on faces
            # a local compound containing a shell
            compound_builder = BRep_Builder()
            comp = TopoDS_Compound()
            compound_builder.MakeCompound(comp)

            # cycle on faces
            faces_explorer = TopExp_Explorer(
                self.shape.Oriented(TopAbs_FORWARD), TopAbs_FACE)
            iface = 0
            while faces_explorer.More():
                topoface = topods.Face(faces_explorer.Current())
                newface = self.write_face(l_shells[0][iface][0],
                                          l_shells[0][iface][1], topoface, tol)

                # add face to compound
                compound_builder.Add(comp, newface)
                iface += 1
                faces_explorer.Next()

            new_shell = self.combine_faces(comp, 0.01)
            itype = TopoDS_Shape.ShapeType(new_shell)
            # add the new shell to the global compound
            global_compound_builder.Add(global_comp, new_shell)

            print("Shell {0} of type {1} Processed ".format(0, itype))
            print "=============================================="

        self.write_shape_to_file(global_comp, self.outfile)
    def get_current_loft(self):
        wires = []
        curves = []
        propeller_number_, xz_mirror_, xy_mirror_, yz_mirror_ \
            , rot_x_, hub_length, pitch_angle, root_le_pos_x_, \
        root_le_pos_y_, root_le_pos_z_, section_1_length_, section_2_length_, \
        section_3_length_, section_4_length_, section_5_length_, section_1_profile_, \
        section_2_profile_, section_3_profile_, section_4_profile_, section_5_profile_, \
        section_1_z_, section_2_z_, section_3_z_, section_4_z_, section_5_z_ \
            , section_1_chord_, section_2_chord_, section_3_chord_, section_4_chord_, \
        section_5_chord_, section_1_pitch_angle_, section_2_pitch_angle_, \
        section_3_pitch_angle_, section_4_pitch_angle_, section_5_pitch_angle_ = read_propeller_parameters(
            name=self.name)
        interval = 5

        chords = [
            section_1_chord_, section_1_chord_, section_2_chord_,
            section_3_chord_, section_4_chord_, section_5_chord_, 0.001
        ]
        profile = [
            section_1_profile_, section_1_profile_, section_2_profile_,
            section_3_profile_, section_4_profile_, section_5_profile_,
            section_5_profile_
        ]
        length = [
            0, section_1_length_, section_2_length_, section_3_length_,
            section_4_length_, section_5_length_ / 2, section_5_length_ / 2
        ]
        z = [
            section_1_z_, section_1_z_, section_2_z_, section_3_z_,
            section_4_z_, section_5_z_, section_5_z_
        ]
        pitch = [
            0, section_1_pitch_angle_, section_2_pitch_angle_,
            section_3_pitch_angle_, section_4_pitch_angle_,
            section_5_pitch_angle_, section_5_pitch_angle_
        ]

        n = random.random()
        lifting_surface = self.config.get_wings().create_wing(
            f"{n}", len(chords), f"naca4412")

        sections = [-hub_length / 2, 0.1 * hub_length, hub_length / 2]

        radius = [
            0.00,
            rot_x_ / 2,
            rot_x_ / 2,
        ]

        x_ = []
        x_.extend(np.linspace(sections[0], sections[1], num=interval))
        x_.append(0.9 * hub_length)

        radii = []
        radii.extend(np.linspace(radius[0], radius[1], num=interval))
        radii.append((rot_x_ / 2))

        print(x_, radii)

        n = random.random()
        hub = self.config.get_fuselages().create_fuselage(
            f"hub{n}", len(x_), "circularProfile")
        for (x, rad, index) in zip(x_, radii, range(1, len(x_) + 1)):
            section = hub.get_section(index)
            sectionElement = section.get_section_element(1)
            sectionElementCenter = sectionElement.get_ctigl_section_element()
            sectionElementCenter.set_center(tigl3.geometry.CTiglPoint(x, 0, 0))
            sectionElementCenter.set_area(getArea(rad))

        n_sections = lifting_surface.get_section_count()
        lifting_surface.set_root_leposition(
            tigl3.geometry.CTiglPoint(-chords[0] / 2, 0, 0))
        print(n_sections)
        y = 0.0
        x = hub_length / 2
        for (z_, chord, length_, pitch_, profile_,
             idx) in zip(z, chords, length, pitch, profile,
                         range(1, n_sections + 1)):
            profile__ = "naca" + profile_
            constant = 0.0
            nacanumber = profile__.split("naca")[1]
            if nacanumber.isdigit():
                if len(nacanumber) == 4:
                    constant = int(nacanumber[2:]) * 0.01
            s = lifting_surface.get_section(idx)
            e = s.get_section_element(1)
            ce = e.get_ctigl_section_element()
            ce.set_width(chord)
            ce.set_height(chord * constant)
            center = ce.get_center()
            y += length_
            center.x = x
            center.y = y
            center.z = 0.0
            ce.set_center(center)
            ce.set_profile_uid(f"{profile_}")
            e.set_rotation(tigl3.geometry.CTiglPoint(0, pitch_, 0))
        lifting_surface.set_rotation(
            tigl3.geometry.CTiglPoint(0, pitch_angle, 0))
        for idx in range(1, n_sections + 1):
            s = lifting_surface.get_section(idx)
            e = s.get_section_element(1)
            ce = e.get_ctigl_section_element()
            wires.append(ce.get_wire())
        for l in wires:
            adapt = BRepAdaptor_CompCurve(l)
            curve = Handle_BRepAdaptor_HCompCurve(
                BRepAdaptor_HCompCurve(adapt))
            approx = Approx_Curve3d(curve, 0.001, GeomAbs_C2, 200, 12)
            if (approx.IsDone() and approx.HasResult()):
                curves.append(approx.Curve())

        surface1 = tigl3.surface_factories.interpolate_curves(curves)
        face1 = BRepBuilderAPI_MakeFace(surface1, 1e-6).Face()
        face2 = BRepBuilderAPI_MakeFace(surface1, 1e-6).Face()

        sew = BRepBuilderAPI_Sewing()
        sew.Add(face1)
        sew.Add(face2)
        sew.Perform()
        shape = sew.SewedShape()
        print(shape)

        tds = topods()
        model = BRepBuilderAPI_MakeSolid()
        model.Add(tds.Shell(shape))
        solid = model.Solid()
        print(solid)

        rot_trafo = tigl3.geometry.CTiglTransformation()
        rot_trafo.add_rotation_x(90)

        loft = []
        loft.append(hub.get_loft().shape())
        loft.append(
            tigl3.geometry.CNamedShape(rot_trafo.transform(solid),
                                       "cut").shape())

        if propeller_number_ == 2:
            trafo = tigl3.geometry.CTiglTransformation()
            trafo.add_mirroring_at_xzplane()
            loft.append(
                tigl3.geometry.CNamedShape(trafo.transform(loft[1]),
                                           "cut").shape())
        elif propeller_number_ >= 3:
            delta = 360 / propeller_number_
            for i in range(1, propeller_number_ + 1):
                loft_copy = deepcopy(loft[1])
                trafo = tigl3.geometry.CTiglTransformation()
                trafo.add_rotation_x(delta * i)
                print(delta * i)
                loft.append(
                    tigl3.geometry.CNamedShape(trafo.transform(loft_copy),
                                               "cut").shape())
        builder = BRep_Builder()
        assembly = TopoDS_Compound()
        builder.MakeCompound(assembly)
        for l in loft:
            builder.Add(assembly, l)
        trafo = tigl3.geometry.CTiglTransformation()
        trafo.add_translation(root_le_pos_x_, root_le_pos_y_, root_le_pos_z_)
        assembly = trafo.transform(assembly)

        return [assembly]
Пример #11
0
    def generate_stl(self, min_length=None, max_length=None, outfile_stl=None):
        """
        Generate and export the .STL surface mesh for the blade as a whole,
        including the upper face, lower face and tip. The method utilizes
        modules from OCC SMESH which is standalone mesh framework based on
        SALOME mesher project. Please refer to https://github.com/tpaviot
        and http://docs.salome-platform.org/7/gui/SMESH/index.html for
        further details.

        This method requires PythonOCC and SMESH to be installed.

        :param double min_length: smallest distance between two nodes. Default
            value is None
        :param double max_length: largest distance between two nodes. Default
            value is None
        :param string outfile_stl: if string is passed then the method exports
            the generated 2D surface mesh into .stl file holding the name
            <outfile_stl>.stl. Default value is None

        We note that since the current implementation performs triangulation
        based on a topological compound that combines the blade 3 generated
        shapes without "fusion", it may happen that the generated triangulation
        of the upper and lower blade faces do not share the same exact nodes
        on the joint edge/wire resulting from the faces intersection. The
        current implementation can be enough for visualization purpose. However
        if the generated mesh is intended for computational analysis then a
        manual mesh healing is recommended by the user (e.g. see
        "Repair > Sewing" in SALOME GUI) for a proper mesh closure.
        """
        from OCC.SMESH import SMESH_Gen
        from OCC.StdMeshers import (StdMeshers_Arithmetic1D,
                                    StdMeshers_TrianglePreference,
                                    StdMeshers_Regular_1D,
                                    StdMeshers_MEFISTO_2D)
        from OCC.BRep import BRep_Builder
        from OCC.TopoDS import TopoDS_Shape, TopoDS_Compound

        if min_length <= 0 or max_length <= 0:
            raise ValueError('min_length and max_length must be positive.')
        if min_length >= max_length:
            raise ValueError('min_length can not be greater than max_length')

        # First we check that blade shapes are generated, otherwise we generate
        # them. After that we combine the generated_upper_face,
        # generated_lower_face, and generated_tip into a topological compound
        # that we use to compute the surface mesh
        if (self.generated_upper_face is None) or not isinstance(
                self.generated_upper_face, TopoDS_Shape):
            # Upper face is generated with a maximal U degree = 1
            self._generate_upper_face(maxDeg=1)
        if (self.generated_lower_face is None) or not isinstance(
                self.generated_lower_face, TopoDS_Shape):
            # Upper face is generated with a maximal U degree = 1
            self._generate_lower_face(maxDeg=1)
        if (self.generated_tip is None) or not isinstance(
                self.generated_tip, TopoDS_Shape):
            # Upper face is generated with a maximal U degree = 1
            self._generate_tip(maxDeg=1)

        # Now we regroup all the shapes into a TopoDS_Compound
        aCompound = TopoDS_Compound()
        aBuilder = BRep_Builder()
        aBuilder.MakeCompound(aCompound)
        # Add shapes
        aBuilder.Add(aCompound, self.generated_upper_face)
        aBuilder.Add(aCompound, self.generated_lower_face)
        aBuilder.Add(aCompound, self.generated_tip)

        # In the following we build the surface mesh according to the given
        # hypotheses
        aMeshGen = SMESH_Gen()
        aMesh = aMeshGen.CreateMesh(0, True)
        # Adding 1D hypothesis and algorithms
        # Wire discretization. Nodes are distributed based on Arithmetic1D
        # hypothesis which allows to split edges into segments with a length
        # that changes in arithmetic progression (Lk = Lk-1 + d) beginning
        # from a given min length and up to a given max length. More about
        # 1D hypotheses can be viewed through:
        # http://docs.salome-platform.org/7/gui/SMESH/a1d_meshing_hypo_page.html
        an1DHypothesis = StdMeshers_Arithmetic1D(0, 0, aMeshGen)
        # Smallest distance between 2 points
        an1DHypothesis.SetLength(min_length, False)
        # Longest distance between 2 points
        an1DHypothesis.SetLength(max_length, True)
        # Regular Interpolation
        an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen)
        # Adding 2D hypothesis and algorithms
        # 2D surface mesh -- Triangulations
        a2dHypothseis = StdMeshers_TrianglePreference(2, 0, aMeshGen)
        a2dAlgo = StdMeshers_MEFISTO_2D(3, 0, aMeshGen)

        #Calculate mesh for the topological compound containing the 3 shapes
        aMesh.ShapeToMesh(aCompound)

        #Assign hyptothesis to mesh
        aMesh.AddHypothesis(aCompound, 0)
        aMesh.AddHypothesis(aCompound, 1)
        aMesh.AddHypothesis(aCompound, 2)
        aMesh.AddHypothesis(aCompound, 3)

        if outfile_stl is not None:
            if not isinstance(outfile_stl, str):
                raise ValueError('outfile_stl must be a valid string.')

            #Compute the data
            aMeshGen.Compute(aMesh, aMesh.GetShapeToMesh())
            # Export STL
            aMesh.ExportSTL(outfile_stl + '.stl', False)
Пример #12
0
    def write(cls, filename, data, tolerance=1e-6):

        # cycle on the faces to update the control points position
        # init some quantities
        shape = data.shape
        control_point_position = data.control_point_position
        mesh_points = data.points

        faces_explorer = TopExp_Explorer(shape, TopAbs_FACE)
        n_faces = 0
        compound_builder = BRep_Builder()
        compound = TopoDS_Compound()
        compound_builder.MakeCompound(compound)

        while faces_explorer.More():
            # similar to the parser method
            face = topods_Face(faces_explorer.Current())
            nurbs_converter = BRepBuilderAPI_NurbsConvert(face)
            nurbs_converter.Perform(face)
            nurbs_face = nurbs_converter.Shape()
            face_aux = topods_Face(nurbs_face)
            brep_face = BRep_Tool.Surface(topods_Face(nurbs_face))
            bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face)
            occ_face = bspline_face.GetObject()

            n_poles_u = occ_face.NbUPoles()
            n_poles_v = occ_face.NbVPoles()

            i = 0
            for pole_u_direction in range(n_poles_u):
                for pole_v_direction in range(n_poles_v):
                    control_point_coordinates = mesh_points[
                        +control_point_position[n_faces], :]
                    point_xyz = gp_XYZ(*control_point_coordinates)

                    gp_point = gp_Pnt(point_xyz)
                    occ_face.SetPole(pole_u_direction + 1,
                                     pole_v_direction + 1, gp_point)
                    i += 1

            # construct the deformed wire for the trimmed surfaces
            wire_maker = BRepBuilderAPI_MakeWire()
            tol = ShapeFix_ShapeTolerance()
            brep = BRepBuilderAPI_MakeFace(occ_face.GetHandle(),
                                           tolerance).Face()
            brep_face = BRep_Tool.Surface(brep)

            # cycle on the edges
            edge_explorer = TopExp_Explorer(nurbs_face, TopAbs_EDGE)
            while edge_explorer.More():
                edge = topods_Edge(edge_explorer.Current())
                # edge in the (u,v) coordinates
                edge_uv_coordinates = BRep_Tool.CurveOnSurface(edge, face_aux)
                # evaluating the new edge: same (u,v) coordinates, but
                # different (x,y,x) ones
                edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge(
                    edge_uv_coordinates[0], brep_face)
                edge_phis_coordinates = edge_phis_coordinates_aux.Edge()
                tol.SetTolerance(edge_phis_coordinates, tolerance)
                wire_maker.Add(edge_phis_coordinates)
                edge_explorer.Next()

            # grouping the edges in a wire
            wire = wire_maker.Wire()

            # trimming the surfaces
            brep_surf = BRepBuilderAPI_MakeFace(occ_face.GetHandle(),
                                                wire).Shape()
            compound_builder.Add(compound, brep_surf)
            n_faces += 1
            faces_explorer.Next()

        IGESControl_Controller_Init()
        writer = IGESControl_Writer()
        writer.AddShape(compound)
        writer.Write(filename)
anEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(Handle_Geom2d_Curve(anArc2), Handle_Geom_Surface(aCyl2))
anEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment.Value(), Handle_Geom_Surface(aCyl2))

threadingWire1 = BRepBuilderAPI_MakeWire(anEdge1OnSurf1.Edge(), anEdge2OnSurf1.Edge())
threadingWire2 = BRepBuilderAPI_MakeWire(anEdge1OnSurf2.Edge(), anEdge2OnSurf2.Edge())

# Compute the 3D representations of the edges/wires
breplib.BuildCurves3d(threadingWire1.Shape())
breplib.BuildCurves3d(threadingWire2.Shape())

# Create the surfaces of the threading
aTool = BRepOffsetAPI_ThruSections(True)
aTool.AddWire(threadingWire1.Wire())
aTool.AddWire(threadingWire2.Wire())
aTool.CheckCompatibility(False)
myThreading = aTool.Shape()

# Build the resulting compound
bottle = TopoDS_Compound()
aBuilder = BRep_Builder()
aBuilder.MakeCompound(bottle)
aBuilder.Add(bottle, myBody.Shape())
aBuilder.Add(bottle, myThreading)
print("bottle finished")

if __name__ == "__main__":
    from OCC.Display.SimpleGui import init_display
    display, start_display, add_menu, add_function_to_menu = init_display()
    display.DisplayColoredShape(bottle, update=True)
    start_display()