55using System . Threading . Tasks ;
66using System . Xml ;
77using System . IO ;
8+ using System . Reflection ;
89
910namespace kibom
1011{
@@ -18,6 +19,9 @@ class Program
1819 {
1920 static void Main ( string [ ] args )
2021 {
22+ Version v = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version ;
23+ Console . WriteLine ( string . Format ( System . Globalization . CultureInfo . InvariantCulture , @"Kibom {0}.{1} (build {2}.{3})" , v . Major , v . Minor , v . Build , v . Revision ) ) ;
24+
2125 string filename = "" ;
2226 string output_filename = "" ;
2327 string path = "" ;
@@ -37,6 +41,7 @@ static void Main(string[] args)
3741 XmlDocument doc = new XmlDocument ( ) ;
3842 doc . Load ( path + filename ) ;
3943 ParseKicadXML ( doc , path , filename , outputs , output_filename ) ;
44+ Console . WriteLine ( "BOM generated." ) ;
4045 }
4146
4247 static bool ParseArgs ( string [ ] args , out string filename , out string path , out string outputs , out string output_filename )
@@ -58,7 +63,7 @@ static bool ParseArgs(string[] args, out string filename, out string path, out s
5863 Console . WriteLine ( "File not found." ) ;
5964 return false ;
6065 }
61- path = Path . GetDirectoryName ( filename ) ;
66+ path = Path . GetDirectoryName ( Path . GetFullPath ( filename ) ) ;
6267 if ( ! path . EndsWith ( "\\ " ) )
6368 path += "\\ " ;
6469 filename = Path . GetFileName ( filename ) ;
@@ -114,9 +119,10 @@ static bool ParseKicadXML(XmlDocument doc, string path, string filename, string
114119 return false ;
115120
116121 // group components by designators and sort by value
117- List < DesignatorGroup > groups = BuildDesignatorGroups ( comp_list ) ;
118- SortDesignatorGroups ( ref groups ) ;
122+ List < DesignatorGroup > groups = Component . BuildDesignatorGroups ( comp_list ) ;
123+ Component . SortDesignatorGroups ( ref groups ) ;
119124 List < DesignatorGroup > merged_groups = Component . MergeComponents ( groups ) ;
125+ Component . SortComponents ( ref merged_groups ) ;
120126
121127 // sort groups alphabetically
122128 merged_groups . Sort ( ( a , b ) => a . designator . CompareTo ( b . designator ) ) ;
@@ -179,7 +185,13 @@ static List<Component> ParseComponents(XmlDocument doc)
179185 comp . value = node . SelectSingleNode ( "value" ) . InnerText ;
180186 comp . numeric_value = Component . ValueToNumeric ( comp . value ) ;
181187 comp . footprint = node . SelectSingleNode ( "footprint" ) . InnerText ;
188+
189+ // normalized footprint
182190 comp . footprint_normalized = Footprint . substitute ( comp . footprint , true , true ) ;
191+ if ( comp . footprint_normalized == "no part" )
192+ comp . no_part = true ;
193+ if ( comp . footprint . Contains ( ':' ) ) // contrains library name
194+ comp . footprint = comp . footprint . Substring ( comp . footprint . IndexOf ( ':' ) + 1 ) ;
183195
184196 // custom BOM fields
185197 XmlNode fields = node . SelectSingleNode ( "fields" ) ;
@@ -191,6 +203,7 @@ static List<Component> ParseComponents(XmlDocument doc)
191203 switch ( field . Attributes [ "name" ] . Value . ToLower ( ) )
192204 {
193205 case "bom_footprint" :
206+ //case "bom_partno":
194207 comp . footprint_normalized = field . InnerText ;
195208 break ;
196209
@@ -209,57 +222,22 @@ static List<Component> ParseComponents(XmlDocument doc)
209222 case "code" :
210223 comp . code = field . InnerText ;
211224 break ;
225+
226+ case "bom_no_part" :
227+ if ( field . InnerText . ToLower ( ) == "true" )
228+ comp . no_part = true ;
229+ break ;
212230 }
213231 }
214232 }
215-
216- if ( ! comp . footprint . Contains ( "no part" ) ) // ignore pad only parts
233+
234+ if ( ! comp . no_part )
217235 comp_list . Add ( comp ) ;
218236
219237 //Console.WriteLine(comp.reference + "\t" + comp.value + "\t" + comp.footprint_normalized);
220238 }
221239
222240 return comp_list ;
223241 }
224-
225- static List < DesignatorGroup > BuildDesignatorGroups ( List < Component > comp_list )
226- {
227- var groups = new List < DesignatorGroup > ( ) ;
228-
229- foreach ( Component comp in comp_list )
230- {
231- bool found = false ;
232- for ( int i = 0 ; i < groups . Count ; i ++ )
233- {
234- if ( groups [ i ] . designator == comp . designator )
235- {
236- groups [ i ] . comp_list . Add ( comp ) ;
237- found = true ;
238- break ;
239- }
240- }
241- if ( ! found )
242- {
243- var new_group = new DesignatorGroup ( ) ;
244- new_group . designator = comp . designator ;
245- new_group . comp_list = new List < Component > ( ) ;
246- new_group . comp_list . Add ( comp ) ;
247- groups . Add ( new_group ) ;
248- }
249- }
250-
251- return groups ;
252- }
253-
254- static void SortDesignatorGroups ( ref List < DesignatorGroup > groups )
255- {
256- foreach ( DesignatorGroup g in groups )
257- {
258- // sort by value
259- //g.comp_list.Sort((a, b) => a.value.CompareTo(b.value));
260- g . comp_list . Sort ( ( a , b ) => a . numeric_value . CompareTo ( b . numeric_value ) ) ;
261- }
262- }
263-
264242 }
265243}
0 commit comments