3

I have declared a shape for D latch, which I wanted to invert based on conditional key "invert" to change it's direction

%D Latch
\makeatletter
\pgfkeys{/pgf/.cd,
    latch dff label/.initial=,% 
    latch d label/.initial= D,%
    latch q label/.initial=$\mathrm{Q}$,
    latch ck label/.initial=CK,%
    latch ck edged/.is if=pgfckedged,
}%
\tikzset{latch/port labels/.style={font=\sffamily\small}}%
\newif\ifpgfckedged%
% D Latch shape
\pgfdeclareshape{dlatch}{%

    \savedmacro\operatorparameters{%
    \pgfmathsetlengthmacro\minimumwidth{\pgfkeysvalueof{/pgf/minimum width}}%
    \pgfmathsetlengthmacro\minimumheight{\pgfkeysvalueof{/pgf/minimum height}}%
    \pgfmathsetlengthmacro\spec{max(\minimumwidth , \minimumheight)/8}%    
    \addtosavedmacro{\spec}%
    \pgfmathsetlengthmacro\radius{min(\minimumwidth , \minimumheight)/16}%    
    \addtosavedmacro{\radius}%
    }%

  \savedanchor\northeast{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=0.5\pgf@x
    \pgf@y=0.5\pgf@y
  }
  % This is redundant, but makes some things easier:
  \savedanchor\southwest{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=-0.5\pgf@x
    \pgf@y=-0.5\pgf@y
  }%
  % Inherit from rectangle
  \inheritanchorborder[from=rectangle]

  % Define same anchor a normal rectangle has
  \anchor{center}{\pgfpointorigin}
  \anchor{north}{\northeast \pgf@x=0pt}
  \anchor{east}{\northeast \pgf@y=0pt}
  \anchor{south}{\southwest \pgf@x=0pt}
  \anchor{west}{\southwest \pgf@y=0pt}
  \anchor{north east}{\northeast}
  \anchor{north west}{\northeast \pgf@x=-\pgf@x}
  \anchor{south west}{\southwest}
  \anchor{south east}{\southwest \pgf@x=-\pgf@x}
  \anchor{text}{
    \pgfpointorigin
    \advance\pgf@x by -.5\wd\pgfnodeparttextbox%
    \advance\pgf@y by -.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by +.5\dp\pgfnodeparttextbox%
  }

  \anchor{D}{
    \pgf@process{\southwest}%
    \operatorparameters%
    \pgfmathsetlength\pgf@xa{\spec}
    \advance\pgf@x by -\pgf@xa
    \pgf@y=-0.5\pgf@x%
    \pgf@x=\pgf@x%
  }


  \anchor{CK}{
    \pgf@process{\southwest}%
    \operatorparameters%
    \pgfmathsetlength\pgf@xa{\spec}
    \advance\pgf@x by -\pgf@xa    
    \pgf@x=1\pgf@x%
%    \advance\pgf@x by {-0.5pt}%
    \pgf@y=0.5\pgf@x%
  }  

  \anchor{Q}{
    \pgf@process{\northeast}%
    \operatorparameters%
    \pgfmathsetlength\pgf@xa{\spec}
    \advance\pgf@x by \pgf@xa
    \pgf@y=0.5\pgf@y%
    \pgf@x=\pgf@x%
  }


  % Draw the rectangle box and the port labels
  \backgroundpath{      
    \operatorparameters%
    % Rectangle box
    \pgfpathrectanglecorners{\southwest}{\northeast}

    % Draw the angle (>) for clock input, if required
    \ifpgfckedged%
        \pgf@xa=\pgf@x \pgf@ya=-0.2\pgf@yb %Check
        \pgf@process{\northeast}%
        \pgfpathmoveto{\pgfpoint{\pgf@x}{\pgf@y}}
        \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@ya}}
        \pgfpathlineto{\pgfpoint{-\pgf@x}{\pgf@y}}
    \else%      
    \fi% 

    \csname pgf@anchor@dlatch@D\endcsname%
    \pgf@xa=\pgf@x \pgf@ya=-\pgf@y
    \pgf@xb=\pgf@x \pgf@yb=-\pgf@y
    \pgfmathsetlength\pgf@x{\spec}
    \advance\pgf@xb by \pgf@x
    \pgfpathmoveto{\pgfpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@ya}}

    \csname pgf@anchor@dlatch@CK\endcsname%
    \pgf@xa=\pgf@x \pgf@ya=-\pgf@y
    \pgf@xb=\pgf@x \pgf@yb=-\pgf@y
    \pgfmathsetlength\pgf@x{\spec}
    \advance\pgf@xb by \pgf@x
    \pgfpathmoveto{\pgfpoint{\pgf@xb}{\pgf@yb}}
        \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@ya}} 

    \csname pgf@anchor@dlatch@Q\endcsname%
    \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfmathsetlength\pgf@x{\spec}
    \advance\pgf@xb by -\pgf@x
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}

    % Draw port labels
    \pgfusepath{stroke}
  }
 \foregroundpath{
   { 
   \operatorparameters%
    % Draw port labels
    \begingroup
    \tikzset{latch/port labels} % Use font from this style
    \tikz@textfont%


    \pgf@anchor@dlatch@D%
      \pgftext[left,base,at={\pgfpoint{\pgf@x+\spec+\radius}{\pgf@y}}]{%
    \raisebox{-.5ex}{\pgfkeysvalueof{/pgf/latch d label}}%
      }%

    \pgf@anchor@dlatch@CK%
      \pgftext[left,base,at={\pgfpoint{\pgf@x+\spec+\radius}{\pgf@y}}]{%
    \raisebox{-.5ex}{\pgfkeysvalueof{/pgf/latch ck label}}%
      }%

    \pgf@anchor@dlatch@Q%
      \pgftext[right,base,at={\pgfpoint{\pgf@x-\spec-\radius}{\pgf@y}}]{%
    \raisebox{-.5ex}{\pgfkeysvalueof{/pgf/latch q label}}%
      }%

    \endgroup
   }% 
}%
}%

