\NeedsTeXFormat{LaTeX2e}[1999/01/01]
\ProvidesPackage{schemabloc}[2006/12/29]

\RequirePackage{ifthen}
\RequirePackage{tikz}
\usetikzlibrary{shapes,arrows}


%macros dessin des schéma-blocs mise à jour 6 janvier 2009
% version 1.5

%initialisation des styles
\tikzstyle{sbStyleLien}=[->,>=latex',]
\tikzstyle{sbStyleBloc}=[draw, rectangle,]
\tikzstyle{sbStyleBlocPatate}=[]
\tikzstyle{sbStyleSum}=[draw, circle,]%style Sum CC
% Commandes de changement de style
\newcommand{\sbStyleLienDefaut}{
\tikzstyle{sbStyleLien}=[->,>=latex']
}

\newcommand{\sbStyleLien}[1]{
\tikzstyle{sbStyleLien}+=[#1]
}

\newcommand{\sbStyleBloc}[1]{
\tikzstyle{sbStyleBloc}+=[#1]
}
\newcommand{\sbStyleBlocDefaut}{
\tikzstyle{sbStyleBloc}=[draw, rectangle,]
}

\newcommand{\sbStyleSum}[1]{
\tikzstyle{sbStyleSum}+=[#1]
}

\newcommand{\sbStyleSumDefaut}{
\tikzstyle{sbStyleBloc}=[draw, circle,]
}%style Sum default CC


% Commandes d'entrée et sortie
\newcommand{\sbEntree}[1]{
    \node [coordinate, name=#1] {};
\sbDecaleNoeudx[0]{#1}{#1}
}
\newcommand{\sbSortie}[3][2]{
    \node [coordinate, right of=#3droite, node distance=#1em, minimum size=0em,right] (#2) {};
}

%Commandes de Bloc
\newcommand{\sbBloc}[4][2]{
\node [draw, rectangle, 
    minimum height=3em, minimum width=3em, right of = #4droite,
node distance=#1em,sbStyleBloc,right] (#2) {#3};
\node (#2droite) at (#2.east){};
\node (BlocdeFindroite) at (#2.east){};
}

\newcommand{\sbBlocPatate}[4][2]{
\node [draw, cloud, cloud puffs=5, draw, 
    minimum height=3em, minimum width=5em, right of = #4droite,
node distance=#1em,sbStyleBlocPatate,right] (#2) {#3};
\node (#2droite) at (#2.east){};
}

\newcommand{\sbBlocr}[4][2]{
\node [ 
    minimum height=3em, minimum width=3em, left of = #4gauche, 
node distance=#1em, sbStyleBloc,left] (#2) {#3};
\node (#2gauche) at (#2.west){};
}

\newcommand{\sbBlocL}[4][2]{
\node [draw, rectangle, 
    minimum height=3em, minimum width=3em, right of = #4droite,node distance=#1em,sbStyleBloc,right] (#2) {#3};
\node (#2droite) at (#2.east){};
\node (BlocdeFindroite) at (#2.east){};
 \draw [sbStyleLien,auto] (#4) -- node[name=#4-#2] {} (#2);
}

\newcommand{\sbBlocrL}[4][2]{
\node [draw, rectangle, 
    minimum height=3em, minimum width=3em, left of = #4gauche, 
node distance=#1em, sbStyleBloc,left] (#2) {#3};
\node (#2gauche) at (#2.west){};
\node (BlocdeFingauche) at (#2.west){};
 \draw [sbStyleLien,auto] (#4) -- node[name=#4-#2] {} (#2);
}

\newcommand{\sbBlocseul}[4][1.5]{
\sbEntree{E1}
\sbBloc[#1]{B1}{#3}{E1}
\sbSortie[#1]{S1}{B1}
\sbRelier{E1}{B1}{#2}
\sbRelier{B1}{S1}{#4}
}

%\Commande de lien
\newcommand{\sbRelier}[3][]{
 \draw [sbStyleLien,auto] (#2) -- node[name=#2-#3] {#1} (#3);
}

\newcommand{\sbRelieryx}[2]{
\draw [sbStyleLien] (#1.south)  |-   (#2)  ;
}
\newcommand{\sbRelierxy}[3][]{
\draw [sbStyleLien] (#2)  -|   node[name=#2-#3,near end,right] {#1} (#3) ;
}

\newcommand{\sbRenvoi}[4][4]{
\node [below of=#2, node distance=#1em, minimum size=0em](retour#2) {};
\draw [sbStyleLien] (#2.south)--(retour#2.south)   -|   node[name=#2-#3,near end,right] {#4} (#3) ;
}

\newcommand{\sbNomLien}[3][0.4]{
\node[above of=#2, node distance=#1em] (#2nom) at (#2) {#3};
}
%Commande comparateurs et sommateurs

\newcommand*{\sbCompSum}{\@ifstar\sbCompSumNorm\sbCompSumUsuel}

\newcommand{\sbCompSumUsuel}[7][4]{
    \node [draw, circle,minimum size=2em, right of=#3,node distance=#1em] (#2) {};
	 \node [draw, cross out,minimum size=1.414em,right of=#3,node distance=#1em] {};
	 \node [above of=#2,node distance=0.6em] {$#4$};
	 \node [below of=#2,node distance=0.6em] {$#5$};
	 \node [left of=#2,node distance=0.6em] {$#6$};
	 \node [right of=#2,node distance=0.6em] {$#7$};
\node (#2droite) at (#2.east){};
\node (#2gauche) at (#2.west){};
}

\newcommand{\sbCompSumNorm}[7][4]{
    \node [draw, circle,minimum size=1.5em, right of=#3,node distance=#1em,
label=85:$#4$,label=-85:$#5$,label=175:$#6$,label=5:$#7$,sbStyleSum] (#2) {};
\node (#2droite) at (#2.east){};
\node (#2gauche) at (#2.west){};
}

\newcommand{\sbSum}[6][4]{
    \node [draw, circle,minimum size=1.5em, right of=#3,node distance=#1em,
label=175:$#4$,label=-85:$#5$,label=85:$#6$,sbStyleSum] (#2) {};
\node (#2droite) at (#2.east){};
\node (#2gauche) at (#2.west){};
}

\newcommand*{\sbComp}{\@ifstar\sbCompNorm\sbCompUsuel}
\newcommand{\sbCompUsuel}[3][4]{
\sbCompSum[#1]{#2}{#3}{}{-}{+}{}
}
\newcommand{\sbCompNorm}[3][4]{
\sbCompSum*[#1]{#2}{#3}{}{-}{+}{}
}


\newcommand*{\sbComph}{\@ifstar\sbComphNorm\sbComphUsuel}
\newcommand{\sbComphUsuel}[3][4]{
\sbCompSum[#1]{#2}{#3}{-}{}{+}{}
}

\newcommand{\sbComphNorm}[3][4]{
\sbCompSum*[#1]{#2}{#3}{-}{}{+}{}
}

\newcommand*{\sbSumh}{\@ifstar\sbSumhNorm\sbSumhUsuel}
\newcommand{\sbSumhUsuel}[3][4]{
\sbCompSum[#1]{#2}{#3}{+}{}{+}{}
}
\newcommand{\sbSumhNorm}[3][4]{
\sbCompSum*[#1]{#2}{#3}{+}{}{+}{}
}

\newcommand*{\sbSumb}{\@ifstar\sbSumbNorm\sbSumbUsuel}
\newcommand{\sbSumbUsuel}[3][4]{
\sbCompSum[#1]{#2}{#3}{}{+}{+}{}
}
\newcommand{\sbSumbNorm}[3][4]{
\sbCompSum*[#1]{#2}{#3}{}{+}{+}{}
}


%Commandes de décalage de noeud

\newcommand{\sbDecaleNoeudy}[3][5]{
\node [below of=#2, node distance=#1em, minimum size=0em](#3) {};
\node (#3droite) at (#3){};
\node (#3gauche) at (#3){};
}
\newcommand{\sbDecaleNoeudx}[3][5]{
\node [right of=#2, node distance=#1em, minimum size=0em](#3) {};
\node (#3droite) at (#3){};
\node (#3gauche) at (#3){};
}


%==============Chaines et Boucles===========

\newcommand{\sbChaine}[3][4]{
\foreach \x/\y [remember=\x as \lastx (initially #2)] in {#3}
{\sbBlocL[#1]{\x}{\y}{\lastx}
}
}

\newcommand{\sbChaineRetour}[3][4]{
\foreach \x/\y [remember=\x as \lastx (initially #2)] in {#3}
{\sbBlocrL[#1]{\x}{\y}{\lastx}
}
}

\newcommand{\sbBoucleSeule}[4][4]{
\sbComp[#1]{Comp#2}{#2}\sbRelier{#2}{Comp#2}
\sbChaine[#1]{Comp#2}{#3}
\sbSortie[#1]{#4}{BlocdeFin}
\draw [sbStyleLien,auto] (BlocdeFindroite.base) -- node[name=FindeChaine-#4] {} (#4);
\sbRenvoi{FindeChaine-#4}{Comp#2}{}
}


\newcommand{\sbBoucle}[3][4]{
\sbComp[#1]{Comp#2}{#2}\sbRelier{#2}{Comp#2}
\sbChaine[#1]{Comp#2}{#3}
\draw [sbStyleLien,auto,-] (BlocdeFindroite.base) --++(1em,0)coordinate[name=FindeChaine];
\sbRenvoi{FindeChaine}{Comp#2}{}
}

\newcommand{\sbBoucleRetour}[4][4]{
\sbComp[#1]{Comp#2}{#2}\sbRelier{#2}{Comp#2}
\sbChaine[#1]{Comp#2}{#3}
\draw [sbStyleLien,auto,-] (BlocdeFindroite.base) --++(1em,0)coordinate[name=FindeChaine];
\sbDecaleNoeudy[5]{FindeChaine}{sbDebutRetour}
\sbChaineRetour[#1]{sbDebutRetour}{#4}
\draw [sbStyleLien,-] (FindeChaine)  |-   (sbDebutRetour.west)  ;

\draw [sbStyleLien] (BlocdeFingauche.base)  -|   node[name=sbNomRetour,near end,right] {} (Comp#2) ;

}




%vielles definitions, ne plus utiliser, décommenter si nécessaire
%\newcommand{\bloc}[4][2]{
%\node [draw, rectangle, 
%    minimum height=1cm, minimum width=1cm, right of = #4,node distance=#1cm] (#2) {#3};
%}
%\newcommand{\blocr}[4][1.5]{
%\node [draw, rectangle, 
%    minimum height=1cm, minimum width=1cm, left of = #4, node distance=#1cm] (#2) {#3};
%}
%\newcommand{\comp}[3][1.5]{
%    \node [draw, circle,minimum size=1cm, right of=#3,node distance=#1cm] (#2) {};
%	 \node [draw, cross out,minimum size=0.707cm,right of=#3,node distance=#1cm] {};
%	 \node [below of=#2,node distance=0.3cm] {$-$};
%		\node [left of=#2,node distance=0.3cm] {$+$};
%}
%\newcommand{\comph}[3][2]{
%    \node [draw, circle,minimum size=1cm, right of=#3,node distance=#1cm] (#2) {};
%	 \node [draw, cross out,minimum size=0.707cm,right of=#3,node distance=#1cm] {};
%	 \node [above of=#2,node distance=0.3cm] {$-$};
%		\node [left of=#2,node distance=0.3cm] {$+$};
%}
%\newcommand{\sumh}[3][2]{
%    \node [draw, circle,minimum size=1cm, right of=#3,node distance=#1cm] (#2) {};
%	 \node [draw, cross out,minimum size=0.707cm,right of=#3,node distance=#1cm] {};
%	 \node [above of=#2,node distance=0.3cm] {$+$};
%		\node [left of=#2,node distance=0.3cm] {$+$};
%}
%\newcommand{\sumb}[3][2]{
%    \node [draw, circle,minimum size=1cm, right of=#3,node distance=#1cm] (#2) {};
%	 \node [draw, cross out,minimum size=0.707cm,right of=#3,node distance=#1cm] {};
%	 \node [below of=#2,node distance=0.3cm] {$+$};
%		\node [left of=#2,node distance=0.3cm] {$+$};
%}
%
%\newcommand{\entree}[1]{
%    \node [coordinate, name=#1,node distance=1cm] {};
%}
%\newcommand{\sortie}[3][1.5]{
%    \node [coordinate, right of=#3, node distance=#1cm, minimum size=0cm] (#2) {};
%}
%\newcommand{\decale}[3][2]{
%\node [below of=#2, node distance=#1cm, minimum size=0cm](#3) {};
%}
%
%\newcommand{\relier}[3]{
% \draw [->,>=latex',auto] (#1) -- node[name=#1-#2] {#3} (#2);
%}
%\newcommand{\relieryx}[2]{
%\draw [->,>=latex'] (#1.south)  |-   (#2)  ;
%}
%\newcommand{\relierxy}[3]{
%\draw [->,>=latex'] (#1)  -|   node[name=#1-#2,near end,right] {#3} (#2) ;
%}
%\newcommand{\renvoi}[4][1.5]{
%\node [below of=#2, node distance=#1cm, minimum size=0cm](retour#2) {};
%\draw [->,>=latex'] (#2.south)--(retour#2.south)   -|   node[name=#2-#3,near end,right] {#4} (#3) ;
%}
%\newcommand{\blocseul}[4][1.5]{
%\entree{E1}
%\bloc[#1]{B1}{#3}{E1}
%\sortie[#1]{S1}{B1}
%\relier{E1}{B1}{#2}
%\relier{B1}{S1}{#4}
%}
