summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/circuitikz/tex/pgfcircpath.tex
blob: 4518f16479c4587b9d8c45a74c7e606158f10797 (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
% Copyright 2018-2022 by Romano Giannetti
% Copyright 2015-2022 by Stefan Lindner
% Copyright 2013-2022 by Stefan Erhardt
% Copyright 2007-2022 by Massimo Redaelli
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the files gpl-3.0_license.txt and lppl-1-3c_license.txt for more details.


\def\pgf@circ@direction{0.0}

% swap two coordinates
\def\pgfcirc@swap@coordinates#1#2{%
    coordinate (pgfcirc@tmp@swap) at (#1)
    coordinate (#1) at (#2)
    coordinate (#2) at (pgfcirc@tmp@swap)
}

% Names
\ctikzset{name/.style = { n={#1} } } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@@@
\ctikzset{n/.code = {
	\pgfkeys{/tikz/circuitikz/bipole/name={#1}}
}}

% Reflect the node along
\ctikzset{mirrored/.is choice}
\ctikzset{mirror value/.initial=1}
\ctikzset{mirrored/true/.code = {\ctikzsetvalof{mirror value}{-1}} }
\ctikzset{mirrored/false/.code = {\ctikzsetvalof{mirror value}{1}} }
\ctikzset{mirror/.style = {/tikz/circuitikz/mirrored=true}}

% Invert node along path
\ctikzset{inverted/.is choice}
\ctikzset{invert value/.initial=1}
\ctikzset{inverted/true/.code = {\ctikzsetvalof{invert value}{-1}\pgf@circuit@bipole@invertedtrue}}
\ctikzset{inverted/false/.code = {\ctikzsetvalof{invert value}{1}\pgf@circuit@bipole@invertedfalse}}
\ctikzset{invert/.style = {/tikz/circuitikz/inverted=true}}
\newif\ifpgf@circuit@bipole@inverted
\ctikzset{bipole/inverted/.is if=pgf@circuit@bipole@inverted}

\newif\ifpgf@circuit@bipole@voltage@backward
\ctikzset{bipole/voltage/direction/.is choice}
\ctikzset{bipole/voltage/direction/forward/.code={\pgf@circuit@bipole@voltage@backwardfalse}}
\ctikzset{bipole/voltage/direction/backward/.code={\pgf@circuit@bipole@voltage@backwardtrue}}

% Initialize paths
\def\pgfcircresetpath{
    \ctikzset{bipole/name=, bipole/label/name=, bipole/label/position=90, ,bipole/annotation/name=, bipole/annotation/position=-90,
        bipole/inverted=false, bipole/kind=,
        bipole/voltage/direction=backward, bipole/voltage/label/name=, bipole/voltage/position=below,
        bipole/nodes/left=none, bipole/nodes/right=none, bipole/is voltage=false,bipole/is voltageoutsideofsymbol=false,bipole/is strokedsymbol=false,
        bipole/is current=false, bipole/current/label/name=, bipole/current/x position=after,
        bipole/current/y position=above, bipole/current/direction=forward,
        mirrored=false
    }
}

%
% expandable IF for the extra nodes (thanks to Henri Menke)
% see https://chat.stackexchange.com/transcript/message/56560808#56560808
%
\def\pgfcirc@if@has@i{%
    \ifpgfcirc@has@i
        \expandafter\pgfutil@firstoftwo
    \else
        \expandafter\pgfutil@secondoftwo
    \fi}
\def\pgfcirc@if@has@v{%
    \ifpgfcirc@has@v
        \expandafter\pgfutil@firstoftwo
    \else
        \expandafter\pgfutil@secondoftwo
    \fi}
\def\pgfcirc@if@has@f{%
    \ifpgfcirc@has@f
        \expandafter\pgfutil@firstoftwo
    \else
        \expandafter\pgfutil@secondoftwo
    \fi}



%% Generic bipole path
\def\pgf@circ@bipole@path#1#2{
    % Create a bipole path from the shapes defined with \pgfcircdeclarebipole
    % or \pgfcircdeclarebipolescaled; the node shapes are named with a "shape"
    % appended to the main (path-style) name
    % #1 path-style node name
    % #2 the argument passed from the to-path structure; don't touch
    %
    % Example:
    % \def\pgf@circ@capacitor@path#1{\pgf@circ@bipole@path{capacitor}{#1}}
    %
    \pgf@circ@bipole@path@base{shape}{}{#1}{#2}
}
%%
%% ultra-generic bipole path
%% I am not sure what the last argument is needed for, but don't touch it or everything explodes
%%
\def\pgf@circ@bipole@path@base#1#2#3#4{%
    %
    % Create a path-style component based on a node-style shape
    % #1: postfix to be added to the name path to obtain the main shape name
    % #2: text to be passed as text to the node
    % #3: name of the bipole component
    % #4: this will be filled by the argument of the to-path
    %
    \pgfextra{
        \ctikzset{bipole/kind = #3}
        \edef\pgf@temp{\ctikzvalof{bipole/name}}
        \def\pgf@circ@temp{}
        \ifx\pgf@temp\pgf@circ@temp % if it has not a name
            \pgfmathrandominteger{\pgf@circ@rand}{1000}{9999}
            \ctikzset{bipole/name = pgfcirc@#3\pgf@circ@rand} % create it (re-usage should not create problem, but...)
            \edef\pgfcirc@a@prefix{pgfcirc}% do not pollute the namespace for nothing
        \else
            \edef\pgfcirc@a@prefix{\ctikzvalof{bipole/name}}% for exporting v-i-f anchors
        \fi
    }
    % save start and stop values
    % notice that we DO NOT MOVE the path position at all!
    coordinate (\ctikzvalof{bipole/name}start) at (\tikztostart)
    coordinate (\ctikzvalof{bipole/name}end) at (\tikztotarget)
    \pgfextra{
        % find the direction (angle) of the path
        \pgfmathanglebetweenpoints{\pgfpointanchor{\ctikzvalof{bipole/name}start}{center}}
            {\pgfpointanchor{\ctikzvalof{bipole/name}end}{center}}
        \edef\pgf@circ@direction{\pgfmathresult}
        % the global macro  pgfcirc@whatever-direction is accessible as \ctikzgetdirection{whatever}
        % this gives access to the element path direction
        \expandafter\xdef\csname pgfcirc@\pgfcirc@a@prefix-direction\endcsname{\pgf@circ@direction}
    }
    % position the component in the middle of the path. We DO NOT MOVE the current position!
    node[#3#1, rotate=\pgf@circ@direction, yscale=\ctikzvalof{mirror value},
        xscale=\ctikzvalof{invert value}] (\ctikzvalof{bipole/name})
        at ($(\tikztostart) ! .5 ! (\tikztotarget)$) {#2}
    % set start and end labels
    \ifpgf@circuit@bipole@inverted
        \ifcsname pgf@anchor@#3#1@pathstart\endcsname%if special path-anchors are defined, use them!
            coordinate	(pgfcirc@anchorstartnode) at (\ctikzvalof{bipole/name}.pathend)
            coordinate	(pgfcirc@anchorendnode) at (\ctikzvalof{bipole/name}.pathstart)
        \else
            coordinate	(pgfcirc@anchorstartnode) at (\ctikzvalof{bipole/name}.right)
            coordinate	(pgfcirc@anchorendnode) at (\ctikzvalof{bipole/name}.left)
        \fi
        \else
        \ifcsname pgf@anchor@#3#1@pathstart\endcsname%if special path-anchors are defined, use them!
            coordinate	(pgfcirc@anchorstartnode) at (\ctikzvalof{bipole/name}.pathstart)
            coordinate	(pgfcirc@anchorendnode) at (\ctikzvalof{bipole/name}.pathend)
        \else
            coordinate	(pgfcirc@anchorstartnode) at (\ctikzvalof{bipole/name}.left)
            coordinate	(pgfcirc@anchorendnode) at (\ctikzvalof{bipole/name}.right)
        \fi
    \fi
    % draw the leads unless it's an open circuit
    % stop at the component
    \pgfextra{\def\pgf@temp{open}\def\pgf@circ@temp{#3}}
    \ifx\pgf@temp\pgf@circ@temp  % if it is an open do nothing
    \else
        % it is important to start the path with -- to have correct line joins!
        -- (pgfcirc@anchorstartnode)
    \fi
    % Add all the "ornaments": labels, annotations, voltages, currents and flows
    \drawpoles
    \pgf@circ@ifkeyempty{bipole/label/name}\else\pgf@circ@drawlabels{label}\fi
    \pgf@circ@ifkeyempty{bipole/annotation/name}\else\pgf@circ@drawlabels{annotation}\fi
    % the following  must be made in their own path scope to avoid crash in TikZ 3.1.8/3.1.8a
    % it should be logically safe for older version too --- even if TikZ reverted the change
    % use explandable ifs too, thanks to Henri Menke
    {\pgfcirc@if@has@v{\pgf@circ@drawvoltage}{}}%
    {\pgfcirc@if@has@i{\pgf@circ@drawcurrent}{}}%
    {\pgfcirc@if@has@f{\pgf@circ@drawflow}{}}%
    % finish the path from the component to the final target
    % you never know --- re-set \pgf@temp to detect open
    \pgfextra{\def\pgf@temp{open}\def\pgf@circ@temp{#3}}
    \ifx\pgf@temp\pgf@circ@temp  % if it is an open do nothing
        (\tikztotarget)
    \else
        (pgfcirc@anchorendnode)  -- (\tikztotarget)
    \fi
    % reset internal circuit keys
    \pgfextra{\pgfcircresetpath}
    %draw pending nodes an path
    \tikztonodes
}

%% Macros for path and style activation for bipoles or path-style

\def\comnpatname{\ifpgf@circuit@compat *\else\fi}
\def\compattikzset#1{%
    % \typeout{BIPOLEDEF:\space \detokenize{#1}}%
    \tikzset{\comnpatname#1}}
%
% this is used for components that are mainly node-style but have a path-style form
%
\def\pgfcirc@node@to@path#1#2#3{%
    % add a path-style component based on a node-style one without mangling the name
    % of the shape.
    % #1: node-type shape name (existing)
    % #2: path-type name (to be created)
    % #3: additional options to add to the path style
    %
    \expandafter\def\csname pgf@circ@#1@path\endcsname##1{\pgf@circ@bipole@path@base{}{##1}{#1}{}}%
    \compattikzset{#2/.style = {\circuitikzbasekey,
        /tikz/to path=\csname pgf@circ@#1@path\endcsname{##1},
        #3}}%
    \ctikzset{bipoles/#1/height/.initial=1}%
}
%
% this one is for normal definition: path to style, directly
% the first parameter (#1) here is l,v,i (l=..., v=..., i=...)
% the last parameter are options to be inserted in the "to path" definition
%
\def\pgfcirc@path@to@style#1#2#3#4{% using #1 as label, assign \pgf@circ@#2@path to style #3
    \compattikzset{#3/.style={\circuitikzbasekey, #4, /tikz/to path=\csname pgf@circ@#2@path\endcsname, #1={##1}}}%
}
% this one create a alias style from a node definition
\def\pgfcirc@node@to@style#1#2#3#4{% using #1 as label, assign \pgf@circ@bipole@path{#2} to style #3
    \compattikzset{#3/.style={\circuitikzbasekey, #4, /tikz/to path=\pgf@circ@bipole@path{#2}, #1={##1}}}%
}
% this create an alias style
\def\pgfcirc@style@to@style#1#2{% alias style #1 to style #2
    \compattikzset{#2/.style={\comnpatname #1={##1}}}%
}
% this create an alias style, changing the labelling
\def\pgfcirc@style@to@style@label#1#2#3{% alias style #1 to style #2
    \compattikzset{#2/.style={\comnpatname #1, #3={##1}}}%
}
% create a bipole
\def\pgfcirc@activate@bipole#1#2#3#4{% path name, base node name, style name
    \expandafter\def\csname pgf@circ@#2@path\endcsname##1{\pgf@circ@bipole@path{#3}{##1}}%
    \pgfcirc@path@to@style{#1}{#2}{#4}{}% no options here, let's see
}
\def\pgfcirc@activate@bipole@simple#1#2{\pgfcirc@activate@bipole{#1}{#2}{#2}{#2}}
% create a bipole with options
\def\pgfcirc@activate@bipole@opt#1#2#3#4#5{% path name, base node name, style name
    \expandafter\def\csname pgf@circ@#2@path\endcsname##1{\pgf@circ@bipole@path{#3}{##1}}%
    \pgfcirc@path@to@style{#1}{#2}{#4}{#5}% no options here, let's see
}
\def\pgfcirc@activate@bipole@simple@opt#1#2#3{\pgfcirc@activate@bipole@opt{#1}{#2}{#2}{#2}{#3}}


%% New system, for simple object
%% \pgfcirc@activate@bipole@simple{l}{mass}
%% New system, different names
%% The old system is the following
%% 1 - define just the pgf@circ@path@whatever#1
%% (see for example the variable one)
%% 2 - set the style
%% \compattikzset{resistor/.style= {\circuitikzbasekey, /tikz/to path=\pgf@circ@resistor@path, l={#1}}}

%% Path definition with the new mechanism have been moved to where the nodes
%% are defined.

%% Handling of terminals%<<<

\ctikzset{bipole/nodes/.is family}
\ctikzset{bipole/nodes/left/.initial=none}
\ctikzset{bipole/nodes/right/.initial=none}
\tikzset{bipole nodes/.style n args={2}{%
    \circuitikzbasekey/bipole/nodes/left=#1,
    \circuitikzbasekey/bipole/nodes/right=#2
    }
}

%% Easily usable styles

\ctikzset{o-o/.style = {\circuitikzbasekey/bipole/nodes/left=ocirc, \circuitikzbasekey/bipole/nodes/right=ocirc}}
\ctikzset{-o/.style = {\circuitikzbasekey/bipole/nodes/left=none, \circuitikzbasekey/bipole/nodes/right=ocirc}}
\ctikzset{o-/.style = {\circuitikzbasekey/bipole/nodes/left=ocirc, \circuitikzbasekey/bipole/nodes/right=none}}
\ctikzset{*-o/.style = {\circuitikzbasekey/bipole/nodes/left=circ, \circuitikzbasekey/bipole/nodes/right=ocirc}}
\ctikzset{o-*/.style = {\circuitikzbasekey/bipole/nodes/left=ocirc, \circuitikzbasekey/bipole/nodes/right=circ}}
\ctikzset{d-o/.style = {\circuitikzbasekey/bipole/nodes/left=diamondpole, \circuitikzbasekey/bipole/nodes/right=ocirc}}
\ctikzset{o-d/.style = {\circuitikzbasekey/bipole/nodes/left=ocirc, \circuitikzbasekey/bipole/nodes/right=diamondpole}}
\ctikzset{*-/.style = {\circuitikzbasekey/bipole/nodes/left=circ, \circuitikzbasekey/bipole/nodes/right=none}}
\ctikzset{-*/.style = {\circuitikzbasekey/bipole/nodes/left=none, \circuitikzbasekey/bipole/nodes/right=circ}}
\ctikzset{d-/.style = {\circuitikzbasekey/bipole/nodes/left=diamondpole, \circuitikzbasekey/bipole/nodes/right=none}}
\ctikzset{-d/.style = {\circuitikzbasekey/bipole/nodes/left=none, \circuitikzbasekey/bipole/nodes/right=diamondpole}}
\ctikzset{*-*/.style = {\circuitikzbasekey/bipole/nodes/left=circ, \circuitikzbasekey/bipole/nodes/right=circ}}
\ctikzset{d-*/.style = {\circuitikzbasekey/bipole/nodes/left=diamondpole, \circuitikzbasekey/bipole/nodes/right=circ}}
\ctikzset{*-d/.style = {\circuitikzbasekey/bipole/nodes/left=circ, \circuitikzbasekey/bipole/nodes/right=diamondpole}}
\ctikzset{d-d/.style = {\circuitikzbasekey/bipole/nodes/left=diamondpole, \circuitikzbasekey/bipole/nodes/right=diamondpole}}

% rectjoinfill workarounds

\ctikzset{.-/.style = {\circuitikzbasekey/bipole/nodes/left=rectjoinfill, \circuitikzbasekey/bipole/nodes/right=none}}
\ctikzset{.-*/.style = {\circuitikzbasekey/bipole/nodes/left=rectjoinfill, \circuitikzbasekey/bipole/nodes/right=circ}}
\ctikzset{.-o/.style = {\circuitikzbasekey/bipole/nodes/left=rectjoinfill, \circuitikzbasekey/bipole/nodes/right=ocirc}}
\ctikzset{.-d/.style = {\circuitikzbasekey/bipole/nodes/left=rectjoinfill, \circuitikzbasekey/bipole/nodes/right=diamondpole}}
\ctikzset{-./.style = {\circuitikzbasekey/bipole/nodes/left=none, \circuitikzbasekey/bipole/nodes/right=rectjoinfill}}
\ctikzset{o-./.style = {\circuitikzbasekey/bipole/nodes/left=ocirc, \circuitikzbasekey/bipole/nodes/right=rectjoinfill}}
\ctikzset{*-./.style = {\circuitikzbasekey/bipole/nodes/left=circ, \circuitikzbasekey/bipole/nodes/right=rectjoinfill}}
\ctikzset{d-./.style = {\circuitikzbasekey/bipole/nodes/left=diamondpole, \circuitikzbasekey/bipole/nodes/right=rectjoinfill}}
\ctikzset{.-./.style = {\circuitikzbasekey/bipole/nodes/left=rectjoinfill, \circuitikzbasekey/bipole/nodes/right=rectjoinfill}}

\tikzset{reversed/.style = {\circuitikzbasekey/bipole/inverted=true}}

\def\drawpoles{
    \pgfextra{ \edef\pgf@circ@temp{\ctikzvalof{bipole/nodes/left}} \def\pgf@temp{none}}
    \ifx\pgf@temp\pgf@circ@temp\else(\tikztostart) node[\pgf@circ@temp] {}\fi
    \pgfextra{ \edef\pgf@circ@temp{\ctikzvalof{bipole/nodes/right}} }
    \ifx\pgf@temp\pgf@circ@temp\else(\tikztotarget) node[\pgf@circ@temp] {}\fi
}
% %>>>

%%
%% Definition of path for transistors
%%
% Transistor like bipoles

\def\pgf@circ@trans@path#1#2{
    \pgfextra{
        \edef\pgf@temp{\ctikzvalof{bipole/name}}
        \def\pgf@circ@temp{#2}
        \ifx\pgf@temp\pgf@circ@temp % if it has not a name
            \pgfmathrandominteger{\pgf@circ@rand}{1000}{9999}
            \ctikzset{bipole/name = trans\pgf@circ@rand} % create it
        \fi
    }
    \ifpgf@circuit@bipole@inverted
        (\tikztostart) node[coordinate] (\ctikzvalof{bipole/name}end) {}
        (\tikztotarget) node[coordinate] (\ctikzvalof{bipole/name}start) {}
    \else
        (\tikztostart) node[coordinate] (\ctikzvalof{bipole/name}start) {}
        (\tikztotarget) node[coordinate] (\ctikzvalof{bipole/name}end) {}
    \fi
    \pgfextra{
        \pgfmathanglebetweenpoints{\pgfpointanchor{\ctikzvalof{bipole/name}start}{center}}
        {\pgfpointanchor{\ctikzvalof{bipole/name}end}{center}}
        \pgfmathadd{\pgfmathresult}{-90}
        \pgfmathround{\pgfmathresult}
        \edef\pgf@circ@direction{\pgfmathresult}
    }
    ($(\tikztostart) ! .5 ! (\tikztotarget)$)
    node[#1, /tikz/rotate=\pgf@circ@direction, xscale=\ctikzvalof{mirror value}]
    (\ctikzvalof{bipole/name}) {}
    node {\ctikzvalof{bipole/label/name}}
    \ifcsname pgf@anchor@#1@pathstart\endcsname%if special path-anchors are defined, use them!
        (\ctikzvalof{bipole/name}start.center) --(\ctikzvalof{bipole/name}.pathstart)
        (\ctikzvalof{bipole/name}.pathend)  -- (\ctikzvalof{bipole/name}end.center)
    \else
        (\ctikzvalof{bipole/name}start.center) --(\ctikzvalof{bipole/name}.left)
        (\ctikzvalof{bipole/name}.right)  -- (\ctikzvalof{bipole/name}end.center)
    \fi
    \drawpoles
    \pgfextra{
        \pgfcircresetpath
    }
    (\tikztotarget) 	\tikztonodes  % and go on!
}

\def\pgf@circ@definetranspath#1{
	\compattikzset{T#1/.style = {\circuitikzbasekey, /tikz/to path=\pgf@circ@trans@path{#1}{}, l=##1}}
}

%
% vim: set fdm=marker fmr=%<<<,%>>>: