Пример #1
0
def generate_nfp(nfp_paris):
    """
    :param nfp_paris:
    :return:
    """

    nfp_list = list()

    for pair in nfp_paris:
        poly_a = pair['A']

        poly_a['points'] = nfp_utls.rotate_polygon(
            poly_a['points'], pair['key']['A_rotation'])['points']
        poly_b = pair['B']
        poly_b['points'] = nfp_utls.rotate_polygon(
            poly_b['points'], pair['key']['B_rotation'])['points']

        if pair['key']['inside']:
            nfp = nfp_utls.nfp_rectangle(poly_a['points'], poly_b['points'])

            if nfp_utls.polygon_area(nfp) > 0:
                nfp.reverse()
        else:
            # pair['key']['inside'] == False , so compute no fit polygon between two segments
            # nfp = nfp_utls.nfp_polygon(poly_a, poly_b)    # 使用自己写的生成nfp的函数
            nfp = minkowski_difference(
                poly_a, poly_b)  # 使用 Minkowski_difference和求两个零件的nfp, 考虑用lib
            # print("nfp = ", nfp)

            if nfp_utls.polygon_area(nfp) > 0:
                nfp.reverse()

        nfp_list.append({'key': pair['key'], 'value': nfp})

    return nfp_list
Пример #2
0
    def process_nfp(self, pair):
        """
        计算所有图形两两组合的相切多边形(NFP)
        :param pair: 两个组合图形的参数
        :return:
        """
        if pair is None or len(pair) == 0:
            return None

        # 考虑有没有洞和凹面
        search_edges = self.config['exploreConcave']
        use_holes = self.config['useHoles']

        # 图形参数
        A = copy.deepcopy(pair['A'])
        A['points'] = nfp_utls.rotate_polygon(
            A['points'], pair['key']['A_rotation'])['points']
        B = copy.deepcopy(pair['B'])
        B['points'] = nfp_utls.rotate_polygon(
            B['points'], pair['key']['B_rotation'])['points']

        if pair['key']['inside']:
            # 内切或者外切
            if nfp_utls.is_rectangle(A['points'], 0.0001):
                nfp = nfp_utls.nfp_rectangle(A['points'], B['points'])
            else:
                nfp = nfp_utls.nfp_polygon(A, B, True, search_edges)

            # ensure all interior NFPs have the same winding direction
            if nfp and len(nfp) > 0:
                for i in range(0, len(nfp)):
                    if nfp_utls.polygon_area(nfp[i]) > 0:
                        nfp[i].reverse()
            else:
                pass
                # print('NFP Warning:', pair['key'])

        else:
            if search_edges:
                nfp = nfp_utls.nfp_polygon(A, B, False, search_edges)
            else:
                nfp = minkowski_difference(A, B)

            # 检查NFP多边形是否合理
            if nfp is None or len(nfp) == 0:
                pass
                # print('error in NFP 260')
                # print('NFP Error:', pair['key'])
                # print('A;', A)
                # print('B:', B)
                return None

            for i in range(0, len(nfp)):
                # if search edges is active, only the first NFP is guaranteed to pass sanity check
                if not search_edges or i == 0:
                    if abs(nfp_utls.polygon_area(nfp[i])) < abs(
                            nfp_utls.polygon_area(A['points'])):
                        pass
                        # print('error in NFP area 269')
                        # print('NFP Area Error: ', abs(nfp_utls.polygon_area(nfp[i])), pair['key'])
                        # print('NFP:', json.dumps(nfp[i]))
                        # print('A: ', A)
                        # print('B: ', B)
                        nfp.pop(i)
                        return None

            if len(nfp) == 0:
                return None
            # for outer NFPs, the first is guaranteed to be the largest.
            # Any subsequent NFPs that lie inside the first are hole
            for i in range(0, len(nfp)):
                if nfp_utls.polygon_area(nfp[i]) > 0:
                    nfp[i].reverse()

                if i > 0:
                    if nfp_utls.point_in_polygon(nfp[i][0], nfp[0]):
                        if nfp_utls.polygon_area(nfp[i]) < 0:
                            nfp[i].reverse()

            # generate nfps for children (holes of parts) if any exist
            # 有洞的暂时不管
            if use_holes and len(A) > 0:
                pass
        return {'key': pair['key'], 'value': nfp}
