I am a beginner in LaTeX and TikZ, trying to make industrial documents, containing a series of terminal block whose wiring is shown in the MWE. I have to make a sereis of these dwawings. The envelope is the same, but the size of the pins, the number of row and the spacing between rows changes. I have tried to put the useful parameters in a macro, in order to easily generate each configuration. However I could not solve the last step, ie automatically calculating the last numbers such as the number 12, in the foreach statements, ie #3-1, which is the number of cavities of the row containing an even number. I could not find a way either to calculate the numbers of the last pins of each row, ie 25, 38,50, that are needed to start the numbering of the next rows. I doubt also that this code is optimized, and would welcome any tip to improve it.
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\newcommand{\DrawTerminalBlock}[5]{
\begin{tikzpicture}[scale=#1, thick,
mynode/.style={circle, draw, very thin, minimum size=2.6mm, text centered,font=\tiny, inner sep=0.5pt},
]
\coordinate (O) at (0,0) ;
\coordinate (A) at (1.495,0) ;
\coordinate (B) at (1.495, 0.684) ;
\coordinate (C) at (1.309,.870) ;
\coordinate (D) at (0.208,0.870) ;
\coordinate (E) at (0,0.684) ;
\draw (O) --(A) --(B)--( C)-- (D) --(E) --(O)--cycle; %Draw outline
% #1 Scale
% #2 Horizontal spacing between cavities
% #3 Number of horizontal cavities long rows (odd number of cavities)
% #4 Vertical distance to lower (bottom) row
% #5 Vertical spacing between rows
\foreach \x in {1,...,#3}
{
\node[mynode,] at (#2*\x, #4) {\x};
}
\foreach \x in {1,...,12}
{\pgfmathparse{int(#3+\x)}
\edef\n{\pgfmathresult}
\node[mynode,] at (0.5*#2+#2*\x,#4+#5) {\n};
}
\foreach \x in {1,...,#3}
{\pgfmathparse{int(25+\x)}
\edef\n{\pgfmathresult}
\node[mynode,] at (#2*\x,#4+2*#5) {\n};
}
\foreach \x in {1,...,12}
{\pgfmathparse{int(38+\x)}
\edef\n{\pgfmathresult}
\node[mynode,] at (0.5*#2+#2*\x,#4+3*#5) {\n};
}
\foreach \x in {1,...,#3}
{\pgfmathparse{int(50+\x)}
\edef\n{\pgfmathresult}
\node[mynode,] at (#2*\x,#4+4*#5) {\n};
}
\foreach \x in {1,...,12}
{\pgfmathparse{int(63+\x)}
\edef\n{\pgfmathresult}
\node[mynode,] at (0.5*#2+#2*\x,#4+5*#5) {\n};
}
\end{tikzpicture}
}
\DrawTerminalBlock{4}{.108}{13}{0.1}{0.1}
\end{document}


