KiCad PCB EDA Suite
sch_sexpr_plugin.cpp File Reference
#include <algorithm>
#include <wx/base64.h>
#include <wx/log.h>
#include <wx/mstream.h>
#include <advanced_config.h>
#include <trace_helpers.h>
#include <locale_io.h>
#include <sch_bitmap.h>
#include <sch_bus_entry.h>
#include <sch_symbol.h>
#include <sch_edit_frame.h>
#include <sch_junction.h>
#include <sch_line.h>
#include <sch_no_connect.h>
#include <sch_text.h>
#include <sch_sheet.h>
#include <sch_sheet_pin.h>
#include <schematic.h>
#include <sch_plugins/kicad/sch_sexpr_plugin.h>
#include <sch_screen.h>
#include <symbol_library.h>
#include <lib_arc.h>
#include <lib_bezier.h>
#include <lib_circle.h>
#include <lib_field.h>
#include <lib_pin.h>
#include <lib_polyline.h>
#include <lib_rectangle.h>
#include <lib_text.h>
#include <eeschema_id.h>
#include <sch_file_versions.h>
#include <schematic_lexer.h>
#include <sch_plugins/kicad/sch_sexpr_parser.h>
#include <symbol_lib_table.h>
#include <ee_selection.h>
#include <kicad_string.h>
#include <wx_filename.h>

Go to the source code of this file.

Classes

class  SCH_SEXPR_PLUGIN_CACHE
 A cache assistant for the symbol library portion of the SCH_PLUGIN API, and only for the SCH_SEXPR_PLUGIN, so therefore is private to this implementation file, i.e. More...
 

Macros

#define wxUSE_BASE64   1
 
#define SCH_PARSE_ERROR(text, reader, pos)
 
#define MIME_BASE64_LENGTH   76
 

Functions

static void formatFill (const LIB_ITEM *aItem, OUTPUTFORMATTER &aFormatter, int aNestLevel)
 Fill token formatting helper. More...
 
static const char * getPinElectricalTypeToken (ELECTRICAL_PINTYPE aType)
 
static const char * getPinShapeToken (GRAPHIC_PINSHAPE aShape)
 
static float getPinAngle (int aOrientation)
 
static const char * getSheetPinShapeToken (PINSHEETLABEL_SHAPE aShape)
 
static double getSheetPinAngle (SHEET_SIDE aSide)
 
static wxString getLineStyleToken (PLOT_DASH_TYPE aStyle)
 
static const char * getTextTypeToken (KICAD_T aType)
 
static void formatStroke (OUTPUTFORMATTER *aFormatter, int aNestLevel, const STROKE_PARAMS &aStroke)
 Write stroke definition to aFormatter. More...
 

Variables

static const char * emptyString = ""
 

Macro Definition Documentation

◆ MIME_BASE64_LENGTH

#define MIME_BASE64_LENGTH   76

◆ SCH_PARSE_ERROR

#define SCH_PARSE_ERROR (   text,
  reader,
  pos 
)
Value:
THROW_PARSE_ERROR( text, reader.GetSource(), reader.Line(), \
reader.LineNumber(), pos - reader.Line() )
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:164

Definition at line 69 of file sch_sexpr_plugin.cpp.

◆ wxUSE_BASE64

#define wxUSE_BASE64   1

Definition at line 27 of file sch_sexpr_plugin.cpp.

Function Documentation

◆ formatFill()

static void formatFill ( const LIB_ITEM aItem,
OUTPUTFORMATTER aFormatter,
int  aNestLevel 
)
static

Fill token formatting helper.

Definition at line 79 of file sch_sexpr_plugin.cpp.

