# Drawing 3D Transformer with TikZ

Consider the picture of a transformer on the page :

How do I draw this using TikZ (which I have not used before)?

-
You can look at my answer for this question and then you can make an attempt with the gray solid and the green path. After if you have some problems, we can help you for the end of the drawing. – Alain Matthes Jul 2 '12 at 7:32
Yes I forgot :( tex.stackexchange.com/questions/15079/… – Alain Matthes Jul 2 '12 at 7:59
As I said, I have not used TikZ before, but I will give it a try. Thank you. – Svend Tveskæg Jul 2 '12 at 8:18
If you want to learn more about TikZ by seeing some examples I'd suggest giving a shot at TikZ and PGF examples There are many examples there with their source code and the final results – Vagelis Bebelis Jul 2 '12 at 14:28
Welcome to TEX.SX! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. – azetina Jul 2 '12 at 14:37
show 2 more comments

It's quite easy to set up custom axes in TikZ: for instance, x={(0.707,0.707)} means that the x-vector points in direction of 45° (up right) with length 1 (in general : (r cos angle,r sin angle)). I used 45°, 165° and 90° for x, y and z axes. The hard part is imagining what coordinates to use. Another difficulty is the upper part of the left coil: if you draw it via a foreach loop, then some parts are in front of the front face, while some are behind it, so I used a scope to restrict the drawing.

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}

\newcommand{\innercolor}{gray!80!black}
\newcommand{\outercolor}{gray!80!white}
\newcommand{\leftcoil}{red!75!gray}
\newcommand{\rightcoil}{green!75!gray}
\pgfmathsetmacro{\coilseparation}{0.02}

\pgfmathsetmacro{\halflinewidth}{0.008}

\begin{document}

\begin{tikzpicture}[x={(0.707cm,0.707cm)},y={(-0.966cm,0.259cm)},z={(0cm,1cm)}]
\filldraw[fill=\innercolor]  (0,1,1) -- (1,1,1) -- (1,4,1) -- (0,4,1) -- cycle;
\filldraw[fill=\innercolor]  (1,4,1) -- (0,4,1) -- (0,4,4) -- (1,4,4) -- cycle;
\filldraw[fill=\innercolor]  (0,0,0) -- (1,0,0) -- (1,0,5) -- (0,0,5) -- cycle;
\filldraw[fill=\innercolor]  (0,0,5) -- (0,5,5) -- (1,5,5) -- (1,0,5) -- cycle;
\filldraw[fill=\outercolor,even odd rule]    (0,0,0) -- (0,5,0) -- (0,5,5) -- (0,0,5) --cycle (0,1,1) -- (0,4,1) -- (0,4,4) -- (0,1,4) --cycle ;
\begin{scope}
\clip (0,3,1) -- (0,6,1) -- (0,6,4) -- (0,3,4);
\foreach \z in {1.125,1.375,...,3.875}
{   \draw[\leftcoil,thick] (0,5,\z) -- (-\coilseparation,5,\z) -- (-\coilseparation,4-\coilseparation,\z) -- (1+\coilseparation,4-\coilseparation,\z) -- (1+\coilseparation,4,\z);
}
\end{scope}
\foreach \z in {1.25,1.75,...,3.75}
{   \draw[\rightcoil,thick] (0,1,\z) -- (-\coilseparation,1,\z) -- (-\coilseparation,0-\coilseparation,\z) -- (1+\coilseparation,0-\coilseparation,\z) -- (1+\coilseparation,0,\z);
}

\end{tikzpicture}

\end{document}


Edit 1: To help you figure out where you are in 3D, you can append this in the tikzpicture:

    \draw[ultra thick,blue,->] (0,0,0) -- (2.5,0,0) node[below right] {x};
\draw[ultra thick,blue,->] (0,0,0) -- (0,6.5,0) node[below] {y};
\draw[ultra thick,blue,->] (0,0,0) -- (0,0,6.5) node[right] {z};
\foreach \x in {1,2}
{   \draw[ultra thick,blue] (\x,0.1,0) -- (\x,-0.1,0) node[below right] {\x};
}
\foreach \yz in {1,2,3,4,5,6}
{   \draw[ultra thick,blue] (0.1,\yz,0) -- (-0.1,\yz,0) node[below] {\yz};
\draw[ultra thick,blue] (0.1,0,\yz) -- (-0.1,0,\yz) node[right] {\yz};
}


-
This is awesome. Thank you. Any chance I can get you (or any other) to draw the rest of the picture? I would like to learn TikZ and I think I will do this best by seeing some examples (and read manuals). – Svend Tveskæg Jul 2 '12 at 10:17
I am still a tikz beginner, but you could browse questions on the site. (I have asked quite a few questions aswell). And then attempt to improve this image. You learn best by trying yourself. =) – N3buchadnezzar Jul 2 '12 at 14:31
@Tom Bombadil: Thank you for adding the axes. I am (very!) slowly going through the example starting on page 40 in the TikZ and PGF manual; I guess I have to follow more basic examples before I can draw the transformer. – Svend Tveskæg Jul 2 '12 at 16:45