[v6/v7 Schematic] Better handling of sub-sheet instances

- When a sub-sheet is the root of another hierarchy.
- We no longer discard the instances, unless expanding the hierarchy
- Also another step to separate the cross and hierarchy expansion
This commit is contained in:
Salvador E. Tropea 2023-04-28 09:22:15 -03:00
parent 42445e8294
commit 4992937cf5
3 changed files with 51 additions and 19 deletions

View File

@ -1132,8 +1132,8 @@ class SchematicComponentV6(SchematicComponent):
v6_ins.path = os.path.join('/', '/'.join(ins.path.split('/')[2:]), comp.uuid_ori)
v6_ins.reference = ins.reference
v6_ins.unit = ins.unit
# Add to the root symbol_instances, so we reconstruct it
parent.symbol_instances.append(v6_ins)
# Add to the root symbol_instances, so we reconstruct it (like in a v6 file)
parent.root_sheet.symbol_instances.append(v6_ins)
else:
raise SchError('Unknown component attribute `{}`'.format(i))
if not lib_id_found or not at_found:
@ -1848,12 +1848,10 @@ class SchematicV6(Schematic):
data.extend([s.write(cross), Sep()])
return [Sep(), Sep(), _symbol('lib_symbols', data), Sep()]
def save(self, fname=None, dest_dir=None, base_sheet=None, saved=None):
def save(self, fname=None, dest_dir=None, base_sheet=None, saved=None, cross=False, exp_hierarchy=False):
# Switch to the current version
global version
version = self.version
cross = dest_dir is not None
# cross = False
if base_sheet is None:
# We are the base sheet
base_sheet = self
@ -1928,21 +1926,22 @@ class SchematicV6(Schematic):
# Net Class Flags
_add_items(self.net_class_flags, sch)
# Symbols
_add_items(self.symbols, sch, sep=True, cross=cross, exp_hierarchy=cross)
_add_items(self.symbols, sch, sep=True, cross=cross, exp_hierarchy=exp_hierarchy)
# Sheets
_add_items(self.sheets, sch, sep=True, exp_hierarchy=cross)
_add_items(self.sheets, sch, sep=True, exp_hierarchy=exp_hierarchy)
# Sheet instances
instances = self.sheet_instances
if cross:
# We are saving a expanded hierarchy, so we must fix the instances
if exp_hierarchy and base_sheet == self:
# We are saving a expanded hierarchy, so we must fix the root page instances
instances = deepcopy(self.sheet_instances)
for ins in instances:
ins.path = self.sheet_paths[ins.path].sheet_path
_add_items_list('sheet_instances', instances, sch)
if base_sheet == self or not exp_hierarchy:
_add_items_list('sheet_instances', instances, sch)
# Symbol instances
if version < KICAD_7_VER and base_sheet == self:
if version < KICAD_7_VER:
instances = self.symbol_instances
if cross:
if exp_hierarchy and base_sheet == self:
# We are saving a expanded hierarchy, so we must fix the instances
instances = deepcopy(self.symbol_instances)
for s in instances:
@ -1951,7 +1950,8 @@ class SchematicV6(Schematic):
# UUID changed to make it different
# s.path = os.path.join(os.path.dirname(s.path), c.uuid)
s.path = os.path.join(c.path, c.uuid)
_add_items_list('symbol_instances', instances, sch)
if base_sheet == self or not exp_hierarchy:
_add_items_list('symbol_instances', instances, sch)
logger.debug('Saving schematic: `{}`'.format(fname))
# Keep a back-up of existing files
if os.path.isfile(fname):
@ -1963,13 +1963,19 @@ class SchematicV6(Schematic):
saved.add(fname)
for sch in self.sheets:
if sch.sch:
sch.sch.save(sch.flat_file if cross else sch.file, dest_dir, base_sheet, saved)
sch.sch.save(sch.flat_file if exp_hierarchy else sch.file, dest_dir, base_sheet, saved, cross=cross,
exp_hierarchy=exp_hierarchy)
def save_variant(self, dest_dir):
fname = os.path.basename(self.fname)
self.save(fname, dest_dir)
self.save(fname, dest_dir, cross=True, exp_hierarchy=self.check_exp_hierarchy())
return fname
def check_exp_hierarchy(self):
""" Check if we really need to expand the hierarchy """
# KiCad v6: we can change the value and footprint, but won't work when imported by v7
return True
def _create_flat_name(self, sch):
""" Create a unique name that doesn't contain subdirs.
Is used to save a variant, where we avoid sharing instance data """
@ -2028,7 +2034,6 @@ class SchematicV6(Schematic):
self.sheet_names = {}
self.all_sheets = []
self.root_sheet = self
self.symbol_instances = []
UUID_Validator.reset()
else:
self.fields = parent.fields
@ -2039,7 +2044,7 @@ class SchematicV6(Schematic):
self.sheet_names = parent.sheet_names
self.all_sheets = parent.all_sheets
self.root_sheet = parent.root_sheet
self.symbol_instances = parent.symbol_instances
self.symbol_instances = []
self.parent = parent
self.fname = fname
self.project = project

