def generate_planar_slices(nozz_dia, sur_nozz_dia):
     xmin, ymin, zmin, xmax, ymax, zmax = get_boundingbox(part_shape)
     print(xmax - xmin, ",", ymax - ymin, ",", zmax - zmin)
     wires = []
     slices = []
     contours = []
     for z in numpy.arange(zmin+(nozz_dia/2)+(sur_nozz_dia/2), \
      zmax-(nozz_dia/2)-(sur_nozz_dia/2), nozz_dia):
         plane = gp_Pln(gp_Pnt(0., 0., z), gp_Dir(0., 0., 1.))
         slices.append(Slicing.plane_shape_intersection(plane, part_shape))
     for s in range(0, len(slices)):
         wire = []
         wires.append([])
         while len(slices[s]) != 0:
             for i in range(0, len(slices[s])):
                 if len(wire) == 0:
                     wire.append(slices[s][i])
                     slices[s].remove(slices[s][i])
                     break
                 elif Slicing.do_edges_connect(slices[s][i], wire[-1]):
                     wire.append(slices[s][i])
                     slices[s].remove(slices[s][i])
                     break
             if Slicing.do_edges_connect(wire[0], wire[-1]):
                 if len(wire) > 2:
                     wires[s].append(wire)
                     wire = []
                 elif len(wire) == 2 and Slicing.do_edges_loop(
                         wire[0], wire[-1]):
                     wires[s].append(wire)
                     wire = []
     for k in range(0, len(wires)):
         contours.append([])
         for l in range(0, len(wires[k])):
             make_wire = BRepBuilderAPI_MakeWire()
             for edge in wires[k][l]:
                 make_wire.Add(edge)
             try:
                 made_wire = make_wire.Wire()
                 contours[k].append(made_wire)
             except:
                 print("Skipped a contour!")
                 continue
     contour_faces = []
     for contour in contours:
         if len(contour) == 1:
             contour_faces.append(
                 BRepBuilderAPI_MakeFace(contour[0]).Face())
         else:
             contour_faces.extend(Slicing.get_layer_faces(contour))
     # for l in range(0,len(contour_faces)):
     #   display.DisplayShape(contour_faces[l], color=colour[l%5],\
     #   	transparency=0.95, update=True)
     return contour_faces
 def get_surfaces_boundingbox(faces):
     minx = []
     miny = []
     minz = []
     maxx = []
     maxy = []
     maxz = []
     for face in faces:
         xmin, ymin, zmin, xmax, ymax, zmax = get_boundingbox(face)
         minx.append(xmin)
         miny.append(ymin)
         minz.append(zmin)
         maxx.append(xmax)
         maxy.append(ymax)
         maxz.append(zmax)
     return min(minx), min(miny), min(minz), max(maxx), max(maxy), max(maxz)
    def sort_surfaces(faces, along):
        min_along = []

        def get_min(e):
            return e['min']

        for i in range(0, len(faces)):
            xmin, ymin, xxx, xxx, xxx, xxx = get_boundingbox(faces[i])
            if along == 'X':
                min_along.append({'id': i, 'min': ymin})
            elif along == 'Y':
                min_along.append({'id': i, 'min': xmin})
        min_along.sort(key=get_min)
        new_faces = []
        for j in range(0, len(faces)):
            new_faces.append(faces[min_along[j]['id']])
        return new_faces
 def get_layer_faces(wires):
     faces = []
     bounds = []
     area = []
     for wire in wires:
         face = BRepBuilderAPI_MakeFace(wire).Face()
         faces.append(face)
         xmin, ymin, zzz, xmax, ymax, zzz = get_boundingbox(face)
         area.append((xmax - xmin) * (ymax - ymin))
         bounds.append([xmin, ymin, xmax, ymax])
     return faces
     for i in range(0, len(faces)):
         for j in range(0, len(faces)):
             if i == j:
                 continue
             if (bounds[i][0] < bounds[j][0]) and (bounds[i][1] < bounds[j][1]) and\
             (bounds[i][2] > bounds[j][2]) and (bounds[i][3] > bounds[j][3]):
                 faces[i] = Slicing.cut_face_from_face(faces[i], faces[j])
                 faces.remove(faces[j])
     return faces
