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.

330 lines
10 KiB

++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
14 years ago
14 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2009 Jean-Pierre Charras, jean-pierre.charras@inpg.fr
  6. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <algorithm>
  26. #include <netclass.h>
  27. #include <macros.h>
  28. #include <base_units.h>
  29. #include <api/api_enums.h>
  30. #include <api/api_utils.h>
  31. #include <api/common/types/project_settings.pb.h>
  32. // This will get mapped to "kicad_default" in the specctra_export.
  33. const char NETCLASS::Default[] = "Default";
  34. // Initial values for netclass initialization
  35. // track to track and track to pads clearance.
  36. const int DEFAULT_CLEARANCE = pcbIUScale.mmToIU( 0.2 );
  37. const int DEFAULT_VIA_DIAMETER = pcbIUScale.mmToIU( 0.6 );
  38. const int DEFAULT_VIA_DRILL = pcbIUScale.mmToIU( 0.3 );
  39. const int DEFAULT_UVIA_DIAMETER = pcbIUScale.mmToIU( 0.3 );
  40. const int DEFAULT_UVIA_DRILL = pcbIUScale.mmToIU( 0.1 );
  41. const int DEFAULT_TRACK_WIDTH = pcbIUScale.mmToIU( 0.2 );
  42. const int DEFAULT_DIFF_PAIR_WIDTH = pcbIUScale.mmToIU( 0.2 );
  43. const int DEFAULT_DIFF_PAIR_GAP = pcbIUScale.mmToIU( 0.25 );
  44. const int DEFAULT_DIFF_PAIR_VIAGAP = pcbIUScale.mmToIU( 0.25 );
  45. const int DEFAULT_WIRE_WIDTH = schIUScale.MilsToIU( 6 );
  46. const int DEFAULT_BUS_WIDTH = schIUScale.MilsToIU( 12 );
  47. const int DEFAULT_LINE_STYLE = 0; // solid
  48. NETCLASS::NETCLASS( const wxString& aName, bool aInitWithDefaults ) : m_isDefault( false )
  49. {
  50. m_constituents.push_back( this );
  51. SetName( aName );
  52. SetPriority( -1 );
  53. // Colors are a special optional case - always set, but UNSPECIFIED used in place of optional
  54. SetPcbColor( COLOR4D::UNSPECIFIED );
  55. SetSchematicColor( COLOR4D::UNSPECIFIED );
  56. if( aInitWithDefaults )
  57. {
  58. SetClearance( DEFAULT_CLEARANCE );
  59. SetViaDrill( DEFAULT_VIA_DRILL );
  60. SetuViaDrill( DEFAULT_UVIA_DRILL );
  61. SetTrackWidth( DEFAULT_TRACK_WIDTH );
  62. SetViaDiameter( DEFAULT_VIA_DIAMETER );
  63. SetuViaDiameter( DEFAULT_UVIA_DIAMETER );
  64. SetDiffPairWidth( DEFAULT_DIFF_PAIR_WIDTH );
  65. SetDiffPairGap( DEFAULT_DIFF_PAIR_GAP );
  66. SetDiffPairViaGap( DEFAULT_DIFF_PAIR_VIAGAP );
  67. SetWireWidth( DEFAULT_WIRE_WIDTH );
  68. SetBusWidth( DEFAULT_BUS_WIDTH );
  69. SetLineStyle( DEFAULT_LINE_STYLE );
  70. }
  71. ResetParents();
  72. }
  73. void NETCLASS::ResetParents()
  74. {
  75. SetClearanceParent( this );
  76. SetTrackWidthParent( this );
  77. SetViaDiameterParent( this );
  78. SetViaDrillParent( this );
  79. SetuViaDiameterParent( this );
  80. SetuViaDrillParent( this );
  81. SetDiffPairWidthParent( this );
  82. SetDiffPairGapParent( this );
  83. SetDiffPairViaGapParent( this );
  84. SetPcbColorParent( this );
  85. SetWireWidthParent( this );
  86. SetBusWidthParent( this );
  87. SetSchematicColorParent( this );
  88. SetLineStyleParent( this );
  89. }
  90. void NETCLASS::ResetParameters()
  91. {
  92. SetPcbColor( COLOR4D::UNSPECIFIED );
  93. SetSchematicColor( COLOR4D::UNSPECIFIED );
  94. SetClearance( std::optional<int>() );
  95. SetViaDrill( std::optional<int>() );
  96. SetuViaDrill( std::optional<int>() );
  97. SetTrackWidth( std::optional<int>() );
  98. SetViaDiameter( std::optional<int>() );
  99. SetuViaDiameter( std::optional<int>() );
  100. SetDiffPairWidth( std::optional<int>() );
  101. SetDiffPairGap( std::optional<int>() );
  102. SetDiffPairViaGap( std::optional<int>() );
  103. SetWireWidth( std::optional<int>() );
  104. SetBusWidth( std::optional<int>() );
  105. SetLineStyle( std::optional<int>() );
  106. ResetParents();
  107. }
  108. bool NETCLASS::operator==( const NETCLASS& other ) const
  109. {
  110. return m_constituents == other.m_constituents;
  111. }
  112. void NETCLASS::Serialize( google::protobuf::Any &aContainer ) const
  113. {
  114. using namespace kiapi::common;
  115. project::NetClass nc;
  116. nc.set_name( m_Name.ToUTF8() );
  117. nc.set_priority( m_Priority );
  118. nc.set_type( m_constituents.empty() ? project::NCT_EXPLICIT : project::NCT_IMPLICIT );
  119. for( NETCLASS* member : m_constituents )
  120. nc.add_constituents( member->GetName() );
  121. project::NetClassBoardSettings* board = nc.mutable_board();
  122. if( m_Clearance )
  123. board->mutable_clearance()->set_value_nm( *m_Clearance );
  124. if( m_TrackWidth )
  125. board->mutable_track_width()->set_value_nm( *m_TrackWidth );
  126. if( m_diffPairWidth )
  127. board->mutable_diff_pair_track_width()->set_value_nm( *m_diffPairWidth );
  128. if( m_diffPairGap )
  129. board->mutable_diff_pair_gap()->set_value_nm( *m_diffPairGap );
  130. if( m_diffPairViaGap )
  131. board->mutable_diff_pair_via_gap()->set_value_nm( *m_diffPairViaGap );
  132. if( m_ViaDia )
  133. {
  134. kiapi::board::types::PadStackLayer* layer = board->mutable_via_stack()->add_copper_layers();
  135. layer->set_shape( kiapi::board::types::PSS_CIRCLE );
  136. layer->set_layer( kiapi::board::types::BoardLayer::BL_F_Cu );
  137. PackVector2( *layer->mutable_size(), { *m_ViaDia, *m_ViaDia } );
  138. }
  139. if( m_ViaDrill )
  140. {
  141. PackVector2( *board->mutable_via_stack()->mutable_drill()->mutable_diameter(),
  142. { *m_ViaDrill, *m_ViaDrill } );
  143. }
  144. if( m_pcbColor != COLOR4D::UNSPECIFIED )
  145. PackColor( *board->mutable_color(), m_pcbColor );
  146. project::NetClassSchematicSettings* schematic = nc.mutable_schematic();
  147. if( m_wireWidth )
  148. schematic->mutable_wire_width()->set_value_nm( *m_wireWidth );
  149. if( m_busWidth )
  150. schematic->mutable_bus_width()->set_value_nm( *m_busWidth );
  151. if( m_schematicColor != COLOR4D::UNSPECIFIED )
  152. PackColor( *schematic->mutable_color(), m_schematicColor );
  153. if( m_lineStyle )
  154. {
  155. // TODO(JE) resolve issues with moving to kicommon
  156. // schematic->set_line_style( ToProtoEnum<LINE_STYLE, types::StrokeLineStyle>(
  157. // static_cast<LINE_STYLE>( *m_lineStyle ) ) );
  158. }
  159. aContainer.PackFrom( nc );
  160. }
  161. bool NETCLASS::Deserialize( const google::protobuf::Any &aContainer )
  162. {
  163. using namespace kiapi::common;
  164. project::NetClass nc;
  165. if( !aContainer.UnpackTo( &nc ) )
  166. return false;
  167. m_Name = wxString::FromUTF8( nc.name() );
  168. m_Priority = nc.priority();
  169. // We don't allow creating implicit classes directly
  170. if( nc.type() == project::NCT_IMPLICIT )
  171. return false;
  172. SetConstituentNetclasses( {} );
  173. if( nc.board().has_clearance() )
  174. m_Clearance = nc.board().clearance().value_nm();
  175. if( nc.board().has_track_width() )
  176. m_TrackWidth = nc.board().track_width().value_nm();
  177. if( nc.board().has_diff_pair_track_width() )
  178. m_diffPairWidth = nc.board().diff_pair_track_width().value_nm();
  179. if( nc.board().has_diff_pair_gap() )
  180. m_diffPairGap = nc.board().diff_pair_gap().value_nm();
  181. if( nc.board().has_diff_pair_via_gap() )
  182. m_diffPairViaGap = nc.board().diff_pair_via_gap().value_nm();
  183. if( nc.board().has_via_stack() )
  184. {
  185. if( nc.board().via_stack().copper_layers_size() > 0 )
  186. m_ViaDia = nc.board().via_stack().copper_layers().at( 0 ).size().x_nm();
  187. if( nc.board().via_stack().has_drill() )
  188. m_ViaDrill = nc.board().via_stack().drill().diameter().x_nm();
  189. }
  190. if( nc.board().has_color() )
  191. m_pcbColor = UnpackColor( nc.board().color() );
  192. if( nc.schematic().has_wire_width() )
  193. m_wireWidth = nc.schematic().wire_width().value_nm();
  194. if( nc.schematic().has_bus_width() )
  195. m_busWidth = nc.schematic().bus_width().value_nm();
  196. if( nc.schematic().has_color() )
  197. m_schematicColor = UnpackColor( nc.schematic().color() );
  198. // TODO(JE) resolve issues with moving to kicommon
  199. // if( nc.schematic().has_line_style() )
  200. // m_lineStyle = static_cast<int>( FromProtoEnum<LINE_STYLE>( nc.schematic().line_style() ) );
  201. return true;
  202. }
  203. const std::vector<NETCLASS*>& NETCLASS::GetConstituentNetclasses() const
  204. {
  205. return m_constituents;
  206. }
  207. void NETCLASS::SetConstituentNetclasses( std::vector<NETCLASS*>&& constituents )
  208. {
  209. m_constituents = std::move( constituents );
  210. }
  211. bool NETCLASS::ContainsNetclassWithName( const wxString& netclass ) const
  212. {
  213. return std::any_of( m_constituents.begin(), m_constituents.end(),
  214. [&netclass]( const NETCLASS* nc )
  215. {
  216. return nc->GetName() == netclass;
  217. } );
  218. }
  219. const wxString NETCLASS::GetHumanReadableName() const
  220. {
  221. if( m_constituents.size() == 1 )
  222. return m_Name;
  223. wxASSERT( m_constituents.size() >= 2 );
  224. wxString name;
  225. if( m_constituents.size() == 2 )
  226. {
  227. name.Printf( _( "%s and %s" ), m_constituents[0]->GetName(), m_constituents[1]->GetName() );
  228. }
  229. else if( m_constituents.size() == 3 )
  230. {
  231. name.Printf( _( "%s, %s and %s" ), m_constituents[0]->GetName(),
  232. m_constituents[1]->GetName(), m_constituents[2]->GetName() );
  233. }
  234. else if( m_constituents.size() > 3 )
  235. {
  236. name.Printf( _( "%s, %s and %d more" ), m_constituents[0]->GetName(),
  237. m_constituents[1]->GetName(), static_cast<int>( m_constituents.size() - 2 ) );
  238. }
  239. return name;
  240. }
  241. const wxString NETCLASS::GetName() const
  242. {
  243. if( m_constituents.size() == 1 )
  244. return m_Name;
  245. wxASSERT( m_constituents.size() >= 2 );
  246. wxString name = m_constituents[0]->m_Name;
  247. for( std::size_t i = 1; i < m_constituents.size(); ++i )
  248. {
  249. name += ",";
  250. name += m_constituents[i]->m_Name;
  251. }
  252. return name;
  253. }