[Tex/LaTex] Drawing 3D Transformer with TikZ or PSTricks

pstrickstikz-pgf

Consider the picture of a transformer on the page:

what-I-want

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

Best Answer

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}

enter image description here


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};
}

enter image description here