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.

2395 lines
68 KiB

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
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
18 years ago
18 years ago
18 years ago
18 years ago
6 years ago
6 years ago
6 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
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
  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-2023 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 <lib_pin.h>
  29. #include <lib_text.h>
  30. #include <lib_shape.h>
  31. #include <pgm_base.h>
  32. #include <sch_symbol.h>
  33. #include <sch_sheet_path.h>
  34. #include <schematic.h>
  35. #include <trace_helpers.h>
  36. #include <trigo.h>
  37. #include <refdes_utils.h>
  38. #include <wx/log.h>
  39. #include <settings/settings_manager.h>
  40. #include <string_utils.h>
  41. #include <utility>
  42. #include "plotters/plotter.h"
  43. std::unordered_map<TRANSFORM, int> SCH_SYMBOL::s_transformToOrientationCache;
  44. /**
  45. * Convert a wxString to UTF8 and replace any control characters with a ~,
  46. * where a control character is one of the first ASCII values up to ' ' 32d.
  47. */
  48. std::string toUTFTildaText( const wxString& txt )
  49. {
  50. std::string ret = TO_UTF8( txt );
  51. for( std::string::iterator it = ret.begin(); it!=ret.end(); ++it )
  52. {
  53. if( (unsigned char) *it <= ' ' )
  54. *it = '~';
  55. }
  56. return ret;
  57. }
  58. /**
  59. * Used to draw a dummy shape when a LIB_SYMBOL is not found in library
  60. *
  61. * This symbol is a 400 mils square with the text "??"
  62. */
  63. static LIB_SYMBOL* dummy()
  64. {
  65. static LIB_SYMBOL* symbol;
  66. if( !symbol )
  67. {
  68. symbol = new LIB_SYMBOL( wxEmptyString );
  69. LIB_SHAPE* square = new LIB_SHAPE( symbol, SHAPE_T::RECTANGLE );
  70. square->MoveTo( VECTOR2I( schIUScale.MilsToIU( -200 ), schIUScale.MilsToIU( 200 ) ) );
  71. square->SetEnd( VECTOR2I( schIUScale.MilsToIU( 200 ), schIUScale.MilsToIU( -200 ) ) );
  72. LIB_TEXT* text = new LIB_TEXT( symbol );
  73. text->SetTextSize( VECTOR2I( schIUScale.MilsToIU( 150 ), schIUScale.MilsToIU( 150 ) ) );
  74. text->SetText( wxString( wxT( "??" ) ) );
  75. symbol->AddDrawItem( square );
  76. symbol->AddDrawItem( text );
  77. }
  78. return symbol;
  79. }
  80. SCH_SYMBOL::SCH_SYMBOL() :
  81. SCH_ITEM( nullptr, SCH_SYMBOL_T )
  82. {
  83. m_DNP = false;
  84. Init( VECTOR2I( 0, 0 ) );
  85. }
  86. SCH_SYMBOL::SCH_SYMBOL( const LIB_SYMBOL& aSymbol, const LIB_ID& aLibId,
  87. const SCH_SHEET_PATH* aSheet, int aUnit, int aConvert,
  88. const VECTOR2I& aPosition, EDA_ITEM* aParent ) :
  89. SCH_ITEM( aParent, SCH_SYMBOL_T )
  90. {
  91. Init( aPosition );
  92. m_unit = aUnit;
  93. m_convert = aConvert;
  94. m_lib_id = aLibId;
  95. std::unique_ptr< LIB_SYMBOL > part;
  96. part = aSymbol.Flatten();
  97. part->SetParent();
  98. SetLibSymbol( part.release() );
  99. // Copy fields from the library symbol
  100. UpdateFields( aSheet,
  101. true, /* update style */
  102. false, /* update ref */
  103. false, /* update other fields */
  104. true, /* reset ref */
  105. true /* reset other fields */ );
  106. m_prefix = UTIL::GetRefDesPrefix( m_part->GetReferenceField().GetText() );
  107. if( aSheet )
  108. SetRef( aSheet, UTIL::GetRefDesUnannotated( m_prefix ) );
  109. // Inherit the include in bill of materials and board netlist settings from flattened
  110. // library symbol.
  111. m_excludedFromSim = m_part->GetExcludedFromSim();
  112. m_excludedFromBOM = m_part->GetExcludedFromBOM();
  113. m_excludedFromBoard = m_part->GetExcludedFromBoard();
  114. m_DNP = false;
  115. }
  116. SCH_SYMBOL::SCH_SYMBOL( const LIB_SYMBOL& aSymbol, const SCH_SHEET_PATH* aSheet,
  117. const PICKED_SYMBOL& aSel, const VECTOR2I& aPosition,
  118. EDA_ITEM* aParent ) :
  119. SCH_SYMBOL( aSymbol, aSel.LibId, aSheet, aSel.Unit, aSel.Convert, aPosition, aParent )
  120. {
  121. // Set any fields that were modified as part of the symbol selection
  122. for( const std::pair<int, wxString>& i : aSel.Fields )
  123. {
  124. if( i.first == REFERENCE_FIELD )
  125. SetRef( aSheet, i.second );
  126. else if( SCH_FIELD* field = GetFieldById( i.first ) )
  127. field->SetText( i.second );
  128. }
  129. }
  130. SCH_SYMBOL::SCH_SYMBOL( const SCH_SYMBOL& aSymbol ) :
  131. SCH_ITEM( aSymbol )
  132. {
  133. m_parent = aSymbol.m_parent;
  134. m_pos = aSymbol.m_pos;
  135. m_unit = aSymbol.m_unit;
  136. m_convert = aSymbol.m_convert;
  137. m_lib_id = aSymbol.m_lib_id;
  138. m_isInNetlist = aSymbol.m_isInNetlist;
  139. m_excludedFromSim = aSymbol.m_excludedFromSim;
  140. m_excludedFromBOM = aSymbol.m_excludedFromBOM;
  141. m_excludedFromBoard = aSymbol.m_excludedFromBoard;
  142. m_DNP = aSymbol.m_DNP;
  143. if( aSymbol.m_part )
  144. SetLibSymbol( new LIB_SYMBOL( *aSymbol.m_part.get() ) );
  145. const_cast<KIID&>( m_Uuid ) = aSymbol.m_Uuid;
  146. m_transform = aSymbol.m_transform;
  147. m_prefix = aSymbol.m_prefix;
  148. m_instanceReferences = aSymbol.m_instanceReferences;
  149. m_fields = aSymbol.m_fields;
  150. // Re-parent the fields, which before this had aSymbol as parent
  151. for( SCH_FIELD& field : m_fields )
  152. field.SetParent( this );
  153. m_fieldsAutoplaced = aSymbol.m_fieldsAutoplaced;
  154. m_schLibSymbolName = aSymbol.m_schLibSymbolName;
  155. }
  156. void SCH_SYMBOL::Init( const VECTOR2I& pos )
  157. {
  158. m_layer = LAYER_DEVICE;
  159. m_pos = pos;
  160. m_unit = 1; // In multi unit chip - which unit to draw.
  161. m_convert = LIB_ITEM::LIB_CONVERT::BASE; // De Morgan Handling
  162. // The rotation/mirror transformation matrix. pos normal
  163. m_transform = TRANSFORM();
  164. // construct only the mandatory fields, which are the first 4 only.
  165. for( int i = 0; i < MANDATORY_FIELDS; ++i )
  166. {
  167. m_fields.emplace_back( pos, i, this, TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) );
  168. if( i == REFERENCE_FIELD )
  169. m_fields.back().SetLayer( LAYER_REFERENCEPART );
  170. else if( i == VALUE_FIELD )
  171. m_fields.back().SetLayer( LAYER_VALUEPART );
  172. else
  173. m_fields.back().SetLayer( LAYER_FIELDS );
  174. }
  175. m_prefix = wxString( wxT( "U" ) );
  176. m_isInNetlist = true;
  177. m_excludedFromSim = false;
  178. m_excludedFromBOM = false;
  179. m_excludedFromBoard = false;
  180. }
  181. EDA_ITEM* SCH_SYMBOL::Clone() const
  182. {
  183. return new SCH_SYMBOL( *this );
  184. }
  185. bool SCH_SYMBOL::IsMissingLibSymbol() const
  186. {
  187. if( !m_part )
  188. return true;
  189. return false;
  190. }
  191. void SCH_SYMBOL::ViewGetLayers( int aLayers[], int& aCount ) const
  192. {
  193. aCount = 8;
  194. aLayers[0] = LAYER_DANGLING; // Pins are drawn by their parent symbol, so the parent
  195. // symbol needs to draw to LAYER_DANGLING
  196. aLayers[1] = LAYER_OP_CURRENTS; // Same for pin operating points
  197. aLayers[2] = LAYER_DEVICE;
  198. aLayers[3] = LAYER_REFERENCEPART;
  199. aLayers[4] = LAYER_VALUEPART;
  200. aLayers[5] = LAYER_FIELDS;
  201. aLayers[6] = LAYER_DEVICE_BACKGROUND;
  202. aLayers[7] = LAYER_SELECTION_SHADOWS;
  203. }
  204. bool SCH_SYMBOL::IsMovableFromAnchorPoint() const
  205. {
  206. // If a symbol's anchor is not grid-aligned to its pins then moving from the anchor is
  207. // going to end up moving the symbol's pins off-grid.
  208. // The minimal grid size allowed to place a pin is 25 mils
  209. const int min_grid_size = schIUScale.MilsToIU( 25 );
  210. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  211. {
  212. if( ( ( pin->GetPosition().x - m_pos.x ) % min_grid_size ) != 0 )
  213. return false;
  214. if( ( ( pin->GetPosition().y - m_pos.y ) % min_grid_size ) != 0 )
  215. return false;
  216. }
  217. return true;
  218. }
  219. void SCH_SYMBOL::SetLibId( const LIB_ID& aLibId )
  220. {
  221. m_lib_id = aLibId;
  222. }
  223. wxString SCH_SYMBOL::GetSchSymbolLibraryName() const
  224. {
  225. if( !m_schLibSymbolName.IsEmpty() )
  226. return m_schLibSymbolName;
  227. else
  228. return m_lib_id.Format();
  229. }
  230. void SCH_SYMBOL::SetLibSymbol( LIB_SYMBOL* aLibSymbol )
  231. {
  232. wxCHECK2( ( aLibSymbol == nullptr ) || ( aLibSymbol->IsRoot() ), aLibSymbol = nullptr );
  233. m_part.reset( aLibSymbol );
  234. UpdatePins();
  235. }
  236. wxString SCH_SYMBOL::GetDescription() const
  237. {
  238. if( m_part )
  239. return m_part->GetDescription();
  240. return wxEmptyString;
  241. }
  242. wxString SCH_SYMBOL::GetKeyWords() const
  243. {
  244. if( m_part )
  245. return m_part->GetKeyWords();
  246. return wxEmptyString;
  247. }
  248. wxString SCH_SYMBOL::GetDatasheet() const
  249. {
  250. if( m_part )
  251. return m_part->GetDatasheetField().GetText();
  252. return wxEmptyString;
  253. }
  254. void SCH_SYMBOL::UpdatePins()
  255. {
  256. std::map<wxString, wxString> altPinMap;
  257. std::map<wxString, KIID> pinUuidMap;
  258. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  259. {
  260. pinUuidMap[ pin->GetNumber() ] = pin->m_Uuid;
  261. if( !pin->GetAlt().IsEmpty() )
  262. altPinMap[ pin->GetNumber() ] = pin->GetAlt();
  263. }
  264. m_pins.clear();
  265. m_pinMap.clear();
  266. if( !m_part )
  267. return;
  268. unsigned i = 0;
  269. std::vector<LIB_PIN*> pins = m_part->GetAllLibPins();
  270. for( LIB_PIN* libPin : pins )
  271. {
  272. // NW: Don't filter by unit: this data-structure is used for all instances,
  273. // some if which might have different units.
  274. if( libPin->GetConvert() && m_convert && m_convert != libPin->GetConvert() )
  275. continue;
  276. m_pins.push_back( std::make_unique<SCH_PIN>( libPin, this ) );
  277. auto ii = pinUuidMap.find( libPin->GetNumber() );
  278. if( ii != pinUuidMap.end() )
  279. const_cast<KIID&>( m_pins.back()->m_Uuid ) = ii->second;
  280. auto iii = altPinMap.find( libPin->GetNumber() );
  281. if( iii != altPinMap.end() )
  282. m_pins.back()->SetAlt( iii->second );
  283. m_pinMap[ libPin ] = i;
  284. ++i;
  285. }
  286. }
  287. void SCH_SYMBOL::SetUnit( int aUnit )
  288. {
  289. UpdateUnit( aUnit );
  290. }
  291. void SCH_SYMBOL::UpdateUnit( int aUnit )
  292. {
  293. m_unit = aUnit;
  294. }
  295. void SCH_SYMBOL::SetConvert( int aConvert )
  296. {
  297. if( m_convert != aConvert )
  298. {
  299. m_convert = aConvert;
  300. // The convert may have a different pin layout so the update the pin map.
  301. UpdatePins();
  302. }
  303. }
  304. void SCH_SYMBOL::SetTransform( const TRANSFORM& aTransform )
  305. {
  306. if( m_transform != aTransform )
  307. m_transform = aTransform;
  308. }
  309. int SCH_SYMBOL::GetUnitCount() const
  310. {
  311. if( m_part )
  312. return m_part->GetUnitCount();
  313. return 0;
  314. }
  315. wxString SCH_SYMBOL::GetUnitDisplayName( int aUnit )
  316. {
  317. wxCHECK( m_part, ( wxString::Format( _( "Unit %s" ), LIB_SYMBOL::SubReference( aUnit ) ) ) );
  318. return m_part->GetUnitDisplayName( aUnit );
  319. }
  320. bool SCH_SYMBOL::HasUnitDisplayName( int aUnit )
  321. {
  322. wxCHECK( m_part, false );
  323. return m_part->HasUnitDisplayName( aUnit );
  324. }
  325. void SCH_SYMBOL::PrintBackground( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset )
  326. {
  327. LIB_SYMBOL_OPTIONS opts;
  328. opts.transform = m_transform;
  329. opts.draw_visible_fields = false;
  330. opts.draw_hidden_fields = false;
  331. if( m_part )
  332. m_part->PrintBackground( aSettings, m_pos + aOffset, m_unit, m_convert, opts, GetDNP() );
  333. }
  334. void SCH_SYMBOL::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset )
  335. {
  336. LIB_SYMBOL_OPTIONS opts;
  337. opts.transform = m_transform;
  338. opts.draw_visible_fields = false;
  339. opts.draw_hidden_fields = false;
  340. if( m_part )
  341. {
  342. LIB_PINS libPins;
  343. m_part->GetPins( libPins, m_unit, m_convert );
  344. LIB_SYMBOL tempSymbol( *m_part );
  345. LIB_PINS tempPins;
  346. tempSymbol.GetPins( tempPins, m_unit, m_convert );
  347. // Copy the pin info from the symbol to the temp pins
  348. for( unsigned i = 0; i < tempPins.size(); ++ i )
  349. {
  350. SCH_PIN* symbolPin = GetPin( libPins[ i ] );
  351. LIB_PIN* tempPin = tempPins[ i ];
  352. tempPin->SetName( symbolPin->GetShownName() );
  353. tempPin->SetType( symbolPin->GetType() );
  354. tempPin->SetShape( symbolPin->GetShape() );
  355. }
  356. tempSymbol.Print( aSettings, m_pos + aOffset, m_unit, m_convert, opts, GetDNP() );
  357. }
  358. else // Use dummy() part if the actual cannot be found.
  359. {
  360. dummy()->Print( aSettings, m_pos + aOffset, 0, 0, opts, GetDNP() );
  361. }
  362. for( SCH_FIELD& field : m_fields )
  363. field.Print( aSettings, aOffset );
  364. if( m_DNP )
  365. {
  366. BOX2I bbox = GetBodyAndPinsBoundingBox();
  367. wxDC* DC = aSettings->GetPrintDC();
  368. COLOR4D dnp_color = aSettings->GetLayerColor( LAYER_DNP_MARKER );
  369. GRFilledSegment( DC, bbox.GetOrigin(), bbox.GetEnd(),
  370. 3.0 * schIUScale.MilsToIU( DEFAULT_LINE_WIDTH_MILS ),
  371. dnp_color );
  372. GRFilledSegment( DC, bbox.GetOrigin() + VECTOR2I( bbox.GetWidth(), 0 ),
  373. bbox.GetOrigin() + VECTOR2I( 0, bbox.GetHeight() ),
  374. 3.0 * schIUScale.MilsToIU( DEFAULT_LINE_WIDTH_MILS ),
  375. dnp_color );
  376. }
  377. }
  378. bool SCH_SYMBOL::GetInstance( SCH_SYMBOL_INSTANCE& aInstance,
  379. const KIID_PATH& aSheetPath, bool aTestFromEnd ) const
  380. {
  381. for( const SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  382. {
  383. if( !aTestFromEnd )
  384. {
  385. if( instance.m_Path == aSheetPath )
  386. {
  387. aInstance = instance;
  388. return true;
  389. }
  390. }
  391. else if( instance.m_Path.EndsWith( aSheetPath ) )
  392. {
  393. aInstance = instance;
  394. return true;
  395. }
  396. }
  397. return false;
  398. }
  399. void SCH_SYMBOL::RemoveInstance( const SCH_SHEET_PATH& aInstancePath )
  400. {
  401. // Search for an existing path and remove it if found (should not occur)
  402. for( unsigned ii = 0; ii < m_instanceReferences.size(); ii++ )
  403. {
  404. if( m_instanceReferences[ii].m_Path == aInstancePath.Path() )
  405. {
  406. wxLogTrace( traceSchSheetPaths, "Removing symbol instance:\n"
  407. " sheet path %s\n"
  408. " reference %s, unit %d from symbol %s.",
  409. aInstancePath.Path().AsString(),
  410. m_instanceReferences[ii].m_Reference,
  411. m_instanceReferences[ii].m_Unit,
  412. m_Uuid.AsString() );
  413. m_instanceReferences.erase( m_instanceReferences.begin() + ii );
  414. ii--;
  415. }
  416. }
  417. }
  418. void SCH_SYMBOL::SortInstances( bool (*aSortFunction)( const SCH_SYMBOL_INSTANCE& aLhs,
  419. const SCH_SYMBOL_INSTANCE& aRhs ) )
  420. {
  421. std::sort( m_instanceReferences.begin(), m_instanceReferences.end(), aSortFunction );
  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, "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,
  446. "Adding symbol '%s' instance:\n"
  447. " sheet path '%s'\n"
  448. " reference '%s'\n"
  449. " unit %d\n",
  450. m_Uuid.AsString(),
  451. aPath.AsString(),
  452. aRef,
  453. aUnit );
  454. m_instanceReferences.push_back( instance );
  455. // This should set the default instance to the first saved instance data for each symbol
  456. // when importing sheets.
  457. if( m_instanceReferences.size() == 1 )
  458. {
  459. m_fields[ REFERENCE_FIELD ].SetText( aRef );
  460. m_unit = aUnit;
  461. }
  462. }
  463. void SCH_SYMBOL::AddHierarchicalReference( const SCH_SYMBOL_INSTANCE& aInstance )
  464. {
  465. KIID_PATH searchPath( aInstance.m_Path );
  466. std::vector<SCH_SYMBOL_INSTANCE>::iterator resultIt;
  467. do
  468. {
  469. resultIt = std::find_if( m_instanceReferences.begin(), m_instanceReferences.end(),
  470. [searchPath]( const auto& it )
  471. {
  472. return it.m_Path == searchPath;
  473. } );
  474. if( resultIt != m_instanceReferences.end() )
  475. {
  476. wxLogTrace( traceSchSheetPaths, "Removing symbol instance:\n"
  477. " sheet path %s\n"
  478. " reference %s, unit %d from symbol %s.",
  479. aInstance.m_Path.AsString(),
  480. resultIt->m_Reference,
  481. resultIt->m_Unit,
  482. m_Uuid.AsString() );
  483. // Instance data should be unique by path. Double check just in case there was
  484. // some buggy code in the past.
  485. resultIt = m_instanceReferences.erase( resultIt );
  486. }
  487. }
  488. while( resultIt != m_instanceReferences.end() );
  489. SCH_SYMBOL_INSTANCE instance = aInstance;
  490. wxLogTrace( traceSchSheetPaths,
  491. "Adding symbol '%s' instance:\n"
  492. " sheet path '%s'\n"
  493. " reference '%s'\n"
  494. " unit %d\n",
  495. m_Uuid.AsString(),
  496. instance.m_Path.AsString(),
  497. instance.m_Reference,
  498. instance.m_Unit );
  499. m_instanceReferences.push_back( instance );
  500. // This should set the default instance to the first saved instance data for each symbol
  501. // when importing sheets.
  502. if( m_instanceReferences.size() == 1 )
  503. {
  504. m_fields[ REFERENCE_FIELD ].SetText( instance.m_Reference );
  505. m_unit = instance.m_Unit;
  506. }
  507. }
  508. const wxString SCH_SYMBOL::GetRef( const SCH_SHEET_PATH* sheet, bool aIncludeUnit ) const
  509. {
  510. KIID_PATH path = sheet->Path();
  511. wxString ref;
  512. wxString subRef;
  513. for( const SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  514. {
  515. if( instance.m_Path == path )
  516. {
  517. ref = instance.m_Reference;
  518. subRef = LIB_SYMBOL::SubReference( instance.m_Unit );
  519. break;
  520. }
  521. }
  522. // If it was not found in m_Paths array, then see if it is in m_Field[REFERENCE] -- if so,
  523. // use this as a default for this path. This will happen if we load a version 1 schematic
  524. // file. It will also mean that multiple instances of the same sheet by default all have
  525. // the same symbol references, but perhaps this is best.
  526. if( ref.IsEmpty() && !GetField( REFERENCE_FIELD )->GetText().IsEmpty() )
  527. {
  528. const_cast<SCH_SYMBOL*>( this )->SetRef( sheet, GetField( REFERENCE_FIELD )->GetText() );
  529. ref = GetField( REFERENCE_FIELD )->GetText();
  530. }
  531. if( ref.IsEmpty() )
  532. ref = UTIL::GetRefDesUnannotated( m_prefix );
  533. if( aIncludeUnit && GetUnitCount() > 1 )
  534. ref += subRef;
  535. return ref;
  536. }
  537. bool SCH_SYMBOL::IsReferenceStringValid( const wxString& aReferenceString )
  538. {
  539. return !UTIL::GetRefDesPrefix( aReferenceString ).IsEmpty();
  540. }
  541. void SCH_SYMBOL::SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref )
  542. {
  543. KIID_PATH path = sheet->Path();
  544. bool found = false;
  545. // check to see if it is already there before inserting it
  546. for( SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  547. {
  548. if( instance.m_Path == path )
  549. {
  550. found = true;
  551. instance.m_Reference = ref;
  552. break;
  553. }
  554. }
  555. if( !found )
  556. AddHierarchicalReference( path, ref, m_unit );
  557. for( std::unique_ptr<SCH_PIN>& pin : m_pins )
  558. pin->ClearDefaultNetName( sheet );
  559. if( Schematic() && *sheet == Schematic()->CurrentSheet() )
  560. m_fields[ REFERENCE_FIELD ].SetText( ref );
  561. // Reinit the m_prefix member if needed
  562. m_prefix = UTIL::GetRefDesPrefix( ref );
  563. if( m_prefix.IsEmpty() )
  564. m_prefix = wxT( "U" );
  565. // Power symbols have references starting with # and are not included in netlists
  566. m_isInNetlist = ! ref.StartsWith( wxT( "#" ) );
  567. }
  568. bool SCH_SYMBOL::IsAnnotated( const SCH_SHEET_PATH* aSheet )
  569. {
  570. KIID_PATH path = aSheet->Path();
  571. for( const SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  572. {
  573. if( instance.m_Path == path )
  574. return instance.m_Reference.Last() != '?';
  575. }
  576. return false;
  577. }
  578. void SCH_SYMBOL::UpdatePrefix()
  579. {
  580. wxString refDesignator = GetField( REFERENCE_FIELD )->GetText();
  581. refDesignator.Replace( "~", " " );
  582. wxString prefix = refDesignator;
  583. while( prefix.Length() )
  584. {
  585. wxUniCharRef last = prefix.Last();
  586. if( ( last >= '0' && last <= '9' ) || last == '?' || last == '*' )
  587. prefix.RemoveLast();
  588. else
  589. break;
  590. }
  591. // Avoid a prefix containing trailing/leading spaces
  592. prefix.Trim( true );
  593. prefix.Trim( false );
  594. if( !prefix.IsEmpty() )
  595. SetPrefix( prefix );
  596. }
  597. int SCH_SYMBOL::GetUnitSelection( const SCH_SHEET_PATH* aSheet ) const
  598. {
  599. KIID_PATH path = aSheet->Path();
  600. for( const SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  601. {
  602. if( instance.m_Path == path )
  603. return instance.m_Unit;
  604. }
  605. // If it was not found in m_Paths array, then use m_unit. This will happen if we load a
  606. // version 1 schematic file.
  607. return m_unit;
  608. }
  609. void SCH_SYMBOL::SetUnitSelection( const SCH_SHEET_PATH* aSheet, int aUnitSelection )
  610. {
  611. KIID_PATH path = aSheet->Path();
  612. // check to see if it is already there before inserting it
  613. for( SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  614. {
  615. if( instance.m_Path == path )
  616. {
  617. instance.m_Unit = aUnitSelection;
  618. return;
  619. }
  620. }
  621. // didn't find it; better add it
  622. AddHierarchicalReference( path, UTIL::GetRefDesUnannotated( m_prefix ), aUnitSelection );
  623. }
  624. void SCH_SYMBOL::SetUnitSelection( int aUnitSelection )
  625. {
  626. for( SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  627. instance.m_Unit = aUnitSelection;
  628. }
  629. const wxString SCH_SYMBOL::GetValueFieldText( bool aResolve, const SCH_SHEET_PATH* aPath,
  630. bool aAllowExtraText ) const
  631. {
  632. if( aResolve )
  633. return GetField( VALUE_FIELD )->GetShownText( aPath, aAllowExtraText );
  634. return GetField( VALUE_FIELD )->GetText();
  635. }
  636. void SCH_SYMBOL::SetValueFieldText( const wxString& aValue )
  637. {
  638. m_fields[ VALUE_FIELD ].SetText( aValue );
  639. }
  640. const wxString SCH_SYMBOL::GetFootprintFieldText( bool aResolve, const SCH_SHEET_PATH* aPath,
  641. bool aAllowExtraText ) const
  642. {
  643. if( aResolve )
  644. return GetField( FOOTPRINT_FIELD )->GetShownText( aPath, aAllowExtraText );
  645. return GetField( FOOTPRINT_FIELD )->GetText();
  646. }
  647. void SCH_SYMBOL::SetFootprintFieldText( const wxString& aFootprint )
  648. {
  649. m_fields[ FOOTPRINT_FIELD ].SetText( aFootprint );
  650. }
  651. SCH_FIELD* SCH_SYMBOL::GetField( MANDATORY_FIELD_T aFieldType )
  652. {
  653. return &m_fields[aFieldType];
  654. }
  655. const SCH_FIELD* SCH_SYMBOL::GetField( MANDATORY_FIELD_T aFieldType ) const
  656. {
  657. return &m_fields[aFieldType];
  658. }
  659. SCH_FIELD* SCH_SYMBOL::GetFieldById( int aFieldId )
  660. {
  661. for( size_t ii = 0; ii < m_fields.size(); ++ii )
  662. {
  663. if( m_fields[ii].GetId() == aFieldId )
  664. return &m_fields[ii];
  665. }
  666. return nullptr;
  667. }
  668. SCH_FIELD* SCH_SYMBOL::GetFieldByName( const wxString& aFieldName )
  669. {
  670. for( size_t ii = 0; ii < m_fields.size(); ++ii )
  671. {
  672. if( m_fields[ii].GetName() == aFieldName )
  673. return &m_fields[ii];
  674. }
  675. return nullptr;
  676. }
  677. wxString SCH_SYMBOL::GetFieldText( const wxString& aFieldName ) const
  678. {
  679. for( const SCH_FIELD& field : m_fields )
  680. {
  681. if( aFieldName == field.GetName() || aFieldName == field.GetCanonicalName() )
  682. return field.GetText();
  683. }
  684. return wxEmptyString;
  685. }
  686. void SCH_SYMBOL::GetFields( std::vector<SCH_FIELD*>& aVector, bool aVisibleOnly )
  687. {
  688. for( SCH_FIELD& field : m_fields )
  689. {
  690. if( aVisibleOnly )
  691. {
  692. if( !field.IsVisible() || field.GetShownText( nullptr, true ).IsEmpty() )
  693. continue;
  694. }
  695. aVector.push_back( &field );
  696. }
  697. }
  698. SCH_FIELD* SCH_SYMBOL::AddField( const SCH_FIELD& aField )
  699. {
  700. int newNdx = m_fields.size();
  701. m_fields.push_back( aField );
  702. return &m_fields[newNdx];
  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. if( m_part )
  738. {
  739. std::vector<LIB_FIELD*> fields;
  740. m_part->GetFields( fields );
  741. for( const LIB_FIELD* libField : fields )
  742. {
  743. int id = libField->GetId();
  744. SCH_FIELD* schField;
  745. if( id >= 0 && id < MANDATORY_FIELDS )
  746. {
  747. schField = GetFieldById( id );
  748. }
  749. else
  750. {
  751. schField = FindField( libField->GetCanonicalName() );
  752. if( !schField )
  753. {
  754. wxString fieldName = libField->GetCanonicalName();
  755. SCH_FIELD newField( VECTOR2I( 0, 0 ), GetFieldCount(), this, fieldName );
  756. schField = AddField( newField );
  757. }
  758. }
  759. if( aUpdateStyle )
  760. {
  761. schField->ImportValues( *libField );
  762. schField->SetTextPos( m_pos + libField->GetTextPos() );
  763. }
  764. if( id == REFERENCE_FIELD && aPath )
  765. {
  766. if( aResetRef )
  767. SetRef( aPath, m_part->GetReferenceField().GetText() );
  768. else if( aUpdateRef )
  769. SetRef( aPath, libField->GetText() );
  770. }
  771. else if( id == VALUE_FIELD )
  772. {
  773. SetValueFieldText( UnescapeString( libField->GetText() ) );
  774. }
  775. else if( id == FOOTPRINT_FIELD )
  776. {
  777. if( aResetOtherFields || aUpdateOtherFields )
  778. SetFootprintFieldText( libField->GetText() );
  779. }
  780. else if( id == DATASHEET_FIELD )
  781. {
  782. if( aResetOtherFields )
  783. schField->SetText( GetDatasheet() ); // alias-specific value
  784. else if( aUpdateOtherFields )
  785. schField->SetText( libField->GetText() );
  786. }
  787. else
  788. {
  789. if( aResetOtherFields || aUpdateOtherFields )
  790. schField->SetText( libField->GetText() );
  791. }
  792. }
  793. }
  794. }
  795. void SCH_SYMBOL::RunOnChildren( const std::function<void( SCH_ITEM* )>& aFunction )
  796. {
  797. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  798. aFunction( pin.get() );
  799. for( SCH_FIELD& field : m_fields )
  800. aFunction( &field );
  801. }
  802. SCH_PIN* SCH_SYMBOL::GetPin( const wxString& aNumber ) const
  803. {
  804. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  805. {
  806. if( pin->GetNumber() == aNumber )
  807. return pin.get();
  808. }
  809. return nullptr;
  810. }
  811. void SCH_SYMBOL::GetLibPins( std::vector<LIB_PIN*>& aPinsList ) const
  812. {
  813. if( m_part )
  814. m_part->GetPins( aPinsList, m_unit, m_convert );
  815. }
  816. std::vector<LIB_PIN*> SCH_SYMBOL::GetAllLibPins() const
  817. {
  818. std::vector<LIB_PIN*> pinList;
  819. if( m_part )
  820. m_part->GetPins( pinList, 0, 0 );
  821. return pinList;
  822. }
  823. SCH_PIN* SCH_SYMBOL::GetPin( LIB_PIN* aLibPin ) const
  824. {
  825. wxASSERT( m_pinMap.count( aLibPin ) );
  826. return m_pins[ m_pinMap.at( aLibPin ) ].get();
  827. }
  828. std::vector<SCH_PIN*> SCH_SYMBOL::GetPins( const SCH_SHEET_PATH* aSheet ) const
  829. {
  830. std::vector<SCH_PIN*> pins;
  831. if( aSheet == nullptr )
  832. {
  833. wxCHECK_MSG( Schematic(), pins, "Can't call GetPins on a symbol with no schematic" );
  834. aSheet = &Schematic()->CurrentSheet();
  835. }
  836. int unit = GetUnitSelection( aSheet );
  837. for( const std::unique_ptr<SCH_PIN>& p : m_pins )
  838. {
  839. if( unit && p->GetLibPin()->GetUnit() && ( p->GetLibPin()->GetUnit() != unit ) )
  840. continue;
  841. pins.push_back( p.get() );
  842. }
  843. return pins;
  844. }
  845. void SCH_SYMBOL::SwapData( SCH_ITEM* aItem )
  846. {
  847. SCH_ITEM::SwapFlags( aItem );
  848. wxCHECK_RET( aItem != nullptr && aItem->Type() == SCH_SYMBOL_T,
  849. wxT( "Cannot swap data with invalid symbol." ) );
  850. SCH_SYMBOL* symbol = (SCH_SYMBOL*) aItem;
  851. std::swap( m_lib_id, symbol->m_lib_id );
  852. m_pins.swap( symbol->m_pins ); // std::vector's swap()
  853. for( std::unique_ptr<SCH_PIN>& pin : symbol->m_pins )
  854. pin->SetParent( symbol );
  855. for( std::unique_ptr<SCH_PIN>& pin : m_pins )
  856. pin->SetParent( this );
  857. LIB_SYMBOL* libSymbol = symbol->m_part.release();
  858. symbol->m_part = std::move( m_part );
  859. symbol->UpdatePins();
  860. m_part.reset( libSymbol );
  861. UpdatePins();
  862. std::swap( m_pos, symbol->m_pos );
  863. std::swap( m_unit, symbol->m_unit );
  864. std::swap( m_convert, symbol->m_convert );
  865. m_fields.swap( symbol->m_fields ); // std::vector's swap()
  866. for( SCH_FIELD& field : symbol->m_fields )
  867. field.SetParent( symbol );
  868. for( SCH_FIELD& field : m_fields )
  869. field.SetParent( this );
  870. TRANSFORM tmp = m_transform;
  871. m_transform = symbol->m_transform;
  872. symbol->m_transform = tmp;
  873. std::swap( m_excludedFromSim, symbol->m_excludedFromSim );
  874. std::swap( m_excludedFromBOM, symbol->m_excludedFromBOM );
  875. std::swap( m_DNP, symbol->m_DNP );
  876. std::swap( m_excludedFromBoard, symbol->m_excludedFromBoard );
  877. std::swap( m_instanceReferences, symbol->m_instanceReferences );
  878. std::swap( m_schLibSymbolName, symbol->m_schLibSymbolName );
  879. }
  880. void SCH_SYMBOL::GetContextualTextVars( wxArrayString* aVars ) const
  881. {
  882. for( int i = 0; i < MANDATORY_FIELDS; ++i )
  883. aVars->push_back( m_fields[i].GetCanonicalName().Upper() );
  884. for( size_t i = MANDATORY_FIELDS; i < m_fields.size(); ++i )
  885. aVars->push_back( m_fields[i].GetName() );
  886. aVars->push_back( wxT( "OP" ) );
  887. aVars->push_back( wxT( "FOOTPRINT_LIBRARY" ) );
  888. aVars->push_back( wxT( "FOOTPRINT_NAME" ) );
  889. aVars->push_back( wxT( "UNIT" ) );
  890. aVars->push_back( wxT( "SYMBOL_LIBRARY" ) );
  891. aVars->push_back( wxT( "SYMBOL_NAME" ) );
  892. aVars->push_back( wxT( "SYMBOL_DESCRIPTION" ) );
  893. aVars->push_back( wxT( "SYMBOL_KEYWORDS" ) );
  894. aVars->push_back( wxT( "EXCLUDE_FROM_BOM" ) );
  895. aVars->push_back( wxT( "EXCLUDE_FROM_BOARD" ) );
  896. aVars->push_back( wxT( "EXCLUDE_FROM_SIM" ) );
  897. aVars->push_back( wxT( "DNP" ) );
  898. }
  899. bool SCH_SYMBOL::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, int aDepth ) const
  900. {
  901. static wxRegEx operatingPoint( wxT( "^"
  902. "OP"
  903. "(:[a-zA-Z]*)?" // port
  904. "(.([0-9])?([a-zA-Z]*))?" // format
  905. "$" ) );
  906. SCHEMATIC* schematic = Schematic();
  907. // SCH_SYMBOL object has no context outside a schematic and the instance on a path.
  908. if( !schematic || !aPath )
  909. return false;
  910. if( operatingPoint.Matches( *token ) )
  911. {
  912. wxString port( operatingPoint.GetMatch( *token, 1 ) );
  913. wxString precisionStr( operatingPoint.GetMatch( *token, 3 ) );
  914. wxString range( operatingPoint.GetMatch( *token, 4 ) );
  915. wxString signal = GetRef( aPath ) + port;
  916. int precision = 3;
  917. if( !precisionStr.IsEmpty() )
  918. precision = precisionStr[0] - '0';
  919. if( range.IsEmpty() )
  920. {
  921. if( port == wxS( ":power" ) )
  922. range = wxS( "~W" );
  923. else
  924. range = wxS( "~A" );
  925. }
  926. *token = schematic->GetOperatingPoint( signal.Lower(), precision, range );
  927. return true;
  928. }
  929. if( token->Contains( ':' ) )
  930. {
  931. if( schematic->ResolveCrossReference( token, aDepth + 1 ) )
  932. return true;
  933. }
  934. for( int i = 0; i < MANDATORY_FIELDS; ++i )
  935. {
  936. if( token->IsSameAs( m_fields[ i ].GetCanonicalName().Upper() ) )
  937. {
  938. if( i == REFERENCE_FIELD )
  939. *token = GetRef( aPath, true );
  940. else
  941. *token = m_fields[ i ].GetShownText( aPath, false, aDepth + 1 );
  942. return true;
  943. }
  944. }
  945. for( size_t i = MANDATORY_FIELDS; i < m_fields.size(); ++i )
  946. {
  947. if( token->IsSameAs( m_fields[ i ].GetName() )
  948. || token->IsSameAs( m_fields[ i ].GetName().Upper() ) )
  949. {
  950. *token = m_fields[ i ].GetShownText( aPath, false, aDepth + 1 );
  951. return true;
  952. }
  953. }
  954. for( const TEMPLATE_FIELDNAME& templateFieldname :
  955. schematic->Settings().m_TemplateFieldNames.GetTemplateFieldNames() )
  956. {
  957. if( token->IsSameAs( templateFieldname.m_Name )
  958. || token->IsSameAs( templateFieldname.m_Name.Upper() ) )
  959. {
  960. // If we didn't find it in the fields list then it isn't set on this symbol.
  961. // Just return an empty string.
  962. *token = wxEmptyString;
  963. return true;
  964. }
  965. }
  966. if( token->IsSameAs( wxT( "FOOTPRINT_LIBRARY" ) ) )
  967. {
  968. wxString footprint = GetFootprintFieldText( true, aPath, false );
  969. wxArrayString parts = wxSplit( footprint, ':' );
  970. *token = parts[ 0 ];
  971. return true;
  972. }
  973. else if( token->IsSameAs( wxT( "FOOTPRINT_NAME" ) ) )
  974. {
  975. wxString footprint = GetFootprintFieldText( true, aPath, false );
  976. wxArrayString parts = wxSplit( footprint, ':' );
  977. *token = parts[ std::min( 1, (int) parts.size() - 1 ) ];
  978. return true;
  979. }
  980. else if( token->IsSameAs( wxT( "UNIT" ) ) )
  981. {
  982. int unit = GetUnitSelection( aPath );
  983. *token = LIB_SYMBOL::SubReference( unit );
  984. return true;
  985. }
  986. else if( token->IsSameAs( wxT( "SYMBOL_LIBRARY" ) ) )
  987. {
  988. *token = m_lib_id.GetLibNickname();
  989. return true;
  990. }
  991. else if( token->IsSameAs( wxT( "SYMBOL_NAME" ) ) )
  992. {
  993. *token = m_lib_id.GetLibItemName();
  994. return true;
  995. }
  996. else if( token->IsSameAs( wxT( "SYMBOL_DESCRIPTION" ) ) )
  997. {
  998. *token = GetDescription();
  999. return true;
  1000. }
  1001. else if( token->IsSameAs( wxT( "SYMBOL_KEYWORDS" ) ) )
  1002. {
  1003. *token = GetKeyWords();
  1004. return true;
  1005. }
  1006. else if( token->IsSameAs( wxT( "EXCLUDE_FROM_BOM" ) ) )
  1007. {
  1008. *token = this->GetExcludedFromBOM() ? _( "Excluded from BOM" )
  1009. : wxString( wxT( "" ) );
  1010. return true;
  1011. }
  1012. else if( token->IsSameAs( wxT( "EXCLUDE_FROM_BOARD" ) ) )
  1013. {
  1014. *token = this->GetExcludedFromBoard() ? _( "Excluded from board" )
  1015. : wxString( wxT( "" ) );
  1016. return true;
  1017. }
  1018. else if( token->IsSameAs( wxT( "EXCLUDE_FROM_SIM" ) ) )
  1019. {
  1020. *token = this->GetExcludedFromSim() ? _( "Excluded from simulation" )
  1021. : wxString( wxT( "" ) );
  1022. return true;
  1023. }
  1024. else if( token->IsSameAs( wxT( "DNP" ) ) )
  1025. {
  1026. *token = this->GetDNP() ? _( "DNP" ) : wxString( wxT( "" ) );
  1027. return true;
  1028. }
  1029. // See if parent can resolve it (this will recurse to ancestors)
  1030. if( aPath->Last() && aPath->Last()->ResolveTextVar( aPath, token, aDepth + 1 ) )
  1031. return true;
  1032. return false;
  1033. }
  1034. void SCH_SYMBOL::ClearAnnotation( const SCH_SHEET_PATH* aSheetPath, bool aResetPrefix )
  1035. {
  1036. if( aSheetPath )
  1037. {
  1038. KIID_PATH path = aSheetPath->Path();
  1039. for( SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  1040. {
  1041. if( instance.m_Path == path )
  1042. {
  1043. if( instance.m_Reference.IsEmpty() || aResetPrefix )
  1044. instance.m_Reference = UTIL::GetRefDesUnannotated( m_prefix );
  1045. else
  1046. instance.m_Reference = UTIL::GetRefDesUnannotated( instance.m_Reference );
  1047. }
  1048. }
  1049. }
  1050. else
  1051. {
  1052. for( SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  1053. {
  1054. if( instance.m_Reference.IsEmpty() || aResetPrefix)
  1055. instance.m_Reference = UTIL::GetRefDesUnannotated( m_prefix );
  1056. else
  1057. instance.m_Reference = UTIL::GetRefDesUnannotated( instance.m_Reference );
  1058. }
  1059. }
  1060. for( std::unique_ptr<SCH_PIN>& pin : m_pins )
  1061. pin->ClearDefaultNetName( aSheetPath );
  1062. // These 2 changes do not work in complex hierarchy.
  1063. // When a clear annotation is made, the calling function must call a
  1064. // UpdateAllScreenReferences for the active sheet.
  1065. // But this call cannot made here.
  1066. wxString currentReference = m_fields[REFERENCE_FIELD].GetText();
  1067. if( currentReference.IsEmpty() || aResetPrefix )
  1068. m_fields[REFERENCE_FIELD].SetText( UTIL::GetRefDesUnannotated( m_prefix ) );
  1069. else
  1070. m_fields[REFERENCE_FIELD].SetText( UTIL::GetRefDesUnannotated( currentReference ) );
  1071. }
  1072. bool SCH_SYMBOL::AddSheetPathReferenceEntryIfMissing( const KIID_PATH& aSheetPath )
  1073. {
  1074. // An empty sheet path is illegal, at a minimum the root sheet UUID must be present.
  1075. wxCHECK( aSheetPath.size() > 0, false );
  1076. for( const SCH_SYMBOL_INSTANCE& instance : m_instanceReferences )
  1077. {
  1078. // if aSheetPath is found, nothing to do:
  1079. if( instance.m_Path == aSheetPath )
  1080. return false;
  1081. }
  1082. // This entry does not exist: add it, with its last-used reference
  1083. AddHierarchicalReference( aSheetPath, m_fields[REFERENCE_FIELD].GetText(), m_unit );
  1084. return true;
  1085. }
  1086. void SCH_SYMBOL::SetOrientation( int aOrientation )
  1087. {
  1088. TRANSFORM temp = TRANSFORM();
  1089. bool transform = false;
  1090. switch( aOrientation )
  1091. {
  1092. case SYM_ORIENT_0:
  1093. case SYM_NORMAL: // default transform matrix
  1094. m_transform.x1 = 1;
  1095. m_transform.y2 = -1;
  1096. m_transform.x2 = m_transform.y1 = 0;
  1097. break;
  1098. case SYM_ROTATE_COUNTERCLOCKWISE: // Rotate + (incremental rotation)
  1099. temp.x1 = temp.y2 = 0;
  1100. temp.y1 = 1;
  1101. temp.x2 = -1;
  1102. transform = true;
  1103. break;
  1104. case SYM_ROTATE_CLOCKWISE: // Rotate - (incremental rotation)
  1105. temp.x1 = temp.y2 = 0;
  1106. temp.y1 = -1;
  1107. temp.x2 = 1;
  1108. transform = true;
  1109. break;
  1110. case SYM_MIRROR_Y: // Mirror Y (incremental rotation)
  1111. temp.x1 = -1;
  1112. temp.y2 = 1;
  1113. temp.y1 = temp.x2 = 0;
  1114. transform = true;
  1115. break;
  1116. case SYM_MIRROR_X: // Mirror X (incremental rotation)
  1117. temp.x1 = 1;
  1118. temp.y2 = -1;
  1119. temp.y1 = temp.x2 = 0;
  1120. transform = true;
  1121. break;
  1122. case SYM_ORIENT_90:
  1123. SetOrientation( SYM_ORIENT_0 );
  1124. SetOrientation( SYM_ROTATE_COUNTERCLOCKWISE );
  1125. break;
  1126. case SYM_ORIENT_180:
  1127. SetOrientation( SYM_ORIENT_0 );
  1128. SetOrientation( SYM_ROTATE_COUNTERCLOCKWISE );
  1129. SetOrientation( SYM_ROTATE_COUNTERCLOCKWISE );
  1130. break;
  1131. case SYM_ORIENT_270:
  1132. SetOrientation( SYM_ORIENT_0 );
  1133. SetOrientation( SYM_ROTATE_CLOCKWISE );
  1134. break;
  1135. case ( SYM_ORIENT_0 + SYM_MIRROR_X ):
  1136. SetOrientation( SYM_ORIENT_0 );
  1137. SetOrientation( SYM_MIRROR_X );
  1138. break;
  1139. case ( SYM_ORIENT_0 + SYM_MIRROR_Y ):
  1140. SetOrientation( SYM_ORIENT_0 );
  1141. SetOrientation( SYM_MIRROR_Y );
  1142. break;
  1143. case ( SYM_ORIENT_90 + SYM_MIRROR_X ):
  1144. SetOrientation( SYM_ORIENT_90 );
  1145. SetOrientation( SYM_MIRROR_X );
  1146. break;
  1147. case ( SYM_ORIENT_90 + SYM_MIRROR_Y ):
  1148. SetOrientation( SYM_ORIENT_90 );
  1149. SetOrientation( SYM_MIRROR_Y );
  1150. break;
  1151. case ( SYM_ORIENT_180 + SYM_MIRROR_X ):
  1152. SetOrientation( SYM_ORIENT_180 );
  1153. SetOrientation( SYM_MIRROR_X );
  1154. break;
  1155. case ( SYM_ORIENT_180 + SYM_MIRROR_Y ):
  1156. SetOrientation( SYM_ORIENT_180 );
  1157. SetOrientation( SYM_MIRROR_Y );
  1158. break;
  1159. case ( SYM_ORIENT_270 + SYM_MIRROR_X ):
  1160. SetOrientation( SYM_ORIENT_270 );
  1161. SetOrientation( SYM_MIRROR_X );
  1162. break;
  1163. case ( SYM_ORIENT_270 + SYM_MIRROR_Y ):
  1164. SetOrientation( SYM_ORIENT_270 );
  1165. SetOrientation( SYM_MIRROR_Y );
  1166. break;
  1167. default:
  1168. transform = false;
  1169. wxFAIL_MSG( "Invalid schematic symbol orientation type." );
  1170. break;
  1171. }
  1172. if( transform )
  1173. {
  1174. /* The new matrix transform is the old matrix transform modified by the
  1175. * requested transformation, which is the temp transform (rot,
  1176. * mirror ..) in order to have (in term of matrix transform):
  1177. * transform coord = new_m_transform * coord
  1178. * where transform coord is the coord modified by new_m_transform from
  1179. * the initial value coord.
  1180. * new_m_transform is computed (from old_m_transform and temp) to
  1181. * have:
  1182. * transform coord = old_m_transform * temp
  1183. */
  1184. TRANSFORM newTransform;
  1185. newTransform.x1 = m_transform.x1 * temp.x1 + m_transform.x2 * temp.y1;
  1186. newTransform.y1 = m_transform.y1 * temp.x1 + m_transform.y2 * temp.y1;
  1187. newTransform.x2 = m_transform.x1 * temp.x2 + m_transform.x2 * temp.y2;
  1188. newTransform.y2 = m_transform.y1 * temp.x2 + m_transform.y2 * temp.y2;
  1189. m_transform = newTransform;
  1190. }
  1191. }
  1192. int SCH_SYMBOL::GetOrientation() const
  1193. {
  1194. /*
  1195. * This is slow, but also a bizarre algorithm. I don't feel like unteasing the algorithm right
  1196. * now, so let's just cache it for the moment.
  1197. */
  1198. if( s_transformToOrientationCache.count( m_transform ) )
  1199. return s_transformToOrientationCache.at( m_transform );
  1200. int rotate_values[] =
  1201. {
  1202. SYM_ORIENT_0,
  1203. SYM_ORIENT_90,
  1204. SYM_ORIENT_180,
  1205. SYM_ORIENT_270,
  1206. SYM_MIRROR_X + SYM_ORIENT_0,
  1207. SYM_MIRROR_X + SYM_ORIENT_90,
  1208. SYM_MIRROR_X + SYM_ORIENT_270,
  1209. SYM_MIRROR_Y,
  1210. SYM_MIRROR_Y + SYM_ORIENT_0,
  1211. SYM_MIRROR_Y + SYM_ORIENT_90,
  1212. SYM_MIRROR_Y + SYM_ORIENT_180,
  1213. SYM_MIRROR_Y + SYM_ORIENT_270
  1214. };
  1215. // Try to find the current transform option:
  1216. TRANSFORM transform = m_transform;
  1217. SCH_SYMBOL temp( *this );
  1218. for( int type_rotate : rotate_values )
  1219. {
  1220. temp.SetOrientation( type_rotate );
  1221. if( transform == temp.GetTransform() )
  1222. {
  1223. s_transformToOrientationCache[m_transform] = type_rotate;
  1224. return type_rotate;
  1225. }
  1226. }
  1227. // Error: orientation not found in list (should not happen)
  1228. wxFAIL_MSG( "Schematic symbol orientation matrix internal error." );
  1229. return SYM_NORMAL;
  1230. }
  1231. #if defined(DEBUG)
  1232. void SCH_SYMBOL::Show( int nestLevel, std::ostream& os ) const
  1233. {
  1234. // for now, make it look like XML:
  1235. NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str()
  1236. << " ref=\"" << TO_UTF8( GetField( REFERENCE_FIELD )->GetName() )
  1237. << '"' << " chipName=\""
  1238. << GetLibId().Format() << '"' << m_pos
  1239. << " layer=\"" << m_layer
  1240. << '"' << ">\n";
  1241. // skip the reference, it's been output already.
  1242. for( int i = 1; i < GetFieldCount(); ++i )
  1243. {
  1244. const wxString& value = GetFields()[i].GetText();
  1245. if( !value.IsEmpty() )
  1246. {
  1247. NestedSpace( nestLevel + 1, os ) << "<field" << " name=\""
  1248. << TO_UTF8( GetFields()[i].GetName() )
  1249. << '"' << " value=\""
  1250. << TO_UTF8( value ) << "\"/>\n";
  1251. }
  1252. }
  1253. NestedSpace( nestLevel, os ) << "</" << TO_UTF8( GetClass().Lower() ) << ">\n";
  1254. }
  1255. #endif
  1256. BOX2I SCH_SYMBOL::doGetBoundingBox( bool aIncludePins, bool aIncludeFields ) const
  1257. {
  1258. BOX2I bBox;
  1259. if( m_part )
  1260. bBox = m_part->GetBodyBoundingBox( m_unit, m_convert, aIncludePins, false );
  1261. else
  1262. bBox = dummy()->GetBodyBoundingBox( m_unit, m_convert, aIncludePins, false );
  1263. int x0 = bBox.GetX();
  1264. int xm = bBox.GetRight();
  1265. // We must reverse Y values, because matrix orientation
  1266. // suppose Y axis normal for the library items coordinates,
  1267. // m_transform reverse Y values, but bBox is already reversed!
  1268. int y0 = -bBox.GetY();
  1269. int ym = -bBox.GetBottom();
  1270. // Compute the real Boundary box (rotated, mirrored ...)
  1271. int x1 = m_transform.x1 * x0 + m_transform.y1 * y0;
  1272. int y1 = m_transform.x2 * x0 + m_transform.y2 * y0;
  1273. int x2 = m_transform.x1 * xm + m_transform.y1 * ym;
  1274. int y2 = m_transform.x2 * xm + m_transform.y2 * ym;
  1275. bBox.SetX( x1 );
  1276. bBox.SetY( y1 );
  1277. bBox.SetWidth( x2 - x1 );
  1278. bBox.SetHeight( y2 - y1 );
  1279. bBox.Normalize();
  1280. bBox.Offset( m_pos );
  1281. if( aIncludeFields )
  1282. {
  1283. for( const SCH_FIELD& field : m_fields )
  1284. {
  1285. if( field.IsVisible() )
  1286. bBox.Merge( field.GetBoundingBox() );
  1287. }
  1288. }
  1289. return bBox;
  1290. }
  1291. BOX2I SCH_SYMBOL::GetBodyBoundingBox() const
  1292. {
  1293. return doGetBoundingBox( false, false );
  1294. }
  1295. BOX2I SCH_SYMBOL::GetBodyAndPinsBoundingBox() const
  1296. {
  1297. return doGetBoundingBox( true, false );
  1298. }
  1299. const BOX2I SCH_SYMBOL::GetBoundingBox() const
  1300. {
  1301. return doGetBoundingBox( true, true );
  1302. }
  1303. void SCH_SYMBOL::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  1304. {
  1305. wxString msg;
  1306. SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( aFrame );
  1307. SCH_SHEET_PATH* currentSheet = schframe ? &schframe->GetCurrentSheet() : nullptr;
  1308. auto addExcludes =
  1309. [&]()
  1310. {
  1311. wxArrayString msgs;
  1312. if( GetExcludedFromSim() )
  1313. msgs.Add( _( "Simulation" ) );
  1314. if( GetExcludedFromBOM() )
  1315. msgs.Add( _( "BOM" ) );
  1316. if( GetExcludedFromBoard() )
  1317. msgs.Add( _( "Board" ) );
  1318. if( GetDNP() )
  1319. msgs.Add( _( "DNP" ) );
  1320. msg = wxJoin( msgs, '|' );
  1321. msg.Replace( '|', wxS( ", " ) );
  1322. if( !msg.empty() )
  1323. aList.emplace_back( _( "Exclude from" ), msg );
  1324. };
  1325. // part and alias can differ if alias is not the root
  1326. if( m_part )
  1327. {
  1328. if( m_part.get() != dummy() )
  1329. {
  1330. if( m_part->IsPower() )
  1331. {
  1332. // Don't use GetShownText(); we want to see the variable references here
  1333. aList.emplace_back( _( "Power symbol" ),
  1334. KIUI::EllipsizeStatusText( aFrame, GetField( VALUE_FIELD )->GetText() ) );
  1335. }
  1336. else
  1337. {
  1338. aList.emplace_back( _( "Reference" ),
  1339. UnescapeString( GetRef( currentSheet ) ) );
  1340. // Don't use GetShownText(); we want to see the variable references here
  1341. aList.emplace_back( _( "Value" ),
  1342. KIUI::EllipsizeStatusText( aFrame, GetField( VALUE_FIELD )->GetText() ) );
  1343. addExcludes();
  1344. aList.emplace_back( _( "Name" ),
  1345. KIUI::EllipsizeStatusText( aFrame, GetLibId().GetLibItemName() ) );
  1346. }
  1347. #if 0 // Display symbol flags, for debug only
  1348. aList.emplace_back( _( "flags" ), wxString::Format( "%X", GetEditFlags() ) );
  1349. #endif
  1350. if( !m_part->IsRoot() )
  1351. {
  1352. msg = _( "Missing parent" );
  1353. std::shared_ptr< LIB_SYMBOL > parent = m_part->GetParent().lock();
  1354. if( parent )
  1355. msg = parent->GetName();
  1356. aList.emplace_back( _( "Derived from" ), UnescapeString( msg ) );
  1357. }
  1358. else if( !m_lib_id.GetLibNickname().empty() )
  1359. {
  1360. aList.emplace_back( _( "Library" ), m_lib_id.GetLibNickname() );
  1361. }
  1362. else
  1363. {
  1364. aList.emplace_back( _( "Library" ), _( "Undefined!!!" ) );
  1365. }
  1366. // Display the current associated footprint, if exists.
  1367. // Don't use GetShownText(); we want to see the variable references here
  1368. msg = KIUI::EllipsizeStatusText( aFrame, GetField( FOOTPRINT_FIELD )->GetText() );
  1369. if( msg.IsEmpty() )
  1370. msg = _( "<Unknown>" );
  1371. aList.emplace_back( _( "Footprint" ), msg );
  1372. // Display description of the symbol, and keywords found in lib
  1373. aList.emplace_back( _( "Description" ) + wxT( ": " )
  1374. + GetField( DESCRIPTION_FIELD )->GetText(),
  1375. _( "Keywords" ) + wxT( ": " ) + m_part->GetKeyWords() );
  1376. }
  1377. }
  1378. else
  1379. {
  1380. aList.emplace_back( _( "Reference" ), GetRef( currentSheet ) );
  1381. // Don't use GetShownText(); we want to see the variable references here
  1382. aList.emplace_back( _( "Value" ),
  1383. KIUI::EllipsizeStatusText( aFrame, GetField( VALUE_FIELD )->GetText() ) );
  1384. addExcludes();
  1385. aList.emplace_back( _( "Name" ),
  1386. KIUI::EllipsizeStatusText( aFrame, GetLibId().GetLibItemName() ) );
  1387. wxString libNickname = GetLibId().GetLibNickname();
  1388. if( libNickname.empty() )
  1389. msg = _( "No library defined!" );
  1390. else
  1391. msg.Printf( _( "Symbol not found in %s!" ), libNickname );
  1392. aList.emplace_back( _( "Library" ), msg );
  1393. }
  1394. }
  1395. BITMAPS SCH_SYMBOL::GetMenuImage() const
  1396. {
  1397. return BITMAPS::add_component;
  1398. }
  1399. void SCH_SYMBOL::MirrorHorizontally( int aCenter )
  1400. {
  1401. int dx = m_pos.x;
  1402. SetOrientation( SYM_MIRROR_Y );
  1403. MIRROR( m_pos.x, aCenter );
  1404. dx -= m_pos.x; // dx,0 is the move vector for this transform
  1405. for( SCH_FIELD& field : m_fields )
  1406. {
  1407. // Move the fields to the new position because the symbol itself has moved.
  1408. VECTOR2I pos = field.GetTextPos();
  1409. pos.x -= dx;
  1410. field.SetTextPos( pos );
  1411. }
  1412. }
  1413. void SCH_SYMBOL::MirrorVertically( int aCenter )
  1414. {
  1415. int dy = m_pos.y;
  1416. SetOrientation( SYM_MIRROR_X );
  1417. MIRROR( m_pos.y, aCenter );
  1418. dy -= m_pos.y; // 0,dy is the move vector for this transform
  1419. for( SCH_FIELD& field : m_fields )
  1420. {
  1421. // Move the fields to the new position because the symbol itself has moved.
  1422. VECTOR2I pos = field.GetTextPos();
  1423. pos.y -= dy;
  1424. field.SetTextPos( pos );
  1425. }
  1426. }
  1427. void SCH_SYMBOL::Rotate( const VECTOR2I& aCenter )
  1428. {
  1429. VECTOR2I prev = m_pos;
  1430. RotatePoint( m_pos, aCenter, ANGLE_90 );
  1431. SetOrientation( SYM_ROTATE_COUNTERCLOCKWISE );
  1432. for( SCH_FIELD& field : m_fields )
  1433. {
  1434. // Move the fields to the new position because the symbol itself has moved.
  1435. VECTOR2I pos = field.GetTextPos();
  1436. pos.x -= prev.x - m_pos.x;
  1437. pos.y -= prev.y - m_pos.y;
  1438. field.SetTextPos( pos );
  1439. }
  1440. }
  1441. bool SCH_SYMBOL::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const
  1442. {
  1443. // Symbols are searchable via the child field and pin item text.
  1444. return false;
  1445. }
  1446. void SCH_SYMBOL::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
  1447. {
  1448. for( auto& pin : m_pins )
  1449. {
  1450. LIB_PIN* lib_pin = pin->GetLibPin();
  1451. if( lib_pin->GetUnit() && m_unit && ( m_unit != lib_pin->GetUnit() ) )
  1452. continue;
  1453. DANGLING_END_ITEM item( PIN_END, lib_pin, GetPinPhysicalPosition( lib_pin ), this );
  1454. aItemList.push_back( item );
  1455. }
  1456. }
  1457. bool SCH_SYMBOL::UpdateDanglingState( std::vector<DANGLING_END_ITEM>& aItemList,
  1458. const SCH_SHEET_PATH* aPath )
  1459. {
  1460. bool changed = false;
  1461. for( std::unique_ptr<SCH_PIN>& pin : m_pins )
  1462. {
  1463. bool previousState = pin->IsDangling();
  1464. pin->SetIsDangling( true );
  1465. VECTOR2I pos = m_transform.TransformCoordinate( pin->GetLocalPosition() ) + m_pos;
  1466. for( DANGLING_END_ITEM& each_item : aItemList )
  1467. {
  1468. // Some people like to stack pins on top of each other in a symbol to indicate
  1469. // internal connection. While technically connected, it is not particularly useful
  1470. // to display them that way, so skip any pins that are in the same symbol as this
  1471. // one.
  1472. if( each_item.GetParent() == this )
  1473. continue;
  1474. switch( each_item.GetType() )
  1475. {
  1476. case PIN_END:
  1477. case LABEL_END:
  1478. case SHEET_LABEL_END:
  1479. case WIRE_END:
  1480. case NO_CONNECT_END:
  1481. case JUNCTION_END:
  1482. if( pos == each_item.GetPosition() )
  1483. pin->SetIsDangling( false );
  1484. break;
  1485. default:
  1486. break;
  1487. }
  1488. if( !pin->IsDangling() )
  1489. break;
  1490. }
  1491. changed = ( changed || ( previousState != pin->IsDangling() ) );
  1492. }
  1493. return changed;
  1494. }
  1495. VECTOR2I SCH_SYMBOL::GetPinPhysicalPosition( const LIB_PIN* Pin ) const
  1496. {
  1497. wxCHECK_MSG( Pin != nullptr && Pin->Type() == LIB_PIN_T, VECTOR2I( 0, 0 ),
  1498. wxT( "Cannot get physical position of pin." ) );
  1499. return m_transform.TransformCoordinate( Pin->GetPosition() ) + m_pos;
  1500. }
  1501. std::vector<VECTOR2I> SCH_SYMBOL::GetConnectionPoints() const
  1502. {
  1503. std::vector<VECTOR2I> retval;
  1504. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  1505. {
  1506. // Collect only pins attached to the current unit and convert.
  1507. // others are not associated to this symbol instance
  1508. int pin_unit = pin->GetLibPin()->GetUnit();
  1509. int pin_convert = pin->GetLibPin()->GetConvert();
  1510. if( pin_unit > 0 && pin_unit != GetUnit() )
  1511. continue;
  1512. if( pin_convert > 0 && pin_convert != GetConvert() )
  1513. continue;
  1514. retval.push_back( m_transform.TransformCoordinate( pin->GetLocalPosition() ) + m_pos );
  1515. }
  1516. return retval;
  1517. }
  1518. LIB_ITEM* SCH_SYMBOL::GetDrawItem( const VECTOR2I& aPosition, KICAD_T aType )
  1519. {
  1520. if( m_part )
  1521. {
  1522. // Calculate the position relative to the symbol.
  1523. VECTOR2I libPosition = aPosition - m_pos;
  1524. return m_part->LocateDrawItem( m_unit, m_convert, aType, libPosition, m_transform );
  1525. }
  1526. return nullptr;
  1527. }
  1528. wxString SCH_SYMBOL::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
  1529. {
  1530. return wxString::Format( _( "Symbol %s [%s]" ),
  1531. KIUI::EllipsizeMenuText( GetField( REFERENCE_FIELD )->GetText() ),
  1532. KIUI::EllipsizeMenuText( GetLibId().GetLibItemName() ) );
  1533. }
  1534. INSPECT_RESULT SCH_SYMBOL::Visit( INSPECTOR aInspector, void* aTestData,
  1535. const std::vector<KICAD_T>& aScanTypes )
  1536. {
  1537. for( KICAD_T scanType : aScanTypes )
  1538. {
  1539. if( scanType == SCH_LOCATE_ANY_T
  1540. || ( scanType == SCH_SYMBOL_T )
  1541. || ( scanType == SCH_SYMBOL_LOCATE_POWER_T && m_part && m_part->IsPower() ) )
  1542. {
  1543. if( INSPECT_RESULT::QUIT == aInspector( this, aTestData ) )
  1544. return INSPECT_RESULT::QUIT;
  1545. }
  1546. if( scanType == SCH_LOCATE_ANY_T || scanType == SCH_FIELD_T )
  1547. {
  1548. for( SCH_FIELD& field : m_fields )
  1549. {
  1550. if( INSPECT_RESULT::QUIT == aInspector( &field, (void*) this ) )
  1551. return INSPECT_RESULT::QUIT;
  1552. }
  1553. }
  1554. if( scanType == SCH_FIELD_LOCATE_REFERENCE_T )
  1555. {
  1556. if( INSPECT_RESULT::QUIT == aInspector( GetField( REFERENCE_FIELD ), (void*) this ) )
  1557. return INSPECT_RESULT::QUIT;
  1558. }
  1559. if( scanType == SCH_FIELD_LOCATE_VALUE_T
  1560. || ( scanType == SCH_SYMBOL_LOCATE_POWER_T && m_part && m_part->IsPower() ) )
  1561. {
  1562. if( INSPECT_RESULT::QUIT == aInspector( GetField( VALUE_FIELD ), (void*) this ) )
  1563. return INSPECT_RESULT::QUIT;
  1564. }
  1565. if( scanType == SCH_FIELD_LOCATE_FOOTPRINT_T )
  1566. {
  1567. if( INSPECT_RESULT::QUIT == aInspector( GetField( FOOTPRINT_FIELD ), (void*) this ) )
  1568. return INSPECT_RESULT::QUIT;
  1569. }
  1570. if( scanType == SCH_FIELD_LOCATE_DATASHEET_T )
  1571. {
  1572. if( INSPECT_RESULT::QUIT == aInspector( GetField( DATASHEET_FIELD ), (void*) this ) )
  1573. return INSPECT_RESULT::QUIT;
  1574. }
  1575. if( scanType == SCH_LOCATE_ANY_T || scanType == SCH_PIN_T )
  1576. {
  1577. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  1578. {
  1579. // Collect only pins attached to the current unit and convert.
  1580. // others are not associated to this symbol instance
  1581. int pin_unit = pin->GetLibPin()->GetUnit();
  1582. int pin_convert = pin->GetLibPin()->GetConvert();
  1583. if( pin_unit > 0 && pin_unit != GetUnit() )
  1584. continue;
  1585. if( pin_convert > 0 && pin_convert != GetConvert() )
  1586. continue;
  1587. if( INSPECT_RESULT::QUIT == aInspector( pin.get(), (void*) this ) )
  1588. return INSPECT_RESULT::QUIT;
  1589. }
  1590. }
  1591. }
  1592. return INSPECT_RESULT::CONTINUE;
  1593. }
  1594. bool SCH_SYMBOL::operator <( const SCH_ITEM& aItem ) const
  1595. {
  1596. if( Type() != aItem.Type() )
  1597. return Type() < aItem.Type();
  1598. auto symbol = static_cast<const SCH_SYMBOL*>( &aItem );
  1599. BOX2I rect = GetBodyAndPinsBoundingBox();
  1600. if( rect.GetArea() != symbol->GetBodyAndPinsBoundingBox().GetArea() )
  1601. return rect.GetArea() < symbol->GetBodyAndPinsBoundingBox().GetArea();
  1602. if( m_pos.x != symbol->m_pos.x )
  1603. return m_pos.x < symbol->m_pos.x;
  1604. if( m_pos.y != symbol->m_pos.y )
  1605. return m_pos.y < symbol->m_pos.y;
  1606. return m_Uuid < aItem.m_Uuid; // Ensure deterministic sort
  1607. }
  1608. bool SCH_SYMBOL::operator==( const SCH_SYMBOL& aSymbol ) const
  1609. {
  1610. if( GetFieldCount() != aSymbol.GetFieldCount() )
  1611. return false;
  1612. for( int i = VALUE_FIELD; i < GetFieldCount(); i++ )
  1613. {
  1614. if( GetFields()[i].GetText().Cmp( aSymbol.GetFields()[i].GetText() ) != 0 )
  1615. return false;
  1616. }
  1617. return true;
  1618. }
  1619. bool SCH_SYMBOL::operator!=( const SCH_SYMBOL& aSymbol ) const
  1620. {
  1621. return !( *this == aSymbol );
  1622. }
  1623. SCH_SYMBOL& SCH_SYMBOL::operator=( const SCH_ITEM& aItem )
  1624. {
  1625. wxCHECK_MSG( Type() == aItem.Type(), *this,
  1626. wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
  1627. GetClass() );
  1628. if( &aItem != this )
  1629. {
  1630. SCH_ITEM::operator=( aItem );
  1631. SCH_SYMBOL* c = (SCH_SYMBOL*) &aItem;
  1632. m_lib_id = c->m_lib_id;
  1633. LIB_SYMBOL* libSymbol = c->m_part ? new LIB_SYMBOL( *c->m_part.get() ) : nullptr;
  1634. m_part.reset( libSymbol );
  1635. m_pos = c->m_pos;
  1636. m_unit = c->m_unit;
  1637. m_convert = c->m_convert;
  1638. m_transform = c->m_transform;
  1639. m_instanceReferences = c->m_instanceReferences;
  1640. m_fields = c->m_fields; // std::vector's assignment operator
  1641. // Reparent fields after assignment to new symbol.
  1642. for( SCH_FIELD& field : m_fields )
  1643. field.SetParent( this );
  1644. UpdatePins();
  1645. }
  1646. return *this;
  1647. }
  1648. bool SCH_SYMBOL::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  1649. {
  1650. BOX2I bBox = GetBodyBoundingBox();
  1651. bBox.Inflate( aAccuracy / 2 );
  1652. if( bBox.Contains( aPosition ) )
  1653. return true;
  1654. return false;
  1655. }
  1656. bool SCH_SYMBOL::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  1657. {
  1658. if( m_flags & STRUCT_DELETED || m_flags & SKIP_STRUCT )
  1659. return false;
  1660. BOX2I rect = aRect;
  1661. rect.Inflate( aAccuracy / 2 );
  1662. if( aContained )
  1663. return rect.Contains( GetBodyBoundingBox() );
  1664. return rect.Intersects( GetBodyBoundingBox() );
  1665. }
  1666. bool SCH_SYMBOL::doIsConnected( const VECTOR2I& aPosition ) const
  1667. {
  1668. VECTOR2I new_pos = m_transform.InverseTransform().TransformCoordinate( aPosition - m_pos );
  1669. for( const auto& pin : m_pins )
  1670. {
  1671. if( pin->GetType() == ELECTRICAL_PINTYPE::PT_NC )
  1672. continue;
  1673. // Collect only pins attached to the current unit and convert.
  1674. // others are not associated to this symbol instance
  1675. int pin_unit = pin->GetLibPin()->GetUnit();
  1676. int pin_convert = pin->GetLibPin()->GetConvert();
  1677. if( pin_unit > 0 && pin_unit != GetUnit() )
  1678. continue;
  1679. if( pin_convert > 0 && pin_convert != GetConvert() )
  1680. continue;
  1681. if( pin->GetLocalPosition() == new_pos )
  1682. return true;
  1683. }
  1684. return false;
  1685. }
  1686. bool SCH_SYMBOL::IsInNetlist() const
  1687. {
  1688. return m_isInNetlist;
  1689. }
  1690. void SCH_SYMBOL::Plot( PLOTTER* aPlotter, bool aBackground ) const
  1691. {
  1692. if( aBackground )
  1693. return;
  1694. if( m_part )
  1695. {
  1696. LIB_PINS libPins;
  1697. m_part->GetPins( libPins, GetUnit(), GetConvert() );
  1698. // Copy the source so we can re-orient and translate it.
  1699. LIB_SYMBOL tempSymbol( *m_part );
  1700. LIB_PINS tempPins;
  1701. tempSymbol.GetPins( tempPins, GetUnit(), GetConvert() );
  1702. // Copy the pin info from the symbol to the temp pins
  1703. for( unsigned i = 0; i < tempPins.size(); ++ i )
  1704. {
  1705. SCH_PIN* symbolPin = GetPin( libPins[ i ] );
  1706. LIB_PIN* tempPin = tempPins[ i ];
  1707. tempPin->SetName( symbolPin->GetShownName() );
  1708. tempPin->SetType( symbolPin->GetType() );
  1709. tempPin->SetShape( symbolPin->GetShape() );
  1710. if( symbolPin->IsDangling() )
  1711. tempPin->SetFlags( IS_DANGLING );
  1712. }
  1713. TRANSFORM temp = GetTransform();
  1714. aPlotter->StartBlock( nullptr );
  1715. for( bool local_background : { true, false } )
  1716. {
  1717. tempSymbol.Plot( aPlotter, GetUnit(), GetConvert(), local_background, m_pos, temp,
  1718. GetDNP() );
  1719. for( const SCH_FIELD& field : m_fields )
  1720. field.Plot( aPlotter, local_background );
  1721. }
  1722. if( m_DNP )
  1723. PlotDNP( aPlotter );
  1724. // Plot attributes to a hypertext menu
  1725. std::vector<wxString> properties;
  1726. SCH_SHEET_PATH* sheet = &Schematic()->CurrentSheet();
  1727. for( const SCH_FIELD& field : GetFields() )
  1728. {
  1729. wxString text_field = field.GetShownText( sheet, false);
  1730. if( text_field.IsEmpty() )
  1731. continue;
  1732. properties.emplace_back( wxString::Format( wxT( "!%s = %s" ),
  1733. field.GetName(), text_field ) );
  1734. }
  1735. if( !m_part->GetKeyWords().IsEmpty() )
  1736. {
  1737. properties.emplace_back( wxString::Format( wxT( "!%s = %s" ),
  1738. _( "Keywords" ),
  1739. m_part->GetKeyWords() ) );
  1740. }
  1741. aPlotter->HyperlinkMenu( GetBoundingBox(), properties );
  1742. aPlotter->EndBlock( nullptr );
  1743. if( !m_part->IsPower() )
  1744. aPlotter->Bookmark( GetBoundingBox(), GetRef( sheet ), _( "Symbols" ) );
  1745. }
  1746. }
  1747. void SCH_SYMBOL::PlotDNP( PLOTTER* aPlotter ) const
  1748. {
  1749. BOX2I bbox = GetBodyAndPinsBoundingBox();
  1750. COLOR_SETTINGS* colors = Pgm().GetSettingsManager().GetColorSettings();
  1751. aPlotter->SetColor( colors->GetColor( LAYER_DNP_MARKER ) );
  1752. aPlotter->ThickSegment( bbox.GetOrigin(), bbox.GetEnd(),
  1753. 3.0 * schIUScale.MilsToIU( DEFAULT_LINE_WIDTH_MILS ),
  1754. FILLED, nullptr );
  1755. aPlotter->ThickSegment( bbox.GetOrigin() + VECTOR2I( bbox.GetWidth(), 0 ),
  1756. bbox.GetOrigin() + VECTOR2I( 0, bbox.GetHeight() ),
  1757. 3.0 * schIUScale.MilsToIU( DEFAULT_LINE_WIDTH_MILS ),
  1758. FILLED, nullptr );
  1759. }
  1760. void SCH_SYMBOL::PlotPins( PLOTTER* aPlotter ) const
  1761. {
  1762. if( m_part )
  1763. {
  1764. LIB_PINS libPins;
  1765. m_part->GetPins( libPins, GetUnit(), GetConvert() );
  1766. // Copy the source to stay const
  1767. LIB_SYMBOL tempSymbol( *m_part );
  1768. LIB_PINS tempPins;
  1769. tempSymbol.GetPins( tempPins, GetUnit(), GetConvert() );
  1770. TRANSFORM transform = GetTransform();
  1771. // Copy the pin info from the symbol to the temp pins
  1772. for( unsigned i = 0; i < tempPins.size(); ++ i )
  1773. {
  1774. SCH_PIN* symbolPin = GetPin( libPins[ i ] );
  1775. LIB_PIN* tempPin = tempPins[ i ];
  1776. tempPin->SetName( symbolPin->GetShownName() );
  1777. tempPin->SetType( symbolPin->GetType() );
  1778. tempPin->SetShape( symbolPin->GetShape() );
  1779. tempPin->Plot( aPlotter, false, m_pos, transform, GetDNP() );
  1780. }
  1781. }
  1782. }
  1783. bool SCH_SYMBOL::HasBrightenedPins()
  1784. {
  1785. for( const auto& pin : m_pins )
  1786. {
  1787. if( pin->IsBrightened() )
  1788. return true;
  1789. }
  1790. return false;
  1791. }
  1792. void SCH_SYMBOL::ClearBrightenedPins()
  1793. {
  1794. for( auto& pin : m_pins )
  1795. pin->ClearBrightened();
  1796. }
  1797. bool SCH_SYMBOL::IsPointClickableAnchor( const VECTOR2I& aPos ) const
  1798. {
  1799. for( const std::unique_ptr<SCH_PIN>& pin : m_pins )
  1800. {
  1801. int pin_unit = pin->GetLibPin()->GetUnit();
  1802. int pin_convert = pin->GetLibPin()->GetConvert();
  1803. if( pin_unit > 0 && pin_unit != GetUnit() )
  1804. continue;
  1805. if( pin_convert > 0 && pin_convert != GetConvert() )
  1806. continue;
  1807. if( pin->IsPointClickableAnchor( aPos ) )
  1808. return true;
  1809. }
  1810. return false;
  1811. }
  1812. bool SCH_SYMBOL::IsSymbolLikePowerGlobalLabel() const
  1813. {
  1814. // return true if the symbol is equivalent to a global label:
  1815. // It is a Power symbol
  1816. // It has only one pin type Power input
  1817. if( !GetLibSymbolRef() || !GetLibSymbolRef()->IsPower() )
  1818. return false;
  1819. std::vector<LIB_PIN*> pin_list = GetAllLibPins();
  1820. if( pin_list.size() != 1 )
  1821. return false;
  1822. return pin_list[0]->GetType() == ELECTRICAL_PINTYPE::PT_POWER_IN;
  1823. }
  1824. bool SCH_SYMBOL::IsPower() const
  1825. {
  1826. wxCHECK( m_part, false );
  1827. return m_part->IsPower();
  1828. }
  1829. static struct SCH_SYMBOL_DESC
  1830. {
  1831. SCH_SYMBOL_DESC()
  1832. {
  1833. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  1834. REGISTER_TYPE( SCH_SYMBOL );
  1835. propMgr.InheritsAfter( TYPE_HASH( SCH_SYMBOL ), TYPE_HASH( SCH_ITEM ) );
  1836. const wxString groupAttributes = _HKI( "Attributes" );
  1837. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, bool>( _HKI( "Exclude from board" ),
  1838. &SCH_SYMBOL::SetExcludedFromBoard, &SCH_SYMBOL::GetExcludedFromBoard ),
  1839. groupAttributes );
  1840. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, bool>( _HKI( "Exclude from simulation" ),
  1841. &SCH_SYMBOL::SetExcludedFromSim, &SCH_SYMBOL::GetExcludedFromSim ),
  1842. groupAttributes );
  1843. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, bool>( _HKI( "Exclude from bill of materials" ),
  1844. &SCH_SYMBOL::SetExcludedFromBOM, &SCH_SYMBOL::GetExcludedFromBOM ),
  1845. groupAttributes );
  1846. propMgr.AddProperty( new PROPERTY<SCH_SYMBOL, bool>( _HKI( "Do not populate" ),
  1847. &SCH_SYMBOL::SetDNP, &SCH_SYMBOL::GetDNP ),
  1848. groupAttributes );
  1849. }
  1850. } _SCH_SYMBOL_DESC;