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.

250 lines
6.6 KiB

  1. /***********************************************************************/
  2. /* component_class.cpp : handle the class SCH_COMPONENT */
  3. /***********************************************************************/
  4. #include "fctsys.h"
  5. #include "gr_basic.h"
  6. #include "common.h"
  7. #include "program.h"
  8. #include "libcmp.h"
  9. #include "general.h"
  10. #include "macros.h"
  11. #include "protos.h"
  12. /***************************************************************************/
  13. SCH_CMP_FIELD::SCH_CMP_FIELD( const wxPoint& aPos, int aFieldId, SCH_COMPONENT* aParent, wxString aName ) :
  14. SCH_ITEM( aParent, DRAW_PART_TEXT_STRUCT_TYPE ),
  15. EDA_TextStruct()
  16. /***************************************************************************/
  17. {
  18. m_Pos = aPos;
  19. m_FieldId = aFieldId;
  20. m_AddExtraText = false;
  21. m_Attributs = TEXT_NO_VISIBLE;
  22. m_Name = aName;
  23. SetLayer( LAYER_FIELDS );
  24. }
  25. /************************************/
  26. SCH_CMP_FIELD::~SCH_CMP_FIELD()
  27. /************************************/
  28. {
  29. }
  30. /**************************************************************************/
  31. void SCH_CMP_FIELD::SwapData( SCH_CMP_FIELD* copyitem )
  32. /**************************************************************************/
  33. /* Used if undo / redo command:
  34. * swap data between this and copyitem
  35. */
  36. {
  37. EXCHG( m_Text, copyitem->m_Text );
  38. EXCHG( m_Layer, copyitem->m_Layer );
  39. EXCHG( m_Pos, copyitem->m_Pos );
  40. EXCHG( m_Size, copyitem->m_Size );
  41. EXCHG( m_Width, copyitem->m_Width );
  42. EXCHG( m_Orient, copyitem->m_Orient );
  43. EXCHG( m_Miroir, copyitem->m_Miroir );
  44. EXCHG( m_Attributs, copyitem->m_Attributs );
  45. EXCHG( m_CharType, copyitem->m_CharType );
  46. EXCHG( m_HJustify, copyitem->m_HJustify );
  47. EXCHG( m_VJustify, copyitem->m_VJustify );
  48. EXCHG( m_ZoomLevelDrawable, copyitem->m_ZoomLevelDrawable );
  49. EXCHG( m_TextDrawings, copyitem->m_TextDrawings );
  50. EXCHG( m_TextDrawingsSize, copyitem->m_TextDrawingsSize );
  51. }
  52. #if 0
  53. /***********************************************************/
  54. void SCH_CMP_FIELD::PartTextCopy( SCH_CMP_FIELD* target )
  55. /***********************************************************/
  56. {
  57. target->m_Text = m_Text;
  58. if( m_FieldId >= FIELD1 )
  59. target->m_Name = m_Name;
  60. target->m_Layer = m_Layer;
  61. target->m_Pos = m_Pos;
  62. target->m_Size = m_Size;
  63. target->m_Attributs = m_Attributs;
  64. target->m_FieldId = m_FieldId;
  65. target->m_Orient = m_Orient;
  66. target->m_HJustify = m_HJustify;
  67. target->m_VJustify = m_VJustify;
  68. target->m_Flags = m_Flags;
  69. }
  70. #endif
  71. /*********************************/
  72. bool SCH_CMP_FIELD::IsVoid()
  73. /*********************************/
  74. /* return True if the field is void, i.e.:
  75. * contains "~" or ""
  76. */
  77. {
  78. if( m_Text.IsEmpty() || m_Text == wxT( "~" ) )
  79. return TRUE;
  80. return FALSE;
  81. }
  82. /********************************************/
  83. EDA_Rect SCH_CMP_FIELD::GetBoundaryBox() const
  84. /********************************************/
  85. /** Function GetBoundaryBox()
  86. * @return an EDA_Rect contains the real (user coordinates) boundary box for a text field,
  87. * according to the component position, rotation, mirror ...
  88. *
  89. */
  90. {
  91. EDA_Rect BoundaryBox;
  92. int hjustify, vjustify;
  93. int textlen;
  94. int orient;
  95. int dx, dy, x1, y1, x2, y2;
  96. SCH_COMPONENT* DrawLibItem = (SCH_COMPONENT*) m_Parent;
  97. orient = m_Orient;
  98. wxPoint pos = DrawLibItem->m_Pos;
  99. x1 = m_Pos.x - pos.x;
  100. y1 = m_Pos.y - pos.y;
  101. textlen = GetLength();
  102. if( m_FieldId == REFERENCE ) // Real Text can be U1 or U1A
  103. {
  104. EDA_LibComponentStruct* Entry =
  105. FindLibPart( DrawLibItem->m_ChipName.GetData(), wxEmptyString, FIND_ROOT );
  106. if( Entry && (Entry->m_UnitCount > 1) )
  107. textlen++; // because U1 is show as U1A or U1B ...
  108. }
  109. dx = m_Size.x * textlen;
  110. // Real X Size is 10/9 char size because space between 2 chars is 1/10 X Size
  111. dx = (dx * 10) / 9;
  112. dy = m_Size.y;
  113. hjustify = m_HJustify;
  114. vjustify = m_VJustify;
  115. x2 = pos.x + (DrawLibItem->m_Transform[0][0] * x1)
  116. + (DrawLibItem->m_Transform[0][1] * y1);
  117. y2 = pos.y + (DrawLibItem->m_Transform[1][0] * x1)
  118. + (DrawLibItem->m_Transform[1][1] * y1);
  119. /* If the component orientation is +/- 90 deg, the text orienation must be changed */
  120. if( DrawLibItem->m_Transform[0][1] )
  121. {
  122. if( orient == TEXT_ORIENT_HORIZ )
  123. orient = TEXT_ORIENT_VERT;
  124. else
  125. orient = TEXT_ORIENT_HORIZ;
  126. /* is it mirrored (for text justify)*/
  127. EXCHG( hjustify, vjustify );
  128. if( DrawLibItem->m_Transform[1][0] < 0 )
  129. vjustify = -vjustify;
  130. if( DrawLibItem->m_Transform[0][1] > 0 )
  131. hjustify = -hjustify;
  132. }
  133. else /* component horizontal: is it mirrored (for text justify)*/
  134. {
  135. if( DrawLibItem->m_Transform[0][0] < 0 )
  136. hjustify = -hjustify;
  137. if( DrawLibItem->m_Transform[1][1] > 0 )
  138. vjustify = -vjustify;
  139. }
  140. if( orient == TEXT_ORIENT_VERT )
  141. EXCHG( dx, dy );
  142. switch( hjustify )
  143. {
  144. case GR_TEXT_HJUSTIFY_CENTER:
  145. x1 = x2 - (dx / 2);
  146. break;
  147. case GR_TEXT_HJUSTIFY_RIGHT:
  148. x1 = x2 - dx;
  149. break;
  150. default:
  151. x1 = x2;
  152. break;
  153. }
  154. switch( vjustify )
  155. {
  156. case GR_TEXT_VJUSTIFY_CENTER:
  157. y1 = y2 - (dy / 2);
  158. break;
  159. case GR_TEXT_VJUSTIFY_BOTTOM:
  160. y1 = y2 - dy;
  161. break;
  162. default:
  163. y1 = y2;
  164. break;
  165. }
  166. BoundaryBox.SetX( x1 );
  167. BoundaryBox.SetY( y1 );
  168. BoundaryBox.SetWidth( dx );
  169. BoundaryBox.SetHeight( dy );
  170. return BoundaryBox;
  171. }
  172. bool SCH_CMP_FIELD::Save( FILE* aFile ) const
  173. {
  174. char hjustify = 'C';
  175. if( m_HJustify == GR_TEXT_HJUSTIFY_LEFT )
  176. hjustify = 'L';
  177. else if( m_HJustify == GR_TEXT_HJUSTIFY_RIGHT )
  178. hjustify = 'R';
  179. char vjustify = 'C';
  180. if( m_VJustify == GR_TEXT_VJUSTIFY_BOTTOM )
  181. vjustify = 'B';
  182. else if( m_VJustify == GR_TEXT_VJUSTIFY_TOP )
  183. vjustify = 'T';
  184. if( fprintf( aFile, "F %d \"%s\" %c %-3d %-3d %-3d %4.4X %c %c",
  185. m_FieldId,
  186. CONV_TO_UTF8( m_Text ),
  187. m_Orient == TEXT_ORIENT_HORIZ ? 'H' : 'V',
  188. m_Pos.x, m_Pos.y,
  189. m_Size.x,
  190. m_Attributs,
  191. hjustify, vjustify ) == EOF )
  192. {
  193. return false;
  194. }
  195. // Save field name, if the name is user definable
  196. if( m_FieldId >= FIELD1 )
  197. {
  198. if( fprintf( aFile, " \"%s\"", CONV_TO_UTF8( m_Name ) ) == EOF )
  199. {
  200. return false;
  201. }
  202. }
  203. if( fprintf( aFile, "\n" ) == EOF )
  204. {
  205. return false;
  206. }
  207. return true;
  208. }