Example #1
0
 def test_list_of_shapes_to_compound(self):
     box_shp = get_test_box_shape()
     sph_shp = get_test_sphere_shape(20.0)
     result, all_shape_converted = list_of_shapes_to_compound(
         [box_shp, sph_shp])
     self.assertTrue(all_shape_converted)
     self.assertEqual(get_type_as_string(result), "Compound")
Example #2
0
def GetOrientedBoundingBox(ifc_elements):
    shapes = CreateShape(ifc_elements)
    shapes_compound, if_all_compound = list_of_shapes_to_compound(shapes)
    aBaryCenter, [aHalfX, aHalfY, aHalfZ], aBox = get_oriented_boundingbox(shapes_compound, 1)
    print("box center point, X Y Z size",aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z(), aHalfX, aHalfY, aHalfZ)
    corner_bot,corner_top = get_oriented_boundingbox_coor(shapes_compound,1)

    center_pt = OCC.Core.gp.gp_Pnt(aBaryCenter)
    return aBox, center_pt, [aHalfX, aHalfY, aHalfZ], corner_bot, corner_top #type(aBox)) #class 'OCC.Core.TopoDS.TopoDS_Solid', gp_Pnt, [float,float,float]
Example #3
0
def GetShapesOBB(shapes):

    ''' Get the oriented bounding box of the input shape'''

    shapes_compound, if_all_compound = list_of_shapes_to_compound(shapes)
    aBaryCenter, [aHalfX, aHalfY, aHalfZ], aBox = get_oriented_boundingbox(shapes_compound, 1)
    print("box center point, X Y Z size", aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z(), aHalfX, aHalfY, aHalfZ)
    corner_bot, corner_top = get_oriented_boundingbox_coor(shapes_compound, 1)
    center_pt = OCC.Core.gp.gp_Pnt(aBaryCenter)
    return aBox, center_pt, [aHalfX, aHalfY,aHalfZ], corner_bot, corner_top  # type(aBox)) #class 'OCC.Core.TopoDS.TopoDS_Solid', gp_Pnt, [float,float,float]
Example #4
0
def read_step_file(filename, as_compound=True, verbosity=True):
    """ read the STEP file and returns a compound
    filename: the file path
    verbosity: optional, False by default.
    as_compound: True by default. If there are more than one shape at root,
    gather all shapes into one compound. Otherwise returns a list of shapes.
    """
    if not os.path.isfile(filename):
        raise FileNotFoundError("%s not found." % filename)

    step_reader = STEPControl_Reader()
    status = step_reader.ReadFile(filename)

    if status == IFSelect_RetDone:  # check status
        if verbosity:
            failsonly = False
            step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
            step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)
        transfer_result = step_reader.TransferRoots()
        if not transfer_result:
            raise AssertionError("Transfer failed.")
        _nbs = step_reader.NbShapes()
        if _nbs == 0:
            raise AssertionError("No shape to transfer.")
        elif _nbs == 1:  # most cases
            return step_reader.Shape(1)
        elif _nbs > 1:
            print("Number of shapes:", _nbs)
            shps = []
            # loop over root shapes
            for k in range(1, _nbs + 1):
                new_shp = step_reader.Shape(k)
                if not new_shp.IsNull():
                    shps.append(new_shp)
            if as_compound:
                compound, result = list_of_shapes_to_compound(shps)
                if not result:
                    print("Warning: all shapes were not added to the compound")
                return compound
            else:
                print("Warning, returns a list of shapes.")
                return shps
    else:
        raise AssertionError("Error: can't read file.")
    return None
Example #5
0
def GetOrientedBoundingBoxShapes(shapes, optimal_OBB=False):
    shapes_compound, if_all_compound = list_of_shapes_to_compound(shapes)

    obb = Bnd_OBB()
    if optimal_OBB:
        is_triangulationUsed = True
        is_optimal = True
        is_shapeToleranceUsed = False
        brepbndlib_AddOBB(shapes_compound, obb, is_triangulationUsed,
                          is_optimal, is_shapeToleranceUsed)
    else:
        brepbndlib_AddOBB(shapes_compound, obb)
    aBaryCenter = obb.Center()

    aBaryCenter = obb.Center()
    aXDir = obb.XDirection()
    aYDir = obb.YDirection()
    aZDir = obb.ZDirection()
    aHalfX = obb.XHSize()
    aHalfY = obb.YHSize()
    aHalfZ = obb.ZHSize()

    ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z())
    ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z())
    az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z())
    p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z())

    pt = []
    pt.append(gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() + ax * aHalfX - ay * aHalfY - az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() + ax * aHalfX + ay * aHalfY - az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() - ax * aHalfX + ay * aHalfY - az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() + ax * aHalfX - ay * aHalfY - az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() + ax * aHalfX + ay * aHalfY - az * aHalfZ))
    pt.append(gp_Pnt(p.XYZ() - ax * aHalfX + ay * aHalfY - az * aHalfZ))

    return pt
Example #6
0
def GetSectionShape(z, shapes): # the mid z value of the storey, list of the storey shapes
    if isinstance(shapes, list):
        shapes_compound, if_all_compound = list_of_shapes_to_compound(shapes)

        plane = gp_Pln(gp_Pnt(0., 0., z), gp_Dir(0., 0., 1.))
        face = BRepBuilderAPI_MakeFace(plane).Shape()
        # Computes Shape/Plane intersection
        section = BRepAlgoAPI_Section(shapes_compound, face)
        section.Build()
        if section.IsDone():
            print("Successfully get the section shape")
            return section.Shape()
        else:
            print("ERROR, the section shape cannot be built")
    else:
        plane = gp_Pln(gp_Pnt(0., 0., z), gp_Dir(0., 0., 1.))
        face = BRepBuilderAPI_MakeFace(plane).Shape()
        # Computes Shape/Plane intersection
        section = BRepAlgoAPI_Section(shapes, face)
        section.Build()
        if section.IsDone():
            print("Successfully get the section shape")
            return section.Shape()