You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
5.1 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: build_BOM.cpp
  3. // Author: jean-pierre Charras
  4. // License: GPL license
  5. /////////////////////////////////////////////////////////////////////////////
  6. #include <algorithm> // to use sort vector
  7. #include <vector>
  8. #include "fctsys.h"
  9. #include "common.h"
  10. #include "class_sch_screen.h"
  11. #include "kicad_string.h"
  12. #include "general.h"
  13. #include "sch_sheet.h"
  14. #include "sch_component.h"
  15. #include "template_fieldnames.h"
  16. #include "netlist.h"
  17. /* Fill aList with labels
  18. */
  19. void GenListeGLabels( LABEL_OBJECT_LIST& aList )
  20. {
  21. // Build the sheet list
  22. SCH_SHEET_LIST sheetList;
  23. LABEL_OBJECT label;
  24. for( SCH_SHEET_PATH* path = sheetList.GetFirst(); path; path = sheetList.GetNext() )
  25. {
  26. SCH_ITEM* schItem = (SCH_ITEM*) path->LastDrawList();
  27. while( schItem )
  28. {
  29. switch( schItem->Type() )
  30. {
  31. case SCH_HIERARCHICAL_LABEL_T:
  32. case SCH_GLOBAL_LABEL_T:
  33. label.m_LabelType = schItem->Type();
  34. label.m_SheetPath = *path;
  35. label.m_Label = schItem;
  36. aList.push_back( label );
  37. break;
  38. case SCH_SHEET_T:
  39. {
  40. SCH_SHEET* sheet = (SCH_SHEET*) schItem;
  41. BOOST_FOREACH( SCH_SHEET_PIN& sheetPin, sheet->GetPins() )
  42. {
  43. label.m_LabelType = SCH_SHEET_PIN_T;
  44. label.m_SheetPath = *path;
  45. label.m_Label = &sheetPin;
  46. aList.push_back( label );
  47. }
  48. }
  49. break;
  50. default:
  51. break;
  52. }
  53. schItem = schItem->Next();
  54. }
  55. }
  56. }
  57. /* compare function for sorting labels
  58. * sort by
  59. * value
  60. * if same value: by sheet
  61. */
  62. bool SortLabelsByValue( const LABEL_OBJECT& obj1, const LABEL_OBJECT& obj2 )
  63. {
  64. int ii;
  65. wxString* Text1, * Text2;
  66. if( obj1.m_LabelType == SCH_SHEET_PIN_T )
  67. Text1 = &( (SCH_SHEET_PIN*)(obj1.m_Label) )->m_Text;
  68. else
  69. Text1 = &( (SCH_TEXT*)(obj1.m_Label) )->m_Text;
  70. if( obj2.m_LabelType == SCH_SHEET_PIN_T )
  71. Text2 = &( (SCH_SHEET_PIN*)(obj2.m_Label) )->m_Text;
  72. else
  73. Text2 = &( (SCH_TEXT*)(obj2.m_Label) )->m_Text;
  74. ii = Text1->CmpNoCase( *Text2 );
  75. if( ii == 0 )
  76. {
  77. ii = obj1.m_SheetPath.Cmp( obj2.m_SheetPath );
  78. }
  79. return ii < 0;
  80. }
  81. /* compare function for sorting labels
  82. * by sheet
  83. * in a sheet, by alphabetic order
  84. */
  85. bool SortLabelsBySheet( const LABEL_OBJECT& obj1, const LABEL_OBJECT& obj2 )
  86. {
  87. int ii;
  88. wxString Text1, Text2;
  89. ii = obj1.m_SheetPath.Cmp( obj2.m_SheetPath );
  90. if( ii == 0 )
  91. {
  92. if( obj1.m_LabelType == SCH_SHEET_PIN_T )
  93. Text1 = ( (SCH_SHEET_PIN*) obj1.m_Label )->m_Text;
  94. else
  95. Text1 = ( (SCH_TEXT*) obj1.m_Label )->m_Text;
  96. if( obj2.m_LabelType == SCH_SHEET_PIN_T )
  97. Text2 = ( (SCH_SHEET_PIN*) obj2.m_Label )->m_Text;
  98. else
  99. Text2 = ( (SCH_TEXT*) obj2.m_Label )->m_Text;
  100. ii = Text1.CmpNoCase( Text2 );
  101. }
  102. return ii < 0;
  103. }
  104. int PrintListeGLabel( FILE* f, LABEL_OBJECT_LIST& aList )
  105. {
  106. SCH_LABEL* label;
  107. SCH_SHEET_PIN* pinsheet;
  108. wxString msg, sheetpath;
  109. wxString labeltype;
  110. for( unsigned ii = 0; ii < aList.size(); ii++ )
  111. {
  112. switch( aList[ii].m_LabelType )
  113. {
  114. case SCH_HIERARCHICAL_LABEL_T:
  115. case SCH_GLOBAL_LABEL_T:
  116. label = (SCH_LABEL*)(aList[ii].m_Label);
  117. if( aList[ii].m_LabelType == SCH_HIERARCHICAL_LABEL_T )
  118. labeltype = wxT( "Hierarchical" );
  119. else
  120. labeltype = wxT( "Global " );
  121. sheetpath = aList[ii].m_SheetPath.PathHumanReadable();
  122. msg.Printf( _( "> %-28.28s %s (Sheet %s) pos: %3.3f, %3.3f\n" ),
  123. GetChars( label->m_Text ),
  124. GetChars( labeltype ),
  125. GetChars( sheetpath ),
  126. (float) label->m_Pos.x / 1000,
  127. (float) label->m_Pos.y / 1000 );
  128. fputs( TO_UTF8( msg ), f );
  129. break;
  130. case SCH_SHEET_PIN_T:
  131. {
  132. pinsheet = (SCH_SHEET_PIN*) aList[ii].m_Label;
  133. int jj = pinsheet->m_Shape;
  134. if( jj < 0 )
  135. jj = NET_TMAX;
  136. if( jj > NET_TMAX )
  137. jj = 4;
  138. wxString labtype = FROM_UTF8( SheetLabelType[jj] );
  139. msg.Printf( _( "> %-28.28s PinSheet %-7.7s (Sheet %s) pos: %3.3f, %3.3f\n" ),
  140. GetChars( pinsheet->m_Text ),
  141. GetChars( labtype ),
  142. GetChars( aList[ii].m_SheetPath.PathHumanReadable() ),
  143. (float) pinsheet->m_Pos.x / 1000,
  144. (float) pinsheet->m_Pos.y / 1000 );
  145. fputs( TO_UTF8( msg ), f );
  146. }
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. msg = _( "#End labels\n" );
  153. fputs( TO_UTF8( msg ), f );
  154. return 0;
  155. }