2

I have the code of a Tikz picture below. How can I clip in the up gray annulus of the stator draw? I already tried with \clip (0,0,\H) circle (\r) circle (\R); but the result is not how I want:

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}

% isometric axes \pgfmathsetmacro\xx{1/sqrt(2)} \pgfmathsetmacro\xy{1/sqrt(6)} \pgfmathsetmacro\zz{sqrt(2/3)} \tikzset{isometric/.style={x={(-\xx cm,-\xy cm)},y={(\xx cm,-\xy cm)},z={(0cm,\zz cm)}}}

% parameters \def\nc{24} % number of cogs (multiple of 4, at least 8) \def\H {1} % total height \def\h {0.25} % cog height \def\R {2} % outer radius \def\r {1.5} % inner radius \def\upper {(0,0,\H) circle (\r);(0,0,\H) circle (\R);}

\pgfmathtruncatemacro\nw{\nc/4} % number of cog walls per quadrant \pgfmathsetmacro\a {180/\nc} % step angle \pgfmathsetmacro\is{135+2\a} % inner part step \pgfmathsetmacro\il{315-4\a} % inner part last iteration \pgfmathsetmacro\os{-45+2\a} % outer part step \pgfmathsetmacro\ol{135-4\a} % outer part last iteration \tikzset { inner/.style={fill=gray!30}, outer/.style={fill=gray!10}, cogs/.style= {fill=gray!50}, top/.style= {fill=gray!20}, cut/.style= {color=red!60, dashed}, }

\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=1,xscale=1, isometric,line join=round,line cap=round] % inner cogs side walls \foreach\i in{-\nw,...,\nw} { \pgfmathsetmacro\j{4\nw-\i} \pgfmathsetmacro\k{6\nw+\i} \draw[cogs] (\a\j:\R) -- (\a\j:\r) --++ (0,0,\h) --++ (\a\j:\R-\r) -- cycle; \draw[cogs] (\a\k:\R) -- (\a\k:\r) --++ (0,0,\h) --++ (\a\k:\R-\r) -- cycle; } % inner part \draw[inner] (135:\r) foreach\i in {135,\is,...,\il} {arc (\i:\i+\a:\r) --++ (0,0,\h) arc (\i+\a:\i+2\a:\r) --++ (0,0,-\h)} arc (315-2\a:315-\a:\r) --++ (0,0,\h) arc (315-\a:315:\r) --++ (0,0, \H-\h) arc (315:135:\r) -- cycle; % outer cogs side walls \foreach\i in{-\nw,...,\nw} { \pgfmathsetmacro\j{-\i+2\nw} \draw[cogs] (\a\i:\R) -- (\a\i:\r) --++ (0,0,\h) --++ (\a\i:\R-\r) -- cycle; \draw[cogs] (\a\j:\R) -- (\a\j:\r) --++ (0,0,\h) --++ (\a\j:\R-\r) -- cycle; } % outer part \draw[outer] (-45:\R) foreach\i in {-45,\os,...,\ol} {arc (\i:\i+\a:\R) --++ (0,0,\h) arc (\i+\a:\i+2\a:\R) --++ (0,0,-\h)} arc (135-2*\a:135-\a:\R) --++ (0,0,\h) arc (135-\a:135:\R) --++ (0,0,\H-\h) arc (135:-45:\R) -- cycle;

% top part \draw[even odd rule,top] (0,0,\H) circle (\R) (0,0,\H) circle (\r);

\foreach \i in {3,5,7,9,11,13,15,17} \draw[even odd rule,cut] ({\Rcos(((\i \a)+((\i+1)\a))/2)},{\Rsin(((\i \a)+((\i+1)\a))/2)},0.25) -- ({\Rcos(((\i \a)+((\i+1)\a))/2)},{\Rsin(((\i \a)+((\i+1)\a))/2)},\H);

