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.

161 lines
4.1 KiB

12 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2012 NBEE Embedded Systems, Miguel Angel Ajo <miguelangel@nbee.es>
  5. * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file pcbnew_scripting_helpers.cpp
  26. * @brief Scripting helper functions for pcbnew functionality
  27. */
  28. #include <Python.h>
  29. #undef HAVE_CLOCK_GETTIME // macro is defined in Python.h and causes redefine warning
  30. #include <pcbnew_scripting_helpers.h>
  31. #include <pcbnew.h>
  32. #include <pcbnew_id.h>
  33. #include <build_version.h>
  34. #include <class_board.h>
  35. #include <class_drawpanel.h>
  36. #include <kicad_string.h>
  37. #include <io_mgr.h>
  38. #include <macros.h>
  39. #include <stdlib.h>
  40. #include <pcb_draw_panel_gal.h>
  41. #include <action_plugin.h>
  42. static PCB_EDIT_FRAME* s_PcbEditFrame = NULL;
  43. BOARD* GetBoard()
  44. {
  45. if( s_PcbEditFrame )
  46. return s_PcbEditFrame->GetBoard();
  47. else
  48. return NULL;
  49. }
  50. void ScriptingSetPcbEditFrame( PCB_EDIT_FRAME* aPcbEditFrame )
  51. {
  52. s_PcbEditFrame = aPcbEditFrame;
  53. }
  54. BOARD* LoadBoard( wxString& aFileName )
  55. {
  56. if( aFileName.EndsWith( wxT( ".kicad_pcb" ) ) )
  57. return LoadBoard( aFileName, IO_MGR::KICAD_SEXP );
  58. else if( aFileName.EndsWith( wxT( ".brd" ) ) )
  59. return LoadBoard( aFileName, IO_MGR::LEGACY );
  60. // as fall back for any other kind use the legacy format
  61. return LoadBoard( aFileName, IO_MGR::LEGACY );
  62. }
  63. BOARD* LoadBoard( wxString& aFileName, IO_MGR::PCB_FILE_T aFormat )
  64. {
  65. BOARD* brd = IO_MGR::Load( aFormat, aFileName );
  66. if( brd )
  67. {
  68. brd->BuildConnectivity();
  69. brd->BuildListOfNets();
  70. brd->SynchronizeNetsAndNetClasses();
  71. }
  72. return brd;
  73. }
  74. bool SaveBoard( wxString& aFileName, BOARD* aBoard, IO_MGR::PCB_FILE_T aFormat )
  75. {
  76. aBoard->BuildConnectivity();
  77. aBoard->SynchronizeNetsAndNetClasses();
  78. aBoard->GetDesignSettings().SetCurrentNetClass( NETCLASS::Default );
  79. IO_MGR::Save( aFormat, aFileName, aBoard, NULL );
  80. return true;
  81. }
  82. bool SaveBoard( wxString& aFileName, BOARD* aBoard )
  83. {
  84. return SaveBoard( aFileName, aBoard, IO_MGR::KICAD_SEXP );
  85. }
  86. void Refresh()
  87. {
  88. if( s_PcbEditFrame )
  89. {
  90. auto board = s_PcbEditFrame->GetBoard();
  91. board->BuildConnectivity();
  92. if( s_PcbEditFrame->IsGalCanvasActive() )
  93. {
  94. auto gal_canvas = static_cast<PCB_DRAW_PANEL_GAL*>( s_PcbEditFrame->GetGalCanvas() );
  95. // Reinit everything: this is the easy way to do that
  96. s_PcbEditFrame->UseGalCanvas( true );
  97. gal_canvas->Refresh();
  98. }
  99. else
  100. // first argument is erase background, second is a wxRect that
  101. // defines a reftresh area (all canvas if null)
  102. s_PcbEditFrame->GetCanvas()->Refresh( true, NULL );
  103. }
  104. }
  105. void WindowZoom( int xl, int yl, int width, int height )
  106. {
  107. EDA_RECT Rect( wxPoint( xl, yl ), wxSize( width, height ) );
  108. if( s_PcbEditFrame )
  109. s_PcbEditFrame->Window_Zoom( Rect );
  110. }
  111. void UpdateUserInterface()
  112. {
  113. if( s_PcbEditFrame )
  114. s_PcbEditFrame->UpdateUserInterface();
  115. }
  116. int GetUserUnits()
  117. {
  118. if( s_PcbEditFrame )
  119. return s_PcbEditFrame->GetUserUnits();
  120. return -1;
  121. }
  122. bool IsActionRunning()
  123. {
  124. return ACTION_PLUGINS::IsActionRunning();
  125. }