KiCad PCB EDA Suite
kicad_clipboard.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) 2017 KiCad Developers, see CHANGELOG.TXT for contributors.
5  * @author Kristoffer Ödmark
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/clipbrd.h>
26 
27 #include <build_version.h>
28 #include <class_board.h>
29 #include <class_track.h>
30 #include <common.h>
31 #include <netinfo.h>
32 #include <pcb_parser.h>
33 
34 #include <kicad_plugin.h>
35 #include <kicad_clipboard.h>
36 
39  m_formatter(),
40  m_parser( new CLIPBOARD_PARSER() )
41 {
42  m_out = &m_formatter;
43 }
44 
45 
47 {
48  delete m_parser;
49 }
50 
51 
53 {
54  return &m_formatter;
55 }
56 
57 
59 {
60  m_board = aBoard;
61 }
62 
63 
65 {
66  VECTOR2I refPoint( 0, 0 );
67 
68  // dont even start if the selection is empty
69  if( aSelected.Empty() )
70  return;
71 
72  if( aSelected.HasReferencePoint() )
73  refPoint = aSelected.GetReferencePoint();
74 
75  // Prepare net mapping that assures that net codes saved in a file are consecutive integers
77 
78  // Differentiate how it is formatted depending on what selection contains
79  bool onlyModuleParts = true;
80  for( const auto i : aSelected )
81  {
82  // check if it not one of the module primitives
83  if( ( i->Type() != PCB_MODULE_EDGE_T ) &&
84  ( i->Type() != PCB_MODULE_TEXT_T ) &&
85  ( i->Type() != PCB_MODULE_ZONE_AREA_T ) &&
86  ( i->Type() != PCB_PAD_T ) )
87  {
88  onlyModuleParts = false;
89  break;
90  }
91  }
92 
93  // if there is only parts of a module selected, format it as a new module else
94  // format it as an entire board
95  MODULE partialModule( m_board );
96 
97  // only a module selected.
98  if( aSelected.Size() == 1 && aSelected.Front()->Type() == PCB_MODULE_T )
99  {
100  // make the module safe to transfer to other pcbs
101  const MODULE* mod = static_cast<MODULE*>( aSelected.Front() );
102  // Do not modify existing board
103  MODULE newModule( *mod );
104 
105  for( D_PAD* pad : newModule.Pads() )
106  pad->SetNetCode( 0 );
107 
108  // locked means "locked in place"; copied items therefore can't be locked
109  newModule.SetLocked( false );
110 
111  // locate the reference point at (0, 0) in the copied items
112  newModule.Move( wxPoint( -refPoint.x, -refPoint.y ) );
113 
114  Format( static_cast<BOARD_ITEM*>( &newModule ) );
115  }
116  // partial module selected.
117  else if( onlyModuleParts )
118  {
119  for( const auto item : aSelected )
120  {
121  BOARD_ITEM* clone = static_cast<BOARD_ITEM*>( item->Clone() );
122 
123  // Do not add reference/value - convert them to the common type
124  if( TEXTE_MODULE* text = dyn_cast<TEXTE_MODULE*>( clone ) )
125  text->SetType( TEXTE_MODULE::TEXT_is_DIVERS );
126 
127  // If it is only a module, clear the nets from the pads
128  if( D_PAD* pad = dyn_cast<D_PAD*>( clone ) )
129  pad->SetNetCode( 0 );
130 
131  // Add the pad to the new module before moving to ensure the local coords are correct
132  partialModule.Add( clone );
133 
134  // locate the reference point at (0, 0) in the copied items
135  clone->Move( (wxPoint) -refPoint );
136  }
137 
138  // Set the new relative internal local coordinates of copied items
139  MODULE* editedModule = m_board->Modules().front();
140  wxPoint moveVector = partialModule.GetPosition() + editedModule->GetPosition();
141 
142  partialModule.MoveAnchorPosition( moveVector );
143 
144  Format( &partialModule, 0 );
145 
146  }
147  // lots of stuff selected
148  else
149  {
150  // we will fake being a .kicad_pcb to get the full parser kicking
151  // This means we also need layers and nets
152  LOCALE_IO io;
153 
154  m_formatter.Print( 0, "(kicad_pcb (version %d) (host pcbnew %s)\n",
156 
157 
158  m_formatter.Print( 0, "\n" );
159 
162 
163  m_formatter.Print( 0, "\n" );
164 
165 
166  for( const auto i : aSelected )
167  {
168  // Dont format stuff that cannot exist standalone!
169  if( ( i->Type() != PCB_MODULE_EDGE_T ) &&
170  ( i->Type() != PCB_MODULE_TEXT_T ) &&
171  ( i->Type() != PCB_MODULE_ZONE_AREA_T ) &&
172  ( i->Type() != PCB_PAD_T ) )
173  {
174  auto item = static_cast<BOARD_ITEM*>( i );
175  std::unique_ptr<BOARD_ITEM> clone( static_cast<BOARD_ITEM*> ( item->Clone() ) );
176 
177  // locked means "locked in place"; copied items therefore can't be locked
178  if( MODULE* module = dyn_cast<MODULE*>( clone.get() ) )
179  module->SetLocked( false );
180  else if( TRACK* track = dyn_cast<TRACK*>( clone.get() ) )
181  track->SetLocked( false );
182 
183  // locate the reference point at (0, 0) in the copied items
184  clone->Move( (wxPoint) -refPoint );
185 
186  Format( clone.get(), 1 );
187  }
188  }
189  m_formatter.Print( 0, "\n)" );
190  }
191 
192  // These are placed at the end to minimize the open time of the clipboard
193  auto clipboard = wxTheClipboard;
194  wxClipboardLocker clipboardLock( clipboard );
195 
196  if( !clipboardLock || !clipboard->IsOpened() )
197  return;
198 
199  clipboard->SetData( new wxTextDataObject(
200  wxString( m_formatter.GetString().c_str(), wxConvUTF8 ) ) );
201 
202  clipboard->Flush();
203 
204  #ifndef __WXOSX__
205  // This section exists to return the clipboard data, ensuring it has fully
206  // been processed by the system clipboard. This appears to be needed for
207  // extremely large clipboard copies on asynchronous linux clipboard managers
208  // such as KDE's Klipper. However, a read back of the data on OSX before the
209  // clipboard is closed seems to cause an ASAN error (heap-buffer-overflow)
210  // since it uses the cached version of the clipboard data and not the system
211  // clipboard data.
212  {
213  wxTextDataObject data;
214  clipboard->GetData( data );
215  ( void )data.GetText(); // Keep unused variable
216  }
217  #endif
218 }
219 
220 
222 {
223  BOARD_ITEM* item;
224  wxString result;
225 
226  auto clipboard = wxTheClipboard;
227  wxClipboardLocker clipboardLock( clipboard );
228 
229  if( !clipboardLock )
230  return nullptr;
231 
232 
233  if( clipboard->IsSupported( wxDF_TEXT ) )
234  {
235  wxTextDataObject data;
236  clipboard->GetData( data );
237  result = data.GetText();
238  }
239 
240  try
241  {
242  item = PCB_IO::Parse( result );
243  }
244  catch (...)
245  {
246  item = nullptr;
247  }
248 
249  return item;
250 }
251 
252 
253 void CLIPBOARD_IO::Save( const wxString& aFileName, BOARD* aBoard,
254  const PROPERTIES* aProperties )
255 {
256  init( aProperties );
257 
258  m_board = aBoard; // after init()
259 
260  // Prepare net mapping that assures that net codes saved in a file are consecutive integers
261  m_mapping->SetBoard( aBoard );
262 
263  STRING_FORMATTER formatter;
264 
265  m_out = &formatter;
266 
267  m_out->Print( 0, "(kicad_pcb (version %d) (host pcbnew %s)\n", SEXPR_BOARD_FILE_VERSION,
268  formatter.Quotew( GetBuildVersion() ).c_str() );
269 
270  Format( aBoard, 1 );
271 
272  m_out->Print( 0, ")\n" );
273 
274  auto clipboard = wxTheClipboard;
275  wxClipboardLocker clipboardLock( clipboard );
276 
277  if( !clipboardLock )
278  return;
279 
280  clipboard->SetData( new wxTextDataObject(
281  wxString( m_formatter.GetString().c_str(), wxConvUTF8 ) ) );
282  clipboard->Flush();
283 
284  // This section exists to return the clipboard data, ensuring it has fully
285  // been processed by the system clipboard. This appears to be needed for
286  // extremely large clipboard copies on asynchronous linux clipboard managers
287  // such as KDE's Klipper
288  {
289  wxTextDataObject data;
290  clipboard->GetData( data );
291  ( void )data.GetText(); // Keep unused variable
292  }
293 }
294 
295 
296 BOARD* CLIPBOARD_IO::Load( const wxString& aFileName,
297  BOARD* aAppendToMe, const PROPERTIES* aProperties )
298 {
299  std::string result;
300 
301  auto clipboard = wxTheClipboard;
302  wxClipboardLocker clipboardLock( clipboard );
303 
304  if( !clipboardLock )
305  return nullptr;
306 
307  if( clipboard->IsSupported( wxDF_TEXT ) )
308  {
309  wxTextDataObject data;
310  clipboard->GetData( data );
311 
312  result = data.GetText().mb_str();
313  }
314 
315  STRING_LINE_READER reader(result, wxT( "clipboard" ) );
316 
317  init( aProperties );
318 
319  m_parser->SetLineReader( &reader );
320  m_parser->SetBoard( aAppendToMe );
321 
322  BOARD_ITEM* item;
323  BOARD* board;
324 
325  try
326  {
327  item = m_parser->Parse();
328  }
329  catch( const FUTURE_FORMAT_ERROR& )
330  {
331  // Don't wrap a FUTURE_FORMAT_ERROR in another
332  throw;
333  }
334  catch( const PARSE_ERROR& parse_error )
335  {
336  if( m_parser->IsTooRecent() )
337  throw FUTURE_FORMAT_ERROR( parse_error, m_parser->GetRequiredVersion() );
338  else
339  throw;
340  }
341 
342  if( item->Type() != PCB_T )
343  {
344  // The parser loaded something that was valid, but wasn't a board.
345  THROW_PARSE_ERROR( _( "Clipboard content is not KiCad compatible" ),
346  m_parser->CurSource(), m_parser->CurLine(),
347  m_parser->CurLineNumber(), m_parser->CurOffset() );
348  }
349  else
350  {
351  board = dynamic_cast<BOARD*>( item );
352  }
353 
354  // Give the filename to the board if it's new
355  if( board && !aAppendToMe )
356  board->SetFileName( aFileName );
357 
358  return board;
359 }
BOARD * Load(const wxString &aFileName, BOARD *aAppendToMe, const PROPERTIES *aProperties=NULL) override
Function Load loads information from some input file format that this PLUGIN implementation knows abo...
VECTOR2I GetReferencePoint() const
Definition: selection.h:240
BOARD_ITEM * Parse()
OUTPUTFORMATTER * m_out
output any Format()s to this, no ownership
STRING_FORMATTER * GetFormatter()
Definition: typeinfo.h:85
BOARD_ITEM * Parse(const wxString &aClipboardSourceInput)
PCB_IO is a PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:216
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
void init(const PROPERTIES *aProperties)
#define SEXPR_BOARD_FILE_VERSION
Current s-expression file format version. 2 was the last legacy format version.
void formatNetInformation(BOARD *aBoard, int aNestLevel=0) const
formats the Nets and Netclasses
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
STRING_FORMATTER m_formatter
PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
void MoveAnchorPosition(const wxPoint &aMoveVector)
Function MoveAnchorPosition Move the reference point of the footprint It looks like a move footprint:...
void SetBoard(BOARD *aBoard)
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
A single base class (TRACK) represents both tracks and vias, with subclasses for curved tracks (ARC) ...
void SetBoard(const BOARD *aBoard)
Function SetBoard Sets a BOARD object that is used to prepare the net code map.
Definition: netinfo.h:296
class MODULE, a footprint
Definition: typeinfo.h:89
wxString GetRequiredVersion()
Return a string representing the version of kicad required to open this file.
Definition: pcb_parser.cpp:208
void SetBoard(BOARD *aBoard)
Definition: pcb_parser.h:342
MODULES & Modules()
Definition: class_board.h:266
wxString GetBuildVersion()
Get the full KiCad version string.
virtual void Move(const wxPoint &aMoveVector)
Function Move move this object.
void Save(const wxString &aFileName, BOARD *aBoard, const PROPERTIES *aProperties=NULL) override
Function Save will write aBoard to a storage file in a format that this PLUGIN implementation knows a...
const std::string & GetString()
Definition: richio.h:475
std::string Quotew(const wxString &aWrapee)
Definition: richio.cpp:472
void SetFileName(const wxString &aFileName)
Definition: class_board.h:253
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:481
NETINFO_MAPPING * m_mapping
mapping for net codes, so only not empty net codes are stored with consecutive integers as net codes
LINE_READER * SetLineReader(LINE_READER *aReader)
Function SetLineReader sets aLineReader into the parser, and returns the previous one,...
Definition: pcb_parser.h:335
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:121
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
CLIPBOARD_PARSER * m_parser
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
void formatBoardLayers(BOARD *aBoard, int aNestLevel=0) const
formats the board layer information
void SaveSelection(const PCBNEW_SELECTION &selected)
bool IsTooRecent()
Return whether a version number, if any was parsed, was too recent.
Definition: pcb_parser.h:360
Struct PARSE_ERROR contains a filename or source description, a problem input line,...
Definition: ki_exception.h:123
bool HasReferencePoint() const
Definition: selection.h:235
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:180
#define _(s)
Definition: 3d_actions.cpp:33
#define CTL_STD_LAYER_NAMES
Use English Standard layer names.
BOARD * m_board
which BOARD, no ownership here
The common library.
Pcbnew s-expression file format parser definition.
wxPoint GetPosition() const override
Definition: class_module.h:206
Struct FUTURE_FORMAT_ERROR variant of PARSE_ERROR indicating that a syntax or related error was likel...
Definition: ki_exception.h:172
class ZONE_CONTAINER, managed by a footprint
Definition: typeinfo.h:95
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:404
void Format(BOARD_ITEM *aItem, int aNestLevel=0) const
Function Format outputs aItem to aFormatter in s-expression format.
STRING_LINE_READER is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:254
STRING_FORMATTER implements OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:445
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193