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.

2578 lines
70 KiB

++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
14 years ago
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
14 years ago
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
14 years ago
12 years ago
19 years ago
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
14 years ago
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
14 years ago
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
14 years ago
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
14 years ago
14 years ago
14 years ago
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
14 years ago
14 years ago
14 years ago
18 years ago
18 years ago
17 years ago
17 years ago
17 years ago
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
14 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
18 years ago
Changed the way of looking up NETINFO_ITEM using net names (using boost::unordered_map). Added a hash function (wxString) for that. Introduced NETINFO_ITEM::GetNetItem( wxString ). BOARD::FindNet() uses the map. Net codes are updated upon net list update. (BOARD::ReplaceNetlist()) Added in some places (mostly class_board.cpp) pad->SetNet() calls to synchronize net codes. On creation of NETINFO_LIST, the first NETINFO_ITEM is added (the unconnected items net). Removed COMPONENT_NET::m_netNumber, as it was not used anywhere. Added an assert to D_PAD::GetNetname(), checking if net code and net name is consistent for unconnected pads. Added an assert for NETINFO_LIST::AppendNet() to assure that appended nets are unique. It seems that at this point: - Updating net lists works fine. The only difference between the file ouput is that after changes it contains empty nets as well. - Nets are not saved in the lexical order. Still, net names and net codes are properly assigned to all items in the .kicad_pcb file. It is going to be addressed in the next commit. I believe it should not create any problems, as pads are sorted by their net names anyway (NETINFO_LIST::buildPadsFullList()) Performed tests: - Created a blank PCB, saved as pic_programmer.kicad_pcb (from demos folder). Updated net lists. .kicad_pcb file (comparing to the results from master branch) differ with net order (as mentioned before), net codes and timestamps. - Removed some of components from the above .kicad_pcb file and updated net lists. Modules reappeared. .kicad_pcb file differs in the same way as described above. - Trying to change a pad net name (via properties dialog) results in assert being fired. It is done on purpose (as there is a call to GetNetname() and net name and net code do not match). This will not happen after the next commit. - Prepared a simple project (starting with schematics). Imported net list, changed schematic, reimported net list - changes are applied. - Eagle & KiCad legacy boards seem to load without any problem.
12 years ago
18 years ago
18 years ago
18 years ago
11 years ago
11 years ago
11 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
11 years ago
11 years ago
11 years ago
8 years ago
8 years ago
8 years ago
8 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
14 years ago
// Dick Hollenbeck's KiROUND R&D // This provides better project control over rounding to int from double // than wxRound() did. This scheme provides better logging in Debug builds // and it provides for compile time calculation of constants. #include <stdio.h> #include <assert.h> #include <limits.h> //-----<KiROUND KIT>------------------------------------------------------------ /** * KiROUND * rounds a floating point number to an int using * "round halfway cases away from zero". * In Debug build an assert fires if will not fit into an int. */ #if defined( DEBUG ) // DEBUG: a macro to capture line and file, then calls this inline static inline int KiRound( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v ); } else if( v < INT_MIN - 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v ); } return int( v ); } #define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) #else // RELEASE: a macro so compile can pre-compute constants. #define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) #endif //-----</KiROUND KIT>----------------------------------------------------------- // Only a macro is compile time calculated, an inline function causes a static constructor // in a situation like this. // Therefore the Release build is best done with a MACRO not an inline function. int Computed = KiROUND( 14.3 * 8 ); int main( int argc, char** argv ) { for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 ) { int i = KiROUND( d ); printf( "t: %d %.16g\n", i, d ); } return 0; }
14 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /**
  2. * @file class_board.cpp
  3. * @brief BOARD class functions.
  4. */
  5. /*
  6. * This program source code file is part of KiCad, a free EDA CAD application.
  7. *
  8. * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  9. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  10. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
  11. *
  12. * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, you may find one here:
  26. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  27. * or you may search the http://www.gnu.org website for the version 2 license,
  28. * or you may write to the Free Software Foundation, Inc.,
  29. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  30. */
  31. #include <limits.h>
  32. #include <algorithm>
  33. #include <iterator>
  34. #include <fctsys.h>
  35. #include <common.h>
  36. #include <kicad_string.h>
  37. #include <pcb_base_frame.h>
  38. #include <msgpanel.h>
  39. #include <pcb_netlist.h>
  40. #include <reporter.h>
  41. #include <base_units.h>
  42. #include <ratsnest_data.h>
  43. #include <ratsnest_viewitem.h>
  44. #include <worksheet_viewitem.h>
  45. #include <pcbnew.h>
  46. #include <collectors.h>
  47. #include <class_board.h>
  48. #include <class_module.h>
  49. #include <class_track.h>
  50. #include <class_zone.h>
  51. #include <class_marker_pcb.h>
  52. #include <class_drawsegment.h>
  53. #include <class_pcb_text.h>
  54. #include <class_pcb_target.h>
  55. #include <class_dimension.h>
  56. #include <connectivity/connectivity_data.h>
  57. /**
  58. * A singleton item of this class is returned for a weak reference that no longer exists.
  59. * Its sole purpose is to flag the item as having been deleted.
  60. */
  61. class DELETED_BOARD_ITEM : public BOARD_ITEM
  62. {
  63. public:
  64. DELETED_BOARD_ITEM() :
  65. BOARD_ITEM( nullptr, NOT_USED )
  66. {}
  67. wxString GetSelectMenuText( EDA_UNITS_T aUnits ) const override
  68. {
  69. return _( "(Deleted Item)" );
  70. }
  71. wxString GetClass() const override
  72. {
  73. return wxT( "DELETED_BOARD_ITEM" );
  74. }
  75. // pure virtuals:
  76. const wxPoint GetPosition() const override { return wxPoint(); }
  77. void SetPosition( const wxPoint& ) override {}
  78. void Draw( EDA_DRAW_PANEL* , wxDC* , GR_DRAWMODE , const wxPoint& ) override {}
  79. #if defined(DEBUG)
  80. void Show( int , std::ostream& ) const override {}
  81. #endif
  82. };
  83. DELETED_BOARD_ITEM g_DeletedItem;
  84. /* This is an odd place for this, but CvPcb won't link if it is
  85. * in class_board_item.cpp like I first tried it.
  86. */
  87. wxPoint BOARD_ITEM::ZeroOffset( 0, 0 );
  88. // this is a dummy colors settings (defined colors are the vdefulat values)
  89. // used to initialize the board.
  90. // these settings will be overriden later, depending on the draw frame that displays the board.
  91. // However, when a board is created by a python script, outside a frame, the colors must be set
  92. // so dummyColorsSettings provide this default initialization
  93. static COLORS_DESIGN_SETTINGS dummyColorsSettings( FRAME_PCB );
  94. BOARD::BOARD() :
  95. BOARD_ITEM_CONTAINER( (BOARD_ITEM*) NULL, PCB_T ),
  96. m_paper( PAGE_INFO::A4 ), m_NetInfo( this )
  97. {
  98. // we have not loaded a board yet, assume latest until then.
  99. m_fileFormatVersionAtLoad = LEGACY_BOARD_FILE_VERSION;
  100. m_colorsSettings = &dummyColorsSettings;
  101. m_Status_Pcb = 0; // Status word: bit 1 = calculate.
  102. m_CurrentZoneContour = NULL; // This ZONE_CONTAINER handle the
  103. // zone contour currently in progress
  104. BuildListOfNets(); // prepare pad and netlist containers.
  105. for( LAYER_NUM layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
  106. {
  107. m_Layer[layer].m_name = GetStandardLayerName( ToLAYER_ID( layer ) );
  108. if( IsCopperLayer( layer ) )
  109. m_Layer[layer].m_type = LT_SIGNAL;
  110. else
  111. m_Layer[layer].m_type = LT_UNDEFINED;
  112. }
  113. // Initialize default netclass.
  114. NETCLASSPTR defaultClass = m_designSettings.GetDefault();
  115. defaultClass->SetDescription( _( "This is the default net class." ) );
  116. m_designSettings.SetCurrentNetClass( defaultClass->GetName() );
  117. // Set sensible initial values for custom track width & via size
  118. m_designSettings.UseCustomTrackViaSize( false );
  119. m_designSettings.SetCustomTrackWidth( m_designSettings.GetCurrentTrackWidth() );
  120. m_designSettings.SetCustomViaSize( m_designSettings.GetCurrentViaSize() );
  121. m_designSettings.SetCustomViaDrill( m_designSettings.GetCurrentViaDrill() );
  122. // Initialize ratsnest
  123. m_connectivity.reset( new CONNECTIVITY_DATA() );
  124. }
  125. BOARD::~BOARD()
  126. {
  127. while( m_ZoneDescriptorList.size() )
  128. {
  129. ZONE_CONTAINER* area_to_remove = m_ZoneDescriptorList[0];
  130. Delete( area_to_remove );
  131. }
  132. DeleteMARKERs();
  133. DeleteZONEOutlines();
  134. delete m_CurrentZoneContour;
  135. m_CurrentZoneContour = NULL;
  136. }
  137. void BOARD::BuildConnectivity()
  138. {
  139. GetConnectivity()->Build( this );
  140. }
  141. const wxPoint BOARD::GetPosition() const
  142. {
  143. return ZeroOffset;
  144. }
  145. void BOARD::SetPosition( const wxPoint& aPos )
  146. {
  147. wxLogWarning( wxT( "This should not be called on the BOARD object") );
  148. }
  149. void BOARD::Move( const wxPoint& aMoveVector ) // overload
  150. {
  151. // @todo : anything like this elsewhere? maybe put into GENERAL_COLLECTOR class.
  152. static const KICAD_T top_level_board_stuff[] = {
  153. PCB_MARKER_T,
  154. PCB_TEXT_T,
  155. PCB_LINE_T,
  156. PCB_DIMENSION_T,
  157. PCB_TARGET_T,
  158. PCB_VIA_T,
  159. PCB_TRACE_T,
  160. // PCB_PAD_T, Can't be at board level
  161. // PCB_MODULE_TEXT_T, Can't be at board level
  162. PCB_MODULE_T,
  163. PCB_ZONE_AREA_T,
  164. EOT
  165. };
  166. INSPECTOR_FUNC inspector = [&] ( EDA_ITEM* item, void* testData )
  167. {
  168. BOARD_ITEM* brd_item = (BOARD_ITEM*) item;
  169. // aMoveVector was snapshotted, don't need "data".
  170. brd_item->Move( aMoveVector );
  171. return SEARCH_CONTINUE;
  172. };
  173. Visit( inspector, NULL, top_level_board_stuff );
  174. }
  175. TRACKS BOARD::TracksInNet( int aNetCode )
  176. {
  177. TRACKS ret;
  178. INSPECTOR_FUNC inspector = [aNetCode,&ret] ( EDA_ITEM* item, void* testData )
  179. {
  180. TRACK* t = (TRACK*) item;
  181. if( t->GetNetCode() == aNetCode )
  182. ret.push_back( t );
  183. return SEARCH_CONTINUE;
  184. };
  185. // visit this BOARD's TRACKs and VIAs with above TRACK INSPECTOR which
  186. // appends all in aNetCode to ret.
  187. Visit( inspector, NULL, GENERAL_COLLECTOR::Tracks );
  188. return ret;
  189. }
  190. /**
  191. * Function removeTrack
  192. * removes aOneToRemove from aList, which is a non-owning std::vector
  193. */
  194. static void removeTrack( TRACKS* aList, TRACK* aOneToRemove )
  195. {
  196. aList->erase( std::remove( aList->begin(), aList->end(), aOneToRemove ), aList->end() );
  197. }
  198. static void otherEnd( const TRACK& aTrack, const wxPoint& aNotThisEnd, wxPoint* aOtherEnd )
  199. {
  200. if( aTrack.GetStart() == aNotThisEnd )
  201. {
  202. *aOtherEnd = aTrack.GetEnd();
  203. }
  204. else
  205. {
  206. wxASSERT( aTrack.GetEnd() == aNotThisEnd );
  207. *aOtherEnd = aTrack.GetStart();
  208. }
  209. }
  210. /**
  211. * Function find_vias_and_tracks_at
  212. * collects TRACKs and VIAs at aPos and returns the @a track_count which excludes vias.
  213. */
  214. static int find_vias_and_tracks_at( TRACKS& at_next, TRACKS& in_net, LSET& lset, const wxPoint& next )
  215. {
  216. // first find all vias (in this net) at 'next' location, and expand LSET with each
  217. for( TRACKS::iterator it = in_net.begin(); it != in_net.end(); )
  218. {
  219. TRACK* t = *it;
  220. if( t->Type() == PCB_VIA_T && (t->GetLayerSet() & lset).any() &&
  221. ( t->GetStart() == next || t->GetEnd() == next ) )
  222. {
  223. lset |= t->GetLayerSet();
  224. at_next.push_back( t );
  225. it = in_net.erase( it );
  226. }
  227. else
  228. ++it;
  229. }
  230. int track_count = 0;
  231. // with expanded lset, find all tracks with an end on any of the layers in lset
  232. for( TRACKS::iterator it = in_net.begin(); it != in_net.end(); /* iterates in the loop body */ )
  233. {
  234. TRACK* t = *it;
  235. if( ( t->GetLayerSet() & lset ).any() && ( t->GetStart() == next || t->GetEnd() == next ) )
  236. {
  237. at_next.push_back( t );
  238. it = in_net.erase( it );
  239. ++track_count;
  240. }
  241. else
  242. {
  243. ++it;
  244. }
  245. }
  246. return track_count;
  247. }
  248. /**
  249. * Function checkConnectedTo
  250. * returns if aTracksInNet contains a copper pathway to aGoal when starting with
  251. * aFirstTrack. aFirstTrack should have one end situated on aStart, and the
  252. * traversal testing begins from the other end of aFirstTrack.
  253. * <p>
  254. * The function throws an exception instead of returning bool so that detailed
  255. * information can be provided about a possible failure in the track layout.
  256. *
  257. * @throw IO_ERROR - if points are not connected, with text saying why.
  258. */
  259. static void checkConnectedTo( BOARD* aBoard, TRACKS* aList, const TRACKS& aTracksInNet,
  260. const wxPoint& aGoal, const wxPoint& aStart, TRACK* aFirstTrack )
  261. {
  262. TRACKS in_net = aTracksInNet; // copy source list so the copy can be modified
  263. wxPoint next;
  264. otherEnd( *aFirstTrack, aStart, &next );
  265. aList->push_back( aFirstTrack );
  266. removeTrack( &in_net, aFirstTrack );
  267. LSET lset( aFirstTrack->GetLayer() );
  268. while( in_net.size() )
  269. {
  270. if( next == aGoal )
  271. return; // success
  272. // Want an exact match on the position of next, i.e. pad at next,
  273. // not a forgiving HitTest() with tolerance type of match, otherwise the overall
  274. // algorithm will not work. GetPadFast() is an exact match as I write this.
  275. if( aBoard->GetPadFast( next, lset ) )
  276. {
  277. std::string m = StrPrintf(
  278. "intervening pad at:(xy %s) between start:(xy %s) and goal:(xy %s)",
  279. FormatInternalUnits( next ).c_str(),
  280. FormatInternalUnits( aStart ).c_str(),
  281. FormatInternalUnits( aGoal ).c_str()
  282. );
  283. THROW_IO_ERROR( m );
  284. }
  285. int track_count = find_vias_and_tracks_at( *aList, in_net, lset, next );
  286. if( track_count != 1 )
  287. {
  288. std::string m = StrPrintf(
  289. "found %d tracks intersecting at (xy %s), exactly 2 would be acceptable.",
  290. track_count + aList->size() == 1 ? 1 : 0,
  291. FormatInternalUnits( next ).c_str()
  292. );
  293. THROW_IO_ERROR( m );
  294. }
  295. // reduce lset down to the layer that the last track at 'next' is on.
  296. lset = aList->back()->GetLayerSet();
  297. otherEnd( *aList->back(), next, &next );
  298. }
  299. std::string m = StrPrintf(
  300. "not enough tracks connecting start:(xy %s) and goal:(xy %s).",
  301. FormatInternalUnits( aStart ).c_str(),
  302. FormatInternalUnits( aGoal ).c_str()
  303. );
  304. THROW_IO_ERROR( m );
  305. }
  306. TRACKS BOARD::TracksInNetBetweenPoints( const wxPoint& aStartPos, const wxPoint& aGoalPos, int aNetCode )
  307. {
  308. TRACKS in_between_pts;
  309. TRACKS on_start_point;
  310. TRACKS in_net = TracksInNet( aNetCode ); // a small subset of TRACKs and VIAs
  311. for( auto t : in_net )
  312. {
  313. if( t->Type() == PCB_TRACE_T && ( t->GetStart() == aStartPos || t->GetEnd() == aStartPos ) )
  314. on_start_point.push_back( t );
  315. }
  316. wxString per_path_problem_text;
  317. for( auto t : on_start_point ) // explore each trace (path) leaving aStartPos
  318. {
  319. // checkConnectedTo() fills in_between_pts on every attempt. For failures
  320. // this set needs to be cleared.
  321. in_between_pts.clear();
  322. try
  323. {
  324. checkConnectedTo( this, &in_between_pts, in_net, aGoalPos, aStartPos, t );
  325. }
  326. catch( const IO_ERROR& ioe ) // means not connected
  327. {
  328. per_path_problem_text += "\n\t";
  329. per_path_problem_text += ioe.Problem();
  330. continue; // keep trying, there may be other paths leaving from aStartPos
  331. }
  332. // success, no exception means a valid connection,
  333. // return this set of TRACKS without throwing.
  334. return in_between_pts;
  335. }
  336. wxString m = wxString::Format(
  337. "no clean path connecting start:(xy %s) with goal:(xy %s)",
  338. FormatInternalUnits( aStartPos ).c_str(),
  339. FormatInternalUnits( aGoalPos ).c_str()
  340. );
  341. THROW_IO_ERROR( m + per_path_problem_text );
  342. }
  343. void BOARD::chainMarkedSegments( TRACK* aTrackList, wxPoint aPosition,
  344. const LSET& aLayerSet, TRACKS* aList )
  345. {
  346. LSET layer_set = aLayerSet;
  347. if( !aTrackList ) // no tracks at all in board
  348. return;
  349. D_PAD* pad = NULL;
  350. double distanceToPadCenter = std::numeric_limits<double>::max();
  351. /* Set the BUSY flag of all connected segments, first search starting at
  352. * aPosition. The search ends when a pad is found (end of a track), a
  353. * segment end has more than one other segment end connected, or when no
  354. * connected item found.
  355. *
  356. * Vias are a special case because they can connect segments
  357. * on other layers and they change the layer mask. They can be a track
  358. * end or not. They will be analyzed later, and vias on terminal points
  359. * of the track will be considered as part of this track if they do not
  360. * connect segments on a other track together and will be considered as
  361. * part of a other track when removing the via, the segments of that other
  362. * track are disconnected.
  363. */
  364. for( ; ; )
  365. {
  366. if( !pad )
  367. pad = GetPad( aPosition, layer_set );
  368. if( pad )
  369. distanceToPadCenter = GetLineLength( aPosition, pad->GetCenter() );
  370. /* Test for a via: a via changes the layer mask and can connect a lot
  371. * of segments at location aPosition. When found, the via is just
  372. * pushed in list. Vias will be examined later, when all connected
  373. * segment are found and push in list. This is because when a via
  374. * is found we do not know at this time the number of connected items
  375. * and we do not know if this via is on the track or finish the track
  376. */
  377. TRACK* via = aTrackList->GetVia( NULL, aPosition, layer_set );
  378. if( via )
  379. {
  380. layer_set = via->GetLayerSet();
  381. aList->push_back( via );
  382. }
  383. int seg_count = 0;
  384. TRACK* candidate = NULL;
  385. /* Search all segments connected to point aPosition.
  386. * if only 1 segment at aPosition: then this segment is "candidate"
  387. * if > 1 segment:
  388. * then end of "track" (because more than 2 segments are connected at aPosition)
  389. */
  390. TRACK* segment = aTrackList;
  391. while( ( segment = ::GetTrack( segment, NULL, aPosition, layer_set ) ) != NULL )
  392. {
  393. if( segment->GetState( BUSY ) ) // already found and selected: skip it
  394. {
  395. segment = segment->Next();
  396. continue;
  397. }
  398. if( segment == via ) // just previously found: skip it
  399. {
  400. segment = segment->Next();
  401. continue;
  402. }
  403. if( ++seg_count == 1 ) // if first connected item: then segment is candidate
  404. {
  405. candidate = segment;
  406. segment = segment->Next();
  407. }
  408. else // More than 1 segment connected -> location is end of track
  409. {
  410. return;
  411. }
  412. }
  413. if( candidate ) // A candidate is found: flag it and push it in list
  414. {
  415. /* Initialize parameters to search items connected to this
  416. * candidate:
  417. * we must analyze connections to its other end
  418. */
  419. if( aPosition == candidate->GetStart() )
  420. {
  421. aPosition = candidate->GetEnd();
  422. }
  423. else
  424. {
  425. aPosition = candidate->GetStart();
  426. }
  427. /* If we are in a pad, only candidates approaching the pad center
  428. * are accepted.
  429. */
  430. if( pad )
  431. {
  432. if( GetPad( aPosition, layer_set ) != pad )
  433. return;
  434. if( GetLineLength( aPosition, pad->GetCenter() ) > distanceToPadCenter )
  435. return;
  436. }
  437. layer_set = candidate->GetLayerSet();
  438. // flag this item and push it in list of selected items
  439. aList->push_back( candidate );
  440. candidate->SetState( BUSY, true );
  441. }
  442. else
  443. {
  444. return;
  445. }
  446. }
  447. }
  448. void BOARD::PushHighLight()
  449. {
  450. m_highLightPrevious = m_highLight;
  451. }
  452. void BOARD::PopHighLight()
  453. {
  454. m_highLight = m_highLightPrevious;
  455. m_highLightPrevious.Clear();
  456. }
  457. bool BOARD::SetLayerDescr( PCB_LAYER_ID aIndex, const LAYER& aLayer )
  458. {
  459. if( unsigned( aIndex ) < arrayDim( m_Layer ) )
  460. {
  461. m_Layer[ aIndex ] = aLayer;
  462. return true;
  463. }
  464. return false;
  465. }
  466. #include <stdio.h>
  467. const PCB_LAYER_ID BOARD::GetLayerID( const wxString& aLayerName ) const
  468. {
  469. // Look for the BOARD specific copper layer names
  470. for( LAYER_NUM layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
  471. {
  472. if ( IsCopperLayer( layer ) && ( m_Layer[ layer ].m_name == aLayerName ) )
  473. {
  474. return ToLAYER_ID( layer );
  475. }
  476. }
  477. // Otherwise fall back to the system standard layer names
  478. for( LAYER_NUM layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
  479. {
  480. if( GetStandardLayerName( ToLAYER_ID( layer ) ) == aLayerName )
  481. {
  482. return ToLAYER_ID( layer );
  483. }
  484. }
  485. return UNDEFINED_LAYER;
  486. }
  487. const wxString BOARD::GetLayerName( PCB_LAYER_ID aLayer ) const
  488. {
  489. // All layer names are stored in the BOARD.
  490. if( IsLayerEnabled( aLayer ) )
  491. {
  492. // Standard names were set in BOARD::BOARD() but they may be
  493. // over-ridden by BOARD::SetLayerName().
  494. // For copper layers, return the actual copper layer name,
  495. // otherwise return the Standard English layer name.
  496. if( IsCopperLayer( aLayer ) )
  497. return m_Layer[aLayer].m_name;
  498. }
  499. return GetStandardLayerName( aLayer );
  500. }
  501. bool BOARD::SetLayerName( PCB_LAYER_ID aLayer, const wxString& aLayerName )
  502. {
  503. if( !IsCopperLayer( aLayer ) )
  504. return false;
  505. if( aLayerName == wxEmptyString )
  506. return false;
  507. // no quote chars in the name allowed
  508. if( aLayerName.Find( wxChar( '"' ) ) != wxNOT_FOUND )
  509. return false;
  510. wxString nameTemp = aLayerName;
  511. // replace any spaces with underscores before we do any comparing
  512. nameTemp.Replace( wxT( " " ), wxT( "_" ) );
  513. if( IsLayerEnabled( aLayer ) )
  514. {
  515. #if 0
  516. for( LAYER_NUM i = FIRST_COPPER_LAYER; i < NB_COPPER_LAYERS; ++i )
  517. {
  518. if( i != aLayer && IsLayerEnabled( i ) && nameTemp == m_Layer[i].m_Name )
  519. return false;
  520. }
  521. #else
  522. for( LSEQ cu = GetEnabledLayers().CuStack(); cu; ++cu )
  523. {
  524. PCB_LAYER_ID id = *cu;
  525. // veto changing the name if it exists elsewhere.
  526. if( id != aLayer && nameTemp == m_Layer[id].m_name )
  527. // if( id != aLayer && nameTemp == wxString( m_Layer[id].m_name ) )
  528. return false;
  529. }
  530. #endif
  531. m_Layer[aLayer].m_name = nameTemp;
  532. return true;
  533. }
  534. return false;
  535. }
  536. LAYER_T BOARD::GetLayerType( PCB_LAYER_ID aLayer ) const
  537. {
  538. if( !IsCopperLayer( aLayer ) )
  539. return LT_SIGNAL;
  540. //@@IMB: The original test was broken due to the discontinuity
  541. // in the layer sequence.
  542. if( IsLayerEnabled( aLayer ) )
  543. return m_Layer[aLayer].m_type;
  544. return LT_SIGNAL;
  545. }
  546. bool BOARD::SetLayerType( PCB_LAYER_ID aLayer, LAYER_T aLayerType )
  547. {
  548. if( !IsCopperLayer( aLayer ) )
  549. return false;
  550. //@@IMB: The original test was broken due to the discontinuity
  551. // in the layer sequence.
  552. if( IsLayerEnabled( aLayer ) )
  553. {
  554. m_Layer[aLayer].m_type = aLayerType;
  555. return true;
  556. }
  557. return false;
  558. }
  559. const char* LAYER::ShowType( LAYER_T aType )
  560. {
  561. const char* cp;
  562. switch( aType )
  563. {
  564. default:
  565. case LT_SIGNAL:
  566. cp = "signal";
  567. break;
  568. case LT_POWER:
  569. cp = "power";
  570. break;
  571. case LT_MIXED:
  572. cp = "mixed";
  573. break;
  574. case LT_JUMPER:
  575. cp = "jumper";
  576. break;
  577. }
  578. return cp;
  579. }
  580. LAYER_T LAYER::ParseType( const char* aType )
  581. {
  582. if( strcmp( aType, "signal" ) == 0 )
  583. return LT_SIGNAL;
  584. else if( strcmp( aType, "power" ) == 0 )
  585. return LT_POWER;
  586. else if( strcmp( aType, "mixed" ) == 0 )
  587. return LT_MIXED;
  588. else if( strcmp( aType, "jumper" ) == 0 )
  589. return LT_JUMPER;
  590. else
  591. return LT_UNDEFINED;
  592. }
  593. int BOARD::GetCopperLayerCount() const
  594. {
  595. return m_designSettings.GetCopperLayerCount();
  596. }
  597. void BOARD::SetCopperLayerCount( int aCount )
  598. {
  599. m_designSettings.SetCopperLayerCount( aCount );
  600. }
  601. LSET BOARD::GetEnabledLayers() const
  602. {
  603. return m_designSettings.GetEnabledLayers();
  604. }
  605. LSET BOARD::GetVisibleLayers() const
  606. {
  607. return m_designSettings.GetVisibleLayers();
  608. }
  609. void BOARD::SetEnabledLayers( LSET aLayerSet )
  610. {
  611. m_designSettings.SetEnabledLayers( aLayerSet );
  612. }
  613. void BOARD::SetVisibleLayers( LSET aLayerSet )
  614. {
  615. m_designSettings.SetVisibleLayers( aLayerSet );
  616. }
  617. void BOARD::SetVisibleElements( int aMask )
  618. {
  619. // Call SetElementVisibility for each item
  620. // to ensure specific calculations that can be needed by some items,
  621. // just changing the visibility flags could be not sufficient.
  622. for( GAL_LAYER_ID ii = GAL_LAYER_ID_START; ii < GAL_LAYER_ID_BITMASK_END; ++ii )
  623. {
  624. int item_mask = 1 << GAL_LAYER_INDEX( ii );
  625. SetElementVisibility( ii, aMask & item_mask );
  626. }
  627. }
  628. void BOARD::SetVisibleAlls()
  629. {
  630. SetVisibleLayers( LSET().set() );
  631. // Call SetElementVisibility for each item,
  632. // to ensure specific calculations that can be needed by some items
  633. for( GAL_LAYER_ID ii = GAL_LAYER_ID_START; ii < GAL_LAYER_ID_BITMASK_END; ++ii )
  634. SetElementVisibility( ii, true );
  635. }
  636. int BOARD::GetVisibleElements() const
  637. {
  638. return m_designSettings.GetVisibleElements();
  639. }
  640. bool BOARD::IsElementVisible( GAL_LAYER_ID aLayer ) const
  641. {
  642. return m_designSettings.IsElementVisible( aLayer );
  643. }
  644. void BOARD::SetElementVisibility( GAL_LAYER_ID aLayer, bool isEnabled )
  645. {
  646. m_designSettings.SetElementVisibility( aLayer, isEnabled );
  647. switch( aLayer )
  648. {
  649. case LAYER_RATSNEST:
  650. {
  651. bool visible = IsElementVisible( LAYER_RATSNEST );
  652. // we must clear or set the CH_VISIBLE flags to hide/show ratsnest
  653. // because we have a tool to show/hide ratsnest relative to a pad or a module
  654. // so the hide/show option is a per item selection
  655. for( unsigned int net = 1 /* skip "No Net" at [0] */; net < GetNetCount(); net++ )
  656. {
  657. auto rn = GetConnectivity()->GetRatsnestForNet( net );
  658. if( rn )
  659. rn->SetVisible( visible );
  660. }
  661. for( auto track : Tracks() )
  662. track->SetLocalRatsnestVisible( isEnabled );
  663. for( auto mod : Modules() )
  664. {
  665. for( auto pad : mod->Pads() )
  666. pad->SetLocalRatsnestVisible( isEnabled );
  667. }
  668. for( int i = 0; i<GetAreaCount(); i++ )
  669. {
  670. auto zone = GetArea( i );
  671. zone->SetLocalRatsnestVisible( isEnabled );
  672. }
  673. m_Status_Pcb = 0;
  674. break;
  675. }
  676. default:
  677. ;
  678. }
  679. }
  680. bool BOARD::IsModuleLayerVisible( PCB_LAYER_ID aLayer )
  681. {
  682. switch( aLayer )
  683. {
  684. case F_Cu:
  685. return IsElementVisible( LAYER_MOD_FR );
  686. case B_Cu:
  687. return IsElementVisible( LAYER_MOD_BK );
  688. default:
  689. wxFAIL_MSG( wxT( "BOARD::IsModuleLayerVisible() param error: bad layer" ) );
  690. return true;
  691. }
  692. }
  693. void BOARD::Add( BOARD_ITEM* aBoardItem, ADD_MODE aMode )
  694. {
  695. if( aBoardItem == NULL )
  696. {
  697. wxFAIL_MSG( wxT( "BOARD::Add() param error: aBoardItem NULL" ) );
  698. return;
  699. }
  700. switch( aBoardItem->Type() )
  701. {
  702. case PCB_NETINFO_T:
  703. m_NetInfo.AppendNet( (NETINFO_ITEM*) aBoardItem );
  704. break;
  705. // this one uses a vector
  706. case PCB_MARKER_T:
  707. m_markers.push_back( (MARKER_PCB*) aBoardItem );
  708. break;
  709. // this one uses a vector
  710. case PCB_ZONE_AREA_T:
  711. m_ZoneDescriptorList.push_back( (ZONE_CONTAINER*) aBoardItem );
  712. break;
  713. case PCB_TRACE_T:
  714. case PCB_VIA_T:
  715. if( aMode == ADD_APPEND )
  716. {
  717. m_Track.PushBack( (TRACK*) aBoardItem );
  718. }
  719. else
  720. {
  721. TRACK* insertAid;
  722. insertAid = ( (TRACK*) aBoardItem )->GetBestInsertPoint( this );
  723. m_Track.Insert( (TRACK*) aBoardItem, insertAid );
  724. }
  725. break;
  726. case PCB_MODULE_T:
  727. if( aMode == ADD_APPEND )
  728. m_Modules.PushBack( (MODULE*) aBoardItem );
  729. else
  730. m_Modules.PushFront( (MODULE*) aBoardItem );
  731. // Because the list of pads has changed, reset the status
  732. // This indicate the list of pad and nets must be recalculated before use
  733. m_Status_Pcb = 0;
  734. break;
  735. case PCB_DIMENSION_T:
  736. case PCB_LINE_T:
  737. case PCB_TEXT_T:
  738. case PCB_TARGET_T:
  739. if( aMode == ADD_APPEND )
  740. m_Drawings.PushBack( aBoardItem );
  741. else
  742. m_Drawings.PushFront( aBoardItem );
  743. break;
  744. // other types may use linked list
  745. default:
  746. {
  747. wxString msg;
  748. msg.Printf( wxT( "BOARD::Add() needs work: BOARD_ITEM type (%d) not handled" ),
  749. aBoardItem->Type() );
  750. wxFAIL_MSG( msg );
  751. return;
  752. }
  753. break;
  754. }
  755. aBoardItem->SetParent( this );
  756. m_connectivity->Add( aBoardItem );
  757. }
  758. void BOARD::Remove( BOARD_ITEM* aBoardItem )
  759. {
  760. // find these calls and fix them! Don't send me no stinking' NULL.
  761. wxASSERT( aBoardItem );
  762. switch( aBoardItem->Type() )
  763. {
  764. case PCB_NETINFO_T:
  765. {
  766. NETINFO_ITEM* item = (NETINFO_ITEM*) aBoardItem;
  767. m_NetInfo.RemoveNet( item );
  768. break;
  769. }
  770. case PCB_MARKER_T:
  771. // find the item in the vector, then remove it
  772. for( unsigned i = 0; i<m_markers.size(); ++i )
  773. {
  774. if( m_markers[i] == (MARKER_PCB*) aBoardItem )
  775. {
  776. m_markers.erase( m_markers.begin() + i );
  777. break;
  778. }
  779. }
  780. break;
  781. case PCB_ZONE_AREA_T: // this one uses a vector
  782. // find the item in the vector, then delete then erase it.
  783. for( unsigned i = 0; i<m_ZoneDescriptorList.size(); ++i )
  784. {
  785. if( m_ZoneDescriptorList[i] == (ZONE_CONTAINER*) aBoardItem )
  786. {
  787. m_ZoneDescriptorList.erase( m_ZoneDescriptorList.begin() + i );
  788. break;
  789. }
  790. }
  791. break;
  792. case PCB_MODULE_T:
  793. m_Modules.Remove( (MODULE*) aBoardItem );
  794. break;
  795. case PCB_TRACE_T:
  796. case PCB_VIA_T:
  797. m_Track.Remove( (TRACK*) aBoardItem );
  798. break;
  799. case PCB_DIMENSION_T:
  800. case PCB_LINE_T:
  801. case PCB_TEXT_T:
  802. case PCB_TARGET_T:
  803. m_Drawings.Remove( aBoardItem );
  804. break;
  805. // other types may use linked list
  806. default:
  807. wxFAIL_MSG( wxT( "BOARD::Remove() needs more ::Type() support" ) );
  808. }
  809. m_connectivity->Remove( aBoardItem );
  810. }
  811. wxString BOARD::GetSelectMenuText( EDA_UNITS_T aUnits ) const
  812. {
  813. return wxString::Format( _( "PCB" ) );
  814. }
  815. void BOARD::DeleteMARKERs()
  816. {
  817. // the vector does not know how to delete the MARKER_PCB, it holds pointers
  818. for( MARKER_PCB* marker : m_markers )
  819. delete marker;
  820. m_markers.clear();
  821. }
  822. void BOARD::DeleteZONEOutlines()
  823. {
  824. // the vector does not know how to delete the ZONE Outlines, it holds pointers
  825. for( ZONE_CONTAINER* zone : m_ZoneDescriptorList )
  826. delete zone;
  827. m_ZoneDescriptorList.clear();
  828. }
  829. BOARD_ITEM* BOARD::GetItem( void* aWeakReference )
  830. {
  831. for( TRACK* track : Tracks() )
  832. if( track == aWeakReference )
  833. return track;
  834. for( MODULE* module : Modules() )
  835. {
  836. if( module == aWeakReference )
  837. return module;
  838. for( D_PAD* pad : module->Pads() )
  839. if( pad == aWeakReference )
  840. return pad;
  841. if( &module->Reference() == aWeakReference )
  842. return &module->Reference();
  843. if( &module->Value() == aWeakReference )
  844. return &module->Value();
  845. for( BOARD_ITEM* drawing : module->GraphicalItems() )
  846. if( drawing == aWeakReference )
  847. return drawing;
  848. }
  849. for( ZONE_CONTAINER* zone : Zones() )
  850. if( zone == aWeakReference )
  851. return zone;
  852. for( BOARD_ITEM* drawing : Drawings() )
  853. if( drawing == aWeakReference )
  854. return drawing;
  855. // Not found; weak reference has been deleted.
  856. return &g_DeletedItem;
  857. }
  858. int BOARD::GetNumSegmTrack() const
  859. {
  860. return m_Track.GetCount();
  861. }
  862. unsigned BOARD::GetNodesCount( int aNet )
  863. {
  864. unsigned retval = 0;
  865. for( auto mod : Modules() )
  866. {
  867. for( auto pad : mod->Pads() )
  868. {
  869. if( ( aNet == -1 && pad->GetNetCode() > 0 ) || aNet == pad->GetNetCode() )
  870. retval++;
  871. }
  872. }
  873. return retval;
  874. }
  875. unsigned BOARD::GetUnconnectedNetCount() const
  876. {
  877. return m_connectivity->GetUnconnectedCount();
  878. }
  879. EDA_RECT BOARD::ComputeBoundingBox( bool aBoardEdgesOnly ) const
  880. {
  881. bool hasItems = false;
  882. EDA_RECT area;
  883. // Check segments, dimensions, texts, and fiducials
  884. for( BOARD_ITEM* item = m_Drawings; item; item = item->Next() )
  885. {
  886. if( aBoardEdgesOnly && (item->Type() != PCB_LINE_T || item->GetLayer() != Edge_Cuts ) )
  887. continue;
  888. if( !hasItems )
  889. area = item->GetBoundingBox();
  890. else
  891. area.Merge( item->GetBoundingBox() );
  892. hasItems = true;
  893. }
  894. if( !aBoardEdgesOnly )
  895. {
  896. // Check modules
  897. for( MODULE* module = m_Modules; module; module = module->Next() )
  898. {
  899. if( !hasItems )
  900. area = module->GetBoundingBox();
  901. else
  902. area.Merge( module->GetBoundingBox() );
  903. hasItems = true;
  904. }
  905. // Check tracks
  906. for( TRACK* track = m_Track; track; track = track->Next() )
  907. {
  908. if( !hasItems )
  909. area = track->GetBoundingBox();
  910. else
  911. area.Merge( track->GetBoundingBox() );
  912. hasItems = true;
  913. }
  914. // Check zones
  915. for( auto aZone : m_ZoneDescriptorList )
  916. {
  917. if( !hasItems )
  918. area = aZone->GetBoundingBox();
  919. else
  920. area.Merge( aZone->GetBoundingBox() );
  921. area.Merge( aZone->GetBoundingBox() );
  922. hasItems = true;
  923. }
  924. }
  925. return area;
  926. }
  927. void BOARD::GetMsgPanelInfo( EDA_UNITS_T aUnits, std::vector< MSG_PANEL_ITEM >& aList )
  928. {
  929. wxString txt;
  930. int viasCount = 0;
  931. int trackSegmentsCount = 0;
  932. for( BOARD_ITEM* item = m_Track; item; item = item->Next() )
  933. {
  934. if( item->Type() == PCB_VIA_T )
  935. viasCount++;
  936. else
  937. trackSegmentsCount++;
  938. }
  939. txt.Printf( wxT( "%d" ), GetPadCount() );
  940. aList.push_back( MSG_PANEL_ITEM( _( "Pads" ), txt, DARKGREEN ) );
  941. txt.Printf( wxT( "%d" ), viasCount );
  942. aList.push_back( MSG_PANEL_ITEM( _( "Vias" ), txt, DARKGREEN ) );
  943. txt.Printf( wxT( "%d" ), trackSegmentsCount );
  944. aList.push_back( MSG_PANEL_ITEM( _( "Track Segments" ), txt, DARKGREEN ) );
  945. txt.Printf( wxT( "%d" ), GetNodesCount() );
  946. aList.push_back( MSG_PANEL_ITEM( _( "Nodes" ), txt, DARKCYAN ) );
  947. txt.Printf( wxT( "%d" ), m_NetInfo.GetNetCount() - 1 /* Don't include "No Net" in count */ );
  948. aList.push_back( MSG_PANEL_ITEM( _( "Nets" ), txt, RED ) );
  949. txt.Printf( wxT( "%d" ), GetConnectivity()->GetUnconnectedCount() );
  950. aList.push_back( MSG_PANEL_ITEM( _( "Unrouted" ), txt, BLUE ) );
  951. }
  952. SEARCH_RESULT BOARD::Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] )
  953. {
  954. KICAD_T stype;
  955. SEARCH_RESULT result = SEARCH_CONTINUE;
  956. const KICAD_T* p = scanTypes;
  957. bool done = false;
  958. #if 0 && defined(DEBUG)
  959. std::cout << GetClass().mb_str() << ' ';
  960. #endif
  961. while( !done )
  962. {
  963. stype = *p;
  964. switch( stype )
  965. {
  966. case PCB_T:
  967. result = inspector( this, testData ); // inspect me
  968. // skip over any types handled in the above call.
  969. ++p;
  970. break;
  971. /* Instances of the requested KICAD_T live in a list, either one
  972. * that I manage, or that my modules manage. If it's a type managed
  973. * by class MODULE, then simply pass it on to each module's
  974. * MODULE::Visit() function by way of the
  975. * IterateForward( m_Modules, ... ) call.
  976. */
  977. case PCB_MODULE_T:
  978. case PCB_PAD_T:
  979. case PCB_MODULE_TEXT_T:
  980. case PCB_MODULE_EDGE_T:
  981. // this calls MODULE::Visit() on each module.
  982. result = IterateForward( m_Modules, inspector, testData, p );
  983. // skip over any types handled in the above call.
  984. for( ; ; )
  985. {
  986. switch( stype = *++p )
  987. {
  988. case PCB_MODULE_T:
  989. case PCB_PAD_T:
  990. case PCB_MODULE_TEXT_T:
  991. case PCB_MODULE_EDGE_T:
  992. continue;
  993. default:
  994. ;
  995. }
  996. break;
  997. }
  998. break;
  999. case PCB_LINE_T:
  1000. case PCB_TEXT_T:
  1001. case PCB_DIMENSION_T:
  1002. case PCB_TARGET_T:
  1003. result = IterateForward( m_Drawings, inspector, testData, p );
  1004. // skip over any types handled in the above call.
  1005. for( ; ; )
  1006. {
  1007. switch( stype = *++p )
  1008. {
  1009. case PCB_LINE_T:
  1010. case PCB_TEXT_T:
  1011. case PCB_DIMENSION_T:
  1012. case PCB_TARGET_T:
  1013. continue;
  1014. default:
  1015. ;
  1016. }
  1017. break;
  1018. }
  1019. ;
  1020. break;
  1021. #if 0 // both these are on same list, so we must scan it twice in order
  1022. // to get VIA priority, using new #else code below.
  1023. // But we are not using separate lists for TRACKs and VIA, because
  1024. // items are ordered (sorted) in the linked
  1025. // list by netcode AND by physical distance:
  1026. // when created, if a track or via is connected to an existing track or
  1027. // via, it is put in linked list after this existing track or via
  1028. // So usually, connected tracks or vias are grouped in this list
  1029. // So the algorithm (used in ratsnest computations) which computes the
  1030. // track connectivity is faster (more than 100 time regarding to
  1031. // a non ordered list) because when it searches for a connection, first
  1032. // it tests the near (near in term of linked list) 50 items
  1033. // from the current item (track or via) in test.
  1034. // Usually, because of this sort, a connected item (if exists) is
  1035. // found.
  1036. // If not found (and only in this case) an exhaustive (and time
  1037. // consuming) search is made, but this case is statistically rare.
  1038. case PCB_VIA_T:
  1039. case PCB_TRACE_T:
  1040. result = IterateForward( m_Track, inspector, testData, p );
  1041. // skip over any types handled in the above call.
  1042. for( ; ; )
  1043. {
  1044. switch( stype = *++p )
  1045. {
  1046. case PCB_VIA_T:
  1047. case PCB_TRACE_T:
  1048. continue;
  1049. default:
  1050. ;
  1051. }
  1052. break;
  1053. }
  1054. break;
  1055. #else
  1056. case PCB_VIA_T:
  1057. result = IterateForward( m_Track, inspector, testData, p );
  1058. ++p;
  1059. break;
  1060. case PCB_TRACE_T:
  1061. result = IterateForward( m_Track, inspector, testData, p );
  1062. ++p;
  1063. break;
  1064. #endif
  1065. case PCB_MARKER_T:
  1066. // MARKER_PCBS are in the m_markers std::vector
  1067. for( unsigned i = 0; i<m_markers.size(); ++i )
  1068. {
  1069. result = m_markers[i]->Visit( inspector, testData, p );
  1070. if( result == SEARCH_QUIT )
  1071. break;
  1072. }
  1073. ++p;
  1074. break;
  1075. case PCB_ZONE_AREA_T:
  1076. // PCB_ZONE_AREA_T are in the m_ZoneDescriptorList std::vector
  1077. for( unsigned i = 0; i< m_ZoneDescriptorList.size(); ++i )
  1078. {
  1079. result = m_ZoneDescriptorList[i]->Visit( inspector, testData, p );
  1080. if( result == SEARCH_QUIT )
  1081. break;
  1082. }
  1083. ++p;
  1084. break;
  1085. default: // catch EOT or ANY OTHER type here and return.
  1086. done = true;
  1087. break;
  1088. }
  1089. if( result == SEARCH_QUIT )
  1090. break;
  1091. }
  1092. return result;
  1093. }
  1094. NETINFO_ITEM* BOARD::FindNet( int aNetcode ) const
  1095. {
  1096. // the first valid netcode is 1 and the last is m_NetInfo.GetCount()-1.
  1097. // zero is reserved for "no connection" and is not actually a net.
  1098. // NULL is returned for non valid netcodes
  1099. wxASSERT( m_NetInfo.GetNetCount() > 0 );
  1100. if( aNetcode == NETINFO_LIST::UNCONNECTED && m_NetInfo.GetNetCount() == 0 )
  1101. return &NETINFO_LIST::ORPHANED_ITEM;
  1102. else
  1103. return m_NetInfo.GetNetItem( aNetcode );
  1104. }
  1105. NETINFO_ITEM* BOARD::FindNet( const wxString& aNetname ) const
  1106. {
  1107. return m_NetInfo.GetNetItem( aNetname );
  1108. }
  1109. MODULE* BOARD::FindModuleByReference( const wxString& aReference ) const
  1110. {
  1111. MODULE* found = nullptr;
  1112. // search only for MODULES
  1113. static const KICAD_T scanTypes[] = { PCB_MODULE_T, EOT };
  1114. INSPECTOR_FUNC inspector = [&] ( EDA_ITEM* item, void* testData )
  1115. {
  1116. MODULE* module = (MODULE*) item;
  1117. if( aReference == module->GetReference() )
  1118. {
  1119. found = module;
  1120. return SEARCH_QUIT;
  1121. }
  1122. return SEARCH_CONTINUE;
  1123. };
  1124. // visit this BOARD with the above inspector
  1125. BOARD* nonconstMe = (BOARD*) this;
  1126. nonconstMe->Visit( inspector, NULL, scanTypes );
  1127. return found;
  1128. }
  1129. MODULE* BOARD::FindModule( const wxString& aRefOrTimeStamp, bool aSearchByTimeStamp ) const
  1130. {
  1131. if( aSearchByTimeStamp )
  1132. {
  1133. for( MODULE* module = m_Modules; module; module = module->Next() )
  1134. {
  1135. if( aRefOrTimeStamp.CmpNoCase( module->GetPath() ) == 0 )
  1136. return module;
  1137. }
  1138. }
  1139. else
  1140. {
  1141. return FindModuleByReference( aRefOrTimeStamp );
  1142. }
  1143. return NULL;
  1144. }
  1145. // The pad count for each netcode, stored in a buffer for a fast access.
  1146. // This is needed by the sort function sortNetsByNodes()
  1147. static std::vector<int> padCountListByNet;
  1148. // Sort nets by decreasing pad count.
  1149. // For same pad count, sort by alphabetic names
  1150. static bool sortNetsByNodes( const NETINFO_ITEM* a, const NETINFO_ITEM* b )
  1151. {
  1152. int countA = padCountListByNet[a->GetNet()];
  1153. int countB = padCountListByNet[b->GetNet()];
  1154. if( countA == countB )
  1155. return a->GetNetname() < b->GetNetname();
  1156. else
  1157. return countB < countA;
  1158. }
  1159. // Sort nets by alphabetic names
  1160. static bool sortNetsByNames( const NETINFO_ITEM* a, const NETINFO_ITEM* b )
  1161. {
  1162. return a->GetNetname() < b->GetNetname();
  1163. }
  1164. int BOARD::SortedNetnamesList( wxArrayString& aNames, bool aSortbyPadsCount )
  1165. {
  1166. if( m_NetInfo.GetNetCount() == 0 )
  1167. return 0;
  1168. // Build the list
  1169. std::vector <NETINFO_ITEM*> netBuffer;
  1170. netBuffer.reserve( m_NetInfo.GetNetCount() );
  1171. int max_netcode = 0;
  1172. for( NETINFO_ITEM* net : m_NetInfo )
  1173. {
  1174. auto netcode = net->GetNet();
  1175. if( netcode > 0 && net->IsCurrent() )
  1176. {
  1177. netBuffer.push_back( net );
  1178. max_netcode = std::max( netcode, max_netcode);
  1179. }
  1180. }
  1181. // sort the list
  1182. if( aSortbyPadsCount )
  1183. {
  1184. // Build the pad count by net:
  1185. padCountListByNet.clear();
  1186. std::vector<D_PAD*> pads = GetPads();
  1187. padCountListByNet.assign( max_netcode + 1, 0 );
  1188. for( D_PAD* pad : pads )
  1189. padCountListByNet[pad->GetNetCode()]++;
  1190. sort( netBuffer.begin(), netBuffer.end(), sortNetsByNodes );
  1191. }
  1192. else
  1193. {
  1194. sort( netBuffer.begin(), netBuffer.end(), sortNetsByNames );
  1195. }
  1196. for( NETINFO_ITEM* net : netBuffer )
  1197. aNames.Add( UnescapeString( net->GetNetname() ) );
  1198. return netBuffer.size();
  1199. }
  1200. void BOARD::RedrawAreasOutlines( EDA_DRAW_PANEL* panel, wxDC* aDC, GR_DRAWMODE aDrawMode, PCB_LAYER_ID aLayer )
  1201. {
  1202. if( !aDC )
  1203. return;
  1204. for( int ii = 0; ii < GetAreaCount(); ii++ )
  1205. {
  1206. ZONE_CONTAINER* edge_zone = GetArea( ii );
  1207. if( (aLayer < 0) || ( aLayer == edge_zone->GetLayer() ) )
  1208. edge_zone->Draw( panel, aDC, aDrawMode );
  1209. }
  1210. }
  1211. void BOARD::RedrawFilledAreas( EDA_DRAW_PANEL* panel, wxDC* aDC, GR_DRAWMODE aDrawMode, PCB_LAYER_ID aLayer )
  1212. {
  1213. if( !aDC )
  1214. return;
  1215. for( int ii = 0; ii < GetAreaCount(); ii++ )
  1216. {
  1217. ZONE_CONTAINER* edge_zone = GetArea( ii );
  1218. if( (aLayer < 0) || ( aLayer == edge_zone->GetLayer() ) )
  1219. edge_zone->DrawFilledArea( panel, aDC, aDrawMode );
  1220. }
  1221. }
  1222. ZONE_CONTAINER* BOARD::HitTestForAnyFilledArea( const wxPoint& aRefPos,
  1223. PCB_LAYER_ID aStartLayer, PCB_LAYER_ID aEndLayer, int aNetCode )
  1224. {
  1225. if( aEndLayer < 0 )
  1226. aEndLayer = aStartLayer;
  1227. if( aEndLayer < aStartLayer )
  1228. std::swap( aEndLayer, aStartLayer );
  1229. for( ZONE_CONTAINER* area : m_ZoneDescriptorList )
  1230. {
  1231. if( area->GetLayer() < aStartLayer || area->GetLayer() > aEndLayer )
  1232. continue;
  1233. // In locate functions we must skip tagged items with BUSY flag set.
  1234. if( area->GetState( BUSY ) )
  1235. continue;
  1236. if( aNetCode >= 0 && area->GetNetCode() != aNetCode )
  1237. continue;
  1238. if( area->HitTestFilledArea( aRefPos ) )
  1239. return area;
  1240. }
  1241. return NULL;
  1242. }
  1243. int BOARD::SetAreasNetCodesFromNetNames()
  1244. {
  1245. int error_count = 0;
  1246. for( int ii = 0; ii < GetAreaCount(); ii++ )
  1247. {
  1248. ZONE_CONTAINER* it = GetArea( ii );
  1249. if( !it->IsOnCopperLayer() )
  1250. {
  1251. it->SetNetCode( NETINFO_LIST::UNCONNECTED );
  1252. continue;
  1253. }
  1254. if( it->GetNetCode() != 0 ) // i.e. if this zone is connected to a net
  1255. {
  1256. const NETINFO_ITEM* net = it->GetNet();
  1257. if( net )
  1258. {
  1259. it->SetNetCode( net->GetNet() );
  1260. }
  1261. else
  1262. {
  1263. error_count++;
  1264. // keep Net Name and set m_NetCode to -1 : error flag.
  1265. it->SetNetCode( -1 );
  1266. }
  1267. }
  1268. }
  1269. return error_count;
  1270. }
  1271. VIA* BOARD::GetViaByPosition( const wxPoint& aPosition, PCB_LAYER_ID aLayer) const
  1272. {
  1273. for( VIA *via = GetFirstVia( m_Track); via; via = GetFirstVia( via->Next() ) )
  1274. {
  1275. if( (via->GetStart() == aPosition) &&
  1276. (via->GetState( BUSY | IS_DELETED ) == 0) &&
  1277. ((aLayer == UNDEFINED_LAYER) || (via->IsOnLayer( aLayer ))) )
  1278. return via;
  1279. }
  1280. return NULL;
  1281. }
  1282. D_PAD* BOARD::GetPad( const wxPoint& aPosition, LSET aLayerSet )
  1283. {
  1284. if( !aLayerSet.any() )
  1285. aLayerSet = LSET::AllCuMask();
  1286. for( MODULE* module = m_Modules; module; module = module->Next() )
  1287. {
  1288. D_PAD* pad = NULL;
  1289. if( module->HitTest( aPosition ) )
  1290. pad = module->GetPad( aPosition, aLayerSet );
  1291. if( pad )
  1292. return pad;
  1293. }
  1294. return NULL;
  1295. }
  1296. D_PAD* BOARD::GetPad( TRACK* aTrace, ENDPOINT_T aEndPoint )
  1297. {
  1298. const wxPoint& aPosition = aTrace->GetEndPoint( aEndPoint );
  1299. LSET lset( aTrace->GetLayer() );
  1300. return GetPad( aPosition, lset );
  1301. }
  1302. std::list<TRACK*> BOARD::GetTracksByPosition( const wxPoint& aPosition, PCB_LAYER_ID aLayer ) const
  1303. {
  1304. std::list<TRACK*> tracks;
  1305. for( TRACK* track = GetFirstTrack( m_Track ); track; track = GetFirstTrack( track->Next() ) )
  1306. {
  1307. if( ( ( track->GetStart() == aPosition ) || track->GetEnd() == aPosition ) &&
  1308. ( track->GetState( BUSY | IS_DELETED ) == 0 ) &&
  1309. ( ( aLayer == UNDEFINED_LAYER ) || ( track->IsOnLayer( aLayer ) ) ) )
  1310. tracks.push_back( track );
  1311. }
  1312. return tracks;
  1313. }
  1314. D_PAD* BOARD::GetPadFast( const wxPoint& aPosition, LSET aLayerSet )
  1315. {
  1316. for( auto mod : Modules() )
  1317. {
  1318. for ( auto pad : mod->Pads() )
  1319. {
  1320. if( pad->GetPosition() != aPosition )
  1321. continue;
  1322. // Pad found, it must be on the correct layer
  1323. if( ( pad->GetLayerSet() & aLayerSet ).any() )
  1324. return pad;
  1325. }
  1326. }
  1327. return nullptr;
  1328. }
  1329. D_PAD* BOARD::GetPad( std::vector<D_PAD*>& aPadList, const wxPoint& aPosition, LSET aLayerSet )
  1330. {
  1331. // Search aPadList for aPosition
  1332. // aPadList is sorted by X then Y values, and a fast binary search is used
  1333. int idxmax = aPadList.size()-1;
  1334. int delta = aPadList.size();
  1335. int idx = 0; // Starting index is the beginning of list
  1336. while( delta )
  1337. {
  1338. // Calculate half size of remaining interval to test.
  1339. // Ensure the computed value is not truncated (too small)
  1340. if( (delta & 1) && ( delta > 1 ) )
  1341. delta++;
  1342. delta /= 2;
  1343. D_PAD* pad = aPadList[idx];
  1344. if( pad->GetPosition() == aPosition ) // candidate found
  1345. {
  1346. // The pad must match the layer mask:
  1347. if( ( aLayerSet & pad->GetLayerSet() ).any() )
  1348. return pad;
  1349. // More than one pad can be at aPosition
  1350. // search for a pad at aPosition that matched this mask
  1351. // search next
  1352. for( int ii = idx+1; ii <= idxmax; ii++ )
  1353. {
  1354. pad = aPadList[ii];
  1355. if( pad->GetPosition() != aPosition )
  1356. break;
  1357. if( ( aLayerSet & pad->GetLayerSet() ).any() )
  1358. return pad;
  1359. }
  1360. // search previous
  1361. for( int ii = idx-1 ;ii >=0; ii-- )
  1362. {
  1363. pad = aPadList[ii];
  1364. if( pad->GetPosition() != aPosition )
  1365. break;
  1366. if( ( aLayerSet & pad->GetLayerSet() ).any() )
  1367. return pad;
  1368. }
  1369. // Not found:
  1370. return 0;
  1371. }
  1372. if( pad->GetPosition().x == aPosition.x ) // Must search considering Y coordinate
  1373. {
  1374. if( pad->GetPosition().y < aPosition.y ) // Must search after this item
  1375. {
  1376. idx += delta;
  1377. if( idx > idxmax )
  1378. idx = idxmax;
  1379. }
  1380. else // Must search before this item
  1381. {
  1382. idx -= delta;
  1383. if( idx < 0 )
  1384. idx = 0;
  1385. }
  1386. }
  1387. else if( pad->GetPosition().x < aPosition.x ) // Must search after this item
  1388. {
  1389. idx += delta;
  1390. if( idx > idxmax )
  1391. idx = idxmax;
  1392. }
  1393. else // Must search before this item
  1394. {
  1395. idx -= delta;
  1396. if( idx < 0 )
  1397. idx = 0;
  1398. }
  1399. }
  1400. return NULL;
  1401. }
  1402. /**
  1403. * Function SortPadsByXCoord
  1404. * is used by GetSortedPadListByXCoord to Sort a pad list by x coordinate value.
  1405. * This function is used to build ordered pads lists
  1406. */
  1407. bool sortPadsByXthenYCoord( D_PAD* const & ref, D_PAD* const & comp )
  1408. {
  1409. if( ref->GetPosition().x == comp->GetPosition().x )
  1410. return ref->GetPosition().y < comp->GetPosition().y;
  1411. return ref->GetPosition().x < comp->GetPosition().x;
  1412. }
  1413. void BOARD::GetSortedPadListByXthenYCoord( std::vector<D_PAD*>& aVector, int aNetCode )
  1414. {
  1415. for ( auto mod : Modules() )
  1416. {
  1417. for ( auto pad : mod->Pads( ) )
  1418. {
  1419. if( aNetCode < 0 || pad->GetNetCode() == aNetCode )
  1420. {
  1421. aVector.push_back( pad );
  1422. }
  1423. }
  1424. }
  1425. std::sort( aVector.begin(), aVector.end(), sortPadsByXthenYCoord );
  1426. }
  1427. void BOARD::PadDelete( D_PAD* aPad )
  1428. {
  1429. GetConnectivity()->Remove( aPad );
  1430. aPad->DeleteStructure();
  1431. }
  1432. TRACK* BOARD::GetVisibleTrack( TRACK* aStartingTrace, const wxPoint& aPosition,
  1433. LSET aLayerSet ) const
  1434. {
  1435. for( TRACK* track = aStartingTrace; track; track = track->Next() )
  1436. {
  1437. PCB_LAYER_ID layer = track->GetLayer();
  1438. if( track->GetState( BUSY | IS_DELETED ) )
  1439. continue;
  1440. // track's layer is not visible
  1441. if( m_designSettings.IsLayerVisible( layer ) == false )
  1442. continue;
  1443. if( track->Type() == PCB_VIA_T ) // VIA encountered.
  1444. {
  1445. if( track->HitTest( aPosition ) )
  1446. return track;
  1447. }
  1448. else
  1449. {
  1450. if( !aLayerSet[layer] )
  1451. continue; // track's layer is not in aLayerSet
  1452. if( track->HitTest( aPosition ) )
  1453. return track;
  1454. }
  1455. }
  1456. return NULL;
  1457. }
  1458. TRACK* BOARD::MarkTrace( TRACK* aTrackList, TRACK* aTrace, int* aCount,
  1459. double* aTraceLength, double* aPadToDieLength,
  1460. bool aReorder )
  1461. {
  1462. TRACKS trackList;
  1463. if( aCount )
  1464. *aCount = 0;
  1465. if( aTraceLength )
  1466. *aTraceLength = 0;
  1467. if( aTrace == NULL )
  1468. return NULL;
  1469. // Ensure the flag BUSY of all tracks of the board is cleared
  1470. // because we use it to mark segments of the track
  1471. for( TRACK* track = aTrackList; track; track = track->Next() )
  1472. track->SetState( BUSY, false );
  1473. // Set flags of the initial track segment
  1474. aTrace->SetState( BUSY, true );
  1475. LSET layer_set = aTrace->GetLayerSet();
  1476. trackList.push_back( aTrace );
  1477. /* Examine the initial track segment : if it is really a segment, this is
  1478. * easy.
  1479. * If it is a via, one must search for connected segments.
  1480. * If <=2, this via connect 2 segments (or is connected to only one
  1481. * segment) and this via and these 2 segments are a part of a track.
  1482. * If > 2 only this via is flagged (the track has only this via)
  1483. */
  1484. if( aTrace->Type() == PCB_VIA_T )
  1485. {
  1486. TRACK* segm1 = ::GetTrack( aTrackList, NULL, aTrace->GetStart(), layer_set );
  1487. TRACK* segm2 = NULL;
  1488. TRACK* segm3 = NULL;
  1489. if( segm1 )
  1490. {
  1491. segm2 = ::GetTrack( segm1->Next(), NULL, aTrace->GetStart(), layer_set );
  1492. }
  1493. if( segm2 )
  1494. {
  1495. segm3 = ::GetTrack( segm2->Next(), NULL, aTrace->GetStart(), layer_set );
  1496. }
  1497. if( segm3 )
  1498. {
  1499. // More than 2 segments are connected to this via.
  1500. // The "track" is only this via.
  1501. if( aCount )
  1502. *aCount = 1;
  1503. return aTrace;
  1504. }
  1505. if( segm1 ) // search for other segments connected to the initial segment start point
  1506. {
  1507. layer_set = segm1->GetLayerSet();
  1508. chainMarkedSegments( aTrackList, aTrace->GetStart(), layer_set, &trackList );
  1509. }
  1510. if( segm2 ) // search for other segments connected to the initial segment end point
  1511. {
  1512. layer_set = segm2->GetLayerSet();
  1513. chainMarkedSegments( aTrackList, aTrace->GetStart(), layer_set, &trackList );
  1514. }
  1515. }
  1516. else // mark the chain using both ends of the initial segment
  1517. {
  1518. TRACKS from_start;
  1519. TRACKS from_end;
  1520. chainMarkedSegments( aTrackList, aTrace->GetStart(), layer_set, &from_start );
  1521. chainMarkedSegments( aTrackList, aTrace->GetEnd(), layer_set, &from_end );
  1522. // combine into one trackList:
  1523. trackList.insert( trackList.end(), from_start.begin(), from_start.end() );
  1524. trackList.insert( trackList.end(), from_end.begin(), from_end.end() );
  1525. }
  1526. // Now examine selected vias and flag them if they are on the track
  1527. // If a via is connected to only one or 2 segments, it is flagged (is on the track)
  1528. // If a via is connected to more than 2 segments, it is a track end, and it
  1529. // is removed from the list.
  1530. // Go through the list backwards.
  1531. for( int i = trackList.size() - 1; i>=0; --i )
  1532. {
  1533. ::VIA* via = dynamic_cast< ::VIA* >( trackList[i] );
  1534. if( !via )
  1535. continue;
  1536. if( via == aTrace )
  1537. continue;
  1538. via->SetState( BUSY, true ); // Try to flag it. the flag will be cleared later if needed
  1539. layer_set = via->GetLayerSet();
  1540. TRACK* track = ::GetTrack( aTrackList, NULL, via->GetStart(), layer_set );
  1541. // GetTrace does not consider tracks flagged BUSY.
  1542. // So if no connected track found, this via is on the current track
  1543. // only: keep it
  1544. if( track == NULL )
  1545. continue;
  1546. /* If a track is found, this via connects also other segments of
  1547. * the other track. This case happens when a via ends the selected
  1548. * track but must we consider this via is on the selected track, or
  1549. * on a other track.
  1550. * (this is important when selecting a track for deletion: must this
  1551. * via be deleted or not?)
  1552. * We consider this via to be on our track if other segments connected
  1553. * to this via remain connected when removing this via.
  1554. * We search for all other segments connected together:
  1555. * if they are on the same layer, then the via is on the selected track;
  1556. * if they are on different layers, the via is on a other track.
  1557. */
  1558. LAYER_NUM layer = track->GetLayer();
  1559. while( ( track = ::GetTrack( track->Next(), NULL, via->GetStart(), layer_set ) ) != NULL )
  1560. {
  1561. if( layer != track->GetLayer() )
  1562. {
  1563. // The via connects segments of a other track: it is removed
  1564. // from list because it is member of a other track
  1565. via->SetState( BUSY, false );
  1566. break;
  1567. }
  1568. }
  1569. }
  1570. /* Rearrange the track list in order to have flagged segments linked
  1571. * from firstTrack so the NbSegmBusy segments are consecutive segments
  1572. * in list, the first item in the full track list is firstTrack, and
  1573. * the NbSegmBusy-1 next items (NbSegmBusy when including firstTrack)
  1574. * are the flagged segments
  1575. */
  1576. int busy_count = 0;
  1577. TRACK* firstTrack;
  1578. for( firstTrack = aTrackList; firstTrack; firstTrack = firstTrack->Next() )
  1579. {
  1580. // Search for the first flagged BUSY segments
  1581. if( firstTrack->GetState( BUSY ) )
  1582. {
  1583. busy_count = 1;
  1584. break;
  1585. }
  1586. }
  1587. if( firstTrack == NULL )
  1588. return NULL;
  1589. // First step: calculate the track length and find the pads (when exist)
  1590. // at each end of the trace.
  1591. double full_len = 0;
  1592. double lenPadToDie = 0;
  1593. // Because we have a track (a set of track segments between 2 nodes),
  1594. // only 2 pads (maximum) will be taken in account:
  1595. // that are on each end of the track, if any.
  1596. // keep trace of them, to know the die length and the track length ibside each pad.
  1597. D_PAD* s_pad = NULL; // the pad on one end of the trace
  1598. D_PAD* e_pad = NULL; // the pad on the other end of the trace
  1599. int dist_fromstart = INT_MAX;
  1600. int dist_fromend = INT_MAX;
  1601. for( TRACK* track = firstTrack; track; track = track->Next() )
  1602. {
  1603. if( !track->GetState( BUSY ) )
  1604. continue;
  1605. layer_set = track->GetLayerSet();
  1606. D_PAD * pad_on_start = GetPad( track->GetStart(), layer_set );
  1607. D_PAD * pad_on_end = GetPad( track->GetEnd(), layer_set );
  1608. // a segment fully inside a pad does not contribute to the track len
  1609. // (another track end inside this pad will contribute to this lenght)
  1610. if( pad_on_start && ( pad_on_start == pad_on_end ) )
  1611. continue;
  1612. full_len += track->GetLength();
  1613. if( pad_on_start == NULL && pad_on_end == NULL )
  1614. // This most of time the case
  1615. continue;
  1616. // At this point, we can have one track end on a pad, or the 2 track ends on
  1617. // 2 different pads.
  1618. // We don't know what pad (s_pad or e_pad) must be used to store the
  1619. // start point and the end point of the track, so if a pad is already set,
  1620. // use the other
  1621. if( pad_on_start )
  1622. {
  1623. SEG segm( track->GetStart(), pad_on_start->GetPosition() );
  1624. int dist = segm.Length();
  1625. if( s_pad == NULL )
  1626. {
  1627. dist_fromstart = dist;
  1628. s_pad = pad_on_start;
  1629. }
  1630. else if( e_pad == NULL )
  1631. {
  1632. dist_fromend = dist;
  1633. e_pad = pad_on_start;
  1634. }
  1635. else // Should not occur, at least for basic pads
  1636. {
  1637. wxLogWarning( "Unexpected BOARD::MarkTrace: multiple pad_on_start" );
  1638. }
  1639. }
  1640. if( pad_on_end )
  1641. {
  1642. SEG segm( track->GetEnd(), pad_on_end->GetPosition() );
  1643. int dist = segm.Length();
  1644. if( s_pad == NULL )
  1645. {
  1646. dist_fromstart = dist;
  1647. s_pad = pad_on_end;
  1648. }
  1649. else if( e_pad == NULL )
  1650. {
  1651. dist_fromend = dist;
  1652. e_pad = pad_on_end;
  1653. }
  1654. else // Should not occur, at least for basic pads
  1655. {
  1656. wxLogWarning( "Unexpected BOARD::MarkTrace: multiple pad_on_end" );
  1657. }
  1658. }
  1659. }
  1660. if( aReorder )
  1661. {
  1662. DLIST<TRACK>* list = (DLIST<TRACK>*)firstTrack->GetList();
  1663. wxASSERT( list );
  1664. /* Rearrange the chain starting at firstTrack
  1665. * All other BUSY flagged items are moved from their position to the end
  1666. * of the flagged list
  1667. */
  1668. TRACK* next;
  1669. for( TRACK* track = firstTrack->Next(); track; track = next )
  1670. {
  1671. next = track->Next();
  1672. if( track->GetState( BUSY ) ) // move it!
  1673. {
  1674. busy_count++;
  1675. track->UnLink();
  1676. list->Insert( track, firstTrack->Next() );
  1677. }
  1678. }
  1679. }
  1680. else if( aTraceLength )
  1681. {
  1682. busy_count = 0;
  1683. for( TRACK* track = firstTrack; track; track = track->Next() )
  1684. {
  1685. if( track->GetState( BUSY ) )
  1686. {
  1687. busy_count++;
  1688. track->SetState( BUSY, false );
  1689. }
  1690. }
  1691. }
  1692. if( s_pad )
  1693. {
  1694. full_len += dist_fromstart;
  1695. lenPadToDie += (double) s_pad->GetPadToDieLength();
  1696. }
  1697. if( e_pad )
  1698. {
  1699. full_len += dist_fromend;
  1700. lenPadToDie += (double) e_pad->GetPadToDieLength();
  1701. }
  1702. if( aTraceLength )
  1703. *aTraceLength = full_len;
  1704. if( aPadToDieLength )
  1705. *aPadToDieLength = lenPadToDie;
  1706. if( aCount )
  1707. *aCount = busy_count;
  1708. return firstTrack;
  1709. }
  1710. MODULE* BOARD::GetFootprint( const wxPoint& aPosition, PCB_LAYER_ID aActiveLayer,
  1711. bool aVisibleOnly, bool aIgnoreLocked )
  1712. {
  1713. MODULE* pt_module;
  1714. MODULE* module = NULL;
  1715. MODULE* alt_module = NULL;
  1716. int min_dim = 0x7FFFFFFF;
  1717. int alt_min_dim = 0x7FFFFFFF;
  1718. bool current_layer_back = IsBackLayer( aActiveLayer );
  1719. for( pt_module = m_Modules; pt_module; pt_module = pt_module->Next() )
  1720. {
  1721. // is the ref point within the module's bounds?
  1722. if( !pt_module->HitTest( aPosition ) )
  1723. continue;
  1724. // if caller wants to ignore locked modules, and this one is locked, skip it.
  1725. if( aIgnoreLocked && pt_module->IsLocked() )
  1726. continue;
  1727. PCB_LAYER_ID layer = pt_module->GetLayer();
  1728. // Filter non visible modules if requested
  1729. if( !aVisibleOnly || IsModuleLayerVisible( layer ) )
  1730. {
  1731. EDA_RECT bb = pt_module->GetFootprintRect();
  1732. int offx = bb.GetX() + bb.GetWidth() / 2;
  1733. int offy = bb.GetY() + bb.GetHeight() / 2;
  1734. // off x & offy point to the middle of the box.
  1735. int dist = ( aPosition.x - offx ) * ( aPosition.x - offx ) +
  1736. ( aPosition.y - offy ) * ( aPosition.y - offy );
  1737. if( current_layer_back == IsBackLayer( layer ) )
  1738. {
  1739. if( dist <= min_dim )
  1740. {
  1741. // better footprint shown on the active side
  1742. module = pt_module;
  1743. min_dim = dist;
  1744. }
  1745. }
  1746. else if( aVisibleOnly && IsModuleLayerVisible( layer ) )
  1747. {
  1748. if( dist <= alt_min_dim )
  1749. {
  1750. // better footprint shown on the other side
  1751. alt_module = pt_module;
  1752. alt_min_dim = dist;
  1753. }
  1754. }
  1755. }
  1756. }
  1757. if( module )
  1758. {
  1759. return module;
  1760. }
  1761. if( alt_module)
  1762. {
  1763. return alt_module;
  1764. }
  1765. return NULL;
  1766. }
  1767. BOARD_CONNECTED_ITEM* BOARD::GetLockPoint( const wxPoint& aPosition, LSET aLayerSet )
  1768. {
  1769. for( MODULE* module = m_Modules; module; module = module->Next() )
  1770. {
  1771. D_PAD* pad = module->GetPad( aPosition, aLayerSet );
  1772. if( pad )
  1773. return pad;
  1774. }
  1775. // No pad has been located so check for a segment of the trace.
  1776. TRACK* segment = ::GetTrack( m_Track, NULL, aPosition, aLayerSet );
  1777. if( !segment )
  1778. segment = GetVisibleTrack( m_Track, aPosition, aLayerSet );
  1779. return segment;
  1780. }
  1781. TRACK* BOARD::CreateLockPoint( wxPoint& aPosition, TRACK* aSegment, PICKED_ITEMS_LIST* aList )
  1782. {
  1783. /* creates an intermediate point on aSegment and break it into two segments
  1784. * at aPosition.
  1785. * The new segment starts from aPosition and ends at the end point of
  1786. * aSegment. The original segment now ends at aPosition.
  1787. */
  1788. if( aSegment->GetStart() == aPosition || aSegment->GetEnd() == aPosition )
  1789. return NULL;
  1790. // A via is a good lock point
  1791. if( aSegment->Type() == PCB_VIA_T )
  1792. {
  1793. aPosition = aSegment->GetStart();
  1794. return aSegment;
  1795. }
  1796. // Calculation coordinate of intermediate point relative to the start point of aSegment
  1797. wxPoint delta = aSegment->GetEnd() - aSegment->GetStart();
  1798. // calculate coordinates of aPosition relative to aSegment->GetStart()
  1799. wxPoint lockPoint = aPosition - aSegment->GetStart();
  1800. // lockPoint must be on aSegment:
  1801. // Ensure lockPoint.y/lockPoint.y = delta.y/delta.x
  1802. if( delta.x == 0 )
  1803. lockPoint.x = 0; // horizontal segment
  1804. else
  1805. lockPoint.y = KiROUND( ( (double)lockPoint.x * delta.y ) / delta.x );
  1806. /* Create the intermediate point (that is to say creation of a new
  1807. * segment, beginning at the intermediate point.
  1808. */
  1809. lockPoint += aSegment->GetStart();
  1810. TRACK* newTrack = (TRACK*)aSegment->Clone();
  1811. // The new segment begins at the new point,
  1812. newTrack->SetStart(lockPoint);
  1813. newTrack->start = aSegment;
  1814. newTrack->SetState( BEGIN_ONPAD, false );
  1815. DLIST<TRACK>* list = (DLIST<TRACK>*)aSegment->GetList();
  1816. wxASSERT( list );
  1817. list->Insert( newTrack, aSegment->Next() );
  1818. if( aList )
  1819. {
  1820. // Prepare the undo command for the now track segment
  1821. ITEM_PICKER picker( newTrack, UR_NEW );
  1822. aList->PushItem( picker );
  1823. // Prepare the undo command for the old track segment
  1824. // before modifications
  1825. picker.SetItem( aSegment );
  1826. picker.SetStatus( UR_CHANGED );
  1827. picker.SetLink( aSegment->Clone() );
  1828. aList->PushItem( picker );
  1829. }
  1830. // Old track segment now ends at new point.
  1831. aSegment->SetEnd(lockPoint);
  1832. aSegment->end = newTrack;
  1833. aSegment->SetState( END_ONPAD, false );
  1834. D_PAD * pad = GetPad( newTrack, ENDPOINT_START );
  1835. if( pad )
  1836. {
  1837. newTrack->start = pad;
  1838. newTrack->SetState( BEGIN_ONPAD, true );
  1839. aSegment->end = pad;
  1840. aSegment->SetState( END_ONPAD, true );
  1841. }
  1842. aPosition = lockPoint;
  1843. return newTrack;
  1844. }
  1845. ZONE_CONTAINER* BOARD::AddArea( PICKED_ITEMS_LIST* aNewZonesList, int aNetcode,
  1846. PCB_LAYER_ID aLayer, wxPoint aStartPointPosition, int aHatch )
  1847. {
  1848. ZONE_CONTAINER* new_area = InsertArea( aNetcode,
  1849. m_ZoneDescriptorList.size( ) - 1,
  1850. aLayer, aStartPointPosition.x,
  1851. aStartPointPosition.y, aHatch );
  1852. if( aNewZonesList )
  1853. {
  1854. ITEM_PICKER picker( new_area, UR_NEW );
  1855. aNewZonesList->PushItem( picker );
  1856. }
  1857. return new_area;
  1858. }
  1859. void BOARD::RemoveArea( PICKED_ITEMS_LIST* aDeletedList, ZONE_CONTAINER* area_to_remove )
  1860. {
  1861. if( area_to_remove == NULL )
  1862. return;
  1863. if( aDeletedList )
  1864. {
  1865. ITEM_PICKER picker( area_to_remove, UR_DELETED );
  1866. aDeletedList->PushItem( picker );
  1867. Remove( area_to_remove ); // remove from zone list, but does not delete it
  1868. }
  1869. else
  1870. {
  1871. Delete( area_to_remove );
  1872. }
  1873. }
  1874. ZONE_CONTAINER* BOARD::InsertArea( int aNetcode, int aAreaIdx, PCB_LAYER_ID aLayer,
  1875. int aCornerX, int aCornerY, int aHatch )
  1876. {
  1877. ZONE_CONTAINER* new_area = new ZONE_CONTAINER( this );
  1878. new_area->SetNetCode( aNetcode );
  1879. new_area->SetLayer( aLayer );
  1880. new_area->SetTimeStamp( GetNewTimeStamp() );
  1881. if( aAreaIdx < (int) ( m_ZoneDescriptorList.size() - 1 ) )
  1882. m_ZoneDescriptorList.insert( m_ZoneDescriptorList.begin() + aAreaIdx + 1, new_area );
  1883. else
  1884. m_ZoneDescriptorList.push_back( new_area );
  1885. new_area->SetHatchStyle( (ZONE_CONTAINER::HATCH_STYLE) aHatch );
  1886. // Add the first corner to the new zone
  1887. new_area->AppendCorner( wxPoint( aCornerX, aCornerY ), -1 );
  1888. return new_area;
  1889. }
  1890. bool BOARD::NormalizeAreaPolygon( PICKED_ITEMS_LIST * aNewZonesList, ZONE_CONTAINER* aCurrArea )
  1891. {
  1892. // mark all areas as unmodified except this one, if modified
  1893. for( ZONE_CONTAINER* zone : m_ZoneDescriptorList )
  1894. zone->SetLocalFlags( 0 );
  1895. aCurrArea->SetLocalFlags( 1 );
  1896. if( aCurrArea->Outline()->IsSelfIntersecting() )
  1897. {
  1898. aCurrArea->UnHatch();
  1899. // Normalize copied area and store resulting number of polygons
  1900. int n_poly = aCurrArea->Outline()->NormalizeAreaOutlines();
  1901. // If clipping has created some polygons, we must add these new copper areas.
  1902. if( n_poly > 1 )
  1903. {
  1904. ZONE_CONTAINER* NewArea;
  1905. // Move the newly created polygons to new areas, removing them from the current area
  1906. for( int ip = 1; ip < n_poly; ip++ )
  1907. {
  1908. // Create new copper area and copy poly into it
  1909. SHAPE_POLY_SET* new_p = new SHAPE_POLY_SET( aCurrArea->Outline()->UnitSet( ip ) );
  1910. NewArea = AddArea( aNewZonesList, aCurrArea->GetNetCode(), aCurrArea->GetLayer(),
  1911. wxPoint(0, 0), aCurrArea->GetHatchStyle() );
  1912. // remove the poly that was automatically created for the new area
  1913. // and replace it with a poly from NormalizeAreaOutlines
  1914. delete NewArea->Outline();
  1915. NewArea->SetOutline( new_p );
  1916. NewArea->Hatch();
  1917. NewArea->SetLocalFlags( 1 );
  1918. }
  1919. SHAPE_POLY_SET* new_p = new SHAPE_POLY_SET( aCurrArea->Outline()->UnitSet( 0 ) );
  1920. delete aCurrArea->Outline();
  1921. aCurrArea->SetOutline( new_p );
  1922. }
  1923. }
  1924. aCurrArea->Hatch();
  1925. return true;
  1926. }
  1927. BOARD_ITEM* BOARD::Duplicate( const BOARD_ITEM* aItem,
  1928. bool aAddToBoard )
  1929. {
  1930. BOARD_ITEM* new_item = NULL;
  1931. switch( aItem->Type() )
  1932. {
  1933. case PCB_MODULE_T:
  1934. case PCB_TEXT_T:
  1935. case PCB_LINE_T:
  1936. case PCB_TRACE_T:
  1937. case PCB_VIA_T:
  1938. case PCB_ZONE_AREA_T:
  1939. case PCB_TARGET_T:
  1940. case PCB_DIMENSION_T:
  1941. new_item = static_cast<BOARD_ITEM*>( aItem->Clone() );
  1942. break;
  1943. default:
  1944. // Un-handled item for duplication
  1945. new_item = NULL;
  1946. break;
  1947. }
  1948. if( new_item && aAddToBoard )
  1949. Add( new_item );
  1950. return new_item;
  1951. }
  1952. /* Extracts the board outlines and build a closed polygon
  1953. * from lines, arcs and circle items on edge cut layer
  1954. * Any closed outline inside the main outline is a hole
  1955. * All contours should be closed, i.e. are valid vertices for a closed polygon
  1956. * return true if success, false if a contour is not valid
  1957. */
  1958. extern bool BuildBoardPolygonOutlines( BOARD* aBoard, SHAPE_POLY_SET& aOutlines,
  1959. wxString* aErrorText, unsigned int aTolerance,
  1960. wxPoint* aErrorLocation = nullptr );
  1961. bool BOARD::GetBoardPolygonOutlines( SHAPE_POLY_SET& aOutlines, wxString* aErrorText, wxPoint* aErrorLocation )
  1962. {
  1963. bool success = BuildBoardPolygonOutlines( this, aOutlines, aErrorText,
  1964. ARC_HIGH_DEF, aErrorLocation );
  1965. // Make polygon strictly simple to avoid issues (especially in 3D viewer)
  1966. aOutlines.Simplify( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
  1967. return success;
  1968. }
  1969. const std::vector<D_PAD*> BOARD::GetPads()
  1970. {
  1971. std::vector<D_PAD*> allPads;
  1972. for( MODULE* mod : Modules() )
  1973. {
  1974. for( D_PAD* pad : mod->Pads() )
  1975. allPads.push_back( pad );
  1976. }
  1977. return allPads;
  1978. }
  1979. unsigned BOARD::GetPadCount()
  1980. {
  1981. unsigned retval = 0;
  1982. for( auto mod : Modules() )
  1983. retval += mod->Pads().Size();
  1984. return retval;
  1985. }
  1986. /**
  1987. * Function GetPad
  1988. * @return D_PAD* - at the \a aIndex
  1989. */
  1990. D_PAD* BOARD::GetPad( unsigned aIndex ) const
  1991. {
  1992. unsigned count = 0;
  1993. for( MODULE* mod = m_Modules; mod ; mod = mod->Next() ) // FIXME: const DLIST_ITERATOR
  1994. {
  1995. for( D_PAD* pad = mod->PadsList(); pad; pad = pad->Next() )
  1996. {
  1997. if( count == aIndex )
  1998. return pad;
  1999. count++;
  2000. }
  2001. }
  2002. return nullptr;
  2003. }
  2004. void BOARD::ClearAllNetCodes()
  2005. {
  2006. for( auto zone : Zones() )
  2007. zone->SetNetCode( 0 );
  2008. for( auto pad : GetPads() )
  2009. pad->SetNetCode( 0 );
  2010. for( auto track : Tracks() )
  2011. track->SetNetCode( 0 );
  2012. }
  2013. const std::vector<BOARD_CONNECTED_ITEM*> BOARD::AllConnectedItems()
  2014. {
  2015. std::vector<BOARD_CONNECTED_ITEM*> items;
  2016. for( auto track : Tracks() )
  2017. {
  2018. items.push_back( track );
  2019. }
  2020. for( auto mod : Modules() )
  2021. {
  2022. for( auto pad : mod->Pads() )
  2023. {
  2024. items.push_back( pad );
  2025. }
  2026. }
  2027. for( int i = 0; i<GetAreaCount(); i++ )
  2028. {
  2029. auto zone = GetArea( i );
  2030. items.push_back( zone );
  2031. }
  2032. return items;
  2033. }
  2034. void BOARD::SanitizeNetcodes()
  2035. {
  2036. for ( auto item : AllConnectedItems() )
  2037. {
  2038. if( FindNet( item->GetNetCode() ) == nullptr )
  2039. item->SetNetCode( NETINFO_LIST::ORPHANED );
  2040. }
  2041. }