def draw_single_type_primitives(single_shape_primitives, bounding_box=None):
    ctx = GerberCairoContext()
    if bounding_box is None:
        bounding_box = get_bounding_box_of_multiply([p.bounding_box for p in single_shape_primitives])
    ctx.set_bounds(bounding_box)
    ctx._paint_background()
    ctx._new_render_layer()
    for p in single_shape_primitives:
        ctx.render(p)
    ctx._flatten()
    return ctx, bounding_box
def is_non_pth_ring_making(file_dir):
    ctx = GerberCairoContext()
    non_pth_gerber_file = file_dir + 'npth.drl'
    top_gerber_file = file_dir + 'top.gbr'
    top_primitives = get_all_primitives_from_gerber(top_gerber_file)
    non_pth_primitives = get_all_primitives_from_gerber(non_pth_gerber_file)
    shape_primitives = get_single_shape_from_gerber(top_primitives, Circle)
    Line_primitives = get_single_shape_from_gerber(top_primitives, Line)
    bounding_box = get_bounding_box_of_multiply([p.bounding_box for p in top_primitives])
    ctx.set_bounds(bounding_box)
    ctx._paint_background()
    ctx._new_render_layer()
    count = 0
    dis = []
    for non_pth_primitive in non_pth_primitives:
        (x, y) = non_pth_primitive.position
        if non_pth_primitive.position == (2.067, 2.392):
            print('ppppppp')
        for shape_primitive in shape_primitives:
            (x1, y1) = shape_primitive.position
            if ("%.3f" % x) == ("%.3f" % x1) and ("%.3f" % y) == ("%.3f" % y1):
                if shape_primitive.radius > non_pth_primitive.radius:
                    count = count + 1
                    dis.append(shape_primitive.radius - non_pth_primitive.radius)
                    if shape_primitive.position == (2.066929, 2.391732) \
                            and non_pth_primitive.position == (2.067, 2.392):
                        if shape_primitive.radius == 0.019685:
                            draw_bounding_box_of_char(Line_primitives[0], 1, shape_primitive.bounding_box, ctx)
                        print('kkkkkkkkk', shape_primitive.radius, shape_primitive.bounding_box)
                    if shape_primitive.radius - non_pth_primitive.radius < 0.01:
                        print('hhhhh', shape_primitive.radius, non_pth_primitive.radius, count)
                        print(shape_primitive.position, non_pth_primitive.position)
                        # draw_bounding_box_of_char(Line_primitives[0], 1, non_pth_primitive.bounding_box, ctx)
                        # ctx.render(non_pth_primitive.bounding_box)
                    else:
                        ctx.render(shape_primitive)

    ctx._flatten()
    ctx.dump('shapepth2.png')
    dis.sort()
    print(dis)
    print(count, len(non_pth_primitives))
    if count > 0:
        return True
    return False
def draw_all_primitives1(index_span_for_primitives,
                         all_lines_and_arc_primitives,
                         all_primitives,
                         output_dir,
                         bounding_box=None):
    if not os.path.exists(output_dir): os.makedirs(output_dir)
    ctx = GerberCairoContext()
    if bounding_box is None:
        bounding_box = get_bounding_box_of_multiply(
            [p.bounding_box for p in all_lines_and_arc_primitives])

    background_height = 20
    background_width = 20

    original_width = bounding_box[0][1] - bounding_box[0][0]
    original_height = bounding_box[1][1] - bounding_box[1][0]
    need_rotate = False
    if original_width > original_height:
        need_rotate = True
    ratio = min(background_height / original_height,
                background_width / original_width)
    (min_x, max_x), (min_y, max_y) = bounding_box
    new_bounding_box = (min_x * ratio, max_x * ratio), (min_y * ratio,
                                                        max_y * ratio)
    ctx.set_bounds(new_bounding_box)
    ctx._paint_background()
    ctx._new_render_layer()

    for i in range(len(index_span_for_primitives) - 1):
        line_index = index_span_for_primitives[i]
        begin = line_index[0]
        end = line_index[1]
        lines = all_lines_and_arc_primitives[begin:end + 1]
        small_bounding_box = get_bounding_box_of_multiply(
            [p.bounding_box for p in lines])
        draw_bounding_box_of_char(lines[0], ratio, small_bounding_box, ctx)
        if True:
            for p in lines:
                s_x, s_y = p.start
                p.start = s_x * ratio, s_y * ratio
                e_x, e_y = p.end
                p.end = e_x * ratio, e_y * ratio

                if isinstance(p, Arc):
                    c_x, c_y = p.center
                    p.center = c_x * ratio, c_y * ratio
                ctx.render(p)
    '''
        count = 0
        print('hhhhhh', len(all_primitives))
        for p in all_primitives:
        
            s_x, s_y = p.start
            p.start = s_x * ratio, s_y * ratio
            e_x, e_y = p.end
            p.end = e_x * ratio, e_y * ratio

            if isinstance(p, Arc):
                c_x, c_y = p.center
                p.center = c_x * ratio, c_y * ratio
            
            if not isinstance(p, Line):  # or isinstance(p, gerber.primitives.Region)):
                # ctx.render(p)
                count = count + 1
                # print(type(p), p.bounding_box, count, len(p.primitives))
            '''
    ctx._flatten()
    filename_with_coordination = append_filename_with_coordination(
        '{}-{}.png'.format(0, 0), bounding_box)
    image_path = os.path.join(output_dir, filename_with_coordination)
    ctx.dump(image_path)
    return filename_with_coordination
