% schulmathematik bundle: package schulma-physik
% Version 1.3
% 12. August 2022
\ProvidesExplPackage{schulma-physik}{2022-08-12}{1.3}{Befehle für die Schulphysik}

\RequirePackage {siunitx}
\RequirePackage {tikz}

\sisetup { locale = DE , uncertainty-mode = separate }

\NewDocumentCommand \Massstab {m m m m}
  {
    \ensuremath
      {
        \qty {#1} {#2}
        \; \widehat {=} \;
        \qty {#3} {#4}
      }
  }

\NewDocumentCommand \tqty {m m}
  {
    \ensuremath
      {  
        \group_begin:
        \textstyle
        \qty [ per-mode = fraction ] {#1} {#2}
        \group_end:
      }
  }

\cs_new_eq:NN \tSI \tqty

\NewDocumentCommand \tunit {m}
  {
    \ensuremath
      {  
        \group_begin:
        \textstyle
        \unit [ per-mode = fraction ] {#1}
        \group_end:
      }
  }

\cs_new_eq:NN \tsi \tunit

\NewDocumentCommand \Beschl {m}
  {
    \tqty {#1} { \m \per \square \s }
  }

\NewDocumentCommand \Erdb { }
  {
    \tqty {9,81} { \m \per \square \s }
  }

\NewDocumentCommand \Ortsf { }
  {
    \tqty {9,81} { \N \per \kg }
  }

\NewDocumentCommand \Elem { }
  {
    \qty {1,60e-19} {\coulomb}
  }

\NewDocumentCommand \Elekm { }
  {
    \qty {9,11e-31} {\kg}
  }

\NewDocumentCommand \Lichtg { }
  {
    \qty {3,00e8} { \m \per \s }
  }

\NewDocumentCommand \Planck { }
  {
    \qty {6,63e-34} { \J \s }
  }

\NewDocumentCommand \EFK { }
  {
    \qty {8,85e-12} { \A \s \per \V \per \m }
  }

\NewDocumentCommand \MFK {s}
  {
    \IfBooleanTF {#1}
      {
        \qty {1,26e-6} { \V \s \per \A \per \m }
      }
      {
        \ensuremath
          {
            4 \,
            \cs_if_exist_use:N \symup
            \pi
            \cdot
            \qty [ print-unity-mantissa = false ] {e-7} { \V \s \per \A \per \m }
          }
      }
  }

\dim_const:Nn \c_schulma_physik_messschieber_l_skala_dim {15.2cm}
\dim_const:Nn \c_schulma_physik_messschieber_b_skala_dim {15mm}
\dim_const:Nn \c_schulma_physik_messschieber_l_schieber_dim {4cm}
\dim_const:Nn \c_schulma_physik_messschieber_b_schieber_dim {17mm}
\dim_const:Nn \c_schulma_physik_messschieber_l_fenster_dim {25mm}
\dim_const:Nn \c_schulma_physik_messschieber_b_fenster_dim {1cm}

\NewDocumentCommand \Messschieber {m}
  {
    \begin {tikzpicture} [ font = \sffamily ]
    % Hauptskala
    \draw ( -1.6cm , -0.5 * \c_schulma_physik_messschieber_b_skala_dim + 0.5 * \c_schulma_physik_messschieber_b_fenster_dim )
      -- ++ ( \c_schulma_physik_messschieber_l_skala_dim , 0 ) node [ above~left ] { \small cm }
      -- ++ ( 0 , \c_schulma_physik_messschieber_b_skala_dim )
      -- ++ ( - \c_schulma_physik_messschieber_l_skala_dim , 0)
      -- cycle ;
    \int_step_inline:nnn {0} {120}
      {
        \int_set:Nn \l_tmpa_int { \int_mod:nn {##1} {10} }
        \int_compare:nNnTF {\l_tmpa_int} = {0}
          {
            \draw (##1*1mm,-1mm)--(##1*1mm,6mm) node [ left , xshift=2pt , yshift = -2pt ]
              { \footnotesize \int_eval:n {##1/10} } ;
          }
          {
            \int_compare:nNnTF {\l_tmpa_int} = {5}
              {
                \draw (##1*1mm,-1mm)--(##1*1mm,4.5mm);
              }
              {
                \draw (##1*1mm,-1mm)--(##1*1mm,3mm);
              }
          }
      }
    % Schieber
    \draw [ fill = white , even~odd~rule ]
      ( #1cm - 0.5 * \c_schulma_physik_messschieber_l_schieber_dim + 4.5mm ,
        -0.5 * \c_schulma_physik_messschieber_b_schieber_dim + 0.5 * \c_schulma_physik_messschieber_b_fenster_dim )
      rectangle ++ ( \c_schulma_physik_messschieber_l_schieber_dim , \c_schulma_physik_messschieber_b_schieber_dim )
      { [rounded~corners = 0.5 * \c_schulma_physik_messschieber_b_fenster_dim ]
        ( #1cm - 0.5 * \c_schulma_physik_messschieber_l_fenster_dim + 4.5mm , 0 )
        rectangle ++ ( \c_schulma_physik_messschieber_l_fenster_dim , \c_schulma_physik_messschieber_b_fenster_dim ) } ;
    % Noniusskala
    \int_step_inline:nnn {0} {10}
      {
        \int_set:Nn \l_tmpa_int { \int_mod:nn {##1} {10} }
        \int_compare:nNnTF {\l_tmpa_int} = {0}
          {
            \draw ( #1cm + ##1 * 0.9mm , 0 ) -- ( #1cm + ##1 * 0.9mm , -1.6mm ) ;
          }
          {
            \int_compare:nNnTF {\l_tmpa_int} = {5}
              {
                \draw ( #1cm + ##1 * 0.9mm , 0 ) -- ( #1cm + ##1 * 0.9mm , -1.3mm ) ;
              }
              {
                \draw ( #1cm + ##1 * 0.9mm , 0 ) -- ( #1cm + ##1 * 0.9mm , -1mm ) ;
              }
          }
      }
    \end{tikzpicture}
  }

\dim_new:N \l_schulma_physik_messschraube_massstab_dim
\dim_set:Nn \l_schulma_physik_messschraube_massstab_dim {2mm}
\dim_new:N \l_schulma_physik_messschraube_skalenstrich_y_dim
\dim_new:N \l_schulma_physik_messschraube_skalenstrich_deltay_dim
\int_new:N \l_schulma_physik_messschraube_skalenbeschriftung_int
\fp_new:N \l_schulma_physik_messschraube_hundertstelbruchteile_fp

\NewDocumentCommand \Messschraube {m}
  {
    % nach: dexteritas, https://tex.stackexchange.com/questions/472876/how-to-draw-micrometer-scale-using-tikz
    \begin{tikzpicture} [ font = \sffamily ]
    % horizontale Skala
    \draw [semithick] (0,0) -- ( #1 * \l_schulma_physik_messschraube_massstab_dim , 0 ) ;
    \int_step_inline:nnn {0} { \fp_to_int:n {#1} + 2 }
      {
        \int_compare:nNnTF { \int_mod:nn {##1} {5} } = {0}
          {
            \draw [thick] ( ##1 * \l_schulma_physik_messschraube_massstab_dim , -0.5 ) -- ++ (0,0.65) node [above] { \small ##1 } ;
          }
          {
            \draw ( ##1 * \l_schulma_physik_messschraube_massstab_dim , 0) -- ++ (0,-0.5) ;
          }
      }
    \node [ above~right ] at (-5mm,-1cm) {\small mm} ;
    % drehbare Skala
    \fp_set:Nn \l_schulma_physik_messschraube_hundertstelbruchteile_fp { #1 * 100 - \fp_to_int:n { #1 * 100} }
    \fill [white] ( #1 * \l_schulma_physik_messschraube_massstab_dim , 1cm ) -- ++ (8mm,5mm) -- ++ (0,-3cm) -- ++ (-8mm,5mm) -- cycle ;
    \draw [thin] ( #1 * \l_schulma_physik_messschraube_massstab_dim , 1 ) -- ++ (0,-2cm) ;
    \draw ( #1 * \l_schulma_physik_messschraube_massstab_dim , 1cm ) -- ++ (8mm,5mm) ;
    \draw ( #1 * \l_schulma_physik_messschraube_massstab_dim , -1cm ) -- ++ (8mm,-5mm) ;
    \int_step_inline:nnn {-17} {17}
      {
        \int_set:Nn \l_schulma_physik_messschraube_skalenbeschriftung_int
          {
            \int_mod:nn { \fp_to_int:n { #1 * 100 } + ##1 } {100}
          }
        \dim_set:Nn \l_schulma_physik_messschraube_skalenstrich_y_dim
          {
            \fp_eval:n { ( ##1 - \l_schulma_physik_messschraube_hundertstelbruchteile_fp ) / 18 } cm
          }
        \int_compare:nNnTF { \int_mod:nn {\l_schulma_physik_messschraube_skalenbeschriftung_int} {5} } = {0}
          {
            % langer Strich mit Zahl
            \dim_set:Nn \l_schulma_physik_messschraube_skalenstrich_deltay_dim { 4.375mm * ##1/18 }
            \int_compare:nNnTF {\l_schulma_physik_messschraube_skalenbeschriftung_int} < {10}
              {
                \draw ( #1 * \l_schulma_physik_messschraube_massstab_dim , \l_schulma_physik_messschraube_skalenstrich_y_dim )
                  -- ++ ( 7mm , \l_schulma_physik_messschraube_skalenstrich_deltay_dim )
                  node [right] { \footnotesize 0 \int_to_arabic:n {\l_schulma_physik_messschraube_skalenbeschriftung_int} } ;
              }
              {
                \draw ( #1 * \l_schulma_physik_messschraube_massstab_dim , \l_schulma_physik_messschraube_skalenstrich_y_dim )
                  -- ++ ( 7mm , \l_schulma_physik_messschraube_skalenstrich_deltay_dim )
                  node [right] { \footnotesize \int_to_arabic:n {\l_schulma_physik_messschraube_skalenbeschriftung_int} } ;
              }
          }
          {
            % kurzer Strich
            \dim_set:Nn \l_schulma_physik_messschraube_skalenstrich_deltay_dim { 2.5mm * ##1/18 }
            \draw ( #1 * \l_schulma_physik_messschraube_massstab_dim , \l_schulma_physik_messschraube_skalenstrich_y_dim )
              -- ++ ( 4mm , \l_schulma_physik_messschraube_skalenstrich_deltay_dim ) ;
          }
       }
    % oberer und unterer Rand
    \draw (-5mm,1cm) -- ( #1 * \l_schulma_physik_messschraube_massstab_dim , 1cm ) ;
    \draw (-5mm,-1cm) -- ( #1 * \l_schulma_physik_messschraube_massstab_dim , -1cm ) ;
    \draw ( #1 * \l_schulma_physik_messschraube_massstab_dim + 8mm, 1.5cm ) -- ++ (2cm,0) ;
    \draw ( #1 * \l_schulma_physik_messschraube_massstab_dim + 8mm, -1.5cm ) -- ++ (2cm,0) ;
    % geschwungene Linien links und rechts
    \draw (-5mm,-1cm) to [ out=130 , in=-130 ] (-5mm,0) to [ out=130 , in=-130 ] (-5mm,1cm) ;
    \draw [very~thin] (-5mm,0) to [ out=50 , in=-50 ] (-5mm,1cm) ;
    \draw ( #1 * \l_schulma_physik_messschraube_massstab_dim + 2.8cm , 1.5cm ) to [ out=-50 , in=50 ] ++ (0,-1.5cm) to [ out=-50 , in=50 ] ++ (0,-1.5cm) ;
    \draw [very~thin] ( #1 * \l_schulma_physik_messschraube_massstab_dim + 2.8cm , 0 ) to [ out=130 , in=-130 ] ++ (0,1.5cm) ;
    \end{tikzpicture}
  }

\ExplSyntaxOff

\usetikzlibrary{circuits.ee.IEC}

\pgfdeclareshape{doppelbuchse}
  {%
    \inheritsavedanchors[from=rectangle ee]%
    \inheritanchor[from=rectangle ee]{center}%
    \inheritanchor[from=rectangle ee]{north}%
    \inheritanchor[from=rectangle ee]{south}%
    \inheritanchor[from=rectangle ee]{east}%
    \inheritanchor[from=rectangle ee]{west}%
    \inheritanchor[from=rectangle ee]{north east}%
    \inheritanchor[from=rectangle ee]{north west}%
    \inheritanchor[from=rectangle ee]{south east}%
    \inheritanchor[from=rectangle ee]{south west}%
    \inheritanchor[from=rectangle ee]{input}%
    \inheritanchor[from=rectangle ee]{output}%
    \inheritanchorborder[from=rectangle ee]%
    \backgroundpath{
      \pgf@process{\pgfpointadd{\southwest}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}%
      \pgf@xa=\pgf@x \pgf@ya=\pgf@y
      \pgf@process{\pgfpointadd{\northeast}{\pgfpointscale{-1}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}}%
      \pgf@xb=\pgf@x \pgf@yb=\pgf@y
      \pgfutil@tempdima=\pgf@yb%
      \advance\pgfutil@tempdima by -\pgf@ya%
      \pgfutil@tempdima=0.5\pgfutil@tempdima%
      \advance\pgf@xa by \pgfutil@tempdima%
      \advance\pgf@xb by -\pgfutil@tempdima%
      {\pgfpathcircle{\pgfqpoint{\pgf@xa}{0pt}}{\pgfutil@tempdima}}
      {\pgfpathcircle{\pgfqpoint{\pgf@xb}{0pt}}{\pgfutil@tempdima}}
    }%
  }

\tikzset
  {
    bulb IEC graphic/.style=
      {
        circuit symbol lines,
        circuit symbol size=width 1.9 height 1.9,
        shape=generic circle IEC,
        /pgf/generic circle IEC/before background=
          {
            \pgfpathmoveto{\pgfpointpolar{-135}{1pt}}
            \pgfpathlineto{\pgfpointpolar{45}{1pt}}
            \pgfpathmoveto{\pgfpointpolar{-45}{1pt}}
            \pgfpathlineto{\pgfpointpolar{135}{1pt}}
            \pgfusepathqstroke
          },
        transform shape,
      },
    capacitor IEC graphic/.style=
      {
        circuit symbol lines,
        circuit symbol size=width 0.8 height 2.4,
        transform shape,
        shape=capacitor IEC,
      },
    amperemeter graphic/.style=
      {
        circuit symbol open,
        circuit symbol size=width 2 height 2,
        transform shape,
        shape=generic circle IEC,
        info=center:{\sffamily A}
      },
    voltmeter graphic/.style=
      {
        circuit symbol open,
        circuit symbol size=width 2 height 2,
        transform shape,
        shape=generic circle IEC,
        info=center:{\sffamily V}
      },
    circuit declare symbol=messgeraet,
    set messgeraet graphic=
      {
        circuit symbol open,
        circuit symbol size=width 2 height 2,
        transform shape,
        shape=generic circle IEC,
        /pgf/generic circle IEC/before background=
          {
            \draw[annotation arrow,->] (0.58pt,-0.58pt)--(-0.58pt,0.58pt);
          }
      },
    circuit declare symbol=spannungsquelle,
    set spannungsquelle graphic=
      {
        circuit symbol open,
        circuit symbol size=width 2.4 height 0.5,
        transform shape,
        shape=doppelbuchse,
        outer sep=0pt
      },
  }