View File

@ -190,7 +190,7 @@
(symbol (lib_id "Device:C") (at 115.57 72.39 0) (unit 1)
(in_bom yes) (on_board yes) (fields_autoplaced)
(uuid 3316c32a-0fe0-447f-ad05-e8a774318855)
(property "Reference" "C1" (id 0) (at 119.38 71.1199 0)
(property "Reference" "C10" (id 0) (at 119.38 71.1199 0)
(effects (font (size 1.27 1.27)) (justify left))
)
(property "Value" "150p" (id 1) (at 119.38 73.6599 0)
@ -209,7 +209,7 @@
(symbol (lib_id "Device:R") (at 100.33 63.5 90) (unit 1)
(in_bom yes) (on_board yes)
(uuid 6e279b82-cec2-44a1-9e20-451a25fb9868)
(property "Reference" "R1" (id 0) (at 100.33 58.42 90))
(property "Reference" "R10" (id 0) (at 100.33 58.42 90))
(property "Value" "100k" (id 1) (at 100.33 60.96 90))
(property "Footprint" "" (id 2) (at 100.33 65.278 90)
(effects (font (size 1.27 1.27)) hide)

View File

@ -47,4 +47,31 @@
(uuid a3235e0d-ce4e-481b-a10d-8cd5307d4186)
)
)
(sheet_instances
(path "/" (page "1"))
(path "/1a520d7d-3ba5-4c74-a100-97625e355b42" (page "2"))
(path "/ceaaef34-04ea-407a-8f1a-90d1b353a084" (page "3"))
)
(symbol_instances
(path "/1a520d7d-3ba5-4c74-a100-97625e355b42/2008b722-6f66-498d-8afb-79e172fe202d"
(reference "#PWR?") (unit 1) (value "GND") (footprint "")
)
(path "/ceaaef34-04ea-407a-8f1a-90d1b353a084/2008b722-6f66-498d-8afb-79e172fe202d"
(reference "#PWR?") (unit 1) (value "GND") (footprint "")
)
(path "/1a520d7d-3ba5-4c74-a100-97625e355b42/3316c32a-0fe0-447f-ad05-e8a774318855"
(reference "C10") (unit 1) (value "150p") (footprint "")
)
(path "/ceaaef34-04ea-407a-8f1a-90d1b353a084/3316c32a-0fe0-447f-ad05-e8a774318855"
(reference "C11") (unit 1) (value "150p") (footprint "")
)
(path "/1a520d7d-3ba5-4c74-a100-97625e355b42/6e279b82-cec2-44a1-9e20-451a25fb9868"
(reference "R10") (unit 1) (value "100k") (footprint "")
)
(path "/ceaaef34-04ea-407a-8f1a-90d1b353a084/6e279b82-cec2-44a1-9e20-451a25fb9868"
(reference "R11") (unit 1) (value "100k") (footprint "")
)
)
)