|
12 | 12 | using System.Drawing; |
13 | 13 | using OfficeOpenXml; |
14 | 14 | using OfficeOpenXml.Style; |
| 15 | +using Keio.Utils; |
15 | 16 |
|
16 | 17 | namespace kibom |
17 | 18 | { |
@@ -260,6 +261,153 @@ public static void OutputTSV(List<DesignatorGroup> groups, HeaderBlock header, s |
260 | 261 | } |
261 | 262 | } |
262 | 263 |
|
| 264 | + private static void PrettyMeasureWidths(List<DesignatorGroup> groups, |
| 265 | + out int count_width, |
| 266 | + out int reference_width, |
| 267 | + out int value_width, |
| 268 | + out int footprint_width, |
| 269 | + out int precision_width) |
| 270 | + { |
| 271 | + count_width = "No.".Length; |
| 272 | + reference_width = "Reference".Length; |
| 273 | + value_width = "Value".Length; |
| 274 | + footprint_width = "Footprint".Length; |
| 275 | + precision_width = "Precision".Length; |
| 276 | + |
| 277 | + foreach (DesignatorGroup g in groups) |
| 278 | + { |
| 279 | + // check for groups that are entire "no part" |
| 280 | + bool all_no_part = true; |
| 281 | + foreach (Component c in g.comp_list) |
| 282 | + { |
| 283 | + if (!c.no_part) |
| 284 | + all_no_part = false; |
| 285 | + } |
| 286 | + if (all_no_part) |
| 287 | + continue; |
| 288 | + |
| 289 | + foreach (Component c in g.comp_list) |
| 290 | + { |
| 291 | + count_width = Math.Max(count_width, (c.count + 1).ToString().Length); |
| 292 | + reference_width = Math.Max(reference_width, c.reference.Length); |
| 293 | + value_width = Math.Max(value_width, c.value.Length); |
| 294 | + if (!string.IsNullOrEmpty(c.precision)) |
| 295 | + precision_width = Math.Max(precision_width, c.precision.Length); |
| 296 | + |
| 297 | + string footprint = c.footprint_normalized; |
| 298 | + if (footprint == "") |
| 299 | + footprint = c.footprint; |
| 300 | + footprint_width = Math.Max(footprint_width, footprint.Length); |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + count_width = Math.Min(count_width, 10); |
| 305 | + reference_width = Math.Min(reference_width, 20); |
| 306 | + value_width = Math.Min(value_width, 30); |
| 307 | + footprint_width = Math.Min(footprint_width, 30); |
| 308 | + precision_width = Math.Min(precision_width, 20); |
| 309 | + } |
| 310 | + |
| 311 | + public static void OutputPretty(List<DesignatorGroup> groups, HeaderBlock header, string file) |
| 312 | + { |
| 313 | + Console.WriteLine("Generating " + file + "..."); |
| 314 | + using (StreamWriter sw = new StreamWriter(file)) |
| 315 | + { |
| 316 | + int count_width; |
| 317 | + int reference_width; |
| 318 | + int value_width; |
| 319 | + int footprint_width; |
| 320 | + int precision_width; |
| 321 | + PrettyMeasureWidths(groups, out count_width, out reference_width, out value_width, out footprint_width, out precision_width); |
| 322 | + |
| 323 | + sw.WriteLine("Title: " + header.title); |
| 324 | + sw.WriteLine("Date: " + header.date); |
| 325 | + sw.WriteLine("Source: " + header.source); |
| 326 | + sw.WriteLine("Revsision: " + header.revision); |
| 327 | + if (header.company != "") |
| 328 | + sw.WriteLine("Company: " + header.company); |
| 329 | + sw.WriteLine(""); |
| 330 | + |
| 331 | + //sw.WriteLine("No. Designation Value Footprint Precision"); |
| 332 | + sw.Write(TextUtils.FixedLengthString("No.", ' ', count_width) + " "); |
| 333 | + sw.Write(TextUtils.FixedLengthString("Reference", ' ', reference_width) + " "); |
| 334 | + sw.Write(TextUtils.FixedLengthString("Value", ' ', value_width) + " "); |
| 335 | + sw.Write(TextUtils.FixedLengthString("Footprint", ' ', footprint_width) + " "); |
| 336 | + sw.WriteLine("Precision"); |
| 337 | + sw.WriteLine(""); |
| 338 | + |
| 339 | + foreach (DesignatorGroup g in groups) |
| 340 | + { |
| 341 | + // check for groups that are entire "no part" |
| 342 | + bool all_no_part = true; |
| 343 | + foreach (Component c in g.comp_list) |
| 344 | + { |
| 345 | + if (!c.no_part) |
| 346 | + all_no_part = false; |
| 347 | + } |
| 348 | + if (all_no_part) |
| 349 | + continue; |
| 350 | + |
| 351 | + // header |
| 352 | + DefaultComp def = Component.FindDefaultComp(g.designator); |
| 353 | + if (def != null) |
| 354 | + { |
| 355 | + sw.Write("[ " + def.long_name); |
| 356 | + sw.Write(", " + g.comp_list.Count.ToString() + (g.comp_list.Count > 1 ? " values" : " value")); |
| 357 | + if (def.has_default) |
| 358 | + sw.Write(", " + def.default_type + " unless otherwise stated"); |
| 359 | + sw.WriteLine(" ]"); |
| 360 | + } |
| 361 | + else |
| 362 | + sw.WriteLine("[ " + g.designator + ", " + g.comp_list.Count.ToString() + |
| 363 | + (g.comp_list.Count > 1 ? " values" : " value") + " ]"); |
| 364 | + |
| 365 | + sw.WriteLine(new string('-', count_width + 2 + reference_width + 2 + value_width + 2 + footprint_width + 2 + precision_width)); |
| 366 | + |
| 367 | + // component list |
| 368 | + foreach (Component c in g.comp_list) |
| 369 | + { |
| 370 | + if (c.no_part) |
| 371 | + continue; |
| 372 | + |
| 373 | + string footprint = c.footprint_normalized; |
| 374 | + if (footprint == "") |
| 375 | + footprint = c.footprint; |
| 376 | + sw.Write(TextUtils.FixedLengthString((c.count + 1).ToString(), ' ', count_width) + " "); |
| 377 | + |
| 378 | + string reference = TextUtils.Reformat(c.reference, reference_width); |
| 379 | + int split_point = reference.IndexOf('\n'); |
| 380 | + if (split_point == -1) |
| 381 | + sw.Write(TextUtils.FixedLengthString(reference, ' ', reference_width) + " "); |
| 382 | + else |
| 383 | + sw.Write(TextUtils.FixedLengthString(reference.Substring(0, split_point - 1), ' ', reference_width) + " "); |
| 384 | + |
| 385 | + sw.Write(TextUtils.FixedLengthString(c.value, ' ', value_width) + " "); |
| 386 | + //sw.Write(TextUtils.FixedLengthString(c.part_no, ' ', 10)); |
| 387 | + sw.Write(TextUtils.FixedLengthString(footprint, ' ', footprint_width) + " "); |
| 388 | + if (!string.IsNullOrEmpty(c.precision)) |
| 389 | + sw.Write(TextUtils.FixedLengthString(c.precision, ' ', precision_width)); |
| 390 | + sw.WriteLine(); |
| 391 | + |
| 392 | + if (split_point != -1) // need to do the rest of the references |
| 393 | + { |
| 394 | + string indent = new string(' ', count_width + 2); |
| 395 | + do |
| 396 | + { |
| 397 | + reference = reference.Substring(split_point + 1); |
| 398 | + split_point = reference.IndexOf('\n'); |
| 399 | + if (split_point == -1) |
| 400 | + sw.WriteLine(indent + reference.Trim()); |
| 401 | + else |
| 402 | + sw.WriteLine(indent + reference.Substring(0, split_point - 1).Trim()); |
| 403 | + } while (split_point != -1); |
| 404 | + } |
| 405 | + } |
| 406 | + sw.WriteLine(); |
| 407 | + } |
| 408 | + } |
| 409 | + } |
| 410 | + |
263 | 411 | public static void OutputPDF(List<DesignatorGroup> groups, HeaderBlock header, string file, bool rtf = false) |
264 | 412 | { |
265 | 413 | Console.WriteLine("Generating " + file + "..."); |
|
0 commit comments