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.

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