\tikzset{add font/.code={\expandafter\def\expandafter\tikz@textfont\expandafter{\tikz@textfont#1}}} 

% Define default style for this node

\tikzset{every dlatch node/.style={draw,minimum width=1.5cm,%{\width},
minimum height=2cm,inner sep=1mm,outer sep=0pt,cap=round,add font=\sffamily}}

\makeatother

and when I use

node[dlatch, invert](d2){}

it should rotate the shape by 180 degree.

How to include the if condition or pgftransform to do it

1
  • This is a very elaborate example. Would you mind presenting a complete MWE. And can't you just use transform canvas={rotate=180}? This will (also) rotate the shape by 180 degrees.
    – user121799
    Apr 24, 2018 at 14:57

1 Answer 1

1

I used the link and modified my code to flip my shape up-side down.

%D Latch
\makeatletter
\newif\ifinvert%

% The keys that the user can provide are all the keys below, except 
% "invert" 
\pgfkeys{
    /latchnode/invert/.is if=invert, 
    /pgf/.cd,
    invert/.code={\pgfkeys{/latchnode/invert=true}},  
    latch dff label/.initial=,%Latch,% 
    latch d label/.initial= D,%
    latch q label/.initial=$\mathrm{Q}$,%   latch qb label/.initial= ,%$\mathrm{Q}$,%
    latch ck label/.initial=CK,%
    latch ck edged/.is if=pgfckedged,%  
}%

% Define default style for this node 

\tikzset{latch/port labels/.style={font=\sffamily\small}}%

\newif\ifpgfckedged%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%  D Latch shape  %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfdeclareshape{dlatch}{%

    \savedmacro\operatorparameters{%
        \pgfmathsetlengthmacro\minimumwidth{\pgfkeysvalueof{/pgf/minimum width}}%
        \pgfmathsetlengthmacro\minimumheight{\pgfkeysvalueof{/pgf/minimum height}}%
        \pgfmathsetlengthmacro\spec{max(\minimumwidth , \minimumheight)/8}%    
        \addtosavedmacro{\spec}%
        \pgfmathsetlengthmacro\radius{min(\minimumwidth , \minimumheight)/16}%    
        \addtosavedmacro{\radius}%
    }%


  \savedanchor\northeast{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=0.5\pgf@x
    \pgf@y=0.5\pgf@y    
  }%
  % This is redundant, but makes some things easier:
  \savedanchor\southwest{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=-0.5\pgf@x
    \pgf@y=-0.5\pgf@y
  }%

  % Inherit from rectangle
  \inheritanchorborder[from=rectangle]

  % Define same anchor a normal rectangle has
  \anchor{center}{\pgfpointorigin}
  \anchor{north}{\northeast \pgf@x=0pt}
  \anchor{east}{\northeast \pgf@y=0pt}
  \anchor{south}{\southwest \pgf@x=0pt}
  \anchor{west}{\southwest \pgf@y=0pt}
  \anchor{north east}{\northeast}
  \anchor{north west}{\northeast \pgf@x=-\pgf@x}
  \anchor{south west}{\southwest}
  \anchor{south east}{\southwest \pgf@x=-\pgf@x}
  \anchor{text}{
    \pgfpointorigin
    \advance\pgf@x by -.5\wd\pgfnodeparttextbox%
    \advance\pgf@y by -.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by +.5\dp\pgfnodeparttextbox%
  }

  \anchor{CK}{
    \pgf@process{\southwest}%
    \operatorparameters%
    \pgfmathsetlength\pgf@xa{\spec}
    \advance\pgf@x by -\pgf@xa
    \ifinvert%
        \pgf@y=0.5\pgf@x%
    \else%
        \pgf@y=-0.5\pgf@x%
    \fi%
    \pgf@x=\pgf@x%
  }

  \anchor{D}{
    \pgf@process{\southwest}%
    \operatorparameters%
    \pgfmathsetlength\pgf@xa{\spec}
    \advance\pgf@x by -\pgf@xa    
    \pgf@x=1\pgf@x%
    \ifinvert%
        \pgf@y=-0.5\pgf@x%
    \else%
        \pgf@y=0.5\pgf@x%
    \fi%
  }  

  \anchor{Q}{
    \pgf@process{\northeast}%
    \operatorparameters%
    \pgfmathsetlength\pgf@xa{\spec}
    \advance\pgf@x by \pgf@xa
    \ifinvert%
        \pgf@y=-0.5\pgf@y%
    \else%
        \pgf@y=0.5\pgf@y%
    \fi% 
    \pgf@x=\pgf@x%  
  }

  % Draw the rectangle box and the port labels
  \backgroundpath{      
    \operatorparameters%
    % Rectangle box
    \pgfpathrectanglecorners{\southwest}{\northeast}

    % Draw the angle (>) for clock input, if required
    \ifpgfckedged%
        \pgf@xa=\pgf@x \pgf@ya=-0.2\pgf@yb %Check
        \pgf@process{\northeast}%
        \pgfpathmoveto{\pgfpoint{\pgf@x}{\pgf@y}}
        \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@ya}}
        \pgfpathlineto{\pgfpoint{-\pgf@x}{\pgf@y}}
    \else%      
    \fi% 

    \csname pgf@anchor@dlatch@D\endcsname%
    \pgf@xa=\pgf@x \pgf@ya=-\pgf@y
    \pgf@xb=\pgf@x \pgf@yb=-\pgf@y
    \pgfmathsetlength\pgf@x{\spec}
    \advance\pgf@xb by \pgf@x
    \pgfpathmoveto{\pgfpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@ya}}

    \csname pgf@anchor@dlatch@CK\endcsname%
    \pgf@xa=\pgf@x \pgf@ya=-\pgf@y
    \pgf@xb=\pgf@x \pgf@yb=-\pgf@y
    \pgfmathsetlength\pgf@x{\spec}
    \advance\pgf@xb by \pgf@x
    \pgfpathmoveto{\pgfpoint{\pgf@xb}{\pgf@yb}}
        \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@ya}} 

    \csname pgf@anchor@dlatch@Q\endcsname%
    \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfmathsetlength\pgf@x{\spec}
    \advance\pgf@xb by -\pgf@x
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}

    % Draw port labels
    \pgfusepath{stroke}
  }
 \foregroundpath{
   { 
   \operatorparameters%
    % Draw port labels
    \begingroup
    \tikzset{latch/port labels} % Use font from this style
    \tikz@textfont%

    \pgf@anchor@dlatch@D%
      \pgftext[left,base,at={\pgfpoint{\pgf@x+\spec+\radius}{\pgf@y}}]{%
    \raisebox{-.5ex}{\pgfkeysvalueof{/pgf/latch d label}}%
      }%

    \pgf@anchor@dlatch@CK%
      \pgftext[left,base,at={\pgfpoint{\pgf@x+\spec+\radius}{\pgf@y}}]{%
    \raisebox{-.5ex}{\pgfkeysvalueof{/pgf/latch ck label}}%
      }%

    \pgf@anchor@dlatch@Q%
      \pgftext[right,base,at={\pgfpoint{\pgf@x-\spec-\radius}{\pgf@y}}]{%
    \raisebox{-.5ex}{\pgfkeysvalueof{/pgf/latch q label}}%
      }%

    \endgroup
   }% 
}%
}%
% Key to add font macros to the current font
\tikzset{add font/.code={\expandafter\def\expandafter\tikz@textfont\expandafter{\tikz@textfont#1}}} 

% Define default style for this node
%\tikzset{flip flop/port labels/.style={font=\sffamily\scriptsize}}
\tikzset{every dlatch node/.style={draw,minimum width=1.5cm,%{\width},
minimum height=2cm,inner sep=1mm,outer sep=0pt,cap=round,add font=\sffamily}}

\makeatother

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .