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.

233 lines
6.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Author Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "symb_transforms_utils.h"
  21. #include <lib_symbol.h>
  22. #include <sch_symbol.h>
  23. struct ORIENT_MIRROR
  24. {
  25. int flag;
  26. int n_rots;
  27. int mirror_x;
  28. int mirror_y;
  29. };
  30. // symbols_orientations_list is the list of possible orientation+mirror values
  31. // like returned by SCH_SYMBOL::GetOrientation()
  32. // Some transforms are equivalent, like rotation 180 + mirror X = mirror Y
  33. static ORIENT_MIRROR symbols_orientations_list[] =
  34. {
  35. { SYM_ORIENT_0, 0, 0, 0 },
  36. { SYM_ORIENT_90, 1, 0, 0 },
  37. { SYM_ORIENT_180, 2, 0, 0 },
  38. { SYM_ORIENT_270, 3, 0, 0 },
  39. { SYM_MIRROR_X + SYM_ORIENT_0, 0, 1, 0 },
  40. { SYM_MIRROR_X + SYM_ORIENT_90, 1, 1, 0 },
  41. { SYM_MIRROR_Y, 0, 0, 1 },
  42. { SYM_MIRROR_X + SYM_ORIENT_270, 3, 1, 0 },
  43. { SYM_MIRROR_Y + SYM_ORIENT_0, 0, 0, 1 },
  44. { SYM_MIRROR_Y + SYM_ORIENT_90, 1, 0, 1 },
  45. { SYM_MIRROR_Y + SYM_ORIENT_180, 2, 0, 1 },
  46. { SYM_MIRROR_Y + SYM_ORIENT_270, 3, 0, 1 }
  47. };
  48. void OrientAndMirrorSymbolItems( LIB_SYMBOL* aSymbol, int aOrientation )
  49. {
  50. ORIENT_MIRROR o = symbols_orientations_list[ 0 ];
  51. for( ORIENT_MIRROR& i : symbols_orientations_list )
  52. {
  53. if( i.flag == aOrientation )
  54. {
  55. o = i;
  56. break;
  57. }
  58. }
  59. for( SCH_ITEM& item : aSymbol->GetDrawItems() )
  60. {
  61. for( int i = 0; i < o.n_rots; i++ )
  62. item.Rotate( VECTOR2I( 0, 0 ), true );
  63. if( o.mirror_x )
  64. item.MirrorVertically( 0 );
  65. if( o.mirror_y )
  66. item.MirrorHorizontally( 0 );
  67. }
  68. }
  69. void RotateAndMirrorPin( SCH_PIN& aPin, int aOrientMirror )
  70. {
  71. ORIENT_MIRROR o = symbols_orientations_list[ 0 ];
  72. for( ORIENT_MIRROR& i : symbols_orientations_list )
  73. {
  74. if( i.flag == aOrientMirror )
  75. {
  76. o = i;
  77. break;
  78. }
  79. }
  80. for( int i = 0; i < o.n_rots; i++ )
  81. aPin.RotatePin( VECTOR2I( 0, 0 ), true );
  82. if( o.mirror_x )
  83. aPin.MirrorVerticallyPin( 0 );
  84. if( o.mirror_y )
  85. aPin.MirrorHorizontallyPin( 0 );
  86. }
  87. SPIN_STYLE GetPinSpinStyle( const SCH_PIN& aPin, const SCH_SYMBOL& aSymbol )
  88. {
  89. SPIN_STYLE ret = SPIN_STYLE::UP;
  90. if( aPin.GetOrientation() == PIN_ORIENTATION::PIN_RIGHT )
  91. ret = SPIN_STYLE::LEFT;
  92. else if( aPin.GetOrientation() == PIN_ORIENTATION::PIN_LEFT )
  93. ret = SPIN_STYLE::RIGHT;
  94. else if( aPin.GetOrientation() == PIN_ORIENTATION::PIN_UP )
  95. ret = SPIN_STYLE::BOTTOM;
  96. else if( aPin.GetOrientation() == PIN_ORIENTATION::PIN_DOWN )
  97. ret = SPIN_STYLE::UP;
  98. switch( static_cast<SYMBOL_ORIENTATION_T>( aSymbol.GetOrientation()
  99. & ( ~( SYM_MIRROR_X | SYM_MIRROR_Y ) ) ) )
  100. {
  101. case SYM_ROTATE_CLOCKWISE:
  102. case SYM_ORIENT_90:
  103. if( ret == SPIN_STYLE::UP )
  104. ret = SPIN_STYLE::LEFT;
  105. else if( ret == SPIN_STYLE::BOTTOM )
  106. ret = SPIN_STYLE::RIGHT;
  107. else if( ret == SPIN_STYLE::LEFT )
  108. ret = SPIN_STYLE::BOTTOM;
  109. else if( ret == SPIN_STYLE::RIGHT )
  110. ret = SPIN_STYLE::UP;
  111. if( aSymbol.GetOrientation() & SYM_MIRROR_X )
  112. {
  113. if( ret == SPIN_STYLE::UP )
  114. ret = SPIN_STYLE::BOTTOM;
  115. else if( ret == SPIN_STYLE::BOTTOM )
  116. ret = SPIN_STYLE::UP;
  117. }
  118. if( aSymbol.GetOrientation() & SYM_MIRROR_Y )
  119. {
  120. if( ret == SPIN_STYLE::LEFT )
  121. ret = SPIN_STYLE::RIGHT;
  122. else if( ret == SPIN_STYLE::RIGHT )
  123. ret = SPIN_STYLE::LEFT;
  124. }
  125. break;
  126. case SYM_ROTATE_COUNTERCLOCKWISE:
  127. case SYM_ORIENT_270:
  128. if( ret == SPIN_STYLE::UP )
  129. ret = SPIN_STYLE::RIGHT;
  130. else if( ret == SPIN_STYLE::BOTTOM )
  131. ret = SPIN_STYLE::LEFT;
  132. else if( ret == SPIN_STYLE::LEFT )
  133. ret = SPIN_STYLE::UP;
  134. else if( ret == SPIN_STYLE::RIGHT )
  135. ret = SPIN_STYLE::BOTTOM;
  136. if( aSymbol.GetOrientation() & SYM_MIRROR_X )
  137. {
  138. if( ret == SPIN_STYLE::UP )
  139. ret = SPIN_STYLE::BOTTOM;
  140. else if( ret == SPIN_STYLE::BOTTOM )
  141. ret = SPIN_STYLE::UP;
  142. }
  143. if( aSymbol.GetOrientation() & SYM_MIRROR_Y )
  144. {
  145. if( ret == SPIN_STYLE::LEFT )
  146. ret = SPIN_STYLE::RIGHT;
  147. else if( ret == SPIN_STYLE::RIGHT )
  148. ret = SPIN_STYLE::LEFT;
  149. }
  150. break;
  151. case SYM_ORIENT_180:
  152. if( ret == SPIN_STYLE::UP )
  153. ret = SPIN_STYLE::BOTTOM;
  154. else if( ret == SPIN_STYLE::BOTTOM )
  155. ret = SPIN_STYLE::UP;
  156. else if( ret == SPIN_STYLE::LEFT )
  157. ret = SPIN_STYLE::RIGHT;
  158. else if( ret == SPIN_STYLE::RIGHT )
  159. ret = SPIN_STYLE::LEFT;
  160. if( aSymbol.GetOrientation() & SYM_MIRROR_X )
  161. {
  162. if( ret == SPIN_STYLE::UP )
  163. ret = SPIN_STYLE::BOTTOM;
  164. else if( ret == SPIN_STYLE::BOTTOM )
  165. ret = SPIN_STYLE::UP;
  166. }
  167. if( aSymbol.GetOrientation() & SYM_MIRROR_Y )
  168. {
  169. if( ret == SPIN_STYLE::LEFT )
  170. ret = SPIN_STYLE::RIGHT;
  171. else if( ret == SPIN_STYLE::RIGHT )
  172. ret = SPIN_STYLE::LEFT;
  173. }
  174. break;
  175. case SYM_ORIENT_0:
  176. case SYM_NORMAL:
  177. default:
  178. if( aSymbol.GetOrientation() & SYM_MIRROR_X )
  179. {
  180. if( ret == SPIN_STYLE::UP )
  181. ret = SPIN_STYLE::BOTTOM;
  182. else if( ret == SPIN_STYLE::BOTTOM )
  183. ret = SPIN_STYLE::UP;
  184. }
  185. if( aSymbol.GetOrientation() & SYM_MIRROR_Y )
  186. {
  187. if( ret == SPIN_STYLE::LEFT )
  188. ret = SPIN_STYLE::RIGHT;
  189. else if( ret == SPIN_STYLE::RIGHT )
  190. ret = SPIN_STYLE::LEFT;
  191. }
  192. break;
  193. }
  194. return ret;
  195. }