A while back I asked some help to find an easy way to draw some component diagrams with TIkZ. The answer was awesome, but when adaption the solution to my case, I ended up copy-pasting the same code several times.
Here is a part of the code (without all the copy-pasted code).
\definecolor{darkgreen}{rgb}{0.18,0.54,0.34}
\tikzset{
component/.style={
rectangle,
rounded corners=0.35cm,
fill=green!50!black,
minimum width=2.5cm,
minimum height=1.5cm,
drop shadow
},
composite/.style={
component,
fill=green!12,
inner sep=0.5cm
},
corba/.style={thick,darkgreen},
u/.style={draw,circle,minimum size=6mm,outer sep=0pt},
p/.style={draw,circle,minimum size=4.5mm,outer sep=0pt},
urec/.style={draw,rectangle,minimum size=6mm,outer sep=0pt},
prec/.style={draw,rectangle,minimum size=4.5mm,outer sep=0pt},
utri/.style={draw,regular polygon,regular polygon sides=3,shape border rotate=-30,minimum size=6mm,outer sep=0pt},
ptri/.style={draw,regular polygon,regular polygon sides=3,shape border rotate=-30,minimum size=4.5mm,outer sep=0pt,inner sep=0pt},
stick port/.style={inner sep=0},
% Balls shapes
ueast/.style 2 args={
stick port,
append after command={
\pgfextra
\begin{scope}
\clip ([yshift={#1+4mm}]\tikzlastnode.east) rectangle ++(1cm,-8mm);
\draw ([yshift=#1]\tikzlastnode.east) -- ++(0:6.8mm) node[u,anchor=west] (\tikzlastnode-#2) {};
\end{scope}
\endpgfextra
}
},
ueast/.default={0mm}{ueast},
uwest/.style 2 args={
stick port,
append after command={
\pgfextra
\begin{scope}
\clip ([yshift={#1+4mm}]\tikzlastnode.west) rectangle ++(-1cm,-8mm);
\draw ([yshift=#1]\tikzlastnode.west) -- ++(180:6.8mm) node[u,anchor=east] (\tikzlastnode-#2) {};
\end{scope}
\endpgfextra
}
},
uwest/.default={0mm}{uwest},
unorth/.style 2 args={
stick port,
append after command={
\pgfextra
\begin{scope}
\clip ([xshift={#1+4mm}]\tikzlastnode.north) rectangle ++(-8mm,1cm);
\draw ([xshift=#1]\tikzlastnode.north) -- ++(90:6.8mm) node[u,anchor=south] (\tikzlastnode-#2) {};
\end{scope}
\endpgfextra
}
},
unorth/.default={0mm}{unorth},
usouth/.style 2 args={
stick port,
append after command={
\pgfextra
\begin{scope}
\clip ([xshift={#1+4mm}]\tikzlastnode.south) rectangle ++(-8mm,-1cm);
\draw ([xshift=#1]\tikzlastnode.south) -- ++(-90:6.8mm) node[u,anchor=north] (\tikzlastnode-#2) {};
\end{scope}
\endpgfextra
}
},
usouth/.default={0mm}{usouth},
peast/.style 2 args={
stick port,
append after command={
\pgfextra
\draw ([yshift=#1]\tikzlastnode.east) -- ++(0:7.75mm) node[p,anchor=west] (\tikzlastnode-#2) {};
\endpgfextra
}
},
peast/.default={0mm}{peast},
pwest/.style 2 args={
stick port,
append after command={
\pgfextra
\draw ([yshift=#1]\tikzlastnode.west) -- ++(180:7.75mm) node[p,anchor=east] (\tikzlastnode-#2) {};
\endpgfextra
}
},
pwest/.default={0mm}{pwest},
pnorth/.style 2 args={
stick port,
append after command={
\pgfextra
\draw ([xshift=#1]\tikzlastnode.north) -- ++(90:7.75mm) node[p,anchor=south] (\tikzlastnode-#2) {};
\endpgfextra
}
},
pnorth/.default={0mm}{pnorth},
psouth/.style 2 args={
stick port,
append after command={
\pgfextra
\draw ([xshift=#1]\tikzlastnode.south) -- ++(-90:7.75mm) node[p,anchor=north] (\tikzlastnode-#2) {};
\endpgfextra
}
},
psouth/.default={0mm}{psouth},
% CORBA shapes
ueastcorba/.style 2 args={
stick port,
append after command={
\pgfextra
\begin{scope}
\clip ([yshift={#1+4mm}]\tikzlastnode.east) rectangle ++(1cm,-8mm);
\draw [corba] ([yshift=#1]\tikzlastnode.east) -- ++(0:6.8mm) node[u,anchor=west] (\tikzlastnode-#2) {};
\end{scope}
\endpgfextra
}
},
ueastcorba/.default={0mm}{ueast},
% [...] same for uwestcorba, unorthcorba, usouthcorba, peastcorba, pwestcorba, pnorthcorba, psouthcorba
% Rectangle shapes
ureceast/.style 2 args={
stick port,
append after command={
\pgfextra
\begin{scope}
\clip ([yshift={#1+4mm}]\tikzlastnode.east) rectangle ++(1.15cm,-8mm);
\draw ([yshift=#1]\tikzlastnode.east) -- ++(0:6.8mm) node[urec,anchor=west] (\tikzlastnode-#2) {};
\end{scope}
\endpgfextra
}
},
ureceast/.default={0mm}{ueast},
% [...] same for urecwest, urecnorth, urecsouth, preceast, precwest, precnorth, precsouth
% Triangle shapes
utrieast/.style 2 args={
stick port,
append after command={
\pgfextra
\begin{scope}
\clip ([yshift={#1+4mm}]\tikzlastnode.east) rectangle ++(1.15cm,-8mm);
\draw ([yshift=#1]\tikzlastnode.east) -- ++(0:6.8mm) node[utri,anchor=west] (\tikzlastnode-#2) {};
\end{scope}
\endpgfextra
}
},
utrieast/.default={0mm}{uwest},
% [...] same for utriwest, utrinorth, utrisouth, ptrieast, ptriwest, ptrinorth, ptrisouth
}
The styles ueast
, uwest
, unorth
, usouth
defines the use port to draw on every side of the component they're attached. The styles peast
, pwest
, pnorth
, psouth
define the provide ports. And there are 4 variations of them.
The plain one that draw black circles / half-circles.
One that draw thick green circles / half circles.
One that draw triangles as ports (not cut in half and shapes rotated).
One that draw squares (not cut in half).
So it's 4 variations of 8 styles, which makes 32 very similar TIkZ styles.
Is there a way to write those styles more concisely? Possibily without using macros to generate the code. Ideally, it would rotate automagically the port.
I can include the missing code if needed.
\newcommand\thing[1]{something that's different: #1 \tikzsetstylestuff{blah blah = blah ##1}}