KiCad PCB EDA Suite
ds_data_model_io.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-2016 CERN
5  * Copyright (C) 2019-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include <string_utils.h>
28 #include <locale_io.h>
29 #include <macros.h>
30 #include <math/vector2d.h>
34 #include <drawing_sheet/drawing_sheet_lexer.h>
36 
37 #include <wx/msgdlg.h>
38 
39 using namespace DRAWINGSHEET_T;
40 
41 #define double2Str Double2Str
42 
43 // A helper function to write tokens:
44 static const char* getTokenName( T aTok )
45 {
46  return DRAWING_SHEET_LEXER::TokenName( aTok );
47 }
48 
49 // A basic helper class to write a drawing sheet file
50 // Not used alone, a file writer or a string writer should be derived to use it.
51 // Therefore the constructor is protected.
53 {
54 public:
55  void Format( DS_DATA_MODEL* aSheet ) const;
56 
57  void Format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem, int aNestLevel ) const;
58 
59 protected:
60  DS_DATA_MODEL_IO() { m_out = NULL; }
61  virtual ~DS_DATA_MODEL_IO() {}
62 
63 private:
64  void format( DS_DATA_ITEM_TEXT* aItem, int aNestLevel ) const;
65  void format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem, int aNestLevel ) const;
66  void format( DS_DATA_ITEM_POLYGONS* aItem, int aNestLevel ) const;
67  void format( DS_DATA_ITEM_BITMAP* aItem, int aNestLevel ) const;
68  void formatCoordinate( const char* aToken, POINT_COORD& aCoord ) const;
69  void formatRepeatParameters( DS_DATA_ITEM* aItem ) const;
70  void formatOptions( DS_DATA_ITEM* aItem ) const;
71 
72 protected:
74 };
75 
76 
77 // A helper class to write a drawing sheet to a file
79 {
80 public:
81  DS_DATA_MODEL_FILEIO( const wxString& aFilename ) :
83  {
84  try
85  {
86  m_fileout = new FILE_OUTPUTFORMATTER( aFilename );
87  m_out = m_fileout;
88  }
89  catch( const IO_ERROR& ioe )
90  {
91  wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
92  }
93  }
94 
96  {
97  delete m_fileout;
98  }
99 
100 private:
102 };
103 
104 
105 // A helper class to write a drawing sheet to a string
107 {
108 public:
109  DS_DATA_MODEL_STRINGIO( wxString* aOutputString ) :
111  m_output( aOutputString )
112  {
113  try
114  {
115  m_writer = new STRING_FORMATTER();
116  m_out = m_writer;
117  }
118  catch( const IO_ERROR& ioe )
119  {
120  wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
121  }
122  }
123 
125  {
126  *m_output = FROM_UTF8( m_writer->GetString().c_str() );
127  delete m_writer;
128  }
129 
130 private:
132  wxString* m_output;
133 };
134 
135 
136 void DS_DATA_MODEL::Save( const wxString& aFullFileName )
137 {
138  DS_DATA_MODEL_FILEIO writer( aFullFileName );
139  writer.Format( this );
140 }
141 
142 
143 void DS_DATA_MODEL::SaveInString( wxString* aOutputString )
144 {
145  DS_DATA_MODEL_STRINGIO writer( aOutputString );
146  writer.Format( this );
147 }
148 
149 
150 void DS_DATA_MODEL::SaveInString( std::vector<DS_DATA_ITEM*>& aItemsList, wxString* aOutputString )
151 {
152  DS_DATA_MODEL_STRINGIO writer( aOutputString );
153 
154  LOCALE_IO toggle; // switch on/off the locale "C" notation
155 
156  for( DS_DATA_ITEM* item : aItemsList )
157  writer.Format( this, item, 0 );
158 }
159 
160 
161 void DS_DATA_MODEL_IO::Format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem, int aNestLevel ) const
162 {
163  switch( aItem->GetType() )
164  {
166  format( (DS_DATA_ITEM_TEXT*) aItem, aNestLevel );
167  break;
168 
171  format( aModel, aItem, aNestLevel );
172  break;
173 
175  format( (DS_DATA_ITEM_POLYGONS*) aItem, aNestLevel );
176  break;
177 
179  format( (DS_DATA_ITEM_BITMAP*) aItem, aNestLevel );
180  break;
181 
182  default:
183  wxFAIL_MSG( wxT( "Cannot format item" ) );
184  }
185 }
186 
187 
189 {
190  LOCALE_IO toggle; // switch on/off the locale "C" notation
191 
192  m_out->Print( 0, "(kicad_wks (version %d) (generator pl_editor)\n",
194 
195  // Setup
196  int nestLevel = 1;
197  // Write default values:
198  m_out->Print( nestLevel, "(setup " );
199  m_out->Print( 0, "(textsize %s %s)",
200  double2Str( aSheet->m_DefaultTextSize.x ).c_str(),
201  double2Str( aSheet->m_DefaultTextSize.y ).c_str() );
202  m_out->Print( 0, "(linewidth %s)", double2Str( aSheet->m_DefaultLineWidth ).c_str() );
203  m_out->Print( 0, "(textlinewidth %s)", double2Str( aSheet->m_DefaultTextThickness ).c_str() );
204  m_out->Print( 0, "\n" );
205 
206  // Write margin values
207  m_out->Print( nestLevel, "(left_margin %s)", double2Str( aSheet->GetLeftMargin() ).c_str() );
208  m_out->Print( 0, "(right_margin %s)", double2Str( aSheet->GetRightMargin() ).c_str() );
209  m_out->Print( 0, "(top_margin %s)", double2Str( aSheet->GetTopMargin() ).c_str() );
210  m_out->Print( 0, "(bottom_margin %s)", double2Str( aSheet->GetBottomMargin() ).c_str() );
211  m_out->Print( 0, ")\n" );
212 
213  // Save the graphical items on the drawing sheet
214  for( unsigned ii = 0; ii < aSheet->GetCount(); ii++ )
215  {
216  DS_DATA_ITEM* item = aSheet->GetItem( ii );
217  Format( aSheet, item, nestLevel );
218  }
219 
220  m_out->Print( 0, ")\n" );
221 }
222 
223 
224 void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_TEXT* aItem, int aNestLevel ) const
225 {
226  m_out->Print( aNestLevel, "(tbtext" );
227  m_out->Print( 0, " %s", m_out->Quotew( aItem->m_TextBase ).c_str() );
228  m_out->Print( 0, " (name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
229 
230  formatCoordinate( getTokenName( T_pos ), aItem->m_Pos );
231  formatOptions( aItem );
232 
233  if( aItem->m_Orient )
234  m_out->Print( 0, " (rotate %s)", double2Str(aItem->m_Orient ).c_str() );
235 
236  // Write font info, only if it is not the default setup
237  bool write_size = aItem->m_TextSize.x != 0.0 || aItem->m_TextSize.y != 0.0;
238  bool write_thickness = aItem->m_LineWidth != 0.0;
239 
240  if( write_thickness || write_size || aItem->m_Bold || aItem->m_Italic )
241  {
242  m_out->Print( 0, " (font" );
243 
244  if( write_thickness )
245  m_out->Print( 0, " (linewidth %s)", double2Str( aItem->m_LineWidth ).c_str() );
246 
247  if( write_size )
248  {
249  m_out->Print( 0, " (size %s %s)",
250  double2Str( aItem->m_TextSize.x ).c_str(),
251  double2Str( aItem->m_TextSize.y ).c_str() );
252  }
253 
254  if( aItem->m_Bold )
255  m_out->Print( 0, " bold" );
256 
257  if( aItem->m_Italic )
258  m_out->Print( 0, " italic" );
259 
260  m_out->Print( 0, ")" );
261  }
262 
263  // Write text justification
265  {
266  m_out->Print( 0, " (justify" );
267 
268  // Write T_center opt first, because it is
269  // also a center for both m_Hjustify and m_Vjustify
270  if( aItem->m_Hjustify == GR_TEXT_HJUSTIFY_CENTER )
271  m_out->Print( 0, " center" );
272  else if( aItem->m_Hjustify == GR_TEXT_HJUSTIFY_RIGHT )
273  m_out->Print( 0, " right" );
274 
275  if( aItem->m_Vjustify == GR_TEXT_VJUSTIFY_TOP )
276  m_out->Print( 0, " top" );
277  else if( aItem->m_Vjustify == GR_TEXT_VJUSTIFY_BOTTOM )
278  m_out->Print( 0, " bottom" );
279 
280  m_out->Print( 0, ")" );
281  }
282 
283  // write constraints
284  if( aItem->m_BoundingBoxSize.x )
285  m_out->Print( 0, " (maxlen %s)", double2Str(aItem->m_BoundingBoxSize.x ).c_str() );
286 
287  if( aItem->m_BoundingBoxSize.y )
288  m_out->Print( 0, " (maxheight %s)", double2Str(aItem->m_BoundingBoxSize.y ).c_str() );
289 
290  formatRepeatParameters( aItem );
291 
292  if( !aItem->m_Info.IsEmpty() )
293  m_out->Print( 0, " (comment %s)\n", m_out->Quotew( aItem->m_Info ).c_str() );
294 
295  m_out->Print( 0, ")\n" );
296 }
297 
298 
299 void DS_DATA_MODEL_IO::format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem, int aNestLevel ) const
300 {
301  if( aItem->GetType() == DS_DATA_ITEM::DS_RECT )
302  m_out->Print( aNestLevel, "(rect" );
303  else
304  m_out->Print( aNestLevel, "(line" );
305 
306  m_out->Print( 0, " (name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
307 
308  formatCoordinate( getTokenName( T_start ), aItem->m_Pos );
309  formatCoordinate( getTokenName( T_end ), aItem->m_End );
310  formatOptions( aItem );
311 
312  if( aItem->m_LineWidth && aItem->m_LineWidth != aModel->m_DefaultLineWidth )
313  m_out->Print( 0, " (linewidth %s)", double2Str( aItem->m_LineWidth ).c_str() );
314 
315  formatRepeatParameters( aItem );
316 
317  if( !aItem->m_Info.IsEmpty() )
318  m_out->Print( 0, " (comment %s)\n", m_out->Quotew( aItem->m_Info ).c_str() );
319 
320  m_out->Print( 0, ")\n" );
321 }
322 
323 
324 void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_POLYGONS* aItem, int aNestLevel ) const
325 {
326  m_out->Print( aNestLevel, "(polygon" );
327  m_out->Print( 0, " (name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
328  formatCoordinate( "pos", aItem->m_Pos );
329  formatOptions( aItem );
330 
331  formatRepeatParameters( aItem );
332 
333  if( aItem->m_Orient )
334  m_out->Print( 0, " (rotate %s)", double2Str(aItem->m_Orient ).c_str() );
335 
336  if( aItem->m_LineWidth )
337  m_out->Print( 0, " (linewidth %s)\n", double2Str( aItem->m_LineWidth ).c_str() );
338 
339  if( !aItem->m_Info.IsEmpty() )
340  m_out->Print( 0, " (comment %s)\n", m_out->Quotew( aItem->m_Info ).c_str() );
341 
342  // Write polygon corners list
343  for( int kk = 0; kk < aItem->GetPolyCount(); kk++ )
344  {
345  m_out->Print( aNestLevel+1, "(pts" );
346  // Create current polygon corners list
347  unsigned ist = aItem->GetPolyIndexStart( kk );
348  unsigned iend = aItem->GetPolyIndexEnd( kk );
349  int ii = 0;
350 
351  while( ist <= iend )
352  {
353  DPOINT pos = aItem->m_Corners[ist++];
354  int nestLevel = 0;
355 
356  if( ii++ > 4)
357  {
358  m_out->Print( 0, "\n" );
359  nestLevel = aNestLevel+2;
360  ii = 0;
361  }
362 
363  m_out->Print( nestLevel, " (xy %s %s)",
364  double2Str( pos.x ).c_str(),
365  double2Str( pos.y ).c_str() );
366  }
367 
368  m_out->Print( 0, ")\n" );
369  }
370 
371  m_out->Print( aNestLevel, ")\n" );
372 }
373 
374 
375 void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_BITMAP* aItem, int aNestLevel ) const
376 {
377  m_out->Print( aNestLevel, "(bitmap" );
378  m_out->Print( 0, " (name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
379  formatCoordinate( "pos", aItem->m_Pos );
380  formatOptions( aItem );
381 
382  m_out->Print( 0, " (scale %s)", double2Str( aItem->m_ImageBitmap->GetScale() ).c_str() );
383 
384  formatRepeatParameters( aItem );
385  m_out->Print( 0,"\n");
386 
387  if( !aItem->m_Info.IsEmpty() )
388  m_out->Print( 0, " (comment %s)\n", m_out->Quotew( aItem->m_Info ).c_str() );
389 
390  // Write image in png readable format
391  m_out->Print( aNestLevel, "(pngdata\n" );
392  wxArrayString pngStrings;
393  aItem->m_ImageBitmap->SaveData( pngStrings );
394 
395  for( unsigned ii = 0; ii < pngStrings.GetCount(); ii++ )
396  m_out->Print( aNestLevel+1, "(data \"%s\")\n", TO_UTF8(pngStrings[ii]) );
397 
398  m_out->Print( aNestLevel+1, ")\n" );
399 
400  m_out->Print( aNestLevel, ")\n" );
401 }
402 
403 
404 void DS_DATA_MODEL_IO::formatCoordinate( const char * aToken, POINT_COORD & aCoord ) const
405 {
406  m_out->Print( 0, " (%s %s %s", aToken,
407  double2Str( aCoord.m_Pos.x ).c_str(),
408  double2Str( aCoord.m_Pos.y ).c_str() );
409 
410  switch( aCoord.m_Anchor )
411  {
412  case RB_CORNER: break;
413  case LT_CORNER: m_out->Print( 0, " ltcorner" ); break;
414  case LB_CORNER: m_out->Print( 0, " lbcorner" ); break;
415  case RT_CORNER: m_out->Print( 0, " rtcorner" ); break;
416  }
417 
418  m_out->Print( 0, ")" );
419 }
420 
421 
423 {
424  if( aItem->m_RepeatCount <= 1 )
425  return;
426 
427  m_out->Print( 0, " (repeat %d)", aItem->m_RepeatCount );
428 
429  if( aItem->m_IncrementVector.x )
430  m_out->Print( 0, " (incrx %s)", double2Str(aItem-> m_IncrementVector.x ).c_str() );
431 
432  if( aItem->m_IncrementVector.y )
433  m_out->Print( 0, " (incry %s)", double2Str( aItem->m_IncrementVector.y ).c_str() );
434 
435  if( aItem->m_IncrementLabel != 1 && aItem->GetType() == DS_DATA_ITEM::DS_TEXT )
436  m_out->Print( 0, " (incrlabel %d)", aItem->m_IncrementLabel );
437 }
438 
439 
441 {
442  if( aItem->GetPage1Option() == FIRST_PAGE_ONLY )
443  m_out->Print( 0, " (option page1only)" );
444  else if( aItem->GetPage1Option() == SUBSEQUENT_PAGES )
445  m_out->Print( 0, " (option notonpage1)" );
446 }
void formatCoordinate(const char *aToken, POINT_COORD &aCoord) const
wxString m_Info
Definition: ds_data_item.h:199
unsigned GetPolyIndexStart(unsigned aContour) const
Definition: ds_data_item.h:252
Handle the graphic items list to draw/plot the frame and title block.
Definition: ds_data_model.h:38
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
EDA_TEXT_VJUSTIFY_T m_Vjustify
Definition: ds_data_item.h:339
bool SaveData(FILE *aFile) const
Write the bitmap data to aFile.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
double GetRightMargin()
Definition: ds_data_model.h:66
int GetPolyCount() const
Definition: ds_data_item.h:246
double m_DefaultLineWidth
double GetScale() const
Definition: bitmap_base.h:79
double GetTopMargin()
Definition: ds_data_model.h:69
static const char * getTokenName(T aTok)
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:309
double GetLeftMargin()
Definition: ds_data_model.h:63
std::vector< DPOINT > m_Corners
Definition: ds_data_item.h:288
POINT_COORD m_End
Definition: ds_data_item.h:201
unsigned GetPolyIndexEnd(unsigned aContour) const
Definition: ds_data_item.h:264
void Save(const wxString &aFullFileName)
Save the description in a file.
double m_DefaultTextThickness
This file contains miscellaneous commonly used macros and functions.
FILE_OUTPUTFORMATTER * m_fileout
EDA_TEXT_HJUSTIFY_T m_Hjustify
Definition: ds_data_item.h:338
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
DPOINT m_IncrementVector
Definition: ds_data_item.h:204
OUTPUTFORMATTER * m_out
double m_LineWidth
Definition: ds_data_item.h:202
DS_ITEM_TYPE GetType() const
Definition: ds_data_item.h:128
virtual ~DS_DATA_MODEL_IO()
DSIZE m_DefaultTextSize
BITMAP_BASE * m_ImageBitmap
Definition: ds_data_item.h:368
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
DPOINT m_Pos
Definition: ds_data_item.h:80
double GetBottomMargin()
Definition: ds_data_model.h:72
int m_IncrementLabel
Definition: ds_data_item.h:205
void Format(DS_DATA_MODEL *aSheet) const
#define SEXPR_WORKSHEET_FILE_VERSION
This file contains the file format version information for the s-expression drawing sheet file format...
#define _(s)
DS_DATA_MODEL_FILEIO(const wxString &aFilename)
STRING_FORMATTER * m_writer
A coordinate point.
Definition: ds_data_item.h:69
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
void format(DS_DATA_ITEM_TEXT *aItem, int aNestLevel) const
POINT_COORD m_Pos
Definition: ds_data_item.h:200
DS_DATA_ITEM * GetItem(unsigned aIdx) const
Drawing sheet structure type definitions.
Definition: ds_data_item.h:95
void formatRepeatParameters(DS_DATA_ITEM *aItem) const
DS_DATA_MODEL_STRINGIO(wxString *aOutputString)
void SaveInString(wxString *aOutputString)
Save the description in a buffer.
unsigned GetCount() const
Used for text file output.
Definition: richio.h:456
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:414
wxString m_Name
Definition: ds_data_item.h:198
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
#define double2Str
PAGE_OPTION GetPage1Option() const
Definition: ds_data_item.h:133
void formatOptions(DS_DATA_ITEM *aItem) const