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-2022 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#include <font/font.h>
37
38#include <wx/msgdlg.h>
39
40using namespace DRAWINGSHEET_T;
41
42// A helper function to write tokens:
43static const char* getTokenName( T aTok )
44{
45 return DRAWING_SHEET_LEXER::TokenName( aTok );
46}
47
48// A basic helper class to write a drawing sheet file
49// Not used alone, a file writer or a string writer should be derived to use it.
50// Therefore the constructor is protected.
52{
53public:
54 void Format( DS_DATA_MODEL* aSheet ) const;
55
56 void Format( DS_DATA_MODEL* aModel, std::vector<DS_DATA_ITEM*>& aItemsList ) const;
57 void Format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem, int aNestLevel ) const;
58
59protected:
60 DS_DATA_MODEL_IO() { m_out = NULL; }
61 virtual ~DS_DATA_MODEL_IO() {}
62
63private:
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
72protected:
74};
75
76
77// A helper class to write a drawing sheet to a file
79{
80public:
81 DS_DATA_MODEL_FILEIO( const wxString& aFilename ) :
83 m_fileout( nullptr )
84 {
85 try
86 {
87 m_fileout = new FILE_OUTPUTFORMATTER( aFilename );
89 }
90 catch( const IO_ERROR& ioe )
91 {
92 wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
93 }
94 }
95
97 {
98 delete m_fileout;
99 }
100
101private:
103};
104
105
106// A helper class to write a drawing sheet to a string
108{
109public:
110 DS_DATA_MODEL_STRINGIO( wxString* aOutputString ) :
112 m_output( aOutputString )
113 {
114 try
115 {
117 m_out = m_writer;
118 }
119 catch( const IO_ERROR& ioe )
120 {
121 wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
122 }
123 }
124
126 {
127 *m_output = FROM_UTF8( m_writer->GetString().c_str() );
128 delete m_writer;
129 }
130
131private:
133 wxString* m_output;
134};
135
136
137void DS_DATA_MODEL::Save( const wxString& aFullFileName )
138{
139 DS_DATA_MODEL_FILEIO writer( aFullFileName );
140 writer.Format( this );
141}
142
143
144void DS_DATA_MODEL::SaveInString( wxString* aOutputString )
145{
146 DS_DATA_MODEL_STRINGIO writer( aOutputString );
147 writer.Format( this );
148}
149
150
151void DS_DATA_MODEL::SaveInString( std::vector<DS_DATA_ITEM*>& aItemsList, wxString* aOutputString )
152{
153 DS_DATA_MODEL_STRINGIO writer( aOutputString );
154 writer.Format( this, aItemsList );
155}
156
157
158void DS_DATA_MODEL_IO::Format( DS_DATA_MODEL* aModel, std::vector<DS_DATA_ITEM*>& aItemsList ) const
159{
160 LOCALE_IO toggle; // switch on/off the locale "C" notation
161
162 m_out->Print( 0, "(kicad_wks (version %d) (generator pl_editor)\n",
164
165 for( DS_DATA_ITEM* item : aItemsList )
166 Format( aModel, item, 1 );
167
168 m_out->Print( 0, ")\n" );
169}
170
171
172void DS_DATA_MODEL_IO::Format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem, int aNestLevel ) const
173{
174 switch( aItem->GetType() )
175 {
177 format( (DS_DATA_ITEM_TEXT*) aItem, aNestLevel );
178 break;
179
182 format( aModel, aItem, aNestLevel );
183 break;
184
186 format( (DS_DATA_ITEM_POLYGONS*) aItem, aNestLevel );
187 break;
188
190 format( (DS_DATA_ITEM_BITMAP*) aItem, aNestLevel );
191 break;
192
193 default:
194 wxFAIL_MSG( wxT( "Cannot format item" ) );
195 }
196}
197
198
200{
201 LOCALE_IO toggle; // switch on/off the locale "C" notation
202
203 m_out->Print( 0, "(kicad_wks (version %d) (generator pl_editor)\n",
205
206 // Setup
207 int nestLevel = 1;
208 // Write default values:
209 m_out->Print( nestLevel, "(setup " );
210 m_out->Print( 0, "(textsize %s %s)",
211 FormatDouble2Str( aSheet->m_DefaultTextSize.x ).c_str(),
212 FormatDouble2Str( aSheet->m_DefaultTextSize.y ).c_str() );
213 m_out->Print( 0, "(linewidth %s)", FormatDouble2Str( aSheet->m_DefaultLineWidth ).c_str() );
214 m_out->Print( 0, "(textlinewidth %s)", FormatDouble2Str( aSheet->m_DefaultTextThickness ).c_str() );
215 m_out->Print( 0, "\n" );
216
217 // Write margin values
218 m_out->Print( nestLevel, "(left_margin %s)", FormatDouble2Str( aSheet->GetLeftMargin() ).c_str() );
219 m_out->Print( 0, "(right_margin %s)", FormatDouble2Str( aSheet->GetRightMargin() ).c_str() );
220 m_out->Print( 0, "(top_margin %s)", FormatDouble2Str( aSheet->GetTopMargin() ).c_str() );
221 m_out->Print( 0, "(bottom_margin %s)", FormatDouble2Str( aSheet->GetBottomMargin() ).c_str() );
222 m_out->Print( 0, ")\n" );
223
224 // Save the graphical items on the drawing sheet
225 for( unsigned ii = 0; ii < aSheet->GetCount(); ii++ )
226 {
227 DS_DATA_ITEM* item = aSheet->GetItem( ii );
228 Format( aSheet, item, nestLevel );
229 }
230
231 m_out->Print( 0, ")\n" );
232}
233
234
235void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_TEXT* aItem, int aNestLevel ) const
236{
237 m_out->Print( aNestLevel, "(tbtext" );
238 m_out->Print( 0, " %s", m_out->Quotew( aItem->m_TextBase ).c_str() );
239 m_out->Print( 0, " (name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
240
241 formatCoordinate( getTokenName( T_pos ), aItem->m_Pos );
242 formatOptions( aItem );
243
244 if( aItem->m_Orient )
245 m_out->Print( 0, " (rotate %s)", FormatDouble2Str( aItem->m_Orient ).c_str() );
246
247 // Write font info, only if it is not the default setup
248 bool write_size = aItem->m_TextSize.x != 0.0 || aItem->m_TextSize.y != 0.0;
249 bool write_thickness = aItem->m_LineWidth != 0.0;
250
251 if( write_thickness || write_size || aItem->m_Bold || aItem->m_Italic )
252 {
253 m_out->Print( 0, " (font" );
254
255 if( aItem->m_Font && !aItem->m_Font->GetName().IsEmpty() )
256 m_out->Print( 0, " (face \"%s\")", aItem->m_Font->NameAsToken() );
257
258 if( write_thickness )
259 m_out->Print( 0, " (linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
260
261 if( write_size )
262 {
263 m_out->Print( 0, " (size %s %s)",
264 FormatDouble2Str( aItem->m_TextSize.x ).c_str(),
265 FormatDouble2Str( aItem->m_TextSize.y ).c_str() );
266 }
267
268 if( aItem->m_Bold )
269 m_out->Print( 0, " bold" );
270
271 if( aItem->m_Italic )
272 m_out->Print( 0, " italic" );
273
274 if( aItem->m_TextColor != COLOR4D::UNSPECIFIED )
275 {
276 m_out->Print( 0, " (color %d %d %d %s)",
277 KiROUND( aItem->m_TextColor.r * 255.0 ),
278 KiROUND( aItem->m_TextColor.g * 255.0 ),
279 KiROUND( aItem->m_TextColor.b * 255.0 ),
280 FormatDouble2Str( aItem->m_TextColor.a ).c_str() );
281 }
282
283 m_out->Print( 0, ")" );
284 }
285
286 // Write text justification
288 {
289 m_out->Print( 0, " (justify" );
290
291 // Write T_center opt first, because it is
292 // also a center for both m_Hjustify and m_Vjustify
293 if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_CENTER )
294 m_out->Print( 0, " center" );
295 else if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_RIGHT )
296 m_out->Print( 0, " right" );
297
298 if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_TOP )
299 m_out->Print( 0, " top" );
300 else if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_BOTTOM )
301 m_out->Print( 0, " bottom" );
302
303 m_out->Print( 0, ")" );
304 }
305
306 // write constraints
307 if( aItem->m_BoundingBoxSize.x )
308 m_out->Print( 0, " (maxlen %s)", FormatDouble2Str( aItem->m_BoundingBoxSize.x ).c_str() );
309
310 if( aItem->m_BoundingBoxSize.y )
311 m_out->Print( 0, " (maxheight %s)", FormatDouble2Str(aItem->m_BoundingBoxSize.y ).c_str() );
312
313 formatRepeatParameters( aItem );
314
315 if( !aItem->m_Info.IsEmpty() )
316 m_out->Print( 0, " (comment %s)\n", m_out->Quotew( aItem->m_Info ).c_str() );
317
318 m_out->Print( 0, ")\n" );
319}
320
321
322void DS_DATA_MODEL_IO::format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem, int aNestLevel ) const
323{
324 if( aItem->GetType() == DS_DATA_ITEM::DS_RECT )
325 m_out->Print( aNestLevel, "(rect" );
326 else
327 m_out->Print( aNestLevel, "(line" );
328
329 m_out->Print( 0, " (name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
330
331 formatCoordinate( getTokenName( T_start ), aItem->m_Pos );
332 formatCoordinate( getTokenName( T_end ), aItem->m_End );
333 formatOptions( aItem );
334
335 if( aItem->m_LineWidth && aItem->m_LineWidth != aModel->m_DefaultLineWidth )
336 m_out->Print( 0, " (linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
337
338 formatRepeatParameters( aItem );
339
340 if( !aItem->m_Info.IsEmpty() )
341 m_out->Print( 0, " (comment %s)\n", m_out->Quotew( aItem->m_Info ).c_str() );
342
343 m_out->Print( 0, ")\n" );
344}
345
346
347void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_POLYGONS* aItem, int aNestLevel ) const
348{
349 m_out->Print( aNestLevel, "(polygon" );
350 m_out->Print( 0, " (name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
351 formatCoordinate( "pos", aItem->m_Pos );
352 formatOptions( aItem );
353
354 formatRepeatParameters( aItem );
355
356 if( !aItem->m_Orient.IsZero() )
357 m_out->Print( 0, " (rotate %s)", FormatDouble2Str( aItem->m_Orient.AsDegrees() ).c_str() );
358
359 if( aItem->m_LineWidth )
360 m_out->Print( 0, " (linewidth %s)\n", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
361
362 if( !aItem->m_Info.IsEmpty() )
363 m_out->Print( 0, " (comment %s)\n", m_out->Quotew( aItem->m_Info ).c_str() );
364
365 // Write polygon corners list
366 for( int kk = 0; kk < aItem->GetPolyCount(); kk++ )
367 {
368 m_out->Print( aNestLevel+1, "(pts" );
369 // Create current polygon corners list
370 unsigned ist = aItem->GetPolyIndexStart( kk );
371 unsigned iend = aItem->GetPolyIndexEnd( kk );
372 int ii = 0;
373
374 while( ist <= iend )
375 {
376 VECTOR2D pos = aItem->m_Corners[ist++];
377 int nestLevel = 0;
378
379 if( ii++ > 4)
380 {
381 m_out->Print( 0, "\n" );
382 nestLevel = aNestLevel+2;
383 ii = 0;
384 }
385
386 m_out->Print( nestLevel, " (xy %s %s)",
387 FormatDouble2Str( pos.x ).c_str(),
388 FormatDouble2Str( pos.y ).c_str() );
389 }
390
391 m_out->Print( 0, ")\n" );
392 }
393
394 m_out->Print( aNestLevel, ")\n" );
395}
396
397
398void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_BITMAP* aItem, int aNestLevel ) const
399{
400 m_out->Print( aNestLevel, "(bitmap" );
401 m_out->Print( 0, " (name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
402 formatCoordinate( "pos", aItem->m_Pos );
403 formatOptions( aItem );
404
405 m_out->Print( 0, " (scale %s)", FormatDouble2Str( aItem->m_ImageBitmap->GetScale() ).c_str() );
406
407 formatRepeatParameters( aItem );
408 m_out->Print( 0,"\n");
409
410 if( !aItem->m_Info.IsEmpty() )
411 m_out->Print( 0, " (comment %s)\n", m_out->Quotew( aItem->m_Info ).c_str() );
412
413 // Write image in png readable format
414 m_out->Print( aNestLevel, "(pngdata\n" );
415 wxArrayString pngStrings;
416 aItem->m_ImageBitmap->SaveData( pngStrings );
417
418 for( unsigned ii = 0; ii < pngStrings.GetCount(); ii++ )
419 m_out->Print( aNestLevel+1, "(data \"%s\")\n", TO_UTF8(pngStrings[ii]) );
420
421 m_out->Print( aNestLevel+1, ")\n" );
422
423 m_out->Print( aNestLevel, ")\n" );
424}
425
426
427void DS_DATA_MODEL_IO::formatCoordinate( const char * aToken, POINT_COORD & aCoord ) const
428{
429 m_out->Print( 0, " (%s %s %s", aToken,
430 FormatDouble2Str( aCoord.m_Pos.x ).c_str(),
431 FormatDouble2Str( aCoord.m_Pos.y ).c_str() );
432
433 switch( aCoord.m_Anchor )
434 {
435 case RB_CORNER: break;
436 case LT_CORNER: m_out->Print( 0, " ltcorner" ); break;
437 case LB_CORNER: m_out->Print( 0, " lbcorner" ); break;
438 case RT_CORNER: m_out->Print( 0, " rtcorner" ); break;
439 }
440
441 m_out->Print( 0, ")" );
442}
443
444
446{
447 if( aItem->m_RepeatCount <= 1 )
448 return;
449
450 m_out->Print( 0, " (repeat %d)", aItem->m_RepeatCount );
451
452 if( aItem->m_IncrementVector.x )
453 m_out->Print( 0, " (incrx %s)", FormatDouble2Str( aItem->m_IncrementVector.x ).c_str() );
454
455 if( aItem->m_IncrementVector.y )
456 m_out->Print( 0, " (incry %s)", FormatDouble2Str( aItem->m_IncrementVector.y ).c_str() );
457
458 if( aItem->m_IncrementLabel != 1 && aItem->GetType() == DS_DATA_ITEM::DS_TEXT )
459 m_out->Print( 0, " (incrlabel %d)", aItem->m_IncrementLabel );
460}
461
462
464{
465 if( aItem->GetPage1Option() == FIRST_PAGE_ONLY )
466 m_out->Print( 0, " (option page1only)" );
467 else if( aItem->GetPage1Option() == SUBSEQUENT_PAGES )
468 m_out->Print( 0, " (option notonpage1)" );
469}
bool SaveData(FILE *aFile) const
Write the bitmap data to aFile.
double GetScale() const
Definition: bitmap_base.h:78
BITMAP_BASE * m_ImageBitmap
Definition: ds_data_item.h:370
unsigned GetPolyIndexStart(unsigned aContour) const
Definition: ds_data_item.h:252
unsigned GetPolyIndexEnd(unsigned aContour) const
Definition: ds_data_item.h:264
int GetPolyCount() const
Definition: ds_data_item.h:246
std::vector< VECTOR2D > m_Corners
Definition: ds_data_item.h:288
KIFONT::FONT * m_Font
Definition: ds_data_item.h:342
GR_TEXT_H_ALIGN_T m_Hjustify
Definition: ds_data_item.h:338
KIGFX::COLOR4D m_TextColor
Definition: ds_data_item.h:344
VECTOR2D m_BoundingBoxSize
Definition: ds_data_item.h:345
GR_TEXT_V_ALIGN_T m_Vjustify
Definition: ds_data_item.h:339
Drawing sheet structure type definitions.
Definition: ds_data_item.h:96
PAGE_OPTION GetPage1Option() const
Definition: ds_data_item.h:133
DS_ITEM_TYPE GetType() const
Definition: ds_data_item.h:128
wxString m_Name
Definition: ds_data_item.h:198
VECTOR2D m_IncrementVector
Definition: ds_data_item.h:204
POINT_COORD m_Pos
Definition: ds_data_item.h:200
wxString m_Info
Definition: ds_data_item.h:199
double m_LineWidth
Definition: ds_data_item.h:202
POINT_COORD m_End
Definition: ds_data_item.h:201
int m_IncrementLabel
Definition: ds_data_item.h:205
FILE_OUTPUTFORMATTER * m_fileout
DS_DATA_MODEL_FILEIO(const wxString &aFilename)
void Format(DS_DATA_MODEL *aSheet) const
virtual ~DS_DATA_MODEL_IO()
void formatRepeatParameters(DS_DATA_ITEM *aItem) const
OUTPUTFORMATTER * m_out
void format(DS_DATA_ITEM_TEXT *aItem, int aNestLevel) const
void formatOptions(DS_DATA_ITEM *aItem) const
void formatCoordinate(const char *aToken, POINT_COORD &aCoord) const
STRING_FORMATTER * m_writer
DS_DATA_MODEL_STRINGIO(wxString *aOutputString)
Handle the graphic items list to draw/plot the frame and title block.
Definition: ds_data_model.h:39
double GetRightMargin()
Definition: ds_data_model.h:66
VECTOR2D m_DefaultTextSize
double GetTopMargin()
Definition: ds_data_model.h:69
unsigned GetCount() const
double m_DefaultLineWidth
DS_DATA_ITEM * GetItem(unsigned aIdx) const
double GetBottomMargin()
Definition: ds_data_model.h:72
double GetLeftMargin()
Definition: ds_data_model.h:63
double m_DefaultTextThickness
void SaveInString(wxString *aOutputString)
Save the description in a buffer.
void Save(const wxString &aFullFileName)
Save the description in a file.
double AsDegrees() const
Definition: eda_angle.h:149
bool IsZero() const
Definition: eda_angle.h:169
Used for text file output.
Definition: richio.h:457
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
const char * NameAsToken() const
Definition: font.h:122
const wxString & GetName() const
Definition: font.h:121
double r
Red component.
Definition: color4d.h:390
double g
Green component.
Definition: color4d.h:391
double a
Alpha component.
Definition: color4d.h:393
double b
Blue component.
Definition: color4d.h:392
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:41
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:310
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:501
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:433
A coordinate point.
Definition: ds_data_item.h:70
VECTOR2D m_Pos
Definition: ds_data_item.h:80
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:415
const std::string & GetString()
Definition: richio.h:438
@ FIRST_PAGE_ONLY
Definition: ds_data_item.h:58
@ SUBSEQUENT_PAGES
Definition: ds_data_item.h:59
@ RB_CORNER
Definition: ds_data_item.h:49
@ RT_CORNER
Definition: ds_data_item.h:50
@ LT_CORNER
Definition: ds_data_item.h:52
@ LB_CORNER
Definition: ds_data_item.h:51
static const char * getTokenName(T aTok)
#define SEXPR_WORKSHEET_FILE_VERSION
This file contains the file format version information for the s-expression drawing sheet file format...
#define _(s)
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
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
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85