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()->DrawingsList().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  {
90  track->ClearFlags( FLAG0 );
91  }
92 
93  DisplayErrorMessage( this, _( "Error loading board in AppendBoardFile" ), ioe.What() );
94 
95  return false;
96  }
97 
98  // Now prepare a block move command to place the new items, and
99  // prepare the undo command.
100  BLOCK_SELECTOR& blockmove = GetScreen()->m_BlockLocate;
102  PICKED_ITEMS_LIST& blockitemsList = blockmove.GetItems();
103  PICKED_ITEMS_LIST undoListPicker;
104  ITEM_PICKER picker( NULL, UR_NEW );
105 
106  EDA_RECT bbox; // the new items bounding box, for block move
107  bool bboxInit = true; // true until the bounding box is initialized
108 
109  for( TRACK* track = GetBoard()->m_Track; track; track = track->Next() )
110  {
111  if( track->GetFlags() & FLAG0 )
112  {
113  track->ClearFlags( FLAG0 );
114  continue;
115  }
116 
117  track->SetFlags( IS_MOVED );
118  picker.SetItem( track );
119  undoListPicker.PushItem( picker );
120  blockitemsList.PushItem( picker );
121 
122  if( bboxInit )
123  bbox = track->GetBoundingBox();
124  else
125  bbox.Merge( track->GetBoundingBox() );
126 
127  bboxInit = false;
128  }
129 
130  if( module )
131  module = module->Next();
132  else
133  module = GetBoard()->m_Modules;
134 
135  for( ; module; module = module->Next() )
136  {
137  module->SetFlags( IS_MOVED );
138  picker.SetItem( module );
139  undoListPicker.PushItem( picker );
140  blockitemsList.PushItem( picker );
141 
142  if( bboxInit )
143  bbox = module->GetBoundingBox();
144  else
145  bbox.Merge( module->GetBoundingBox() );
146 
147  bboxInit = false;
148  }
149 
150  if( drawing )
151  drawing = drawing->Next();
152  else
153  drawing = GetBoard()->DrawingsList();
154 
155  for( ; drawing; drawing = drawing->Next() )
156  {
157  drawing->SetFlags( IS_MOVED );
158  picker.SetItem( drawing );
159  undoListPicker.PushItem( picker );
160  blockitemsList.PushItem( picker );
161 
162  if( bboxInit )
163  bbox = drawing->GetBoundingBox();
164  else
165  bbox.Merge( drawing->GetBoundingBox() );
166 
167  bboxInit = false;
168  }
169 
170  for( ZONE_CONTAINER* zone = GetBoard()->GetArea( zonescount ); zone;
171  zone = GetBoard()->GetArea( zonescount ) )
172  {
173  zone->SetFlags( IS_MOVED );
174  picker.SetItem( zone );
175  undoListPicker.PushItem( picker );
176  blockitemsList.PushItem( picker );
177  zonescount++;
178 
179  if( bboxInit )
180  bbox = zone->GetBoundingBox();
181  else
182  bbox.Merge( zone->GetBoundingBox() );
183 
184  bboxInit = false;
185  }
186 
187  SaveCopyInUndoList( undoListPicker, UR_NEW );
188 
189  // we should not ask PLUGINs to do these items:
190  int copperLayerCount = GetBoard()->GetCopperLayerCount();
191 
192  if( copperLayerCount > initialCopperLayerCount )
193  GetBoard()->SetCopperLayerCount( copperLayerCount );
194 
195  // Enable all used layers, and make them visible:
196  LSET enabledLayers = GetBoard()->GetEnabledLayers();
197  enabledLayers |= initialEnabledLayers;
198  GetBoard()->SetEnabledLayers( enabledLayers );
199  GetBoard()->SetVisibleLayers( enabledLayers );
202 
203  if( IsGalCanvasActive() )
204  static_cast<PCB_DRAW_PANEL_GAL*>( GetGalCanvas() )->SyncLayersVisibility( GetBoard() );
205 
208 
209  SetStatusText( wxEmptyString );
210  BestZoom();
211 
212  // Finish block move command:
213  wxPoint cpos = GetNearestGridPosition( bbox.Centre() );
214  blockmove.SetOrigin( bbox.GetOrigin() );
215  blockmove.SetSize( bbox.GetSize() );
216  blockmove.SetLastCursorPosition( cpos );
217  HandleBlockEnd( NULL );
218 
219  return true;
220 }
void BuildListOfNets()
Definition: class_board.h:722
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:60
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.
const wxPoint GetOrigin() const
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...
const wxSize GetSize() const
DLIST< BOARD_ITEM > & DrawingsList()
Definition: class_board.h:256
Class BOARD to handle a board.
MODULE * Next() const
Definition: class_module.h:120
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:714
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:546
bool AppendBoardFile(const wxString &aFullFileName, int aCtl)
Function AppendBoardFile appends a board file onto the current one, creating God knows what...
virtual bool HandleBlockBegin(wxDC *aDC, EDA_KEY aKey, const wxPoint &aPosition, int aExplicitCommand=0)
Initialize a block command.
Definition: draw_frame.cpp:834
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 PCB_LAYER_IDs.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:87
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:280
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.
bool IsGalCanvasActive() const
Function IsGalCanvasActive is used to check which canvas (GAL-based or standard) is currently in use...
Definition: draw_frame.h:856
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:1011
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:199
void SetItem(EDA_ITEM *aItem)
#define FLAG0
Pcbnew: flag used in local computations.
Definition: base_struct.h:145
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.
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:982
DLIST< MODULE > m_Modules
Definition: class_board.h:245
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:281
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Function GetGalCanvas returns a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:864
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:578
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
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:126