KiCad PCB EDA Suite
board_test_utils.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2019-2021 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include "board_test_utils.h"
25 
26 #include <wx/filename.h>
27 #include <board.h>
28 #include <board_design_settings.h>
31 #include <tool/tool_manager.h>
32 #include <zone_filler.h>
33 
34 // For the temp directory logic: can be std::filesystem in C++17
35 #include <boost/filesystem.hpp>
36 #include <boost/test/unit_test.hpp>
37 #include <board_commit.h>
38 
39 namespace KI_TEST
40 {
41 
43  m_dump_boards( std::getenv( "KICAD_TEST_DUMP_BOARD_FILES" ) )
44 {
45 }
46 
47 
48 void BOARD_DUMPER::DumpBoardToFile( BOARD& aBoard, const std::string& aName ) const
49 {
50  if( !m_dump_boards )
51  return;
52 
53  auto path = boost::filesystem::temp_directory_path() / aName;
54  path += ".kicad_pcb";
55 
56  BOOST_TEST_MESSAGE( "Dumping board file: " << path.string() );
57  ::KI_TEST::DumpBoardToFile( aBoard, path.string() );
58 }
59 
60 
61 void LoadBoard( SETTINGS_MANAGER& aSettingsManager, const wxString& aRelPath,
62  std::unique_ptr<BOARD>& aBoard )
63 {
64  if( aBoard )
65  {
66  aBoard->SetProject( nullptr );
67  aBoard = nullptr;
68  }
69 
70  std::string absPath = GetPcbnewTestDataDir() + aRelPath.ToStdString();
71  wxFileName projectFile( absPath + ".kicad_pro" );
72  wxFileName legacyProject( absPath + ".pro" );
73  std::string boardPath = absPath + ".kicad_pcb";
74  wxFileName rulesFile( absPath + ".kicad_dru" );
75 
76  if( projectFile.Exists() )
77  aSettingsManager.LoadProject( projectFile.GetFullPath() );
78  else if( legacyProject.Exists() )
79  aSettingsManager.LoadProject( legacyProject.GetFullPath() );
80 
81  aBoard = ReadBoardFromFileOrStream( boardPath );
82 
83  if( projectFile.Exists() || legacyProject.Exists() )
84  aBoard->SetProject( &aSettingsManager.Prj() );
85 
86  auto m_DRCEngine = std::make_shared<DRC_ENGINE>( aBoard.get(), &aBoard->GetDesignSettings() );
87 
88  if( rulesFile.Exists() )
89  m_DRCEngine->InitEngine( rulesFile );
90  else
91  m_DRCEngine->InitEngine( wxFileName() );
92 
93  aBoard->GetDesignSettings().m_DRCEngine = m_DRCEngine;
94 }
95 
96 
97 void FillZones( BOARD* m_board, int aFillVersion )
98 {
99  TOOL_MANAGER toolMgr;
100  toolMgr.SetEnvironment( m_board, nullptr, nullptr, nullptr, nullptr );
101 
102  BOARD_COMMIT commit( &toolMgr );
103  ZONE_FILLER filler( m_board, &commit );
104  std::vector<ZONE*> toFill;
105 
106  m_board->GetDesignSettings().m_ZoneFillVersion = aFillVersion;
107 
108  for( ZONE* zone : m_board->Zones() )
109  toFill.push_back( zone );
110 
111  if( filler.Fill( toFill, false, nullptr ) )
112  commit.Push( _( "Fill Zone(s)" ), false, false );
113 }
114 
115 
116 } // namespace KI_TEST
int m_ZoneFillVersion
Option to select different fill algorithms.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
ZONES & Zones()
Definition: board.h:239
Definition: bitmap.cpp:64
void DumpBoardToFile(BOARD &aBoard, const std::string &aName) const
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:590
Master controller class:
Definition: tool_manager.h:54
bool Fill(std::vector< ZONE * > &aZones, bool aCheck=false, wxWindow *aParent=nullptr)
Fills the given list of zones.
Definition: zone_filler.cpp:78
void DumpBoardToFile(BOARD &board, const std::string &aFilename)
Utility function to simply write a Board out to a file.
#define _(s)
Handle a list of polygons defining a copper zone.
Definition: zone.h:56
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Set the work environment (model, view, view controls and the parent window).
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Revert the commit by restoring the modified items state.
General utilities for PCB file IO for QA programs.
std::unique_ptr< BOARD > ReadBoardFromFileOrStream(const std::string &aFilename, std::istream &aFallback)
Read a board from a file, or another stream, as appropriate.
void FillZones(BOARD *m_board, int aFillVersion)
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)