%%-----------------------------------------------------------------%%
%%                        GRAFCET 1.0
%%-----------------------------------------------------------------%%
%%
%% Copyleft Pierre Chatelier
%%       e-mail: pierre.chatelier@club-internet.fr
%%   page perso: http://perso.club-internet.fr/ktd
%%
%%Un package pour ecrire des grafcets
%%
%%Les commandes disponibles sont les suivantes:
%% - \Transition{texte}     pour ecrire une transition
%% - \Step{texte}           pour ecrire une etape
%% - \MacroStep{texte}      pour ecrire une macro-etape
%% - \FirstStep{texte}      pour ecrire l'etape initiale
%% - \Divergence{...}{...}  pour ecrire une divergence (un "OU")
%% - \Fork{...}{...}        pour ecrire un processus parallele (un "ET")
%% - \Loop{...}             pour ecrire une boucle qui revient
%% - \Nop                   pour combler des trous
%%-----------------------------------------------------------------%%
%%This is a package to write grafcets
%%
%%Provided commands are:
%% - \Transition{text}     For a transition
%% - \Step{text}           For a step
%% - \MacroStep{text}      For a macro step
%% - \FirstStep{text}      For the first step
%% - \Divergence{...}{...} For a divergence ("OR")
%% - \Fork{...}{...}       For a parallel process ("AND")
%% - \Loop{...}            For making a loop go back
%% - \Nop                  To fill some blanks
%%-----------------------------------------------------------------%%

%%-----------------------------------------------------------------%%
%%---------------------- Identification ---------------------------%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{grafcet}

%%-----------------------------------------------------------------%%
%%
%%
%%-----------------------------------------------------------------%%
%%-------------- Code initial / Initial code ----------------------%%
\RequirePackage{float}  %%
\RequirePackage{ifthen} %%\ifthenelse{}{}
\RequirePackage{hhline} %%\hhline{}

%%-------------------- Langage / Language -------------------------%%
\@ifpackageloaded{babel}
{
  \iflanguage{french}{\def\gra@language{french}}{}
  \iflanguage{english}{\def\gra@language{english}}{}
  \iflanguage{american}{\def\gra@language{english}}{}
}
{
  \def\gra@language{french} %Langage par defaut / Default language
}
\ifthenelse{\equal{\gra@language}{french}}
{
  \def\gra@floatname{Grafcet}
  \def\gra@listname{Liste des Grafcets}
}{}
\ifthenelse{\equal{\gra@language}{english}}
{
  \def\gra@floatname{Grafcets}
  \def\gra@listname{List of Grafcets}
}{}

