Exemple #1
0
 def on_compute_change(self, change):
     if change['type'] == 'change' and change['name'] == 'value':
         selection = change['new']
         output = ""
         if 'Inertia' in selection:
             cog, mass, mass_property = measure_shape_mass_center_of_gravity(
                 self._current_shape_selection)
             # display this point (type gp_Pnt)
             self.DisplayShape([cog])
             output += "<u><b>Center of Gravity</b></u>:<br><b>Xcog=</b>%.3f<br><b>Ycog=</b>%.3f<br><b>Zcog=</b>%.3f<br>" % (
                 cog.X(), cog.Y(), cog.Z())
             output += "<u><b>%s=</b></u>:<b>%.3f</b><br>" % (mass_property,
                                                              mass)
         elif 'Oriented' in selection:
             center, dim, oobb_shp = get_oriented_boundingbox(
                 self._current_shape_selection)
             self.DisplayShape(oobb_shp,
                               render_edges=True,
                               transparency=True,
                               opacity=0.2,
                               selectable=False)
             output += "<u><b>OOBB center</b></u>:<br><b>X=</b>%.3f<br><b>Y=</b>%.3f<br><b>Z=</b>%.3f<br>" % (
                 center.X(), center.Y(), center.Z())
             output += "<u><b>OOBB dimensions</b></u>:<br><b>dX=</b>%.3f<br><b>dY=</b>%.3f<br><b>dZ=</b>%.3f<br>" % (
                 dim[0], dim[1], dim[2])
             output += "<u><b>OOBB volume</b></u>:<br><b>V=</b>%.3f<br>" % (
                 dim[0] * dim[1] * dim[2])
         elif 'Aligned' in selection:
             center, dim, albb_shp = get_aligned_boundingbox(
                 self._current_shape_selection)
             self.DisplayShape(albb_shp,
                               render_edges=True,
                               transparency=True,
                               opacity=0.2,
                               selectable=False)
             output += "<u><b>ABB center</b></u>:<br><b>X=</b>%.3f<br><b>Y=</b>%.3f<br><b>Z=</b>%.3f<br>" % (
                 center.X(), center.Y(), center.Z())
             output += "<u><b>ABB dimensions</b></u>:<br><b>dX=</b>%.3f<br><b>dY=</b>%.3f<br><b>dZ=</b>%.3f<br>" % (
                 dim[0], dim[1], dim[2])
             output += "<u><b>ABB volume</b></u>:<br><b>V=</b>%.3f<br>" % (
                 dim[0] * dim[1] * dim[2])
         elif 'Recognize' in selection:
             # try featrue recognition
             kind, pnt, vec = recognize_face(self._current_shape_selection)
             output += "<u><b>Type</b></u>: %s<br>" % kind
             if kind == "Plane":
                 self.DisplayShape([pnt])
                 output += "<u><b>Properties</b></u>:<br>"
                 output += "<u><b>Point</b></u>:<br><b>X=</b>%.3f<br><b>Y=</b>%.3f<br><b>Z=</b>%.3f<br>" % (
                     pnt.X(), pnt.Y(), pnt.Z())
                 output += "<u><b>Normal</b></u>:<br><b>u=</b>%.3f<br><b>v=</b>%.3f<br><b>w=</b>%.3f<br>" % (
                     vec.X(), vec.Y(), vec.Z())
             elif kind == "Cylinder":
                 self.DisplayShape([pnt])
                 output += "<u><b>Properties</b></u>:<br>"
                 output += "<u><b>Axis point</b></u>:<br><b>X=</b>%.3f<br><b>Y=</b>%.3f<br><b>Z=</b>%.3f<br>" % (
                     pnt.X(), pnt.Y(), pnt.Z())
                 output += "<u><b>Axis direction</b></u>:<br><b>u=</b>%.3f<br><b>v=</b>%.3f<br><b>w=</b>%.3f<br>" % (
                     vec.X(), vec.Y(), vec.Z())
         self.html.value = output
def tag_faces(_shape, _color, shape_name):
    """ tag the faces of a shape

    in this example, this easy to see which faces of the 2 shapes we need to glue together
    so by reading the tagged faces, its easy to find the correct index for the
    list `facesA` (5) and `facesB` (4)

    :param _shape: the shape to tag
    :param color: a string, to colors the faces the `_shape` we're exploring
    :param shape_name: name to tag the faces the `_shape`
    """
    for n, f in enumerate(_shape):
        # centroid of the face
        center_pt = get_aligned_boundingbox(f)[0]
        # displays the face in the viewer
        display.DisplayShape(f, color=_color, transparency=0.9)
        # tag the face in the viewer
        display.DisplayMessage(center_pt, "{0}_nr_{1}".format(shape_name, n))
def run(n_procs, compare_by_number_of_processors=False):
    shape = get_brep()
    center, [dx, dy, dz], box_shp = get_aligned_boundingbox(shape)
    z_min = center.Z() - dz / 2
    z_max = center.Z() + dz / 2

    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, dz / 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, dz / 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()
def tag_edge(_edge, msg, _color=(1, 0, 0)):
    """ tag an edge
    """
    center_pt = get_aligned_boundingbox(_edge)[0]
    display.DisplayMessage(center_pt, msg, None, _color, False)
Exemple #5
0
def slice(shape):
    # keep track of time for perf
    init_time = time.time()
    # get the axis-aligned bounding box and dimensions of shape
    center, [dx, dy, dz], box_shp = get_aligned_boundingbox(shape)
    # get maximum z coordinate
    z_max = center.Z() + dz / 2
    # slice height in mm
    slice_height = 2  # 0.3
    # number of shells (offsets)
    num_shells = 3
    # distance between offsets
    shell_thickness = 0.4
    # list of slicing plane heights
    # z_list = [i * slice_height for i in range(int(z_max / slice_height) + 1)]
    # splitter function
    splitter = BOPAlgo_Splitter()
    splitter.SetRunParallel(True)
    splitter.SetFuzzyValue(0.001)
    splitter.AddArgument(shape)
    # loop over slicing planes
    planes = {}
    for z in arange(0, z_max, slice_height):
        # create slicing plane
        plane = gp_Pln(gp_Pnt(0., 0., z), gp.DZ())
        face = BRepBuilderAPI_MakeFace(plane).Shape()
        # add slicing plane (face) to dict
        planes[z] = face
        # add slicing plane to splitter algo
        splitter.AddTool(face)

    # split the shape
    splitter.Perform()
    # record the time spent
    split_time = time.time()
    # get the solid slices from the result
    exp = TopExp_Explorer(splitter.Shape(), TopAbs_SOLID)
    faces = []
    while exp.More():
        faces.extend(find_faces(exp.Current()))
        exp.Next()
    search_time = time.time()
    # display.EraseAll()
    # offset wires
    print('offsetting wires')
    wires = []
    # loop over faces
    for f in faces:
        # display.DisplayShape(f, update=False)
        # make offsets of all wires in face
        # wires.append(make_offsets(f, num_shells, shell_thickness))
        z = TopExp_Explorer(f, TopAbs_WIRE)
        while z.More():
            wires.append(z.Current())
            z.Next()

    offset_time = time.time()

    print('displaying offsets')
    for w in wires:
        wire_gcode(w)

    # update viewer when all is added:
    display.Repaint()
    current_time = time.time()
    print('Splitting time: ', split_time - init_time)
    print('Searching time: ', search_time - split_time)
    print('Offsetting time: ', offset_time - split_time)
    print('Display time: ', current_time - offset_time)
    print('Total time: ', current_time - init_time)
    viewer = get_viewer()
    viewer.sig_topods_selected.connect(on_select)
    start_display()