80 {
81  wxCHECK_RET( aItem && aItem->IsFillable(), "Invalid fill item." );
82 
83  const char* fillType;
84 
85  switch( aItem->GetFillMode() )
86  {
87  case FILL_TYPE::FILLED_SHAPE: fillType = "outline"; break;
88  case FILL_TYPE::FILLED_WITH_BG_BODYCOLOR: fillType = "background"; break;
90  default: fillType = "none";
91  }
92 
93  aFormatter.Print( aNestLevel, "(fill (type %s))", fillType );
94 }
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
FILL_TYPE GetFillMode() const
Definition: lib_item.h:265
bool IsFillable() const
Check if draw object can be filled.
Definition: lib_item.h:256
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:408

References FILLED_SHAPE, FILLED_WITH_BG_BODYCOLOR, LIB_ITEM::GetFillMode(), LIB_ITEM::IsFillable(), KI_FALLTHROUGH, NO_FILL, and OUTPUTFORMATTER::Print().

Referenced by SCH_SEXPR_PLUGIN_CACHE::saveArc(), SCH_SEXPR_PLUGIN_CACHE::saveBezier(), SCH_SEXPR_PLUGIN_CACHE::saveCircle(), SCH_SEXPR_PLUGIN_CACHE::savePolyLine(), and SCH_SEXPR_PLUGIN_CACHE::saveRectangle().

◆ formatStroke()

static void formatStroke ( OUTPUTFORMATTER aFormatter,
int  aNestLevel,
const STROKE_PARAMS aStroke 
)
static

Write stroke definition to aFormatter.

This only writes the stroke definition if aWidth, aStyle and aColor are not the default setting or are not defined.

Parameters
aFormatterA pointer to the OUTPUTFORMATTER object to write to.
aNestLevelThe nest level to indent the stroke definition.
aWidthThe stroke line width in internal units.
aStyleThe stroke line style.
aColorThe stroke line color.

Definition at line 271 of file sch_sexpr_plugin.cpp.

273 {
274  wxASSERT( aFormatter != nullptr );
275 
276  aFormatter->Print( aNestLevel, "(stroke (width %s) (type %s) (color %d %d %d %s))",
277  FormatInternalUnits( aStroke.GetWidth() ).c_str(),
278  TO_UTF8( getLineStyleToken( aStroke.GetPlotStyle() ) ),
279  KiROUND( aStroke.GetColor().r * 255.0 ),
280  KiROUND( aStroke.GetColor().g * 255.0 ),
281  KiROUND( aStroke.GetColor().b * 255.0 ),
282  Double2Str( aStroke.GetColor().a ).c_str() );
283 }
double g
Green component.
Definition: color4d.h:364
double b
Blue component.
Definition: color4d.h:365
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
double a
Alpha component.
Definition: color4d.h:366
static wxString getLineStyleToken(PLOT_DASH_TYPE aStyle)
PLOT_DASH_TYPE GetPlotStyle() const
Definition: sch_item.h:170
int GetWidth() const
Definition: sch_item.h:167
COLOR4D GetColor() const
Definition: sch_item.h:173
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:70
double r
Red component.
Definition: color4d.h:363
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:408
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:484
std::string Double2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 We want to avoid scientific ...
Definition: string.cpp:940

References KIGFX::COLOR4D::a, KIGFX::COLOR4D::b, Double2Str(), FormatInternalUnits(), KIGFX::COLOR4D::g, STROKE_PARAMS::GetColor(), getLineStyleToken(), STROKE_PARAMS::GetPlotStyle(), STROKE_PARAMS::GetWidth(), KiROUND(), OUTPUTFORMATTER::Print(), KIGFX::COLOR4D::r, and TO_UTF8.

Referenced by SCH_SEXPR_PLUGIN::saveBusEntry(), SCH_SEXPR_PLUGIN::saveLine(), and SCH_SEXPR_PLUGIN::saveSheet().

◆ getLineStyleToken()

static wxString getLineStyleToken ( PLOT_DASH_TYPE  aStyle)
static

Definition at line 229 of file sch_sexpr_plugin.cpp.

