feat: add complete yiyezhqiu productivity suite

This commit is contained in:
阿白叔 committed 2026-10-10 17:10:32 +08:00
commit e422ca8b2a
155 files changed
+18959

No files matched your search

+124
View File
@@ -0,0 +1,124 @@
from dx_productivity.features.assembly_refdes.planner import (
ComponentFootprint,
DeletePlan,
FabricationText,
MovePlan,
plan_assembly_refdes,
render_plan,
)
def _text(index, **overrides):
defaults = dict(
index=index,
text_type=1,
side=1,
is_assembly=True,
text_string="C1",
orientation=0.0,
component_name="C1",
)
defaults.update(overrides)
return FabricationText(**defaults)
def test_refdes_moves_to_component_origin():
texts = [_text(0)]
footprints = {"C1": ComponentFootprint("C1", 10.0, 20.0, 0.5, 0.3)}
plans, processed = plan_assembly_refdes(texts, footprints)
assert processed == 1
plan = plans[0]
assert isinstance(plan, MovePlan)
assert (plan.x, plan.y) == (10.0, 20.0)
assert plan.rotation == 0
assert plan.height == 0.3 # within [0.1, 0.8] keeps the outline height
def test_small_and_large_heights_clamp_to_max():
texts = [_text(0), _text(1, text_string="R10000", component_name="R1")]
footprints = {
"C1": ComponentFootprint("C1", 0.0, 0.0, 0.5, 0.05),
"R1": ComponentFootprint("R1", 0.0, 0.0, 0.5, 2.0),
}
plans, _ = plan_assembly_refdes(texts, footprints)
assert all(isinstance(p, MovePlan) and p.height == 0.8 for p in plans)
def test_non_refdes_fab_text_is_deleted():
texts = [_text(0, text_type=0)]
footprints = {"C1": ComponentFootprint("C1", 0, 0, 1, 1)}
plans, processed = plan_assembly_refdes(texts, footprints)
assert processed == 0
assert isinstance(plans[0], DeletePlan)
def test_board_level_text_without_component_is_untouched():
texts = [_text(0, component_name="")]
plans, _ = plan_assembly_refdes(texts, {})
assert plans == ()
def test_bottom_side_vertical_text_gets_270():
texts = [_text(0, orientation=270, side=0)]
footprints = {"C1": ComponentFootprint("C1", 1, 2, 0.4, 0.5)}
plans, _ = plan_assembly_refdes(texts, footprints)
assert plans[0].rotation == 270
assert plans[0].height == 0.4 # x half span
def test_diagonal_uses_the_legacy_factor():
texts = [_text(0, orientation=45)]
footprints = {"C1": ComponentFootprint("C1", 0, 0, 1.0, 1.0)}
plans, _ = plan_assembly_refdes(texts, footprints)
# legacy: full span * 0.54 / 2 = half span * 0.54
assert plans[0].height == 0.54
def test_steep_angles_flip_orientation():
texts = [
_text(0, orientation=135, component_name="C1"),
_text(1, orientation=15, component_name="C2"),
]
footprints = {
"C1": ComponentFootprint("C1", 0, 0, 0.5, 0.5),
"C2": ComponentFootprint("C2", 0, 0, 0.5, 0.5),
}
plans, _ = plan_assembly_refdes(texts, footprints)
assert plans[0].rotation == 315 # 135 branch
assert plans[0].height == 0.27 # half span * 0.54
assert plans[1].rotation == 15 # else branch keeps ori
assert plans[1].height == 0.4 # generic branches force 0.4
def test_long_refdes_gets_narrow_font_and_fixed_height():
texts = [_text(0, text_string="R10000")]
footprints = {"C1": ComponentFootprint("C1", 0, 0, 0.5, 0.5)}
plans, _ = plan_assembly_refdes(texts, footprints)
assert plans[0].font == "Arial Narrow"
def test_render_plan_lines():
texts = [_text(0), _text(1, text_type=0)]
footprints = {"C1": ComponentFootprint("C1", 10.0, 20.0, 0.5, 0.3)}
plans, _ = plan_assembly_refdes(texts, footprints)
rendered = render_plan(plans)
assert rendered.startswith("MOVE|0|10.0000|20.0000|0.00|0.3000|Arial\n")
assert "DELETE|1\n" in rendered
+140
View File
@@ -0,0 +1,140 @@
from decimal import Decimal
from dx_productivity.domain.models import AttributeRecord
from dx_productivity.features.bom import BOMSettings, build_bom
def _component(component_factory, bridge_id, refdes, value, pn="P1", **kw):
return component_factory(
bridge_id=bridge_id,
stable_uid=f"uid-{bridge_id}",
full_path=f"ROOT\{bridge_id}",
logical_group_key=f"uid-{bridge_id}",
refdes=refdes,
symbol_refdes_default=refdes,
attributes=(
AttributeRecord(name="VALUE", value=value, visible=True),
AttributeRecord(name="Part Number", value=pn, visible=True),
),
**kw,
)
def test_same_page_same_part_merges(component_factory, snapshot_factory):
components = tuple(
_component(component_factory, f"c{i}", f"R{i}", "10K")
for i in range(1, 4)
)
snapshot = snapshot_factory(components=components)
result = build_bom(snapshot, BOMSettings())
assert len(result.rows) == 1
assert result.rows[0].qty == 3
assert result.rows[0].refdes == "R1,R2,R3"
assert result.rows[0].part_number == "P1"
def test_nc_filter_excludes_rows(component_factory, snapshot_factory):
nc = _component(component_factory, "c1", "R1", "NC CAP")
normal = _component(
component_factory, "c2", "R2", "10K", pn="P2"
)
snapshot = snapshot_factory(components=(nc, normal))
filtered = build_bom(snapshot, BOMSettings(exclude_nc=True))
unfiltered = build_bom(snapshot, BOMSettings(exclude_nc=False))
assert filtered.excluded_count == 1
assert len(filtered.rows) == 1
assert len(unfiltered.rows) == 2
def test_mark_rows_are_removed(component_factory, snapshot_factory):
marker = _component(
component_factory, "c1", "TP1", "TEST MARK", pn="MARK-1"
)
normal = _component(component_factory, "c2", "R2", "10K", pn="P2")
snapshot = snapshot_factory(components=(marker, normal))
result = build_bom(snapshot, BOMSettings())
assert [row.part_number for row in result.rows] == ["P2"]
def test_sections_are_grouped_by_page(component_factory, snapshot_factory):
from decimal import Decimal as D
p1 = _component(
component_factory, "c1", "R1", "10K", sheet_number=1, y=D(10)
)
p2 = _component(
component_factory, "c2", "R2", "10K", sheet_number=2, y=D(10)
)
snapshot = snapshot_factory(components=(p1, p2))
result = build_bom(snapshot, BOMSettings())
assert [page for page, _rows in result.sections] == [1, 2]
def test_duplicate_refdes_flagged(component_factory, snapshot_factory):
a = _component(component_factory, "c1", "R1", "10K", pn="P1")
b = _component(component_factory, "c2", "R1", "22K", pn="P2")
snapshot = snapshot_factory(components=(a, b))
result = build_bom(snapshot, BOMSettings())
flagged = [row for row in result.rows if row.duplicate_refdes]
assert len(flagged) == 2
def test_selected_scope_only_exports_selection(
component_factory, snapshot_factory
):
selected = _component(component_factory, "c1", "R1", "10K")
other = _component(
component_factory, "c2", "R2", "10K", selected=False
)
snapshot = snapshot_factory(components=(selected, other))
result = build_bom(snapshot, BOMSettings(scope="selected"))
assert len(result.rows) == 1
assert result.rows[0].refdes == "R1"
def test_part_label_description_source(component_factory, snapshot_factory):
component = component_factory(
bridge_id="c1",
attributes=(
AttributeRecord(name="VALUE", value="10K", visible=True),
AttributeRecord(name="Part Label", value="10K 1% RES", visible=True),
AttributeRecord(name="Part Number", value="410000010A20", visible=True),
),
)
snapshot = snapshot_factory(components=(component,))
result = build_bom(
snapshot, BOMSettings(description_source="part-label")
)
assert result.rows[0].description == "10K 1% RES"
def test_pvcheck_mismatch_flagged(component_factory, snapshot_factory):
component = component_factory(
bridge_id="c1",
attributes=(
AttributeRecord(name="VALUE", value="4.7R", visible=True),
AttributeRecord(
name="Part Number", value="410000010A20", visible=True
),
),
)
snapshot = snapshot_factory(components=(component,))
result = build_bom(snapshot, BOMSettings())
assert result.rows[0].part_number_mismatch is True
assert "1.02R" in result.rows[0].mismatch_reason
+147
View File
@@ -0,0 +1,147 @@
"""Table-driven PVCheck tests using the legacy EFM's own comment examples."""
import pytest
from dx_productivity.features.bom import pv_check
# (part_number, description, expected_description_contains)
RESISTOR_CASES = [
# VBScript comments: '10A2=1.02R 12A0=1.2R 10A0=1R
("410000010A20", "1.02R 1% RES", False),
("410000010A20", "1.0R 1% RES", True),
("410000012A00", "1.2R", False),
("410000010A00", "1R", False),
# '10D2=0.00102R 12D0=0.0012R 10D0=0.001R
("410000010D20", "0.00102R", False),
("410000012D00", "0.0012R", False),
("410000010D00", "0.001R", False),
# '1002=10.2R 1000=10R 0000=0R
("410000010020", "10.2R", False),
("410000010000", "10R", False),
# '1012=102R
("410000010120", "102R", False),
# '1022=1.02K 1220=1.2K 1020=1K
("410000010220", "1.02K", False),
("410000012200", "1.2K", False),
("410000010200", "1K", False),
# '1032=10.2K 1030=10K
("410000010320", "10.2K", False),
("410000010300", "10K", False),
# '1042=102K
("410000010420", "102K", False),
# '1052=1.02M 1250=1.2M 1050=1M
("410000010520", "1.02M", False),
("410000012500", "1.2M", False),
("410000010500", "1M", False),
# '1062=10.2M 1060=10M
("410000010620", "10.2M", False),
("410000010600", "10M", False),
]
CAPACITOR_CASES = [
# '12B=0.12P 10B=0.1P
("430000012B00", "0.12P", False),
("430000010B00", "0.1P", False),
# '12A=1.2P 10A=1P
("430000012A00", "1.2P", False),
("430000010A00", "1P", False),
# '100=10P 101=100P
("430000010000", "10P", False),
("430000010100", "100P", False),
# '122=1.2N 102=1N 103=10N 104=100N
("430000012200", "1.2N", False),
("430000010200", "1N", False),
("430000010300", "10N", False),
("430000010400", "100N", False),
# '475=4.7U 105=1U 106=10U 107=100U 108=1000U
("430000047500", "4.7U", False),
("430000010500", "1U", False),
("430000010600", "10U", False),
("430000010700", "100U", False),
("430000010800", "1000U", False),
]
COIL_CASES = [
# '47D=4.7N 10D=1N 12C=12N 12B=120N
("480000047D00", "4.7N", False),
("480000010D00", "1N", False),
("480000012C00", "12N", False),
("480000012B00", "120N", False),
# '47A=4.7U 10A=1U 100=10U 101=100U
("480000047A00", "4.7U", False),
("480000010A00", "1U", False),
("480000010000", "10U", False),
("480000010100", "100U", False),
# '122=1.2M 102=1M 103=10M 104=100M
("480000012200", "1.2M", False),
("480000010200", "1M", False),
("480000010300", "10M", False),
("480000010400", "100M", False),
]
@pytest.mark.parametrize(
"part_number,description",
[(pn, desc) for pn, desc, flag in RESISTOR_CASES if not flag],
)
def test_resistor_matching_descriptions_pass(part_number, description):
mismatch, _ = pv_check(part_number, description)
assert mismatch is False
@pytest.mark.parametrize("part_number,description,should_flag", RESISTOR_CASES)
def test_resistor_flag_expectation(part_number, description, should_flag):
mismatch, _ = pv_check(part_number, description)
assert mismatch is should_flag
@pytest.mark.parametrize("part_number,description", [(p, d) for p, d, _ in CAPACITOR_CASES])
def test_capacitor_matching_descriptions_pass(part_number, description):
mismatch, _ = pv_check(part_number, description)
assert mismatch is False
@pytest.mark.parametrize("part_number,description", [(p, d) for p, d, _ in COIL_CASES])
def test_coil_matching_descriptions_pass(part_number, description):
mismatch, _ = pv_check(part_number, description)
assert mismatch is False
def test_wrong_value_is_flagged():
mismatch, reason = pv_check("410000010A20", "4.7R 1%")
assert mismatch is True
assert "1.02R" in reason
def test_precision_mismatch_is_flagged():
# tail 2200 => 1% expected; description says %1 but not 1%
mismatch, reason = pv_check("410000010A2200", "1.02R %1")
assert mismatch is True
assert "精度" in reason
def test_precision_match_passes():
mismatch, _ = pv_check("410000010A2200", "1.02R 1%")
assert mismatch is False
def test_xx_part_numbers_are_skipped():
mismatch, _ = pv_check("41XX00010A20", "anything")
assert mismatch is False
def test_use_descriptions_are_skipped():
mismatch, _ = pv_check("410000010A20", "USE AS JUMPER")
assert mismatch is False
def test_unknown_prefix_passes():
mismatch, _ = pv_check("990000010A20", "whatever")
assert mismatch is False
def test_short_description_passes():
# legacy guard: Len(PartVal) > 1
mismatch, _ = pv_check("410000010A20", "R")
assert mismatch is False
+112
View File
@@ -0,0 +1,112 @@
from dx_productivity.domain.models import AttributeRecord, NetRecord
from dx_productivity.features.inspection import (
has_net_data,
missing_attribute_values,
net_cross_page,
net_list,
solitary_nets,
)
def _snapshot_factory(snapshot_factory, nets, components=()):
if not components and nets == ():
return snapshot_factory()
return snapshot_factory(components=components, nets=nets)
def test_solitary_nets_found(snapshot_factory, component_factory):
snapshot = snapshot_factory(
nets=(
NetRecord(name="N1", pages=(1,)),
NetRecord(name="N2", pages=(1, 2)),
)
)
rows = solitary_nets(snapshot)
assert len(rows) == 1
assert rows[0].name == "N1"
assert rows[0].locate_net is True
def test_net_list_covers_all(snapshot_factory):
snapshot = snapshot_factory(
nets=(
NetRecord(name="N1", pages=(1,)),
NetRecord(name="N2", pages=(1, 2)),
)
)
assert len(net_list(snapshot)) == 2
def test_cross_page_lookup(snapshot_factory):
snapshot = snapshot_factory(nets=(NetRecord(name="N2", pages=(1, 2)),))
rows = net_cross_page(snapshot, "n2")
assert [row.pages[0] for row in rows] == [1, 2]
assert net_cross_page(snapshot, "missing") == []
def test_value_miss_flags_empty_values_only_by_default(
component_factory, snapshot_factory
):
empty = component_factory(
bridge_id="c1",
attributes=(AttributeRecord(name="Value", value="", visible=True),),
)
missing = component_factory(
bridge_id="c2",
stable_uid="uid-c2",
full_path="ROOT\C2",
logical_group_key="uid-c2",
attributes=(),
)
ok = component_factory(
bridge_id="c3",
stable_uid="uid-c3",
full_path="ROOT\C3",
logical_group_key="uid-c3",
attributes=(AttributeRecord(name="Value", value="10K", visible=True),),
)
snapshot = snapshot_factory(components=(empty, missing, ok))
rows = missing_attribute_values(snapshot, "Value")
assert len(rows) == 1
assert rows[0].detail == "属性值为空"
assert rows[0].locate_bridge_id == "c1"
def test_value_miss_include_missing_option(component_factory, snapshot_factory):
missing = component_factory(
bridge_id="c2",
attributes=(),
)
snapshot = snapshot_factory(components=(missing,))
rows = missing_attribute_values(
snapshot, "Value", include_missing=True
)
assert len(rows) == 1
assert rows[0].detail == "属性缺失"
def test_unnamed_components_are_skipped(component_factory, snapshot_factory):
component = component_factory(
refdes="",
attributes=(AttributeRecord(name="Value", value="", visible=True),),
)
snapshot = snapshot_factory(components=(component,))
assert missing_attribute_values(snapshot, "Value") == []
def test_has_net_data(snapshot_factory):
empty = snapshot_factory()
populated = snapshot_factory(nets=(NetRecord(name="N1", pages=(1,)),))
assert has_net_data(empty) is False
assert has_net_data(populated) is True
+170
View File
@@ -0,0 +1,170 @@
from dx_productivity.features.property_change import (
PropertyChangeSettings,
build_property_change_plan,
)
from dx_productivity.domain.models import AttributeRecord, OpType, Severity
def _component_with_attributes(component_factory, attributes):
return component_factory(attributes=tuple(attributes))
def test_plan_rewrites_selected_components_with_attribute(
component_factory, snapshot_factory
):
component = _component_with_attributes(
component_factory,
(AttributeRecord(name="Value", value="10K", visible=True),),
)
snapshot = snapshot_factory(components=(component,))
plan = build_property_change_plan(
snapshot,
PropertyChangeSettings(
property_name="Value",
overrides=((component.bridge_id, "47K"),),
),
)
assert plan.can_apply
assert plan.rows[0].changed is True
assert plan.rows[0].new_refdes == "47K"
assert plan.schedule[0].op is OpType.PROPERTY_VALUE
assert plan.schedule[0].property_name == "Value"
assert plan.schedule[0].expected_refdes == "10K"
assert plan.schedule[0].new_refdes == "47K"
def test_missing_attribute_is_reported_not_silent(
component_factory, snapshot_factory
):
"""Legacy EFM silently skipped components without the attribute."""
component = _component_with_attributes(
component_factory, (AttributeRecord(name="Part Number", value="P1", visible=True),)
)
snapshot = snapshot_factory(components=(component,))
plan = build_property_change_plan(
snapshot,
PropertyChangeSettings(
property_name="Value",
overrides=((component.bridge_id, "47K"),),
),
)
assert plan.rows[0].new_refdes is None
assert any(issue.code == "skipped-missing" for issue in plan.issues)
def test_locked_components_are_skipped(component_factory, snapshot_factory):
component = _component_with_attributes(
component_factory,
(AttributeRecord(name="Value", value="10K", visible=True),),
locked=True,
) if False else component_factory(
attributes=(AttributeRecord(name="Value", value="10K", visible=True),),
locked=True,
)
snapshot = snapshot_factory(components=(component,))
plan = build_property_change_plan(
snapshot,
PropertyChangeSettings(
property_name="Value",
overrides=((component.bridge_id, "47K"),),
),
)
assert plan.rows[0].changed is False
assert any(issue.code == "skipped-locked" for issue in plan.issues)
def test_unselected_components_are_ignored(component_factory, snapshot_factory):
component = component_factory(
selected=False,
attributes=(AttributeRecord(name="Value", value="10K", visible=True),),
)
snapshot = snapshot_factory(components=(component,))
plan = build_property_change_plan(
snapshot,
PropertyChangeSettings(
property_name="Value",
overrides=((component.bridge_id, "47K"),),
),
)
assert plan.rows == ()
assert plan.schedule == ()
def test_duplicate_overrides_are_blocking(component_factory, snapshot_factory):
component = component_factory(
attributes=(AttributeRecord(name="Value", value="10K", visible=True),)
)
snapshot = snapshot_factory(components=(component,))
plan = build_property_change_plan(
snapshot,
PropertyChangeSettings(
property_name="Value",
overrides=(
(component.bridge_id, "47K"),
(component.bridge_id, "22K"),
),
),
)
assert any(
issue.code == "duplicate-override"
and issue.severity is Severity.BLOCKING
for issue in plan.issues
)
def test_clearing_value_is_a_change(component_factory, snapshot_factory):
component = component_factory(
attributes=(AttributeRecord(name="Value", value="10K", visible=True),)
)
snapshot = snapshot_factory(components=(component,))
plan = build_property_change_plan(
snapshot,
PropertyChangeSettings(
property_name="Value",
overrides=((component.bridge_id, ""),),
),
)
assert plan.can_apply
assert plan.rows[0].changed is True
assert plan.schedule[0].new_refdes == ""
def test_multiple_components_get_independent_schedule_rows(
component_factory, snapshot_factory
):
components = tuple(
component_factory(
bridge_id=f"c{i}",
stable_uid=f"uid-c{i}",
full_path=f"ROOT\C{i}",
logical_group_key=f"uid-c{i}",
x=__import__("decimal").Decimal(10 + i),
attributes=(AttributeRecord(name="Value", value="10K", visible=True),),
)
for i in range(1, 3)
)
snapshot = snapshot_factory(components=components)
plan = build_property_change_plan(
snapshot,
PropertyChangeSettings(
property_name="Value",
overrides=(("c1", "47K"), ("c2", "47K")),
),
)
assert plan.can_apply
assert len(plan.schedule) == 2
assert plan.schedule[0].bridge_id != plan.schedule[1].bridge_id
@@ -0,0 +1,96 @@
from decimal import Decimal
from dx_productivity.domain.models import AttributeRecord, OpType, Severity
from dx_productivity.features.property_visibility import (
PropertyVisibilitySettings,
build_property_visibility_plan,
)
def _component(component_factory, visible, **overrides):
return component_factory(
attributes=(
AttributeRecord(name="Part Name", value="RES", visible=visible),
),
**overrides,
)
def test_hide_selected_components(component_factory, snapshot_factory):
component = _component(component_factory, True)
snapshot = snapshot_factory(components=(component,))
plan = build_property_visibility_plan(
snapshot, PropertyVisibilitySettings(visible=False)
)
assert plan.can_apply
assert plan.rows[0].changed is True
assert plan.schedule[0].op is OpType.PROPERTY_VISIBILITY
assert plan.schedule[0].property_name == "Part Name"
assert plan.schedule[0].expected_refdes == "1"
assert plan.schedule[0].new_refdes == "0"
def test_show_restores_hidden_attribute(component_factory, snapshot_factory):
component = _component(component_factory, False)
snapshot = snapshot_factory(components=(component,))
plan = build_property_visibility_plan(
snapshot, PropertyVisibilitySettings(visible=True)
)
assert plan.can_apply
assert plan.schedule[0].new_refdes == "1"
def test_already_hidden_component_is_preserved(
component_factory, snapshot_factory
):
component = _component(component_factory, False)
snapshot = snapshot_factory(components=(component,))
plan = build_property_visibility_plan(
snapshot, PropertyVisibilitySettings(visible=False)
)
assert plan.rows[0].changed is False
assert plan.schedule == ()
assert not plan.can_apply
def test_missing_attribute_is_reported(component_factory, snapshot_factory):
component = component_factory(attributes=())
snapshot = snapshot_factory(components=(component,))
plan = build_property_visibility_plan(
snapshot, PropertyVisibilitySettings(visible=False)
)
assert any(issue.code == "skipped-missing" for issue in plan.issues)
def test_locked_components_are_skipped(component_factory, snapshot_factory):
component = _component(component_factory, True, locked=True)
snapshot = snapshot_factory(components=(component,))
plan = build_property_visibility_plan(
snapshot, PropertyVisibilitySettings(visible=False)
)
assert any(issue.code == "skipped-locked" for issue in plan.issues)
assert plan.rows[0].changed is False
def test_unselected_components_are_ignored(
component_factory, snapshot_factory
):
component = _component(component_factory, True, selected=False)
snapshot = snapshot_factory(components=(component,))
plan = build_property_visibility_plan(
snapshot, PropertyVisibilitySettings(visible=False)
)
assert plan.rows == ()
assert plan.schedule == ()