不幸的是,我无法将节点 Client1 连接到端口 13。下面显示了我的失败尝试。我知道与节点不同,pics 无法在以后引用。有人能帮我将 port/.pic 转换为以后可以引用的节点吗?我的示例中的红色圆圈仅供参考。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc, shadings, shadows, shapes.arrows}
\usetikzlibrary{positioning}
\tikzset{
port/.pic={% style, size, label
\tikzset{port/.cd,#1}
\coordinate[] (main) at (0cm,0cm);
\coordinate[] (labelposition) at (.5cm,1.2cm);
\draw[port/rect] (main) rectangle (\pgfkeysvalueof{/tikz/port/tr});
\draw[fill=black] (.1cm,0cm) rectangle (.9cm,.5cm);
\draw[fill=black] (.2cm,.5cm) rectangle (.8cm,.7cm);
\draw[fill=black] (.4cm,.7cm) rectangle (.6cm,.8cm);
\node (labelnode) at (labelposition)
{\pgfkeysvalueof{/tikz/port/label}};
},
port/.cd,
rect/.style={thin, fill=gray!20},
tr/.initial={1cm,1.5cm},
label/.initial={},
name/.initial={},
/tikz/.cd,
rack switch/.style={
name=#1,
fill=white, draw,
minimum width=20cm,
minimum height=4cm,
path picture={
\draw[top color=gray!5,bottom color=gray!40](path picture bounding box.south west) rectangle (path picture bounding box.north east);
%
\coordinate (A-west) at([xshift=1cm,yshift=.2]path picture bounding box.west);
%
\draw[rack switch/point] (A-west) circle ;
%
\coordinate (A-center) at ($(path picture bounding box.center)!0!(path picture bounding box.south)$);
%
\draw[rack switch/point] (A-center) circle;
%
\coordinate (A-east) at (path picture bounding box.east);
%
\draw[rack switch/point] (A-east) circle;
%
\foreach \x/\a in {0/1,1.2/2,2.4/3,3.6/4,4.8/5,6/7,7.2/8,8.4/9,9.6/10,10.8/11, 12/12, 13.2/13}{
\pic(\a) at ($(A-west)+(\x,0cm)$){port={label=\a}};
}
\foreach \x/\b in {1/14,2.2/15,3.4/16, 4.6/17, 5.8/18, 7/19, 8.2/20, 9.4/21, 10.6/22, 11.8/23, 13/24, 14.2/25}{
\pic[rotate=180](\b) at ($(A-west)+(\x,-.2cm)$){port={label=\b}};
}
}
},
rack switch/.cd,
point/.style={fill=red!50, radius=0.2cm},
/tikz/.cd
}
\begin{document}
\begin{tikzpicture}
\node(Switch1) at (0,0) [rack switch=s]{};
\node(Client1) [above right=2of s] {\includegraphics{icons/fileserver}};
\draw [red, ->] (Client1) -- (13);
\end{tikzpicture}
\end{document}
答案1
正如 Black Mild 所建议的,您需要nodes
在其中包含pic
对它们的引用。
以下代码显示了交换机的另一个版本。完整的switch
是pic
,每个端口都是一个node
,并带有一个用于path picture
绘制孔。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc, shadings, shadows, shapes.arrows, matrix}
\usetikzlibrary{positioning}
\tikzset{
port_up/.style={draw, top color=gray!5, bottom color=gray!40, minimum width=1cm, minimum height=1.5cm,
path picture={\fill ([xshift=1mm]path picture bounding box.south west)|-++(1mm,5mm)|-++(2mm,2mm)|-++(2mm,1mm)|-++(2mm,-1mm)|-++(1mm,-2mm)|-cycle;}, label={[anchor=north, outer sep=1mm]north:#1}},
port_down/.style={draw, top color=gray!5, bottom color=gray!40, minimum width=1cm, minimum height=1.5cm,
path picture={\fill ([xshift=1mm]path picture bounding box.north west)|-++(1mm,-5mm)|-++(2mm,-2mm)|-++(2mm,-1mm)|-++(2mm,1mm)|-++(1mm,2mm)|-cycle;}, label={[anchor=south, outer sep=1mm]south:#1}},
switch/.pic={
\node[minimum width=20cm, minimum height=4cm, draw, top color=gray!5,bottom color=gray!40] (-body) {};
\path[fill=red!50] ([shift={(1cm,.2cm)}]-body.west) coordinate (-A) circle (.2);
\path[fill=red!50] (-body.center) circle (.2);
\path[fill=red!50] (-body.east)++(90:.2) arc (90:270:.2)--cycle;
\foreach \i [count=\topi from 1, count=\bottomi from 14] in {1,...,13}{
\node[port_up=\topi, anchor=south west] (-\topi) at ([xshift=(\i-1)*1.1cm]-A) {};
\node[port_down=\bottomi, anchor=north, below=2mm of -\topi] (-\bottomi){};
}
},
}
\begin{document}
\begin{tikzpicture}
\path pic (Switch1) {switch};
\node[above right=2cm of Switch1-body] (Client) {\includegraphics{fileserver}};
\draw[red] (Client) -- (Switch1-13);
\draw[green, ultra thick] (Switch1-16.center) to[out=280, in=-100] (Switch1-22.center);
\end{tikzpicture}
\end{document}