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

Allow component rotation to be overridden in CLI #145

Open
wants to merge 3 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
28 changes: 25 additions & 3 deletions pcbdraw/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ class ResistorValue:
value: Optional[str] = None
flip_bands: bool=False

@dataclass
class RotationValue:
value: Optional[str] = None

def collect_holes(board: pcbnew.BOARD) -> List[Hole]:
holes: List[Hole] = [] # Tuple: position, orientation, drillsize
Expand Down Expand Up @@ -776,6 +779,7 @@ class PlotComponents(PlotInterface):
filter: Callable[[str], bool] = lambda x: True # Components to show
highlight: Callable[[str], bool] = lambda x: False # References to highlight
remapping: Callable[[str, str, str], Tuple[str, str]] = lambda ref, lib, name: (lib, name)
rotation_values: Dict[str, RotationValue] = field(default_factory=dict)
resistor_values: Dict[str, ResistorValue] = field(default_factory=dict)

def render(self, plotter: PcbPlotter) -> None:
Expand Down Expand Up @@ -812,7 +816,7 @@ def _append_component(self, lib: str, name: str, ref: str, value: str,
else:
ret = self._create_component(lib, name, ref, value)
if ret is None:
self._plotter.yield_warning("component", f"Component {lib}:{name} has no footprint.")
self._plotter.yield_warning("component", f"Component \"{ref}\" {lib}:{name} has no footprint.")
return
component_element, component_info = ret
self._used_components[unique_name] = component_info
Expand All @@ -821,10 +825,19 @@ def _append_component(self, lib: str, name: str, ref: str, value: str,
group = etree.Element("g")
group.append(component_element)
ci = component_info

rotation = -math.degrees(position[2])

# Override rotation values
if ref in self.rotation_values:
v = self.rotation_values[ref].value
if v is not None:
rotation = v

group.attrib["transform"] = \
f"translate({self._plotter.ki2svg(position[0])} {self._plotter.ki2svg(position[1])}) " + \
f"scale({ci.scale[0]}, {ci.scale[1]}) " + \
f"rotate({-math.degrees(position[2])}) " + \
f"rotate({rotation}) " + \
f"translate({-ci.origin[0]} {-ci.origin[1]})"
self._plotter.append_component_element(group)

Expand Down Expand Up @@ -879,9 +892,18 @@ def _build_highlight(self, ref: str, info: PlacedComponentInfo,
width=str(self._plotter.ki2svg(int(info.size[0] + 2 * padding))),
height=str(self._plotter.ki2svg(int(info.size[1] + 2 * padding))),
style=self._plotter.get_style("highlight-style"))

rotation = -math.degrees(position[2])

# Override rotation values
if ref in self.rotation_values:
v = self.rotation_values[ref].value
if v is not None:
rotation = v

h.attrib["transform"] = \
f"translate({self._plotter.ki2svg(position[0])} {self._plotter.ki2svg(position[1])}) " + \
f"rotate({-math.degrees(position[2])}) " + \
f"rotate({rotation}) " + \
f"translate({-(info.origin[0] - info.svg_offset[0]) * info.scale[0]}, {-(info.origin[1] - info.svg_offset[1]) * info.scale[1]})"
self._plotter.append_highlight_element(h)

Expand Down
23 changes: 17 additions & 6 deletions pcbdraw/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from . import __version__
from .convert import save
from .plot import (PcbPlotter, PlotComponents, PlotPaste, PlotPlaceholders,
PlotSubstrate, PlotVCuts, ResistorValue, load_remapping,
mm2ki)
PlotSubstrate, PlotVCuts, ResistorValue, RotationValue,
load_remapping, mm2ki)
from .populate import populate
from .pcbnew_common import fakeKiCADGui

Expand Down Expand Up @@ -117,6 +117,8 @@ def __call__(self, tag: str, msg: str) -> None:
help="Comma separated list of resistor value remapping. For example, \"R1:10k,R2:470\"")
@click.option("--resistor-flip", type=CommaList(), default=[],
help="Comma separated list of resistor bands to flip")
@click.option("--rotation-values", type=CommaList(), default=[],
help="Comma separated list of rotation value remapping. For example, \"R1:90,R2:270\"")
@click.option("--paste", is_flag=True,
help="Add paste layer")
@click.option("--components/--no-components", default=True,
Expand All @@ -132,7 +134,8 @@ def plot(input: str, output: str, style: Optional[str], libs: List[str],
mirror: bool, highlight: List[str], filter: Optional[List[str]],
vcuts: bool, dpi: int, margin: float, silent: bool, werror: bool,
resistor_values: List[str], resistor_flip: List[str], components: bool,
copper: bool, paste: bool, outline_width: float, show_lib_paths: bool) -> int:
rotation_values: List[str], copper: bool, paste: bool, outline_width: float,
show_lib_paths: bool) -> int:
"""
Create a stylized drawing of the PCB.
"""
Expand Down Expand Up @@ -178,7 +181,8 @@ def plot(input: str, output: str, style: Optional[str], libs: List[str],

if components:
plotter.plot_plan.append(
build_plot_components(remap, highlight, filter, resistor_flip, resistor_values))
build_plot_components(remap, highlight, filter, resistor_flip, resistor_values,
rotation_values))
if placeholders:
plotter.plot_plan.append(PlotPlaceholders())

Expand All @@ -191,9 +195,10 @@ def plot(input: str, output: str, style: Optional[str], libs: List[str],
return 0

def build_plot_components(remap: str, highlight: List[str], filter: Optional[List[str]],
resistor_flip: List[str], resistor_values_input: List[str]) \
-> PlotComponents:
resistor_flip: List[str], resistor_values_input: List[str],
rotation_values_input: List[str]) -> PlotComponents:
remapping = load_remapping(remap)

def remapping_fun(ref: str, lib: str, name: str) -> Tuple[str, str]:
if ref in remapping:
remapped_lib, remapped_name = remapping[ref]
Expand All @@ -212,8 +217,14 @@ def remapping_fun(ref: str, lib: str, name: str) -> Tuple[str, str]:
field.flip_bands = True
resistor_values[ref] = field

rotation_values = {}
for mapping in rotation_values_input:
key, value = tuple(mapping.split(":"))
rotation_values[key] = RotationValue(value=value)

plot_components = PlotComponents(
remapping=remapping_fun,
rotation_values=rotation_values,
resistor_values=resistor_values)

if filter is not None:
Expand Down
Loading