summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/tikz-ladder/tex/tikzlibrarycircuits.plc.ladder.code.tex
blob: 86ce172402273293f728bc72f064b7fa7fd58921 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
%% This is file `tikzlibrarycircuits.plc.ladder.code.tex'
%% Version: 1.3
%% Version date: 2022-04-10
%% 
%% 
%% Copyright (C) 2018 by Luis Paulo Laus, laus@utfpr.edu.br
%%
%% This package can be redistributed and/or modified under the terms
%% of the LaTeX Project Public License distributed from CTAN
%% archives in directory macros/latex/base/lppl.txt; either
%% version 1 of the License, or (at your option) any later version,
%% with `The Package' referring to the software
%% `tikzlibrarycircuits.plc.ladder.code.tex'
%% and its accompanying documentation and `The Copyright Holder'
%% referring to the person Luis Paulo Laus.
%% 
%% 
%% IMPORTANT NOTICE: 
%% 
%% For error reports, comments or suggestions in case of UNCHANGED 
%% versions send mail to:
%% laus@utfpr.edu.br
%% 
%%
%% Change History:
%% 1.0: 10 January 2018:  Original Version
%% 1.1: 06 February 2018: Macro file renamed to proper TikZ library file name.
%%                        Parent library changed from `circuit.ee' to `circuits'.
%%                        Bug fixes: added \pgfusepathqstroke where it was missing,
%%                        block anchors reworked.
%% 1.2: 05 December 2020: Bugfix: corrected block height when the symbol is empty ({}).
%% 1.3: 10 April 2022: new macro for typesetting power rails, fix the annoying bug that prints
%%                       `Missing character....'

\usetikzlibrary{circuits}
%
% Length and macros
%
\makeatletter
\newlength{\ladderskip}
\def\ladderrungsep@{0.2}
\def\ladderrungend#1{\pgftransformyshift{-#1\ladderskip-\ladderrungsep@\ladderskip}}% mark the end of a rung, #1 is the number of rows
\def\ladderpowerrails{{% draws power rails
  \draw let \p1=(laddertopright) in%
    (0,\y1+0.7\ladderskip) -- (0,0.8\ladderskip)%
    (\x1,\y1+0.7\ladderskip) -- (\x1,0.8\ladderskip);}}

%
% Setting up a ladder
%
\tikzset{
  ladderrungsep/.store in=\ladderrungsep@,
  circuit plc ladder/.style={
    circuit,
    inputs/.style={block ladder inputs={##1}},
    outputs/.style={block ladder outputs={##1}},
    input sep/.style={block ladder input sep={##1}},
    output sep/.style={block ladder output sep={##1}},
    clksize/.style={block ladder clksize={##1}},
    symbol/.style={ladder symbol={##1}},
    symbol color/.style={ladder symbol color={##1}},
    x=5\tikzcircuitssizeunit,
    y=5\tikzcircuitssizeunit,
    execute at begin picture={\ladderskip=\pgf@yy\coordinate (laddertopright) at (0,0);}, % sets \ladderskip and defines a point for power rails
    every circuit plc ladder/.try
  }
}       

\newif\ifpgfnormallyclosednegated%
\pgfkeys{
  /pgf/crossed ladder/.is if=pgfnormallyclosednegated,%
}

\pgfkeys{/pgf/.cd,
  contact ladder symbol/.initial=, % no value
  contact NC ladder symbol/.initial=, % no value
  var contact NC ladder symbol/.initial=$/$,
  contact N ladder symbol/.initial=N,
  contact P ladder symbol/.initial=P,
  coil ladder symbol/.initial=, % no value
  coil NA ladder symbol/.initial=, % no value
  var coil NA ladder symbol/.initial=$/$,
  coil S ladder symbol/.initial=S,
  coil R ladder symbol/.initial=R,
  coil ladder curvature/.initial=0.2, % parameter for ( and ) in coils
  ladder symbol color/.initial=, % no value
  ladder symbol/.initial=,
  block ladder input sep/.initial=1.6\tikzcircuitssizeunit,
  block ladder output sep/.initial=1.6\tikzcircuitssizeunit,
  block ladder inputs/.initial={IN},
  block ladder outputs/.initial={Q},
  block ladder clksize/.initial=0.8\tikzcircuitssizeunit,
  block ladder symbol/.initial=TON, % blocks like TON, TOF, TP, ADD, CDU, CDUD\_*
}

% Internal macro for parsing inputs, outputs and clock inputs (no short version).
%
\expandafter\ifx\csname pgf@lib@sh@ladder@parseinout\endcsname\relax%
\def\pgf@lib@sh@ladder@parseinout#1{%
	\edef\pgf@lib@sh@temp{\pgfkeysvalueof{/pgf/block ladder \pgf@lib@sh@ladder@inout}}%
	\c@pgf@counta#1\relax%
	\c@pgf@countb0\relax%
	\expandafter\pgfutil@in@\expandafter,\expandafter{\pgf@lib@sh@temp}%
	\let\pgf@lib@sh@next\pgf@lib@sh@ladder@parseinout@long%
	\pgf@lib@sh@next%
}	
\def\pgf@lib@sh@ladder@parseinout@long{%
	\expandafter\pgf@lib@sh@ladder@parseinout@@long\pgf@lib@sh@temp,\pgf@stop,%
}
\def\pgf@lib@sh@ladder@parseinout@@long#1,{%
	\ifx#1\pgf@stop%
		\expandafter\edef\csname pgf@lib@sh@ladder@num\pgf@lib@sh@ladder@inout\endcsname{\the\c@pgf@countb}%
		\let\pgf@lib@sh@next\relax%
	\else%
		\ifnum\c@pgf@countb=\c@pgf@counta%
			\expandafter\edef\csname pgf@lib@sh@ladder@num\pgf@lib@sh@ladder@inout\endcsname{\the\c@pgf@countb}%
			\let\pgf@lib@sh@next\relax%
		\else%
			\advance\c@pgf@countb1\relax%
			\def\pgf@lib@sh@temp{#1}%
			\expandafter\pgf@sh@resavedmacro\expandafter{\csname \pgf@lib@sh@ladder@inout-\the\c@pgf@countb\endcsname}{%
				\expandafter\def\csname \pgf@lib@sh@ladder@inout-\the\c@pgf@countb\endcsname{\expandafter\pgf@lib@sh@temp}}%
			\let\pgf@lib@sh@next\pgf@lib@sh@ladder@parseinout@@@long%
		\fi%
	\fi%
	\pgf@lib@sh@next%
}
\def\pgf@lib@sh@ladder@parseinout@@@long{%
	\pgfutil@ifnextchar x{\pgf@lib@sh@ladder@parseinout@@long}%
		{\pgf@lib@sh@ladder@parseinout@@long}%
}
\fi%


% Adjust the width and height of the ladder block  symbol.
\def\pgf@lib@sh@ladder@dimensions{
  \pgf@x.5\wd\pgfnodeparttextbox
  \pgf@y.5\ht\pgfnodeparttextbox
  \advance\pgf@y.5\dp\pgfnodeparttextbox

  %
  % Adjust the height for the number of inputs and the width and height of the ladder block symbol.
  %
  \c@pgf@counta\pgf@lib@sh@ladder@numinputs
  \pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/block ladder input sep}}
  \edef\inputsep{\the\pgf@yb}
  \addtosavedmacro\inputsep
  \advance\c@pgf@counta-1\relax
  \multiply\pgf@yb\c@pgf@counta
   \c@pgf@counta\pgf@lib@sh@ladder@numoutputs
  \pgfmathsetlength\pgf@yc{\pgfkeysvalueof{/pgf/block ladder output sep}}
  \edef\outputsep{\the\pgf@yc}
  \addtosavedmacro\outputsep
  \advance\c@pgf@counta-1\relax
  \multiply\pgf@yc\c@pgf@counta
  \ifdim\pgf@yb<\pgf@yc% the highest between inputs and outputs
    \pgf@yb\pgf@yc
  \fi
  % now the symbol width and height
  \begingroup% keep font setting local
    \pgfutil@selectfont% restore font
    \global\setbox\pgf@hbox\hbox{{\pgfkeysvalueof{/pgf/ladder symbol}}}% add braces as color-aftergroup-fix TT
  \endgroup%
% \typeout{Test \the\wd\pgf@hbox  \the\ht\pgf@hbox  \the\dp\pgf@hbox}
  \pgf@xc.5\wd\pgf@hbox
  \pgf@yc\ht\pgf@hbox
  \advance\pgf@yc\dp\pgf@hbox
  \ifdim\pgf@yc=0pt
    \advance\pgf@yb4pt
  \fi
  \advance\pgf@yc1pt
  \advance\pgf@yb0.5\pgf@yc
  \advance\pgf@yc2pt
  \edef\symbolypos{\the\pgf@yc}
  \addtosavedmacro\symbolypos
  \advance\pgf@yc0.5\pgf@yc
  \advance\pgf@yc\pgf@yb
  \pgf@yc.5\pgf@yc
  \ifdim\pgf@x<\pgf@xc
    \pgf@x\pgf@xc
  \fi
  \ifdim\pgf@y<\pgf@yc
    \pgf@y\pgf@yc
  \fi
  \pgfmathaddtolength\pgf@x{\pgfkeysvalueof{/pgf/inner xsep}}
  \pgfmathaddtolength\pgf@y{\pgfkeysvalueof{/pgf/inner ysep}}

  %
  % Adjust for minimum height and width.
  %
  \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/minimum width}}%
  \pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/minimum height}}%
  \ifdim\pgf@x<.5\pgf@xa%
    \pgf@x.5\pgf@xa%
  \fi%
  \ifdim\pgf@y<.5\pgf@ya%
    \pgf@y.5\pgf@ya%
  \fi%
  %
  % Take into account the outer sep.
  %
  \pgfmathaddtolength\pgf@x{\pgfkeysvalueof{/pgf/outer xsep}}%
  \pgfmathaddtolength\pgf@y{\pgfkeysvalueof{/pgf/outer ysep}}%
  %
  \edef\halfwidth{\the\pgf@x}%
  \edef\halfheight{\the\pgf@y}%
  \addtosavedmacro\halfwidth%
  \addtosavedmacro\halfheight%

  \advance\pgf@yb\pgf@y
  \advance\pgf@yb-\pgf@yc
  \edef\boxdepth{\the\pgf@yb}% below 1st input
  \addtosavedmacro\boxdepth
}%


\def\pgf@lib@sh@ladder@inputanchor#1{%
  \dimensions%
  \pgfpointorigin%
  \advance\pgf@x-\halfwidth%
  %
  \pgfutil@tempdima\inputsep%
  \multiply\pgfutil@tempdima#1\relax%
  \advance\pgf@y-\pgfutil@tempdima%
  \advance\pgf@y\inputsep%
}

\def\pgf@lib@sh@ladder@outputanchor#1{%
  \dimensions%
  \pgfpointorigin%
  \advance\pgf@x\halfwidth%
  %
  \pgfutil@tempdima\outputsep%
  \multiply\pgfutil@tempdima#1\relax%
  \advance\pgf@y-\pgfutil@tempdima%
  \advance\pgf@y\outputsep%
}

\def\pgfutil@first@until@relax#1#2\relax{#1}

% Shapes
% block
\pgfdeclareshape{block ladder}{
	\expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@block ladder\endcsname{%
	     \def\pgf@lib@sh@ladder@inout{inputs}
		\pgf@lib@sh@ladder@parseinout{1024}% Maximum 1024 (!) inputs.
		%
		\pgfmathloop%
			\ifnum\pgfmathcounter>\pgf@lib@sh@ladder@numinputs%
			\else%
				\pgfutil@ifundefined{pgf@anchor@block ladder@input \pgfmathcounter}{%
					\expandafter\xdef\csname pgf@anchor@block ladder@input \pgfmathcounter\endcsname{%
						\noexpand\pgf@lib@sh@ladder@inputanchor{\pgfmathcounter}%
					}%
				}{}%
		\repeatpgfmathloop%
		\ifnum\pgf@lib@sh@ladder@numinputs<1\relax%
			\pgferror{A block must have at least one input}%
		\fi%
		%
	     \def\pgf@lib@sh@ladder@inout{outputs}
		\pgf@lib@sh@ladder@parseinout{1024}% Maximum 1024 (!) outputs.
		%
		\pgfmathloop%
			\ifnum\pgfmathcounter>\pgf@lib@sh@ladder@numoutputs%
			\else%
				\pgfutil@ifundefined{pgf@anchor@block ladder@output \pgfmathcounter}{%
					\expandafter\xdef\csname pgf@anchor@block ladder@output \pgfmathcounter\endcsname{%
						\noexpand\pgf@lib@sh@ladder@outputanchor{\pgfmathcounter}%
					}%
				}{}%
		\repeatpgfmathloop%
		\ifnum\pgf@lib@sh@ladder@numoutputs<1\relax%
			\pgferror{A block must have at least one output}%
		\fi%
	}%

	\savedmacro\numinputs{\let\numinputs\pgf@lib@sh@ladder@numinputs}%
	\savedmacro\numoutputs{\let\numoutputs\pgf@lib@sh@ladder@numoutputs}%
	\savedmacro\dimensions{%
		\pgf@lib@sh@ladder@dimensions%
	}
	\savedanchor\centerpoint{%
		\pgfpointorigin
	}
	\savedanchor\midpoint{%
      \pgfpointorigin
      \advance\pgf@y-.5\ht\pgfnodeparttextbox
      \advance\pgf@y.5\dp\pgfnodeparttextbox
		\pgfmathaddtolength\pgf@y{+0.5ex}%
	}
   \savedanchor\basepoint{%
      \pgfpointorigin
      \advance\pgf@y-.5\ht\pgfnodeparttextbox
      \advance\pgf@y.5\dp\pgfnodeparttextbox
   }
	\savedanchor\textpoint{%
      \pgfpointorigin
      \advance\pgf@x-.5\wd\pgfnodeparttextbox
      \advance\pgf@y-.5\ht\pgfnodeparttextbox
      \advance\pgf@y.5\dp\pgfnodeparttextbox
   }
	\anchor{center}{\pgfpointorigin}%
	\anchor{block center}{\dimensions\pgfpointorigin\advance\pgf@y\halfheight\advance\pgf@y-\boxdepth}%
	\anchor{text}{\dimensions\textpoint\advance\pgf@y\halfheight\advance\pgf@y-\boxdepth}%
	\anchor{mid}{\dimensions\midpoint\advance\pgf@y\halfheight\advance\pgf@y-\boxdepth}%
	\anchor{mid west}{\dimensions\midpoint\advance\pgf@y\halfheight\advance\pgf@y-\boxdepth\advance\pgf@x-\halfwidth}%
	\anchor{mid east}{\dimensions\midpoint\advance\pgf@y\halfheight\advance\pgf@y-\boxdepth\advance\pgf@x\halfwidth}%
	\anchor{base}{\dimensions\basepoint\advance\pgf@y\halfheight\advance\pgf@y-\boxdepth}%
	\anchor{base west}{\dimensions\basepoint\advance\pgf@y\halfheight\advance\pgf@y-\boxdepth\advance\pgf@x-\halfwidth}%
	\anchor{base east}{\dimensions\basepoint\advance\pgf@y\halfheight\advance\pgf@y-\boxdepth\advance\pgf@x\halfwidth}%
	\anchor{north}{\dimensions\pgfpointorigin\advance\pgf@y\halfheight\advance\pgf@y\halfheight\advance\pgf@y-\boxdepth}%
	\anchor{south}{\dimensions\pgfpointorigin\advance\pgf@y-\boxdepth}%
	\anchor{east}{\dimensions\pgfpointorigin\advance\pgf@x\halfwidth\advance\pgf@y\halfheight\advance\pgf@y-\boxdepth}%
	\anchor{west}{\dimensions\pgfpointorigin\advance\pgf@x-\halfwidth\advance\pgf@y\halfheight\advance\pgf@y-\boxdepth}%
	\anchor{north east}{\dimensions\pgfpointorigin\advance\pgf@x\halfwidth\advance\pgf@y\halfheight\advance\pgf@y\halfheight\advance\pgf@y-\boxdepth}%
	\anchor{north west}{\dimensions\pgfpointorigin\advance\pgf@x-\halfwidth\advance\pgf@y\halfheight\advance\pgf@y\halfheight\advance\pgf@y-\boxdepth}%
	\anchor{south east}{\dimensions\pgfpointorigin\advance\pgf@x\halfwidth\advance\pgf@y-\boxdepth}%
	\anchor{south west}{\dimensions\pgfpointorigin\advance\pgf@x-\halfwidth\advance\pgf@y-\boxdepth}%
	\backgroundpath{%
		\dimensions%
		\pgf@xc\halfwidth%
		\pgf@yc\halfheight%
		\pgfmathaddtolength\pgf@xc{-\pgfkeysvalueof{/pgf/outer xsep}}%
		\pgfmathaddtolength\pgf@yc{-\pgfkeysvalueof{/pgf/outer ysep}}%
		      
		\pgf@yb\boxdepth%
		\pgfmathaddtolength\pgf@yb{-\pgfkeysvalueof{/pgf/outer ysep}}%
		{%
			\pgfpathrectangle{\pgfqpoint{-\pgf@xc}{-\pgf@yb}}%
			{%
				\pgf@x2.0\pgf@xc%
				\pgf@y2.0\pgf@yc%
			}%
			\pgfpathclose%
		}%
	}
	\foregroundpath{
		\dimensions%
		{%
			%
			% Write block type (symbol)
			%
			\pgf@yc\symbolypos
			\expandafter\pgftext\expandafter[/pgf/text/at=\pgfqpoint{0pt}{\pgf@yc},/pgf/text/center]{%
				\pgfkeysgetvalue{/pgf/ladder symbol color}{\pgf@lib@sh@temp}%
				\ifx\pgf@lib@sh@temp\pgfutil@empty%
				\else%
					\expandafter\color\expandafter{\pgf@lib@sh@temp}%
				\fi%
				\pgfkeysvalueof{/pgf/ladder symbol}%
			}%
			% Draw inputs.
			%
			\pgf@xc-\halfwidth%
			\pgfmathaddtolength\pgf@xc{\pgfkeysvalueof{/pgf/outer xsep}}
			\pgf@xa\pgf@xc
			\pgfmathaddtolength\pgf@xc{\pgfkeysvalueof{/pgf/inner xsep}}
  			\pgf@yc\inputsep%
			\pgfmathloop%
			\ifnum\pgfmathcounter>\numinputs%
			\else%
				\advance\pgf@yc-\inputsep%
				\edef\pgf@lib@sh@temp{\csname inputs-\pgfmathcounter\endcsname}
				\pgf@xb\pgf@xc
				\if>\expandafter\pgfutil@first@until@relax\pgf@lib@sh@temp\relax
                           \edef\pgf@lib@sh@temp{\expandafter\expandafter\expandafter\pgfutil@gobble\pgf@lib@sh@temp}
					\pgf@ya\pgf@yc
					\pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/block ladder clksize}}
					\pgfmathaddtolength\pgf@ya{0.5\pgf@yb}
					\pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
					\pgfmathaddtolength\pgf@ya{-0.5\pgf@yb}
					\pgfmathaddtolength\pgf@xa{\pgf@yb}
					\pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
					\pgfmathaddtolength\pgf@ya{-0.5\pgf@yb}
					\pgfmathaddtolength\pgf@xa{-\pgf@yb}
					\pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
					\pgfmathaddtolength\pgf@xb{\pgf@yb}
					\pgfpathclose
				\fi
				\expandafter\pgftext\expandafter[/pgf/text/at=\pgfqpoint{\pgf@xb}{\pgf@yc},/pgf/text/left]{%
					\pgfkeysgetvalue{/pgf/ladder symbol color}{\pgf@lib@sh@@temp}%
					\ifx\pgf@lib@sh@@temp\pgfutil@empty%
					\else%
						\expandafter\color\expandafter{\pgf@lib@sh@@temp}%
					\fi%
					{\pgf@lib@sh@temp}%
				}%
			\repeatpgfmathloop%
			% Draw outputs.
			%
			\pgf@xc\halfwidth%
			\pgfmathaddtolength\pgf@xc{-\pgfkeysvalueof{/pgf/inner xsep}}
			\pgfmathaddtolength\pgf@xc{-\pgfkeysvalueof{/pgf/outer xsep}}
  			\pgf@yc\outputsep%
			\pgfmathloop%
			\ifnum\pgfmathcounter>\numoutputs%
			\else%
				\advance\pgf@yc-\outputsep%
				\expandafter\pgftext\expandafter[/pgf/text/at=\pgfqpoint{\pgf@xc}{\pgf@yc},/pgf/text/right]{%
					\pgfkeysgetvalue{/pgf/ladder symbol color}{\pgf@lib@sh@temp}%
					\ifx\pgf@lib@sh@temp\pgfutil@empty%
					\else%
						\expandafter\color\expandafter{\pgf@lib@sh@temp}%
					\fi%
					{\csname outputs-\pgfmathcounter\endcsname}%
				}%
			\repeatpgfmathloop%
		}%
	} 
	\anchorborder{%
		\dimensions%
		\pgfextract@process\externalpoint{}%
		\ifdim\pgf@y<0pt%
			\pgf@process{\pgfpointborderrectangle{\externalpoint}{\pgfqpoint{\halfwidth}{\boxdepth}}}%
		\else%
			\pgf@yc\halfheight
			\advance\pgf@yc\halfheight\advance\pgf@yc-\boxdepth
			\pgf@process{\pgfpointborderrectangle{\externalpoint}{\pgfqpoint{\halfwidth}{\pgf@yc}}}%
		\fi%
	}%  
}




% contact
\pgfdeclareshape{contact ladder}
{
  \inheritsavedanchors[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{south east}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{input}
  \inheritanchor[from=rectangle]{output}
  \inheritanchorborder[from=rectangle]  % New:
  \anchor{input}{
    \pgf@process{\northeast}%
    \pgf@ya=.5\pgf@y%
    \pgf@process{\southwest}%
    \pgf@y=.5\pgf@y%
    \advance\pgf@y by \pgf@ya%
  }
  \anchor{output}{%
    \pgf@process{\southwest}%
    \pgf@ya=.5\pgf@y%
    \pgf@process{\northeast}%
    \pgf@y=.5\pgf@y%
    \advance\pgf@y by \pgf@ya%
  }
  \savedanchor\centerpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y.5\ht\pgfnodeparttextbox%
    \advance\pgf@y-.5\dp\pgfnodeparttextbox%
  }

  \backgroundpath{
    \pgf@process{\pgfpointadd{\southwest}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}
    \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \pgf@process{\pgfpointadd{\northeast}{\pgfpointscale{-1}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}}
    \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
    \ifpgfnormallyclosednegated
       \pgfsetbeveljoin
       \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \else
       \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \fi
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
    \pgfusepathqstroke
  }
  \foregroundpath{
    \pgftransformshift{\centerpoint}
    \pgftext{\pgfkeysvalueof{/pgf/ladder symbol}}
  }
}

% coil
\pgfdeclareshape{coil ladder}
{
  \inheritsavedanchors[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{south east}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{input}
  \inheritanchor[from=rectangle]{output}
  \inheritanchorborder[from=rectangle]  % New:
  \anchor{input}{
    \pgf@process{\northeast}%
    \pgf@ya=.5\pgf@y%
    \pgf@process{\southwest}%
    \pgf@y=.5\pgf@y%
    \advance\pgf@y by \pgf@ya%
  }
  \anchor{output}{%
    \pgf@process{\southwest}%
    \pgf@ya=.5\pgf@y%
    \pgf@process{\northeast}%
    \pgf@y=.5\pgf@y%
    \advance\pgf@y by \pgf@ya%
  }
  \savedanchor\centerpoint{%
    \pgf@x.5\wd\pgfnodeparttextbox%
    \pgf@y.5\ht\pgfnodeparttextbox%
    \advance\pgf@y-.5\dp\pgfnodeparttextbox%
  }

  \backgroundpath{
    \pgf@process{\pgfpointadd{\southwest}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}
    \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \pgf@process{\pgfpointadd{\northeast}{\pgfpointscale{-1}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}}
    \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    
    \pgfmathsetlength\pgf@xc{\pgfkeysvalueof{/pgf/coil ladder curvature}\pgf@xb-\pgfkeysvalueof{/pgf/coil ladder curvature}\pgf@xa} % uses a reduction of 20% (by default) for beginning of the arcs, if it is 0.5 and width=height we got a circle, do not set to zero
    \pgfmathsetlength\pgf@yc{.5\pgf@yb-.5\pgf@ya}
    \pgfmathsetlength\pgf@ya{0.5\pgf@xc + 0.5\pgf@yc*\pgf@yc / \pgf@xc} % radius
    \advance\pgf@xa by \pgf@xc
    \advance\pgf@xb by-\pgf@xc
    \pgfmathsetlength\pgf@yc{\pgf@yc / \pgf@ya}
    \pgfmathasin{\pgf@yc}
    \pgfmathsetlength\pgf@yc{\pgfmathresult} % angle
    \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfpatharc{180-\pgf@yc}{180+\pgf@yc}{\pgf@ya}
    \ifpgfnormallyclosednegated
       \pgfsetbeveljoin
       \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \else
       \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \fi
    \pgfpatharc{\pgf@yc}{-\pgf@yc}{\pgf@ya}
    \pgfusepathqstroke
  }
  \foregroundpath{
    \pgftransformshift{\centerpoint}
    \pgftext{\pgfkeysvalueof{/pgf/ladder symbol}}
  }
}

\makeatother

\tikzset{
  circuit declare symbol=contact NO,
  circuit declare symbol=contact NC,
  circuit declare symbol=var contact NC,
  circuit declare symbol=contact P,
  circuit declare symbol=contact N,
  circuit declare symbol=coil,
  circuit declare symbol=coil NA,
  circuit declare symbol=var coil NA,
  circuit declare symbol=coil S,
  circuit declare symbol=coil R,
  circuit declare symbol=block,
  set contact NO graphic={
    circuit symbol lines,
    circuit symbol size=width 1.5 height 2,
    transform shape,
    shape=contact ladder
  },
  set contact NC graphic={
    circuit symbol lines,
    circuit symbol size=width 1.5 height 2,
    transform shape,
    crossed ladder=true,
    shape=contact ladder
  },
  set var contact NC graphic={
    circuit symbol lines,
    circuit symbol size=width 1.5 height 2,
    transform shape,
    ladder symbol=\pgfkeysvalueof{/pgf/var contact NC ladder symbol},
    shape=contact ladder
  },
  set contact P graphic={
    circuit symbol lines,
    circuit symbol size=width 1.5 height 2,
    transform shape,
    ladder symbol=\pgfkeysvalueof{/pgf/contact P ladder symbol},
    shape=contact ladder
  },
  set contact N graphic={
    circuit symbol lines,
    circuit symbol size=width 1.5 height 2,
    transform shape,
    ladder symbol=\pgfkeysvalueof{/pgf/contact N ladder symbol},
    shape=contact ladder
  },
  set coil graphic={
    circuit symbol lines,
    fill=none,
    circuit symbol size=width 2.4 height 2,
    transform shape,
    shape=coil ladder
  },
  set coil NA graphic={
    circuit symbol lines,
    circuit symbol size=width 2.4 height 2,
    crossed ladder=true,
    transform shape,
    shape=coil ladder
  },
  set var coil NA graphic={
    circuit symbol lines,
    circuit symbol size=width 2.4 height 2,
    ladder symbol=\pgfkeysvalueof{/pgf/var coil NA ladder symbol},
    transform shape,
    shape=coil ladder
  },
  set coil S graphic={
    circuit symbol lines,
    circuit symbol size=width 2.4 height 2,
    ladder symbol=\pgfkeysvalueof{/pgf/coil S ladder symbol},
    transform shape,
    shape=coil ladder
  },
  set coil R graphic={
    circuit symbol lines,
    circuit symbol size=width 2.4 height 2,
    ladder symbol=\pgfkeysvalueof{/pgf/coil R ladder symbol},
    transform shape,
    shape=coil ladder
  },
  set block graphic={
    circuit symbol lines,
    circuit symbol size=width 2.4 height 2,
    ladder symbol=\pgfkeysvalueof{/pgf/block ladder symbol},
    transform shape,
    inner sep=.5ex,
    shape=block ladder,
    every ladder block/.try
  }
}

\endinput