首页 > 解决方案 > python中的分层矩形

问题描述

我正在向SKiDL添加功能以自动生成 KiCAD 原理图(电路图片),我需要一种嵌套这些分层电路的方法。

在附图中, buck0vin_protection0电路需要嵌套在buck0层次结构中。然后我需要将所有电路嵌套在父stm32f405r0电路内。

每个电路轮廓矩形由 4 个点组成:

    rect_coord = {
        'xMin',
        'xMax',
        'yMin',
        'yMax',
    }

我已经获得了一些基本的层次结构移动(带有碰撞检测)代码,但它不能很好地处理嵌套层次结构:

def move_subhierarchy(moving_hierarchy, dx, dy, hierarchy_list, move_dir = 'L'):
    hierarchy_list[moving_hierarchy]['outline_coord']['xMin'] += dx
    hierarchy_list[moving_hierarchy]['outline_coord']['xMax'] += dx
    hierarchy_list[moving_hierarchy]['outline_coord']['yMin'] += dy
    hierarchy_list[moving_hierarchy]['outline_coord']['yMax'] += dy


    for pt in hierarchy_list[moving_hierarchy]['parts']:
        pt.sch_bb[0] += dx
        pt.sch_bb[1] += dy
    
    for w in hierarchy_list[moving_hierarchy]['wires']:
        w[0][0] += dx
        w[0][1] += dy
        w[1][0] += dx
        w[1][1] += dy
    # Check to see if we're colliding with any other parts

    # Range through hierarchies and look for overlaps of outlines
    # If we are overlapping then nudge the part 50mil left/right and rerun this function
    for h in hierarchy_list:
        # Don't detect collisions with itself
        if h == moving_hierarchy:
            continue
        # Don't detect collisions with hierarchies outside the parents
        mv_hr_lst = moving_hierarchy.split('.')
        h_lst = h.split('.')
        # check if the parent hierarchy is in the hierarchy being evaluated
        if set(mv_hr_lst[:-1]).issubset(set(h_lst)):

            # Calculate the min/max for x/y in order to detect collision between rectangles
            x1min = hierarchy_list[moving_hierarchy]['outline_coord']['xMin']
            x1max = hierarchy_list[moving_hierarchy]['outline_coord']['xMax']
            
            x2min = hierarchy_list[h]['outline_coord']['xMin']
            x2max = hierarchy_list[h]['outline_coord']['xMax']
            
            y1min = hierarchy_list[moving_hierarchy]['outline_coord']['yMax']
            y1max = hierarchy_list[moving_hierarchy]['outline_coord']['yMin']
            
            y2min = hierarchy_list[h]['outline_coord']['yMax']
            y2max = hierarchy_list[h]['outline_coord']['yMin']
            # Logic to tell whether parts collide
            # Note that the movement direction is opposite of what's intuitive ('R' = move left, 'U' = -50)
            # https://stackoverflow.com/questions/20925818/algorithm-to-check-if-two-boxes-overlap

            if (x1min <= x2max) and (x2min <= x1max) and (y1min <= y2max) and (y2min <= y1max):
                if move_dir == 'R':
                    move_subhierarchy(moving_hierarchy, 200, 0, hierarchy_list, move_dir = move_dir)
                else:
                    move_subhierarchy(moving_hierarchy, -200, 0, hierarchy_list, move_dir = move_dir)

我将不胜感激有关如何解决此问题的任何建议或指导。谢谢

标签: pythonrectangleshierarchicalkicad

解决方案


推荐阅读