def run(n_procs, compare_by_number_of_processors=False):
    shape = get_brep()
    x_min, y_min, z_min, x_max, y_max, z_max = get_boundingbox(shape)
    z_delta = abs(z_min - z_max)

    init_time = time.time()  # for total time computation

    def get_z_coords_for_n_procs(n_slices, n_procs):
        z_slices = drange(z_min, z_max, z_delta / n_slices)

        slices = []
        n = len(z_slices) // n_procs
        print('number of slices:', len(z_slices))

        _str_slices = []
        for i in range(1, n_procs + 1):
            if i == 1:
                slices.append(z_slices[:i * n])
                _str_slices.append(':' + str(i * n) + ' ')
            elif i == n_procs:
                # does a little extra work if the number of slices
                # isnt divisible by n_procs
                slices.append(z_slices[(i - 1) * n:])
                _str_slices.append(str((i - 1) * n) + ': ')
                print('last slice', len(z_slices[(i - 1) * n:]))
            else:
                slices.append(z_slices[(i - 1) * n:i * n])
                _str_slices.append(' %s:%s ' % ((i - 1) * n, i * n))
        print(
            'the z-index array is sliced over %s processors like this: \n %s' %
            (n_procs, _str_slices))
        return slices

    def arguments(n_slices, n_procs):
        _tmp = []
        slices = get_z_coords_for_n_procs(n_slices, n_procs)
        for i in slices:
            _tmp.append([i, shape])
        return _tmp

    n_slice = 50

    if not compare_by_number_of_processors:
        _results = []
        P = multiprocessing.Pool(n_procs)
        _results = P.map(vectorized_slicer, arguments(n_slice, n_procs))

    else:
        # run a few tests from 1 to 9 processors
        for i in range(1, 9):
            tA = time.time()
            _results = []
            if i == 1:
                _results = vectorized_slicer(
                    [drange(z_min, z_max, z_delta / n_slice), shape])
            else:
                P = multiprocessing.Pool(n_procs)
                _results = P.map(vectorized_slicer, arguments(n_slice, i))
            print('slicing took %s seconds for %s processors' %
                  (time.time() - tA, i))
        sys.exit()

    print('\n\n\n done slicing on %i cores \n\n\n' % nprocs)

    # Display result
    display, start_display, add_menu, add_function_to_menu = init_display()
    print('displaying original shape')
    display.DisplayShape(shape, update=True)
    for n, result_shp in enumerate(_results):
        print('displaying results from process {0}'.format(n))
        display.DisplayShape(result_shp, update=True)

    # update viewer when all is added:
    display.Repaint()
    total_time = time.time() - init_time
    print("%s necessary to perform slice with %s processor(s)." %
          (total_time, n_procs))
    start_display()
예제 #6
0
        section.Build()
        if section.IsDone():
            _slices.append(section.Shape())
    return _slices


def get_brep():
    cylinder_head = TopoDS_Shape()
    builder = BRep_Builder()
    breptools_Read(cylinder_head, './core_example/cylinder_head.brep', builder)
    return cylinder_head


if __name__ == '__main__':
    shp = get_brep()
    xyz_min_max = get_boundingbox(shp)
    p1 = gp_Pnt(*xyz_min_max[0:3])
    p2 = gp_Pnt(*xyz_min_max[3:])
    box = make_box(p1, p2)

    obj = plotocc()
    obj.display.DisplayShape(box, transparency=0.9)
    obj.display.DisplayShape(shp)

    z_delta = abs(xyz_min_max[2] - xyz_min_max[5])
    for z in np.linspace(xyz_min_max[2], xyz_min_max[5], 5):
        print(z)
        plane = gp_Pln(gp_Pnt(0., 0., z), gp_Dir(0., 0.0, 1.))
        face = BRepBuilderAPI_MakeFace(plane).Shape()
        # Computes Shape/Plane intersection
        section = BRepAlgoAPI_Section(shp, face)
 def get_boundingbox_dimensions(shape):
     xmin, ymin, zmin, xmax, ymax, zmax = get_boundingbox(shape)
     return xmax - xmin, ymax - ymin, zmax - zmin