为局部坐标系中的阴影矩形定义 TikZ 宏

为局部坐标系中的阴影矩形定义 TikZ 宏

我想为结构力学课程中梁上定义的函数定义宏:

受力和应力的梁

我正在尝试编写一些宏,这些宏将绘制函数为常数的每个段(也许稍后会绘制为线性或类似函数的段)。理想情况下,它们的结果将在 TikZ 中像这样使用:

\beamFunc{AB={(0,0),(0,2)},func value=-500,label pos=.5,show label=true,show sign=false}; % this would be the first segment lower left

其中图案自动垂直于(A)--(B)。下面是我最初的尝试,但我最终

! Incomplete \iffalse; all text was ignored after line 43.
<inserted text> 
                \fi 

不太清楚到底发生了什么:

\documentclass{article}
\usepackage{tikz}
\begin{document}
   % define keys for the next command
   \pgfkeys{
      /prpe/beam/.cd,
      % endpoints of the beam (segment)
      AB/.value required,
      AB/.code n args={2}{
         \def\beamA{#1}
         \def\beamB{#2}
         \def\beamLen{\pgfmathparse{veclen($\beamB-\beamA$)}}
      },
      % constant function value along the beam (segment)
      func value/.value required,
      func value/.get=\beamF,
      % scaling of the function value to coordinates
      func scale/.get=\beamFuncScale,
      func scale/.initial=1.,
   }
   % define command for drawing the beam
   \newcommand{\prpeBeamConstFunc}[1]{
      \pgfkeys{/prpe/beam/.cd,#1}
      % find out how is the beam axis rotated
      \pgfmathanglebetweenpoints{(1,0)}{$\beamB-\beamA$};
      \let\beamAngle=\pgfmathresult
      % do the rest in beam-local coordinates
      \begin[rotate=\beamAngle,shift={\beamA}]{scope}
         % rectangle in local coordinates
         \coordinate (A) at (0,0);
         \coordinate (B) (\beamLen,0);
         \coordinate (Af) at (0,\beamFuncScale*\beamF);
         \coordinate (Bf) at (\beamLen,\beamFuncScale*\beamF);
         % beam itself
         \draw[thick] (A) -- (B);
         % hatched rectangle, with lines locally in the vertical direction
         \fill[pattern=north lines] (A)--(B)--(Bf)--(Af)--cycle;
      \end{scope}
   }
   % try it out here
   \begin{tikzpicture}   
      \prpeBeamConstFunc{AB={(0,0),(0,5)},func value=100,func scale=.01};
   \end{tikzpicture}
\end{document}

我可以得到帮助来发现错误吗?由于这是我的第一个 TikZ 宏,因此也欢迎任何其他建议。

答案1

这是一个基本的装饰。它读取要绘制的梁是否应该被夹紧。然后根据负载大小的符号,它在装饰中放置一个+-符号,并将大小放在其顶部。您可以通过反向绘制来更改夹紧边缘,但需要一个小子if句来更改标签的方向。

有一件事让我困惑,那就是我无法给出一个line width只影响光束而不影响装饰的选项。我尝试了一些postaction魔法,但做不到,它仍然会渗透到装饰中。此外,目前还没有太多的选择自动化。夹钳阴影相当粗糙等。请随时纠正/改进此代码。

我认为你可以通过定制它来获得一些东西具体的(难以抗拒!)。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns,decorations}
\newdimen\lohei
\newif\ifclamp
\pgfkeys{%
    /pgf/decoration/.cd,%
    load height/.code={\pgfmathsetlength\lohei{#1}},%
    loadmag/.code={\pgfmathparse{notless(#1,0) ? "$+$": "$-$"}\let\signtype=\pgfmathresult%
    \def\loadlabel{#1}},%
    clamped/.initial=false,
    clamped/.is if=clamp
    }

\pgfdeclaredecoration{beamloader}{initial}
{
    \state{initial}[width=0mm,next state=hatchedit]
    {
    \ifclamp
         \pgfmoveto{\pgfpoint{0}{-10mm}}
         \pgflineto{\pgfpoint{0}{10mm}}
         \foreach \x in {0,1,...,10}{
             \pgfmoveto{\pgfpoint{0}{\x mm}}
             \pgflineto{\pgfpointadd{\pgfpoint{0}{\x mm}}{\pgfpoint{-2mm}{-2mm}}}
             \pgfmoveto{\pgfpoint{0}{-\x mm}}
             \pgflineto{\pgfpointadd{\pgfpoint{0}{-\x mm}}{\pgfpoint{-2mm}{-2mm}}}
             \pgfmoveto{\pgfpointorigin}
         }
    \else \fi
    }
    \state{hatchedit}[width=2mm]
    {
        \pgfmoveto{\pgfpointorigin}
        \pgflineto{\pgfpoint{0cm}{\lohei}}
        \pgfmoveto{\pgfpointdecoratedinputsegmentlast}
    }
    \state{final}
    {
        \pgfpathrectanglecorners{\pgfpointdecoratedpathlast}{\pgfpoint{-\pgfdecoratedpathlength/1.0}{\lohei}}
        \pgfusepath{stroke}
        \pgftransformshift{\pgfpoint{-\pgfdecoratedpathlength/2}{.5\lohei+5}}
        \color{white}\pgfsetstrokecolor{black}
        \pgfset{inner sep=0}
        \pgfnode{circle}{north}{\color{black}\signtype}{node}{\pgfusepath{fill,stroke}}
        \color{red}
        %\pgftransformresetnontranslations
        \pgftext[bottom,at=\pgfpoint{0}{.5\lohei}]{\loadlabel N}
    }
}
\begin{document}


\begin{tikzpicture}
\draw [decorate,decoration={beamloader,clamped,load height=2cm,loadmag=500}] (-2cm,0) -- (-2cm,3cm);
\draw [decorate,decoration={beamloader,load height=0.6cm,loadmag=-100}] (-2cm,3cm) -- (-2cm,6cm) node[above] {kN};
\draw [decorate,decoration={beamloader,load height=0.5cm,loadmag=-275,clamped}] (5cm,2cm) -- (3cm,2cm);
\draw [decorate,decoration={beamloader,load height=0.5cm,loadmag=-275,clamped}] (0cm,0cm) -- (3cm,2cm);
\end{tikzpicture}

\end{document}

TikZ 输出的 Beam 函数

我还发现,如果从更通用的代码开始,也可以轻松地获得弧形的东西。

最后,抱歉,加载数字有点奇怪。我只是检查一下。

相关内容