使用 Tikz 创建流体储存器

使用 Tikz 创建流体储存器

我想要画这个图:

在此处输入图片描述

我尝试使用以下方法:

\documentclass[11pt,a4paper]{article} 

\usepackage{xcolor}
\definecolor{lightgray}{gray}{0.9}
\usepackage{tikz}
\usetikzlibrary{calc,patterns,decorations.pathmorphing,arrows.meta,decorations.markings}
\tikzset{place/.style={  circle,
        fill=gray!20,
        thick,
        inner sep=0pt,
        minimum size=6mm},
        spring/.style={ decorate,
                        decoration={zigzag,
                                    pre length=0.3cm,
                                    post length=0.3cm,
                                    segment length=6,
                                    amplitude=6
                                    }
                      },
        damper/.style={ thick,
                        decoration={markings,
                                    mark connection node=dmp,
                                    mark=at position 0.5 with {\node (dmp) [thick,
                                                                            inner sep=0pt,
                                                                            transform shape,
                                                                            rotate=-90,
                                                                            minimum width=15pt,
                                                                            minimum height=3pt,
                                                                            draw=none] {};
                                                                \draw [thick] ($(dmp.north east)+(2.5pt,0)$) -- (dmp.south east) -- (dmp.south west) -- ($(dmp.north west)+(2pt,0)$);
                                                                \draw [thick] ($(dmp.north)+(0,-5pt)$) -- ($(dmp.north)+(0,5pt)$);
                                                                }
                                    },
                        decorate
                      },
        ground/.style={ fill,
                        pattern=north east lines,
                        draw=black,
                        minimum width=0.3cm,
                        minimum height=0.3cm
                      },
        bar/.style={fill=lightgray,
                    draw=black,
                    minimum width=6cm,
                    minimum height=0.2cm,
                    inner sep=0pt,
                    anchor=west
                   }
    }


\begin{document}


\begin{tikzpicture}[>=latex,
                    every node/.style={outer sep = 0pt,
                                       thick
                                      }
                   ]
%% Begining


\draw[thick] (0,0) -+ (6,0) node(l1){};;
\draw[thick] (0,0.5) -- (0.5,0.5) node(l2){};
\draw[thick] (l2.east) -+ ($(l2.east)+(0,2)$) node(l3){};
\draw[thick] (6,0.5) -+ (5.5,0.5) node(l4){};
\draw[thick] (l4.west) -+ ($(l4.west)+(0,2)$) node(l5){};

\node (seperation) [ground,anchor=south,minimum width=0.5cm,minimum height=2cm] at (3,0.5) {};

\draw[thick] (l1.east) -+ ($(l1.east)+(0,-2)$) node(l6){};
\draw[thick] (l6.south) -+ ($(l6.south)+(2.75,0)$) node(l7){};
\draw[thick] ($(l7.east)+(0,0.5)$) -+ ($(l7.east)+(-0.5,0.5)$) node(l8){};
\draw[thick] (l8.west) -+ ($(l8.west)+(0,2)$) node(l9){};

\draw[thick] (0,0) -+ ($(0,0)+(0,-2)$) node(l10){};
\draw[thick] (l10.south) -+ ($(l10.south)+(-2.75,0)$) node(l11){};
\draw[thick] ($(l11.west)+(0,0.5)$) -+ ($(l11.west)+(0.5,0.5)$) node(l12){};
\draw[thick] (l12.east) -+ ($(l12.east)+(0,2)$) node(l13){};

\draw[thick] (l5.north) -+ ($(l5.north)+(2.75,0)$) node(l14){};
\draw[thick] ($(l5.north)+(0,0.5)$) -+ ($(l5.north)+(0.5,0.5)$) node(l15){};
\draw[thick] (l15.east) -+ ($(l15.east)+(0,2)$) node(l16){};
\draw[thick] (l14.east) -+ ($(l14.east)+(0,2)$) node(l17){};

\end{tikzpicture}

\end{document}

