如何遍历嵌套字典在Schemdraw中绘制无引脚重叠的集成电路
集成电路绘制重叠问题修复
问题现象
遍历嵌套字典调用schemdraw库绘制集成电路时,生成的绘图中数值相互重叠,无法在集成电路上生成独立的对应引脚条目,疑似缺少迭代逻辑。
原有问题代码
import schemdraw import schemdraw.elements.intcircuits as eInt dict1 = {'channel 1': {'source_name': '1', }, 'channel 2': {'source_name': '2', }} with schemdraw.Drawing(file='/Users/Desktop/circuitdrawing.png') as d: for channel, info in dict1.items(): if channel != None: for channel, info in dict1.items(): #might need another level of iteration here? d += eInt.Ic(pins=[eInt.IcPin(name=(channel), pin=(info['source_name']), side='left')], edgepadW = 2, pinspacing = 1).label('console 1', 'top', fontsize=12) else: continue
问题根因
- 代码写了两层完全重复的循环遍历同一个字典,每次循环都会新建一个独立的IC元件,所有元件默认定位在画布同一坐标,直接导致图形、文字重叠
- 逻辑方向错误:不需要多层循环创建多个IC,只需要创建1个IC实例,提前遍历字典把所有引脚收集为列表,一次性传入IC的pins参数即可
修正后代码
import schemdraw import schemdraw.elements.intcircuits as eInt dict1 = { 'channel 1': {'source_name': '1'}, 'channel 2': {'source_name': '2'} } with schemdraw.Drawing(file='/Users/Desktop/circuitdrawing.png') as d: # 单次遍历收集所有引脚配置 ic_pins = [] for channel, info in dict1.items(): ic_pins.append( eInt.IcPin(name=channel, pin=info['source_name'], side='left') ) # 仅创建1个IC元件,传入全部引脚列表 d += eInt.Ic( pins=ic_pins, edgepadW=2, pinspacing=1 ).label('console 1', 'top', fontsize=12)
补充说明
修正后所有引脚会按照pinspacing设定的间距自动排列在IC对应侧边,不会出现重叠,每个channel对应独立引脚条目。如果后续需要增加其他侧边的引脚,只需要在收集引脚时给对应IcPin指定side参数(支持left/right/top/bottom),追加到引脚列表即可。
内容的提问来源于stack exchange,提问作者Briana Billups
相关产品推荐
相关产品推荐