Пример #3
0
    def process_nfp(self, pair):
        """
        Tính đa giác tiếp tuyến của tất cả các cặp hình
        :param pair: Tham số kết hợp của hai hình
        :return:
        """
        if pair is None or len(pair) == 0:
            return None

        # Tham số cài đặt, có lỗ hay mặt lõm hay không
        search_edges = self.config['exploreConcave']
        use_holes = self.config['useHoles']

        # Thông số đồ họa, quay các hình theo góc quay
        # A nếu là bin thì không chứa thông tin offset
        A = copy.deepcopy(pair['A'])
        A['points'] = nfp_utls.rotate_polygon(
            A['points'], pair['key']['A_rotation'])['points']

        # Là path, chứa thông tin offset
        B = copy.deepcopy(pair['B'])
        # Rotate B theo key pair đã tạo với A
        B['points'] = nfp_utls.rotate_polygon(
            B['points'], pair['key']['B_rotation'])['points']

        if pair['key']['inside']:
            # Inside or outside
            # Thường thì đây là cặp bin và path, tính NPF của part với bin hoặc giữa các path với nhau
            if nfp_utls.is_rectangle(A['points'], 0.0001):
                # Tính npf của hình với bin
                nfp = nfp_utls.nfp_rectangle(A['points'], B['points'])
            else:
                nfp = nfp_utls.nfp_polygon(A, B, True, search_edges)

            # ensure all interior NFPs have the same winding direction

            # Test_Di chuyển về sát lề
            # for nf in nfp:
            #     for p in nf:
            #         p['x'] = p['x'] - 10
            #         p['y'] = p['y'] - 10

            if nfp and len(nfp) > 0:
                for i in range(0, len(nfp)):
                    if nfp_utls.polygon_area(nfp[i]) > 0:
                        nfp[i].reverse()
            else:
                pass
                # print('NFP Warning:', pair['key'])

        else:
            if search_edges:
                nfp = nfp_utls.nfp_polygon(A, B, False, search_edges)
            else:
                nfp = minkowski_difference(A, B)

            # Kiểm tra xem đa giác NFP có hợp lý không
            if nfp is None or len(nfp) == 0:
                pass
                # print('error in NFP 260')
                # print('NFP Error:', pair['key'])
                # print('A;', A)
                # print('B:', B)
                return None

            for i in range(0, len(nfp)):
                # if search edges is active, only the first NFP is guaranteed to pass sanity check
                if not search_edges or i == 0:
                    if abs(nfp_utls.polygon_area(nfp[i])) < abs(
                            nfp_utls.polygon_area(A['points'])):
                        pass
                        # print('error in NFP area 269')
                        # print('NFP Area Error: ', abs(nfp_utls.polygon_area(nfp[i])), pair['key'])
                        # print('NFP:', json.dumps(nfp[i]))
                        # print('A: ', A)
                        # print('B: ', B)
                        nfp.pop(i)
                        return None

            if len(nfp) == 0:
                return None
            # for outer NFPs, the first is guaranteed to be the largest.
            # Any subsequent NFPs that lie inside the first are hole
            for i in range(0, len(nfp)):
                if nfp_utls.polygon_area(nfp[i]) > 0:
                    nfp[i].reverse()

                if i > 0:
                    if nfp_utls.point_in_polygon(nfp[i][0], nfp[0]):
                        if nfp_utls.polygon_area(nfp[i]) < 0:
                            nfp[i].reverse()

            # generate nfps for children (holes of parts) if any exist
            # Leave the hole in
            if use_holes and len(A) > 0:
                pass
        return {'key': pair['key'], 'value': nfp}