Re: tikz creare macro

#79361
Up
0
Down
::

warcomeb” post=78565Ho provato a fare qualche modifica e funziona:

`
\newcommand{\bigpicsize}[1]{\footnotesize{#1}}
\newcommand{\picsize}[1]{\scriptsize{#1}}
\newcommand{\picsizett}[1]{\scriptsize{\texttt{#1}}}
\newcommand{\smallpicsize}[1]{\tiny{#1}}

\pgfmathtruncatemacro{\classwidth}{3}
\pgfmathtruncatemacro{\classheight}{2}
\newcommand{\setclasswidth}[1]{\pgfmathtruncatemacro{\classwidth}{#1}}
\newcommand{\setclassheight}[1]{\pgfmathtruncatemacro{\classheight}{#1}}

\tikzset{class/.style args={#1 and #2}{
rectangle,draw,rounded corners,thick,minimum width=#1, minimum height=#2
}
}

\tikzset{classinside/.code n args={2}{%
\node at ($(#1.north)!0.20!(#1.center)$){#2};
\draw($(#1.north west)!0.50!(#1.west)$)–($(#1.north east)!0.50!(#1.east)$);
}
}

% DRAWCLASS
% #1 – Coordinate
% #2 – Label
% #3 – Nome Classe
\NewDocumentCommand{\drawclass}{d() m m}{
\IfNoValueTF{#1}{%true
\node[class=\classwidth cm and \classheight cm,name=#2]{};
}
{%false
\node[class=\classwidth cm and \classheight cm,name=#2]at(#1){};
}
\node[classinside={#2}{#3}]{};
}
`

e lo uso in questo modo (in pratica uguale a come lo hai scritto tu, solo che io aggiungo la label:
`\drawclass{m1}{sc\_module}`

ma ho due domande:
1) il nome della classe e la linea verticale che gli sta sotto vorrei che stessero sempre alla stessa distanza e che non vari se io allargo il rettangolo sia in altezza che in larghezza! è sensato mettere una misura assoluta? ma come?
2) il contenuto all’interno di ogni classe, quali modi ci sono per aggiungerlo?

grazie mille
Marco

Prova così:

`\documentclass[a4paper,11pt]{article}
\usepackage{tikz,xparse}
\usetikzlibrary{calc,positioning}

\newcommand{\bigpicsize}[1]{\footnotesize{#1}}
\newcommand{\picsize}[1]{\scriptsize{#1}}
\newcommand{\picsizett}[1]{\scriptsize{\texttt{#1}}}
\newcommand{\smallpicsize}[1]{\tiny{#1}}

\pgfmathtruncatemacro{\classwidth}{3}
\pgfmathtruncatemacro{\classheight}{2}
\newcommand{\setclasswidth}[1]{\pgfmathtruncatemacro{\classwidth}{#1}}
\newcommand{\setclassheight}[1]{\pgfmathtruncatemacro{\classheight}{#1}}

\tikzset{class/.style args={#1 and #2}{
rectangle,draw,rounded corners,thick,minimum width=#1, minimum height=#2
}
}

\tikzset{classinside/.code n args={3}{%
\pgfmathsetmacro\vertshift{0.2+(\classheight/80)}
\node at ($(#1.north)+(0,0.25)$){#1};
\node at ($(#1.north)!0.95*\vertshift!(#1.center)$){#2};
\draw($(#1.north west)!0.50!(#1.west)$)–($(#1.north east)!0.50!(#1.east)$);
\node[yshift=-\classheight,text width=\classwidth cm,align=center] at (#1){#3};
}
}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture,baseline=-0.5ex,xshift=-0.3cm] \node[draw,fill=black,circle,scale=0.05] (#1) {};}

% DRAWCLASS
% #1 – Coordinate
% #2 – Label
% #3 – Nome Classe
\NewDocumentCommand{\drawclass}{d() m m m}{
\IfNoValueTF{#1}{%true
\node[class=\classwidth cm and \classheight cm,name=#2]{};
}
{%false
\node[class=\classwidth cm and \classheight cm,name=#2]at(#1){};
}
\node[classinside={#2}{#3}{#4}]{};
}

\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\drawclass(0,0){m1}{sc\_module}{\vdots \\ \texttt{\tikzmark{m1pos}ps\_init()}}
\drawclass(4.5,0){m2}{sc\_module}{\vdots \\ \texttt{\tikzmark{m2pos}ps\_init()}}
\drawclass(9,0){m3}{sc\_module}{\vdots \\ \texttt{\tikzmark{m3pos}ps\_init()}}

\draw(m1pos.center)– ($(m1pos)-(0,1)$)-|(m2pos.center)– ($(m2pos)-(0,1)$)-|(m3pos.center);
\end{tikzpicture}
\vspace{6cm}

\setclassheight{4}
\setclasswidth{5}
\begin{tikzpicture}[remember picture,overlay]
\drawclass(0,0){m1}{sc\_module}{\vdots \\ \texttt{\tikzmark{m1pos}ps\_init()}}
\drawclass(5.5,0){m2}{sc\_module}{\vdots \\ \texttt{\tikzmark{m2pos}ps\_init()}}
\drawclass(11,0){m3}{sc\_module}{\vdots \\ \texttt{\tikzmark{m3pos}ps\_init()}}

\draw(m1pos.center)– ($(m1pos)-(0,3)$)-|(m2pos.center)– ($(m2pos)-(0,3)$)-|(m3pos.center);
\end{tikzpicture}
\end{document}`

Ciao
Claudio

Go to top