TikZ 节点内的坐标

TikZ 节点内的坐标

我有一个文档,其中包含本质上相同的图像的多个变体,并且使用这个问题和答案,我能够使事情简化很多。

当我画画的时候我通常使用许多坐标有助于将各个部分连接在一起。我刚刚注意到,节点内指定的坐标似乎可以在节点外部访问,这对我的绘图风格非常有帮助,但我不知道这是 TikZ 的预期功能,还是一个有用的技巧,但会带来难以调试的麻烦。我真的希望是前者,因为这种技术使绘图变得非常简单。

这是一个平均能量损失来说明我的意思。我删除了所有不需要的包和代码,但绘图本身仍然相当复杂,因为我想说明图片中命名节点的数量和坐标,无论将其放置在何处都可以访问(前提是没有名称冲突)。

\RequirePackage{luatex85}
\documentclass[tikz]{standalone}

\usepackage{tikz}

\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usepgflibrary{arrows.meta}

\usepackage{siunitx}
\sisetup{detect-all, per-mode=symbol}
\DeclareSIPostPower\fourth{4}


\tikzset{%
  % End view, "real" x-axis points directly out of page. Projects end view of
  % view top left/right systems
  view end yz/.style={ 
    x={(0cm,1cm)},     % Simulated y-axis (actually x-axis)
    y={(1cm,0cm)},    % Simulated z-axis (actually y-axis)
  },
  dim edge/.style={densely dashed, line width=0.5pt, shorten >= -2.5mm, color=black!75!white},
  dim length/.style={draw, line width=0.5pt, arrows={Stealth-Stealth}, every node/.style={anchor=mid, midway, fill=white, node font=\footnotesize, inner sep=1pt},
  },
}

\begin{document}

\pgfkeys{/tikz/.cd,
  cross section/.pic={
    \path
      (0.1, 0) coordinate (bottom-left)
      -- ++(0, 1.5) coordinate (bottom-right)
      -- ++(1.5, 0) coordinate (top-right)
      -- ++(0, -1.5) coordinate (top-left)
      -- cycle;

    \fill[draw]
      (top-left)
      -- (top-right)
      -- ++(0.1, 0) coordinate (flange-top-right)
      -- ($(top-left) + (0.1, 0)$) coordinate (flange-top-left)
      -- (top-left)
      -- cycle
      (bottom-left)
      -- ++(-0.1, 0) coordinate (flange-bottom-left)
      -- ++(0, 0.5) coordinate (flange-bottom-inner-left)
      -- ++(0.1, 0) coordinate (flange-top-inner-left)
      -- cycle
      (bottom-right)
      -- ++(-0.1, 0) coordinate (flange-bottom-right)
      -- ++(0, -0.5) coordinate (flange-bottom-inner-right)
      -- ++(0.1, 0) coordinate (flange-top-inner-right)
      -- cycle;

    \fill[draw]
      ($(bottom-left)!0.5!(flange-top-inner-left) - (0, 0.03)$) coordinate (left-web-bottom-left)
      -- ($(bottom-left)!0.5!(flange-top-inner-left) + (1.5, -0.03)$) coordinate (left-web-top-left)
      -- ($(bottom-left)!0.5!(flange-top-inner-left) + (1.5, 0.03)$) coordinate (left-web-top-right)
      -- ($(bottom-left)!0.5!(flange-top-inner-left) + (0, 0.03)$) coordinate (left-web-bottom-right)
      -- cycle
      ($(bottom-right)!0.5!(flange-top-inner-right) - (0, 0.03)$) coordinate (right-web-bottom-left)
      -- ($(bottom-right)!0.5!(flange-top-inner-right) + (1.5, -0.03)$) coordinate (right-web-top-left)
      -- ($(bottom-right)!0.5!(flange-top-inner-right) + (1.5, 0.03)$) coordinate (right-web-top-right)
      -- ($(bottom-right)!0.5!(flange-top-inner-right) + (0, 0.03)$) coordinate (right-web-bottom-right)
      -- cycle;

  }
}