230 {
231  wxString token;
232 
233  switch( aStyle )
234  {
235  case PLOT_DASH_TYPE::DASH: token = "dash"; break;
236  case PLOT_DASH_TYPE::DOT: token = "dot"; break;
237  case PLOT_DASH_TYPE::DASHDOT: token = "dash_dot"; break;
239  default: token = "solid"; break;
240  }
241 
242  return token;
243 }

References DASH, DASHDOT, DOT, and SOLID.

Referenced by formatStroke().

◆ getPinAngle()

static float getPinAngle ( int  aOrientation)
static

Definition at line 184 of file sch_sexpr_plugin.cpp.

185 {
186  switch( aOrientation )
187  {
188  case PIN_RIGHT: return 0.0;
189  case PIN_LEFT: return 180.0;
190  case PIN_UP: return 90.0;
191  case PIN_DOWN: return 270.0;
192  default: wxFAIL_MSG( "Missing symbol library pin orientation type" ); return 0.0;
193  }
194 }
Definition: lib_pin.h:48

References PIN_DOWN, PIN_LEFT, PIN_RIGHT, and PIN_UP.

Referenced by SCH_SEXPR_PLUGIN_CACHE::savePin().

◆ getPinElectricalTypeToken()

static const char* getPinElectricalTypeToken ( ELECTRICAL_PINTYPE  aType)
static

Definition at line 97 of file sch_sexpr_plugin.cpp.

98 {
99  switch( aType )
100  {
102  return SCHEMATIC_LEXER::TokenName( T_input );
103 
105  return SCHEMATIC_LEXER::TokenName( T_output );
106 
108  return SCHEMATIC_LEXER::TokenName( T_bidirectional );
109 
111  return SCHEMATIC_LEXER::TokenName( T_tri_state );
112 
114  return SCHEMATIC_LEXER::TokenName( T_passive );
115 
117  return SCHEMATIC_LEXER::TokenName( T_free );
118 
120  return SCHEMATIC_LEXER::TokenName( T_unspecified );
121 
123  return SCHEMATIC_LEXER::TokenName( T_power_in );
124 
126  return SCHEMATIC_LEXER::TokenName( T_power_out );
127 
129  return SCHEMATIC_LEXER::TokenName( T_open_collector );
130 
132  return SCHEMATIC_LEXER::TokenName( T_open_emitter );
133 
135  return SCHEMATIC_LEXER::TokenName( T_no_connect );
136 
137  default:
138  wxFAIL_MSG( "Missing symbol library pin connection type" );
139  }
140 
141  return emptyString;
142 }
power input (GND, VCC for ICs). Must be connected to a power output.
pin for passive symbols: must be connected, and can be connected to any pin
unknown electrical properties: creates always a warning when connected
static const char * emptyString
not internally connected (may be connected to anything)
usual pin input: must be connected
input or output (like port for a microprocessor)
not connected (must be left open)
output of a regulator: intended to be connected to power input pins

References emptyString, PT_BIDI, PT_INPUT, PT_NC, PT_NIC, PT_OPENCOLLECTOR, PT_OPENEMITTER, PT_OUTPUT, PT_PASSIVE, PT_POWER_IN, PT_POWER_OUT, PT_TRISTATE, and PT_UNSPECIFIED.

Referenced by SCH_SEXPR_PLUGIN_CACHE::savePin().

◆ getPinShapeToken()

static const char* getPinShapeToken ( GRAPHIC_PINSHAPE  aShape)
static

Definition at line 145 of file sch_sexpr_plugin.cpp.

146 {
147  switch( aShape )
148  {
150  return SCHEMATIC_LEXER::TokenName( T_line );
151 
153  return SCHEMATIC_LEXER::TokenName( T_inverted );
154 
156  return SCHEMATIC_LEXER::TokenName( T_clock );
157 
159  return SCHEMATIC_LEXER::TokenName( T_inverted_clock );
160 
162  return SCHEMATIC_LEXER::TokenName( T_input_low );
163 
165  return SCHEMATIC_LEXER::TokenName( T_clock_low );
166 
168  return SCHEMATIC_LEXER::TokenName( T_output_low );
169 
171  return SCHEMATIC_LEXER::TokenName( T_edge_clock_high );
172 
174  return SCHEMATIC_LEXER::TokenName( T_non_logic );
175 
176  default:
177  wxFAIL_MSG( "Missing symbol library pin shape type" );
178  }
179 
180  return emptyString;
181 }
static const char * emptyString

