Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configured CLI to allow 'manual' option for layout. #535

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions kikit/panelize_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import glob
import traceback
from kikit.panelize_ui_sections import *
from json import JSONDecodeError

PKG_BASE = os.path.dirname(__file__)
PRESETS = os.path.join(PKG_BASE, "resources/panelizePresets")
Expand Down Expand Up @@ -34,6 +35,44 @@ def splitStr(delimiter, escapeChar, s):
return x


class Input(click.ParamType):
"""
A CLI argument type for defining input boards. Can be either
- Path to PCB file
- Path to JSON file containing list of Board objects
- JSON string containing list of Board objects
"""
name = "input"

def convert(self, value, param, ctx):
if len(value.strip()) == 0:
self.fail(f"{value} is not a valid argument specification",
param, ctx)
try:
values = []
if value.endswith(".kicad_pcb"):
values.append(Board(value, ["0mm","0mm"]))
else:
json_str = value
s = Section()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Section object in order to not rewrite the conversion code.
Might be cleaner to move that code out into it's own function?

if value.endswith(".json"):
with open(value) as json_file:
json_str = json_file.read()
for d in json.loads(json_str):
source = {}
if "source" in d.keys():
source = s.convert(d.get("source"), param, ctx)
values.append(Board(d["board"], d["pos"], source))
return values
except IOError:
self.fail(f"Could not open file '{value}'",
param,
ctx)
except JSONDecodeError as e:
self.fail(f"Invalid JSON at Line: {e.lineno}, Col: {e.colno}",
param,
ctx)

class Section(click.ParamType):
"""
A CLI argument type for overriding section parameters. Basically a semicolon
Expand Down Expand Up @@ -138,8 +177,7 @@ def fun(ctx, args, incomplete):
return fun

@click.command()
@click.argument("input", type=click.Path(dir_okay=False),
**addCompatibleShellCompletion(pathCompletion(".kicad_pcb")))
@click.argument("input", type=Input())
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure what to do about shell completion

@click.argument("output", type=click.Path(dir_okay=False),
**addCompatibleShellCompletion(pathCompletion(".kicad_pcb")))
@click.option("--preset", "-p", multiple=True,
Expand Down Expand Up @@ -244,7 +282,8 @@ def doPanelization(input, output, preset, plugins=[]):
import kikit.substrate
kikit.substrate.TABFAIL_VISUAL = True

board = LoadBoard(input)

board = LoadBoard(input[0].path)
panel = Panel(output)

useHookPlugins = ki.loadHookPlugins(plugins, board, preset)
Expand Down
17 changes: 14 additions & 3 deletions kikit/panelize_ui_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def obtainPreset(presetPaths, validate=True, **kwargs):
postProcessPreset(preset)
return preset

def buildLayout(preset, panel, sourceBoard, sourceArea):
def buildLayout(preset, panel, boardList, sourceArea):
"""
Build layout for the boards - e.g., make a grid out of them.

Expand All @@ -252,7 +252,7 @@ def buildLayout(preset, panel, sourceBoard, sourceArea):
hbonefirst=layout["hbonefirst"],
vbonefirst=layout["vbonefirst"])
substrates = panel.makeGrid(
boardfile=sourceBoard, sourceArea=sourceArea,
boardfile=boardList[0].path, sourceArea=sourceArea,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just uses the first board in the list.
Would you rather it error out if there are multiple boards in the list?
Could even make a grid for each board in the list?

rows=layout["rows"], cols=layout["cols"], destination=VECTOR2I(0, 0),
rotation=layout["rotation"], placer=placer,
netRenamePattern=layout["renamenet"], refRenamePattern=layout["renameref"],
Expand All @@ -261,11 +261,22 @@ def buildLayout(preset, panel, sourceBoard, sourceArea):
panel.buildPartitionLineFromBB(framingSubstrates)
backboneCuts = buildBackBone(layout, panel, substrates, framing)
return substrates, framingSubstrates, backboneCuts
if type == "manual":
for current in boardList:
board = LoadBoard(current.path)
sourceArea = readSourceArea(current.mergeSource(preset["source"]), board)
panel.appendBoard(current.path, VECTOR2I(*current.pos), sourceArea, inheritDrc=False)
substrates = panel.substrates
framingSubstrates = dummyFramingSubstrate(substrates, preset)
panel.buildPartitionLineFromBB(framingSubstrates)
backboneCuts = buildBackBone(layout, panel, substrates, framing)
return substrates, framingSubstrates, backboneCuts

if type == "plugin":
lPlugin = layout["code"](preset, layout["arg"], layout["renamenet"],
layout["renameref"], layout["vspace"],
layout["hspace"], layout["rotation"])
substrates = lPlugin.buildLayout(panel, sourceBoard, sourceArea)
substrates = lPlugin.buildLayout(panel, boardList, sourceArea)
framingSubstrates = dummyFramingSubstrate(substrates, preset)
lPlugin.buildPartitionLine(panel, framingSubstrates)
backboneCuts = lPlugin.buildExtraCuts(panel)
Expand Down
20 changes: 17 additions & 3 deletions kikit/panelize_ui_sections.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from dataclasses import dataclass
import os
from typing import Any, List

from kikit import plugin
from kikit.units import readLength, readAngle, readPercents
from kikit.defs import Layer, EDA_TEXT_HJUSTIFY_T, EDA_TEXT_VJUSTIFY_T, PAPER_SIZES
import json

class PresetError(RuntimeError):
pass
Expand Down Expand Up @@ -240,7 +242,7 @@ def never():

LAYOUT_SECTION = {
"type": SChoice(
["grid", "plugin"],
["grid", "plugin", "manual"],
always(),
"Layout type"),
"alternation": SChoice(
Expand Down Expand Up @@ -297,8 +299,7 @@ def never():
"Reference renaming pattern"),
"baketext": SBool(
always(),
"Substitute variables in text elements"
),
"Substitute variables in text elements"),
"code": SPlugin(
plugin.LayoutPlugin,
typeIn(["plugin"]),
Expand Down Expand Up @@ -756,3 +757,16 @@ def ppDebug(section):
"Post": POST_SECTION,
"Debug": DEBUG_SECTION,
}

class Board:
def __init__(self, path, pos, source={}):
self.path = path
self.pos = [readLength(p) for p in pos]
ppSource(source)
self.source = source

def mergeSource(self, source):
merged = {}
for key, value in source.items():
merged[key] = self.source.get(key, value)
return merged