[Tex/LaTex] Draw dimension of a line as a decoration in TikZ

dimensionslabelstechnical-drawingtikz-pgf

Is it possible to define in TikZ a new decoration or something like that, such that it is possible to write something like:

\draw[dim={text,above}] (A) -- (B);
\draw[dim={text,below}] (C) -- (D);

or even better

\draw (A) dim[text,above] (B);
\draw (C) dim[text,below] (C);

to get something like this:

dim

It's important that the text is sloped.

If possible I would prefer the second syntax, because then I could easily combine such path's:

\draw (A) dim[text,above] (B) -- (C) -- (D) dim[text] (E) dim[text,below] (F) -- circle;

Best Answer

Update

The result looks fine but perhaps the code can be improved.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations,decorations.markings,decorations.text}

\begin{document}
 \pgfkeys{/pgf/decoration/.cd,
      distance/.initial=10pt
}  

\pgfdeclaredecoration{add dim}{final}{
\state{final}{% 
\pgfmathsetmacro{\dist}{5pt*\pgfkeysvalueof{/pgf/decoration/distance}/abs(\pgfkeysvalueof{/pgf/decoration/distance})} 
          \pgfpathmoveto{\pgfpoint{0pt}{0pt}}             
          \pgfpathlineto{\pgfpoint{0pt}{2*\dist}}   
          \pgfpathmoveto{\pgfpoint{\pgfdecoratedpathlength}{0pt}} 
          \pgfpathlineto{\pgfpoint{(\pgfdecoratedpathlength}{2*\dist}}
           \pgfusepath{stroke} 
          \pgfsetdash{{0.1cm}{0.1cm}{0.1cm}{0.1cm}}{0cm}     
          \pgfsetarrowsstart{latex}
          \pgfsetarrowsend{latex}  
          \pgfpathmoveto{\pgfpoint{0pt}{\dist}}
          \pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{\dist}} 
          \pgfusepath{stroke} 
          \pgfsetdash{}{0pt}
          \pgfpathmoveto{\pgfpoint{0pt}{0pt}}
          \pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{0pt}}
}}

\tikzset{dim/.style args={#1,#2}{decoration={add dim,distance=#2},
                decorate,
                postaction={decorate,decoration={text along path,
                                                 raise=#2,
                                                 text align={align=center},
                                                 text={#1}}}}}

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,2);
\coordinate (C) at (8,-2);

\draw[dim={5 cm,10pt,}]  (A) --  (B);
\draw[dim={7 cm,-15pt}]  (B) --  (C); 

\draw[fill=gray] (A) circle(2pt); 
\draw[fill=gray] (B) circle(2pt);
\draw[fill=gray] (C) circle(2pt);   
\end{tikzpicture}

\end{document} 

enter image description here

Update : possible with edge

\draw (A) edge [dim={5 cm,10pt}]  (B) 
          edge[dim={7 cm,-15pt}]  (C)
       (B)edge[dim={4 cm,+10pt}]  (C);  

enter image description here

Related Question