tikz 中的数据库形状

tikz 中的数据库形状

我想在 tikz 中创建一个如下所示的数据库形状:

在此处输入图片描述

我需要中间的两个弧,因此我不能直接使用cylinder,如这个问题

理想情况下,我希望从形状开始cylinder,然后添加一些圆弧,但我不知道该怎么做?形状的代码cylinder在哪里可以找到?或者有更好的方法来实现这一点吗?

我看过这个问题询问如何创建相机的形状,但它与我想要的相差甚远。

答案1

我认为您不需要为此定义新形状,除非您还想使用边框锚点。如果您这样做,请告诉我。

为了简单使用,您可以使用以下命令:

\documentclass[tikz,margin=2mm]{standalone}

\makeatletter
\tikzset{
    database/.style={
        path picture={
            \draw (0, 1.5*\database@segmentheight) circle [x radius=\database@radius,y radius=\database@aspectratio*\database@radius];
            \draw (-\database@radius, 0.5*\database@segmentheight) arc [start angle=180,end angle=360,x radius=\database@radius, y radius=\database@aspectratio*\database@radius];
            \draw (-\database@radius,-0.5*\database@segmentheight) arc [start angle=180,end angle=360,x radius=\database@radius, y radius=\database@aspectratio*\database@radius];
            \draw (-\database@radius,1.5*\database@segmentheight) -- ++(0,-3*\database@segmentheight) arc [start angle=180,end angle=360,x radius=\database@radius, y radius=\database@aspectratio*\database@radius] -- ++(0,3*\database@segmentheight);
        },
        minimum width=2*\database@radius + \pgflinewidth,
        minimum height=3*\database@segmentheight + 2*\database@aspectratio*\database@radius + \pgflinewidth,
    },
    database segment height/.store in=\database@segmentheight,
    database radius/.store in=\database@radius,
    database aspect ratio/.store in=\database@aspectratio,
    database segment height=0.1cm,
    database radius=0.25cm,
    database aspect ratio=0.35,
}
\makeatother

\begin{document}
    \begin{tikzpicture}[line width=1pt]
        \node[database,label=below:some label] {};
        \node[database,label=below:some label,database radius=1cm,database segment height=0.5cm] at (3,0) {};
    \end{tikzpicture}
\end{document}

\makeatletter\makeatother使用宏所必需的@。这里其实不需要,但我更喜欢使用它来保持宏的可读性,并防止您在不使用键的情况下在某处覆盖它们pgf

此示例的结果是:

在此处输入图片描述

编辑

根据评论中的要求,使用以下代码,您可以分别为所有三个部分设置样式。您可以使用database top segment={<style>}database middle segment={<style>}database bottom segment={<style>}来实现这一点。

\documentclass[tikz,margin=2mm]{standalone}

\makeatletter
\tikzset{
    database top segment style/.style={draw},
    database middle segment style/.style={draw},
    database bottom segment style/.style={draw},
    database/.style={
        path picture={
            \path [database bottom segment style]
                (-\db@r,-0.5*\db@sh) 
                -- ++(0,-1*\db@sh) 
                arc [start angle=180, end angle=360,
                    x radius=\db@r, y radius=\db@ar*\db@r]
                -- ++(0,1*\db@sh)
                arc [start angle=360, end angle=180,
                    x radius=\db@r, y radius=\db@ar*\db@r];
            \path [database middle segment style]
                (-\db@r,0.5*\db@sh) 
                -- ++(0,-1*\db@sh) 
                arc [start angle=180, end angle=360,
                    x radius=\db@r, y radius=\db@ar*\db@r]
                -- ++(0,1*\db@sh)
                arc [start angle=360, end angle=180,
                    x radius=\db@r, y radius=\db@ar*\db@r];
            \path [database top segment style]
                (-\db@r,1.5*\db@sh) 
                -- ++(0,-1*\db@sh) 
                arc [start angle=180, end angle=360,
                    x radius=\db@r, y radius=\db@ar*\db@r]
                -- ++(0,1*\db@sh)
                arc [start angle=360, end angle=180,
                    x radius=\db@r, y radius=\db@ar*\db@r];
            \path [database top segment style]
                (0, 1.5*\db@sh) circle [x radius=\db@r, y radius=\db@ar*\db@r];
        },
        minimum width=2*\db@r + \pgflinewidth,
        minimum height=3*\db@sh + 2*\db@ar*\db@r + \pgflinewidth,
    },
    database segment height/.store in=\db@sh,
    database radius/.store in=\db@r,
    database aspect ratio/.store in=\db@ar,
    database segment height=0.1cm,
    database radius=0.25cm,
    database aspect ratio=0.35,
    database top segment/.style={
        database top segment style/.append style={#1}},
    database middle segment/.style={
        database middle segment style/.append style={#1}},
    database bottom segment/.style={
        database bottom segment style/.append style={#1}}
}
\makeatother

\begin{document}
    \begin{tikzpicture}[line width=1pt]
        \node[database,label=below:some label, database middle segment={fill=green}, database bottom segment={draw=none, fill=blue}] {};
        \node[database,label=below:some label,database radius=1cm,database segment height=0.5cm, database top segment={draw=blue,fill=red}] at (3,0) {};
    \end{tikzpicture}
\end{document}

上面的例子的结果是:

在此处输入图片描述

答案2

你是对的,圆柱体已经存在shapes.geometric,并且很容易用它来绘制数据库形状。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{shapes.geometric}
\tikzset{database/.style={cylinder,aspect=0.5,draw,rotate=90,path picture={
\draw (path picture bounding box.160) to[out=180,in=180] (path picture bounding
box.20);
\draw (path picture bounding box.200) to[out=180,in=180] (path picture bounding
box.340);
}}}
\begin{document}
\begin{tikzpicture}
\node[database] {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容