KiCad PCB EDA Suite
append_board_to_current.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) 2004-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2014 KiCad Developers, see change_log.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 
30 #include <fctsys.h>
31 #include <confirm.h>
32 #include <properties.h>
33 #include <wxPcbStruct.h>
34 #include <pcbnew.h>
35 #include <io_mgr.h>
36 #include <class_module.h>
37 #include <class_zone.h>
38 #include <class_board.h>
39 #include <pcb_draw_panel_gal.h>
40 
41 // Defined in files.cpp:
42 extern IO_MGR::PCB_FILE_T plugin_type( const wxString& aFileName, int aCtl );
43 
44 
45 bool PCB_EDIT_FRAME::AppendBoardFile( const wxString& aFullFileName, int aCtl )
46 {
47  IO_MGR::PCB_FILE_T pluginType = plugin_type( aFullFileName, aCtl );
48  PLUGIN::RELEASER pi( IO_MGR::PluginFind( pluginType ) );
49 
50  // keep trace of existing items, in order to know what are the new items
51  // (for undo command for instance)
52 
53  // Tracks are inserted, not append, so mark existing tracks to know what are
54  // the new tracks
55  for( TRACK* track = GetBoard()->m_Track; track; track = track->Next() )
56  track->SetFlags( FLAG0 );
57 
58  // Other items are append to the item list, so keep trace to the
59  // last existing item is enough
60  MODULE* module = GetBoard()->m_Modules.GetLast();
61  BOARD_ITEM* drawing = GetBoard()->m_Drawings.GetLast();
62  int zonescount = GetBoard()->GetAreaCount();
63 
64  // Keep also the count of copper layers, because we can happen boards
65  // with different copper layers counts,
66  // and the enabled layers
67  int initialCopperLayerCount = GetBoard()->GetCopperLayerCount();
68  LSET initialEnabledLayers = GetBoard()->GetEnabledLayers();
69 
70  try
71  {
72  PROPERTIES props;
73  char xbuf[30];
74  char ybuf[30];
75 
76  // EAGLE_PLUGIN can use this info to center the BOARD, but it does not yet.
77  sprintf( xbuf, "%d", GetPageSizeIU().x );
78  sprintf( ybuf, "%d", GetPageSizeIU().y );
79 
80  props["page_width"] = xbuf;
81  props["page_height"] = ybuf;
82 
84  pi->Load( aFullFileName, GetBoard(), &props );
85  }
86  catch( const IO_ERROR& ioe )
87  {
88  for( TRACK* track = GetBoard()->m_Track; track; track = track->Next() )
89  track->ClearFlags( FLAG0 );
90 
91  wxString msg = wxString::Format( _(
92  "Error loading board.\n%s" ),
93  GetChars( ioe.What() )
94  );
95  DisplayError( this, msg );
96 
97  return false;
98  }
99 
100  // Now prepare a block move command to place the new items, and
101  // prepare the undo command.
102  BLOCK_SELECTOR& blockmove = GetScreen()->m_BlockLocate;
104  PICKED_ITEMS_LIST& blockitemsList = blockmove.GetItems();
105  PICKED_ITEMS_LIST undoListPicker;
106  ITEM_PICKER picker( NULL, UR_NEW );
107 
108  EDA_RECT bbox; // the new items bounding box, for block move
109  bool bboxInit = true; // true until the bounding box is initialized
110 
111  for( TRACK* track = GetBoard()->m_Track; track; track = track->Next() )
112  {
113  if( track->GetFlags() & FLAG0 )
114  {
115  track->ClearFlags( FLAG0 );
116  continue;
117  }
118 
119  track->SetFlags( IS_MOVED );
120  picker.SetItem( track );
121  undoListPicker.PushItem( picker );
122  blockitemsList.PushItem( picker );
123 
124  if( bboxInit )
125  bbox = track->GetBoundingBox();
126  else
127  bbox.Merge( track->GetBoundingBox() );
128 
129  bboxInit = false;
130  }
131 
132  if( module )
133  module = module->Next();
134  else
135  module = GetBoard()->m_Modules;
136 
137  for( ; module; module = module->Next() )
138  {
139  module->SetFlags( IS_MOVED );
140  picker.SetItem( module );
141  undoListPicker.PushItem( picker );
142  blockitemsList.PushItem( picker );
143 
144  if( bboxInit )
145  bbox = module->GetBoundingBox();
146  else
147  bbox.Merge( module->GetBoundingBox() );
148 
149  bboxInit = false;
150  }
151 
152  if( drawing )
153  drawing = drawing->Next();
154  else
155  drawing = GetBoard()->m_Drawings;
156 
157  for( ; drawing; drawing = drawing->Next() )
158  {
159  drawing->SetFlags( IS_MOVED );
160  picker.SetItem( drawing );
161  undoListPicker.PushItem( picker );
162  blockitemsList.PushItem( picker );
163 
164  if( bboxInit )
165  bbox = drawing->GetBoundingBox();
166  else
167  bbox.Merge( drawing->GetBoundingBox() );
168 
169  bboxInit = false;
170  }
171 
172  for( ZONE_CONTAINER* zone = GetBoard()->GetArea( zonescount ); zone;
173  zone = GetBoard()->GetArea( zonescount ) )
174  {
175  zone->SetFlags( IS_MOVED );
176  picker.SetItem( zone );
177  undoListPicker.PushItem( picker );
178  blockitemsList.PushItem( picker );
179  zonescount++;
180 
181  if( bboxInit )
182  bbox = zone->GetBoundingBox();
183  else
184  bbox.Merge( zone->GetBoundingBox() );
185 
186  bboxInit = false;
187  }
188 
189  SaveCopyInUndoList( undoListPicker, UR_NEW );
190 
191  // we should not ask PLUGINs to do these items:
192  int copperLayerCount = GetBoard()->GetCopperLayerCount();
193 
194  if( copperLayerCount > initialCopperLayerCount )
195  GetBoard()->SetCopperLayerCount( copperLayerCount );
196 
197  // Enable all used layers, and make them visible:
198  LSET enabledLayers = GetBoard()->GetEnabledLayers();
199  enabledLayers |= initialEnabledLayers;
200  GetBoard()->SetEnabledLayers( enabledLayers );
201  GetBoard()->SetVisibleLayers( enabledLayers );
204 
205  if( IsGalCanvasActive() )
206  static_cast<PCB_DRAW_PANEL_GAL*>( GetGalCanvas() )->SyncLayersVisibility( GetBoard() );
207 
210 
211  SetStatusText( wxEmptyString );
212  BestZoom();
213 
214  // Finish block move command:
215  wxPoint cpos = GetNearestGridPosition( bbox.Centre() );
216  blockmove.SetOrigin( bbox.GetOrigin() );
217  blockmove.SetSize( bbox.GetSize() );
218  blockmove.SetLastCursorPosition( cpos );
219  HandleBlockEnd( NULL );
220 
221  return true;
222 }
void BuildListOfNets()
Definition: class_board.h:764
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:78
virtual const EDA_RECT GetBoundingBox() const
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
BLOCK_SELECTOR m_BlockLocate
Block description for block commands.
void Merge(const EDA_RECT &aRect)
Function Merge modifies the position and size of the rectangle in order to contain aRect...
This file is part of the common library.
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
Class BOARD to handle a board.
MODULE * Next() const
Definition: class_module.h:99
int GetCopperLayerCount() const
Function GetCopperLayerCount.
void SetCopperLayerCount(int aCount)
void ReCreateLayerBox(bool aForceResizeToolbar=true)
Re create the layer Box by clearing the old list, and building le new one, from the new layers names ...
Definition: tool_pcb.cpp:709
void SetOrigin(const wxPoint &pos)
BOARD * GetBoard() const
Classes to handle copper zones.
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
Class PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
LSET GetEnabledLayers() const
Function GetEnabledLayers is a proxy function that calls the corresponding function in m_BoardSetting...
BOARD_ITEM * Next() const
Class RELEASER releases a PLUGIN in the context of a potential thrown exception, through its destruct...
Definition: io_mgr.h:476
bool AppendBoardFile(const wxString &aFullFileName, int aCtl)
Function AppendBoardFile appends a board file onto the current one, creating God knows what...
const wxPoint & GetOrigin() const
virtual bool HandleBlockBegin(wxDC *aDC, EDA_KEY aKey, const wxPoint &aPosition, int aExplicitCommand=0)
Function HandleBlockBegin initializes the block command including the command type, initial position, and other variables.
Definition: draw_frame.cpp:801
void SynchronizeNetsAndNetClasses()
Function SynchronizeNetsAndNetClasses copies NETCLASS info to each NET, based on NET membership in a ...
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
Class LSET is a set of LAYER_IDs.
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:253
T * GetLast() const
Function GetLast returns the last T* in the list without removing it, or NULL if the list is empty...
Definition: dlist.h:170
void SetVisibleLayers(LSET aLayerMask)
Function SetVisibleLayers is a proxy function that calls the correspondent function in m_BoardSetting...
virtual bool HandleBlockEnd(wxDC *DC) override
Function HandleBlockEnd() Handle the "end" of a block command, i.e.
DLIST< BOARD_ITEM > m_Drawings
Definition: class_board.h:242
bool IsGalCanvasActive() const
Function IsGalCanvasActive is used to check which canvas (GAL-based or standard) is currently in use...
Definition: draw_frame.h:795
virtual BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings returns the BOARD_DESIGN_SETTINGS for the BOARD owned by this frame...
int GetAreaCount() const
Function GetAreaCount.
Definition: class_board.h:1051
wxPoint Centre() const
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
void SaveCopyInUndoList(BOARD_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, const wxPoint &aTransformPoint=wxPoint(0, 0)) override
Function SaveCopyInUndoList Creates a new entry in undo list of commands.
Definition: undo_redo.cpp:172
void SetItem(EDA_ITEM *aItem)
#define FLAG0
Pcbnew: flag used in local computations.
Definition: base_struct.h:131
virtual double BestZoom() override
Function BestZoom.
void SetSize(const wxSize &size)
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
wxPoint GetNearestGridPosition(const wxPoint &aPosition, wxRealPoint *aGridSize=NULL) const
Function GetNearestGridPosition returns the nearest aGridSize location to aPosition.
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void Clear()
Function Clear destroys any contained NETCLASS instances except the Default one.
ZONE_CONTAINER * GetArea(int index) const
Function GetArea returns the Area (Zone Container) at a given index.
Definition: class_board.h:1022
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
DLIST< MODULE > m_Modules
Definition: class_board.h:243
static PLUGIN * PluginFind(PCB_FILE_T aFileType)
Function PluginFind returns a PLUGIN which the caller can use to import, export, save, or load design documents.
Definition: io_mgr.cpp:58
Class EDA_RECT handles the component boundary box.
PCB_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
PICKED_ITEMS_LIST & GetItems()
const wxSize GetPageSizeIU() const override
Function GetPageSizeIU works off of GetPageSettings() to return the size of the paper page in the int...
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:254
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Function GetGalCanvas returns a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:803
IO_MGR::PCB_FILE_T plugin_type(const wxString &aFileName, int aCtl)
Module description (excepted pads)
void ReFillLayerWidget()
Function ReFillLayerWidget changes out all the layers in m_Layers and may be called upon loading a ne...
Definition: pcbframe.cpp:581
void SetLastCursorPosition(const wxPoint &aPosition)
Function SetLastCursorPosition sets the last cursor position to aPosition.
PCB_FILE_T
Enum PCB_FILE_T is a set of file types that the IO_MGR knows about, and for which there has been a pl...
Definition: io_mgr.h:51
const wxSize & GetSize() const
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:69
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
void SetEnabledLayers(LSET aLayerMask)
Function SetEnabledLayers is a proxy function that calls the correspondent function in m_BoardSetting...
NETCLASSES m_NetClasses
List of current netclasses. There is always the default netclass.
virtual BOARD * Load(const wxString &aFileName, BOARD *aAppendToMe, const PROPERTIES *aProperties=NULL)
Function Load loads information from some input file format that this PLUGIN implementation knows abo...
Definition: plugin.cpp:47
#define IS_MOVED
Item being moved.
Definition: base_struct.h:112