KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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 <build_version.h>
28#include <string_utils.h>
29#include <locale_io.h>
30#include <math/vector2d.h>
34#include <drawing_sheet/drawing_sheet_lexer.h>
36#include <font/font.h>
38#include <string_utils.h>
39
40#include <wx/msgdlg.h>
41#include <wx/mstream.h>
42
43using namespace DRAWINGSHEET_T;
44
45
46// A helper function to write tokens:
47static const char* getTokenName( T aTok )
48{
49 return DRAWING_SHEET_LEXER::TokenName( aTok );
50}
51
52
60{
61public:
62 void Format( DS_DATA_MODEL* aSheet ) const;
63
64 void Format( DS_DATA_MODEL* aModel, std::vector<DS_DATA_ITEM*>& aItemsList ) const;
65 void Format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const;
66
67protected:
68 DS_DATA_MODEL_IO() { m_out = NULL; }
69 virtual ~DS_DATA_MODEL_IO() {}
70
71private:
72 void format( DS_DATA_ITEM_TEXT* aItem ) const;
73 void format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const;
74 void format( DS_DATA_ITEM_POLYGONS* aItem ) const;
75 void format( DS_DATA_ITEM_BITMAP* aItem ) const;
76 void formatCoordinate( const char* aToken, POINT_COORD& aCoord ) const;
77 void formatRepeatParameters( DS_DATA_ITEM* aItem ) const;
78 void formatOptions( DS_DATA_ITEM* aItem ) const;
79
80protected:
82};
83
84
89{
90public:
91 DS_DATA_MODEL_FILEIO( const wxString& aFilename ) :
93 m_fileout( nullptr )
94 {
95 try
96 {
99 }
100 catch( const IO_ERROR& ioe )
101 {
102 wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
103 }
104 }
105
107 {
108 delete m_fileout;
109 }
110
111private:
113};
114
115
120{
121public:
122 DS_DATA_MODEL_STRINGIO( wxString* aOutputString ) :
124 m_output( aOutputString )
125 {
126 try
127 {
129 m_out = m_writer;
130 }
131 catch( const IO_ERROR& ioe )
132 {
133 wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
134 }
135 }
136
138 {
139 *m_output = From_UTF8( m_writer->GetString().c_str() );
140 delete m_writer;
141 }
142
143private:
145 wxString* m_output;
146};
147
148
149void DS_DATA_MODEL::Save( const wxString& aFullFileName )
150{
151 DS_DATA_MODEL_FILEIO writer( aFullFileName );
152 writer.Format( this );
153}
154
155
156void DS_DATA_MODEL::SaveInString( wxString* aOutputString )
157{
158 DS_DATA_MODEL_STRINGIO writer( aOutputString );
159 writer.Format( this );
160}
161
162
163void DS_DATA_MODEL::SaveInString( std::vector<DS_DATA_ITEM*>& aItemsList, wxString* aOutputString )
164{
165 DS_DATA_MODEL_STRINGIO writer( aOutputString );
166 writer.Format( this, aItemsList );
167}
168
169
170void DS_DATA_MODEL_IO::Format( DS_DATA_MODEL* aModel, std::vector<DS_DATA_ITEM*>& aItemsList ) const
171{
172 LOCALE_IO toggle; // switch on/off the locale "C" notation
173
174 m_out->Print( "(kicad_wks (version %d) (generator \"pl_editor\") (generator_version %s)",
176 m_out->Quotew( GetMajorMinorVersion() ).c_str() );
177
178 for( DS_DATA_ITEM* item : aItemsList )
179 Format( aModel, item );
180
181 m_out->Print( ")" );
182}
183
184
186{
187 switch( aItem->GetType() )
188 {
190 format( (DS_DATA_ITEM_TEXT*) aItem );
191 break;
192
195 format( aModel, aItem );
196 break;
197
199 format( (DS_DATA_ITEM_POLYGONS*) aItem );
200 break;
201
203 format( (DS_DATA_ITEM_BITMAP*) aItem );
204 break;
205
206 default:
207 wxFAIL_MSG( wxT( "Cannot format item" ) );
208 }
209}
210
211
213{
214 LOCALE_IO toggle; // switch on/off the locale "C" notation
215
216 m_out->Print( "(kicad_wks (version %d) (generator \"pl_editor\") (generator_version %s)",
218 m_out->Quotew( GetMajorMinorVersion() ).c_str() );
219
220 // Setup
221
222 // Write default values:
223 m_out->Print( "(setup" );
224 m_out->Print( "(textsize %s %s)",
225 FormatDouble2Str( aSheet->m_DefaultTextSize.x ).c_str(),
226 FormatDouble2Str( aSheet->m_DefaultTextSize.y ).c_str() );
227 m_out->Print( "(linewidth %s)", FormatDouble2Str( aSheet->m_DefaultLineWidth ).c_str() );
228 m_out->Print( "(textlinewidth %s)",
229 FormatDouble2Str( aSheet->m_DefaultTextThickness ).c_str() );
230
231 // Write margin values
232 m_out->Print( "(left_margin %s)", FormatDouble2Str( aSheet->GetLeftMargin() ).c_str() );
233 m_out->Print( "(right_margin %s)", FormatDouble2Str( aSheet->GetRightMargin() ).c_str() );
234 m_out->Print( "(top_margin %s)", FormatDouble2Str( aSheet->GetTopMargin() ).c_str() );
235 m_out->Print( "(bottom_margin %s)", FormatDouble2Str( aSheet->GetBottomMargin() ).c_str() );
236
237 m_out->Print( ")" );
238
239 // Save the graphical items on the drawing sheet
240 for( unsigned ii = 0; ii < aSheet->GetCount(); ii++ )
241 {
242 DS_DATA_ITEM* item = aSheet->GetItem( ii );
243 Format( aSheet, item );
244 }
245
246 m_out->Print( ")" );
247}
248
249
251{
252 m_out->Print( "(tbtext %s", m_out->Quotew( aItem->m_TextBase ).c_str() );
253 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
254
255 formatCoordinate( getTokenName( T_pos ), aItem->m_Pos );
256 formatOptions( aItem );
257
258 if( aItem->m_Orient )
259 m_out->Print( "(rotate %s)", FormatDouble2Str( aItem->m_Orient ).c_str() );
260
261 // Write font info, only if it is not the default setup
262 bool write_size = aItem->m_TextSize.x != 0.0 || aItem->m_TextSize.y != 0.0;
263 bool write_thickness = aItem->m_LineWidth != 0.0;
264 bool write_face = aItem->m_Font && !aItem->m_Font->GetName().IsEmpty();
265
266 if( write_thickness || write_size || aItem->m_Bold || aItem->m_Italic
267 || write_face || aItem->m_TextColor != COLOR4D::UNSPECIFIED )
268 {
269 m_out->Print( "(font" );
270
271 if( write_face )
272 m_out->Print( "(face %s)", m_out->Quotew( aItem->m_Font->NameAsToken() ).c_str() );
273
274 if( write_thickness )
275 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
276
277 if( write_size )
278 {
279 m_out->Print( "(size %s %s)",
280 FormatDouble2Str( aItem->m_TextSize.x ).c_str(),
281 FormatDouble2Str( aItem->m_TextSize.y ).c_str() );
282 }
283
284 if( aItem->m_Bold )
285 m_out->Print( " bold" );
286
287 if( aItem->m_Italic )
288 m_out->Print( " italic" );
289
290 if( aItem->m_TextColor != COLOR4D::UNSPECIFIED )
291 {
292 m_out->Print( "(color %d %d %d %s)",
293 KiROUND( aItem->m_TextColor.r * 255.0 ),
294 KiROUND( aItem->m_TextColor.g * 255.0 ),
295 KiROUND( aItem->m_TextColor.b * 255.0 ),
296 FormatDouble2Str( aItem->m_TextColor.a ).c_str() );
297 }
298
299 m_out->Print( ")" );
300 }
301
302 // Write text justification
304 {
305 m_out->Print( "(justify" );
306
307 // Write T_center opt first, because it is
308 // also a center for both m_Hjustify and m_Vjustify
309 if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_CENTER )
310 m_out->Print( " center" );
311 else if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_RIGHT )
312 m_out->Print( " right" );
313
314 if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_TOP )
315 m_out->Print( " top" );
316 else if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_BOTTOM )
317 m_out->Print( " bottom" );
318
319 m_out->Print( ")" );
320 }
321
322 // write constraints
323 if( aItem->m_BoundingBoxSize.x )
324 m_out->Print( "(maxlen %s)", FormatDouble2Str( aItem->m_BoundingBoxSize.x ).c_str() );
325
326 if( aItem->m_BoundingBoxSize.y )
327 m_out->Print( "(maxheight %s)", FormatDouble2Str(aItem->m_BoundingBoxSize.y ).c_str() );
328
329 formatRepeatParameters( aItem );
330
331 if( !aItem->m_Info.IsEmpty() )
332 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
333
334 m_out->Print( ")" );
335}
336
337
339{
340 if( aItem->GetType() == DS_DATA_ITEM::DS_RECT )
341 m_out->Print( "(rect" );
342 else
343 m_out->Print( "(line" );
344
345 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
346
347 formatCoordinate( getTokenName( T_start ), aItem->m_Pos );
348 formatCoordinate( getTokenName( T_end ), aItem->m_End );
349 formatOptions( aItem );
350
351 if( aItem->m_LineWidth && aItem->m_LineWidth != aModel->m_DefaultLineWidth )
352 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
353
354 formatRepeatParameters( aItem );
355
356 if( !aItem->m_Info.IsEmpty() )
357 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
358
359 m_out->Print( ")" );
360}
361
362
364{
365 m_out->Print( "(polygon" );
366 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
367 formatCoordinate( "pos", aItem->m_Pos );
368 formatOptions( aItem );
369
370 formatRepeatParameters( aItem );
371
372 if( !aItem->m_Orient.IsZero() )
373 m_out->Print( "(rotate %s)", FormatDouble2Str( aItem->m_Orient.AsDegrees() ).c_str() );
374
375 if( aItem->m_LineWidth )
376 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
377
378 if( !aItem->m_Info.IsEmpty() )
379 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
380
381 // Write polygon corners list
382 for( int kk = 0; kk < aItem->GetPolyCount(); kk++ )
383 {
384 m_out->Print( "(pts" );
385
386 // Create current polygon corners list
387 unsigned ist = aItem->GetPolyIndexStart( kk );
388 unsigned iend = aItem->GetPolyIndexEnd( kk );
389
390 while( ist <= iend )
391 {
392 VECTOR2D pos = aItem->m_Corners[ist++];
393 m_out->Print( "(xy %s %s)",
394 FormatDouble2Str( pos.x ).c_str(),
395 FormatDouble2Str( pos.y ).c_str() );
396 }
397
398 m_out->Print( ")" );
399 }
400
401 m_out->Print( ")" );
402}
403
404
406{
407 // Don't save empty images
408 if( !aItem->m_ImageBitmap->GetOriginalImageData() )
409 return;
410
411 m_out->Print( "(bitmap" );
412 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
413 formatCoordinate( "pos", aItem->m_Pos );
414 formatOptions( aItem );
415
416 m_out->Print( "(scale %s)", FormatDouble2Str( aItem->m_ImageBitmap->GetScale() ).c_str() );
417
418 formatRepeatParameters( aItem );
419
420 if( !aItem->m_Info.IsEmpty() )
421 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
422
423 // Write image in png readable format
424 wxMemoryOutputStream stream;
425 aItem->m_ImageBitmap->SaveImageData( stream );
426
427 KICAD_FORMAT::FormatStreamData( *m_out, *stream.GetOutputStreamBuffer() );
428
429 m_out->Print( ")" ); // Closes bitmap token.
430}
431
432
433void DS_DATA_MODEL_IO::formatCoordinate( const char * aToken, POINT_COORD & aCoord ) const
434{
435 m_out->Print( "(%s %s %s", aToken,
436 FormatDouble2Str( aCoord.m_Pos.x ).c_str(),
437 FormatDouble2Str( aCoord.m_Pos.y ).c_str() );
438
439 switch( aCoord.m_Anchor )
440 {
441 case RB_CORNER: break;
442 case LT_CORNER: m_out->Print( " ltcorner" ); break;
443 case LB_CORNER: m_out->Print( " lbcorner" ); break;
444 case RT_CORNER: m_out->Print( " rtcorner" ); break;
445 }
446
447 m_out->Print( ")" );
448}
449
450
452{
453 if( aItem->m_RepeatCount <= 1 )
454 return;
455
456 m_out->Print( "(repeat %d)", aItem->m_RepeatCount );
457
458 if( aItem->m_IncrementVector.x )
459 m_out->Print( "(incrx %s)", FormatDouble2Str( aItem->m_IncrementVector.x ).c_str() );
460
461 if( aItem->m_IncrementVector.y )
462 m_out->Print( "(incry %s)", FormatDouble2Str( aItem->m_IncrementVector.y ).c_str() );
463
464 if( aItem->m_IncrementLabel != 1 && aItem->GetType() == DS_DATA_ITEM::DS_TEXT )
465 m_out->Print( "(incrlabel %d)", aItem->m_IncrementLabel );
466}
467
468
470{
471 if( aItem->GetPage1Option() == FIRST_PAGE_ONLY )
472 m_out->Print( "(option page1only)" );
473 else if( aItem->GetPage1Option() == SUBSEQUENT_PAGES )
474 m_out->Print( "(option notonpage1)" );
475}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
const wxImage * GetOriginalImageData() const
Definition: bitmap_base.h:71
double GetScale() const
Definition: bitmap_base.h:73
bool SaveImageData(wxOutputStream &aOutStream) const
Write the bitmap data to aOutStream.
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
A helper class to write a drawing sheet to a file.
DS_DATA_MODEL_FILEIO(const wxString &aFilename)
PRETTIFIED_FILE_OUTPUTFORMATTER * m_fileout
A basic helper class to write a drawing sheet file.
void Format(DS_DATA_MODEL *aSheet) const
virtual ~DS_DATA_MODEL_IO()
void formatRepeatParameters(DS_DATA_ITEM *aItem) const
OUTPUTFORMATTER * m_out
void formatOptions(DS_DATA_ITEM *aItem) const
void format(DS_DATA_ITEM_TEXT *aItem) const
void formatCoordinate(const char *aToken, POINT_COORD &aCoord) const
A helper class to write a drawing sheet to a string.
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:113
bool IsZero() const
Definition: eda_angle.h:133
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
const char * NameAsToken() const
Definition: font.h:150
const wxString & GetName() const
Definition: font.h:149
double r
Red component.
Definition: color4d.h:392
double g
Green component.
Definition: color4d.h:393
double a
Alpha component.
Definition: color4d.h:395
double b
Blue component.
Definition: color4d.h:394
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:545
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:460
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:449
const std::string & GetString()
Definition: richio.h:472
@ 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)
void FormatStreamData(OUTPUTFORMATTER &aOut, const wxStreamBuffer &aStream)
Write binary data to the formatter as base 64 encoded string.
wxString From_UTF8(const char *cstring)
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