Индивидуальный стиль линии с индивидуальным декором

Индивидуальный стиль линии с индивидуальным декором

Я хочу создать фланцевую версию 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декор для позиционирования фланцев в начале и конце сегментов 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}

введите описание изображения здесь

Связанный контент