自定义线条风格,自定义装饰

自定义线条风格,自定义装饰

我想创建一个法兰版本的 3D 管道,看起来像原帖中的图片我的“强力”尝试如下:

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture} 
\definecolor{LPtgrey}{rgb}{.7,.7,.7}
\def\pipesize{1cm}
\def\flangesize{1.75*\pipesize}
\def\ft{0.2} % for flange thickness
\def\fg{0.02} % for flange gap
\def\xx{0.5} % length of middle of bend to inside edge of flange, ie, same as bend radius
\pgfsetcornersarced{\pgfpoint{0.5cm}{0.5cm}}
%pipe
\draw[color=LPtgrey!80!black, line width=\pipesize*0.4] (2,0)--(2,6)--(5,6)--(7,4)--(9,4);%
\draw[color=LPtgrey, line width=\pipesize*0.38] (2,0)--(2,6)--(5,6)--(7,4)--(9,4);%
\draw[color=LPtgrey!85, line width=\pipesize*0.35] (2,0)--(2,6)--(5,6)--(7,4)--(9,4);%
\draw[color=LPtgrey!70, line width=\pipesize*0.32] (2,0)--(2,6)--(5,6)--(7,4)--(9,4);%
\draw[color=LPtgrey!55, line width=\pipesize*0.29] (2,0)--(2,6)--(5,6)--(7,4)--(9,4);%
\draw[color=LPtgrey!45, line width=\pipesize*0.25] (2,0)--(2,6)--(5,6)--(7,4)--(9,4);%
\draw[color=LPtgrey!35, line width=\pipesize*0.20] (2,0)--(2,6)--(5,6)--(7,4)--(9,4);%
%flange 1
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (2,0)--(2,0+\ft);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (2,0)--(2,0+\ft);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (2,0)--(2,0+\ft);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (2,0)--(2,0+\ft);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (2,0)--(2,0+\ft);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (2,0)--(2,0+\ft);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (2,0)--(2,0+\ft);%
%flange 2
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (2,6-\xx-\ft-\fg-\ft)--(2,6-\xx-\ft-\fg);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (2,6-\xx-\ft-\fg-\ft)--(2,6-\xx-\ft-\fg);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (2,6-\xx-\ft-\fg-\ft)--(2,6-\xx-\ft-\fg);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (2,6-\xx-\ft-\fg-\ft)--(2,6-\xx-\ft-\fg);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (2,6-\xx-\ft-\fg-\ft)--(2,6-\xx-\ft-\fg);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (2,6-\xx-\ft-\fg-\ft)--(2,6-\xx-\ft-\fg);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (2,6-\xx-\ft-\fg-\ft)--(2,6-\xx-\ft-\fg);%
%flange 3
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (2,6-\xx-\ft)--(2,6-\xx);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (2,6-\xx-\ft)--(2,6-\xx);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (2,6-\xx-\ft)--(2,6-\xx);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (2,6-\xx-\ft)--(2,6-\xx);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (2,6-\xx-\ft)--(2,6-\xx);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (2,6-\xx-\ft)--(2,6-\xx);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (2,6-\xx-\ft)--(2,6-\xx);%
%flange 4
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (2+\xx,6)--(2+\xx+\ft,6);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (2+\xx,6)--(2+\xx+\ft,6);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (2+\xx,6)--(2+\xx+\ft,6);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (2+\xx,6)--(2+\xx+\ft,6);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (2+\xx,6)--(2+\xx+\ft,6);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (2+\xx,6)--(2+\xx+\ft,6);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (2+\xx,6)--(2+\xx+\ft,6);%
%flange 5
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (2+\xx+\ft+\fg,6)--(2+\xx+\ft+\fg+\ft,6);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (2+\xx+\ft+\fg,6)--(2+\xx+\ft+\fg+\ft,6);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (2+\xx+\ft+\fg,6)--(2+\xx+\ft+\fg+\ft,6);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (2+\xx+\ft+\fg,6)--(2+\xx+\ft+\fg+\ft,6);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (2+\xx+\ft+\fg,6)--(2+\xx+\ft+\fg+\ft,6);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (2+\xx+\ft+\fg,6)--(2+\xx+\ft+\fg+\ft,6);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (2+\xx+\ft+\fg,6)--(2+\xx+\ft+\fg+\ft,6);%
%flange 6
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (5-\xx-\ft-\fg-\ft,6)--(5-\xx-\ft-\fg,6);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (5-\xx-\ft-\fg-\ft,6)--(5-\xx-\ft-\fg,6);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (5-\xx-\ft-\fg-\ft,6)--(5-\xx-\ft-\fg,6);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (5-\xx-\ft-\fg-\ft,6)--(5-\xx-\ft-\fg,6);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (5-\xx-\ft-\fg-\ft,6)--(5-\xx-\ft-\fg,6);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (5-\xx-\ft-\fg-\ft,6)--(5-\xx-\ft-\fg,6);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (5-\xx-\ft-\fg-\ft,6)--(5-\xx-\ft-\fg,6);%
%flange 7
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (5-\xx-\ft,6)--(5-\xx,6);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (5-\xx-\ft,6)--(5-\xx,6);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (5-\xx-\ft,6)--(5-\xx,6);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (5-\xx-\ft,6)--(5-\xx,6);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (5-\xx-\ft,6)--(5-\xx,6);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (5-\xx-\ft,6)--(5-\xx,6);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (5-\xx-\ft,6)--(5-\xx,6);%
%flange 8
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (5+\xx,6-\xx)--(5+\xx+\ft,6-\xx-\ft);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (5+\xx,6-\xx)--(5+\xx+\ft,6-\xx-\ft);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (5+\xx,6-\xx)--(5+\xx+\ft,6-\xx-\ft);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (5+\xx,6-\xx)--(5+\xx+\ft,6-\xx-\ft);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (5+\xx,6-\xx)--(5+\xx+\ft,6-\xx-\ft);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (5+\xx,6-\xx)--(5+\xx+\ft,6-\xx-\ft);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (5+\xx,6-\xx)--(5+\xx+\ft,6-\xx-\ft);%
%flange 9
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (5+\xx+\ft+\fg,6-\xx-\ft-\fg)--(5+\xx+\ft+\fg+\ft,6-\xx-\ft-\fg-\ft);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (5+\xx+\ft+\fg,6-\xx-\ft-\fg)--(5+\xx+\ft+\fg+\ft,6-\xx-\ft-\fg-\ft);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (5+\xx+\ft+\fg,6-\xx-\ft-\fg)--(5+\xx+\ft+\fg+\ft,6-\xx-\ft-\fg-\ft);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (5+\xx+\ft+\fg,6-\xx-\ft-\fg)--(5+\xx+\ft+\fg+\ft,6-\xx-\ft-\fg-\ft);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (5+\xx+\ft+\fg,6-\xx-\ft-\fg)--(5+\xx+\ft+\fg+\ft,6-\xx-\ft-\fg-\ft);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (5+\xx+\ft+\fg,6-\xx-\ft-\fg)--(5+\xx+\ft+\fg+\ft,6-\xx-\ft-\fg-\ft);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (5+\xx+\ft+\fg,6-\xx-\ft-\fg)--(5+\xx+\ft+\fg+\ft,6-\xx-\ft-\fg-\ft);%
%flange 10
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (7-\xx-\ft-\fg-\ft,4+\xx+\ft+\fg+\ft)--(7-\xx-\ft-\fg,4+\xx+\ft+\fg);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (7-\xx-\ft-\fg-\ft,4+\xx+\ft+\fg+\ft)--(7-\xx-\ft-\fg,4+\xx+\ft+\fg);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (7-\xx-\ft-\fg-\ft,4+\xx+\ft+\fg+\ft)--(7-\xx-\ft-\fg,4+\xx+\ft+\fg);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (7-\xx-\ft-\fg-\ft,4+\xx+\ft+\fg+\ft)--(7-\xx-\ft-\fg,4+\xx+\ft+\fg);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (7-\xx-\ft-\fg-\ft,4+\xx+\ft+\fg+\ft)--(7-\xx-\ft-\fg,4+\xx+\ft+\fg);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (7-\xx-\ft-\fg-\ft,4+\xx+\ft+\fg+\ft)--(7-\xx-\ft-\fg,4+\xx+\ft+\fg);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (7-\xx-\ft-\fg-\ft,4+\xx+\ft+\fg+\ft)--(7-\xx-\ft-\fg,4+\xx+\ft+\fg);%
%flange 11
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (7-\xx-\ft,4+\xx+\ft)--(7-\xx,4+\xx);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (7-\xx-\ft,4+\xx+\ft)--(7-\xx,4+\xx);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (7-\xx-\ft,4+\xx+\ft)--(7-\xx,4+\xx);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (7-\xx-\ft,4+\xx+\ft)--(7-\xx,4+\xx);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (7-\xx-\ft,4+\xx+\ft)--(7-\xx,4+\xx);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (7-\xx-\ft,4+\xx+\ft)--(7-\xx,4+\xx);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (7-\xx-\ft,4+\xx+\ft)--(7-\xx,4+\xx);%
%flange 12
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (7+\xx,4)--(7+\xx+\ft,4);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (7+\xx,4)--(7+\xx+\ft,4);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (7+\xx,4)--(7+\xx+\ft,4);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (7+\xx,4)--(7+\xx+\ft,4);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (7+\xx,4)--(7+\xx+\ft,4);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (7+\xx,4)--(7+\xx+\ft,4);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (7+\xx,4)--(7+\xx+\ft,4);%
%flange 13
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (7+\xx+\ft+\fg,4)--(7+\xx+\ft+\fg+\ft,4);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (7+\xx+\ft+\fg,4)--(7+\xx+\ft+\fg+\ft,4);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (7+\xx+\ft+\fg,4)--(7+\xx+\ft+\fg+\ft,4);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (7+\xx+\ft+\fg,4)--(7+\xx+\ft+\fg+\ft,4);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (7+\xx+\ft+\fg,4)--(7+\xx+\ft+\fg+\ft,4);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (7+\xx+\ft+\fg,4)--(7+\xx+\ft+\fg+\ft,4);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (7+\xx+\ft+\fg,4)--(7+\xx+\ft+\fg+\ft,4);%
%flange 14
\draw[color=LPtgrey!80!black, line width=\flangesize*0.4] (9-\ft,4)--(9,4);%
\draw[color=LPtgrey, line width=\flangesize*0.38] (9-\ft,4)--(9,4);%
\draw[color=LPtgrey!85, line width=\flangesize*0.35] (9-\ft,4)--(9,4);%
\draw[color=LPtgrey!70, line width=\flangesize*0.32] (9-\ft,4)--(9,4);%
\draw[color=LPtgrey!55, line width=\flangesize*0.29] (9-\ft,4)--(9,4);%
\draw[color=LPtgrey!45, line width=\flangesize*0.25] (9-\ft,4)--(9,4);%
\draw[color=LPtgrey!35, line width=\flangesize*0.20] (9-\ft,4)--(9,4);%
\end{tikzpicture}
\end{document}

