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.

2743 lines
82 KiB

Make the new schematic and symbol library file formats the default. This is a very large and potentially disruptive change so this will be an unusually long and detailed commit message. The new file formats are now the default in both the schematic and symbol library editors. Existing symbol libraries will be saved in their current format until new features are added to library symbols. Once this happens, both the legacy schematic and symbol file formats will be no longer be savable and existing libraries will have to be converted. Saving to the legacy file formats is still available for round robin testing and should not be used for normal editing. When loading the legacy schematic file, it is imperative that the schematic library symbols are rescued and/or remapped to valid library identifiers. Otherwise, there will be no way to link to the original library symbol and the user will be required manually set the library identifier. The cached symbol will be saved in the schematic file so the last library symbol in the cache will still be used but there will be no way to update it from the original library. The next save after loading a legacy schematic file will be converted to the s-expression file format. Schematics with hierarchical sheets will automatically have all sheet file name extensions changed to .kicad_sym and saved to the new format as well. Appending schematics requires that the schematic to append has already been converted to the new file format. This is required to ensure that library symbols are guaranteed to be valid for the appended schematic. The schematic symbol library symbol link resolution has been moved out of the SCH_COMPONENT object and move into the SCH_SCREEN object that owns the symbol. This was done to ensure that there is a single place where the library symbol links get resolved rather than the dozen or so different code paths that previously existed. It also removes the necessity of the SCH_COMPONENT object of requiring any knowledge of the symbol library table and/or the cache library. When opening an s-expression schematic, the legacy cache library is not loaded so any library symbols not rescued cannot be loaded. Broken library symbol links will have to be manually resolved by adding the cache library to the symbol library table and changing the links in the schematic symbol. Now that the library symbols are embedded in the schematic file, the SCH_SCREEN object maintains the list of library symbols for the schematic automatically. No external manipulation of this library cache should ever occur. ADDED: S-expression schematic and symbol library file formats.
6 years ago
Make the new schematic and symbol library file formats the default. This is a very large and potentially disruptive change so this will be an unusually long and detailed commit message. The new file formats are now the default in both the schematic and symbol library editors. Existing symbol libraries will be saved in their current format until new features are added to library symbols. Once this happens, both the legacy schematic and symbol file formats will be no longer be savable and existing libraries will have to be converted. Saving to the legacy file formats is still available for round robin testing and should not be used for normal editing. When loading the legacy schematic file, it is imperative that the schematic library symbols are rescued and/or remapped to valid library identifiers. Otherwise, there will be no way to link to the original library symbol and the user will be required manually set the library identifier. The cached symbol will be saved in the schematic file so the last library symbol in the cache will still be used but there will be no way to update it from the original library. The next save after loading a legacy schematic file will be converted to the s-expression file format. Schematics with hierarchical sheets will automatically have all sheet file name extensions changed to .kicad_sym and saved to the new format as well. Appending schematics requires that the schematic to append has already been converted to the new file format. This is required to ensure that library symbols are guaranteed to be valid for the appended schematic. The schematic symbol library symbol link resolution has been moved out of the SCH_COMPONENT object and move into the SCH_SCREEN object that owns the symbol. This was done to ensure that there is a single place where the library symbol links get resolved rather than the dozen or so different code paths that previously existed. It also removes the necessity of the SCH_COMPONENT object of requiring any knowledge of the symbol library table and/or the cache library. When opening an s-expression schematic, the legacy cache library is not loaded so any library symbols not rescued cannot be loaded. Broken library symbol links will have to be manually resolved by adding the cache library to the symbol library table and changing the links in the schematic symbol. Now that the library symbols are embedded in the schematic file, the SCH_SCREEN object maintains the list of library symbols for the schematic automatically. No external manipulation of this library cache should ever occur. ADDED: S-expression schematic and symbol library file formats.
6 years ago
Make the new schematic and symbol library file formats the default. This is a very large and potentially disruptive change so this will be an unusually long and detailed commit message. The new file formats are now the default in both the schematic and symbol library editors. Existing symbol libraries will be saved in their current format until new features are added to library symbols. Once this happens, both the legacy schematic and symbol file formats will be no longer be savable and existing libraries will have to be converted. Saving to the legacy file formats is still available for round robin testing and should not be used for normal editing. When loading the legacy schematic file, it is imperative that the schematic library symbols are rescued and/or remapped to valid library identifiers. Otherwise, there will be no way to link to the original library symbol and the user will be required manually set the library identifier. The cached symbol will be saved in the schematic file so the last library symbol in the cache will still be used but there will be no way to update it from the original library. The next save after loading a legacy schematic file will be converted to the s-expression file format. Schematics with hierarchical sheets will automatically have all sheet file name extensions changed to .kicad_sym and saved to the new format as well. Appending schematics requires that the schematic to append has already been converted to the new file format. This is required to ensure that library symbols are guaranteed to be valid for the appended schematic. The schematic symbol library symbol link resolution has been moved out of the SCH_COMPONENT object and move into the SCH_SCREEN object that owns the symbol. This was done to ensure that there is a single place where the library symbol links get resolved rather than the dozen or so different code paths that previously existed. It also removes the necessity of the SCH_COMPONENT object of requiring any knowledge of the symbol library table and/or the cache library. When opening an s-expression schematic, the legacy cache library is not loaded so any library symbols not rescued cannot be loaded. Broken library symbol links will have to be manually resolved by adding the cache library to the symbol library table and changing the links in the schematic symbol. Now that the library symbols are embedded in the schematic file, the SCH_SCREEN object maintains the list of library symbols for the schematic automatically. No external manipulation of this library cache should ever occur. ADDED: S-expression schematic and symbol library file formats.
6 years ago
Make the new schematic and symbol library file formats the default. This is a very large and potentially disruptive change so this will be an unusually long and detailed commit message. The new file formats are now the default in both the schematic and symbol library editors. Existing symbol libraries will be saved in their current format until new features are added to library symbols. Once this happens, both the legacy schematic and symbol file formats will be no longer be savable and existing libraries will have to be converted. Saving to the legacy file formats is still available for round robin testing and should not be used for normal editing. When loading the legacy schematic file, it is imperative that the schematic library symbols are rescued and/or remapped to valid library identifiers. Otherwise, there will be no way to link to the original library symbol and the user will be required manually set the library identifier. The cached symbol will be saved in the schematic file so the last library symbol in the cache will still be used but there will be no way to update it from the original library. The next save after loading a legacy schematic file will be converted to the s-expression file format. Schematics with hierarchical sheets will automatically have all sheet file name extensions changed to .kicad_sym and saved to the new format as well. Appending schematics requires that the schematic to append has already been converted to the new file format. This is required to ensure that library symbols are guaranteed to be valid for the appended schematic. The schematic symbol library symbol link resolution has been moved out of the SCH_COMPONENT object and move into the SCH_SCREEN object that owns the symbol. This was done to ensure that there is a single place where the library symbol links get resolved rather than the dozen or so different code paths that previously existed. It also removes the necessity of the SCH_COMPONENT object of requiring any knowledge of the symbol library table and/or the cache library. When opening an s-expression schematic, the legacy cache library is not loaded so any library symbols not rescued cannot be loaded. Broken library symbol links will have to be manually resolved by adding the cache library to the symbol library table and changing the links in the schematic symbol. Now that the library symbols are embedded in the schematic file, the SCH_SCREEN object maintains the list of library symbols for the schematic automatically. No external manipulation of this library cache should ever occur. ADDED: S-expression schematic and symbol library file formats.
6 years ago
2 years ago
2 years ago
2 years ago
18 years ago
18 years ago
18 years ago
18 years ago
6 years ago
6 years ago
2 years ago
2 years ago
2 years ago
18 years ago
18 years ago
18 years ago
Make the new schematic and symbol library file formats the default. This is a very large and potentially disruptive change so this will be an unusually long and detailed commit message. The new file formats are now the default in both the schematic and symbol library editors. Existing symbol libraries will be saved in their current format until new features are added to library symbols. Once this happens, both the legacy schematic and symbol file formats will be no longer be savable and existing libraries will have to be converted. Saving to the legacy file formats is still available for round robin testing and should not be used for normal editing. When loading the legacy schematic file, it is imperative that the schematic library symbols are rescued and/or remapped to valid library identifiers. Otherwise, there will be no way to link to the original library symbol and the user will be required manually set the library identifier. The cached symbol will be saved in the schematic file so the last library symbol in the cache will still be used but there will be no way to update it from the original library. The next save after loading a legacy schematic file will be converted to the s-expression file format. Schematics with hierarchical sheets will automatically have all sheet file name extensions changed to .kicad_sym and saved to the new format as well. Appending schematics requires that the schematic to append has already been converted to the new file format. This is required to ensure that library symbols are guaranteed to be valid for the appended schematic. The schematic symbol library symbol link resolution has been moved out of the SCH_COMPONENT object and move into the SCH_SCREEN object that owns the symbol. This was done to ensure that there is a single place where the library symbol links get resolved rather than the dozen or so different code paths that previously existed. It also removes the necessity of the SCH_COMPONENT object of requiring any knowledge of the symbol library table and/or the cache library. When opening an s-expression schematic, the legacy cache library is not loaded so any library symbols not rescued cannot be loaded. Broken library symbol links will have to be manually resolved by adding the cache library to the symbol library table and changing the links in the schematic symbol. Now that the library symbols are embedded in the schematic file, the SCH_SCREEN object maintains the list of library symbols for the schematic automatically. No external manipulation of this library cache should ever occur. ADDED: S-expression schematic and symbol library file formats.
6 years ago
Make the new schematic and symbol library file formats the default. This is a very large and potentially disruptive change so this will be an unusually long and detailed commit message. The new file formats are now the default in both the schematic and symbol library editors. Existing symbol libraries will be saved in their current format until new features are added to library symbols. Once this happens, both the legacy schematic and symbol file formats will be no longer be savable and existing libraries will have to be converted. Saving to the legacy file formats is still available for round robin testing and should not be used for normal editing. When loading the legacy schematic file, it is imperative that the schematic library symbols are rescued and/or remapped to valid library identifiers. Otherwise, there will be no way to link to the original library symbol and the user will be required manually set the library identifier. The cached symbol will be saved in the schematic file so the last library symbol in the cache will still be used but there will be no way to update it from the original library. The next save after loading a legacy schematic file will be converted to the s-expression file format. Schematics with hierarchical sheets will automatically have all sheet file name extensions changed to .kicad_sym and saved to the new format as well. Appending schematics requires that the schematic to append has already been converted to the new file format. This is required to ensure that library symbols are guaranteed to be valid for the appended schematic. The schematic symbol library symbol link resolution has been moved out of the SCH_COMPONENT object and move into the SCH_SCREEN object that owns the symbol. This was done to ensure that there is a single place where the library symbol links get resolved rather than the dozen or so different code paths that previously existed. It also removes the necessity of the SCH_COMPONENT object of requiring any knowledge of the symbol library table and/or the cache library. When opening an s-expression schematic, the legacy cache library is not loaded so any library symbols not rescued cannot be loaded. Broken library symbol links will have to be manually resolved by adding the cache library to the symbol library table and changing the links in the schematic symbol. Now that the library symbols are embedded in the schematic file, the SCH_SCREEN object maintains the list of library symbols for the schematic automatically. No external manipulation of this library cache should ever occur. ADDED: S-expression schematic and symbol library file formats.
6 years ago
18 years ago
18 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2024 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <sch_edit_frame.h>
  25. #include <widgets/msgpanel.h>
  26. #include <bitmaps.h>
  27. #include <core/mirror.h>
  28. #include <sch_shape.h>
  29. #include <pgm_base.h>
  30. #include <sim/sim_model.h>
  31. #include <sim/spice_generator.h>
  32. #include <sim/sim_lib_mgr.h>
  33. #include <trace_helpers.h>
  34. #include <trigo.h>
  35. #include <refdes_utils.h>
  36. #include <wx/log.h>
  37. #include <settings/settings_manager.h>
  38. #include <sch_plotter.h>
  39. #include <string_utils.h>
  40. #include <utility>
  41. std::unordered_map<TRANSFORM, int> SCH_SYMBOL::s_transformToOrientationCache;
  42. /**
  43. * Convert a wxString to UTF8 and replace any control characters with a ~, where a control
  44. * character is one of the first ASCII values up to ' ' 32d.
  45. */
  46. std::string toUTFTildaText( const wxString& txt )
  47. {
  48. std::string ret = TO_UTF8( txt );
  49. for( char& c : ret )
  50. {
  51. if( (unsigned char) c <= ' ' )
  52. c = '~';
  53. }
  54. return ret;
  55. }
  56. SCH_SYMBOL::SCH_SYMBOL() : SCH_SYMBOL( *LIB_SYMBOL::Dummy(), LIB_ID(), nullptr, 0 ) { }
  57. SCH_SYMBOL::SCH_SYMBOL( const LIB_SYMBOL& aSymbol, const LIB_ID& aLibId,
  58. const SCH_SHEET_PATH* aSheet, int aUnit, int aBodyStyle,
  59. const VECTOR2I& aPosition, EDA_ITEM* aParent ) :
  60. SYMBOL( aParent, SCH_SYMBOL_T )
  61. {
  62. SetLibSymbol( &aSymbol );
  63. Init( aPosition );
  64. m_unit = aUnit;
  65. m_bodyStyle = aBodyStyle;
  66. m_lib_id = aLibId;
  67. // Copy fields from the library symbol
  68. UpdateFields( aSheet,
  69. true, /* update style */
  70. false, /* update ref */
  71. false, /* update other fields */
  72. true, /* reset ref */
  73. true /* reset other fields */ );
  74. m_prefix = UTIL::GetRefDesPrefix( m_part->GetReferenceField().GetText() );
  75. if( aSheet )
  76. SetRef( aSheet, UTIL::GetRefDesUnannotated( m_prefix ) );
  77. // Inherit the include in bill of materials and board netlist settings from flattened
  78. // library symbol.
  79. m_excludedFromSim = m_part->GetExcludedFromSim();
  80. m_excludedFromBOM = m_part->GetExcludedFromBOM();
  81. m_excludedFromBoard = m_part->GetExcludedFromBoard();
  82. }
  83. SCH_SYMBOL::SCH_SYMBOL( const LIB_SYMBOL& aSymbol, const SCH_SHEET_PATH* aSheet,
  84. const PICKED_SYMBOL& aSel, const VECTOR2I& aPosition, EDA_ITEM* aParent ) :
  85. SCH_SYMBOL( aSymbol, aSel.LibId, aSheet, aSel.Unit, aSel.Convert, aPosition, aParent )
  86. {
  87. // Set any fields that were modified as part of the symbol selection
  88. for( const std::pair<int, wxString>& i : aSel.Fields )
  89. {
  90. if( i.first == REFERENCE_FIELD )
  91. SetRef( aSheet, i.second );
  92. else if( SCH_FIELD* field = GetFieldById( i.first ) )
  93. field->SetText( i.second );
  94. }
  95. }
  96. SCH_SYMBOL::SCH_SYMBOL( const SCH_SYMBOL& aSymbol ) :
  97. SYMBOL( aSymbol )
  98. {
  99. SetLibSymbol( &aSymbol.GetLibSymbolRef() );
  100. m_parent = aSymbol.m_parent;
  101. m_pos = aSymbol.m_pos;
  102. m_unit = aSymbol.m_unit;
  103. m_bodyStyle = aSymbol.m_bodyStyle;
  104. m_lib_id = aSymbol.m_lib_id;
  105. m_isInNetlist = aSymbol.m_isInNetlist;
  106. m_DNP = aSymbol.m_DNP;
  107. const_cast<KIID&>( m_Uuid ) = aSymbol.m_Uuid;
  108. m_transform = aSymbol.m_transform;
  109. m_prefix = aSymbol.m_prefix;
  110. m_instanceReferences = aSymbol.m_instanceReferences;
  111. m_fields = aSymbol.m_fields;
  112. // Re-parent the fields, which before this had aSymbol as parent
  113. for( SCH_FIELD& field : m_fields )
  114. field.SetParent( this );
  115. m_pins.clear();
  116. // Copy (and re-parent) the pins
  117. for( const std::unique_ptr<SCH_PIN>& pin : aSymbol.m_pins )
  118. {
  119. m_pins.emplace_back( std::make_unique<SCH_PIN>( *pin ) );
  120. m_pins.back()->SetParent( this );
  121. }
  122. m_fieldsAutoplaced = aSymbol.m_fieldsAutoplaced;
  123. m_schLibSymbolName = aSymbol.m_schLibSymbolName;
  124. }
  125. SCH_SYMBOL::~SCH_SYMBOL()
  126. {
  127. }
  128. void SCH_SYMBOL::Init( const VECTOR2I& pos )
  129. {
  130. m_layer = LAYER_DEVICE;
  131. m_pos = pos;
  132. m_unit = 1; // In multi unit chip - which unit to draw.
  133. m_bodyStyle = BODY_STYLE::BASE; // De Morgan Handling
  134. // The rotation/mirror transformation matrix. pos normal
  135. m_transform = TRANSFORM();
  136. // construct only the mandatory fields, which are the first 4 only.
  137. for( int i = 0; i < MANDATORY_FIELDS; ++i )
  138. {
  139. m_fields.emplace_back( pos, i, this, TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) );
  140. if( i == REFERENCE_FIELD )
  141. m_fields.back().SetLayer( LAYER_REFERENCEPART );
  142. else if( i == VALUE_FIELD )
  143. m_fields.back().SetLayer( LAYER_VALUEPART );
  144. else
  145. m_fields.back().SetLayer( LAYER_FIELDS );
  146. }
  147. m_prefix = wxString( wxT( "U" ) );
  148. m_isInNetlist = true;
  149. }
  150. EDA_ITEM* SCH_SYMBOL::Clone() const
  151. {
  152. return new SCH_SYMBOL( *this );
  153. }
  154. bool SCH_SYMBOL::IsMovableFromAnchorPoint() const
  155. {
  156. // If a symbol's anchor is not grid-aligned to its pins then moving from the anchor is
  157. // going to end up moving the symbol's pins off-grid.
  158. // The minimal grid size allowed to place a pin is 25 mils
  159. const int min_grid_size = schIUScale.MilsToIU( 25 );
  160. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  161. {
  162. if( ( ( pin->GetPosition().x - m_pos.x ) % min_grid_size ) != 0 )
  163. return false;
  164. if( ( ( pin->GetPosition().y - m_pos.y ) % min_grid_size ) != 0 )
  165. return false;
  166. }
  167. return true;
  168. }
  169. void SCH_SYMBOL::SetLibId( const LIB_ID& aLibId )
  170. {
  171. m_lib_id = aLibId;
  172. }
  173. wxString SCH_SYMBOL::GetSchSymbolLibraryName() const
  174. {
  175. if( !m_schLibSymbolName.IsEmpty() )
  176. return m_schLibSymbolName;
  177. else
  178. return m_lib_id.Format();
  179. }
  180. void SCH_SYMBOL::SetLibSymbol( const LIB_SYMBOL* aLibSymbol )
  181. {
  182. wxASSERT_MSG( aLibSymbol, wxT( "SCH_SYMBOL::SetLibSymbol() called with NULL pointer" ) );
  183. m_part.reset( aLibSymbol->Flatten().release() );
  184. m_part->SetParent();
  185. UpdatePins();
  186. }
  187. wxString SCH_SYMBOL::GetDescription() const
  188. {
  189. return m_part->GetDescription();
  190. }
  191. wxString SCH_SYMBOL::GetKeyWords() const
  192. {
  193. return m_part->GetKeyWords();
  194. }
  195. wxString SCH_SYMBOL::GetDatasheet() const
  196. {
  197. return m_part->GetDatasheetField().GetText();
  198. }
  199. void SCH_SYMBOL::UpdatePins()
  200. {
  201. std::map<wxString, wxString> altPinMap;
  202. std::map<wxString, std::set<SCH_PIN*>> pinUuidMap;
  203. std::set<SCH_PIN*> unassignedSchPins;
  204. std::set<SCH_PIN*> unassignedLibPins;
  205. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  206. {
  207. pinUuidMap[ pin->GetNumber() ].insert( pin.get() );
  208. unassignedSchPins.insert( pin.get() );
  209. if( !pin->GetAlt().IsEmpty() )
  210. altPinMap[ pin->GetNumber() ] = pin->GetAlt();
  211. pin->SetLibPin( nullptr );
  212. }
  213. m_pinMap.clear();
  214. for( SCH_PIN* libPin : m_part->GetAllLibPins() )
  215. {
  216. // NW: Don't filter by unit: this data-structure is used for all instances,
  217. // some of which might have different units.
  218. if( libPin->GetBodyStyle() && m_bodyStyle && m_bodyStyle != libPin->GetBodyStyle() )
  219. continue;
  220. SCH_PIN* pin = nullptr;
  221. auto ii = pinUuidMap.find( libPin->GetNumber() );
  222. if( ii == pinUuidMap.end() || ii->second.empty() )
  223. {
  224. unassignedLibPins.insert( libPin );
  225. continue;
  226. }
  227. auto it = ii->second.begin();
  228. pin = *it;
  229. ii->second.erase( it );
  230. pin->SetLibPin( libPin );
  231. pin->SetPosition( libPin->GetPosition() );
  232. unassignedSchPins.erase( pin );
  233. auto iii = altPinMap.find( libPin->GetNumber() );
  234. if( iii != altPinMap.end() )
  235. pin->SetAlt( iii->second );
  236. m_pinMap[ libPin ] = pin;
  237. }
  238. // Add any pins that were not found in the symbol
  239. for( SCH_PIN* libPin : unassignedLibPins )
  240. {
  241. SCH_PIN* pin = nullptr;
  242. // First try to re-use an existing pin
  243. if( !unassignedSchPins.empty() )
  244. {
  245. auto it = unassignedSchPins.begin();
  246. pin = *it;
  247. unassignedSchPins.erase( it );
  248. }
  249. else
  250. {
  251. // This is a pin that was not found in the symbol, so create a new one.
  252. pin = m_pins.emplace_back( std::make_unique<SCH_PIN>( SCH_PIN( this, libPin ) ) ).get();
  253. }
  254. m_pinMap[ libPin ] = pin;
  255. pin->SetLibPin( libPin );
  256. pin->SetPosition( libPin->GetPosition() );
  257. pin->SetNumber( libPin->GetNumber() );
  258. auto iii = altPinMap.find( libPin->GetNumber() );
  259. if( iii != altPinMap.end() )
  260. pin->SetAlt( iii->second );
  261. }
  262. // If we have any pins left in the symbol that were not found in the library, remove them.
  263. for( auto it1 = m_pins.begin(); it1 != m_pins.end() && !unassignedSchPins.empty(); )
  264. {
  265. auto it2 = unassignedSchPins.find( it1->get() );
  266. if( it2 != unassignedSchPins.end() )
  267. {
  268. it1 = m_pins.erase( it1 );
  269. unassignedSchPins.erase( it2 );
  270. }
  271. else
  272. {
  273. ++it1;
  274. }
  275. }
  276. // If the symbol is selected, then its pins are selected.
  277. if( IsSelected() )
  278. {
  279. for( std::unique_ptr<SCH_PIN>& pin : m_pins )
  280. pin->SetSelected();
  281. }
  282. }
  283. void SCH_SYMBOL::SetBodyStyle( int aBodyStyle )
  284. {
  285. if( HasAlternateBodyStyle() && m_bodyStyle != aBodyStyle )
  286. {
  287. m_bodyStyle = ( m_bodyStyle == BODY_STYLE::BASE ) ? BODY_STYLE::DEMORGAN
  288. : BODY_STYLE::BASE;
  289. // The body style may have a different pin layout so the update the pin map.
  290. UpdatePins();
  291. }
  292. }
  293. bool SCH_SYMBOL::HasAlternateBodyStyle() const
  294. {
  295. return m_part->HasAlternateBodyStyle();
  296. }
  297. void SCH_SYMBOL::SetTransform( const TRANSFORM& aTransform )
  298. {
  299. m_transform = aTransform;
  300. }
  301. int SCH_SYMBOL::GetUnitCount() const
  302. {
  303. return m_part->GetUnitCount();
  304. }
  305. wxString SCH_SYMBOL::GetUnitDisplayName( int aUnit ) const
  306. {
  307. return m_part->GetUnitDisplayName( aUnit );
  308. }
  309. bool SCH_SYMBOL::HasUnitDisplayName( int aUnit ) const
  310. {
  311. return m_part->HasUnitDisplayName( aUnit );
  312. }
  313. void SCH_SYMBOL::PrintBackground( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle,
  314. const VECTOR2I& aOffset, bool aDimmed )
  315. {
  316. SCH_RENDER_SETTINGS localRenderSettings( *aSettings );
  317. localRenderSettings.m_Transform = m_transform;
  318. localRenderSettings.m_ShowVisibleFields = false;
  319. localRenderSettings.m_ShowHiddenFields = false;
  320. if( GetDNP() )
  321. aDimmed = true;
  322. m_part->PrintBackground( &localRenderSettings, m_unit, m_bodyStyle, m_pos + aOffset, aDimmed );
  323. }
  324. void SCH_SYMBOL::Print( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle,
  325. const VECTOR2I& aOffset, bool aForceNoFill, bool aDimmed )
  326. {
  327. SCH_RENDER_SETTINGS localRenderSettings( *aSettings );
  328. localRenderSettings.m_Transform = m_transform;
  329. localRenderSettings.m_ShowVisibleFields = false;
  330. localRenderSettings.m_ShowHiddenFields = false;
  331. if( m_DNP )
  332. aDimmed = true;
  333. std::vector<SCH_PIN*> libPins = m_part->GetPins( m_unit, m_bodyStyle );
  334. LIB_SYMBOL tempSymbol( *m_part );
  335. std::vector<SCH_PIN*> tempPins = tempSymbol.GetPins( m_unit, m_bodyStyle );
  336. // Copy the pin info from the symbol to the temp pins
  337. for( unsigned i = 0; i < tempPins.size(); ++ i )
  338. {
  339. SCH_PIN* symbolPin = GetPin( libPins[ i ] );
  340. SCH_PIN* tempPin = tempPins[ i ];
  341. tempPin->SetName( symbolPin->GetShownName() );
  342. tempPin->SetType( symbolPin->GetType() );
  343. tempPin->SetShape( symbolPin->GetShape() );
  344. }
  345. for( SCH_ITEM& item : tempSymbol.GetDrawItems() )
  346. {
  347. if( EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( &item ) )
  348. {
  349. // Use SCH_FIELD's text resolver
  350. SCH_FIELD dummy( this, -1 );
  351. dummy.SetText( text->GetText() );
  352. text->SetText( dummy.GetShownText( false ) );
  353. }
  354. }
  355. tempSymbol.Print( &localRenderSettings, m_unit, m_bodyStyle, m_pos + aOffset, false,
  356. aDimmed );
  357. for( SCH_FIELD& field : m_fields )
  358. field.Print( &localRenderSettings, m_unit, m_bodyStyle, aOffset, aForceNoFill, aDimmed );
  359. if( m_DNP )
  360. {
  361. wxDC* DC = localRenderSettings.GetPrintDC();
  362. BOX2I bbox = GetBodyBoundingBox();
  363. BOX2I pins = GetBodyAndPinsBoundingBox();
  364. COLOR4D dnp_color = localRenderSettings.GetLayerColor( LAYER_DNP_MARKER );
  365. VECTOR2D margins( std::max( bbox.GetX() - pins.GetX(), pins.GetEnd().x - bbox.GetEnd().x ),
  366. std::max( bbox.GetY() - pins.GetY(), pins.GetEnd().y - bbox.GetEnd().y ) );
  367. margins.x = std::max( margins.x * 0.6, margins.y * 0.3 );
  368. margins.y = std::max( margins.y * 0.6, margins.x * 0.3 );
  369. bbox.Inflate( KiROUND( margins.x ), KiROUND( margins.y ) );
  370. GRFilledSegment( DC, bbox.GetOrigin(), bbox.GetEnd(),
  371. 3.0 * schIUScale.MilsToIU( DEFAULT_LINE_WIDTH_MILS ),
  372. dnp_color );
  373. GRFilledSegment( DC, bbox.GetOrigin() + VECTOR2I( bbox.GetWidth(), 0 ),
  374. bbox.GetOrigin() + VECTOR2I( 0, bbox.GetHeight() ),
  375. 3.0 * schIUScale.MilsToIU( DEFAULT_LINE_WIDTH_MILS ),
  376. dnp_color );
  377. }
  378. }
  379. bool SCH_SYMBOL::GetInstance( SCH_SYMBOL_INSTANCE& aInstance,
  380. const KIID_PATH& aSheetPath, bool aTestFromEnd ) const
  381. {
  382. for( const SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  383. {
  384. if( !aTestFromEnd )
  385. {
  386. if( instance.m_Path == aSheetPath )
  387. {
  388. aInstance = instance;
  389. return true;
  390. }
  391. }
  392. else if( instance.m_Path.EndsWith( aSheetPath ) )
  393. {
  394. aInstance = instance;
  395. return true;
  396. }
  397. }
  398. return false;
  399. }
  400. void SCH_SYMBOL::RemoveInstance( const SCH_SHEET_PATH& aInstancePath )
  401. {
  402. RemoveInstance( aInstancePath.Path() );
  403. }
  404. void SCH_SYMBOL::RemoveInstance( const KIID_PATH& aInstancePath )
  405. {
  406. // Search for an existing path and remove it if found (should not occur)
  407. for( unsigned ii = 0; ii < m_instanceReferences.size(); ii++ )
  408. {
  409. if( m_instanceReferences[ii].m_Path == aInstancePath )
  410. {
  411. wxLogTrace( traceSchSheetPaths, wxS( "Removing symbol instance:\n"
  412. " sheet path %s\n"
  413. " reference %s, unit %d from symbol %s." ),
  414. aInstancePath.AsString(),
  415. m_instanceReferences[ii].m_Reference,
  416. m_instanceReferences[ii].m_Unit,
  417. m_Uuid.AsString() );
  418. m_instanceReferences.erase( m_instanceReferences.begin() + ii );
  419. ii--;
  420. }
  421. }
  422. }
  423. void SCH_SYMBOL::AddHierarchicalReference( const KIID_PATH& aPath, const wxString& aRef, int aUnit )
  424. {
  425. // Search for an existing path and remove it if found (should not occur)
  426. for( unsigned ii = 0; ii < m_instanceReferences.size(); ii++ )
  427. {
  428. if( m_instanceReferences[ii].m_Path == aPath )
  429. {
  430. wxLogTrace( traceSchSheetPaths, wxS( "Removing symbol instance:\n"
  431. " sheet path %s\n"
  432. " reference %s, unit %d from symbol %s." ),
  433. aPath.AsString(),
  434. m_instanceReferences[ii].m_Reference,
  435. m_instanceReferences[ii].m_Unit,
  436. m_Uuid.AsString() );
  437. m_instanceReferences.erase( m_instanceReferences.begin() + ii );
  438. ii--;
  439. }
  440. }
  441. SCH_SYMBOL_INSTANCE instance;
  442. instance.m_Path = aPath;
  443. instance.m_Reference = aRef;
  444. instance.m_Unit = aUnit;
  445. wxLogTrace( traceSchSheetPaths, wxS( "Adding symbol '%s' instance:\n"
  446. " sheet path '%s'\n"
  447. " reference '%s'\n"
  448. " unit %d\n" ),
  449. m_Uuid.AsString(),
  450. aPath.AsString(),
  451. aRef,
  452. aUnit );
  453. m_instanceReferences.push_back( instance );
  454. // This should set the default instance to the first saved instance data for each symbol
  455. // when importing sheets.
  456. if( m_instanceReferences.size() == 1 )
  457. {
  458. m_fields[ REFERENCE_FIELD ].SetText( aRef );
  459. m_unit = aUnit;
  460. }
  461. }
  462. void SCH_SYMBOL::AddHierarchicalReference( const SCH_SYMBOL_INSTANCE& aInstance )
  463. {
  464. KIID_PATH searchPath( aInstance.m_Path );
  465. std::vector<SCH_SYMBOL_INSTANCE>::iterator resultIt;
  466. do
  467. {
  468. resultIt = std::find_if( m_instanceReferences.begin(), m_instanceReferences.end(),
  469. [searchPath]( const auto& it )
  470. {
  471. return it.m_Path == searchPath;
  472. } );
  473. if( resultIt != m_instanceReferences.end() )
  474. {
  475. wxLogTrace( traceSchSheetPaths, wxS( "Removing symbol instance:\n"
  476. " sheet path %s\n"
  477. " reference %s, unit %d from symbol %s." ),
  478. aInstance.m_Path.AsString(),
  479. resultIt->m_Reference,
  480. resultIt->m_Unit,
  481. m_Uuid.AsString() );
  482. // Instance data should be unique by path. Double check just in case there was
  483. // some buggy code in the past.
  484. resultIt = m_instanceReferences.erase( resultIt );
  485. }
  486. }
  487. while( resultIt != m_instanceReferences.end() );
  488. SCH_SYMBOL_INSTANCE instance = aInstance;
  489. wxLogTrace( traceSchSheetPaths, wxS( "Adding symbol '%s' instance:\n"
  490. " sheet path '%s'\n"
  491. " reference '%s'\n"
  492. " unit %d\n" ),
  493. m_Uuid.AsString(),
  494. instance.m_Path.AsString(),
  495. instance.m_Reference,
  496. instance.m_Unit );
  497. m_instanceReferences.push_back( instance );
  498. // This should set the default instance to the first saved instance data for each symbol
  499. // when importing sheets.
  500. if( m_instanceReferences.size() == 1 )
  501. {
  502. m_fields[ REFERENCE_FIELD ].SetText( instance.m_Reference );
  503. m_unit = instance.m_Unit;
  504. }
  505. }
  506. const wxString SCH_SYMBOL::GetRef( const SCH_SHEET_PATH* sheet, bool aIncludeUnit ) const
  507. {
  508. KIID_PATH path = sheet->Path();
  509. wxString ref;
  510. wxString subRef;
  511. for( const SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  512. {
  513. if( instance.m_Path == path )
  514. {
  515. ref = instance.m_Reference;
  516. subRef = SubReference( instance.m_Unit );
  517. break;
  518. }
  519. }
  520. // If it was not found in m_Paths array, then see if it is in m_Field[REFERENCE] -- if so,
  521. // use this as a default for this path. This will happen if we load a version 1 schematic
  522. // file. It will also mean that multiple instances of the same sheet by default all have
  523. // the same symbol references, but perhaps this is best.
  524. if( ref.IsEmpty() && !GetField( REFERENCE_FIELD )->GetText().IsEmpty() )
  525. ref = GetField( REFERENCE_FIELD )->GetText();
  526. if( ref.IsEmpty() )
  527. ref = UTIL::GetRefDesUnannotated( m_prefix );
  528. if( aIncludeUnit && GetUnitCount() > 1 )
  529. ref += subRef;
  530. return ref;
  531. }
  532. bool SCH_SYMBOL::IsReferenceStringValid( const wxString& aReferenceString )
  533. {
  534. return !UTIL::GetRefDesPrefix( aReferenceString ).IsEmpty();
  535. }
  536. void SCH_SYMBOL::SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref )
  537. {
  538. KIID_PATH path = sheet->Path();
  539. bool found = false;
  540. // check to see if it is already there before inserting it
  541. for( SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  542. {
  543. if( instance.m_Path == path )
  544. {
  545. found = true;
  546. instance.m_Reference = ref;
  547. break;
  548. }
  549. }
  550. if( !found )
  551. AddHierarchicalReference( path, ref, m_unit );
  552. for( std::unique_ptr<SCH_PIN>& pin : m_pins )
  553. pin->ClearDefaultNetName( sheet );
  554. if( Schematic() && *sheet == Schematic()->CurrentSheet() )
  555. m_fields[ REFERENCE_FIELD ].SetText( ref );
  556. // Reinit the m_prefix member if needed
  557. m_prefix = UTIL::GetRefDesPrefix( ref );
  558. if( m_prefix.IsEmpty() )
  559. m_prefix = wxT( "U" );
  560. // Power symbols have references starting with # and are not included in netlists
  561. m_isInNetlist = ! ref.StartsWith( wxT( "#" ) );
  562. }
  563. bool SCH_SYMBOL::IsAnnotated( const SCH_SHEET_PATH* aSheet ) const
  564. {
  565. KIID_PATH path = aSheet->Path();
  566. for( const SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  567. {
  568. if( instance.m_Path == path )
  569. return instance.m_Reference.Last() != '?';
  570. }
  571. return false;
  572. }
  573. void SCH_SYMBOL::UpdatePrefix()
  574. {
  575. wxString refDesignator = GetField( REFERENCE_FIELD )->GetText();
  576. refDesignator.Replace( "~", " " );
  577. wxString prefix = refDesignator;
  578. while( prefix.Length() )
  579. {
  580. wxUniCharRef last = prefix.Last();
  581. if( ( last >= '0' && last <= '9' ) || last == '?' || last == '*' )
  582. prefix.RemoveLast();
  583. else
  584. break;
  585. }
  586. // Avoid a prefix containing trailing/leading spaces
  587. prefix.Trim( true );
  588. prefix.Trim( false );
  589. if( !prefix.IsEmpty() )
  590. SetPrefix( prefix );
  591. }
  592. wxString SCH_SYMBOL::SubReference( int aUnit, bool aAddSeparator ) const
  593. {
  594. if( SCHEMATIC* schematic = Schematic() )
  595. return schematic->Settings().SubReference( aUnit, aAddSeparator );
  596. return LIB_SYMBOL::LetterSubReference( aUnit, 'A' );
  597. }
  598. int SCH_SYMBOL::GetUnitSelection( const SCH_SHEET_PATH* aSheet ) const
  599. {
  600. KIID_PATH path = aSheet->Path();
  601. for( const SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  602. {
  603. if( instance.m_Path == path )
  604. return instance.m_Unit;
  605. }
  606. // If it was not found in m_Paths array, then use m_unit. This will happen if we load a
  607. // version 1 schematic file.
  608. return m_unit;
  609. }
  610. void SCH_SYMBOL::SetUnitSelection( const SCH_SHEET_PATH* aSheet, int aUnitSelection )
  611. {
  612. KIID_PATH path = aSheet->Path();
  613. // check to see if it is already there before inserting it
  614. for( SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  615. {
  616. if( instance.m_Path == path )
  617. {
  618. instance.m_Unit = aUnitSelection;
  619. return;
  620. }
  621. }
  622. // didn't find it; better add it
  623. AddHierarchicalReference( path, UTIL::GetRefDesUnannotated( m_prefix ), aUnitSelection );
  624. }
  625. void SCH_SYMBOL::SetUnitSelection( int aUnitSelection )
  626. {
  627. for( SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  628. instance.m_Unit = aUnitSelection;
  629. }
  630. const wxString SCH_SYMBOL::GetValue( bool aResolve, const SCH_SHEET_PATH* aPath,
  631. bool aAllowExtraText ) const
  632. {
  633. if( aResolve )
  634. return GetField( VALUE_FIELD )->GetShownText( aPath, aAllowExtraText );
  635. return GetField( VALUE_FIELD )->GetText();
  636. }
  637. void SCH_SYMBOL::SetValueFieldText( const wxString& aValue )
  638. {
  639. m_fields[ VALUE_FIELD ].SetText( aValue );
  640. }
  641. const wxString SCH_SYMBOL::GetFootprintFieldText( bool aResolve, const SCH_SHEET_PATH* aPath,
  642. bool aAllowExtraText ) const
  643. {
  644. if( aResolve )
  645. return GetField( FOOTPRINT_FIELD )->GetShownText( aPath, aAllowExtraText );
  646. return GetField( FOOTPRINT_FIELD )->GetText();
  647. }
  648. void SCH_SYMBOL::SetFootprintFieldText( const wxString& aFootprint )
  649. {
  650. m_fields[ FOOTPRINT_FIELD ].SetText( aFootprint );
  651. }
  652. SCH_FIELD* SCH_SYMBOL::GetField( MANDATORY_FIELD_T aFieldType )
  653. {
  654. return &m_fields[aFieldType];
  655. }
  656. const SCH_FIELD* SCH_SYMBOL::GetField( MANDATORY_FIELD_T aFieldType ) const
  657. {
  658. return &m_fields[aFieldType];
  659. }
  660. SCH_FIELD* SCH_SYMBOL::GetFieldById( int aFieldId )
  661. {
  662. for( SCH_FIELD& field : m_fields )
  663. {
  664. if( field.GetId() == aFieldId )
  665. return &field;
  666. }
  667. return nullptr;
  668. }
  669. SCH_FIELD* SCH_SYMBOL::GetFieldByName( const wxString& aFieldName )
  670. {
  671. for( SCH_FIELD& field : m_fields )
  672. {
  673. if( field.GetName() == aFieldName )
  674. return &field;
  675. }
  676. return nullptr;
  677. }
  678. const SCH_FIELD* SCH_SYMBOL::GetFieldByName( const wxString& aFieldName ) const
  679. {
  680. for( const SCH_FIELD& field : m_fields )
  681. {
  682. if( field.GetName() == aFieldName )
  683. return &field;
  684. }
  685. return nullptr;
  686. }
  687. void SCH_SYMBOL::GetFields( std::vector<SCH_FIELD*>& aVector, bool aVisibleOnly )
  688. {
  689. for( SCH_FIELD& field : m_fields )
  690. {
  691. if( aVisibleOnly )
  692. {
  693. if( !field.IsVisible() || field.GetText().IsEmpty() )
  694. continue;
  695. }
  696. aVector.push_back( &field );
  697. }
  698. }
  699. SCH_FIELD* SCH_SYMBOL::AddField( const SCH_FIELD& aField )
  700. {
  701. m_fields.push_back( aField );
  702. return &m_fields.back();
  703. }
  704. void SCH_SYMBOL::RemoveField( const wxString& aFieldName )
  705. {
  706. for( unsigned i = MANDATORY_FIELDS; i < m_fields.size(); ++i )
  707. {
  708. if( aFieldName == m_fields[i].GetName( false ) )
  709. {
  710. m_fields.erase( m_fields.begin() + i );
  711. return;
  712. }
  713. }
  714. }
  715. SCH_FIELD* SCH_SYMBOL::FindField( const wxString& aFieldName, bool aIncludeDefaultFields,
  716. bool aCaseInsensitive )
  717. {
  718. unsigned start = aIncludeDefaultFields ? 0 : MANDATORY_FIELDS;
  719. for( unsigned i = start; i < m_fields.size(); ++i )
  720. {
  721. if( aCaseInsensitive )
  722. {
  723. if( aFieldName.Upper() == m_fields[i].GetName( false ).Upper() )
  724. return &m_fields[i];
  725. }
  726. else
  727. {
  728. if( aFieldName == m_fields[i].GetName( false ) )
  729. return &m_fields[i];
  730. }
  731. }
  732. return nullptr;
  733. }
  734. void SCH_SYMBOL::UpdateFields( const SCH_SHEET_PATH* aPath, bool aUpdateStyle, bool aUpdateRef,
  735. bool aUpdateOtherFields, bool aResetRef, bool aResetOtherFields )
  736. {
  737. std::vector<SCH_FIELD*> fields;
  738. m_part->GetFields( fields );
  739. for( const SCH_FIELD* libField : fields )
  740. {
  741. int id = libField->GetId();
  742. SCH_FIELD* schField;
  743. if( libField->IsMandatory() )
  744. {
  745. schField = GetFieldById( id );
  746. }
  747. else
  748. {
  749. schField = FindField( libField->GetCanonicalName() );
  750. if( !schField )
  751. {
  752. wxString fieldName = libField->GetCanonicalName();
  753. SCH_FIELD newField( VECTOR2I( 0, 0 ), GetFieldCount(), this, fieldName );
  754. schField = AddField( newField );
  755. }
  756. }
  757. if( aUpdateStyle )
  758. {
  759. schField->ImportValues( *libField );
  760. schField->SetTextPos( m_pos + libField->GetTextPos() );
  761. }
  762. if( id == REFERENCE_FIELD && aPath )
  763. {
  764. if( aResetRef )
  765. SetRef( aPath, m_part->GetReferenceField().GetText() );
  766. else if( aUpdateRef )
  767. SetRef( aPath, libField->GetText() );
  768. }
  769. else if( id == VALUE_FIELD )
  770. {
  771. SetValueFieldText( UnescapeString( libField->GetText() ) );
  772. }
  773. else if( id == FOOTPRINT_FIELD )
  774. {
  775. if( aResetOtherFields || aUpdateOtherFields )
  776. SetFootprintFieldText( libField->GetText() );
  777. }
  778. else if( id == DATASHEET_FIELD )
  779. {
  780. if( aResetOtherFields )
  781. schField->SetText( GetDatasheet() ); // alias-specific value
  782. else if( aUpdateOtherFields )
  783. schField->SetText( libField->GetText() );
  784. }
  785. else
  786. {
  787. if( aResetOtherFields || aUpdateOtherFields )
  788. schField->SetText( libField->GetText() );
  789. }
  790. }
  791. }
  792. void SCH_SYMBOL::RunOnChildren( const std::function<void( SCH_ITEM* )>& aFunction )
  793. {
  794. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  795. aFunction( pin.get() );
  796. for( SCH_FIELD& field : m_fields )
  797. aFunction( &field );
  798. }
  799. SCH_PIN* SCH_SYMBOL::GetPin( const wxString& aNumber ) const
  800. {
  801. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  802. {
  803. if( pin->GetNumber() == aNumber )
  804. return pin.get();
  805. }
  806. return nullptr;
  807. }
  808. const SCH_PIN* SCH_SYMBOL::GetPin( const VECTOR2I& aPos ) const
  809. {
  810. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  811. {
  812. int pin_unit = pin->GetLibPin() ? pin->GetLibPin()->GetUnit()
  813. : GetUnit();
  814. int pin_bodyStyle = pin->GetLibPin() ? pin->GetLibPin()->GetBodyStyle()
  815. : GetBodyStyle();
  816. if( pin_unit > 0 && pin_unit != GetUnit() )
  817. continue;
  818. if( pin_bodyStyle > 0 && pin_bodyStyle != GetBodyStyle() )
  819. continue;
  820. if( pin->IsPointClickableAnchor( aPos ) )
  821. return pin.get();
  822. }
  823. return nullptr;
  824. }
  825. std::vector<SCH_PIN*> SCH_SYMBOL::GetLibPins() const
  826. {
  827. return m_part->GetPins( m_unit, m_bodyStyle );
  828. }
  829. std::vector<SCH_PIN*> SCH_SYMBOL::GetAllLibPins() const
  830. {
  831. return m_part->GetAllLibPins();
  832. }
  833. size_t SCH_SYMBOL::GetFullPinCount() const
  834. {
  835. return m_part ? m_part->GetPinCount() : 0;
  836. }
  837. SCH_PIN* SCH_SYMBOL::GetPin( SCH_PIN* aLibPin ) const
  838. {
  839. auto it = m_pinMap.find( aLibPin );
  840. if( it != m_pinMap.end() )
  841. return it->second;
  842. wxFAIL_MSG_AT( "Pin not found", __FILE__, __LINE__, __FUNCTION__ );
  843. return nullptr;
  844. }
  845. std::vector<SCH_PIN*> SCH_SYMBOL::GetPins( const SCH_SHEET_PATH* aSheet ) const
  846. {
  847. std::vector<SCH_PIN*> pins;
  848. if( aSheet == nullptr )
  849. {
  850. wxCHECK_MSG( Schematic(), pins, "Can't call GetPins on a symbol with no schematic" );
  851. aSheet = &Schematic()->CurrentSheet();
  852. }
  853. int unit = GetUnitSelection( aSheet );
  854. for( const std::unique_ptr<SCH_PIN>& p : m_pins )
  855. {
  856. if( unit && p->GetLibPin() && p->GetLibPin()->GetUnit()
  857. && ( p->GetLibPin()->GetUnit() != unit ) )
  858. {
  859. continue;
  860. }
  861. pins.push_back( p.get() );
  862. }
  863. return pins;
  864. }
  865. void SCH_SYMBOL::SwapData( SCH_ITEM* aItem )
  866. {
  867. SCH_ITEM::SwapFlags( aItem );
  868. wxCHECK_RET( aItem != nullptr && aItem->Type() == SCH_SYMBOL_T,
  869. wxT( "Cannot swap data with invalid symbol." ) );
  870. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( aItem );
  871. std::swap( m_lib_id, symbol->m_lib_id );
  872. m_pins.swap( symbol->m_pins ); // std::vector's swap()
  873. for( std::unique_ptr<SCH_PIN>& pin : symbol->m_pins )
  874. pin->SetParent( symbol );
  875. for( std::unique_ptr<SCH_PIN>& pin : m_pins )
  876. pin->SetParent( this );
  877. LIB_SYMBOL* libSymbol = symbol->m_part.release();
  878. symbol->m_part = std::move( m_part );
  879. symbol->UpdatePins();
  880. m_part.reset( libSymbol );
  881. UpdatePins();
  882. std::swap( m_pos, symbol->m_pos );
  883. std::swap( m_unit, symbol->m_unit );
  884. std::swap( m_bodyStyle, symbol->m_bodyStyle );
  885. m_fields.swap( symbol->m_fields ); // std::vector's swap()
  886. for( SCH_FIELD& field : symbol->m_fields )
  887. field.SetParent( symbol );
  888. for( SCH_FIELD& field : m_fields )
  889. field.SetParent( this );
  890. TRANSFORM tmp = m_transform;
  891. m_transform = symbol->m_transform;
  892. symbol->m_transform = tmp;
  893. std::swap( m_excludedFromSim, symbol->m_excludedFromSim );
  894. std::swap( m_excludedFromBOM, symbol->m_excludedFromBOM );
  895. std::swap( m_DNP, symbol->m_DNP );
  896. std::swap( m_excludedFromBoard, symbol->m_excludedFromBoard );
  897. std::swap( m_instanceReferences, symbol->m_instanceReferences );
  898. std::swap( m_schLibSymbolName, symbol->m_schLibSymbolName );
  899. }
  900. void SCH_SYMBOL::GetContextualTextVars( wxArrayString* aVars ) const
  901. {
  902. for( int i = 0; i < MANDATORY_FIELDS; ++i )
  903. aVars->push_back( m_fields[i].GetCanonicalName().Upper() );
  904. for( size_t i = MANDATORY_FIELDS; i < m_fields.size(); ++i )
  905. aVars->push_back( m_fields[i].GetName() );
  906. aVars->push_back( wxT( "OP" ) );
  907. aVars->push_back( wxT( "FOOTPRINT_LIBRARY" ) );
  908. aVars->push_back( wxT( "FOOTPRINT_NAME" ) );
  909. aVars->push_back( wxT( "UNIT" ) );
  910. aVars->push_back( wxT( "SYMBOL_LIBRARY" ) );
  911. aVars->push_back( wxT( "SYMBOL_NAME" ) );
  912. aVars->push_back( wxT( "SYMBOL_DESCRIPTION" ) );
  913. aVars->push_back( wxT( "SYMBOL_KEYWORDS" ) );
  914. aVars->push_back( wxT( "EXCLUDE_FROM_BOM" ) );
  915. aVars->push_back( wxT( "EXCLUDE_FROM_BOARD" ) );
  916. aVars->push_back( wxT( "EXCLUDE_FROM_SIM" ) );
  917. aVars->push_back( wxT( "DNP" ) );
  918. aVars->push_back( wxT( "SHORT_NET_NAME(<pin_number>)" ) );
  919. aVars->push_back( wxT( "NET_NAME(<pin_number>)" ) );
  920. aVars->push_back( wxT( "NET_CLASS(<pin_number>)" ) );
  921. aVars->push_back( wxT( "PIN_NAME(<pin_number>)" ) );
  922. }
  923. bool SCH_SYMBOL::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, int aDepth ) const
  924. {
  925. static wxRegEx operatingPoint( wxT( "^"
  926. "OP"
  927. "(:[^.]*)?" // pin
  928. "(.([0-9])?([a-zA-Z]*))?" // format
  929. "$" ) );
  930. wxCHECK( aPath, false );
  931. SCHEMATIC* schematic = Schematic();
  932. if( !schematic )
  933. return false;
  934. if( operatingPoint.Matches( *token ) )
  935. {
  936. wxString pin( operatingPoint.GetMatch( *token, 1 ).Lower() );
  937. wxString precisionStr( operatingPoint.GetMatch( *token, 3 ) );
  938. wxString range( operatingPoint.GetMatch( *token, 4 ) );
  939. int precision = 3;
  940. if( !precisionStr.IsEmpty() )
  941. precision = precisionStr[0] - '0';
  942. if( range.IsEmpty() )
  943. range = wxS( "~A" );
  944. SIM_LIB_MGR simLibMgr( &schematic->Prj() );
  945. NULL_REPORTER devnull;
  946. SIM_MODEL& model = simLibMgr.CreateModel( aPath, const_cast<SCH_SYMBOL&>( *this ),
  947. devnull ).model;
  948. SPICE_ITEM spiceItem;
  949. spiceItem.refName = GetRef( aPath );
  950. wxString spiceRef = model.SpiceGenerator().ItemName( spiceItem );
  951. spiceRef = spiceRef.Lower();
  952. if( pin.IsEmpty() )
  953. {
  954. *token = schematic->GetOperatingPoint( spiceRef, precision, range );
  955. return true;
  956. }
  957. else if( pin == wxS( ":power" ) )
  958. {
  959. if( range.IsEmpty() )
  960. range = wxS( "~W" );
  961. *token = schematic->GetOperatingPoint( spiceRef + wxS( ":power" ), precision, range );
  962. return true;
  963. }
  964. else
  965. {
  966. pin = pin.SubString( 1, -1 ); // Strip ':' from front
  967. for( const std::reference_wrapper<const SIM_MODEL_PIN>& modelPin : model.GetPins() )
  968. {
  969. SCH_PIN* symbolPin = GetPin( modelPin.get().symbolPinNumber );
  970. if( pin == symbolPin->GetName().Lower() || pin == symbolPin->GetNumber().Lower() )
  971. {
  972. if( model.GetPins().size() == 2 )
  973. {
  974. *token = schematic->GetOperatingPoint( spiceRef, precision, range );
  975. }
  976. else
  977. {
  978. wxString signalName = spiceRef + wxS( ":" ) + modelPin.get().modelPinName;
  979. *token = schematic->GetOperatingPoint( signalName, precision, range );
  980. }
  981. return true;
  982. }
  983. }
  984. }
  985. *token = wxS( "?" );
  986. return true;
  987. }
  988. if( token->Contains( ':' ) )
  989. {
  990. if( schematic->ResolveCrossReference( token, aDepth + 1 ) )
  991. return true;
  992. }
  993. for( int i = 0; i < MANDATORY_FIELDS; ++i )
  994. {
  995. if( token->IsSameAs( m_fields[ i ].GetCanonicalName().Upper() ) )
  996. {
  997. if( i == REFERENCE_FIELD )
  998. *token = GetRef( aPath, true );
  999. else
  1000. *token = m_fields[ i ].GetShownText( aPath, false, aDepth + 1 );
  1001. return true;
  1002. }
  1003. }
  1004. for( size_t i = MANDATORY_FIELDS; i < m_fields.size(); ++i )
  1005. {
  1006. if( token->IsSameAs( m_fields[ i ].GetName() )
  1007. || token->IsSameAs( m_fields[ i ].GetName().Upper() ) )
  1008. {
  1009. *token = m_fields[ i ].GetShownText( aPath, false, aDepth + 1 );
  1010. return true;
  1011. }
  1012. }
  1013. // Consider missing simulation fields as empty, not un-resolved
  1014. if( token->IsSameAs( wxT( "SIM.DEVICE" ) )
  1015. || token->IsSameAs( wxT( "SIM.TYPE" ) )
  1016. || token->IsSameAs( wxT( "SIM.PINS" ) )
  1017. || token->IsSameAs( wxT( "SIM.PARAMS" ) )
  1018. || token->IsSameAs( wxT( "SIM.LIBRARY" ) )
  1019. || token->IsSameAs( wxT( "SIM.NAME" ) ) )
  1020. {
  1021. *token = wxEmptyString;
  1022. return true;
  1023. }
  1024. for( const TEMPLATE_FIELDNAME& templateFieldname :
  1025. schematic->Settings().m_TemplateFieldNames.GetTemplateFieldNames() )
  1026. {
  1027. if( token->IsSameAs( templateFieldname.m_Name )
  1028. || token->IsSameAs( templateFieldname.m_Name.Upper() ) )
  1029. {
  1030. // If we didn't find it in the fields list then it isn't set on this symbol.
  1031. // Just return an empty string.
  1032. *token = wxEmptyString;
  1033. return true;
  1034. }
  1035. }
  1036. if( token->IsSameAs( wxT( "FOOTPRINT_LIBRARY" ) ) )
  1037. {
  1038. wxString footprint = GetFootprintFieldText( true, aPath, false );
  1039. wxArrayString parts = wxSplit( footprint, ':' );
  1040. if( parts.Count() > 0 )
  1041. *token = parts[ 0 ];
  1042. else
  1043. *token = wxEmptyString;
  1044. return true;
  1045. }
  1046. else if( token->IsSameAs( wxT( "FOOTPRINT_NAME" ) ) )
  1047. {
  1048. wxString footprint = GetFootprintFieldText( true, aPath, false );
  1049. wxArrayString parts = wxSplit( footprint, ':' );
  1050. if( parts.Count() > 1 )
  1051. *token = parts[ std::min( 1, (int) parts.size() - 1 ) ];
  1052. else
  1053. *token = wxEmptyString;
  1054. return true;
  1055. }
  1056. else if( token->IsSameAs( wxT( "UNIT" ) ) )
  1057. {
  1058. *token = SubReference( GetUnitSelection( aPath ) );
  1059. return true;
  1060. }
  1061. else if( token->IsSameAs( wxT( "SYMBOL_LIBRARY" ) ) )
  1062. {
  1063. *token = m_lib_id.GetUniStringLibNickname();
  1064. return true;
  1065. }
  1066. else if( token->IsSameAs( wxT( "SYMBOL_NAME" ) ) )
  1067. {
  1068. *token = m_lib_id.GetUniStringLibItemName();
  1069. return true;
  1070. }
  1071. else if( token->IsSameAs( wxT( "SYMBOL_DESCRIPTION" ) ) )
  1072. {
  1073. *token = GetDescription();
  1074. return true;
  1075. }
  1076. else if( token->IsSameAs( wxT( "SYMBOL_KEYWORDS" ) ) )
  1077. {
  1078. *token = GetKeyWords();
  1079. return true;
  1080. }
  1081. else if( token->IsSameAs( wxT( "EXCLUDE_FROM_BOM" ) ) )
  1082. {
  1083. *token = wxEmptyString;
  1084. if( aPath->GetExcludedFromBOM() || this->GetExcludedFromBOM() )
  1085. *token = _( "Excluded from BOM" );
  1086. return true;
  1087. }
  1088. else if( token->IsSameAs( wxT( "EXCLUDE_FROM_BOARD" ) ) )
  1089. {
  1090. *token = wxEmptyString;
  1091. if( aPath->GetExcludedFromBoard() || this->GetExcludedFromBoard() )
  1092. *token = _( "Excluded from board" );
  1093. return true;
  1094. }
  1095. else if( token->IsSameAs( wxT( "EXCLUDE_FROM_SIM" ) ) )
  1096. {
  1097. *token = wxEmptyString;
  1098. if( aPath->GetExcludedFromSim() || this->GetExcludedFromSim() )
  1099. *token = _( "Excluded from simulation" );
  1100. return true;
  1101. }
  1102. else if( token->IsSameAs( wxT( "DNP" ) ) )
  1103. {
  1104. *token = wxEmptyString;
  1105. if( aPath->GetDNP() || this->GetDNP() )
  1106. *token = _( "DNP" );
  1107. return true;
  1108. }
  1109. else if( token->StartsWith( wxT( "SHORT_NET_NAME(" ) )
  1110. || token->StartsWith( wxT( "NET_NAME(" ) )
  1111. || token->StartsWith( wxT( "NET_CLASS(" ) )
  1112. || token->StartsWith( wxT( "PIN_NAME(" ) ) )
  1113. {
  1114. wxString pinNumber = token->AfterFirst( '(' );
  1115. pinNumber = pinNumber.BeforeLast( ')' );
  1116. for( SCH_PIN* pin : GetPins( aPath ) )
  1117. {
  1118. if( pin->GetNumber() == pinNumber )
  1119. {
  1120. if( token->StartsWith( wxT( "PIN_NAME" ) ) )
  1121. {
  1122. *token = pin->GetAlt().IsEmpty() ? pin->GetName() : pin->GetAlt();
  1123. return true;
  1124. }
  1125. SCH_CONNECTION* conn = pin->Connection( aPath );
  1126. if( !conn )
  1127. *token = wxEmptyString;
  1128. else if( token->StartsWith( wxT( "SHORT_NET_NAME" ) ) )
  1129. *token = conn->LocalName();
  1130. else if( token->StartsWith( wxT( "NET_NAME" ) ) )
  1131. *token = conn->Name();
  1132. else if( token->StartsWith( wxT( "NET_CLASS" ) ) )
  1133. *token = pin->GetEffectiveNetClass( aPath )->GetName();
  1134. return true;
  1135. }
  1136. }
  1137. }
  1138. // See if parent can resolve it (this will recurse to ancestors)
  1139. if( aPath->Last() && aPath->Last()->ResolveTextVar( aPath, token, aDepth + 1 ) )
  1140. return true;
  1141. return false;
  1142. }
  1143. void SCH_SYMBOL::ClearAnnotation( const SCH_SHEET_PATH* aSheetPath, bool aResetPrefix )
  1144. {
  1145. if( aSheetPath )
  1146. {
  1147. KIID_PATH path = aSheetPath->Path();
  1148. for( SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  1149. {
  1150. if( instance.m_Path == path )
  1151. {
  1152. if( instance.m_Reference.IsEmpty() || aResetPrefix )
  1153. instance.m_Reference = UTIL::GetRefDesUnannotated( m_prefix );
  1154. else
  1155. instance.m_Reference = UTIL::GetRefDesUnannotated( instance.m_Reference );
  1156. }
  1157. }
  1158. }
  1159. else
  1160. {
  1161. for( SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  1162. {
  1163. if( instance.m_Reference.IsEmpty() || aResetPrefix)
  1164. instance.m_Reference = UTIL::GetRefDesUnannotated( m_prefix );
  1165. else
  1166. instance.m_Reference = UTIL::GetRefDesUnannotated( instance.m_Reference );
  1167. }
  1168. }
  1169. for( std::unique_ptr<SCH_PIN>& pin : m_pins )
  1170. pin->ClearDefaultNetName( aSheetPath );
  1171. // These 2 changes do not work in complex hierarchy.
  1172. // When a clear annotation is made, the calling function must call a
  1173. // UpdateAllScreenReferences for the active sheet.
  1174. // But this call cannot made here.
  1175. wxString currentReference = m_fields[REFERENCE_FIELD].GetText();
  1176. if( currentReference.IsEmpty() || aResetPrefix )
  1177. m_fields[REFERENCE_FIELD].SetText( UTIL::GetRefDesUnannotated( m_prefix ) );
  1178. else
  1179. m_fields[REFERENCE_FIELD].SetText( UTIL::GetRefDesUnannotated( currentReference ) );
  1180. }
  1181. bool SCH_SYMBOL::AddSheetPathReferenceEntryIfMissing( const KIID_PATH& aSheetPath )
  1182. {
  1183. // An empty sheet path is illegal, at a minimum the root sheet UUID must be present.
  1184. wxCHECK( aSheetPath.size() > 0, false );
  1185. for( const SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  1186. {
  1187. // if aSheetPath is found, nothing to do:
  1188. if( instance.m_Path == aSheetPath )
  1189. return false;
  1190. }
  1191. // This entry does not exist: add it, with its last-used reference
  1192. AddHierarchicalReference( aSheetPath, m_fields[REFERENCE_FIELD].GetText(), m_unit );
  1193. return true;
  1194. }
  1195. void SCH_SYMBOL::SetOrientation( int aOrientation )
  1196. {
  1197. TRANSFORM temp = TRANSFORM();
  1198. bool transform = false;
  1199. switch( aOrientation )
  1200. {
  1201. case SYM_ORIENT_0:
  1202. case SYM_NORMAL: // default transform matrix
  1203. m_transform = TRANSFORM();
  1204. break;
  1205. case SYM_ROTATE_COUNTERCLOCKWISE: // Rotate + (incremental rotation)
  1206. temp.x1 = 0;
  1207. temp.y1 = 1;
  1208. temp.x2 = -1;
  1209. temp.y2 = 0;
  1210. transform = true;
  1211. break;
  1212. case SYM_ROTATE_CLOCKWISE: // Rotate - (incremental rotation)
  1213. temp.x1 = 0;
  1214. temp.y1 = -1;
  1215. temp.x2 = 1;
  1216. temp.y2 = 0;
  1217. transform = true;
  1218. break;
  1219. case SYM_MIRROR_Y: // Mirror Y (incremental transform)
  1220. temp.x1 = -1;
  1221. temp.y1 = 0;
  1222. temp.x2 = 0;
  1223. temp.y2 = 1;
  1224. transform = true;
  1225. break;
  1226. case SYM_MIRROR_X: // Mirror X (incremental transform)
  1227. temp.x1 = 1;
  1228. temp.y1 = 0;
  1229. temp.x2 = 0;
  1230. temp.y2 = -1;
  1231. transform = true;
  1232. break;
  1233. case SYM_ORIENT_90:
  1234. SetOrientation( SYM_ORIENT_0 );
  1235. SetOrientation( SYM_ROTATE_COUNTERCLOCKWISE );
  1236. break;
  1237. case SYM_ORIENT_180:
  1238. SetOrientation( SYM_ORIENT_0 );
  1239. SetOrientation( SYM_ROTATE_COUNTERCLOCKWISE );
  1240. SetOrientation( SYM_ROTATE_COUNTERCLOCKWISE );
  1241. break;
  1242. case SYM_ORIENT_270:
  1243. SetOrientation( SYM_ORIENT_0 );
  1244. SetOrientation( SYM_ROTATE_CLOCKWISE );
  1245. break;
  1246. case ( SYM_ORIENT_0 + SYM_MIRROR_X ):
  1247. SetOrientation( SYM_ORIENT_0 );
  1248. SetOrientation( SYM_MIRROR_X );
  1249. break;
  1250. case ( SYM_ORIENT_0 + SYM_MIRROR_Y ):
  1251. SetOrientation( SYM_ORIENT_0 );
  1252. SetOrientation( SYM_MIRROR_Y );
  1253. break;
  1254. case ( SYM_ORIENT_0 + SYM_MIRROR_X + SYM_MIRROR_Y ):
  1255. SetOrientation( SYM_ORIENT_0 );
  1256. SetOrientation( SYM_MIRROR_X );
  1257. SetOrientation( SYM_MIRROR_Y );
  1258. break;
  1259. case ( SYM_ORIENT_90 + SYM_MIRROR_X ):
  1260. SetOrientation( SYM_ORIENT_90 );
  1261. SetOrientation( SYM_MIRROR_X );
  1262. break;
  1263. case ( SYM_ORIENT_90 + SYM_MIRROR_Y ):
  1264. SetOrientation( SYM_ORIENT_90 );
  1265. SetOrientation( SYM_MIRROR_Y );
  1266. break;
  1267. case ( SYM_ORIENT_90 + SYM_MIRROR_X + SYM_MIRROR_Y ):
  1268. SetOrientation( SYM_ORIENT_90 );
  1269. SetOrientation( SYM_MIRROR_X );
  1270. SetOrientation( SYM_MIRROR_Y );
  1271. break;
  1272. case ( SYM_ORIENT_180 + SYM_MIRROR_X ):
  1273. SetOrientation( SYM_ORIENT_180 );
  1274. SetOrientation( SYM_MIRROR_X );
  1275. break;
  1276. case ( SYM_ORIENT_180 + SYM_MIRROR_Y ):
  1277. SetOrientation( SYM_ORIENT_180 );
  1278. SetOrientation( SYM_MIRROR_Y );
  1279. break;
  1280. case ( SYM_ORIENT_180 + SYM_MIRROR_X + SYM_MIRROR_Y ):
  1281. SetOrientation( SYM_ORIENT_180 );
  1282. SetOrientation( SYM_MIRROR_X );
  1283. SetOrientation( SYM_MIRROR_Y );
  1284. break;
  1285. case ( SYM_ORIENT_270 + SYM_MIRROR_X ):
  1286. SetOrientation( SYM_ORIENT_270 );
  1287. SetOrientation( SYM_MIRROR_X );
  1288. break;
  1289. case ( SYM_ORIENT_270 + SYM_MIRROR_Y ):
  1290. SetOrientation( SYM_ORIENT_270 );
  1291. SetOrientation( SYM_MIRROR_Y );
  1292. break;
  1293. case ( SYM_ORIENT_270 + SYM_MIRROR_X + SYM_MIRROR_Y ):
  1294. SetOrientation( SYM_ORIENT_270 );
  1295. SetOrientation( SYM_MIRROR_X );
  1296. SetOrientation( SYM_MIRROR_Y );
  1297. break;
  1298. default:
  1299. transform = false;
  1300. wxFAIL_MSG( "Invalid schematic symbol orientation type." );
  1301. break;
  1302. }
  1303. if( transform )
  1304. {
  1305. /* The new matrix transform is the old matrix transform modified by the
  1306. * requested transformation, which is the temp transform (rot,
  1307. * mirror ..) in order to have (in term of matrix transform):
  1308. * transform coord = new_m_transform * coord
  1309. * where transform coord is the coord modified by new_m_transform from
  1310. * the initial value coord.
  1311. * new_m_transform is computed (from old_m_transform and temp) to
  1312. * have:
  1313. * transform coord = old_m_transform * temp
  1314. */
  1315. TRANSFORM newTransform;
  1316. newTransform.x1 = m_transform.x1 * temp.x1 + m_transform.x2 * temp.y1;
  1317. newTransform.y1 = m_transform.y1 * temp.x1 + m_transform.y2 * temp.y1;
  1318. newTransform.x2 = m_transform.x1 * temp.x2 + m_transform.x2 * temp.y2;
  1319. newTransform.y2 = m_transform.y1 * temp.x2 + m_transform.y2 * temp.y2;
  1320. m_transform = newTransform;
  1321. }
  1322. }
  1323. int SCH_SYMBOL::GetOrientation() const
  1324. {
  1325. /*
  1326. * This is slow, but also a bizarre algorithm. I don't feel like unteasing the algorithm right
  1327. * now, so let's just cache it for the moment.
  1328. */
  1329. if( s_transformToOrientationCache.count( m_transform ) )
  1330. return s_transformToOrientationCache.at( m_transform );
  1331. int rotate_values[] =
  1332. {
  1333. SYM_ORIENT_0,
  1334. SYM_ORIENT_90,
  1335. SYM_ORIENT_180,
  1336. SYM_ORIENT_270,
  1337. SYM_MIRROR_X + SYM_ORIENT_0,
  1338. SYM_MIRROR_X + SYM_ORIENT_90,
  1339. SYM_MIRROR_X + SYM_ORIENT_270,
  1340. SYM_MIRROR_Y,
  1341. SYM_MIRROR_Y + SYM_ORIENT_0,
  1342. SYM_MIRROR_Y + SYM_ORIENT_90,
  1343. SYM_MIRROR_Y + SYM_ORIENT_180,
  1344. SYM_MIRROR_Y + SYM_ORIENT_270
  1345. };
  1346. // Try to find the current transform option:
  1347. TRANSFORM transform = m_transform;
  1348. SCH_SYMBOL temp( *this );
  1349. for( int type_rotate : rotate_values )
  1350. {
  1351. temp.SetOrientation( type_rotate );
  1352. if( transform == temp.GetTransform() )
  1353. {
  1354. s_transformToOrientationCache[m_transform] = type_rotate;
  1355. return type_rotate;
  1356. }
  1357. }
  1358. // Error: orientation not found in list (should not happen)
  1359. wxFAIL_MSG( "Schematic symbol orientation matrix internal error." );
  1360. return SYM_NORMAL;
  1361. }
  1362. #if defined(DEBUG)
  1363. void SCH_SYMBOL::Show( int nestLevel, std::ostream& os ) const
  1364. {
  1365. // for now, make it look like XML:
  1366. NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str()
  1367. << " ref=\"" << TO_UTF8( GetField( REFERENCE_FIELD )->GetName() )
  1368. << '"' << " chipName=\""
  1369. << GetLibId().Format().wx_str() << '"' << m_pos
  1370. << " layer=\"" << m_layer
  1371. << '"' << ">\n";
  1372. // skip the reference, it's been output already.
  1373. for( int i = 1; i < GetFieldCount(); ++i )
  1374. {
  1375. const wxString& value = GetFields()[i].GetText();
  1376. if( !value.IsEmpty() )
  1377. {
  1378. NestedSpace( nestLevel + 1, os ) << "<field" << " name=\""
  1379. << TO_UTF8( GetFields()[i].GetName() )
  1380. << '"' << " value=\""
  1381. << TO_UTF8( value ) << "\"/>\n";
  1382. }
  1383. }
  1384. NestedSpace( nestLevel, os ) << "</" << TO_UTF8( GetClass().Lower() ) << ">\n";
  1385. }
  1386. #endif
  1387. BOX2I SCH_SYMBOL::doGetBoundingBox( bool aIncludePins, bool aIncludeFields ) const
  1388. {
  1389. BOX2I bBox;
  1390. bBox = m_part->GetBodyBoundingBox( m_unit, m_bodyStyle, aIncludePins, false );
  1391. bBox = m_transform.TransformCoordinate( bBox );
  1392. bBox.Normalize();
  1393. bBox.Offset( m_pos );
  1394. if( aIncludeFields )
  1395. {
  1396. for( const SCH_FIELD& field : m_fields )
  1397. {
  1398. if( field.IsVisible() )
  1399. bBox.Merge( field.GetBoundingBox() );
  1400. }
  1401. }
  1402. return bBox;
  1403. }
  1404. BOX2I SCH_SYMBOL::GetBodyBoundingBox() const
  1405. {
  1406. try
  1407. {
  1408. return doGetBoundingBox( false, false );
  1409. }
  1410. catch( const boost::bad_pointer& exc )
  1411. {
  1412. // This may be overkill and could be an assertion but we are more likely to
  1413. // find any boost pointer container errors this way.
  1414. wxLogError( wxT( "Boost bad pointer exception '%s' occurred." ), exc.what() );
  1415. return BOX2I();
  1416. }
  1417. }
  1418. BOX2I SCH_SYMBOL::GetBodyAndPinsBoundingBox() const
  1419. {
  1420. return doGetBoundingBox( true, false );
  1421. }
  1422. const BOX2I SCH_SYMBOL::GetBoundingBox() const
  1423. {
  1424. return doGetBoundingBox( true, true );
  1425. }
  1426. void SCH_SYMBOL::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  1427. {
  1428. wxString msg;
  1429. SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( aFrame );
  1430. SCH_SHEET_PATH* currentSheet = schframe ? &schframe->GetCurrentSheet() : nullptr;
  1431. auto addExcludes =
  1432. [&]()
  1433. {
  1434. wxArrayString msgs;
  1435. if( GetExcludedFromSim() )
  1436. msgs.Add( _( "Simulation" ) );
  1437. if( GetExcludedFromBOM() )
  1438. msgs.Add( _( "BOM" ) );
  1439. if( GetExcludedFromBoard() )
  1440. msgs.Add( _( "Board" ) );
  1441. if( GetDNP() )
  1442. msgs.Add( _( "DNP" ) );
  1443. msg = wxJoin( msgs, '|' );
  1444. msg.Replace( '|', wxS( ", " ) );
  1445. if( !msg.empty() )
  1446. aList.emplace_back( _( "Exclude from" ), msg );
  1447. };
  1448. if( !m_part->IsDummy() )
  1449. {
  1450. if( m_part->IsPower() )
  1451. {
  1452. // Don't use GetShownText(); we want to see the variable references here
  1453. aList.emplace_back( _( "Power symbol" ),
  1454. KIUI::EllipsizeStatusText( aFrame, GetField( VALUE_FIELD )->GetText() ) );
  1455. }
  1456. else
  1457. {
  1458. aList.emplace_back( _( "Reference" ),
  1459. UnescapeString( GetRef( currentSheet ) ) );
  1460. // Don't use GetShownText(); we want to see the variable references here
  1461. aList.emplace_back( _( "Value" ),
  1462. KIUI::EllipsizeStatusText( aFrame, GetField( VALUE_FIELD )->GetText() ) );
  1463. addExcludes();
  1464. aList.emplace_back( _( "Name" ),
  1465. KIUI::EllipsizeStatusText( aFrame, GetLibId().GetLibItemName() ) );
  1466. }
  1467. #if 0 // Display symbol flags, for debug only
  1468. aList.emplace_back( _( "flags" ), wxString::Format( "%X", GetEditFlags() ) );
  1469. #endif
  1470. if( !m_part->IsRoot() )
  1471. {
  1472. msg = _( "Missing parent" );
  1473. std::shared_ptr< LIB_SYMBOL > parent = m_part->GetParent().lock();
  1474. if( parent )
  1475. msg = parent->GetName();
  1476. aList.emplace_back( _( "Derived from" ), UnescapeString( msg ) );
  1477. }
  1478. else if( !m_lib_id.GetLibNickname().empty() )
  1479. {
  1480. aList.emplace_back( _( "Library" ), m_lib_id.GetLibNickname() );
  1481. }
  1482. else
  1483. {
  1484. aList.emplace_back( _( "Library" ), _( "Undefined!!!" ) );
  1485. }
  1486. // Display the current associated footprint, if exists.
  1487. // Don't use GetShownText(); we want to see the variable references here
  1488. msg = KIUI::EllipsizeStatusText( aFrame, GetField( FOOTPRINT_FIELD )->GetText() );
  1489. if( msg.IsEmpty() )
  1490. msg = _( "<Unknown>" );
  1491. aList.emplace_back( _( "Footprint" ), msg );
  1492. // Display description of the symbol, and keywords found in lib
  1493. aList.emplace_back( _( "Description" ) + wxT( ": " )
  1494. + GetField( DESCRIPTION_FIELD )->GetText(),
  1495. _( "Keywords" ) + wxT( ": " ) + m_part->GetKeyWords() );
  1496. }
  1497. }
  1498. BITMAPS SCH_SYMBOL::GetMenuImage() const
  1499. {
  1500. return BITMAPS::add_component;
  1501. }
  1502. void SCH_SYMBOL::MirrorHorizontally( int aCenter )
  1503. {
  1504. int dx = m_pos.x;
  1505. SetOrientation( SYM_MIRROR_Y );
  1506. MIRROR( m_pos.x, aCenter );
  1507. dx -= m_pos.x; // dx,0 is the move vector for this transform
  1508. for( SCH_FIELD& field : m_fields )
  1509. {
  1510. // Move the fields to the new position because the symbol itself has moved.
  1511. VECTOR2I pos = field.GetTextPos();
  1512. pos.x -= dx;
  1513. field.SetTextPos( pos );
  1514. }
  1515. }
  1516. void SCH_SYMBOL::MirrorVertically( int aCenter )
  1517. {
  1518. int dy = m_pos.y;
  1519. SetOrientation( SYM_MIRROR_X );
  1520. MIRROR( m_pos.y, aCenter );
  1521. dy -= m_pos.y; // 0,dy is the move vector for this transform
  1522. for( SCH_FIELD& field : m_fields )
  1523. {
  1524. // Move the fields to the new position because the symbol itself has moved.
  1525. VECTOR2I pos = field.GetTextPos();
  1526. pos.y -= dy;
  1527. field.SetTextPos( pos );
  1528. }
  1529. }
  1530. void SCH_SYMBOL::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
  1531. {
  1532. VECTOR2I prev = m_pos;
  1533. RotatePoint( m_pos, aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
  1534. SetOrientation( aRotateCCW ? SYM_ROTATE_COUNTERCLOCKWISE : SYM_ROTATE_CLOCKWISE );
  1535. for( SCH_FIELD& field : m_fields )
  1536. {
  1537. // Move the fields to the new position because the symbol itself has moved.
  1538. VECTOR2I pos = field.GetTextPos();
  1539. pos.x -= prev.x - m_pos.x;
  1540. pos.y -= prev.y - m_pos.y;
  1541. field.SetTextPos( pos );
  1542. }
  1543. }
  1544. bool SCH_SYMBOL::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const
  1545. {
  1546. // Symbols are searchable via the child field and pin item text.
  1547. return false;
  1548. }
  1549. void SCH_SYMBOL::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
  1550. {
  1551. for( std::unique_ptr<SCH_PIN>& pin : m_pins )
  1552. {
  1553. SCH_PIN* lib_pin = pin->GetLibPin();
  1554. if( lib_pin && lib_pin->GetUnit() && m_unit && ( m_unit != lib_pin->GetUnit() ) )
  1555. continue;
  1556. DANGLING_END_ITEM item( PIN_END, lib_pin, GetPinPhysicalPosition( lib_pin ), this );
  1557. aItemList.push_back( item );
  1558. }
  1559. }
  1560. bool SCH_SYMBOL::UpdateDanglingState( std::vector<DANGLING_END_ITEM>& aItemListByType,
  1561. std::vector<DANGLING_END_ITEM>& aItemListByPos,
  1562. const SCH_SHEET_PATH* aPath )
  1563. {
  1564. bool changed = false;
  1565. for( std::unique_ptr<SCH_PIN>& pin : m_pins )
  1566. {
  1567. bool previousState = pin->IsDangling();
  1568. pin->SetIsDangling( true );
  1569. VECTOR2I pos = m_transform.TransformCoordinate( pin->GetLocalPosition() ) + m_pos;
  1570. auto lower = DANGLING_END_ITEM_HELPER::get_lower_pos( aItemListByPos, pos );
  1571. bool do_break = false;
  1572. for( auto it = lower; it < aItemListByPos.end() && it->GetPosition() == pos; it++ )
  1573. {
  1574. DANGLING_END_ITEM& each_item = *it;
  1575. // Some people like to stack pins on top of each other in a symbol to indicate
  1576. // internal connection. While technically connected, it is not particularly useful
  1577. // to display them that way, so skip any pins that are in the same symbol as this
  1578. // one.
  1579. if( each_item.GetParent() == this )
  1580. continue;
  1581. switch( each_item.GetType() )
  1582. {
  1583. case PIN_END:
  1584. case LABEL_END:
  1585. case SHEET_LABEL_END:
  1586. case WIRE_END:
  1587. case NO_CONNECT_END:
  1588. case JUNCTION_END:
  1589. pin->SetIsDangling( false );
  1590. do_break = true;
  1591. break;
  1592. default:
  1593. break;
  1594. }
  1595. if( do_break )
  1596. break;
  1597. }
  1598. changed = ( changed || ( previousState != pin->IsDangling() ) );
  1599. }
  1600. return changed;
  1601. }
  1602. VECTOR2I SCH_SYMBOL::GetPinPhysicalPosition( const SCH_PIN* Pin ) const
  1603. {
  1604. wxCHECK_MSG( Pin != nullptr && Pin->Type() == SCH_PIN_T, VECTOR2I( 0, 0 ),
  1605. wxT( "Cannot get physical position of pin." ) );
  1606. return m_transform.TransformCoordinate( Pin->GetPosition() ) + m_pos;
  1607. }
  1608. bool SCH_SYMBOL::HasConnectivityChanges( const SCH_ITEM* aItem,
  1609. const SCH_SHEET_PATH* aInstance ) const
  1610. {
  1611. // Do not compare to ourself.
  1612. if( aItem == this )
  1613. return false;
  1614. const SCH_SYMBOL* symbol = dynamic_cast<const SCH_SYMBOL*>( aItem );
  1615. // Don't compare against a different SCH_ITEM.
  1616. wxCHECK( symbol, false );
  1617. if( GetPosition() != symbol->GetPosition() )
  1618. return true;
  1619. if( GetLibId() != symbol->GetLibId() )
  1620. return true;
  1621. if( GetUnitSelection( aInstance ) != symbol->GetUnitSelection( aInstance ) )
  1622. return true;
  1623. if( GetRef( aInstance ) != symbol->GetRef( aInstance ) )
  1624. return true;
  1625. // Power symbol value field changes are connectivity changes.
  1626. if( IsPower()
  1627. && ( GetValue( true, aInstance, false ) != symbol->GetValue( true, aInstance, false ) ) )
  1628. return true;
  1629. if( m_pins.size() != symbol->m_pins.size() )
  1630. return true;
  1631. for( size_t i = 0; i < m_pins.size(); i++ )
  1632. {
  1633. if( m_pins[i]->HasConnectivityChanges( symbol->m_pins[i].get() ) )
  1634. return true;
  1635. }
  1636. return false;
  1637. }
  1638. std::vector<VECTOR2I> SCH_SYMBOL::GetConnectionPoints() const
  1639. {
  1640. std::vector<VECTOR2I> retval;
  1641. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  1642. {
  1643. // Collect only pins attached to the current unit and convert.
  1644. // others are not associated to this symbol instance
  1645. int pin_unit = pin->GetLibPin() ? pin->GetLibPin()->GetUnit()
  1646. : GetUnit();
  1647. int pin_bodyStyle = pin->GetLibPin() ? pin->GetLibPin()->GetBodyStyle()
  1648. : GetBodyStyle();
  1649. if( pin_unit > 0 && pin_unit != GetUnit() )
  1650. continue;
  1651. if( pin_bodyStyle > 0 && pin_bodyStyle != GetBodyStyle() )
  1652. continue;
  1653. retval.push_back( m_transform.TransformCoordinate( pin->GetLocalPosition() ) + m_pos );
  1654. }
  1655. return retval;
  1656. }
  1657. SCH_ITEM* SCH_SYMBOL::GetDrawItem( const VECTOR2I& aPosition, KICAD_T aType )
  1658. {
  1659. // Calculate the position relative to the symbol.
  1660. VECTOR2I libPosition = aPosition - m_pos;
  1661. return m_part->LocateDrawItem( m_unit, m_bodyStyle, aType, libPosition, m_transform );
  1662. }
  1663. wxString SCH_SYMBOL::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
  1664. {
  1665. return wxString::Format( _( "Symbol %s [%s]" ),
  1666. KIUI::EllipsizeMenuText( GetField( REFERENCE_FIELD )->GetText() ),
  1667. KIUI::EllipsizeMenuText( GetLibId().GetLibItemName() ) );
  1668. }
  1669. INSPECT_RESULT SCH_SYMBOL::Visit( INSPECTOR aInspector, void* aTestData,
  1670. const std::vector<KICAD_T>& aScanTypes )
  1671. {
  1672. for( KICAD_T scanType : aScanTypes )
  1673. {
  1674. if( scanType == SCH_LOCATE_ANY_T
  1675. || ( scanType == SCH_SYMBOL_T )
  1676. || ( scanType == SCH_SYMBOL_LOCATE_POWER_T && m_part && m_part->IsPower() ) )
  1677. {
  1678. if( INSPECT_RESULT::QUIT == aInspector( this, aTestData ) )
  1679. return INSPECT_RESULT::QUIT;
  1680. }
  1681. if( scanType == SCH_LOCATE_ANY_T || scanType == SCH_FIELD_T )
  1682. {
  1683. for( SCH_FIELD& field : m_fields )
  1684. {
  1685. if( INSPECT_RESULT::QUIT == aInspector( &field, (void*) this ) )
  1686. return INSPECT_RESULT::QUIT;
  1687. }
  1688. }
  1689. if( scanType == SCH_FIELD_LOCATE_REFERENCE_T )
  1690. {
  1691. if( INSPECT_RESULT::QUIT == aInspector( GetField( REFERENCE_FIELD ), (void*) this ) )
  1692. return INSPECT_RESULT::QUIT;
  1693. }
  1694. if( scanType == SCH_FIELD_LOCATE_VALUE_T
  1695. || ( scanType == SCH_SYMBOL_LOCATE_POWER_T && m_part && m_part->IsPower() ) )
  1696. {
  1697. if( INSPECT_RESULT::QUIT == aInspector( GetField( VALUE_FIELD ), (void*) this ) )
  1698. return INSPECT_RESULT::QUIT;
  1699. }
  1700. if( scanType == SCH_FIELD_LOCATE_FOOTPRINT_T )
  1701. {
  1702. if( INSPECT_RESULT::QUIT == aInspector( GetField( FOOTPRINT_FIELD ), (void*) this ) )
  1703. return INSPECT_RESULT::QUIT;
  1704. }
  1705. if( scanType == SCH_FIELD_LOCATE_DATASHEET_T )
  1706. {
  1707. if( INSPECT_RESULT::QUIT == aInspector( GetField( DATASHEET_FIELD ), (void*) this ) )
  1708. return INSPECT_RESULT::QUIT;
  1709. }
  1710. if( scanType == SCH_LOCATE_ANY_T || scanType == SCH_PIN_T )
  1711. {
  1712. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  1713. {
  1714. // Collect only pins attached to the current unit and convert.
  1715. // others are not associated to this symbol instance
  1716. int pin_unit = pin->GetLibPin() ? pin->GetLibPin()->GetUnit()
  1717. : GetUnit();
  1718. int pin_bodyStyle = pin->GetLibPin() ? pin->GetLibPin()->GetBodyStyle()
  1719. : GetBodyStyle();
  1720. if( pin_unit > 0 && pin_unit != GetUnit() )
  1721. continue;
  1722. if( pin_bodyStyle > 0 && pin_bodyStyle != GetBodyStyle() )
  1723. continue;
  1724. if( INSPECT_RESULT::QUIT == aInspector( pin.get(), (void*) this ) )
  1725. return INSPECT_RESULT::QUIT;
  1726. }
  1727. }
  1728. }
  1729. return INSPECT_RESULT::CONTINUE;
  1730. }
  1731. bool SCH_SYMBOL::operator <( const SCH_ITEM& aItem ) const
  1732. {
  1733. if( Type() != aItem.Type() )
  1734. return Type() < aItem.Type();
  1735. const SCH_SYMBOL* symbol = static_cast<const SCH_SYMBOL*>( &aItem );
  1736. BOX2I rect = GetBodyAndPinsBoundingBox();
  1737. if( rect.GetArea() != symbol->GetBodyAndPinsBoundingBox().GetArea() )
  1738. return rect.GetArea() < symbol->GetBodyAndPinsBoundingBox().GetArea();
  1739. if( m_pos.x != symbol->m_pos.x )
  1740. return m_pos.x < symbol->m_pos.x;
  1741. if( m_pos.y != symbol->m_pos.y )
  1742. return m_pos.y < symbol->m_pos.y;
  1743. return m_Uuid < aItem.m_Uuid; // Ensure deterministic sort
  1744. }
  1745. bool SCH_SYMBOL::operator==( const SCH_SYMBOL& aSymbol ) const
  1746. {
  1747. if( GetFieldCount() != aSymbol.GetFieldCount() )
  1748. return false;
  1749. for( int i = VALUE_FIELD; i < GetFieldCount(); i++ )
  1750. {
  1751. if( GetFields()[i].GetText().Cmp( aSymbol.GetFields()[i].GetText() ) != 0 )
  1752. return false;
  1753. }
  1754. return true;
  1755. }
  1756. bool SCH_SYMBOL::operator!=( const SCH_SYMBOL& aSymbol ) const
  1757. {
  1758. return !( *this == aSymbol );
  1759. }
  1760. SCH_SYMBOL& SCH_SYMBOL::operator=( const SCH_SYMBOL& aSymbol )
  1761. {
  1762. wxCHECK_MSG( Type() == aSymbol.Type(), *this,
  1763. wxT( "Cannot assign object type " ) + aSymbol.GetClass() + wxT( " to type " ) +
  1764. GetClass() );
  1765. if( &aSymbol != this )
  1766. {
  1767. SYMBOL::operator=( aSymbol );
  1768. m_lib_id = aSymbol.m_lib_id;
  1769. m_part.reset( aSymbol.m_part ? new LIB_SYMBOL( *aSymbol.m_part ) : nullptr );
  1770. m_pos = aSymbol.m_pos;
  1771. m_unit = aSymbol.m_unit;
  1772. m_bodyStyle = aSymbol.m_bodyStyle;
  1773. m_transform = aSymbol.m_transform;
  1774. m_instanceReferences = aSymbol.m_instanceReferences;
  1775. m_fields = aSymbol.m_fields; // std::vector's assignment operator
  1776. // Reparent fields after assignment to new symbol.
  1777. for( SCH_FIELD& field : m_fields )
  1778. field.SetParent( this );
  1779. UpdatePins();
  1780. }
  1781. return *this;
  1782. }
  1783. bool SCH_SYMBOL::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  1784. {
  1785. BOX2I bBox = GetBodyBoundingBox();
  1786. bBox.Inflate( aAccuracy / 2 );
  1787. if( bBox.Contains( aPosition ) )
  1788. return true;
  1789. return false;
  1790. }
  1791. bool SCH_SYMBOL::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  1792. {
  1793. if( m_flags & STRUCT_DELETED || m_flags & SKIP_STRUCT )
  1794. return false;
  1795. BOX2I rect = aRect;
  1796. rect.Inflate( aAccuracy / 2 );
  1797. if( aContained )
  1798. return rect.Contains( GetBodyBoundingBox() );
  1799. return rect.Intersects( GetBodyBoundingBox() );
  1800. }
  1801. bool SCH_SYMBOL::doIsConnected( const VECTOR2I& aPosition ) const
  1802. {
  1803. VECTOR2I new_pos = m_transform.InverseTransform().TransformCoordinate( aPosition - m_pos );
  1804. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  1805. {
  1806. if( pin->GetType() == ELECTRICAL_PINTYPE::PT_NC )
  1807. continue;
  1808. // Collect only pins attached to the current unit and convert.
  1809. // others are not associated to this symbol instance
  1810. int pin_unit = pin->GetLibPin() ? pin->GetLibPin()->GetUnit()
  1811. : GetUnit();
  1812. int pin_bodyStyle = pin->GetLibPin() ? pin->GetLibPin()->GetBodyStyle()
  1813. : GetBodyStyle();
  1814. if( pin_unit > 0 && pin_unit != GetUnit() )
  1815. continue;
  1816. if( pin_bodyStyle > 0 && pin_bodyStyle != GetBodyStyle() )
  1817. continue;
  1818. if( pin->GetLocalPosition() == new_pos )
  1819. return true;
  1820. }
  1821. return false;
  1822. }
  1823. bool SCH_SYMBOL::IsInNetlist() const
  1824. {
  1825. return m_isInNetlist;
  1826. }
  1827. void SCH_SYMBOL::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
  1828. int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
  1829. {
  1830. if( aBackground )
  1831. return;
  1832. std::vector<SCH_PIN*> libPins = m_part->GetPins( GetUnit(), GetBodyStyle() );
  1833. // Copy the source so we can re-orient and translate it.
  1834. LIB_SYMBOL tempSymbol( *m_part );
  1835. std::vector<SCH_PIN*> tempPins = tempSymbol.GetPins( GetUnit(), GetBodyStyle() );
  1836. // Copy the pin info from the symbol to the temp pins
  1837. for( unsigned i = 0; i < tempPins.size(); ++ i )
  1838. {
  1839. SCH_PIN* symbolPin = GetPin( libPins[ i ] );
  1840. SCH_PIN* tempPin = tempPins[ i ];
  1841. tempPin->SetName( symbolPin->GetShownName() );
  1842. tempPin->SetType( symbolPin->GetType() );
  1843. tempPin->SetShape( symbolPin->GetShape() );
  1844. if( symbolPin->IsDangling() )
  1845. tempPin->SetFlags( IS_DANGLING );
  1846. }
  1847. for( SCH_ITEM& item : tempSymbol.GetDrawItems() )
  1848. {
  1849. if( EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( &item ) )
  1850. {
  1851. // Use SCH_FIELD's text resolver
  1852. SCH_FIELD dummy( this, -1 );
  1853. dummy.SetText( text->GetText() );
  1854. text->SetText( dummy.GetShownText( false ) );
  1855. }
  1856. }
  1857. SCH_RENDER_SETTINGS* renderSettings = getRenderSettings( aPlotter );
  1858. TRANSFORM savedTransform = renderSettings->m_Transform;
  1859. renderSettings->m_Transform = GetTransform();
  1860. aPlotter->StartBlock( nullptr );
  1861. for( bool local_background : { true, false } )
  1862. {
  1863. tempSymbol.Plot( aPlotter, local_background, aPlotOpts, GetUnit(), GetBodyStyle(),
  1864. m_pos, GetDNP() );
  1865. for( SCH_FIELD field : m_fields )
  1866. {
  1867. field.ClearRenderCache();
  1868. field.Plot( aPlotter, local_background, aPlotOpts, GetUnit(), GetBodyStyle(),
  1869. m_pos, GetDNP() );
  1870. }
  1871. }
  1872. if( m_DNP )
  1873. PlotDNP( aPlotter );
  1874. SCH_SHEET_PATH* sheet = &Schematic()->CurrentSheet();
  1875. // Plot attributes to a hypertext menu
  1876. if( aPlotOpts.m_PDFPropertyPopups )
  1877. {
  1878. std::vector<wxString> properties;
  1879. for( const SCH_FIELD& field : GetFields() )
  1880. {
  1881. wxString text_field = field.GetShownText( sheet, false);
  1882. if( text_field.IsEmpty() )
  1883. continue;
  1884. properties.emplace_back( wxString::Format( wxT( "!%s = %s" ),
  1885. field.GetName(), text_field ) );
  1886. }
  1887. if( !m_part->GetKeyWords().IsEmpty() )
  1888. {
  1889. properties.emplace_back( wxString::Format( wxT( "!%s = %s" ),
  1890. _( "Keywords" ),
  1891. m_part->GetKeyWords() ) );
  1892. }
  1893. aPlotter->HyperlinkMenu( GetBoundingBox(), properties );
  1894. }
  1895. aPlotter->EndBlock( nullptr );
  1896. renderSettings->m_Transform = savedTransform;
  1897. if( !m_part->IsPower() )
  1898. aPlotter->Bookmark( GetBoundingBox(), GetRef( sheet ), _( "Symbols" ) );
  1899. }
  1900. void SCH_SYMBOL::PlotDNP( PLOTTER* aPlotter ) const
  1901. {
  1902. COLOR_SETTINGS* colors = Pgm().GetSettingsManager().GetColorSettings();
  1903. BOX2I bbox = GetBodyBoundingBox();
  1904. BOX2I pins = GetBodyAndPinsBoundingBox();
  1905. VECTOR2D margins( std::max( bbox.GetX() - pins.GetX(),
  1906. pins.GetEnd().x - bbox.GetEnd().x ),
  1907. std::max( bbox.GetY() - pins.GetY(),
  1908. pins.GetEnd().y - bbox.GetEnd().y ) );
  1909. int strokeWidth = 3.0 * schIUScale.MilsToIU( DEFAULT_LINE_WIDTH_MILS );
  1910. margins.x = std::max( margins.x * 0.6, margins.y * 0.3 );
  1911. margins.y = std::max( margins.y * 0.6, margins.x * 0.3 );
  1912. bbox.Inflate( KiROUND( margins.x ), KiROUND( margins.y ) );
  1913. aPlotter->SetColor( colors->GetColor( LAYER_DNP_MARKER ) );
  1914. aPlotter->ThickSegment( bbox.GetOrigin(), bbox.GetEnd(), strokeWidth, FILLED, nullptr );
  1915. aPlotter->ThickSegment( bbox.GetOrigin() + VECTOR2I( bbox.GetWidth(), 0 ),
  1916. bbox.GetOrigin() + VECTOR2I( 0, bbox.GetHeight() ),
  1917. strokeWidth, FILLED, nullptr );
  1918. }
  1919. void SCH_SYMBOL::PlotPins( PLOTTER* aPlotter ) const
  1920. {
  1921. SCH_RENDER_SETTINGS* renderSettings = getRenderSettings( aPlotter );
  1922. TRANSFORM savedTransform = renderSettings->m_Transform;
  1923. renderSettings->m_Transform = GetTransform();
  1924. std::vector<SCH_PIN*> libPins = m_part->GetPins( GetUnit(), GetBodyStyle() );
  1925. // Copy the source to stay const
  1926. LIB_SYMBOL tempSymbol( *m_part );
  1927. std::vector<SCH_PIN*> tempPins = tempSymbol.GetPins( GetUnit(), GetBodyStyle() );
  1928. SCH_PLOT_OPTS plotOpts;
  1929. // Copy the pin info from the symbol to the temp pins
  1930. for( unsigned i = 0; i < tempPins.size(); ++ i )
  1931. {
  1932. SCH_PIN* symbolPin = GetPin( libPins[ i ] );
  1933. SCH_PIN* tempPin = tempPins[ i ];
  1934. tempPin->SetName( symbolPin->GetShownName() );
  1935. tempPin->SetType( symbolPin->GetType() );
  1936. tempPin->SetShape( symbolPin->GetShape() );
  1937. tempPin->Plot( aPlotter, false, plotOpts, GetUnit(), GetBodyStyle(), m_pos, GetDNP() );
  1938. }
  1939. renderSettings->m_Transform = savedTransform;
  1940. }
  1941. bool SCH_SYMBOL::HasBrightenedPins()
  1942. {
  1943. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  1944. {
  1945. if( pin->IsBrightened() )
  1946. return true;
  1947. }
  1948. return false;
  1949. }
  1950. void SCH_SYMBOL::ClearBrightenedPins()
  1951. {
  1952. for( std::unique_ptr<SCH_PIN>& pin : m_pins )
  1953. pin->ClearBrightened();
  1954. }
  1955. /*
  1956. * When modified at the schematic level, we still store the values of these flags in the
  1957. * associated m_part. If m_part now diverges from other usages, a new derived LIB_SYMBOL
  1958. * will be created and stored locally in the schematic.
  1959. */
  1960. bool SCH_SYMBOL::GetShowPinNames() const
  1961. {
  1962. wxCHECK( m_part, false );
  1963. return m_part->GetShowPinNames();
  1964. }
  1965. void SCH_SYMBOL::SetShowPinNames( bool aShow )
  1966. {
  1967. wxCHECK( m_part, /* void */ );
  1968. m_part->SetShowPinNames( aShow );
  1969. }
  1970. bool SCH_SYMBOL::GetShowPinNumbers() const
  1971. {
  1972. wxCHECK( m_part, false );
  1973. return m_part->GetShowPinNumbers();
  1974. }
  1975. void SCH_SYMBOL::SetShowPinNumbers( bool aShow )
  1976. {
  1977. wxCHECK( m_part, /* void */ );
  1978. m_part->SetShowPinNumbers( aShow );
  1979. }
  1980. bool SCH_SYMBOL::IsPointClickableAnchor( const VECTOR2I& aPos ) const
  1981. {
  1982. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  1983. {
  1984. int pin_unit = pin->GetLibPin() ? pin->GetLibPin()->GetUnit()
  1985. : GetUnit();
  1986. int pin_bodyStyle = pin->GetLibPin() ? pin->GetLibPin()->GetBodyStyle()
  1987. : GetBodyStyle();
  1988. if( pin_unit > 0 && pin_unit != GetUnit() )
  1989. continue;
  1990. if( pin_bodyStyle > 0 && pin_bodyStyle != GetBodyStyle() )
  1991. continue;
  1992. if( pin->IsPointClickableAnchor( aPos ) )
  1993. return true;
  1994. }
  1995. return false;
  1996. }
  1997. bool SCH_SYMBOL::IsSymbolLikePowerGlobalLabel() const
  1998. {
  1999. // return true if the symbol is equivalent to a global label:
  2000. // It is a Power symbol
  2001. // It has only one pin type Power input
  2002. if( !GetLibSymbolRef().IsPower() )
  2003. return false;
  2004. std::vector<SCH_PIN*> pin_list = GetAllLibPins();
  2005. if( pin_list.size() != 1 )
  2006. return false;
  2007. return pin_list[0]->GetType() == ELECTRICAL_PINTYPE::PT_POWER_IN;
  2008. }
  2009. bool SCH_SYMBOL::IsPower() const
  2010. {
  2011. return m_part->IsPower();
  2012. }
  2013. bool SCH_SYMBOL::IsNormal() const
  2014. {
  2015. return m_part->IsNormal();
  2016. }
  2017. bool SCH_SYMBOL::operator==( const SCH_ITEM& aOther ) const
  2018. {
  2019. if( Type() != aOther.Type() )
  2020. return false;
  2021. const SCH_SYMBOL& symbol = static_cast<const SCH_SYMBOL&>( aOther );
  2022. if( GetLibId() != symbol.GetLibId() )
  2023. return false;
  2024. if( GetPosition() != symbol.GetPosition() )
  2025. return false;
  2026. if( GetUnit() != symbol.GetUnit() )
  2027. return false;
  2028. if( GetBodyStyle() != symbol.GetBodyStyle() )
  2029. return false;
  2030. if( GetTransform() != symbol.GetTransform() )
  2031. return false;
  2032. if( GetFields() != symbol.GetFields() )
  2033. return false;
  2034. if( m_pins.size() != symbol.m_pins.size() )
  2035. return false;
  2036. for( unsigned i = 0; i < m_pins.size(); ++i )
  2037. {
  2038. if( *m_pins[i] != *symbol.m_pins[i] )
  2039. return false;
  2040. }
  2041. return true;
  2042. }
  2043. double SCH_SYMBOL::Similarity( const SCH_ITEM& aOther ) const
  2044. {
  2045. if( Type() != aOther.Type() )
  2046. return 0.0;
  2047. const SCH_SYMBOL& symbol = static_cast<const SCH_SYMBOL&>( aOther );
  2048. if( GetLibId() != symbol.GetLibId() )
  2049. return 0.0;
  2050. if( GetPosition() == symbol.GetPosition() )
  2051. return 1.0;
  2052. return 0.0;
  2053. }
  2054. static struct SCH_SYMBOL_DESC
  2055. {
  2056. SCH_SYMBOL_DESC()
  2057. {
  2058. ENUM_MAP<SYMBOL_ORIENTATION_PROP>::Instance()
  2059. .Map( SYMBOL_ANGLE_0, wxS( "0" ) )
  2060. .Map( SYMBOL_ANGLE_90, wxS( "90" ) )
  2061. .Map( SYMBOL_ANGLE_180, wxS( "180" ) )
  2062. .Map( SYMBOL_ANGLE_270, wxS( "270" ) );
  2063. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  2064. REGISTER_TYPE( SCH_SYMBOL );
  2065. propMgr.InheritsAfter( TYPE_HASH( SCH_SYMBOL ), TYPE_HASH( SYMBOL ) );
  2066. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, int>( _HKI( "Position X" ),
  2067. &SCH_SYMBOL::SetX, &SCH_SYMBOL::GetX, PROPERTY_DISPLAY::PT_COORD,
  2068. ORIGIN_TRANSFORMS::ABS_X_COORD ) );
  2069. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, int>( _HKI( "Position Y" ),
  2070. &SCH_SYMBOL::SetY, &SCH_SYMBOL::GetY, PROPERTY_DISPLAY::PT_COORD,
  2071. ORIGIN_TRANSFORMS::ABS_Y_COORD ) );
  2072. propMgr.AddProperty( new PROPERTY_ENUM<SCH_SYMBOL, SYMBOL_ORIENTATION_PROP>(
  2073. _HKI( "Orientation" ),
  2074. &SCH_SYMBOL::SetOrientationProp, &SCH_SYMBOL::GetOrientationProp ) );
  2075. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, bool>( _HKI( "Mirror X" ),
  2076. &SCH_SYMBOL::SetMirrorX, &SCH_SYMBOL::GetMirrorX ) );
  2077. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, bool>( _HKI( "Mirror Y" ),
  2078. &SCH_SYMBOL::SetMirrorY, &SCH_SYMBOL::GetMirrorY ) );
  2079. propMgr.AddProperty( new PROPERTY<SYMBOL, bool>( _HKI( "Pin numbers" ),
  2080. &SYMBOL::SetShowPinNumbers, &SYMBOL::GetShowPinNumbers ) );
  2081. propMgr.AddProperty( new PROPERTY<SYMBOL, bool>( _HKI( "Pin names" ),
  2082. &SYMBOL::SetShowPinNames, &SYMBOL::GetShowPinNames ) );
  2083. const wxString groupFields = _HKI( "Fields" );
  2084. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, wxString>( _HKI( "Reference" ),
  2085. &SCH_SYMBOL::SetRefProp, &SCH_SYMBOL::GetRefProp ),
  2086. groupFields );
  2087. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, wxString>( _HKI( "Value" ),
  2088. &SCH_SYMBOL::SetValueProp, &SCH_SYMBOL::GetValueProp ),
  2089. groupFields );
  2090. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, wxString>( _HKI( "Library Link" ),
  2091. NO_SETTER( SCH_SYMBOL, wxString ), &SCH_SYMBOL::GetSymbolIDAsString ),
  2092. groupFields );
  2093. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, wxString>( _HKI( "Library Description" ),
  2094. NO_SETTER( SCH_SYMBOL, wxString ), &SCH_SYMBOL::GetDescription ),
  2095. groupFields );
  2096. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, wxString>( _HKI( "Keywords" ),
  2097. NO_SETTER( SCH_SYMBOL, wxString ), &SCH_SYMBOL::GetKeyWords ),
  2098. groupFields );
  2099. auto multiUnit =
  2100. [=]( INSPECTABLE* aItem ) -> bool
  2101. {
  2102. if( SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( aItem ) )
  2103. return symbol->IsMulti();
  2104. return false;
  2105. };
  2106. auto multiBodyStyle =
  2107. [=]( INSPECTABLE* aItem ) -> bool
  2108. {
  2109. if( SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( aItem ) )
  2110. return symbol->HasAlternateBodyStyle();
  2111. return false;
  2112. };
  2113. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, int>( _HKI( "Unit" ),
  2114. &SCH_SYMBOL::SetUnitProp, &SCH_SYMBOL::GetUnitProp ) )
  2115. .SetAvailableFunc( multiUnit );
  2116. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, int>( _HKI( "Body Style" ),
  2117. &SCH_SYMBOL::SetBodyStyleProp, &SCH_SYMBOL::GetBodyStyleProp ) )
  2118. .SetAvailableFunc( multiBodyStyle );
  2119. const wxString groupAttributes = _HKI( "Attributes" );
  2120. propMgr.AddProperty( new PROPERTY<SYMBOL, bool>( _HKI( "Exclude From Board" ),
  2121. &SYMBOL::SetExcludedFromBoard, &SYMBOL::GetExcludedFromBoard ),
  2122. groupAttributes );
  2123. propMgr.AddProperty( new PROPERTY<SYMBOL, bool>( _HKI( "Exclude From Simulation" ),
  2124. &SYMBOL::SetExcludedFromSim, &SYMBOL::GetExcludedFromSim ),
  2125. groupAttributes );
  2126. propMgr.AddProperty( new PROPERTY<SYMBOL, bool>( _HKI( "Exclude From Bill of Materials" ),
  2127. &SYMBOL::SetExcludedFromBOM, &SYMBOL::GetExcludedFromBOM ),
  2128. groupAttributes );
  2129. propMgr.AddProperty( new PROPERTY<SYMBOL, bool>( _HKI( "Do not Populate" ),
  2130. &SYMBOL::SetDNP, &SYMBOL::GetDNP ),
  2131. groupAttributes );
  2132. }
  2133. } _SCH_SYMBOL_DESC;
  2134. ENUM_TO_WXANY( SYMBOL_ORIENTATION_PROP )