\foreach \i in {4,6,8,10,12,14,16} \draw[even odd rule,cut] ({\Rcos(((\i \a)+((\i+1)\a))/2)},{\Rsin(((\i \a)+((\i+1)\a))/2)},0) -- ({\Rcos(((\i \a)+((\i+1)\a))/2)},{\Rsin(((\i \a)+((\i+1)\a))/2)},\H);

\foreach \i in {27,29,31,33,35,37} \draw[even odd rule,cut] ({\rcos(((\i \a)+((\i+1)\a))/2)},{\rsin(((\i \a)+((\i+1)\a))/2)},0.25) -- ({\rcos(((\i \a)+((\i+1)\a))/2)},{\rsin(((\i \a)+((\i+1)\a))/2)},\H);

\foreach \i in {28,30,32,34,36,38} \draw[even odd rule,cut] ({\rcos(((\i \a)+((\i+1)\a))/2)},{\rsin(((\i \a)+((\i+1)\a))/2)},0) -- ({\rcos(((\i \a)+((\i+1)\a))/2)},{\rsin(((\i \a)+((\i+1)\a))/2)},\H);

\begin{scope} \clip (0,0,\H) circle (\r) circle (\R); \foreach \i in {3,5,7,9,11,13,15,17} \draw[even odd rule,cut] ({\Rcos(((\i \a)+((\i+1)\a))/2)},{\Rsin(((\i \a)+((\i+1)\a))/2)},\H) -- ({\Rcos(-(((\i \a)+((\i+1)\a))/2))},{\Rsin(-(((\i \a)+((\i+1)\a))/2))},\H);

\foreach \i in {4,6,8,10,12,14,16,18,19,20,21} \draw[even odd rule,cut] ({\Rcos(((\i \a)+((\i+1)\a))/2)},{\Rsin(((\i \a)+((\i+1)\a))/2)},\H) -- ({\Rcos(-(((\i \a))+((\i+1)\a))/2)},{\Rsin(-(((\i \a)+((\i+1)\a)))/2)},\H); \end{scope}

\end{tikzpicture}

\end{document}

1 Answers1

4

Use:

\clip (0,0,\H) circle[radius=\R] (0,0,\H) circle[x radius=-\r, y radius=\r];

To understand this, see: Reverse TikZ stroke direction to use with fill rules

Result:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}

% isometric axes \pgfmathsetmacro\xx{1/sqrt(2)} \pgfmathsetmacro\xy{1/sqrt(6)} \pgfmathsetmacro\zz{sqrt(2/3)} \tikzset{isometric/.style={x={(-\xx cm,-\xy cm)},y={(\xx cm,-\xy cm)},z={(0cm,\zz cm)}}}

% parameters \def\nc{24} % number of cogs (multiple of 4, at least 8) \def\H {1} % total height \def\h {0.25} % cog height \def\R {2} % outer radius \def\r {1.5} % inner radius \def\upper {(0,0,\H) circle (\r);(0,0,\H) circle (\R);}

\pgfmathtruncatemacro\nw{\nc/4} % number of cog walls per quadrant \pgfmathsetmacro\a {180/\nc} % step angle \pgfmathsetmacro\is{135+2\a} % inner part step \pgfmathsetmacro\il{315-4\a} % inner part last iteration \pgfmathsetmacro\os{-45+2\a} % outer part step \pgfmathsetmacro\ol{135-4\a} % outer part last iteration \tikzset { inner/.style={fill=gray!30}, outer/.style={fill=gray!10}, cogs/.style= {fill=gray!50}, top/.style= {fill=gray!20}, cut/.style= {color=red!60, dashed}, }

