Skip to content

Commit

Permalink
Move parameters from **kwargs to more explicit parameters when creati…
Browse files Browse the repository at this point in the history
…ng Parts.

* Fix problem where part prefix was missing for parts created algorithmically instead of from a part library.
* Moved import of kinet2pcb into _gen_pcb_ function so it doesn't occur unless a KiCad PCB is generated. This avoids some problems when trying to import pcbnew.
  • Loading branch information
xesscorp committed May 25, 2021
1 parent 61c4e3d commit 0a45472
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
35 changes: 19 additions & 16 deletions skidl/part.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ class Part(SkidlBaseObject):
part_defn: A list of strings that define the part (usually read from a
schematic library file).
circuit: The Circuit object this Part belongs to.
ref_prefix: Prefix for part references such as 'U' or 'J'.
ref: A specific part reference to be assigned.
tag: A specific tag to tie the part to its footprint in the PCB.
pin_splitters: String of characters that split long pin names into shorter aliases.
tool_version: Select between KiCad V5 and V6.
Keyword Args:
kwargs: Name/value pairs for setting attributes for the part.
Expand All @@ -146,6 +151,11 @@ def __init__(
connections=None,
part_defn=None,
circuit=None,
ref_prefix='U',
ref=None,
tag=None,
pin_splitters=None,
tool_version=None,
**kwargs
):

Expand All @@ -166,18 +176,11 @@ def __init__(
self.name = name # Assign initial part name.
self.description = "" # Make sure there is a description, even if empty.
self._ref = "" # Provide a member for holding a reference.
self.ref_prefix = "" # Provide a member for holding the part reference prefix.
self.ref_prefix = ref_prefix # Store the part reference prefix.
self.tool = tool # Initial type of part (SKIDL, KICAD, etc.)
self.circuit = None # Part starts off unassociated with any circuit.
self.match_pin_regex = False # Don't allow regex matches of pin names.

# Remove a part reference if it has been explicitly set as None.
# Otherwise, this causes the assigned part reference to be incremented twice:
# once by Circuit.add_parts() and again by setattr().
ref = kwargs.pop("ref", None)
if ref:
kwargs["ref"] = ref

# Create a Part from a library entry.
if lib:
# If the lib argument is a string, then create a library using the
Expand Down Expand Up @@ -219,7 +222,6 @@ def __init__(

# If given, set the tool version before parsing the part definition.
# At this time, this is done to support differences between KiCad V5 and V6.
tool_version = kwargs.pop("tool_version", None)
if tool_version:
self.tool_version = tool_version

Expand All @@ -238,15 +240,12 @@ def __init__(
)

# Split multi-part pin names into individual pin aliases.
self.split_pin_names(kwargs.pop("pin_splitters", None))
self.split_pin_names(pin_splitters)

# Setup the tag for tieing the part to a footprint in a pcb editor.
# Use the user specified tag if present.
tag = kwargs.pop("tag", None)
if tag is not None:
self.tag = tag
else:
self.tag = str(randint(0, 2 ** 64 - 1))
# Do this before adding the part to the circuit or an exception will occur
# because the part can't give its hierarchical name to the circuit.
self.tag = tag or str(randint(0, 2 ** 64 - 1))

if dest != LIBRARY:
if dest == NETLIST:
Expand All @@ -267,6 +266,10 @@ def __init__(
# Add any XSPICE I/O as pins. (This only happens with SPICE simulations.)
self.add_xspice_io(kwargs.pop("io", []))

# Set the part reference if one was explicitly provided.
if ref:
self.ref = ref

# Add any other passed-in attributes to the part.
for k, v in list(kwargs.items()):
setattr(self, k, v)
Expand Down
8 changes: 7 additions & 1 deletion skidl/tools/kicad.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from collections import namedtuple

from future import standard_library
import kinet2pcb # For creating KiCad PCB directly from Circuit object.

from ..common import *
from ..coord import *
Expand Down Expand Up @@ -986,6 +985,13 @@ def _gen_netlist_net_(self):

def _gen_pcb_(self, file_):
"""Create a KiCad PCB file directly from a Circuit object."""

# Keep the import in here so it doesn't get triggered unless this is used
# so it eases some problems with tox testing.
# It requires pcbnew module which may not be present or may be for the
# wrong Python version (2 vs. 3).
import kinet2pcb # For creating KiCad PCB directly from Circuit object.

file_ = file_ or (get_script_name() + ".kicad_pcb")
kinet2pcb.kinet2pcb(self, file_)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_hierarchical_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@


class Resistor(Part):
def __init__(self, value, ref=None, footprint="Resistors_SMD:R_0805", **kwargs):
def __init__(self, value, ref=None, footprint="Resistors_SMD:R_0805", tag=None, **kwargs):
super().__init__(
"Device", "R", value=value, ref=ref, footprint=footprint, **kwargs
"Device", "R", value=value, ref=ref, footprint=footprint, tag=tag, **kwargs
)


Expand Down

0 comments on commit 0a45472

Please sign in to comment.