7

I want to make a new shape for switch in my network diagram. It's really simple based on circle.

enter image description here

I got this manually by

\def \drawswitch {
  \node[circle,draw] (circle){};
  \draw (circle.south west) -- (circle.north east);
  \draw (circle.north west) -- (circle.south east);
}

but I still want to make it perfect by defining a new shape using \pgfdeclareshape, however I've no idea how to overwrite the \backgroundpath. I don't know how to find saved anchor a circle have and annotations like \pgf@xb confused me a lot .

qweruiop
  • 485

1 Answers1

8

Next is the code for a switch shape. I've taken the code from the forbidden sign (pgflibraryshapes.symbols.code.tex) and added the missing line.

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}

\makeatletter
\pgfdeclareshape{switch}
{
  \inheritsavedanchors[from=circle] % this is nearly a circle
  \inheritanchorborder[from=circle]
  \inheritanchor[from=circle]{north}
  \inheritanchor[from=circle]{north west}
  \inheritanchor[from=circle]{north east}
  \inheritanchor[from=circle]{center}
  \inheritanchor[from=circle]{west}
  \inheritanchor[from=circle]{east}
  \inheritanchor[from=circle]{mid}
  \inheritanchor[from=circle]{mid west}
  \inheritanchor[from=circle]{mid east}
  \inheritanchor[from=circle]{base}
  \inheritanchor[from=circle]{base west}
  \inheritanchor[from=circle]{base east}
  \inheritanchor[from=circle]{south}
  \inheritanchor[from=circle]{south west}
  \inheritanchor[from=circle]{south east}
  \inheritbackgroundpath[from=circle]
  \foregroundpath{
    \centerpoint%
    \pgf@xc=\pgf@x%
    \pgf@yc=\pgf@y%
    \pgfutil@tempdima=\radius%
    \pgfmathsetlength{\pgf@xb}{\pgfkeysvalueof{/pgf/outer xsep}}%  
    \pgfmathsetlength{\pgf@yb}{\pgfkeysvalueof{/pgf/outer ysep}}%  
    \ifdim\pgf@xb<\pgf@yb%
      \advance\pgfutil@tempdima by-\pgf@yb%
    \else%
      \advance\pgfutil@tempdima by-\pgf@xb%
    \fi%
    \pgfpathmoveto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{0.707107\pgfutil@tempdima}{-0.707107\pgfutil@tempdima}}}
    \pgfpathlineto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{-0.707107\pgfutil@tempdima}{0.707107\pgfutil@tempdima}}}
    \pgfpathmoveto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{-0.707107\pgfutil@tempdima}{-0.707107\pgfutil@tempdima}}}
    \pgfpathlineto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{0.707107\pgfutil@tempdima}{0.707107\pgfutil@tempdima}}}
    \pgfsetarrowsstart{}
    \pgfsetarrowsend{}
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}
\node[switch,draw,fill=red!30] (A) {Switch A};
\node[switch,draw,fill=red!30, right=1cm of A] (B) {Switch B};
\draw (A)--(B);
\end{tikzpicture}

\end{document}

enter image description here

Ignasi
  • 136,588