客製化線條風格和客製化裝飾

客製化線條風格和客製化裝飾

我想創建一個 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具有不同線寬和顏色的鍵來繪製管道(以及法蘭)。第一種方法是使用markings裝飾作為postaction定位法蘭的位置。第二個使用裝飾將凸緣定位在和段show path construction的開頭和結尾。linetocurveto

\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}

在此輸入影像描述

相關內容