References CLOCK, CLOCK_LOW, emptyString, FALLING_EDGE_CLOCK, INPUT_LOW, INVERTED, INVERTED_CLOCK, LINE, NONLOGIC, and OUTPUT_LOW.

Referenced by SCH_SEXPR_PLUGIN_CACHE::savePin().

◆ getSheetPinAngle()

static double getSheetPinAngle ( SHEET_SIDE  aSide)
static

Definition at line 211 of file sch_sexpr_plugin.cpp.

212 {
213  double retv;
214 
215  switch( aSide )
216  {
218  case SHEET_SIDE::LEFT: retv = 180.0; break;
219  case SHEET_SIDE::RIGHT: retv = 0.0; break;
220  case SHEET_SIDE::TOP: retv = 90.0; break;
221  case SHEET_SIDE::BOTTOM: retv = 270.0; break;
222  default: wxFAIL; retv = 0.0; break;
223  }
224 
225  return retv;
226 }

References BOTTOM, LEFT, RIGHT, TOP, and UNDEFINED.

Referenced by SCH_SEXPR_PLUGIN::saveSheet().

◆ getSheetPinShapeToken()

static const char* getSheetPinShapeToken ( PINSHEETLABEL_SHAPE  aShape)
static

Definition at line 197 of file sch_sexpr_plugin.cpp.

198 {
199  switch( aShape )
200  {
201  case PINSHEETLABEL_SHAPE::PS_INPUT: return SCHEMATIC_LEXER::TokenName( T_input );
202  case PINSHEETLABEL_SHAPE::PS_OUTPUT: return SCHEMATIC_LEXER::TokenName( T_output );
203  case PINSHEETLABEL_SHAPE::PS_BIDI: return SCHEMATIC_LEXER::TokenName( T_bidirectional );
204  case PINSHEETLABEL_SHAPE::PS_TRISTATE: return SCHEMATIC_LEXER::TokenName( T_tri_state );
205  case PINSHEETLABEL_SHAPE::PS_UNSPECIFIED: return SCHEMATIC_LEXER::TokenName( T_passive );
206  default: wxFAIL; return SCHEMATIC_LEXER::TokenName( T_passive );
207  }
208 }

References PS_BIDI, PS_INPUT, PS_OUTPUT, PS_TRISTATE, and PS_UNSPECIFIED.

Referenced by SCH_SEXPR_PLUGIN::saveSheet(), and SCH_SEXPR_PLUGIN::saveText().

◆ getTextTypeToken()

static const char* getTextTypeToken ( KICAD_T  aType)
static

Definition at line 246 of file sch_sexpr_plugin.cpp.

247 {
248  switch( aType )
249  {
250  case SCH_TEXT_T: return SCHEMATIC_LEXER::TokenName( T_text );
251  case SCH_LABEL_T: return SCHEMATIC_LEXER::TokenName( T_label );
252  case SCH_GLOBAL_LABEL_T: return SCHEMATIC_LEXER::TokenName( T_global_label );
253  case SCH_HIER_LABEL_T: return SCHEMATIC_LEXER::TokenName( T_hierarchical_label );
254  default: wxFAIL; return SCHEMATIC_LEXER::TokenName( T_text );
255  }
256 }

References SCH_GLOBAL_LABEL_T, SCH_HIER_LABEL_T, SCH_LABEL_T, and SCH_TEXT_T.

Referenced by SCH_SEXPR_PLUGIN::saveText().

Variable Documentation

◆ emptyString

const char* emptyString = ""
static