fix(assembly): part number suffix incorrectly incremented when inserting duplicate parts #327
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
When parts with structured part numbers (e.g.,
P03-0001) are inserted into an assembly multiple times, the auto-deduplication logic increments the trailing numeric portion of the part number itself, producingP03-0002,P03-0003, etc. This corrupts the part number — the user expects instance suffixes likeP03-0001-1,P03-0001-2.Current Behavior
P03-0001→ label isP03-0001P03-0001again → label becomesP03-0002(wrong)P03-0001again → label becomesP03-0003(wrong)Expected Behavior
P03-0001→ label isP03-0001-1P03-0001again → label becomesP03-0001-2P03-0001again → label becomesP03-0001-3Or alternatively, the first instance keeps its original label and only subsequent duplicates get the
-Nsuffix.Root Cause
The
UniqueNameManager::decomposeName()method (src/Base/UniqueNameManager.cpp:33-48) scans backwards from the end of the name for trailing digits, then treats them as the "unique suffix" to increment:When
Document::makeUniqueLabel()(src/App/Document.cpp:1962) callsmakeUniqueName("P03-0001", 3), the manager sees that"0001"is already registered under base name"P03-", so it increments to"0002"→"P03-0002".This is correct behavior for FreeCAD's default naming convention (e.g.,
Body→Body001→Body002) but is wrong for structured part numbers where the digits are semantically meaningful, not auto-generated instance counters.Proposed Approach
For assembly contexts (and possibly globally), when a duplicate label is detected, append an instance suffix (e.g.,
-1,-2) rather than incrementing the trailing digits. Options:Assembly-specific override: In the assembly module, override the label deduplication to append
-Ninstead of usingUniqueNameManager::makeUniqueName(). TheAssemblyLinkclass already hasallowDuplicateLabel()returningtrue(src/Mod/Assembly/App/AssemblyLink.cpp:758-761) — this could be extended with a custom deduplication strategy.UniqueNameManager subclass: Create a derived
UniqueNameManagerthat overridesgetNameSuffixStartPosition()to treat the entire name (including trailing digits) as a fixed base, then appends a-Nseparator + instance number. This preserves the existing behavior for default FreeCAD objects while enabling part-number-aware behavior for assemblies.Separator-aware decomposition: Teach
decomposeName()to recognize that digits preceded by-in a structured name (e.g.,P03-0001) are part of the base name, not a uniquifying suffix. This is more invasive but would fix the problem globally.Files Involved
src/Base/UniqueNameManager.h— base class with virtualgetNameSuffixStartPosition()src/Base/UniqueNameManager.cpp—decomposeName()andmakeUniqueName()logicsrc/App/Document.cpp:1962—makeUniqueLabel()callsmakeUniqueName(modelLabel, 3)src/Mod/Assembly/App/AssemblyLink.cpp—allowDuplicateLabel()already returnstruetests/src/Base/UniqueNameManager.cpp— unit tests to update/extend