Script to adjust DNP by variant

Hello,
I needed a script that sets the DNP values of the components in the schematic based on the correlation of the property field in the components (symbols) and the specified vatiant string. I considered using KiVAR GitHub - markh-de/KiVar: KiCad Action Plugin providing PCB assembly variant selection which is strong in variant evaluation. I’m using KiCAD V7 which doesn’t have a DNP field in PCBnew and I want to do a check in the schematic. So I decided to create a simple script that uses the kiutils library to load the hierarchy schema. It then changes the DNP values and saves the schematic to a new folder for inspection and BOM generation.

Script is here

KiBOM does variants . . . did you look at it ?

I briefly looked at its description, but it seemed to me that it does not allow visualization in the schematic.

1 Like

Hello,
I want to add the ability to create a BOM directly with this script. I thought it would be relatively simple.
I have now encountered a problem with determining the correct “designator” in KiCAD terminology, it is a reference (for example R19). I found that I have multiple designators and some are missing. This can be seen by simply searching the kicad_sch file in a text editor. I don’t know exactly how this situation occurs, it is probably a combination of copy and paste component and reannotation. So far, I’ve been able to demonstrate this in a simple example with one subscheet.
The fact is that KiCAD itself has no problem with this.

My question is how does KiCAD find out what the correct designator value is?

Here is a simple example. Schema with Root sheet and one subsheet. There are no more than a few resistors, GNDs and power flags. The file “subsheet1.kicad_sch” is interesting
All resistors have a symbol in the header
(property “Reference” “R19” …)
these resistors differ in the array of instances
for example
(instances
(project “test”
(path “/4ac61623-f679-4d8f-8a28-b049be752388”
(reference “R19”) (unit 1)
)
(path “/4ac61623-f679-4d8f-8a28-b049be752388/50bb2962-c518-410f-b4a2-b19642b96c89”
(reference “R12”) (unit 1)
)
)
)
another has
(instances
(project “test”
(path “/4ac61623-f679-4d8f-8a28-b049be752388”
(reference “R19”) (unit 1)
)
(path “/4ac61623-f679-4d8f-8a28-b049be752388/50bb2962-c518-410f-b4a2-b19642b96c89”
(reference “R5”) (unit 1)
)
)
)
test.kicad_sch (9.8 KB)
subsheet1.kicad_sch (17.4 KB)

Hello,
The solution to the challenging problem of determining the correct designator appears to lie in those ugly hashes. The issue arose when I examined the subsheet in isolation, attempting to identify a solution there. It turns out it’s not feasible to resolve the annotation within the sheet itself, except at the top level.

Therefore, one must begin from the TOP sheet. As additional sheets are incorporated and the level of immersion deepens, the ugly hash grows in size, resembling the following pattern: /top-hash/sub-sheet-hash/… and so forth. The correct value of the designator, then, is the value of that reference in “instances” section where the ugly hash of the reference matches the ugly hash accumulated on the path from the TOP sheet. Essentially, that encapsulates the whole foundational idea.

I’ve incorporated the solution into the script in the repository, along with some extra enhancements. It appears to be functioning within my project. However, I acknowledge there might be various combinations, with or without ugly hashes, older format drives, etc., where it may not operate correctly.

I would welcome comments from someone with a understanding of this matter—though there’s no immediate rush now that it’s operational for me.