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