Skip to content

Commit c042b21

Browse files
author
SebastianRumohr
committed
fix: Handle missing properties in .kicad_sym file like ki_keywords or ki_description
1 parent b14c791 commit c042b21

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

release-notes/3.0.0.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ Breaking changes:
66
Features:
77

88
- Spaces are converted to underscores when a custom field is added.
9-
-

release-notes/3.0.1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Bug fixes:
2+
- Handle missing properties in `.kicad_sym` file like `ki_keywords` or `ki_description`

src/KiCadDbLib/Services/KiCad/LibraryWriter/KiCad6LibraryWriter.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ public async Task WritePartAsync(Part part)
8080
SetSymbolId(symbol, part.Value);
8181
UpdateUnitIds(symbol, symbolInfo.Name, part.Value);
8282

83-
SetPropertyByName(symbol, "Reference", part.Reference);
84-
SetPropertyByName(symbol, "Value", part.Value);
85-
SetPropertyByName(symbol, "Footprint", part.Footprint);
86-
SetPropertyByName(symbol, "Datasheet", string.IsNullOrEmpty(part.Datasheet) ? "~" : part.Datasheet);
87-
SetPropertyByName(symbol, "ki_keywords", part.Keywords);
88-
SetPropertyByName(symbol, "ki_description", part.Description);
83+
SetOrAddPropertyByName(symbol, "Reference", part.Reference);
84+
SetOrAddPropertyByName(symbol, "Value", part.Value);
85+
SetOrAddPropertyByName(symbol, "Footprint", part.Footprint);
86+
SetOrAddPropertyByName(symbol, "Datasheet", string.IsNullOrEmpty(part.Datasheet) ? "~" : part.Datasheet);
87+
SetOrAddPropertyByName(symbol, "ki_keywords", part.Keywords);
88+
SetOrAddPropertyByName(symbol, "ki_description", part.Description);
8989

9090
foreach (var customField in part.CustomFields.OrderBy(cf => cf.Key))
9191
{
@@ -113,20 +113,23 @@ private static void UpdateUnitIds(SNode symbol, string symbolName, string partVa
113113
}
114114
}
115115

116-
private static void SetPropertyByName(SNode symbol, string propertyName, string value)
116+
private static void SetOrAddPropertyByName(SNode symbol, string propertyName, string value)
117117
{
118118
var property = symbol.Childs
119119
.Where(child => child.Name == _property)
120120
.SingleOrDefault(property => property.Childs[0].Name == propertyName);
121121

122-
if (property is null)
122+
if (property is not null)
123123
{
124-
var symbolName = symbol.Childs[0].Name;
125-
throw new KeyNotFoundException($"Property \"{propertyName}\" not found in symbol\"{symbolName}\".");
124+
property.Childs[1].Name = value;
125+
property.Childs[1].IsString = true;
126+
}
127+
else
128+
{
129+
var index = GetIndexOfLastProperty(symbol) + 1;
130+
property = CreateCustomProperty(null, KeyValuePair.Create(propertyName, value));
131+
symbol.Insert(index, property);
126132
}
127-
128-
property.Childs[1].Name = value;
129-
property.Childs[1].IsString = true;
130133
}
131134

132135
private static void AddCustomProperty(SNode symbol, KeyValuePair<string, string> property)
@@ -176,7 +179,7 @@ private static SNode CreateCustomProperty(int? propertyId, KeyValuePair<string,
176179
}
177180

178181
return symbol.Childs
179-
.Where(child => child.Name == "property")
182+
.Where(child => child.Name == _property)
180183
.Select(GetIdOfProperty)
181184
.Last();
182185
}

0 commit comments

Comments
 (0)