python - 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)
我将不胜感激有关如何解决此问题的任何建议或指导。谢谢
解决方案
推荐阅读
- git - 为什么我的 git 代理配置不起作用?我确定我已正确设置它,但它仍然无法正常工作
- unit-testing - 使用指定的初始化器语法跟踪用户定义的“结构”成员
- datetime - 如何在输入控件中获取 java.sql.Timestamp 的默认值表达式
- firebase - 使用颤振在 Firestore 上执行 CRUD 操作的最佳方法是什么?
- java - 无法确定任务 ':myapplication:preHuaweiDebugBuild' 的依赖关系
- angular7 - 如何将值从一个组件更新到另一个组件?
- c# - JsonConvert 反序列化十进制到 jtoken 将其转换为科学数,我如何将其转换回十进制?
- angular - 如何以角度展开/折叠多个面板?
- r - 无法使用 R 版本 3.4.1 创建 conda 环境
- c# - 有什么方法可以使用材质视觉在 iOS 中自定义控件样式?