def generate_some_primitives(primitives,
                             output_dir,
                             filename,
                             bounding_box=None):
    if len(primitives) == 0: return

    if not os.path.exists(output_dir): os.makedirs(output_dir)

    ctx = GerberCairoContext()

    if bounding_box is None:
        bounding_box = get_bounding_box_of_multiply(
            [p.bounding_box for p in primitives])

    # standard_width = 1
    # standard_height = 1.4

    background_height = 2
    background_width = 2

    original_width = bounding_box[0][1] - bounding_box[0][0]
    original_height = bounding_box[1][1] - bounding_box[1][0]

    need_rotate = False

    if original_width > original_height:
        # if we find the orientation is left-to-right, we set the height smaller than height
        # standard_height, standard_width = standard_width, standard_height
        need_rotate = True

    ratio = min(background_height / original_height,
                background_width / original_width)

    (min_x, max_x), (min_y, max_y) = bounding_box
    new_bounding_box = (min_x * ratio, max_x * ratio), (min_y * ratio,
                                                        max_y * ratio)

    ctx.set_bounds(new_bounding_box)

    ctx._paint_background()
    ctx._new_render_layer()

    for p in primitives:
        s_x, s_y = p.start
        p.start = s_x * ratio, s_y * ratio

        e_x, e_y = p.end
        p.end = e_x * ratio, e_y * ratio

        if isinstance(p, Arc):
            c_x, c_y = p.center
            p.center = c_x * ratio, c_y * ratio

        ctx.render(p)

    ctx._flatten()

    filename_with_coordination = append_filename_with_coordination(
        filename, bounding_box)

    image_path = os.path.join(output_dir, '20-' + filename)

    ctx.dump(image_path)

    image = Image.open(image_path)

    if need_rotate:
        image = image.transpose(Image.ROTATE_270)

        # tranposed.show()
        # input('continue?')
        image.save(image_path)

    return filename_with_coordination
Beispiel #5
0
def draw_all_primitives(index_span_for_primitives,
                        all_lines_and_arc_primitives,
                        all_primitives,
                        output_dir,
                        bounding_box=None):
    if not os.path.exists(output_dir): os.makedirs(output_dir)
    ctx = GerberCairoContext()
    if bounding_box is None:
        bounding_box = get_bounding_box_of_multiply(
            [p.bounding_box for p in all_primitives])

    original_width = bounding_box[0][1] - bounding_box[0][0]
    original_height = bounding_box[1][1] - bounding_box[1][0]

    background_height = original_height
    background_width = original_width
    need_rotate = False
    if original_width > original_height:
        need_rotate = True
    ratio = min(background_height / original_height,
                background_width / original_width)
    (min_x, max_x), (min_y, max_y) = bounding_box
    new_bounding_box = (min_x * ratio, max_x * ratio), (min_y * ratio,
                                                        max_y * ratio)
    ctx.set_bounds(new_bounding_box)
    ctx._paint_background()
    ctx._new_render_layer()

    def size_of_box(bounding_box):
        (min_x, max_x), (min_y, max_y) = bounding_box
        return (max_x - min_x) * (max_y - min_y)

    def is_include(p, box1):
        (min_x1, max_x1), (min_y1, max_y1) = box1
        ((min_x2, max_x2), (min_y2, max_y2)) = p.bounding_box
        margin_threshold = (max_x1 - min_x1) / 3
        if min_x1 - margin_threshold <= min_x2 and max_x1 + margin_threshold >= max_x2 \
                and min_y1 - margin_threshold <= min_y2 and max_y1 + margin_threshold >= max_y2:
            return True
        return False

    check_box = []
    count = 0
    box_threshold = 0.001
    size_of_check_box = 0.0121
    checked_sign_nums = 8
    for i in range(len(index_span_for_primitives) - 1):
        line_index = index_span_for_primitives[i]
        begin = line_index[0]
        end = line_index[1]
        lines = all_lines_and_arc_primitives[begin:end + 1]
        small_bounding_box = get_bounding_box_of_multiply(
            [p.bounding_box for p in lines])
        # draw_bounding_box_of_char(lines[0], ratio, small_bounding_box, ctx)
        if abs(size_of_box(small_bounding_box) -
               size_of_check_box) < box_threshold and len(lines) == 4:
            check_box.append(small_bounding_box)
            # draw_bounding_box_of_char(lines[0], ratio, small_bounding_box, ctx)
        # draw_lines(ctx, ratio, lines)
    choose_box = []
    for p in all_primitives:
        for box in check_box:
            if isinstance(p, gerber.primitives.Region) and len(
                    p.primitives) == checked_sign_nums and is_include(p, box):
                count += 1
                ((min_xp, max_xp), (min_yp, max_yp)) = p.bounding_box
                x_min = (min_xp - min_x) / (max_x - min_x)
                x_max = (max_xp - min_x) / (max_x - min_x)
                y_min = (min_yp - min_y) / (max_y - min_y)
                y_max = (max_yp - min_y) / (max_y - min_y)
                cbox = (x_min, x_max), (y_min, y_max)
                choose_box.append(cbox)
                ctx.render(p)
                draw_bounding_box_of_char(lines[0], ratio, box, ctx)

    print('--------', len(choose_box))
    ctx._flatten()
    filename_with_coordination = append_filename_with_coordination(
        '{}-{}.png'.format(0, 0), bounding_box)
    image_path = os.path.join(output_dir, filename_with_coordination)
    ctx.dump(image_path)
    print(image_path)
    return filename_with_coordination
