Re: How to create new node shapes ?
Mark Wibrow <m.wibrow <at> gmail.com>
2012-05-08 18:14:06 GMT
Hi again,
The following (although far from perfect) shows how the approach can be
used to build symbols from other symbols:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,shapes.symbols}
\begin{document}
\makeatletter
\tikzset{
symbol/.style={
inner sep=0pt,
shape=rectangle,
execute at begin node={
\begin{tikzpicture}
\def\tikz <at> atbegin <at> node{}
\tikzset{symbols/.cd, #1}
\end{tikzpicture}
}
}
}
\makeatother
\tikzset{
symbol lines/.style={
very thick,
line join=round,
},
}
\colorlet{symbol color}{red!50}
\tikzset{symbols/.cd,
.unknown/.code={
\let\symbolkey=\pgfkeyscurrentname%
\tikzset{\symbolkey={#1}}%
},
code/.code=\begin{scope}#1\end{scope},
cylinder top/.code={
\begin{scope}[yscale=0.5]
\path [fill=symbol color!50] circle [radius=1cm];
\end{scope}
},
cylinder body/.code={
\path [fill=symbol color]
(-1cm, 0cm) -- (-1cm, -#1) arc (180:360:1cm and 0.5cm) -- (1cm, 0cm)
arc (360:180:1cm and 0.5cm) -- cycle;
},
router arrows/.code={
\begin{scope}[yscale=0.5]
\foreach \i in {0, ..., 3}
\path [fill=symbol color!5, rotate=\i*90+45,
xshift={0.25cm+mod(\i,2)*0.625cm}, xscale={mod(\i+1,2)*2-1}]
(0cm,0cm) -- (0cm,0.125cm) -- (0.375cm,0.125cm) -- (0.375cm, 0.25cm)
-- (0.625cm, 0cm) --
(0.375cm, -0.25cm) -- (0.375cm, -0.125cm) -- (0cm, -0.125cm) -- cycle;
\end{scope}
},
3D box/.code args={width #1 height #2 depth #3}{
\path [fill=symbol color!50] (-#1/2,-#2/2) rectangle ++(#1, #2);
\path [fill=symbol color] (#1/2, -#2/2) -- ++(0cm, #2) -- ++(#3, #3)
-- ++(0cm, -#2) -- cycle;
\path [fill=symbol color!75] (-#1/2, #2/2) -- ++(#1, 0cm) -- ++(#3,
#3) -- ++(-#1, 0cm) -- cycle;
},
arrows from circle/.code={
\path [fill=symbol color!5]
circle [radius=0.25cm];
\foreach \i in {0, 45, ..., 315}
\path [symbol lines, draw=symbol color!5, fill=symbol color!5,
rotate=\i]
(0cm,0cm) -- (#1, 0cm) (#1,0.0625cm) -- (#1+0.125cm,0cm) --
(#1,-0.0625cm) -- cycle;
}
}
\tikzset{symbols/.cd,
internet/.code={
\node [cloud, cloud puffs=9, cloud puff arc=110,
minimum width=2.5cm, minimum height=1.5cm, fill=symbol color,
draw=symbol draw]{};
},
router/.style={
cylinder body=0.5cm,
cylinder top,
router arrows
},
generic gateway/.style={
cylinder body=1.25cm,
cylinder top,
router arrows,
code={
\tikzset{yshift=-1.125cm, symbols/.cd, arrows from circle=0.375cm}
}
},
router with firewall/.style={
cylinder body=0.25cm,
code={
\tikzset{yshift=-0.3125cm, symbols/.cd, cylinder body=0.5cm}
},
cylinder top,
code={
\begin{scope}[yscale=0.5]
\clip circle [radius=1cm];
\tikzset{rotate=-45}
\foreach \i in {-3,-2,...,3}{
\draw [draw=symbol color!75, symbol lines](\i*0.5cm,-1cm) --
(\i*.5cm,1cm);
\foreach \j in {-1,0, 1}
\draw [draw=symbol color!75, symbol lines]
[yshift={mod(\i,2)*0.5cm)}](\i*.5cm,\j*1cm) -- (\i*.5cm+0.5cm,\j*1cm);
}
\end{scope}
},
router arrows
},
access point/.style={
3D box=width 2cm height 0.5cm depth 0.25cm,
code={
\path [symbol lines, draw=symbol color!25]
(-0.75, 0) \foreach \i in {0,...,2} {
sin ++(0.125,0.125) cos ++(0.125,-0.125) sin ++(0.125,-0.125) cos
++(0.125,0.125)
};
\path [symbol lines, draw=symbol color!5, yscale=-1]
(-0.75, 0) \foreach \i in {0,...,2} {
sin ++(0.125,0.125) cos ++(0.125,-0.125) sin ++(0.125,-0.125) cos
++(0.125,0.125)
};
}
},
layer 3 switch/.style={
3D box=width 1.5cm height 1.5cm depth 0.25cm,
arrows from circle=0.5cm
}
}
\foreach \c in {red, blue, green!50!red}{
\colorlet{symbol color}{\c!50}
\foreach \s in {access point, layer 3 switch, generic gateway, router
with firewall, router}
{\tikz\node[symbol=\s]{};\space}
\par
}
\end{document}
Hope this helps
mark
On 08/05/12 09:33, Ignasi wrote:
> Thank you Mark for the example.
> Just a question about it.
> Where is the 'outer sep' defined, i.e., why the edge between the cloud and
> cable modem doesn't touch the borders?
>
> Thank you,
>
> Ignasi
>
> On Tue, 08 May 2012 09:58:42 +0200, Mark Wibrow<m.wibrow <at> gmail.com> wrote:
>
>> \documentclass{article}
>> \usepackage{tikz}
>> \usetikzlibrary{shapes.symbols}
>> \begin{document}
>> \makeatletter
>> \tikzset{
>> symbol/.style={
>> execute at begin node={
>> \begin{tikzpicture}
>> \def\tikz <at> atbegin <at> node{}
>> \tikzset{symbols/#1}
>> \end{tikzpicture}
>> }
>> }
>> }
>> \makeatother
>> \tikzset{
>> symbol lines/.style={
>> very thick,
>> line join=round,
>> },
>> }
>> \colorlet{symbol fill}{black!50}
>> \colorlet{symbol draw}{black!25}
>> \tikzset{symbols/.cd,
>> internet/.code={
>> \node [cloud, cloud puffs=9, cloud puff arc=110,
>> minimum width=2.5cm, minimum height=1.5cm, fill=symbol fill,
>> draw=symbol draw]{};
>> },
>> cable modem/.code={
>> \path [fill=symbol fill, draw=symbol draw, symbol lines]
>> (0,0) -- (0,0.5) -- (0.5,1) -- (1.75,1) -- (1.75,0.5) -- (1.25,0) --
>> cycle;
>> \path [draw=symbol draw, symbol lines]
>> (0,0.5) -- (1.25,0.5) -- (1.75,1) (1.25,0.5) -- (1.25,0);
>> \path [draw=symbol draw, line width=0.1cm, stealth-stealth,
>> fill=symbol draw]
>> (0.0625,0.25) -- (0.875,0.25);
>> \path [draw=symbol draw, line width=0.1cm, -stealth, fill=symbol draw,
>> line join=rect]
>> (1,0.25) -- ++(-0.1, 0);
>> }
>> }
>> \begin{tikzpicture}[level distance=2cm, every child/.style={symbol
>> lines}]
>> \node [symbol=internet, label=center:Internet] {}
>> child { node [symbol=cable modem, label=below:cable modem] {} };
>> \end{tikzpicture}
>> \end{document}
>
>
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/