tikz-uml 在序列图中删除对象

tikz-uml 在序列图中删除对象

我希望有人能帮助我。我用 tikz-uml 创建了一个对象。我想删除它,但我不知道该怎么做。我想要一个同步箭头,就像 create 上的箭头一样,然后在对象的生命线上有一个“x”。此示例代码应以 b 生命线上的“x”上的 delete 结尾。

\documentclass[a4paper, fontsize=12pt]{scrartcl}
\usepackage{ifthen}
\usepackage{xstring}
\usepackage{pgfkeys}
\usepackage{pgfopts}
\usepackage{tikz-uml}

\begin{document}
\begin{tikzpicture}
\begin{umlseqdiag}
\umlobject[class=A]{a}
\umlcreatecall [ class=B] {a}{b}
\begin{umlcall}
[op={function()},dt=6, type=synchron, return={return}]{a}{b}
\end{umlcall}
\begin{umlcall}
[op={delete}, dt=4, type=synchron, ]{a}{b}
\end{umlcall}
\end{umlseqdiag}
\end{tikzpicture}
\end{document}

答案1

希望对你有帮助。将修改后的代码粘贴到文件“tikz-uml.sty”的最后一行中。

\newenvironment{umlcallKill}[3][]
{
    \stepcounter{tikzumlCallNum}
    \edef\tikzumlCall@lastchildNum{\thetikzumlCallNum}
    \gdef\tikzumlCallBottom{0}
    \pgfkeys{/tikzuml/call/.cd, 
            dt/.initial=\tikzumlCallDefaultDT, 
            name/.initial={call-\thetikzumlCallNum},
                        op/.initial={},
                        .unknown/.code=
                        {
                            \let\keyname=\pgfkeyscurrentname
                                \let\keyvalue=\pgfkeyscurrentvalue
                                \errmessage{TIKZUML ERROR : in umlcallKill,  invalid option \keyname}
                        }
    }
    \pgfkeys{/tikzuml/call/.cd, #1}
    \pgfkeys{/tikzuml/call/.cd, dt/.get=\tikzumlCallDT, op/.get=\tikzumlCallOp}
  
    \let\tikzumlCallStartNodeNameold\tikzumlCallStartNodeName
    \def\tikzumlCallStartNodeName{#2}
    \let\tikzumlCallEndNodeNameold\tikzumlCallEndNodeName
    \def\tikzumlCallEndNodeName{#3}
    \edef\tikzumlCall@parent{}
    \edef\tikzumlCall@parentold{}
    \edef\tikzumlCall@nameold{}
    \edef\tikzumlCall@name{call-\thetikzumlCallNum}

    \begingroup
    \def\_{_}\edef\x{\endgroup
        \def\noexpand\tikzumlCall@nodeName{\tikzumlCall@name}}\x
    \let\tikzumlCall@nodeNameold\tikzumlCall@nodeName
    % top node of activity period of call sender
    \begin{pgfonlayer}{connections}
        \pgfmathparse{\tikzumlCallDT}
        \draw (\csname tikzumlLastChild@\tikzumlCallStartNodeName \endcsname)+(0,-\pgfmathresult ex) node[coordinate, name=tikzumlTmpNode] {};
        \node[tikzuml activity style] (st-\tikzumlCall@nodeName) at (\tikzumlCallStartNodeName |- tikzumlTmpNode) {};
        % update last node drawn on sender lifeline
        \expandafter\xdef\csname tikzumlLastChild@\tikzumlCallStartNodeName \endcsname{st-\tikzumlCall@nodeName}
        \node[tikzuml activity style, draw=none] (et-\tikzumlCall@nodeName) at (\tikzumlCallEndNodeName |- st-\tikzumlCall@nodeName) {};
        % update last node drawn on receiver lifeline
        \expandafter\xdef\csname tikzumlLastChild@\tikzumlCallEndNodeName \endcsname{et-\tikzumlCall@nodeName}
        \xdef\tikzumlCallBottomSrc{et-\tikzumlCall@nodeName}%
    \end{pgfonlayer}
    \stepcounter{tikzumlCallLevel}
}{
    \addtocounter{tikzumlCallLevel}{-1}
    % bottom nodes of activity periods of call sender and receiver
    \begin{pgfonlayer}{connections}
            \draw (\tikzumlCallBottomSrc)+(0, -0.2 ex) node[coordinate, name=tikzumlTmpNode] {};
            \node[cross out, draw=black, line width=1pt, minimum size=12pt, inner sep=0pt, outer sep=0pt] (eb-\tikzumlCall@nodeName) at (\tikzumlCallEndNodeName |- tikzumlTmpNode) {};
            \node[tikzuml activity style] (sb-\tikzumlCall@nodeName) at (\tikzumlCallStartNodeName |- eb-\tikzumlCall@nodeName) {};
            \xdef\tikzumlCallBottomSrc{sb-\tikzumlCall@nodeName}
    \end{pgfonlayer}
    % draw activity periods
    \begin{pgfonlayer}{activity}
            % draw root activity period
            \draw[draw=\tikzumlDefaultDrawColor, fill=\tikzumlCallDefaultFillColor] (st-\[email protected] west) rectangle (sb-\[email protected] east);
            % Do not draw receiver activity period
            \draw[draw=none] (et-\[email protected] west) rectangle (eb-\[email protected] east);
    \end{pgfonlayer}
    % update last nodes drawn on sender and receiver lifelines
    \expandafter\xdef\csname tikzumlLastChild@\tikzumlCallEndNodeName \endcsname{eb-\tikzumlCall@nodeName}
    \expandafter\xdef\csname tikzumlLastChild@\tikzumlCallStartNodeName \endcsname{sb-\tikzumlCall@nodeName}
    % draw call arrows
    \begin{pgfonlayer}{connections}
            \pgfextractx{\tikzumlCall@xa}{\pgfpointanchor{\tikzumlCallStartNodeName}{center}}
            \pgfextractx{\tikzumlCall@xb}{\pgfpointanchor{\tikzumlCallEndNodeName}{center}}
            \ifthenelse{\tikzumlCall@xb>\tikzumlCall@xa}
            {
                \draw[tikzuml synchron-msg style, \tikzumlDefaultDrawColor] (st-\[email protected]) -- (et-\[email protected]) 
                node[font=\tikzumlDefaultFont, text=\tikzumlDefaultTextColor, midway, above=-0.4ex, name=\tikzumlCall@nodeName-op] {\tikzumlCallOp};
            }
            {
                \draw[tikzuml synchron-msg style, \tikzumlDefaultDrawColor] (st-\[email protected]) -- (et-\[email protected]) 
                node[font=\tikzumlDefaultFont, text=\tikzumlDefaultTextColor, midway, above=-0.4ex, name=\tikzumlCall@nodeName-op] {\tikzumlCallOp};
            }
    \end{pgfonlayer}
}

现在您可以按如下方式调用所需的解决方案:

\begin{umlcallKill}[dt=3,op={functionName()}]{instanceNameSrc}{instanceNameDes}
\end{umlcallKill}

重要的是,您只输入“dt=”和“op={}”,但必须指定它们。自调用不起作用。然后您将得到以下内容:

示例图像

示例代码:

\documentclass[border=1]{standalone}
\usepackage[english]{babel}
\usepackage{tikz-uml}
\begin{document}
    \begin{tikzpicture}[framed]
        \tikzumlset{fill object = white, fill call = gray!20} 
        \begin{umlseqdiag}
            % class instances
            \umlobject[class=A]{a}
            \umlobject[class=B]{b}
            
            \begin{umlcallKill}[dt=3,op={functionName()}]{a}{b}
            \end{umlcallKill}
            
        \end{umlseqdiag}
    \end{tikzpicture}   
\end{document}

相关内容