\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=1,xscale=1, isometric,line join=round,line cap=round] % inner cogs side walls \foreach\i in{-\nw,...,\nw} { \pgfmathsetmacro\j{4\nw-\i} \pgfmathsetmacro\k{6\nw+\i} \draw[cogs] (\a\j:\R) -- (\a\j:\r) --++ (0,0,\h) --++ (\a\j:\R-\r) -- cycle; \draw[cogs] (\a\k:\R) -- (\a\k:\r) --++ (0,0,\h) --++ (\a\k:\R-\r) -- cycle; } % inner part \draw[inner] (135:\r) foreach\i in {135,\is,...,\il} {arc (\i:\i+\a:\r) --++ (0,0,\h) arc (\i+\a:\i+2\a:\r) --++ (0,0,-\h)} arc (315-2\a:315-\a:\r) --++ (0,0,\h) arc (315-\a:315:\r) --++ (0,0, \H-\h) arc (315:135:\r) -- cycle; % outer cogs side walls \foreach\i in{-\nw,...,\nw} { \pgfmathsetmacro\j{-\i+2\nw} \draw[cogs] (\a\i:\R) -- (\a\i:\r) --++ (0,0,\h) --++ (\a\i:\R-\r) -- cycle; \draw[cogs] (\a\j:\R) -- (\a\j:\r) --++ (0,0,\h) --++ (\a\j:\R-\r) -- cycle; } % outer part \draw[outer] (-45:\R) foreach\i in {-45,\os,...,\ol} {arc (\i:\i+\a:\R) --++ (0,0,\h) arc (\i+\a:\i+2\a:\R) --++ (0,0,-\h)} arc (135-2*\a:135-\a:\R) --++ (0,0,\h) arc (135-\a:135:\R) --++ (0,0,\H-\h) arc (135:-45:\R) -- cycle;

% top part \draw[even odd rule,top] (0,0,\H) circle (\R) (0,0,\H) circle (\r);

\foreach \i in {3,5,7,9,11,13,15,17} \draw[even odd rule,cut] ({\Rcos(((\i \a)+((\i+1)\a))/2)},{\Rsin(((\i \a)+((\i+1)\a))/2)},0.25) -- ({\Rcos(((\i \a)+((\i+1)\a))/2)},{\Rsin(((\i \a)+((\i+1)\a))/2)},\H);

\foreach \i in {4,6,8,10,12,14,16} \draw[even odd rule,cut] ({\Rcos(((\i \a)+((\i+1)\a))/2)},{\Rsin(((\i \a)+((\i+1)\a))/2)},0) -- ({\Rcos(((\i \a)+((\i+1)\a))/2)},{\Rsin(((\i \a)+((\i+1)\a))/2)},\H);

\foreach \i in {27,29,31,33,35,37} \draw[even odd rule,cut] ({\rcos(((\i \a)+((\i+1)\a))/2)},{\rsin(((\i \a)+((\i+1)\a))/2)},0.25) -- ({\rcos(((\i \a)+((\i+1)\a))/2)},{\rsin(((\i \a)+((\i+1)\a))/2)},\H);

\foreach \i in {28,30,32,34,36,38} \draw[even odd rule,cut] ({\rcos(((\i \a)+((\i+1)\a))/2)},{\rsin(((\i \a)+((\i+1)\a))/2)},0) -- ({\rcos(((\i \a)+((\i+1)\a))/2)},{\rsin(((\i \a)+((\i+1)\a))/2)},\H);

\begin{scope} \clip (0,0,\H) circle[radius=\R] (0,0,\H) circle[x radius=-\r, y radius=\r]; \foreach \i in {3,5,7,9,11,13,15,17} \draw[even odd rule,cut] ({\Rcos(((\i \a)+((\i+1)\a))/2)},{\Rsin(((\i \a)+((\i+1)\a))/2)},\H) -- ({\Rcos(-(((\i \a)+((\i+1)\a))/2))},{\Rsin(-(((\i \a)+((\i+1)\a))/2))},\H);

\foreach \i in {4,6,8,10,12,14,16,18,19,20,21} \draw[even odd rule,cut] ({\Rcos(((\i \a)+((\i+1)\a))/2)},{\Rsin(((\i \a)+((\i+1)\a))/2)},\H) -- ({\Rcos(-(((\i \a))+((\i+1)\a))/2)},{\Rsin(-(((\i \a)+((\i+1)\a)))/2)},\H); \end{scope}

\end{tikzpicture}

\end{document}

Ring with clipped red lines