在此处输入图片描述

但我有一个问题:

1.连接线

2.创建细节,例如 Q、x、R 刻度和其他标签以及水流,如下所示:

在此处输入图片描述

答案1

我认为这是pic语法的一个很好的做法

\documentclass[border=9,tikz]{standalone}
\begin{document}

\tikzset{
    pics/.cd,
    reservoir/.style={
        code={
            \fill[rounded corners,blue!50]  (-1,#1-1)--(-1,-1)--(1,-1)--(1,#1-1);
            \draw[rounded corners](-1.1,1)--(-1,.9)--(-1,-1)--(1,-1)--(1,.9)--(1.1,1);
        }
    },
    right valve/.style={
        code={
            \fill[blue!50](-.1, .1)--(.2, .1)arc(90:0:.2)        --(-.1,-.1);
            \draw         (-.1, .1)--(.2, .1)arc(90:0:.2)(.2,-.1)--(-.1,-.1);
            \fill(.1, .1)+(-.05,0)--+(.05,0)--+(0,-.07)
                 (.1,-.1)+(-.05,0)--+(.05,0)--+(0, .07);
            \draw[blue!50,line width=.2cm,domain=0:#1,samples=#1*10,shift={(.3,-.3)}]
                 (0,.2)--(0,.1)--plot({rand/30},-\x);
        }
    },
    left valve/.style={
        /tikz/xscale=-1,right valve=#1
    },
    pipe/.style={
        code={
            \fill[blue!50](-#1-.1,.1)rectangle(#1+.1,-.1);
            \draw         (-#1-.1,.1)--(#1+.1,.1)(-#1-.1,-.1)--(#1+.1,-.1);
            \fill(0, .1)+(-.05,0)--+(.05,0)--+(0,-.07)
                 (0,-.1)+(-.05,0)--+(.05,0)--+(0, .07);
        }
    }
}

\tikz{
    \path                                               (4,6  )pic{reservoir=1.5}
         (-3,6.5)pic{right valve=2}                     (3,5.5)pic{left valve=2.2}
         (-2,3  )pic{reservoir=1.8}                     (2,3  )pic{reservoir=.7}
                                    (0,2.5)pic{pipe={1}}
         (-3,2.5)pic{left valve=2.7}                    (3,2.5)pic{right valve=1.6}
         (-4,0  )pic{reservoir=.2}                      (4,0  )pic{reservoir=1.1}
    ;
}

\end{document}

对于标签,可以在定义中添加相应的代码,pic如下所示

\tikzset{
    pics/labeled reservoir/.style 2 args={
        code={
            \fill[rounded corners,blue!50]  (-1,#1-1)--(-1,-1)--(1,-1)--(1,#1-1);
            \draw[rounded corners](-1.1,1)--(-1,.9)--(-1,-1)--(1,-1)--(1,.9)--(1.1,1);
            \draw[->](0,-.9)--(0,#1-1)node[below right]{$#2$};
        }
    },
}
\tikz{
    \path pic{labeled reservoir={1.3}{h_3}};
}

\tikzset{
    pics/labeled right valve/.style n args={3}{
        code={
            \fill[blue!50](-.1, .1)--(.2, .1)arc(90:0:.2)        --(-.1,-.1);
            \draw         (-.1, .1)--(.2, .1)arc(90:0:.2)(.2,-.1)--(-.1,-.1);
            \fill(.1, .1)+(-.05,0)--+(.05,0)--+(0,-.07)
                 (.1,-.1)+(-.05,0)--+(.05,0)--+(0, .07)
                 (.2, .1)node[above]{$#2$};
            \draw[blue!50,line width=.2cm,domain=0:#1,samples=#1*10,shift={(.3,-.3)}]
                 (0,.2)--(0,.1)--plot({rand/30},-\x)node[above right,black]{$#3$};
        }
    },
}
\tikz{
    \path pic{labeled right valve={1.3}{R_4}{Q_4}};
}

相关内容