diff --git a/CHANGELOG.md b/CHANGELOG.md
index b4f36146..fc005691 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.6.3] - UNRELEASED
+### Changed
+- KiCad v6/7 schematic:
+ - The hierarchy is expanded only if needed, i.e. value of an instance changed
+
### Fixed
- KiCad v6/7 schematic:
- Net Class Flags not saved in variants or annotated schematics
diff --git a/kibot/kicad/v6_sch.py b/kibot/kicad/v6_sch.py
index 0ced1166..33f8b7c2 100644
--- a/kibot/kicad/v6_sch.py
+++ b/kibot/kicad/v6_sch.py
@@ -1974,7 +1974,60 @@ class SchematicV6(Schematic):
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
+ # Collect component instances (UUID -> list of instances)
+ instances = {}
+ for s in self.symbol_instances:
+ c_uuid = os.path.basename(s.path)
+ instances.setdefault(c_uuid, []).append(s.component)
+ # Look for variants in the instances
+ for _, l_ins in instances.items():
+ if len(l_ins) < 2:
+ # 0 or 1 can't be different
+ continue
+ ref_c = l_ins[0]
+ if any(map(lambda c: self.compare_component(ref_c, c), l_ins[1:])):
+ return True
+ # No variant
+ return False
+
+ @staticmethod
+ def compare_component(r, c):
+ """ Returns True if the components need separated instances (are different) """
+ if len(r.dfields) != len(c.dfields):
+ SchematicV6.log_difference(r, c, 'list of field names {} vs {}'.format(len(r.dfields), len(c.dfields)))
+ return True
+ for name, field in r.dfields.items():
+ if name == 'reference':
+ continue
+ field2 = c.dfields.get(name)
+ if field2 is None:
+ SchematicV6.log_difference(r, c, 'field `{}` not in the second'.format(name))
+ if field.value != field2.value:
+ SchematicV6.log_difference(r, c, '`{}` fields (`{}` != `{}`)'.format(name, field.value, field2.value))
+ return True
+ if c.fitted != r.fitted:
+ SchematicV6.log_difference(r, c, 'fitted status')
+ return True
+ if c.included != r.included:
+ SchematicV6.log_difference(r, c, 'included status')
+ return True
+ if c.fixed != r.fixed:
+ SchematicV6.log_difference(r, c, 'fixed status')
+ return True
+ if c.fixed != r.fixed:
+ SchematicV6.log_difference(r, c, 'fixed status')
+ return True
+ if c.in_bom != r.in_bom:
+ SchematicV6.log_difference(r, c, 'in_bom status')
+ return True
+ if c.on_board != r.on_board:
+ SchematicV6.log_difference(r, c, 'on_board status')
+ return True
+ return False
+
+ @staticmethod
+ def log_difference(r, c, msg):
+ logger.debug('Expanding hierarchy because {} and {} has different {}'.format(r.ref, c.ref, msg))
def _create_flat_name(self, sch):
""" Create a unique name that doesn't contain subdirs.
diff --git a/tests/reference/6_0_8/test_v5-schematic_(no_L).pdf b/tests/reference/6_0_8/test_v5-schematic_(no_L).pdf
index 9c9b4c12..796bf3a6 100644
Binary files a/tests/reference/6_0_8/test_v5-schematic_(no_L).pdf and b/tests/reference/6_0_8/test_v5-schematic_(no_L).pdf differ
diff --git a/tests/reference/6_0_8/test_v5-schematic_(no_L).svg b/tests/reference/6_0_8/test_v5-schematic_(no_L).svg
index 71d7518a..3b241c12 100644
--- a/tests/reference/6_0_8/test_v5-schematic_(no_L).svg
+++ b/tests/reference/6_0_8/test_v5-schematic_(no_L).svg
@@ -7,7 +7,7 @@
xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.1"
width="29.700220cm" height="21.000720cm" viewBox="0 0 116930 82680">
-
SVG Picture created as test_v5.svg date 2022/10/14 10:37:51
+SVG Picture created as test_v5.svg date 2023/04/28 13:38:14
Picture generated by Eeschema-SVG
KiCad E.D.A. eeschema 6.0.8+dfsg-1~bpo11+1
-KiCad E.D.A. eeschema 6.0.8+dfsg-1~bpo11+1
+textLength="22388.464567" font-size="787.401575" lengthAdjust="spacingAndGlyphs"
+text-anchor="start" opacity="0">KiCad E.D.A. eeschema 6.0.11+dfsg-1~bpo11+1
+KiCad E.D.A. eeschema 6.0.11+dfsg-1~bpo11+1
@@ -962,185 +962,161 @@ L82792 77367
L82820 77339
L82820 77395
" />
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
+
+
-File: sub-sheet_1.kicad_sch
-File: sub-sheet_1.kicad_sch
+File: sub-sheet.kicad_sch
+File: sub-sheet.kicad_sch
@@ -4146,134 +4131,122 @@ L45793 35835
L45840 35858
L45888 35858
" />
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-File: sub-sheet_2.kicad_sch
-File: sub-sheet_2.kicad_sch
+File: sub-sheet.kicad_sch
+File: sub-sheet.kicad_sch
@@ -4669,137 +4642,122 @@ L45793 43835
L45840 43858
L45888 43858
" />
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
diff --git a/tests/reference/6_0_8/test_v5_wks-schematic_(no_L).pdf b/tests/reference/6_0_8/test_v5_wks-schematic_(no_L).pdf
index d464cdd7..c07dbe16 100644
Binary files a/tests/reference/6_0_8/test_v5_wks-schematic_(no_L).pdf and b/tests/reference/6_0_8/test_v5_wks-schematic_(no_L).pdf differ
diff --git a/tests/reference/7_0_0/test_v5-schematic_(no_L).pdf b/tests/reference/7_0_0/test_v5-schematic_(no_L).pdf
index 292b813b..778c1c54 100644
Binary files a/tests/reference/7_0_0/test_v5-schematic_(no_L).pdf and b/tests/reference/7_0_0/test_v5-schematic_(no_L).pdf differ
diff --git a/tests/reference/7_0_0/test_v5-schematic_(no_L).svg b/tests/reference/7_0_0/test_v5-schematic_(no_L).svg
index 204170e0..0ef520b3 100644
--- a/tests/reference/7_0_0/test_v5-schematic_(no_L).svg
+++ b/tests/reference/7_0_0/test_v5-schematic_(no_L).svg
@@ -7,7 +7,7 @@
xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.1"
width="297.0022mm" height="210.0072mm" viewBox="0.0000 0.0000 297.0022 210.0072">
-SVG Image created as test_v5.svg date 2023/03/08 13:54:19
+SVG Image created as test_v5.svg date 2023/04/28 10:37:15
Image generated by Eeschema-SVG
KiCad E.D.A. kicad-cli 7.0.0-262-g117d431f25-dirty-fork+411c9707ee+deb11
-KiCad E.D.A. kicad-cli 7.0.0-262-g117d431f25-dirty-fork+411c9707ee+deb11
+textLength="73.5097" font-size="2.0000" lengthAdjust="spacingAndGlyphs"
+text-anchor="start" opacity="0">KiCad E.D.A. kicad-cli 7.0.2.1-36-g582732918d-dirty-deb11
+KiCad E.D.A. kicad-cli 7.0.2.1-36-g582732918d-dirty-deb11
@@ -1959,1091 +1959,917 @@ L209.7879 196.4582
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
File: sub-sheet_1.kicad_sch
-File: sub-sheet_1.kicad_sch
+textLength="24.9827" font-size="1.6933" lengthAdjust="spacingAndGlyphs"
+text-anchor="start" opacity="0">File: sub-sheet.kicad_sch
+File: sub-sheet.kicad_sch
@@ -8228,278 +8054,260 @@ L116.3562 90.8150
-
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
File: sub-sheet_2.kicad_sch
-File: sub-sheet_2.kicad_sch
+textLength="24.9827" font-size="1.6933" lengthAdjust="spacingAndGlyphs"
+text-anchor="start" opacity="0">File: sub-sheet.kicad_sch
+File: sub-sheet.kicad_sch
@@ -9310,293 +9118,260 @@ L116.3562 111.1350
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/tests/reference/7_0_0/test_v5_wks-schematic_(no_L).pdf b/tests/reference/7_0_0/test_v5_wks-schematic_(no_L).pdf
index 0b791ad2..dfa92270 100644
Binary files a/tests/reference/7_0_0/test_v5_wks-schematic_(no_L).pdf and b/tests/reference/7_0_0/test_v5_wks-schematic_(no_L).pdf differ
diff --git a/tests/reference/8_0_0/test_v5-schematic_(no_L).pdf b/tests/reference/8_0_0/test_v5-schematic_(no_L).pdf
index 5a569ebf..b74b2258 100644
Binary files a/tests/reference/8_0_0/test_v5-schematic_(no_L).pdf and b/tests/reference/8_0_0/test_v5-schematic_(no_L).pdf differ
diff --git a/tests/reference/8_0_0/test_v5_wks-schematic_(no_L).pdf b/tests/reference/8_0_0/test_v5_wks-schematic_(no_L).pdf
index 4f4a0dae..66c9d6da 100644
Binary files a/tests/reference/8_0_0/test_v5_wks-schematic_(no_L).pdf and b/tests/reference/8_0_0/test_v5_wks-schematic_(no_L).pdf differ