%%----- Adequation au package "float" / Match "float" package -----%%
%%-----------------------------------------------------------------%%
%%----- Pour la table des grafcets / for the list of grafcets -----%%
\newcommand\floatc@gra[2]{\begin{center}{\bfseries\rmfamily #1:} #2\end{center}}
\newcommand\fs@gra{
   \let\@fs@capt\floatc@gra
   \def\@fs@pre{}\def\@fs@post{}\def\@fs@mid{\vspace{3pt}}
   \let\@fs@iftopcapt\iftrue}
\newfloat{grafcetfloat}{h}{lor}
\floatname{grafcetfloat}{\gra@floatname}
\newcommand{\listofgrafcets}{\listof{grafcetfloat}{\gra@listname}}


%% --------- Compteurs et autres / Counters and others... -------%%
\newcounter{CompteurEtapes}
\renewcommand{\theCompteurEtapes}{\arabic{CompteurEtapes}}
\newbox\LHS
\newbox\RHS
\newbox\LHSS
\newbox\RHSS
\newdimen\Lskip
\newdimen\Rskip
\newlength{\diff}
\newlength{\difff}
\newlength{\LLHS}
\newlength{\LRHS}

%%-----------------------------------------------------------------%%
%%------------------ Les commandes / Commands ---------------------%%

\newcommand{\SimpleBox}[1]{\begin{tabular}{|c|}\hline\rule{0pt}{3.5ex}\LARGE#1\\\hline\end{tabular}}
\newcommand{\DoubleBox}[1]{\begin{tabular}{|c|}\hhline{|=|}\rule{0pt}{3.5ex}\LARGE#1\\\hhline{|=|}\end{tabular}}

\newcommand{\Transition}[1]{%
                        \setbox\LHSS\hbox{\hspace{-1.67em}\rule{1em}{.4pt}\rule[-3ex]{.4pt}{6ex}\rule{1em}{.4pt}\hspace{.5em}}
                        \setbox\RHSS\hbox{\begin{tabular}{@{}c}#1\end{tabular}}%
                        \ifthenelse{\dp\LHSS < \dp\RHSS}
                        {
                            \setlength{\diff}{\dp\RHSS}
                            \addtolength{\diff}{-\dp\LHSS}
                            \setlength{\difff}{\dp\LHSS}
                            \addtolength{\difff}{\diff}
                            \begin{tabular}{@{}c@{}l@{}}%
                                \raise - \difff
                                \vbox{
                                    \hsize\wd\LHSS
                                    \hbox to \hsize{\hfill\vrule height \diff\hfill}
                                    \box\LHSS
                                    \hbox to \hsize{\hfill\vrule height \diff\hfill}
                                }
                                &
                                \raise - 1.2ex
                                \vbox{
                                    \box\RHSS%
                                }
                            \end{tabular}%
                        }%else if gauche > droite
                        {
                            \setlength{\diff}{\dp\LHSS}
                            \addtolength{\diff}{-\dp\RHSS}
                            \setlength{\difff}{\dp\RHSS}
                            \begin{tabular}{@{}c@{}l@{}}%
                                \raise - \difff
                                \vbox{
                                    \box\LHSS
                                }
                                &
                                \raise - 1.2ex
                                \vbox{
                                    \box\RHSS%
                                }
                            \end{tabular}%
                        }%fin ifthenelse
\\}%fin Transition

\newcommand{\Nop}{\begin{tabular}{@{}p{1em}@{}|@{}p{1em}l@{}}&&\\\end{tabular}\\}%

\newcommand{\Step}[2][\theCompteurEtapes]{%
                        \hspace{1em}\vrule\\
                        \setbox\LHSS\hbox{\SimpleBox{#1}\hrulefill}
                        \setbox\RHSS\hbox{\raisebox{.5ex}{\rule{1em}{.4pt}}\begin{tabular}{@{}|l|l|}\hline#2\\\hline\end{tabular}}%
                        \ifthenelse{\dp\LHSS < \dp\RHSS}
                        {
                            \setlength{\diff}{\dp\RHSS}
                            \addtolength{\diff}{-\dp\LHSS}
                            \setlength{\difff}{\dp\LHSS}
                            \ifthenelse{#1<10}{\hspace{-1.63em}}{\hspace{-2.05em}}%
                            \begin{tabular}{@{}c@{}l@{}}%
                                \raise - \difff
                                \vbox{
                                    \hsize\wd\LHSS
                                    \hbox to \hsize{\hfill\vrule height \diff\hfill}
                                    \box\LHSS
                                    \hbox to \hsize{\hfill\vrule height \diff\hfill}
                                }
                                &
                                \raise + \diff
                                \vbox{
                                    \box\RHSS%
                                }
                            \end{tabular}%
                            \setcounter{CompteurEtapes}{#1}
                            \stepcounter{CompteurEtapes}%
                        }%else if gauche > droite
                        {
                            \setlength{\diff}{\dp\LHSS}
                            \addtolength{\diff}{-\dp\RHSS}
                            \setlength{\difff}{\dp\RHSS}
                            \ifthenelse{#1<10}{\hspace{-1.63em}}{\hspace{-2.05em}}%
                            \begin{tabular}{@{}c@{}l@{}}%
                                \raise - \difff
                                \vbox{
                                    \box\LHSS
                                }
                                &
                                \raise - \difff
                                \vbox{
                                    \box\RHSS%
                                }
                            \end{tabular}%
                            \setcounter{CompteurEtapes}{#1}
                            \stepcounter{CompteurEtapes}%
                        }%fin ifthenelse
\\}%fin Etape

\newcommand{\MacroStep}[2][\theCompteurEtapes]{%
                        \hspace{1em}\vrule\\ %%
                        \setbox\LHSS\hbox{\DoubleBox{#1}\hrulefill}
                        \setbox\RHSS\hbox{\raisebox{.5ex}{\rule{1em}{.4pt}}\begin{tabular}{@{}|l|l|}\hline#2\\\hline\end{tabular}}%
                        \ifthenelse{\dp\LHSS < \dp\RHSS}
                        {
                            \setlength{\diff}{\dp\RHSS}
                            \addtolength{\diff}{-\dp\LHSS}
                            \setlength{\difff}{\dp\LHSS}
                            \ifthenelse{#1<10}{\hspace{-1.64em}}{\hspace{-2.05em}}%
                            \begin{tabular}{@{}c@{}l@{}}%
                                \raise - \difff
                                \vbox{
                                    \hsize\wd\LHSS
                                    \hbox to \hsize{\hfill\vrule height \diff\hfill}
                                    \box\LHSS
                                    \hbox to \hsize{\hfill\vrule height \diff\hfill}
                                }
                                &
                                \raise + \diff
                                \vbox{
                                    \box\RHSS%
                                }
                            \end{tabular}%
                            \setcounter{CompteurEtapes}{#1}
                            \stepcounter{CompteurEtapes}%
                        }%else if gauche > droite
                        {
                            \setlength{\diff}{\dp\LHSS}
                            \addtolength{\diff}{-\dp\RHSS}
                            \setlength{\difff}{\dp\RHSS}
                            \ifthenelse{#1<10}{\hspace{-1.64em}}{\hspace{-2.05em}}%
                            \begin{tabular}{@{}c@{}l@{}}%
                                \raise - \difff
                                \vbox{
                                    \box\LHSS
                                }
                                &
                                \raise - \difff
                                \vbox{
                                    \box\RHSS%
                                }
                            \end{tabular}%
                            \setcounter{CompteurEtapes}{#1}
                            \stepcounter{CompteurEtapes}%
                        }%fin ifthenelse
\\}%fin MacroEtape

\newcommand{\FirstStep}[2][\theCompteurEtapes]{%
                        \hspace{1em}\vrule\\
                        \setbox\LHSS\hbox{\fbox{\fbox{\LARGE #1}}\hrulefill}
                        \setbox\RHSS\hbox{\raisebox{.5ex}{\rule{1em}{.4pt}}\begin{tabular}{@{}|l|l|}\hline#2\\\hline\end{tabular}}%
                        \ifthenelse{\dp\LHSS < \dp\RHSS}
                        {
                            \setlength{\diff}{\dp\RHSS}
                            \addtolength{\diff}{-\dp\LHSS}
                            \setlength{\difff}{\dp\LHSS}
                            \addtolength{\difff}{\diff}
                            \ifthenelse{#1<10}{\hspace{-2.07em}}{\hspace{-2.4em}}%
                            \begin{tabular}{@{}c@{}l@{}}%
                                \raise - \difff
                                \vbox{
                                    \hsize\wd\LHSS
                                    \box\LHSS
                                    \hbox to \hsize{\hfill\vrule height \diff\hfill}
                                }
                                &
                                \raise + \diff
                                \vbox{
                                    \box\RHSS%
                                }
                            \end{tabular}%
                            \setcounter{CompteurEtapes}{#1}
                            \stepcounter{CompteurEtapes}%
                        }%else if gauche > droite
                        {
                            \setlength{\diff}{\dp\LHSS}
                            \addtolength{\diff}{-\dp\RHSS}
                            \setlength{\difff}{\dp\RHSS}
                            \addtolength{\difff}{\diff}
                            \ifthenelse{#1<10}{\hspace{-2.07em}}{\hspace{-2.4em}}%
                            \begin{tabular}{@{}c@{}l@{}}%
                                \raise - \difff
                                \vbox{
                                    \box\LHSS
                                }
                                &
                                \vbox{
                                    \box\RHSS%
                                }
                            \end{tabular}%
                            \setcounter{CompteurEtapes}{#1}
                            \stepcounter{CompteurEtapes}%
                        }%fin ifthenelse
\\}%fin EtapeInitiale

\newcommand{\Divergence}[2]
{
  \hspace{1em}\vrule\\
  \setbox\LHS\hbox{\hspace{-2.33em}\begin{tabular}{@{}l}#1\end{tabular}}%
  \setbox\RHS\hbox{\begin{tabular}{@{}l}#2\end{tabular}}%
  \setlength{\LLHS}{\wd\LHS}
  \setlength{\LRHS}{\wd\RHS}

  \ifthenelse{\dp\LHS < \dp\RHS}
  {
  \setlength{\diff}{\dp\RHS}
  \addtolength{\diff}{-\dp\LHS}
  \setlength{\difff}{\dp\LHS}
  \addtolength{\difff}{\diff}
  \begin{tabular}{@{}l@{}l@{}}
          \raise - \difff
          \vbox{%
              \hsize\LLHS
              \hbox to \hsize{\hspace{-2.5em}\rule{\LLHS}{.4pt}\rule{4.5em}{.4pt}}
              \hbox to \hsize{\hspace{-1.35em}\vrule height \diff}
              \box\LHS
              \hbox to \hsize{\hspace{-1.35em}\vrule height \diff}
              \hbox to \LLHS{\hspace{-2.5em}\rule[-.4pt]{\LLHS}{.4pt}\rule[-.4pt]{4.5em}{.4pt}}
          }%
          &
          \box\RHS
   \end{tabular}
  }
  {
  \setlength{\diff}{\dp\LHS}
  \addtolength{\diff}{-\dp\RHS}
  \setlength{\difff}{\dp\RHS}
  \addtolength{\difff}{\diff}
  \begin{tabular}{@{}l@{}l@{}}
          \raise - \difff
          \vbox{%
              \hsize\LLHS
              \hbox to \hsize{\hspace{-2.5em}\rule{\LLHS}{.4pt}\rule{4.5em}{.4pt}}
              \box\LHS
              \hbox to \hsize{\hspace{-2.5em}\rule{\LLHS}{.4pt}\rule{4.5em}{.4pt}}
          }%
          &
          \addtolength{\difff}{-.4pt}   
          \raise - \difff
          \vbox{%
              \hsize\LRHS
              \hbox to \hsize{\hspace{1em}\vrule height \diff}
              \box\RHS
              \hbox to \hsize{\hspace{1em}\vrule height \diff}
          }
   \end{tabular}
  }
\\}%fin divergence

\newcommand{\Loop}[1]
{
  \hspace{1.01em}\vrule\\
  \setbox\LHS\hbox{\hspace{-2.33em}\begin{tabular}{@{}l}#1\end{tabular}}%
  \setbox\RHS\hbox{\hspace{.2pt}\begin{tabular}{@{}c@{}|@{}c@{}}&\hspace{-.8ex}{$\wedge$}\\\end{tabular}}

  \setlength{\LLHS}{\wd\LHS}
  \setlength{\LRHS}{\wd\RHS}  

  \setlength{\diff}{\dp\LHS}
  \addtolength{\diff}{-\dp\RHS}
  \setlength{\difff}{\dp\RHS}
  \addtolength{\difff}{\diff}
  \begin{tabular}{@{}l@{}l@{}}
          \raise - \diff
          \vbox{%
              \hsize\LLHS
              \addtolength{\LLHS}{1.33em}
              \addtolength{\LLHS}{-.4pt}              
              \hbox to \hsize{\hspace{-1.33em}\vrule\rule{\LLHS}{.4pt}\hspace{1pt}\hspace{-.5\LLHS}\raisebox{-.5ex}{$<$}}
              \box\LHS
              %\hbox to \hsize{\hspace{-1.33em}\hspace{-.2pt}\vrule\rule[.05pt]{\LLHS}{.4pt}}
              \hbox to \hsize{\hspace{-1.35em}\rule[.5ex]{.4pt}{2.5ex}\rule[.5ex]{\LLHS}{.4pt}\hspace{1pt}\hspace{-.5\LLHS}$>$}              
          }%
          &
          \raise - \diff
          \vbox{%
              \hsize.4pt
              \addtolength{\diff}{.8pt}
              \addtolength{\diff}{1.4ex}              
              \hbox to \hsize{\rule{.4pt}{\diff}}
              \box\RHS
              %\addtolength{\diff}{1ex}
              \hbox to \hsize{\rule[.5ex]{.4pt}{\diff}}
          }\\
   \end{tabular}\\
}%fin Boucle

\newcommand{\Fork}[2]
{
  \hspace{1em}\vrule\\
  \setbox\LHS\hbox{\hspace{-2.65em}\begin{tabular}{@{}l}#1\end{tabular}}%
  \setbox\RHS\hbox{\begin{tabular}{@{}l}#2\end{tabular}}%
  \setlength{\LLHS}{\wd\LHS}
  \setlength{\LRHS}{\wd\RHS}

  \ifthenelse{\dp\LHS < \dp\RHS}
  {
  \setlength{\diff}{\dp\RHS}
  \addtolength{\diff}{-\dp\LHS}
  \setlength{\difff}{\dp\LHS}
  \addtolength{\difff}{\diff}
  \begin{tabular}{@{\hspace{.31em}}l@{}l@{}}
          \raise - \difff
          \vbox{%
              \hsize\LLHS
              \hbox to \hsize{\hspace{-2.5em}\rule{\LLHS}{.4pt}\rule{4.5em}{.4pt}}
              \hbox to \hsize{\hspace{-2.5em}\rule[-.75ex]{\LLHS}{.4pt}\rule[-.75ex]{4.5em}{.4pt}}
              \hbox to \hsize{\hspace{-1.65em}\vrule height \diff}
              \box\LHS
              \hbox to \hsize{\hspace{-1.65em}\vrule height \diff}
              \hbox to \hsize{\hspace{-2.5em}\rule{\LLHS}{.4pt}\rule{4.5em}{.4pt}}
              \hbox to \hsize{\hspace{-2.5em}\rule[-.75ex]{\LLHS}{.4pt}\rule[-.75ex]{4.5em}{.4pt}}
          }%
          &
          \raise + .4pt
          \vbox{
          \box\RHS
          }
   \end{tabular}\\
  \hspace{1em}\vrule
  }
  {
  \setlength{\diff}{\dp\LHS}
  \addtolength{\diff}{-\dp\RHS}
  \setlength{\difff}{\dp\RHS}
  \addtolength{\difff}{\diff}
  \begin{tabular}{@{\hspace{.31em}}l@{}l@{}}
          \raise - \difff
          \vbox{%
              \hsize\LLHS
              \hbox to \hsize{\hspace{-2.5em}\rule{\LLHS}{.4pt}\rule{4.5em}{.4pt}}
              \hbox to \hsize{\hspace{-2.5em}\rule[-.75ex]{\LLHS}{.4pt}\rule[-.75ex]{4.5em}{.4pt}}
              \box\LHS
              \hbox to \hsize{\hspace{-2.5em}\rule{\LLHS}{.4pt}\rule{4.5em}{.4pt}}
              \hbox to \hsize{\hspace{-2.5em}\rule[-.75ex]{\LLHS}{.4pt}\rule[-.75ex]{4.5em}{.4pt}}
          }%
          &
          \addtolength{\difff}{-.4pt}
          \raise - \difff
          \vbox{%
              \hsize\LRHS
              \hbox to \hsize{\hspace{1em}\vrule height \diff}
              \box\RHS
              \hbox to \hsize{\hspace{1em}\vrule height \diff}
          }
   \end{tabular}\\
  \hspace{1em}\vrule
  }
\\}%fin dedoublement

\newenvironment{grafcet}[1][H]
{
    \begin{grafcetfloat}[#1]
    \setcounter{CompteurEtapes}{0}
    \begin{tabular}{@{}l@{}}
}%
{%
    \end{tabular}
    \end{grafcetfloat}
}%

%% ----------------------------- FIN / END ---------------------------------------
