2020-07-03 16:37:08 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2020-06-13 23:28:08 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <profile.h>
|
|
|
|
|
|
|
|
#include <wx/cmdline.h>
|
|
|
|
|
|
|
|
#include <pcbnew_utils/board_file_utils.h>
|
2020-09-11 15:04:11 +00:00
|
|
|
#include <pcbnew/drc/drc_engine.h>
|
2020-09-13 10:37:20 +00:00
|
|
|
#include <pcbnew/class_board.h>
|
2020-09-11 15:04:11 +00:00
|
|
|
#include <pcbnew/drc/drc_rule_parser.h>
|
2020-06-18 16:55:22 +00:00
|
|
|
#include <reporter.h>
|
|
|
|
#include <widgets/progress_reporter.h>
|
2020-06-13 23:28:08 +00:00
|
|
|
|
2020-09-04 23:19:01 +00:00
|
|
|
#include <project.h>
|
|
|
|
#include <settings/settings_manager.h>
|
|
|
|
#include <wildcards_and_files_ext.h>
|
|
|
|
|
2020-08-26 22:06:39 +00:00
|
|
|
class CONSOLE_LOG
|
2020-06-13 23:28:08 +00:00
|
|
|
{
|
2020-08-26 22:06:39 +00:00
|
|
|
public:
|
|
|
|
enum COLOR {
|
|
|
|
RED = 0,
|
|
|
|
GREEN,
|
|
|
|
DEFAULT
|
|
|
|
};
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2020-08-26 22:06:39 +00:00
|
|
|
CONSOLE_LOG() {};
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2020-08-26 22:06:39 +00:00
|
|
|
void PrintProgress( const wxString& aMessage )
|
|
|
|
{
|
|
|
|
if( m_lastLineIsProgressBar )
|
|
|
|
eraseLastLine();
|
2020-06-17 22:36:54 +00:00
|
|
|
|
2020-08-26 22:06:39 +00:00
|
|
|
printf("%s", (const char *) aMessage.c_str() );
|
|
|
|
fflush(stdout);
|
2020-06-18 16:55:22 +00:00
|
|
|
|
2020-08-26 22:06:39 +00:00
|
|
|
m_lastLineIsProgressBar = true;
|
|
|
|
}
|
2020-06-13 23:28:08 +00:00
|
|
|
|
|
|
|
|
2020-08-26 22:06:39 +00:00
|
|
|
void Print( const wxString& aMessage )
|
|
|
|
{
|
|
|
|
if( m_lastLineIsProgressBar )
|
|
|
|
eraseLastLine();
|
|
|
|
|
|
|
|
printf("%s", (const char *) aMessage.c_str() );
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
m_lastLineIsProgressBar = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SetColor( COLOR color )
|
|
|
|
{
|
|
|
|
std::map<COLOR, wxString> colorMap =
|
|
|
|
{
|
|
|
|
{ RED, "\033[0;31m" },
|
|
|
|
{ GREEN, "\033[0;32m" },
|
|
|
|
{ DEFAULT, "\033[0;37m" }
|
|
|
|
};
|
|
|
|
|
|
|
|
printf( "%s", (const char*) colorMap[ color ].c_str() );
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
void eraseLastLine()
|
|
|
|
{
|
|
|
|
printf("\r\033[K");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool m_lastLineIsProgressBar = false;
|
|
|
|
std::mutex m_lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CONSOLE_PROGRESS_REPORTER : public PROGRESS_REPORTER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CONSOLE_PROGRESS_REPORTER( CONSOLE_LOG* log ) :
|
|
|
|
PROGRESS_REPORTER( 0 ),
|
|
|
|
m_log( log ) {};
|
|
|
|
~CONSOLE_PROGRESS_REPORTER() {};
|
|
|
|
|
|
|
|
virtual void SetCurrentProgress( double aProgress ) override
|
|
|
|
{
|
|
|
|
PROGRESS_REPORTER::SetCurrentProgress( aProgress );
|
|
|
|
updateUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool updateUI() override
|
|
|
|
{
|
|
|
|
m_log->SetColor( CONSOLE_LOG::GREEN );
|
|
|
|
m_log->PrintProgress( wxString::Format( " | %s : %.02f%%", m_rptMessage, (double) m_progress / (double) m_maxProgress * 100.0 ) );
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-18 16:55:22 +00:00
|
|
|
|
2020-08-26 22:06:39 +00:00
|
|
|
CONSOLE_LOG* m_log;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CONSOLE_MSG_REPORTER : public REPORTER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CONSOLE_MSG_REPORTER( CONSOLE_LOG *log ) :
|
|
|
|
m_log(log)
|
|
|
|
{};
|
|
|
|
~CONSOLE_MSG_REPORTER() {};
|
|
|
|
|
|
|
|
|
|
|
|
virtual REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override
|
2020-06-18 16:55:22 +00:00
|
|
|
{
|
2020-08-26 22:06:39 +00:00
|
|
|
switch( aSeverity )
|
|
|
|
{
|
2020-09-20 09:32:47 +00:00
|
|
|
case RPT_SEVERITY_ERROR:
|
2020-08-26 22:06:39 +00:00
|
|
|
m_log->SetColor( CONSOLE_LOG::RED );
|
|
|
|
m_log->Print("ERROR | ");
|
2020-09-20 09:32:47 +00:00
|
|
|
break;
|
|
|
|
|
2020-08-26 22:06:39 +00:00
|
|
|
default:
|
|
|
|
m_log->SetColor( CONSOLE_LOG::DEFAULT );
|
|
|
|
m_log->Print(" | ");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_log->SetColor( CONSOLE_LOG::DEFAULT );
|
|
|
|
m_log->Print( aText + "\n" );
|
|
|
|
return *this;
|
2020-06-18 16:55:22 +00:00
|
|
|
}
|
2020-08-26 22:06:39 +00:00
|
|
|
|
|
|
|
virtual bool HasMessage() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CONSOLE_LOG* m_log;
|
|
|
|
};
|
|
|
|
|
2020-09-04 23:19:01 +00:00
|
|
|
|
2020-09-13 10:37:20 +00:00
|
|
|
class DRC_REPORT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct ENTRY
|
|
|
|
{
|
|
|
|
std::shared_ptr<DRC_ITEM> m_item;
|
|
|
|
MARKER_PCB* m_marker;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<ENTRY> ENTRIES;
|
|
|
|
|
|
|
|
DRC_REPORT() {};
|
|
|
|
~DRC_REPORT();
|
|
|
|
|
|
|
|
void AddItem( std::shared_ptr<DRC_ITEM> aItem, MARKER_PCB *aMarker = nullptr )
|
|
|
|
{
|
|
|
|
ENTRY ent;
|
|
|
|
ent.m_item = aItem;
|
|
|
|
ent.m_marker = aMarker;
|
|
|
|
m_entries.push_back(ent);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ENTRIES& GetReportEntries() const { return m_entries; };
|
|
|
|
|
|
|
|
private:
|
|
|
|
ENTRIES m_entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-09-04 23:19:01 +00:00
|
|
|
struct PROJECT_CONTEXT {
|
|
|
|
PROJECT* project;
|
|
|
|
std::shared_ptr<BOARD> board;
|
|
|
|
};
|
|
|
|
|
|
|
|
SETTINGS_MANAGER g_settingsManager( true );
|
|
|
|
|
|
|
|
PROJECT_CONTEXT loadKicadProject( wxString filename )
|
|
|
|
{
|
|
|
|
PROJECT_CONTEXT rv;
|
|
|
|
wxFileName pro( filename );
|
|
|
|
wxFileName brdName ( filename );
|
|
|
|
pro.SetExt( ProjectFileExtension );
|
|
|
|
brdName.SetExt( KiCadPcbFileExtension );
|
|
|
|
|
|
|
|
g_settingsManager.LoadProject( pro.GetFullPath() );
|
|
|
|
|
|
|
|
rv.project = &g_settingsManager.Prj();
|
|
|
|
rv.board.reset( KI_TEST::ReadBoardFromFileOrStream( (const char *) brdName.GetFullPath().c_str() ).release() );
|
|
|
|
rv.board->SetProject( rv.project );
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-26 22:06:39 +00:00
|
|
|
int main( int argc, char *argv[] )
|
|
|
|
{
|
|
|
|
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
|
|
|
|
propMgr.Rebuild();
|
|
|
|
|
|
|
|
if( argc < 2 )
|
2020-06-13 23:28:08 +00:00
|
|
|
{
|
2020-08-26 22:06:39 +00:00
|
|
|
printf("usage: %s board_file.kicad_pcb [drc-rules-file]\n", argv[0] );
|
2020-08-03 22:12:11 +00:00
|
|
|
return -1;
|
2020-06-13 23:28:08 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 23:19:01 +00:00
|
|
|
PROJECT_CONTEXT project = loadKicadProject( argv[1] );
|
|
|
|
|
|
|
|
|
2020-08-26 22:06:39 +00:00
|
|
|
|
2020-09-12 00:22:46 +00:00
|
|
|
DRC_ENGINE drcEngine( project.board.get(), &project.board->GetDesignSettings() );
|
2020-08-26 22:06:39 +00:00
|
|
|
|
|
|
|
CONSOLE_LOG consoleLog;
|
|
|
|
|
|
|
|
drcEngine.SetLogReporter( new CONSOLE_MSG_REPORTER ( &consoleLog ) );
|
|
|
|
drcEngine.SetProgressReporter( new CONSOLE_PROGRESS_REPORTER ( &consoleLog ) );
|
|
|
|
|
2020-09-14 17:54:14 +00:00
|
|
|
drcEngine.SetViolationHandler(
|
|
|
|
[&]( const std::shared_ptr<DRC_ITEM>& aItem, wxPoint aPos )
|
|
|
|
{
|
|
|
|
// fixme
|
|
|
|
} );
|
|
|
|
|
2020-09-12 00:22:46 +00:00
|
|
|
wxString rulesFilepath;
|
|
|
|
|
2020-08-26 22:06:39 +00:00
|
|
|
if( argc > 2 )
|
2020-09-12 00:22:46 +00:00
|
|
|
rulesFilepath = wxString( argv[2] );
|
|
|
|
else
|
|
|
|
rulesFilepath = project.project->AbsolutePath( "drc-rules" );
|
|
|
|
|
|
|
|
drcEngine.InitEngine( rulesFilepath );
|
2020-08-26 22:06:39 +00:00
|
|
|
|
2020-09-14 17:54:14 +00:00
|
|
|
drcEngine.RunTests();
|
2020-06-13 23:28:08 +00:00
|
|
|
|
|
|
|
return 0;
|
2020-08-18 14:17:16 +00:00
|
|
|
}
|