在 TikZ 中定义“块”和“连接器”命令?

在 TikZ 中定义“块”和“连接器”命令?

我如何在 TikZ 中定义两个命令block和,connector以便能够以与和相同的方式使用umlpackage它们umlimportuml-tikz

例如我希望能够说出类似的话:

\block[x=0,y=0]{UI}
\block[x=2]{Services}
\block[x=4]{Business Layer}
\block{x=6]{Database Layer}

\connector{UI}{Services}
\connector{Services}{Business Layer}
\connector{Business Layer}{Database Layer}

并得到如下图表:

在此处输入图片描述

如果有一种在 TikZ 中执行此操作的简单方法(无需执行所有节点和绘制命令 - 我想以声明方式执行此操作)或者如果有一个用于制作这样的组件图的库,请告诉我。

PS:最终,我希望能够绘制模型图像这样

在此处输入图片描述

答案1

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}

\newcommand{\block}[2][0,0]{%
\node[draw,minimum height=1cm,minimum width=3cm] (#2) at (#1) {#2};
}

\newcommand{\connector}[2]{%
\draw (#1) -- (#2);
}

\begin{document}

\begin{tikzpicture}
    \block{First Block}
    \block[4,-1]{Second Block}
    \block[3,2]{Third Block}
    \block[1,4]{Fourth Block}
    \connector{First Block}{Second Block}
    \connector{Third Block}{Second Block}
    \connector{First Block}{Fourth Block}
\end{tikzpicture}

\end{document}

在此处输入图片描述

PS 有关在 Boxes 中安装节点的信息,请查看fitting库(手册中的第 34 章)


编辑1:稍微高级一点,现在有了连接器、文件夹类节点的选项,并且可以使用适合大文件夹节点的库:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows,fit}
\usepackage{xifthen}
\usepackage{xparse}

\newcommand{\block}[2][0,0]{%
\node[draw,minimum height=1cm,minimum width=3cm] (#2) at (#1) {#2};
}

%\newcommandx{\connector}[4][1=s,2=black]{% (s,hv,vh,bl,br), arrows and line options, start, end
\DeclareDocumentCommand\connector{ O{s} m O{->} m }{
\ifthenelse{\equal{#1}{s}}{\draw[#3] (#2) -- (#4);}{}
\ifthenelse{\equal{#1}{hv}}{\draw[#3] (#2) -| (#4);}{}
\ifthenelse{\equal{#1}{vh}}{\draw[#3] (#2) |- (#4);}{}
\ifthenelse{\equal{#1}{bl}}{\draw[#3] (#2) to[bend left=45] (#4);}{}
\ifthenelse{\equal{#1}{br}}{\draw[#3] (#2) to[bend right=45] (#4);}{}
}

\newcommand{\smallblock}[2][0,0]{
\node[draw,minimum height=1.5cm,minimum width=3.5cm] (#2) at (#1) {#2};
\node[above right,minimum height=0.5cm,minimum width=1cm] (#2_1) at (#2.north west) {};
\draw (#2_1.south west) -- (#2_1.north west) -- (#2_1.north east) -- (#2_1.south east);
}

\newcommand{\bigblock}[2]{
\node[draw,minimum height=2.5cm,minimum width=4.5cm,fit=#1,inner sep=1cm] (#2) {};
\node[above right,minimum height=0.5cm,minimum width=1cm] (#2_1) at (#2.north west) {#2};
\draw (#2_1.south west) -- (#2_1.north west) -- (#2_1.north east) -- (#2_1.south east);
}

\begin{document}

\begin{tikzpicture}
    \smallblock{First Block}
    \smallblock[5,0]{Second Block}
    \smallblock[5,-3]{Third Block}
    \smallblock[0,9]{Fourth Block}
    \smallblock[5,9]{Fifth Block}
    \smallblock[5,6]{Sixth Block}
    \smallblock[5,12]{Seventh Block}
    \connector{First Block}[->]{Second Block}
    \connector[hv]{Third Block}[latex-]{First Block}
    \connector{Fourth Block}{Fifth Block}
    \connector[br]{Fourth Block}[<->,dotted,thick]{Sixth Block}
    \connector[bl]{Fourth Block}[latex'-stealth,red,ultra thick]{Seventh Block}
    \bigblock{(First Block)(Second Block)(Third Block)}{First Group}
    \bigblock{(Fourth Block)(Fifth Block)(Sixth Block)(Seventh Block)}{Second Group}
    \connector{First Group}[latex-latex,blue,thick]{Second Group}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容