\begin{tikzpicture}

  \begin{scope}[view end yz, name prefix=end-]
    \fill[blue!60!white, draw=blue]
      (0.1, 0) coordinate (bottom-left)
      -- ++(0, 1.5) coordinate (bottom-right)
      -- ++(1.5, 0) coordinate (top-right)
      -- ++(0, -1.5) coordinate (top-left)
      -- cycle;

    \pic[fill=blue!40!white, draw=blue] at (0, 0){cross section};

    \draw[dim edge] (flange-bottom-left) -- ++(-0.25, 0) coordinate (dim-left-flange-left);
    \draw[dim edge] (flange-bottom-inner-left) -- ++(-0.25, 0) coordinate (dim-left-flange-inner);
    \draw[dim edge] (flange-bottom-inner-right) -- ++(-0.25, 0) coordinate (dim-right-flange-inner);
    \draw[dim edge] (flange-bottom-right) -- ++(-0.25, 0) coordinate (dim-right-flange-right);

    \draw[dim edge] (flange-top-left) -- ++(0.25, 0) coordinate (dim-top-flange-left);
    \draw[dim edge] (flange-top-right) -- ++(0.25, 0) coordinate (dim-top-flange-right);

    \draw[dim length] (dim-left-flange-left) -- (dim-left-flange-inner) node[below=0.1] {\SI{25}{\mm}};
    \draw[dim length] (dim-right-flange-right) -- (dim-right-flange-inner) node[below=0.1] {\SI{25}{\mm}};

    \draw[dim length] (dim-top-flange-left) -- (dim-top-flange-right) node[above=0.1] {\SI{75}{\mm}};

  \end{scope}

  \begin{scope}[xshift=2cm, view end yz, name prefix=centre-]

    \pic[fill=blue!40!white, draw=blue] at (0, 0){cross section};

    \draw[dim edge] ($(left-web-bottom-left) - (0.1, 0)$) -- ++(-0.5, 0) coordinate (dim-left-web-left);
    \draw[dim edge] ($(left-web-bottom-right) - (0.1, 0)$) -- ++(-0.5, 0) coordinate (dim-left-web-right);
    \draw[dim edge] ($(right-web-bottom-left) - (0.1, 0)$) -- ++(-0.5, 0) coordinate (dim-right-web-left);
    \draw[dim edge] ($(right-web-bottom-right) - (0.1, 0)$) -- ++(-0.5, 0) coordinate (dim-right-web-right);

    \draw[dim length, arrows={Stealth-}] (dim-left-web-left) -- ++(0, -0.25);
    \draw[dim length, arrows={Stealth-}] (dim-right-web-right) -- ++(0, 0.25);
    \draw[dim length] (dim-left-web-right) -- (dim-right-web-left) node[above=0.1] {\SI{47}{\mm}};
    \draw[dim length, draw=none] (dim-left-web-left) -- (dim-left-web-right) node[below=0.1] {\SI{3}{\mm}};
    \draw[dim length, draw=none] (dim-right-web-left) -- (dim-right-web-right) node[below=0.1] {\SI{3}{\mm}};

  \end{scope}

  \begin{scope}[yshift=3cm, view end yz, transform shape, scale=2, name prefix=big-]
    \fill[red!60!white, draw=red]
      (0.1, 0) coordinate (bottom-left)
      -- ++(0, 1.5) coordinate (bottom-right)
      -- ++(1.5, 0) coordinate (top-right)
      -- ++(0, -1.5) coordinate (top-left)
      -- cycle;

    \pic[fill=red!40!white, draw=red] at (0, 0){cross section};

  \end{scope}

  \draw[dim edge] (big-flange-bottom-left) -- ++(-0.25, 0) coordinate (dim-flange-bottom-left);
  \draw[dim edge] (big-bottom-left) -- ++(-0.25, 0) coordinate (dim-web-bottom);
  \draw[dim edge] (big-top-left) -- ++(-0.25, 0) coordinate (dim-web-top);
  \draw[dim edge] (big-flange-top-left) -- ++(-0.25, 0) coordinate (dim-flange-top-left);

  \draw[dim length] (dim-web-bottom) -- (dim-web-top) node[left=0.1] {\SI{75}{\mm}};
  \draw[dim length, arrows={Stealth-}] (dim-flange-bottom-left) -- ++(0, -0.25);
  \draw[dim length, arrows={Stealth-}] (dim-flange-top-left) -- ++(0, 0.25);
  \draw[dim length, draw=none] (dim-flange-bottom-left) -- (dim-web-bottom) node[left=0.1] {\SI{5}{\mm}};
  \draw[dim length, draw=none] (dim-web-top) -- (dim-flange-top-left) node[left=0.1] {\SI{5}{\mm}};

\end{tikzpicture}

\end{document}

相关内容