Skip to content

Commit

Permalink
Adds support for hierarchical sheets when rewriting schematics files. (
Browse files Browse the repository at this point in the history
…#506)

* Adds support for hierarchical sheets when rewriting schematics files.

Recursively proceses hierarchical sheets when rewriting schematics
files.

This only affects KiCad 8+.

* Fix dict_factory issue for all versions

---------

Co-authored-by: Bouni <bouni@owee.de>
  • Loading branch information
gonzalop and Bouni committed Aug 2, 2024
1 parent ec4aee0 commit b8a88c1
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions schematicexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def _update_schematic(self, path):
lastLoc = m.group(4)
lastRef = value
for part in store_parts:
if value == part[0]:
newLcsc = part[3]
if value == part["reference"]:
newLcsc = part["lcsc"]
break
# if we hit the pin section without finding a LCSC property, add it
m = pinRx.search(inLine)
Expand Down Expand Up @@ -162,8 +162,8 @@ def _update_schematic7(self, path):
lastLoc = m.group(3)
lastRef = value
for part in store_parts:
if value == part[0]:
newLcsc = part[3]
if value == part["reference"]:
newLcsc = part["lcsc"]
break
# if we hit the pin section without finding a LCSC property, add it
m = pinRx.search(inLine)
Expand Down Expand Up @@ -206,10 +206,8 @@ def _update_schematic8(self, path):
with open(path, encoding="utf-8") as f:
lines = f.readlines()

if os.path.exists(path + "_old"):
os.remove(path + "_old")
os.rename(path, path + "_old")
partSection = False
files_seen = set() # keeps sheet files already processed.

for i in range(0, len(lines) - 1):
inLine = lines[i].rstrip()
Expand All @@ -230,7 +228,7 @@ def _update_schematic8(self, path):
value = m.group(2)
lastLcsc = value
if newLcsc not in (lastLcsc, ""):
self.logger.info("Updating %s on %s", newLcsc, lastRef)
self.logger.info("Updating %s on %s in %s", newLcsc, lastRef, path)
outLine = outLine.replace(
'"' + lastLcsc + '"', '"' + newLcsc + '"'
)
Expand All @@ -242,9 +240,15 @@ def _update_schematic8(self, path):
# self.logger.info("value %s", value)
lastRef = value
for part in store_parts:
if value == part[0]:
newLcsc = part[3]
if value == part["reference"]:
newLcsc = part["lcsc"]
break
if key == "Sheetfile":
file_name = m.group(2)
if file_name not in files_seen:
files_seen.add(file_name)
dir_name = os.path.dirname(path)
self._update_schematic8(os.path.join(dir_name, file_name))
# if we hit the pin section without finding a LCSC property, add it
m3 = pinRx.search(inLine)
if m3 and partSection:
Expand All @@ -263,6 +267,9 @@ def _update_schematic8(self, path):
lastRef = ""
newlines.append(outLine)
newlines.append(lines[len(lines) - 1].rstrip())
if os.path.exists(path + "_old"):
os.remove(path + "_old")
os.rename(path, path + "_old")
with open(path, "w", encoding="utf-8") as f:
for line in newlines:
f.write(line + "\n")
Expand Down

0 comments on commit b8a88c1

Please sign in to comment.