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.

399 lines
12 KiB

  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. #include <wx/clipbrd.h>
  25. #include <build_version.h>
  26. #include <class_board.h>
  27. #include <class_track.h>
  28. #include <pcb_shape.h>
  29. #include <pcb_text.h>
  30. #include <fp_text.h>
  31. #include <common.h>
  32. #include <netinfo.h>
  33. #include <plugins/kicad/pcb_parser.h>
  34. #include <plugins/kicad/kicad_plugin.h>
  35. #include <kicad_clipboard.h>
  36. CLIPBOARD_IO::CLIPBOARD_IO():
  37. PCB_IO( CTL_FOR_CLIPBOARD ),
  38. m_formatter(),
  39. m_parser( new CLIPBOARD_PARSER() )
  40. {
  41. m_out = &m_formatter;
  42. }
  43. CLIPBOARD_IO::~CLIPBOARD_IO()
  44. {
  45. delete m_parser;
  46. }
  47. STRING_FORMATTER* CLIPBOARD_IO::GetFormatter()
  48. {
  49. return &m_formatter;
  50. }
  51. void CLIPBOARD_IO::SetBoard( BOARD* aBoard )
  52. {
  53. m_board = aBoard;
  54. }
  55. void CLIPBOARD_IO::SaveSelection( const PCBNEW_SELECTION& aSelected, bool isModEdit )
  56. {
  57. VECTOR2I refPoint( 0, 0 );
  58. // dont even start if the selection is empty
  59. if( aSelected.Empty() )
  60. return;
  61. if( aSelected.HasReferencePoint() )
  62. refPoint = aSelected.GetReferencePoint();
  63. // Prepare net mapping that assures that net codes saved in a file are consecutive integers
  64. m_mapping->SetBoard( m_board );
  65. if( aSelected.Size() == 1 && aSelected.Front()->Type() == PCB_MODULE_T )
  66. {
  67. // make the module safe to transfer to other pcbs
  68. const MODULE* mod = static_cast<MODULE*>( aSelected.Front() );
  69. // Do not modify existing board
  70. MODULE newModule( *mod );
  71. for( D_PAD* pad : newModule.Pads() )
  72. pad->SetNetCode( 0 );
  73. // locked means "locked in place"; copied items therefore can't be locked
  74. newModule.SetLocked( false );
  75. // locate the reference point at (0, 0) in the copied items
  76. newModule.Move( wxPoint( -refPoint.x, -refPoint.y ) );
  77. Format( static_cast<BOARD_ITEM*>( &newModule ) );
  78. }
  79. else if( isModEdit )
  80. {
  81. MODULE partialModule( m_board );
  82. for( const EDA_ITEM* item : aSelected )
  83. {
  84. BOARD_ITEM* clone = static_cast<BOARD_ITEM*>( item->Clone() );
  85. // Do not add reference/value - convert them to the common type
  86. if( FP_TEXT* text = dyn_cast<FP_TEXT*>( clone ) )
  87. text->SetType( FP_TEXT::TEXT_is_DIVERS );
  88. // If it is only a module, clear the nets from the pads
  89. if( D_PAD* pad = dyn_cast<D_PAD*>( clone ) )
  90. pad->SetNetCode( 0 );
  91. // Add the pad to the new module before moving to ensure the local coords are correct
  92. partialModule.Add( clone );
  93. // locate the reference point at (0, 0) in the copied items
  94. clone->Move( (wxPoint) -refPoint );
  95. }
  96. // Set the new relative internal local coordinates of copied items
  97. MODULE* editedModule = m_board->Modules().front();
  98. wxPoint moveVector = partialModule.GetPosition() + editedModule->GetPosition();
  99. partialModule.MoveAnchorPosition( moveVector );
  100. Format( &partialModule, 0 );
  101. }
  102. else
  103. {
  104. // we will fake being a .kicad_pcb to get the full parser kicking
  105. // This means we also need layers and nets
  106. LOCALE_IO io;
  107. m_formatter.Print( 0, "(kicad_pcb (version %d) (generator pcbnew)\n",
  108. SEXPR_BOARD_FILE_VERSION );
  109. m_formatter.Print( 0, "\n" );
  110. formatBoardLayers( m_board );
  111. formatNetInformation( m_board );
  112. m_formatter.Print( 0, "\n" );
  113. for( EDA_ITEM* i : aSelected )
  114. {
  115. BOARD_ITEM* item = static_cast<BOARD_ITEM*>( i );
  116. BOARD_ITEM* copy = nullptr;
  117. if( item->Type() == PCB_FP_SHAPE_T )
  118. {
  119. // Convert to PCB_SHAPE_T
  120. copy = (BOARD_ITEM*) reinterpret_cast<PCB_SHAPE*>( item )->Clone();
  121. copy->SetLayer( item->GetLayer() );
  122. }
  123. else if( item->Type() == PCB_FP_TEXT_T )
  124. {
  125. // Convert to PCB_TEXT_T
  126. MODULE* mod = static_cast<MODULE*>( item->GetParent() );
  127. FP_TEXT* fp_text = static_cast<FP_TEXT*>( item );
  128. PCB_TEXT* pcb_text = new PCB_TEXT( m_board );
  129. if( fp_text->GetText() == "${VALUE}" )
  130. pcb_text->SetText( mod->GetValue() );
  131. else if( fp_text->GetText() == "${REFERENCE}" )
  132. pcb_text->SetText( mod->GetReference() );
  133. else
  134. pcb_text->CopyText( *fp_text );
  135. pcb_text->SetEffects( *fp_text );
  136. pcb_text->SetLayer( fp_text->GetLayer() );
  137. copy = pcb_text;
  138. }
  139. else if( item->Type() == PCB_PAD_T )
  140. {
  141. // Create a parent to own the copied pad
  142. MODULE* mod = new MODULE( m_board );
  143. D_PAD* pad = (D_PAD*) item->Clone();
  144. mod->SetPosition( pad->GetPosition() );
  145. pad->SetPos0( wxPoint() );
  146. mod->Add( pad );
  147. copy = mod;
  148. }
  149. else if( item->Type() == PCB_FP_ZONE_AREA_T )
  150. {
  151. // Convert to PCB_ZONE_AREA_T
  152. ZONE_CONTAINER* zone = new ZONE_CONTAINER( m_board );
  153. zone->InitDataFromSrcInCopyCtor( *static_cast<ZONE_CONTAINER*>( item ) );
  154. copy = zone;
  155. }
  156. else if( item->Type() == PCB_GROUP_T )
  157. {
  158. copy = static_cast<PCB_GROUP*>( item )->DeepClone();
  159. }
  160. else
  161. {
  162. copy = static_cast<BOARD_ITEM*>( item->Clone() );
  163. }
  164. auto prepItem = [&]( BOARD_ITEM* titem ) {
  165. // locked means "locked in place"; copied items therefore can't be locked
  166. if( MODULE* module = dyn_cast<MODULE*>( titem ) )
  167. module->SetLocked( false );
  168. else if( TRACK* track = dyn_cast<TRACK*>( titem ) )
  169. track->SetLocked( false );
  170. };
  171. if( copy )
  172. {
  173. prepItem( copy );
  174. // locate the reference point at (0, 0) in the copied items
  175. copy->Move( (wxPoint) -refPoint );
  176. Format( copy, 1 );
  177. if( copy->Type() == PCB_GROUP_T )
  178. {
  179. static_cast<PCB_GROUP*>( copy )->RunOnDescendants( prepItem );
  180. static_cast<PCB_GROUP*>( copy )->RunOnDescendants( [&]( BOARD_ITEM* titem ) {
  181. Format( titem, 1 );
  182. } );
  183. }
  184. delete copy;
  185. }
  186. }
  187. m_formatter.Print( 0, "\n)" );
  188. }
  189. // These are placed at the end to minimize the open time of the clipboard
  190. auto clipboard = wxTheClipboard;
  191. wxClipboardLocker clipboardLock( clipboard );
  192. if( !clipboardLock || !clipboard->IsOpened() )
  193. return;
  194. clipboard->SetData( new wxTextDataObject(
  195. wxString( m_formatter.GetString().c_str(), wxConvUTF8 ) ) );
  196. clipboard->Flush();
  197. #ifndef __WXOSX__
  198. // This section exists to return the clipboard data, ensuring it has fully
  199. // been processed by the system clipboard. This appears to be needed for
  200. // extremely large clipboard copies on asynchronous linux clipboard managers
  201. // such as KDE's Klipper. However, a read back of the data on OSX before the
  202. // clipboard is closed seems to cause an ASAN error (heap-buffer-overflow)
  203. // since it uses the cached version of the clipboard data and not the system
  204. // clipboard data.
  205. {
  206. wxTextDataObject data;
  207. clipboard->GetData( data );
  208. ( void )data.GetText(); // Keep unused variable
  209. }
  210. #endif
  211. }
  212. BOARD_ITEM* CLIPBOARD_IO::Parse()
  213. {
  214. BOARD_ITEM* item;
  215. wxString result;
  216. auto clipboard = wxTheClipboard;
  217. wxClipboardLocker clipboardLock( clipboard );
  218. if( !clipboardLock )
  219. return nullptr;
  220. if( clipboard->IsSupported( wxDF_TEXT ) )
  221. {
  222. wxTextDataObject data;
  223. clipboard->GetData( data );
  224. result = data.GetText();
  225. }
  226. try
  227. {
  228. item = PCB_IO::Parse( result );
  229. }
  230. catch (...)
  231. {
  232. item = nullptr;
  233. }
  234. return item;
  235. }
  236. void CLIPBOARD_IO::Save( const wxString& aFileName, BOARD* aBoard,
  237. const PROPERTIES* aProperties )
  238. {
  239. init( aProperties );
  240. m_board = aBoard; // after init()
  241. // Prepare net mapping that assures that net codes saved in a file are consecutive integers
  242. m_mapping->SetBoard( aBoard );
  243. STRING_FORMATTER formatter;
  244. m_out = &formatter;
  245. m_out->Print( 0, "(kicad_pcb (version %d) (generator pcbnew)\n", SEXPR_BOARD_FILE_VERSION );
  246. Format( aBoard, 1 );
  247. m_out->Print( 0, ")\n" );
  248. auto clipboard = wxTheClipboard;
  249. wxClipboardLocker clipboardLock( clipboard );
  250. if( !clipboardLock )
  251. return;
  252. clipboard->SetData( new wxTextDataObject(
  253. wxString( m_formatter.GetString().c_str(), wxConvUTF8 ) ) );
  254. clipboard->Flush();
  255. // This section exists to return the clipboard data, ensuring it has fully
  256. // been processed by the system clipboard. This appears to be needed for
  257. // extremely large clipboard copies on asynchronous linux clipboard managers
  258. // such as KDE's Klipper
  259. {
  260. wxTextDataObject data;
  261. clipboard->GetData( data );
  262. ( void )data.GetText(); // Keep unused variable
  263. }
  264. }
  265. BOARD* CLIPBOARD_IO::Load( const wxString& aFileName,
  266. BOARD* aAppendToMe, const PROPERTIES* aProperties )
  267. {
  268. std::string result;
  269. auto clipboard = wxTheClipboard;
  270. wxClipboardLocker clipboardLock( clipboard );
  271. if( !clipboardLock )
  272. return nullptr;
  273. if( clipboard->IsSupported( wxDF_TEXT ) )
  274. {
  275. wxTextDataObject data;
  276. clipboard->GetData( data );
  277. result = data.GetText().mb_str();
  278. }
  279. STRING_LINE_READER reader(result, wxT( "clipboard" ) );
  280. init( aProperties );
  281. m_parser->SetLineReader( &reader );
  282. m_parser->SetBoard( aAppendToMe );
  283. BOARD_ITEM* item;
  284. BOARD* board;
  285. try
  286. {
  287. item = m_parser->Parse();
  288. }
  289. catch( const FUTURE_FORMAT_ERROR& )
  290. {
  291. // Don't wrap a FUTURE_FORMAT_ERROR in another
  292. throw;
  293. }
  294. catch( const PARSE_ERROR& parse_error )
  295. {
  296. if( m_parser->IsTooRecent() )
  297. throw FUTURE_FORMAT_ERROR( parse_error, m_parser->GetRequiredVersion() );
  298. else
  299. throw;
  300. }
  301. if( item->Type() != PCB_T )
  302. {
  303. // The parser loaded something that was valid, but wasn't a board.
  304. THROW_PARSE_ERROR( _( "Clipboard content is not KiCad compatible" ),
  305. m_parser->CurSource(), m_parser->CurLine(),
  306. m_parser->CurLineNumber(), m_parser->CurOffset() );
  307. }
  308. else
  309. {
  310. board = dynamic_cast<BOARD*>( item );
  311. }
  312. // Give the filename to the board if it's new
  313. if( board && !aAppendToMe )
  314. board->SetFileName( aFileName );
  315. return board;
  316. }