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

Use dict_factory in store for better readability #487

Merged
merged 23 commits into from
Jul 30, 2024
Merged
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
25 changes: 16 additions & 9 deletions fabrication.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ def generate_cpl(self):
part = self.parent.store.get_part(fp.GetReference())
if not part: # No matching part in the database, continue
continue
if part[6] == 1: # Exclude from POS
if part["exclude_from_pos"] == 1:
continue
if not add_without_lcsc and not part[3]:
if not add_without_lcsc and not part["lcsc"]:
continue
try: # Kicad <= 8.0
position = self.get_position(fp) - aux_orgin
Expand All @@ -285,9 +285,9 @@ def generate_cpl(self):
position = VECTOR2I(x1 - x2, y1 - y2)
writer.writerow(
[
part[0],
part[1],
part[2],
part["reference"],
part["value"],
part["footprint"],
ToMM(position.x),
ToMM(position.y) * -1,
self.fix_rotation(fp),
Expand All @@ -310,19 +310,26 @@ def generate_bom(self):
writer = csv.writer(csvfile, delimiter=",")
writer.writerow(["Comment", "Designator", "Footprint", "LCSC"])
for part in self.parent.store.read_bom_parts():
components = part[1].split(",")
components = part["refs"].split(",")
for component in components:
for fp in self.board.Footprints():
if fp.GetReference() == component and fp.IsDNP():
components.remove(component)
part[1] = ",".join(components)
part["refs"] = ",".join(components)
self.logger.info(
"Component %s has 'Do not placed' enabled: removing from BOM",
component,
)
if not add_without_lcsc and not part[3]:
if not add_without_lcsc and not part["lcsc"]:
continue
writer.writerow(part)
writer.writerow(
[
part["value"],
part["refs"],
part["footprint"],
part["lcsc"]
]
)
self.logger.info(
"Finished generating BOM file %s", os.path.join(self.outputdir, bomname)
)
8 changes: 8 additions & 0 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ def alphanum_key(key):
return -1 if natorder.index(a) == 0 else 1


def dict_factory(cursor, row) -> dict:
"""Row factory that returns a dict."""
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d


def get_lcsc_value(fp):
"""Get the first lcsc number (C123456 for example) from the properties of the footprint."""
# KiCad 7.99
Expand Down
69 changes: 41 additions & 28 deletions mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def assign_parts(self, e):
"""Assign a selected LCSC number to parts."""
for reference in e.references:
self.store.set_lcsc(reference, e.lcsc)
self.store.set_stock(reference, e.stock)
self.store.set_stock(reference, int(e.stock))
self.populate_footprint_list()

def display_message(self, e):
Expand Down Expand Up @@ -590,51 +590,64 @@ def populate_footprint_list(self, *_):
numbers = []
parts = []
for part in self.store.read_all():
fp = self.pcbnew.GetBoard().FindFootprintByReference(part[0])
if part[3] and part[3] not in numbers:
numbers.append(part[3])
part.insert(4, "")
part[5] = str(part[5])
fp = self.pcbnew.GetBoard().FindFootprintByReference(part["reference"])
if part["lcsc"] and part["lcsc"] not in numbers:
numbers.append(part["lcsc"])
part["stock"] = str(part["stock"])
# don't show the part if hide BOM is set
if self.hide_bom_parts and part[6]:
if self.hide_bom_parts and part["exclude_from_bom"]:
continue
# don't show the part if hide POS is set
if self.hide_pos_parts and part[7]:
if self.hide_pos_parts and part["exclude_from_pos"]:
continue
# decide which icon to use
part[6] = icons.get(part[6], icons.get(0))
part[7] = icons.get(part[7], icons.get(0))
part.insert(8, "")
side = "Top" if fp.GetLayer() == 0 else "Bot"
part.insert(9, side)
part.insert(10, "")
part["exclude_from_bom"] = icons.get(part["exclude_from_bom"], icons.get(0))
part["exclude_from_pos"] = icons.get(part["exclude_from_pos"], icons.get(0))
part["side"] = "Top" if fp.GetLayer() == 0 else "Bot"
part["rotation"] = ""
part["type"] = ""
part["side"] = ""
parts.append(part)
details = self.library.get_part_details(numbers)
corrections = self.library.get_all_correction_data()
# find rotation correction values
for part in parts:
detail = list(
filter(
lambda x, lcsc=part[3]: x[0] == lcsc,
lambda x, lcsc=part["lcsc"]: x[0] == lcsc,
details,
)
)
if detail:
part[4] = detail[0][2]
part[5] = detail[0][1]
part["type"] = detail[0][2]
part["stock"] = detail[0][1]
# First check if the part name mathes
for regex, correction in corrections:
if re.search(regex, str(part[1])):
part[8] = str(correction)
if re.search(regex, str(part["reference"])):
part["rotation"] = str(correction)
break
# If there was no match for the part name, check if the package matches
if part[8] == "":
if part["rotation"] == "":
for regex, correction in corrections:
if re.search(regex, str(part[2])):
part[8] = str(correction)
if re.search(regex, str(part["footprint"])):
part["rotation"] = str(correction)
break

self.footprint_list.AppendItem(part)
self.footprint_list.AppendItem(
[
part["reference"],
part["value"],
part["footprint"],
part["lcsc"],
part["type"],
part["stock"],
part["exclude_from_bom"],
part["exclude_from_pos"],
part["rotation"],
part["side"],
"",
]
)

def OnSortFootprintList(self, e):
"""Set order_by to the clicked column and trigger list refresh."""
Expand Down Expand Up @@ -766,8 +779,8 @@ def toggle_bom_pos(self, *_):
fp = board.FindFootprintByReference(ref)
bom = toggle_exclude_from_bom(fp)
pos = toggle_exclude_from_pos(fp)
self.store.set_bom(ref, bom)
self.store.set_pos(ref, pos)
self.store.set_bom(ref, int(bom))
Copy link
Collaborator

Choose a reason for hiding this comment

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

what was up with the necessity to cast here but not previously? (No mention in the commit message of the reason why)

Copy link
Owner Author

Choose a reason for hiding this comment

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

I don't remember but I guess that sqlite doesn't like to get a "1" for a boolean value. Previously the casting was done in th e set_bom function, in other places it was done like this. So I decided to make the cast always in the same place.

self.store.set_pos(ref, int(pos))
self.footprint_list.SetValue(
GetListIcon(bom, self.scale_factor), row, Column.BOM
)
Expand All @@ -785,7 +798,7 @@ def toggle_bom(self, *_):
board = self.pcbnew.GetBoard()
fp = board.FindFootprintByReference(ref)
bom = toggle_exclude_from_bom(fp)
self.store.set_bom(ref, bom)
self.store.set_bom(ref, int(bom))
self.footprint_list.SetValue(
GetListIcon(bom, self.scale_factor), row, Column.BOM
)
Expand All @@ -800,7 +813,7 @@ def toggle_pos(self, *_):
board = self.pcbnew.GetBoard()
fp = board.FindFootprintByReference(ref)
pos = toggle_exclude_from_pos(fp)
self.store.set_pos(ref, pos)
self.store.set_pos(ref, int(pos))
self.footprint_list.SetValue(
GetListIcon(pos, self.scale_factor), row, Column.POS
)
Expand Down Expand Up @@ -829,7 +842,7 @@ def select_alike(self, *_):
for r in range(self.footprint_list.GetItemCount()):
value = self.footprint_list.GetValue(r, 1)
fp = self.footprint_list.GetValue(r, 2)
if part[1] == value and part[2] == fp:
if part["value"] == value and part["footprint"] == fp:
self.footprint_list.SelectRow(r)

def get_part_details(self, *_):
Expand Down
Loading