2

Motivation

I have a document in which I draw dashed lines. Ultimately, I want to use it for a laser-cutting project which needs to cut the lines in a dashed fashion.

In TikZ, I define the paths (straight lines, arcs, etc.) with a special style:

In preamble:

\tikzset{LaserCutterOutlineCut/.style={dash pattern=on 20pt off 1 pt}}%
\newcommand*{\LaserCutterLineWidth}{0.04cm}

In a figure (assuming the (x1,y1) and (x2,y2) points are defined above):

\draw[style=LaserCutterOutlineCut,line width=\LaserCutterLineWidth] (x1,y1) rectangle (x2,y2);

The Problem

When I run pdflatexmk through TexShop, it creates a PDF that looks awesome. When I import the PDF in Adobe Illustrator, the path is a single path with appropriate dashed lines. However, when I export it to a different format that the laser-cutter can use, the laser cutter sees it as a single, solid path — the dashed metadata is stripped away.

My Question

Is there a way to force TikZ to render a given dashed path as a series of paths according to the specified dashed line style instead of rendering it as a single path?

If it doesn't exist, I can think of a workaround: one could write one or more wrapper functions and redefine the original functions such that whenever one draws a rectangle or arc or circle (etc.) in a dashed line style, it calls the wrapper function instead. The wrapper function then would iterate in a for loop and instead draws a series of paths. Unfortunately, the syntax needed for that is several steps beyond my current understanding.

jvriesem
  • 1,149
  • 1
    I think that decorations work by replacing the path in the manner you need. There might even be an already existing one. Take a look at the decorations section of the pgf manual. – Andrew Stacey Jul 13 '21 at 19:43
  • My solution is not optimal at all. Can you describe a little more about your needed result? Do your small line segments need to individually follow a curve? -or can they be straight line segments? Are they always 20pt on 1pt off? What should happen when there are not an integer number of segments on a line? How would you generate such a line in other programs? – hpekristiansen Jul 13 '21 at 21:31
  • @hpekristiansen: When the PDF gets rendered, geometrical shapes are represented as a path with attributes — just as in TikZ. When the PDF is opened in Adobe Illustrator, the path is a single path and the attributes remain. However, exporting it (within AI) to another format leaves only the path and removes the attributes, so the dashed nature is gone: it's a single, solid line. I'm hoping there's a way to render the dashes in a dashed line independently — as separate graphical objects — in a PDF. Does that help? – jvriesem Jul 14 '21 at 16:47
  • @jvriesem: No it does not help :o) I already understood what you are writing from your original question. -That is why I wrote my answer, using the information given by @Andrew Stacey that a decoration makes individual paths. Do my answer work? I am not happy with my answer! It can be made better, but I need to know more. Please see my questions. – hpekristiansen Jul 14 '21 at 17:55

1 Answers1

1

I do not know if there exist a dashed decoration, but a border decoration with angle=0 will behave like that with straight line segments.

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary {decorations.pathreplacing}
\tikzset{LaserCutterOutlineCut/.style={decoration={border, angle=0, amplitude=20pt, segment length=21pt}, decorate, line width=0.04cm}}
\begin{document}
\begin{tikzpicture}
\draw[LaserCutterOutlineCut] (0,0) rectangle (3,2);
\end{tikzpicture}
\end{document}

Dashed rectangle

To avoid the overshooting, a post decoration and a segmented rectangle can be used:

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary {decorations.pathreplacing, decorations.pathmorphing}
\tikzset{LaserCutterOutlineCut/.style={
 decoration={border, angle=0, amplitude=20pt, segment length=21pt, post length=14pt, post=lineto},
 line width=0.04cm,
  }}
\begin{document}
\begin{tikzpicture}
\newcommand{\segmentrect}[3]{
  decorate{ #1 -- ++ (#2,0) }
  decorate{ -- ++ (0,#3) }
  decorate{ -- ++ ({-(#2)},0) }
  decorate{ -- #1 }
}
\draw[LaserCutterOutlineCut]   \segmentrect{(0,0)}{3}{2};
\end{tikzpicture}
\end{document}

Dashed rectangle without overshoot

Segmented rectangle from: https://tex.stackexchange.com/a/604455/8650