TikZ connect two blocks

If blocks are used inside a tikzpicture Andrew's pic solution is one option. I propose an alternative with a matrix node which simplifies the block definition. Being container style defined as a matrix of nodes

container/.style={
    matrix of nodes,
    draw, dashed,
    inner sep=1em,
    nodes={block, anchor=center},
    row sep=.3cm,
    label={[anchor=south west]above left:#1},

the block command is just:

\newcommand{\Block}[5][]{
    \matrix[container=#5,#1] (#5) {
        #2\\#3\\#4\\}; 
}

The optional parameter can be used for blocks placement while the four mandatory parameters follow original \Block definition. In this case the fourth mandatory parameter acts as matrix name and label. With this matrix definition, inner nodes are identified as matrixname-1-1|-2-1|-3-1. An example of use:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,matrix}
\usepackage{amsmath}

\tikzset{
    input/.style={coordinate},
    block/.style={
        draw, solid, fill=white!10, 
        minimum width=2cm, minimum height=.5cm,
        inner sep=.3333em},
    container/.style={
        matrix of nodes,
        draw, dashed,
        inner sep=1em,
        nodes={block, anchor=center},
        row sep=.3cm,
        label={[anchor=south west]above left:#1},
    }
}

\newcommand{\Block}[5][]{
    \matrix[container=#5,#1] (#5) {
        #2\\#3\\#4\\}; 
}

\begin{document}

\begin{tikzpicture}
\Block{Val1}{Val2}{Val3}{NameA}
\Block[right=1cm of NameA] {Val1}{Val2}{Val3}{NameB}
\draw[->] (NameA)-- coordinate (aux) (NameB);
\draw[->] (NameA-1-1)--(NameA-1-1-|aux) |-(NameB-3-1);

\Block[below right=1cm and 2cm of NameB] {Val1}{Val2}{Val3}{NameC}
\draw (NameC-3-1)-|(NameB);
\end{tikzpicture}

\end{document}

enter image description here

If blocks are drawn in its own tikzpicture and distributed over the page, remember picture option has to be used inside the tikzpicture parameters in order to allow to draw links (with another tikzpicture) between them


I would define your block using \tikzset{pics/block/.style=....}. This way you can still use your \Block macro, so that

\Block{Val1}{Val2}{Val3}{Name}
\Block{Val1}{Val2}{Val3}{Name}

will produce the image in the original question. The advantage of a pics definition is that you can also put two blocks side-by-side in the same tizkpicture environment. Further, via a parameter given to the `pic you can label the nodes in each block differently, which means that you can easily refer to them in code like

\begin{tikzpicture}
  \draw(0,0) pic{block={A,Val1,Val2,Val3,Name1}};% labels Adi, ABlockName...
  \draw(4,0) pic{block={B,Val1,Val2,Val3,Name1}};% labels Bdi, BBlockName...
  \draw[->,blue](Adi.east) -- (Bdi.west);
\end{tikzpicture}

will produce

enter image description here

Here is the full code:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{shapes,shapes.geometric,arrows,fit,calc,positioning,automata}
\usepackage{amsmath}

\tikzset{
  block/.style = {draw, fill=white!10, rectangle, minimum height=5mm, minimum width=2cm},
  container/.style = {draw, rectangle, dashed, inner sep=1em},
  pics/block/.style args={#1,#2,#3,#4,#5}{% label, val1, val2, val3, name
    code = {
        \node[block] (#1di) {#2};
        \node[block, below of=#1di, node distance = 0.6cm] (#1BlockName) {#3};
        \node[block, below of=#1BlockName, node distance = 0.6cm] (#1xi) {#4};
        \node[container, fit=(#1di)(#1BlockName)(#1xi)] (FTCBlockVal) (#1Blk){};
        \node at (#1Blk.north west) [above right,node distance=0 and 0] {#5};
     }
  }
}
%\Block{label}{val1}{val2}{val3}{name}, label empty by default
\newcommand{\Block}[5][]{%
  \begin{tikzpicture}
     \draw (0,0) pic{block={#1,#2,#3,#4,#5}};
  \end{tikzpicture}%
}

\begin{document}

  \Block{Val1}{Val2}{Val3}{Name}
  \Block{Val1}{Val2}{Val3}{Name}

  \bigskip

  \begin{tikzpicture}
    \draw(0,0) pic{block={A,Val1,Val2,Val3,Name1}};
    \draw(4,0) pic{block={B,Val1,Val2,Val3,Name1}};
    \draw[->,blue](Adi.east) -- (Bdi.west);
  \end{tikzpicture}
\end{document}

Note that I have put all of your style definitions together inside \tikzset{...}. I have also reduced the definition of block as you had different definitions for the minimum height and width in the style definitions and when they were actually used in the nodes.

Tags:

Nodes

Tikz Pgf