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.

430 lines
12 KiB

2 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020-2023 KiCad Developers.
  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. // WARNING - this Tom's crappy PNS hack tool code. Please don't complain about its quality
  24. // (unless you want to improve it).
  25. #include "pns_log_file.h"
  26. #include <router/pns_segment.h>
  27. #include <board_design_settings.h>
  28. #include <pcbnew/plugins/kicad/pcb_plugin.h>
  29. #include <pcbnew/drc/drc_engine.h>
  30. #include <project.h>
  31. #include <project/project_local_settings.h>
  32. #include <../../tests/common/console_log.h>
  33. BOARD_CONNECTED_ITEM* PNS_LOG_FILE::ItemById( const PNS::LOGGER::EVENT_ENTRY& evt )
  34. {
  35. BOARD_CONNECTED_ITEM* parent = nullptr;
  36. for( BOARD_CONNECTED_ITEM* item : m_board->AllConnectedItems() )
  37. {
  38. if( item->m_Uuid == evt.uuid )
  39. {
  40. parent = item;
  41. break;
  42. };
  43. }
  44. return parent;
  45. }
  46. static const wxString readLine( FILE* f )
  47. {
  48. char str[16384];
  49. fgets( str, sizeof( str ) - 1, f );
  50. return wxString( str );
  51. }
  52. PNS_LOG_FILE::PNS_LOG_FILE() :
  53. m_mode( PNS::ROUTER_MODE::PNS_MODE_ROUTE_SINGLE )
  54. {
  55. m_routerSettings.reset( new PNS::ROUTING_SETTINGS( nullptr, "" ) );
  56. }
  57. std::shared_ptr<SHAPE> parseShape( SHAPE_TYPE expectedType, wxStringTokenizer& aTokens )
  58. {
  59. SHAPE_TYPE type = static_cast<SHAPE_TYPE> ( wxAtoi( aTokens.GetNextToken() ) );
  60. if( type == SHAPE_TYPE::SH_SEGMENT )
  61. {
  62. std::shared_ptr<SHAPE_SEGMENT> sh( new SHAPE_SEGMENT );
  63. VECTOR2I a, b;
  64. a.x = wxAtoi( aTokens.GetNextToken() );
  65. a.y = wxAtoi( aTokens.GetNextToken() );
  66. b.x = wxAtoi( aTokens.GetNextToken() );
  67. b.y = wxAtoi( aTokens.GetNextToken() );
  68. int width = wxAtoi( aTokens.GetNextToken() );
  69. sh->SetSeg( SEG( a, b ));
  70. sh->SetWidth( width );
  71. return sh;
  72. }
  73. else if( type == SHAPE_TYPE::SH_CIRCLE )
  74. {
  75. std::shared_ptr<SHAPE_CIRCLE> sh( new SHAPE_CIRCLE );
  76. VECTOR2I a;
  77. a.x = wxAtoi( aTokens.GetNextToken() );
  78. a.y = wxAtoi( aTokens.GetNextToken() );
  79. int radius = wxAtoi( aTokens.GetNextToken() );
  80. sh->SetCenter( a );
  81. sh->SetRadius( radius );
  82. return sh;
  83. }
  84. return nullptr;
  85. }
  86. bool PNS_LOG_FILE::parseCommonPnsProps( PNS::ITEM* aItem, const wxString& cmd,
  87. wxStringTokenizer& aTokens )
  88. {
  89. if( cmd == wxS( "net" ) )
  90. {
  91. if( aItem->Parent() && aItem->Parent()->GetBoard() )
  92. {
  93. aItem->SetNet( m_board->FindNet( aTokens.GetNextToken() ) );
  94. return true;
  95. }
  96. return false;
  97. }
  98. else if( cmd == wxS( "layers" ) )
  99. {
  100. int start = wxAtoi( aTokens.GetNextToken() );
  101. int end = wxAtoi( aTokens.GetNextToken() );
  102. aItem->SetLayers( LAYER_RANGE( start, end ) );
  103. return true;
  104. }
  105. return false;
  106. }
  107. PNS::SEGMENT* PNS_LOG_FILE::parsePnsSegmentFromString( PNS::SEGMENT* aSeg,
  108. wxStringTokenizer& aTokens )
  109. {
  110. PNS::SEGMENT* seg = new PNS::SEGMENT();
  111. while( aTokens.CountTokens() )
  112. {
  113. wxString cmd = aTokens.GetNextToken();
  114. if( !parseCommonPnsProps( seg, cmd, aTokens ) )
  115. {
  116. if( cmd == wxS( "shape" ) )
  117. {
  118. std::shared_ptr<SHAPE> sh = parseShape( SH_SEGMENT, aTokens );
  119. if( !sh )
  120. return nullptr;
  121. seg->SetShape( *static_cast<SHAPE_SEGMENT*>(sh.get()) );
  122. }
  123. }
  124. }
  125. return seg;
  126. }
  127. PNS::VIA* PNS_LOG_FILE::parsePnsViaFromString( PNS::VIA* aSeg, wxStringTokenizer& aTokens )
  128. {
  129. PNS::VIA* via = new PNS::VIA();
  130. while( aTokens.CountTokens() )
  131. {
  132. wxString cmd = aTokens.GetNextToken();
  133. if( !parseCommonPnsProps( via, cmd, aTokens ) )
  134. {
  135. if( cmd == wxS( "shape" ) )
  136. {
  137. std::shared_ptr<SHAPE> sh = parseShape( SH_CIRCLE, aTokens );
  138. if( !sh )
  139. return nullptr;
  140. SHAPE_CIRCLE* sc = static_cast<SHAPE_CIRCLE*>( sh.get() );
  141. via->SetPos( sc->GetCenter() );
  142. via->SetDiameter( 2 * sc->GetRadius() );
  143. }
  144. else if( cmd == wxS( "drill" ) )
  145. {
  146. via->SetDrill( wxAtoi( aTokens.GetNextToken() ) );
  147. }
  148. }
  149. }
  150. return via;
  151. }
  152. PNS::ITEM* PNS_LOG_FILE::parseItemFromString( wxStringTokenizer& aTokens )
  153. {
  154. wxString type = aTokens.GetNextToken();
  155. if( type == wxS( "segment" ) )
  156. {
  157. PNS::SEGMENT* seg = new PNS::SEGMENT();
  158. return parsePnsSegmentFromString( seg, aTokens );
  159. }
  160. else if( type == wxS( "via" ) )
  161. {
  162. PNS::VIA* seg = new PNS::VIA();
  163. return parsePnsViaFromString( seg, aTokens );
  164. }
  165. return nullptr;
  166. }
  167. bool comparePnsItems( const PNS::ITEM* a , const PNS::ITEM* b )
  168. {
  169. if( a->Kind() != b->Kind() )
  170. return false;
  171. if( a->Net() != b->Net() )
  172. return false;
  173. if( a->Layers() != b->Layers() )
  174. return false;
  175. if( a->Kind() == PNS::ITEM::VIA_T )
  176. {
  177. const PNS::VIA* va = static_cast<const PNS::VIA*>(a);
  178. const PNS::VIA* vb = static_cast<const PNS::VIA*>(b);
  179. if( va->Diameter() != vb->Diameter() )
  180. return false;
  181. if( va->Drill() != vb->Drill() )
  182. return false;
  183. if( va->Pos() != vb->Pos() )
  184. return false;
  185. }
  186. else if ( a->Kind() == PNS::ITEM::SEGMENT_T )
  187. {
  188. const PNS::SEGMENT* sa = static_cast<const PNS::SEGMENT*>(a);
  189. const PNS::SEGMENT* sb = static_cast<const PNS::SEGMENT*>(b);
  190. if( sa->Seg() != sb->Seg() )
  191. return false;
  192. if( sa->Width() != sb->Width() )
  193. return false;
  194. }
  195. return true;
  196. }
  197. const std::set<PNS::ITEM*> deduplicate( const std::vector<PNS::ITEM*>& items )
  198. {
  199. std::set<PNS::ITEM*> rv;
  200. for( PNS::ITEM* item : items )
  201. {
  202. bool isDuplicate = false;
  203. for( PNS::ITEM* ritem : rv )
  204. {
  205. if( comparePnsItems( ritem, item) )
  206. {
  207. isDuplicate = true;
  208. break;
  209. }
  210. if( !isDuplicate )
  211. rv.insert( item );
  212. }
  213. }
  214. return rv;
  215. }
  216. bool PNS_LOG_FILE::COMMIT_STATE::Compare( const PNS_LOG_FILE::COMMIT_STATE& aOther )
  217. {
  218. COMMIT_STATE check( aOther );
  219. //printf("pre-compare: %d/%d\n", check.m_addedItems.size(), check.m_removedIds.size() );
  220. //printf("pre-compare (log): %d/%d\n", m_addedItems.size(), m_removedIds.size() );
  221. for( const KIID& uuid : m_removedIds )
  222. {
  223. if( check.m_removedIds.find( uuid ) != check.m_removedIds.end() )
  224. check.m_removedIds.erase( uuid );
  225. else
  226. return false; // removed twice? wtf
  227. }
  228. std::set<PNS::ITEM*> addedItems = deduplicate( m_addedItems );
  229. std::set<PNS::ITEM*> chkAddedItems = deduplicate( check.m_addedItems );
  230. for( PNS::ITEM* item : addedItems )
  231. {
  232. for( PNS::ITEM* chk : chkAddedItems )
  233. {
  234. if( comparePnsItems( item, chk ) )
  235. {
  236. chkAddedItems.erase( chk );
  237. break;
  238. }
  239. }
  240. }
  241. //printf("post-compare: %d/%d\n", chkAddedItems.size(), check.m_removedIds.size() );
  242. return chkAddedItems.empty() && check.m_removedIds.empty();
  243. }
  244. bool PNS_LOG_FILE::SaveLog( const wxFileName& logFileName, REPORTER* aRpt )
  245. {
  246. std::vector<PNS::ITEM*> dummyHeads; // todo - save heads when we support it in QA
  247. FILE* log_f = wxFopen( logFileName.GetFullPath(), "wb" );
  248. wxString logString = PNS::LOGGER::FormatLogFileAsString( m_mode, m_commitState.m_addedItems,
  249. m_commitState.m_removedIds, dummyHeads,
  250. m_events );
  251. fprintf( log_f, "%s\n", logString.c_str().AsChar() );
  252. fclose( log_f );
  253. return true;
  254. }
  255. bool PNS_LOG_FILE::Load( const wxFileName& logFileName, REPORTER* aRpt )
  256. {
  257. wxFileName fname_log( logFileName );
  258. fname_log.SetExt( wxT( "log" ) );
  259. wxFileName fname_dump( logFileName );
  260. fname_dump.SetExt( wxT( "dump" ) );
  261. wxFileName fname_project( logFileName );
  262. fname_project.SetExt( wxT( "kicad_pro" ) );
  263. fname_project.MakeAbsolute();
  264. wxFileName fname_settings( logFileName );
  265. fname_settings.SetExt( wxT( "settings" ) );
  266. aRpt->Report( wxString::Format( wxT( "Loading router settings from '%s'" ),
  267. fname_settings.GetFullPath() ) );
  268. bool ok = m_routerSettings->LoadFromRawFile( fname_settings.GetFullPath() );
  269. if( !ok )
  270. {
  271. aRpt->Report( wxString::Format( wxT( "Failed to load routing settings. Usign defaults." ) ) ,
  272. RPT_SEVERITY_WARNING );
  273. }
  274. aRpt->Report( wxString::Format( wxT( "Loading project settings from '%s'" ),
  275. fname_settings.GetFullPath() ) );
  276. m_settingsMgr.reset( new SETTINGS_MANAGER ( true ) );
  277. m_settingsMgr->LoadProject( fname_project.GetFullPath() );
  278. PROJECT* project = m_settingsMgr->GetProject( fname_project.GetFullPath() );
  279. project->SetReadOnly();
  280. try
  281. {
  282. PCB_PLUGIN io;
  283. aRpt->Report( wxString::Format( wxT("Loading board snapshot from '%s'"), fname_dump.GetFullPath() ) );
  284. m_board.reset( io.LoadBoard( fname_dump.GetFullPath(), nullptr, nullptr ) );
  285. m_board->SetProject( project );
  286. std::shared_ptr<DRC_ENGINE> drcEngine( new DRC_ENGINE );
  287. CONSOLE_LOG consoleLog;
  288. BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
  289. bds.m_DRCEngine = drcEngine;
  290. bds.m_UseConnectedTrackWidth = project->GetLocalSettings().m_AutoTrackWidth;
  291. m_board->SynchronizeNetsAndNetClasses( true );
  292. drcEngine->SetBoard( m_board.get() );
  293. drcEngine->SetDesignSettings( &bds );
  294. drcEngine->SetLogReporter( new CONSOLE_MSG_REPORTER( &consoleLog ) );
  295. drcEngine->InitEngine( wxFileName() );
  296. }
  297. catch( const PARSE_ERROR& parse_error )
  298. {
  299. aRpt->Report( wxString::Format( "parse error : %s (%s)\n", parse_error.Problem(),
  300. parse_error.What() ), RPT_SEVERITY_ERROR );
  301. return false;
  302. }
  303. FILE* f = fopen( fname_log.GetFullPath().c_str(), "rb" );
  304. aRpt->Report( wxString::Format( "Loading log from '%s'", fname_log.GetFullPath() ) );
  305. if( !f )
  306. {
  307. aRpt->Report( wxT( "Failed to load log" ), RPT_SEVERITY_ERROR );
  308. return false;
  309. }
  310. while( !feof( f ) )
  311. {
  312. wxString line = readLine( f );
  313. wxStringTokenizer tokens( line );
  314. if( !tokens.CountTokens() )
  315. continue;
  316. wxString cmd = tokens.GetNextToken();
  317. if( cmd == wxT( "mode" ) )
  318. {
  319. m_mode = static_cast<PNS::ROUTER_MODE>( wxAtoi( tokens.GetNextToken() ) );
  320. }
  321. else if( cmd == wxT( "event" ) )
  322. {
  323. m_events.push_back( PNS::LOGGER::ParseEvent( line ) );
  324. }
  325. else if ( cmd == wxT( "added" ) )
  326. {
  327. PNS::ITEM* item = parseItemFromString( tokens );
  328. m_commitState.m_addedItems.push_back( item );
  329. }
  330. else if ( cmd == wxT( "removed" ) )
  331. {
  332. m_commitState.m_removedIds.insert( KIID( tokens.GetNextToken() ) );
  333. }
  334. }
  335. fclose( f );
  336. return true;
  337. }