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.

358 lines
10 KiB

1 year ago
1 year ago
1 year ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef LSET_H
  20. #define LSET_H
  21. #include <layer_ids.h>
  22. #include <base_set.h>
  23. class LSEQ;
  24. class LAYER_RANGE;
  25. /**
  26. * LSET is a set of PCB_LAYER_IDs.
  27. *
  28. * It can be converted to numerous purpose LSEQs using the various member functions, most of
  29. * which are based on Seq(). The advantage of converting to #LSEQ using purposeful code, is it
  30. * removes any dependency on order/sequence inherent in this set.
  31. */
  32. class KICOMMON_API LSET : public BASE_SET
  33. {
  34. public:
  35. /**
  36. * Create an empty (cleared) set.
  37. */
  38. LSET() : BASE_SET( PCB_LAYER_ID_COUNT ) {} // all bits are set to zero in BASE_SET()
  39. LSET( const BASE_SET& aOther ) : BASE_SET( aOther ) {}
  40. LSET( std::initializer_list<PCB_LAYER_ID> aList );
  41. LSET( const std::vector<PCB_LAYER_ID>& aList );
  42. LSET( const LSEQ& aSeq );
  43. LSET( const LAYER_RANGE& aRange );
  44. LSET( unsigned long __val ) = delete;
  45. /**
  46. * See if the layer set contains a PCB layer.
  47. *
  48. * @param aLayer is the layer to check.
  49. * @return true if the layer is included.
  50. */
  51. bool Contains( PCB_LAYER_ID aLayer ) const
  52. {
  53. // At the moment, LSET cannot store negative layers, but PCB_LAYER_ID can contain them
  54. if( aLayer < 0 )
  55. return false;
  56. try
  57. {
  58. return test( aLayer );
  59. }
  60. catch( std::out_of_range& )
  61. {
  62. return false;
  63. }
  64. }
  65. /**
  66. * Return the fixed name association with @a aLayerId.
  67. *
  68. * @note These names must not be translated or changed. They are used as tokens in the board
  69. * file format because the ordinal value of the PCB_LAYER_ID enum was not stable over time.
  70. * @see LayerName() for what should be used to display the default name of a layer in the GUI.
  71. */
  72. static wxString Name( PCB_LAYER_ID aLayerId );
  73. /**
  74. * Return the layer number from a layer name.
  75. */
  76. static int NameToLayer( wxString& aName );
  77. /**
  78. * Return true if aLayer is between aStart and aEnd, inclusive.
  79. *
  80. * This takes into account the direction of the layers and the fact that #B_Cu comes before
  81. * In*_Cu.
  82. */
  83. static bool IsBetween( PCB_LAYER_ID aStart, PCB_LAYER_ID aEnd, PCB_LAYER_ID aLayer );
  84. /**
  85. * Return a complete set of internal copper layers which is all Cu layers
  86. * except #F_Cu and #B_Cu.
  87. */
  88. static const LSET& InternalCuMask();
  89. /**
  90. * Return a complete set of all top assembly layers which is all #F_SilkS and #F_Mask.
  91. */
  92. static const LSET& FrontAssembly();
  93. /**
  94. * Return a complete set of all bottom assembly layers which is all #B_SilkS and #B_Mask.
  95. */
  96. static const LSET& BackAssembly();
  97. /**
  98. * Return a mask holding the requested number of Cu PCB_LAYER_IDs.
  99. */
  100. static LSET AllCuMask( int aCuLayerCount );
  101. /**
  102. * return AllCuMask( MAX_CU_LAYERS );
  103. */
  104. static LSET AllCuMask();
  105. /**
  106. * Return a mask holding the Front and Bottom layers.
  107. */
  108. static const LSET& ExternalCuMask();
  109. /**
  110. * Return a mask holding all layer minus CU layers.
  111. */
  112. static LSET AllNonCuMask();
  113. static const LSET& AllLayersMask();
  114. /**
  115. * Return a mask holding all technical layers (no CU layer) on front side.
  116. */
  117. static const LSET& FrontTechMask();
  118. /**
  119. * Return a mask holding technical layers used in a board fabrication
  120. * (no CU layer) on front side.
  121. */
  122. static const LSET& FrontBoardTechMask();
  123. /**
  124. * Return a mask holding all technical layers (no CU layer) on back side.
  125. */
  126. static const LSET& BackTechMask();
  127. /**
  128. * Return a mask holding technical layers used in a board fabrication
  129. * (no CU layer) on Back side.
  130. */
  131. static const LSET& BackBoardTechMask();
  132. /**
  133. * Return a mask holding all technical layers (no CU layer) on both side.
  134. */
  135. static const LSET& AllTechMask();
  136. /**
  137. * Return a mask holding board technical layers (no CU layer) on both side.
  138. */
  139. static const LSET& AllBoardTechMask();
  140. /**
  141. * Return a mask holding all technical layers and the external CU layer on front side.
  142. */
  143. static const LSET& FrontMask();
  144. /**
  145. * Return a mask holding all technical layers and the external CU layer on back side.
  146. */
  147. static const LSET& BackMask();
  148. static const LSET& SideSpecificMask();
  149. static const LSET& UserMask();
  150. /**
  151. * Return a mask holding all layers which are physically realized.
  152. *
  153. * Equivalent to the copper layers + the board tech mask.
  154. */
  155. static const LSET& PhysicalLayersMask();
  156. /**
  157. * Return a mask with the requested number of user defined layers.
  158. *
  159. * @param aUserDefinedLayerCount The number of user defined layers
  160. */
  161. static LSET UserDefinedLayersMask( int aUserDefinedLayerCount = MAX_USER_DEFINED_LAYERS );
  162. /**
  163. * Return a sequence of copper layers in starting from the front/top
  164. * and extending to the back/bottom.
  165. */
  166. LSEQ CuStack() const;
  167. /**
  168. * Return the technical and user layers in the order shown in layer widget.
  169. */
  170. LSEQ TechAndUserUIOrder() const;
  171. /**
  172. * Return the copper, technical and user layers in the order shown in layer widget.
  173. */
  174. LSEQ UIOrder() const;
  175. /**
  176. * Return an #LSEQ from the union of this #LSET and a desired sequence.
  177. *
  178. * The #LSEQ element will be in the same sequence as aWishListSequence if they are present.
  179. *
  180. * @param aWishListSequence establishes the order of the returned LSEQ, and the LSEQ will only
  181. * contain PCB_LAYER_IDs which are present in this set.
  182. */
  183. LSEQ Seq( const LSEQ& aSequence ) const;
  184. /**
  185. * Return a #LSEQ from this #LSET in ascending PCB_LAYER_ID order.
  186. *
  187. * Each #LSEQ element will be in the same sequence as in PCB_LAYER_ID and only present
  188. * in the resultant #LSEQ if present in this set. Therefore the sequence is subject to
  189. * change, use it only when enumeration and not order is important.
  190. */
  191. LSEQ Seq() const;
  192. /**
  193. * Generate a sequence of layers that represent a top to bottom stack of this set of layers.
  194. *
  195. * @param aSelectedLayer is the layer to put at the top of stack when defined.
  196. * @return the top to bottom layer sequence.
  197. */
  198. LSEQ SeqStackupTop2Bottom( PCB_LAYER_ID aSelectedLayer = UNDEFINED_LAYER ) const;
  199. /**
  200. * Return the sequence that is typical for a bottom-to-top stack-up.
  201. *
  202. * For instance, to plot multiple layers in a single image, the top layers output last.
  203. */
  204. LSEQ SeqStackupForPlotting() const;
  205. /**
  206. * Execute a function on each layer of the #LSET.
  207. */
  208. void RunOnLayers( const std::function<void( PCB_LAYER_ID )>& aFunction ) const
  209. {
  210. for( size_t ii = 0; ii < size(); ++ii )
  211. {
  212. if( test( ii ) )
  213. aFunction( PCB_LAYER_ID( ii ) );
  214. }
  215. }
  216. /**
  217. * Find the first set #PCB_LAYER_ID.
  218. *
  219. * @return #UNDEFINED_LAYER if more than one is set or #UNSELECTED_LAYER if none is set.
  220. */
  221. PCB_LAYER_ID ExtractLayer() const;
  222. /**
  223. * Flip the layers in this set.
  224. *
  225. * BACK and FRONT copper layers, mask, paste, solder layers are swapped
  226. * internal layers are flipped only if the copper layers count is known.
  227. *
  228. * @param aMask = the LSET to flip
  229. * @param aCopperLayersCount = the number of copper layers. if 0 (in fact if < 4 )
  230. * internal layers will be not flipped because the layer count is not known
  231. */
  232. LSET& FlipStandardLayers( int aCopperLayersCount = 0 );
  233. /**
  234. * Return the number of layers between aStart and aEnd, inclusive.
  235. */
  236. static int LayerCount( PCB_LAYER_ID aStart, PCB_LAYER_ID aEnd, int aCopperLayerCount );
  237. /**
  238. * Clear the copper layers in this set.
  239. */
  240. LSET& ClearCopperLayers();
  241. /**
  242. * Clear the non-copper layers in this set.
  243. */
  244. LSET& ClearNonCopperLayers();
  245. /**
  246. * Clear the user defined layers in this set.
  247. */
  248. LSET& ClearUserDefinedLayers();
  249. #ifndef SWIG
  250. // Custom iterator to iterate over all set bits
  251. class KICOMMON_API all_set_layers_iterator : public BASE_SET::set_bits_iterator
  252. {
  253. public:
  254. all_set_layers_iterator( const BASE_SET& set, size_t index ) :
  255. BASE_SET::set_bits_iterator( set, index )
  256. {
  257. }
  258. PCB_LAYER_ID operator*() const
  259. {
  260. return PCB_LAYER_ID( BASE_SET::set_bits_iterator::operator*() );
  261. }
  262. all_set_layers_iterator& operator++()
  263. {
  264. BASE_SET::set_bits_iterator::operator++();
  265. return *this;
  266. }
  267. };
  268. all_set_layers_iterator begin() const { return all_set_layers_iterator( *this, 0 ); }
  269. all_set_layers_iterator end() const { return all_set_layers_iterator( *this, size() ); }
  270. // Custom iterators for Copper and Non-Copper layers
  271. class KICOMMON_API copper_layers_iterator : public BASE_SET::set_bits_iterator
  272. {
  273. public:
  274. copper_layers_iterator( const BASE_SET& set, size_t index );
  275. PCB_LAYER_ID operator*() const;
  276. copper_layers_iterator& operator++();
  277. private:
  278. void advance_to_next_set_copper_bit();
  279. void next_copper_layer();
  280. };
  281. class KICOMMON_API non_copper_layers_iterator : public BASE_SET::set_bits_iterator
  282. {
  283. public:
  284. non_copper_layers_iterator( const BASE_SET& set, size_t index );
  285. PCB_LAYER_ID operator*() const;
  286. non_copper_layers_iterator& operator++();
  287. private:
  288. void advance_to_next_set_non_copper_bit();
  289. };
  290. copper_layers_iterator copper_layers_begin() const;
  291. copper_layers_iterator copper_layers_end() const;
  292. non_copper_layers_iterator non_copper_layers_begin() const;
  293. non_copper_layers_iterator non_copper_layers_end() const;
  294. #endif
  295. };
  296. #endif // LSET_H