KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_kicad_sexpr_common.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <base_units.h>
21#include <macros.h>
22#include <schematic_lexer.h>
24#include <string_utils.h>
25
26
27using namespace TSCHEMATIC_T;
28
29
30static const char* emptyString = "";
31
32
33void formatFill( OUTPUTFORMATTER* aFormatter, FILL_T aFillMode, const COLOR4D& aFillColor )
34{
35 const char* fillType;
36
37 switch( aFillMode )
38 {
39 default:
40 case FILL_T::NO_FILL: fillType = "none"; break;
41 case FILL_T::FILLED_SHAPE: fillType = "outline"; break;
42 case FILL_T::FILLED_WITH_BG_BODYCOLOR: fillType = "background"; break;
43 case FILL_T::FILLED_WITH_COLOR: fillType = "color"; break;
44 case FILL_T::HATCH: fillType = "hatch"; break;
45 case FILL_T::REVERSE_HATCH: fillType = "reverse_hatch"; break;
46 case FILL_T::CROSS_HATCH: fillType = "cross_hatch"; break;
47 }
48
49 if( aFillMode == FILL_T::FILLED_WITH_COLOR )
50 {
51 aFormatter->Print( "(fill (type %s) (color %d %d %d %s))",
52 fillType,
53 KiROUND( aFillColor.r * 255.0 ),
54 KiROUND( aFillColor.g * 255.0 ),
55 KiROUND( aFillColor.b * 255.0 ),
56 FormatDouble2Str( aFillColor.a ).c_str() );
57 }
58 else
59 {
60 aFormatter->Print( "(fill (type %s))",
61 fillType );
62 }
63}
64
65
67{
68 switch( aType )
69 {
71 return SCHEMATIC_LEXER::TokenName( T_input );
72
74 return SCHEMATIC_LEXER::TokenName( T_output );
75
77 return SCHEMATIC_LEXER::TokenName( T_bidirectional );
78
80 return SCHEMATIC_LEXER::TokenName( T_tri_state );
81
83 return SCHEMATIC_LEXER::TokenName( T_passive );
84
86 return SCHEMATIC_LEXER::TokenName( T_free );
87
89 return SCHEMATIC_LEXER::TokenName( T_unspecified );
90
92 return SCHEMATIC_LEXER::TokenName( T_power_in );
93
95 return SCHEMATIC_LEXER::TokenName( T_power_out );
96
98 return SCHEMATIC_LEXER::TokenName( T_open_collector );
99
101 return SCHEMATIC_LEXER::TokenName( T_open_emitter );
102
104 return SCHEMATIC_LEXER::TokenName( T_no_connect );
105
106 default:
107 wxFAIL_MSG( "Missing symbol library pin connection type" );
108 }
109
110 return emptyString;
111}
112
113
115{
116 switch( aShape )
117 {
119 return SCHEMATIC_LEXER::TokenName( T_line );
120
122 return SCHEMATIC_LEXER::TokenName( T_inverted );
123
125 return SCHEMATIC_LEXER::TokenName( T_clock );
126
128 return SCHEMATIC_LEXER::TokenName( T_inverted_clock );
129
131 return SCHEMATIC_LEXER::TokenName( T_input_low );
132
134 return SCHEMATIC_LEXER::TokenName( T_clock_low );
135
137 return SCHEMATIC_LEXER::TokenName( T_output_low );
138
140 return SCHEMATIC_LEXER::TokenName( T_edge_clock_high );
141
143 return SCHEMATIC_LEXER::TokenName( T_non_logic );
144
145 default:
146 wxFAIL_MSG( "Missing symbol library pin shape type" );
147 }
148
149 return emptyString;
150}
151
152
154{
155 switch( aOrientation )
156 {
161 default: wxFAIL_MSG( "Missing symbol library pin orientation type" ); return ANGLE_0;
162 }
163}
164
165
167{
168 switch( aShape )
169 {
170 case LABEL_FLAG_SHAPE::L_INPUT: return SCHEMATIC_LEXER::TokenName( T_input );
171 case LABEL_FLAG_SHAPE::L_OUTPUT: return SCHEMATIC_LEXER::TokenName( T_output );
172 case LABEL_FLAG_SHAPE::L_BIDI: return SCHEMATIC_LEXER::TokenName( T_bidirectional );
173 case LABEL_FLAG_SHAPE::L_TRISTATE: return SCHEMATIC_LEXER::TokenName( T_tri_state );
174 case LABEL_FLAG_SHAPE::L_UNSPECIFIED: return SCHEMATIC_LEXER::TokenName( T_passive );
175 case LABEL_FLAG_SHAPE::F_DOT: return SCHEMATIC_LEXER::TokenName( T_dot );
176 case LABEL_FLAG_SHAPE::F_ROUND: return SCHEMATIC_LEXER::TokenName( T_round );
177 case LABEL_FLAG_SHAPE::F_DIAMOND: return SCHEMATIC_LEXER::TokenName( T_diamond );
178 case LABEL_FLAG_SHAPE::F_RECTANGLE: return SCHEMATIC_LEXER::TokenName( T_rectangle );
179 default: wxFAIL; return SCHEMATIC_LEXER::TokenName( T_passive );
180 }
181}
182
183
185{
186 switch( aSide )
187 {
189 case SHEET_SIDE::LEFT: return ANGLE_180;
190 case SHEET_SIDE::RIGHT: return ANGLE_0;
191 case SHEET_SIDE::TOP: return ANGLE_90;
192 case SHEET_SIDE::BOTTOM: return ANGLE_270;
193 default: wxFAIL; return ANGLE_0;
194 }
195}
196
197
198const char* getTextTypeToken( KICAD_T aType )
199{
200 switch( aType )
201 {
202 case SCH_TEXT_T: return SCHEMATIC_LEXER::TokenName( T_text );
203 case SCH_LABEL_T: return SCHEMATIC_LEXER::TokenName( T_label );
204 case SCH_GLOBAL_LABEL_T: return SCHEMATIC_LEXER::TokenName( T_global_label );
205 case SCH_HIER_LABEL_T: return SCHEMATIC_LEXER::TokenName( T_hierarchical_label );
206 case SCH_DIRECTIVE_LABEL_T: return SCHEMATIC_LEXER::TokenName( T_netclass_flag );
207 default: wxFAIL; return SCHEMATIC_LEXER::TokenName( T_text );
208 }
209}
210
211
212std::string formatIU( const int& aValue )
213{
215}
216
217
218std::string formatIU( const VECTOR2I& aPt, bool aInvertY )
219{
220 VECTOR2I pt( aPt.x, aInvertY ? -aPt.y : aPt.y );
222}
223
224
225void formatArc( OUTPUTFORMATTER* aFormatter, EDA_SHAPE* aArc, bool aIsPrivate,
226 const STROKE_PARAMS& aStroke, FILL_T aFillMode, const COLOR4D& aFillColor,
227 bool aInvertY, const KIID& aUuid )
228{
229 aFormatter->Print( "(arc %s (start %s) (mid %s) (end %s)",
230 aIsPrivate ? "private" : "",
231 formatIU( aArc->GetStart(), aInvertY ).c_str(),
232 formatIU( aArc->GetArcMid(), aInvertY ).c_str(),
233 formatIU( aArc->GetEnd(), aInvertY ).c_str() );
234
235 aStroke.Format( aFormatter, schIUScale );
236 formatFill( aFormatter, aFillMode, aFillColor );
237
238 if( aUuid != niluuid )
239 aFormatter->Print( "(uuid %s)", TO_UTF8( aUuid.AsString() ) );
240
241 aFormatter->Print( ")" );
242}
243
244
245void formatCircle( OUTPUTFORMATTER* aFormatter, EDA_SHAPE* aCircle, bool aIsPrivate,
246 const STROKE_PARAMS& aStroke, FILL_T aFillMode, const COLOR4D& aFillColor,
247 bool aInvertY, const KIID& aUuid )
248{
249 aFormatter->Print( "(circle %s (center %s) (radius %s)",
250 aIsPrivate ? "private" : "",
251 formatIU( aCircle->GetStart(), aInvertY ).c_str(),
252 formatIU( aCircle->GetRadius() ).c_str() );
253
254 aStroke.Format( aFormatter, schIUScale );
255 formatFill( aFormatter, aFillMode, aFillColor );
256
257 if( aUuid != niluuid )
258 aFormatter->Print( "(uuid %s)", TO_UTF8( aUuid.AsString() ) );
259
260 aFormatter->Print( ")" );
261}
262
263
264void formatRect( OUTPUTFORMATTER* aFormatter, EDA_SHAPE* aRect, bool aIsPrivate,
265 const STROKE_PARAMS& aStroke, FILL_T aFillMode, const COLOR4D& aFillColor,
266 bool aInvertY, const KIID& aUuid )
267{
268 aFormatter->Print( "(rectangle %s (start %s) (end %s)",
269 aIsPrivate ? "private" : "",
270 formatIU( aRect->GetStart(), aInvertY ).c_str(),
271 formatIU( aRect->GetEnd(), aInvertY ).c_str() );
272 aStroke.Format( aFormatter, schIUScale );
273 formatFill( aFormatter, aFillMode, aFillColor );
274
275 if( aUuid != niluuid )
276 aFormatter->Print( "(uuid %s)", TO_UTF8( aUuid.AsString() ) );
277
278 aFormatter->Print( ")" );
279}
280
281
282void formatBezier( OUTPUTFORMATTER* aFormatter, EDA_SHAPE* aBezier, bool aIsPrivate,
283 const STROKE_PARAMS& aStroke, FILL_T aFillMode, const COLOR4D& aFillColor,
284 bool aInvertY, const KIID& aUuid )
285{
286 aFormatter->Print( "(bezier %s (pts ",
287 aIsPrivate ? "private" : "" );
288
289 for( const VECTOR2I& pt : { aBezier->GetStart(), aBezier->GetBezierC1(),
290 aBezier->GetBezierC2(), aBezier->GetEnd() } )
291 {
292 aFormatter->Print( "(xy %s)", formatIU( pt, aInvertY ).c_str() );
293 }
294
295 aFormatter->Print( ")" ); // Closes pts token
296
297 aStroke.Format( aFormatter, schIUScale );
298 formatFill( aFormatter, aFillMode, aFillColor );
299
300 if( aUuid != niluuid )
301 aFormatter->Print( "(uuid %s)", TO_UTF8( aUuid.AsString() ) );
302
303 aFormatter->Print( ")" );
304}
305
306
307void formatPoly( OUTPUTFORMATTER* aFormatter, EDA_SHAPE* aPolyLine, bool aIsPrivate,
308 const STROKE_PARAMS& aStroke, FILL_T aFillMode, const COLOR4D& aFillColor,
309 bool aInvertY, const KIID& aUuid )
310{
311 aFormatter->Print( "(polyline %s (pts ",
312 aIsPrivate ? "private" : "" );
313
314 const SHAPE_POLY_SET& aPolySet = aPolyLine->GetPolyShape();
315
316 if( aPolySet.OutlineCount() == 0 )
317 {
318 // If we've managed to get a polyline with no points, that's probably a bad thing,
319 // but at least don't dereference it and crash.
320 wxFAIL_MSG( "Polyline has no outlines" );
321 }
322 else
323 {
324 for( const VECTOR2I& pt : aPolyLine->GetPolyShape().Outline( 0 ).CPoints() )
325 aFormatter->Print( "(xy %s)", formatIU( pt, aInvertY ).c_str() );
326 }
327
328 aFormatter->Print( ")" ); // Closes pts token
329
330 aStroke.Format( aFormatter, schIUScale );
331 formatFill( aFormatter, aFillMode, aFillColor );
332
333 if( aUuid != niluuid )
334 aFormatter->Print( "(uuid %s)", TO_UTF8( aUuid.AsString() ) );
335
336 aFormatter->Print( ")" );
337}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:110
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
const VECTOR2I & GetBezierC2() const
Definition: eda_shape.h:258
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:336
int GetRadius() const
Definition: eda_shape.cpp:961
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:215
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:173
const VECTOR2I & GetBezierC1() const
Definition: eda_shape.h:255
VECTOR2I GetArcMid() const
Definition: eda_shape.cpp:931
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
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
Definition: kiid.h:49
wxString AsString() const
Definition: kiid.cpp:246
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:460
const std::vector< VECTOR2I > & CPoints() const
Represent a set of closed polygons.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int OutlineCount() const
Return the number of outlines in the set.
Simple container to manage line stroke parameters.
Definition: stroke_params.h:94
void Format(OUTPUTFORMATTER *out, const EDA_IU_SCALE &aIuScale) const
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:401
static constexpr EDA_ANGLE ANGLE_90
Definition: eda_angle.h:403
static constexpr EDA_ANGLE ANGLE_270
Definition: eda_angle.h:406
static constexpr EDA_ANGLE ANGLE_180
Definition: eda_angle.h:405
FILL_T
Definition: eda_shape.h:56
@ FILLED_WITH_COLOR
@ REVERSE_HATCH
@ FILLED_WITH_BG_BODYCOLOR
@ FILLED_SHAPE
Fill with object color.
@ CROSS_HATCH
KIID niluuid(0)
This file contains miscellaneous commonly used macros and functions.
KICOMMON_API std::string FormatInternalUnits(const EDA_IU_SCALE &aIuScale, int aValue)
Converts aValue from internal units to a string appropriate for writing to file.
Definition: eda_units.cpp:170
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition: pin_type.h:36
@ PT_INPUT
usual pin input: must be connected
@ PT_NC
not connected (must be left open)
@ PT_OUTPUT
usual output
@ PT_TRISTATE
tri state bus pin
@ PT_NIC
not internally connected (may be connected to anything)
@ PT_BIDI
input or output (like port for a microprocessor)
@ PT_OPENEMITTER
pin type open emitter
@ PT_POWER_OUT
output of a regulator: intended to be connected to power input pins
@ PT_OPENCOLLECTOR
pin type open collector
@ PT_POWER_IN
power input (GND, VCC for ICs). Must be connected to a power output.
@ PT_UNSPECIFIED
unknown electrical properties: creates always a warning when connected
@ PT_PASSIVE
pin for passive symbols: must be connected, and can be connected to any pin.
PIN_ORIENTATION
The symbol library pin object orientations.
Definition: pin_type.h:80
@ PIN_UP
The pin extends upwards from the connection point: Probably on the bottom side of the symbol.
@ PIN_RIGHT
The pin extends rightwards from the connection point.
@ PIN_LEFT
The pin extends leftwards from the connection point: Probably on the right side of the symbol.
@ PIN_DOWN
The pin extends downwards from the connection: Probably on the top side of the symbol.
GRAPHIC_PINSHAPE
Definition: pin_type.h:59
EDA_ANGLE getPinAngle(PIN_ORIENTATION aOrientation)
void formatArc(OUTPUTFORMATTER *aFormatter, EDA_SHAPE *aArc, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, bool aInvertY, const KIID &aUuid)
const char * getSheetPinShapeToken(LABEL_FLAG_SHAPE aShape)
void formatCircle(OUTPUTFORMATTER *aFormatter, EDA_SHAPE *aCircle, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, bool aInvertY, const KIID &aUuid)
const char * getPinElectricalTypeToken(ELECTRICAL_PINTYPE aType)
const char * getTextTypeToken(KICAD_T aType)
void formatBezier(OUTPUTFORMATTER *aFormatter, EDA_SHAPE *aBezier, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, bool aInvertY, const KIID &aUuid)
void formatRect(OUTPUTFORMATTER *aFormatter, EDA_SHAPE *aRect, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, bool aInvertY, const KIID &aUuid)
std::string formatIU(const int &aValue)
void formatPoly(OUTPUTFORMATTER *aFormatter, EDA_SHAPE *aPolyLine, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, bool aInvertY, const KIID &aUuid)
EDA_ANGLE getSheetPinAngle(SHEET_SIDE aSide)
void formatFill(OUTPUTFORMATTER *aFormatter, FILL_T aFillMode, const COLOR4D &aFillColor)
Fill token formatting helper.
const char * getPinShapeToken(GRAPHIC_PINSHAPE aShape)
static const char * emptyString
LABEL_FLAG_SHAPE
Definition: sch_label.h:99
@ L_BIDI
Definition: sch_label.h:102
@ L_TRISTATE
Definition: sch_label.h:103
@ L_UNSPECIFIED
Definition: sch_label.h:104
@ F_DOT
Definition: sch_label.h:107
@ F_ROUND
Definition: sch_label.h:108
@ L_OUTPUT
Definition: sch_label.h:101
@ F_DIAMOND
Definition: sch_label.h:109
@ L_INPUT
Definition: sch_label.h:100
@ F_RECTANGLE
Definition: sch_label.h:110
SHEET_SIDE
Define the edge of the sheet that the sheet pin is positioned.
Definition: sch_sheet_pin.h:46
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:403
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ SCH_DIRECTIVE_LABEL_T
Definition: typeinfo.h:171
@ SCH_LABEL_T
Definition: typeinfo.h:167
@ SCH_HIER_LABEL_T
Definition: typeinfo.h:169
@ SCH_TEXT_T
Definition: typeinfo.h:151
@ SCH_GLOBAL_LABEL_T
Definition: typeinfo.h:168