KiCad PCB EDA Suite
pcb_test_frame.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) 2013-2017 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
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 #include <wx/timer.h>
26 #include <wx/math.h>
27 #include <wx/log.h>
28 #include <wx/popupwin.h>
29 #include <wx/cmdline.h>
30 
32 
34 #include <view/view.h>
35 #include <class_draw_panel_gal.h>
36 #include <pcb_draw_panel_gal.h>
37 #include <view/wx_view_controls.h>
38 #include <pcb_painter.h>
39 
40 #include <class_pad.h>
41 #include <class_module.h>
42 #include <class_board.h>
43 #include <class_track.h>
44 #include <class_zone.h>
45 
46 #include <pcb_painter.h>
47 #include <pcb_edit_frame.h>
48 
50 
51 #include <io_mgr.h>
52 #include <memory>
53 #include <set>
54 
55 #include <tool/actions.h>
56 #include <tool/tool_manager.h>
57 #include <tool/tool_dispatcher.h>
58 #include <tools/pcb_tool_base.h>
59 #include <tools/pcb_actions.h>
60 #include <tools/selection_tool.h>
61 #include <kicad_plugin.h>
62 
63 #include "pcb_test_frame.h"
64 
65 IMPLEMENT_APP( GAL_TEST_APP )
66 
67 using namespace KIGFX;
68 
70 {
71  if( !wxApp::OnInit() )
72  return false;
73 
74  // Create the main frame window
75  auto frame = CreateMainFrame( (const char*) m_filename.c_str() );
76 
77  return frame != nullptr;
78 }
79 
80 
81 void GAL_TEST_APP::OnInitCmdLine( wxCmdLineParser& parser )
82 {
83  parser.AddOption( "f", wxEmptyString, "Open board file" );
84  wxApp::OnInitCmdLine( parser );
85 }
86 
87 
88 bool GAL_TEST_APP::OnCmdLineParsed( wxCmdLineParser& parser )
89 {
90  wxString filename;
91 
92  if( parser.Found( "f", &filename ) )
93  {
94  m_filename = filename;
95  }
96 
97  return true;
98 }
99 
100 
101 class TEST_ACTIONS : public ACTIONS
102 {
103 public:
104 
105  virtual ~TEST_ACTIONS() {};
106 
107  virtual OPT<TOOL_EVENT> TranslateLegacyId( int aId ) override
108  {
109  return NULLOPT;
110  }
111 };
112 
113 void PCB_TEST_FRAME::OnMenuFileOpen( wxCommandEvent& WXUNUSED( event ) )
114 {
115 }
116 
117 
118 void PCB_TEST_FRAME::OnMotion( wxMouseEvent& aEvent )
119 {
120  aEvent.Skip();
121 }
122 
123 
125 {
126  m_board.reset( b );
127  m_board->GetConnectivity()->Build( m_board.get() );
128  m_galPanel->DisplayBoard( m_board.get() );
129 #ifdef USE_TOOL_MANAGER
130  m_toolManager->SetEnvironment( m_board.get(), m_galPanel->GetView(),
131  m_galPanel->GetViewControls(), nullptr, nullptr );
132 
133  m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
134 #endif
135 }
136 
137 
138 BOARD* PCB_TEST_FRAME::LoadAndDisplayBoard( const std::string& filename )
139 {
140  PLUGIN::RELEASER pi( new PCB_IO );
141  BOARD* brd = nullptr;
142 
143  try
144  {
145  brd = pi->Load( wxString( filename.c_str() ), NULL, NULL );
146  }
147  catch( const IO_ERROR& ioe )
148  {
149  wxString msg = wxString::Format( _( "Error loading board.\n%s" ),
150  ioe.Problem() );
151 
152  printf( "%s\n", (const char*) msg.mb_str() );
153  return nullptr;
154  }
155 
156  SetBoard( brd );
157 
158  return brd;
159 }
160 
161 
162 PCB_TEST_FRAME::PCB_TEST_FRAME( wxFrame* frame, const wxString& title, const wxPoint& pos,
163  const wxSize& size, long style, PCB_DRAW_PANEL_GAL::GAL_TYPE aGalType ) :
164  wxFrame( frame, wxID_ANY, title, pos, size, style )
165 {
166  // Make a menubar
167  wxMenu* fileMenu = new wxMenu;
168 
169  fileMenu->Append( wxID_OPEN, wxT( "&Open..." ) );
170  fileMenu->AppendSeparator();
171  fileMenu->Append( wxID_EXIT, wxT( "E&xit" ) );
172  wxMenuBar* menuBar = new wxMenuBar;
173  menuBar->Append( fileMenu, wxT( "&File" ) );
174  SetMenuBar( menuBar );
175 
176  Show( true );
177  Maximize();
178  Raise();
179 
181 
182  options.gl_antialiasing_mode = KIGFX::OPENGL_ANTIALIASING_MODE::NONE; //SUPERSAMPLING_X4;
183 
184  m_galPanel = std::make_unique<PCB_DRAW_PANEL_GAL>( this, -1, wxPoint( 0,
185  0 ), wxDefaultSize, options, aGalType );
186 
187  m_galPanel->SetEvtHandlerEnabled( true );
188  m_galPanel->SetFocus();
189  m_galPanel->Show( true );
190  m_galPanel->Raise();
191  m_galPanel->StartDrawing();
192 
193  auto gal = m_galPanel->GetGAL();
194 
195  gal->SetGridVisibility( true );
196  gal->SetGridSize( VECTOR2D( 100000.0, 100000.0 ) );
197  gal->SetGridOrigin( VECTOR2D( 0.0, 0.0 ) );
198 
199  m_galPanel->Connect( wxEVT_MOTION,
200  wxMouseEventHandler( PCB_TEST_FRAME::OnMotion ), NULL, this );
201 
202  m_galPanel->GetViewControls()->ShowCursor( true );
203 
204 #ifdef USE_TOOL_MANAGER
205  m_toolManager = std::make_unique<TOOL_MANAGER>( );
206  m_toolManager->SetEnvironment( m_board.get(), m_galPanel->GetView(),
207  m_galPanel->GetViewControls(), nullptr, nullptr );
208 
209  m_pcbActions = std::make_unique<TEST_ACTIONS>( );
210  m_toolDispatcher = std::make_unique<TOOL_DISPATCHER>( m_toolManager.get(), m_pcbActions.get() );
211 
212  m_toolManager->RegisterTool( new SELECTION_TOOL );
213 
214  m_toolManager->InitTools();
215  m_galPanel->SetEventDispatcher( m_toolDispatcher.get() );
216  m_toolManager->InvokeTool( "common.InteractiveSelection" );
217 #endif
218 
219  SetBoard( new BOARD );
220 }
221 
222 
224 {
225 }
226 
227 
228 // Intercept menu commands
229 void PCB_TEST_FRAME::OnExit( wxCommandEvent& WXUNUSED( event ) )
230 {
231  // true is to force the frame to close
232  Close( true );
233 }
virtual bool OnInit() override
PCB_IO is a PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
virtual OPT< TOOL_EVENT > TranslateLegacyId(int aId) override
Function TranslateLegacyId() Translates legacy tool ids to the corresponding TOOL_ACTION name.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
Model changes (required full reload)
Definition: tool_base.h:82
void SetBoard(BOARD *b)
SELECTION_TOOL.
OPENGL_ANTIALIASING_MODE gl_antialiasing_mode
virtual const wxString Problem() const
what was the problem?
Definition: exceptions.cpp:45
unique_ptr< PCB_DRAW_PANEL_GAL > m_galPanel
WX_VIEW_CONTROLS class definition.
virtual ~TEST_ACTIONS()
A single base class (TRACK) represents both tracks and vias, with subclasses for curved tracks (ARC) ...
unique_ptr< BOARD > m_board
BOARD * LoadAndDisplayBoard(const std::string &filename)
RELEASER releases a PLUGIN in the context of a potential thrown exception, through its destructor.
Definition: io_mgr.h:579
const auto NULLOPT
Definition: optional.h:9
#define NULL
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
virtual void OnMotion(wxMouseEvent &aEvent)
virtual bool OnCmdLineParsed(wxCmdLineParser &parser) override
virtual ~PCB_TEST_FRAME()
Pad object description.
PCB_TEST_FRAME(wxFrame *frame, const wxString &title, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE, PCB_DRAW_PANEL_GAL::GAL_TYPE aGalType=PCB_DRAW_PANEL_GAL::GAL_TYPE_OPENGL)
Board layer functions and definitions.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:180
#define _(s)
Definition: 3d_actions.cpp:33
boost::optional< T > OPT
Definition: optional.h:7
ACTIONS.
Definition: actions.h:43
wxFrame * CreateMainFrame(const std::string &aFileName)
virtual void OnMenuFileOpen(wxCommandEvent &WXUNUSED(event))
virtual void OnExit(wxCommandEvent &event)
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
virtual void OnInitCmdLine(wxCmdLineParser &parser) override