def draw_all_primitives(index_span_for_primitives, all_lines_and_arc_primitives, output_dir, bounding_box=None):
    if not os.path.exists(output_dir): os.makedirs(output_dir)
    ctx = GerberCairoContext()
    if bounding_box is None:
        bounding_box = get_bounding_box_of_multiply([p.bounding_box for p in all_lines_and_arc_primitives])

    background_height = 20
    background_width = 20

    original_width = bounding_box[0][1] - bounding_box[0][0]
    original_height = bounding_box[1][1] - bounding_box[1][0]
    need_rotate = False
    if original_width > original_height:
        need_rotate = True
    ratio = min(background_height / original_height, background_width / original_width)
    (min_x, max_x), (min_y, max_y) = bounding_box
    new_bounding_box = (min_x * ratio, max_x * ratio), (min_y * ratio, max_y * ratio)
    # print('ratio', ratio)
    max_area = (max_x - min_x) * (max_y - min_y)

    ctx.set_bounds(new_bounding_box)
    ctx._paint_background()

    ctx._new_render_layer()
    show_char = True

    all_grouped_box = []
    for i in range(len(index_span_for_primitives) - 1):
        line_index = index_span_for_primitives[i]
        begin = line_index[0]
        end = line_index[1]
        lines = all_lines_and_arc_primitives[begin:end + 1]
        if len(lines) >= 2:
            small_bounding_box = get_bounding_box_of_multiply([p.bounding_box for p in lines])
            (x_min, y_min), (x_max, y_max) = small_bounding_box
            # print(abs((x_max - x_min) * (y_max - y_min)))
            # print(abs((x_max - x_min) * (y_max - y_min) / max_area))
            print(max_area)
            print('*' * 20)

            if (x_max - x_min) * (y_max - y_min) / max_area > 0.1:
                print('inside point', (x_max - x_min) * (y_max - y_min))

                all_grouped_box.append(small_bounding_box)
                draw_bounding_box_of_char(lines[0], ratio, small_bounding_box, ctx)
            else:
                print('outside point', (x_max - x_min) * (y_max - y_min))

    # for ((x_min, y_min), (x_max, y_max)) in all_grouped_box:
    #     # print((x_min, y_min), (x_max, y_max))

    ctx._flatten(color=(0, 1, 0))

    ctx._new_render_layer()
    for i in range(len(index_span_for_primitives) - 1):
        line_index = index_span_for_primitives[i]
        begin = line_index[0]
        end = line_index[1]
        lines = all_lines_and_arc_primitives[begin:end + 1]
        if show_char:  # 显示被筛选出来的字符,false则只有bounding box

            for p in lines:
                s_x, s_y = p.start
                p.start = s_x * ratio, s_y * ratio
                e_x, e_y = p.end
                p.end = e_x * ratio, e_y * ratio

                if isinstance(p, Arc):
                    c_x, c_y = p.center
                    p.center = c_x * ratio, c_y * ratio
                ctx.render(p)
    ctx._flatten()

    filename_with_coordination = append_filename_with_coordination('{}-{}.png'.format(0, 0), bounding_box)
    image_path = os.path.join(output_dir, filename_with_coordination)
    ctx.dump(image_path)
    return filename_with_coordination