结果还不错:

法兰管

强力旋转 90 度角没问题,但 45 度角的凸缘太厚。其他角度需要太多三角学来计算凸缘位置。

  1. 必须有一种方法来定义“线条样式”,它是 5 条重叠线条的组合(我不知道如何定义)。

  2. 必须有一种方法来定义自定义装饰,该装饰将终止每条线(或折线的每个节点)以获得所需的结果。装饰会旋转到与线相同的角度吗?

理想情况下,我想使用如下代码:

\draw[pipe,style=flange,diameter=3cm](2,0)--(2,6)--(5,6);

并且所有与法兰放置相关的数学计算都将得到处理。我将在不久的将来绘制大量管道图,并感谢任何有助于简化此过程的有用代码。

答案1

这里演示了两种方法。两种方法都重复使用preaction具有不同线宽和颜色的键来绘制管道(以及法兰)。第一种方法使用装饰markingspostaction定位法兰。第二种方法使用show path construction装饰将法兰定位在段的开头和lineto结尾curveto

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.markings,decorations.pathreplacing,calc}
\makeatletter
\tikzset{every pipe/.code={%
  \pgfmathloop
  \ifnum\pgfmathcounter>20
  \else
    \pgfmathsetmacro\i{\pgfmathcounter}%
    \pgfmathsetmacro\j{80-\pgfmathcounter*3}%
    \tikzset{preaction/.expanded={line width=(\pipewidth)*(1-\i*\i/500),
        draw=black!50!white!\j!white}}%
  \repeatpgfmathloop
},
every flange/.style 2 args={insert path={
  let \p1=(#1), \p2=(#2),\n1={atan2(\y2-\y1, \x2-\x1)} in
    [shift={(\p1)}, rotate=\n1, pipe width={(\pipewidth)*4/3}, every pipe] 
      (0.01,0) -- (0.2,0)
}},
flanges/.style={postaction={%
  /utils/exec={
    \@for\pos:=#1\do{%
      \tikzset{decoration/.expanded={markings,
        mark=at position \pos\space with {
          \noexpand\path [pipe width=\pipewidth*4/3, every pipe] (0.01,0) -- (0.2,0);
    }}}}}, 
  decorate}},
pipe width/.code=\pgfmathsetlengthmacro\pipewidth{#1},
pipes/.style={
  every pipe,
  postaction={decoration={show path construction,
    lineto code={
      \path [every flange={\tikzinputsegmentfirst}{\tikzinputsegmentlast}];
      \path [every flange={\tikzinputsegmentlast}{\tikzinputsegmentfirst}];
    },
    curveto code={
      \path [every flange={\tikzinputsegmentfirst}{\tikzinputsegmentsupporta}];
      \path [every flange={\tikzinputsegmentlast}{\tikzinputsegmentsupportb}];
    }
}, decorate}},
pipe/.style={
  every pipe,
  flanges={0, 1}
}}
\begin{document}
\begin{tikzpicture}[line join=round, pipe width=0.5cm]
\path [pipes] (0,4) -- (0,1) arc (180:330:1) (-1,3) -- (1,3);
\path [pipes] (3,-1/3) -- (3,2);
\path [pipes] (4,-1/3) -- (4,4);
\path [pipe]  (11/2,3) -- (4,5/4) -- (11/2,0);
\path [pipe]  (13/2,5/2) -- (8,5/2) -- (13/2,0) -- (8,0);
\path [pipes] (4,2) ellipse [x radius=6, y radius=4];
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容