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>
#include <widgets/progress_reporter.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 70 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 80 of file sch_sexpr_plugin.cpp.

81 {
82  wxCHECK_RET( aItem && aItem->IsFillable(), "Invalid fill item." );
83 
84  const char* fillType;
85 
86  switch( aItem->GetFillMode() )
87  {
88  case FILL_TYPE::FILLED_SHAPE: fillType = "outline"; break;
89  case FILL_TYPE::FILLED_WITH_BG_BODYCOLOR: fillType = "background"; break;
91  default: fillType = "none";
92  }
93 
94  aFormatter.Print( aNestLevel, "(fill (type %s))", fillType );
95 }
#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:426

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 272 of file sch_sexpr_plugin.cpp.

274 {
275  wxASSERT( aFormatter != nullptr );
276 
277  aFormatter->Print( aNestLevel, "(stroke (width %s) (type %s) (color %d %d %d %s))",
278  FormatInternalUnits( aStroke.GetWidth() ).c_str(),
279  TO_UTF8( getLineStyleToken( aStroke.GetPlotStyle() ) ),
280  KiROUND( aStroke.GetColor().r * 255.0 ),
281  KiROUND( aStroke.GetColor().g * 255.0 ),
282  KiROUND( aStroke.GetColor().b * 255.0 ),
283  Double2Str( aStroke.GetColor().a ).c_str() );
284 }
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:73
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:426
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:485
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:955

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 230 of file sch_sexpr_plugin.cpp.

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

References DASH, DASHDOT, DOT, and SOLID.

Referenced by formatStroke().

◆ getPinAngle()

static float getPinAngle ( int  aOrientation)
static

Definition at line 185 of file sch_sexpr_plugin.cpp.

186 {
187  switch( aOrientation )
188  {
189  case PIN_RIGHT: return 0.0;
190  case PIN_LEFT: return 180.0;
191  case PIN_UP: return 90.0;
192  case PIN_DOWN: return 270.0;
193  default: wxFAIL_MSG( "Missing symbol library pin orientation type" ); return 0.0;
194  }
195 }
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 98 of file sch_sexpr_plugin.cpp.

99 {
100  switch( aType )
101  {
103  return SCHEMATIC_LEXER::TokenName( T_input );
104 
106  return SCHEMATIC_LEXER::TokenName( T_output );
107 
109  return SCHEMATIC_LEXER::TokenName( T_bidirectional );
110 
112  return SCHEMATIC_LEXER::TokenName( T_tri_state );
113 
115  return SCHEMATIC_LEXER::TokenName( T_passive );
116 
118  return SCHEMATIC_LEXER::TokenName( T_free );
119 
121  return SCHEMATIC_LEXER::TokenName( T_unspecified );
122 
124  return SCHEMATIC_LEXER::TokenName( T_power_in );
125 
127  return SCHEMATIC_LEXER::TokenName( T_power_out );
128 
130  return SCHEMATIC_LEXER::TokenName( T_open_collector );
131 
133  return SCHEMATIC_LEXER::TokenName( T_open_emitter );
134 
136  return SCHEMATIC_LEXER::TokenName( T_no_connect );
137 
138  default:
139  wxFAIL_MSG( "Missing symbol library pin connection type" );
140  }
141 
142  return emptyString;
143 }
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 146 of file sch_sexpr_plugin.cpp.

147 {
148  switch( aShape )
149  {
151  return SCHEMATIC_LEXER::TokenName( T_line );
152 
154  return SCHEMATIC_LEXER::TokenName( T_inverted );
155 
157  return SCHEMATIC_LEXER::TokenName( T_clock );
158 
160  return SCHEMATIC_LEXER::TokenName( T_inverted_clock );
161 
163  return SCHEMATIC_LEXER::TokenName( T_input_low );
164 
166  return SCHEMATIC_LEXER::TokenName( T_clock_low );
167 
169  return SCHEMATIC_LEXER::TokenName( T_output_low );
170 
172  return SCHEMATIC_LEXER::TokenName( T_edge_clock_high );
173 
175  return SCHEMATIC_LEXER::TokenName( T_non_logic );
176 
177  default:
178  wxFAIL_MSG( "Missing symbol library pin shape type" );
179  }
180 
181  return emptyString;
182 }
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 212 of file sch_sexpr_plugin.cpp.

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

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 198 of file sch_sexpr_plugin.cpp.

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

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 247 of file sch_sexpr_plugin.cpp.

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

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