我已经声明了 D 闩锁的形状,我想根据条件键“反转”来反转它以改变它的方向
%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
当我使用
node[dlatch, invert](d2){}
它应该将形状旋转 180 度。
如何包含if
条件或pgftransform
如何执行
答案1
我用的是关联并修改了我的代码以将我的形状翻转过来。
%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