KiCad PCB EDA Suite
Loading...
Searching...
No Matches
HPGL_plotter.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2020-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
33/* Some HPGL commands:
34 * Note: the HPGL unit is 25 micrometers
35 * All commands MUST be terminated by a semi-colon or a linefeed.
36 * Spaces can NOT be substituted for required commas in the syntax of a command.
37 *
38 *
39 * AA (Arc Absolute): Angle is a floating point # (requires non integer value)
40 * Draws an arc with the center at (X,Y).
41 * A positive angle creates a counter-clockwise arc.
42 * If the chord angle is specified,
43 * this will be the number of degrees used for stepping around the arc.
44 * If no value is given then a default value of five degrees is used.
45 * AA x, y, a {,b};
46 *
47 * AR (Arc Relative):
48 * AR Dx, Dy, a {, b};
49 *
50 * CA (Alternate Character Set):
51 * CA {n};
52 *
53 * CI (Circle):
54 * CI r {,b};
55 *
56 * CP (Character Plot):
57 * CP {h, v};
58 * h [-127.9999 .. 127.9999] Number of characters horizontally
59 * v [-127.9999 .. 127.9999] Number of characters vertically
60 *
61 * CS (Standard Character Set):
62 * CS {n};
63 *
64 * DR (Relative Direction for Label Text):
65 * DR s, a;
66 *
67 * DI (Absolute Direction for Label Text):
68 * DI {s, a};
69 *
70 * DT (Define Terminator - this character becomes unavailable except to terminate a label string.
71 * Default is ^C control-C):
72 * DT t;
73 *
74 * EA (rEctangle Absolute - Unfilled, from current position to diagonal x,y):
75 * EA x, y;
76 *
77 * ER (rEctangle Relative - Unfilled, from current position to diagonal x,y):
78 * ER x,y;
79 *
80 * FT (Fill Type):
81 * FT {s {,l {a}}};
82 *
83 * IM (Input Mask):
84 * IM {f};
85 *
86 * IN (Initialize): This command instructs the controller to begin processing the HPGL plot file.
87 * Without this, the commands in the file are received but never executed.
88 * If multiple IN s are found during execution of the file,
89 * the controller performs a Pause/Cancel operation.
90 * All motion from the previous job, yet to be executed, is lost,
91 * and the new information is executed.
92 * IN;
93 *
94 * IP Input P1 and P2:
95 * IP {P1x, P1y {, P2x, P2y}};
96 *
97 * IW (Input Window):
98 * IW {XUL, YUL, XOR, YOR};
99 *
100 * LB (Label):
101 * LB c1 .. cn t;
102 *
103 * PA (Plot Absolute): Moves to an absolute HPGL position and sets absolute mode for
104 * future PU and PD commands. If no arguments follow the command,
105 * only absolute mode is set.
106 * PA {x1, y1 {{PU|PD|,} ..., ..., xn, yn}};
107 * P1x, P1y, P2x, P2y [Integer in ASCII]
108 *
109 * PD (Pen Down): Executes <current pen> pen then moves to the requested position
110 * if one is specified. This position is dependent on whether absolute
111 * or relative mode is set. This command performs no motion in 3-D mode,
112 * but the outputs and feedrates are affected.
113 * PD {x, y};
114 *
115 * PM Polygon mode
116 * associated commands:
117 * PM2 End polygon mode
118 * FP Fill polygon
119 * EP Draw polygon outline
120 *
121 * PR (Plot Relative): Moves to the relative position specified and sets relative mode
122 * for future PU and PD commands.
123 * If no arguments follow the command, only relative mode is set.
124 * PR {Dx1, Dy1 {{PU|PD|,} ..., ..., Dxn, Dyn}};
125 *
126 * PS (Paper Size):
127 * PS {n};
128 *
129 * PT (Pen Thickness): in mm
130 * PT {l};
131 *
132 * PU (Pen Up): Executes <current pen> pen then moves to the requested position
133 * if one is specified. This position is dependent on whether absolute
134 * or relative mode is set.
135 * This command performs no motion in 3-D mode, but the outputs
136 * and feedrates are affected.
137 * PU {x, y};
138 *
139 * RA (Rectangle Absolute - Filled, from current position to diagonal x,y):
140 * RA x, y;
141 *
142 * RO (Rotate Coordinate System):
143 * RO;
144 *
145 * RR (Rectangle Relative - Filled, from current position to diagonal x,y):
146 * RR x, y;
147 *
148 * SA (Select Alternate Set):
149 * SA;
150 *
151 * SC (Scale):
152 * SC {Xmin, Xmax, Ymin, Ymax};
153 *
154 * SI (Absolute Character Size):
155 * SI b, h;
156 * b [-127.9999 .. 127.9999, keine 0]
157 * h [-127.9999 .. 127.9999, keine 0]
158 *
159 * SL (Character Slant):
160 * SL {a};
161 * a [-3.5 .. -0.5, 0.5 .. 3.5]
162*
163 * SP (Select Pen): Selects a new pen or tool for use.
164 * If no pen number or a value of zero is given,
165 * the controller performs an EOF (end of file command).
166 * Once an EOF is performed, no motion is executed,
167 * until a new IN command is received.
168 * SP n;
169 *
170 * SR (Relative Character Size):
171 * SR {b, h};
172 * b [-127.9999 .. 127.9999, keine 0]
173 * h [-127.9999 .. 127.9999, keine 0]
174 *
175 * SS (Select Standard Set):
176 * SS;
177 *
178 * TL (Tick Length):
179 * TL {tp {, tm}};
180 *
181 * UC (User Defined Character):
182 * UC {i,} x1, y1, {i,} x2, y2, ... {i,} xn, yn;
183 *
184 * VS (Velocity Select):
185 * VS {v {, n}};
186 * v [1 .. 40] in cm/s
187 * n [1 .. 8]
188 *
189 * XT (X Tick):
190 * XT;
191 *
192 * YT (Y Tick):
193 * YT;
194 */
195
196#include <cstdio>
197
198#include <string_utils.h>
200#include <math/util.h> // for KiROUND
201#include <trigo.h>
202
204
205
207static double dpoint_dist( const VECTOR2D& a, const VECTOR2D& b );
208
209
210// The hpgl command to close a polygon def, fill it and plot outline:
211// PM 2; ends the polygon definition and closes it if not closed
212// FP; fills the polygon
213// EP; draws the polygon outline. It usually gives a better look to the filled polygon
214static const char hpgl_end_polygon_cmd[] = "PM 2; FP; EP;\n";
215
216
217// HPGL scale factor (1 Plotter Logical Unit = 1/40mm = 25 micrometers)
218// PLUsPERDECIMIL = (25.4 / 10000) / 0.025
219static const double PLUsPERDECIMIL = 0.1016;
220
221
223 m_arcTargetChordLength( 0 ),
224 m_arcMinChordDegrees( 5.0, DEGREES_T ),
225 m_lineStyle( LINE_STYLE::SOLID ),
226 m_useUserCoords( false ),
227 m_fitUserCoords( false ),
228 m_current_item( nullptr )
229{
230 SetPenSpeed( 40 ); // Default pen speed = 40 cm/s; Pen speed is *always* in cm
231 SetPenNumber( 1 ); // Default pen num = 1
232 SetPenDiameter( 0.0 );
233}
234
235
236void HPGL_PLOTTER::SetViewport( const VECTOR2I& aOffset, double aIusPerDecimil,
237 double aScale, bool aMirror )
238{
239 m_plotOffset = aOffset;
240 m_plotScale = aScale;
241 m_IUsPerDecimil = aIusPerDecimil;
242 m_iuPerDeviceUnit = PLUsPERDECIMIL / aIusPerDecimil;
243
244 // Compute the paper size in IUs.
246 m_paperSize.x *= 10.0 * aIusPerDecimil;
247 m_paperSize.y *= 10.0 * aIusPerDecimil;
248 m_plotMirror = aMirror;
249}
250
251
253{
255}
256
257
258bool HPGL_PLOTTER::StartPlot( const wxString& aPageNumber )
259{
260 wxASSERT( m_outputFile );
261 fprintf( m_outputFile, "IN;VS%d;PU;PA;SP%d;\n", m_penSpeed, m_penNumber );
262
263 // Set HPGL Pen Thickness (in mm) (useful in polygon fill command)
264 double penThicknessMM = userToDeviceSize( m_penDiameter ) / 40;
265 fprintf( m_outputFile, "PT %.1f;\n", penThicknessMM );
266
267 return true;
268}
269
270
272{
273 wxASSERT( m_outputFile );
274
275 fputs( "PU;\n", m_outputFile );
276
277 flushItem();
279
280 if( m_items.size() > 0 )
281 {
282 if( m_useUserCoords )
283 {
284 if( m_fitUserCoords )
285 {
286 BOX2D bbox = m_items.front().bbox;
287
288 for( HPGL_ITEM const& item : m_items )
289 bbox.Merge( item.bbox );
290
291 fprintf( m_outputFile, "SC%.0f,%.0f,%.0f,%.0f;\n",
292 bbox.GetX(),
293 bbox.GetX() + bbox.GetWidth(),
294 bbox.GetY(),
295 bbox.GetY() + bbox.GetHeight() );
296 }
297 else
298 {
299 VECTOR2D pagesize_device( m_paperSize * m_iuPerDeviceUnit );
300 fprintf( m_outputFile, "SC%.0f,%.0f,%.0f,%.0f;\n",
301 0.0,
302 pagesize_device.x,
303 0.0,
304 pagesize_device.y );
305 }
306 }
307
308 VECTOR2D loc = m_items.begin()->loc_start;
309 bool pen_up = true;
310 LINE_STYLE current_dash = LINE_STYLE::SOLID;
311 int current_pen = m_penNumber;
312
313 for( HPGL_ITEM const& item : m_items )
314 {
315 if( item.loc_start != loc || pen_up )
316 {
317 if( !pen_up )
318 {
319 fputs( "PU;", m_outputFile );
320 pen_up = true;
321 }
322
323 fprintf( m_outputFile, "PA %.0f,%.0f;", item.loc_start.x, item.loc_start.y );
324 }
325
326 if( item.dashType != current_dash )
327 {
328 current_dash = item.dashType;
329 fputs( lineStyleCommand( item.dashType ), m_outputFile );
330 }
331
332 if( item.pen != current_pen )
333 {
334 if( !pen_up )
335 {
336 fputs( "PU;", m_outputFile );
337 pen_up = true;
338 }
339
340 fprintf( m_outputFile, "SP%d;", item.pen );
341 current_pen = item.pen;
342 }
343
344 if( pen_up && !item.lift_before )
345 {
346 fputs( "PD;", m_outputFile );
347 pen_up = false;
348 }
349 else if( !pen_up && item.lift_before )
350 {
351 fputs( "PU;", m_outputFile );
352 pen_up = true;
353 }
354
355 fputs( static_cast<const char*>( item.content.utf8_str() ), m_outputFile );
356
357 if( !item.pen_returns )
358 {
359 // Assume commands drop the pen
360 pen_up = false;
361 }
362
363 if( item.lift_after )
364 {
365 fputs( "PU;", m_outputFile );
366 pen_up = true;
367 }
368 else
369 {
370 loc = item.loc_end;
371 }
372
373 fputs( "\n", m_outputFile );
374 }
375 }
376
377 fputs( "PU;PA;SP0;\n", m_outputFile );
378 fclose( m_outputFile );
379 m_outputFile = nullptr;
380 return true;
381}
382
383
384void HPGL_PLOTTER::SetPenDiameter( double diameter )
385{
386 m_penDiameter = diameter;
387}
388
389
390void HPGL_PLOTTER::Rect( const VECTOR2I& p1, const VECTOR2I& p2, FILL_T aFill, int aWidth )
391{
392 wxASSERT( m_outputFile );
393
394 // EA command seems to always fill the rectangle, so plot as a polygon instead
395 std::vector<VECTOR2I> cornerList;
396
397 cornerList.emplace_back( p1.x, p1.y );
398 cornerList.emplace_back( p2.x, p1.y );
399 cornerList.emplace_back( p2.x, p2.y );
400 cornerList.emplace_back( p1.x, p2.y );
401 cornerList.emplace_back( p1.x, p1.y );
402
403 PlotPoly( cornerList, aFill, aWidth, nullptr );
404}
405
406
407void HPGL_PLOTTER::Circle( const VECTOR2I& aCenter, int aDiameter, FILL_T aFill, int aWidth )
408{
409 wxASSERT( m_outputFile );
410 double radius = userToDeviceSize( aDiameter / 2 );
411 VECTOR2D center_dev = userToDeviceCoordinates( aCenter );
412 SetCurrentLineWidth( aWidth );
413
414 double const circumf = 2.0 * M_PI * radius;
415 double const target_chord_length = m_arcTargetChordLength;
416 EDA_ANGLE chord_angle = ANGLE_360 * target_chord_length / circumf;
417
418 chord_angle = std::clamp( chord_angle, m_arcMinChordDegrees, ANGLE_45 );
419
420 if( aFill == FILL_T::FILLED_SHAPE )
421 {
422 // Draw the filled area
423 MoveTo( aCenter );
424 startOrAppendItem( center_dev, wxString::Format( "PM 0;CI %g,%g;%s",
425 radius,
426 chord_angle.AsDegrees(),
429 m_current_item->bbox.Merge( BOX2D( center_dev - radius,
430 VECTOR2D( 2 * radius, 2 * radius ) ) );
431 PenFinish();
432 }
433
434 if( radius > 0 )
435 {
436 MoveTo( aCenter );
437 startOrAppendItem( center_dev, wxString::Format( "CI %g,%g;",
438 radius,
439 chord_angle.AsDegrees() ) );
441 m_current_item->bbox.Merge( BOX2D( center_dev - radius,
442 VECTOR2D( 2 * radius, 2 * radius ) ) );
443 PenFinish();
444 }
445}
446
447
448void HPGL_PLOTTER::PlotPoly( const std::vector<VECTOR2I>& aCornerList, FILL_T aFill, int aWidth,
449 void* aData )
450{
451 if( aFill == FILL_T::NO_FILL && aWidth <= 0 )
452 return;
453
454 if( aCornerList.size() <= 1 )
455 return;
456
457 // Width less than zero is occasionally used to create background-only
458 // polygons. Don't set that as the plotter line width, that'll cause
459 // trouble. Also, later, skip plotting the outline if this is the case.
460 if( aWidth > 0 )
461 {
462 SetCurrentLineWidth( aWidth );
463 }
464
465 MoveTo( aCornerList[0] );
466 startItem( userToDeviceCoordinates( aCornerList[0] ) );
467
468 if( aFill != FILL_T::NO_FILL )
469 {
470 // Draw the filled area
472
473 m_current_item->content << wxString( "PM 0;\n" ); // Start polygon
474
475 for( unsigned ii = 1; ii < aCornerList.size(); ++ii )
476 LineTo( aCornerList[ii] );
477
478 int ii = aCornerList.size() - 1;
479
480 if( aCornerList[ii] != aCornerList[0] )
481 LineTo( aCornerList[0] );
482
483 m_current_item->content << hpgl_end_polygon_cmd; // Close, fill polygon and draw outlines
485 }
486 else if( aWidth != 0 )
487 {
488 // Plot only the polygon outline.
489 for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
490 LineTo( aCornerList[ii] );
491
492 // Always close polygon if filled.
493 if( aFill != FILL_T::NO_FILL )
494 {
495 int ii = aCornerList.size() - 1;
496
497 if( aCornerList[ii] != aCornerList[0] )
498 LineTo( aCornerList[0] );
499 }
500 }
501
502 PenFinish();
503}
504
505
506void HPGL_PLOTTER::PenTo( const VECTOR2I& pos, char plume )
507{
508 wxASSERT( m_outputFile );
509
510 if( plume == 'Z' )
511 {
512 m_penState = 'Z';
513 flushItem();
514 return;
515 }
516
517 VECTOR2D pos_dev = userToDeviceCoordinates( pos );
519
520 if( plume == 'U' )
521 {
522 m_penState = 'U';
523 flushItem();
524 }
525 else if( plume == 'D' )
526 {
527 m_penState = 'D';
528 startOrAppendItem( lastpos_dev, wxString::Format( "PA %.0f,%.0f;", pos_dev.x, pos_dev.y ) );
529 m_current_item->loc_end = pos_dev;
530 m_current_item->bbox.Merge( pos_dev );
531 }
532
533 m_penLastpos = pos;
534}
535
536
537void HPGL_PLOTTER::SetDash( int aLineWidth, LINE_STYLE aLineStyle )
538{
539 m_lineStyle = aLineStyle;
540 flushItem();
541}
542
543
544void HPGL_PLOTTER::ThickSegment( const VECTOR2I& start, const VECTOR2I& end,
545 int width, OUTLINE_MODE tracemode, void* aData )
546{
547 wxASSERT( m_outputFile );
548
549 // Suppress overlap if pen is too big
550 if( m_penDiameter >= width )
551 {
552 MoveTo( start );
553 FinishTo( end );
554 }
555 else
556 {
557 segmentAsOval( start, end, width, tracemode );
558 }
559}
560
561
562void HPGL_PLOTTER::Arc( const VECTOR2D& aCenter, const EDA_ANGLE& aStartAngle,
563 const EDA_ANGLE& aAngle, double aRadius, FILL_T aFill, int aWidth )
564{
565 if( aRadius <= 0 )
566 return;
567
568 // Avoid integer overflow when calculating the center point
569 if( std::abs( aAngle.AsDegrees() ) < 5 )
570 {
571 polyArc( aCenter, aStartAngle, aAngle, aRadius, aFill, aWidth );
572 return;
573 }
574
575 double const radius_device = userToDeviceSize( aRadius );
576 double const circumf_device = 2.0 * M_PI * radius_device;
577 double const target_chord_length = m_arcTargetChordLength;
578 EDA_ANGLE chord_angle = ANGLE_360 * target_chord_length / circumf_device;
579
580 chord_angle = std::max( m_arcMinChordDegrees, std::min( chord_angle, ANGLE_45 ) );
581
582 VECTOR2D centre_device = userToDeviceCoordinates( aCenter );
583 EDA_ANGLE angle = aAngle;
584
585 if( !m_plotMirror )
586 angle = -angle;
587
588 EDA_ANGLE startAngle = -aStartAngle;
589
590 // Calculate arc start point:
591 VECTOR2I cmap( KiROUND( aCenter.x + aRadius * startAngle.Cos() ),
592 KiROUND( aCenter.y - aRadius * startAngle.Sin() ) );
593 VECTOR2D cmap_dev = userToDeviceCoordinates( cmap );
594
595 startOrAppendItem( cmap_dev, wxString::Format( "AA %.0f,%.0f,%g,%g",
596 centre_device.x,
597 centre_device.y,
598 angle.AsDegrees(),
599 chord_angle.AsDegrees() ) );
600
601 // TODO We could compute the final position and full bounding box instead...
602 m_current_item->bbox.Merge( BOX2D( centre_device - radius_device,
603 VECTOR2D( radius_device * 2, radius_device * 2 ) ) );
605 flushItem();
606}
607
608
609void HPGL_PLOTTER::FlashPadOval( const VECTOR2I& aPos, const VECTOR2I& aSize,
610 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode, void* aData )
611{
612 wxASSERT( m_outputFile );
613
614 VECTOR2I size( aSize );
615 EDA_ANGLE orient( aOrient );
616
617 // The pad will be drawn as an oblong shape with size.y > size.x (Oval vertical orientation 0).
618 if( size.x > size.y )
619 {
620 std::swap( size.x, size.y );
621 orient += ANGLE_90;
622 }
623
624 if( aTraceMode == FILLED )
625 {
626 int deltaxy = size.y - size.x; // distance between centers of the oval
627
628 FlashPadRect( aPos, VECTOR2I( size.x, deltaxy + KiROUND( m_penDiameter ) ), orient,
629 aTraceMode, aData );
630
631 VECTOR2I pt( 0, deltaxy / 2 );
632 RotatePoint( pt, orient );
633 FlashPadCircle( pt + aPos, size.x, aTraceMode, aData );
634
635 pt = VECTOR2I( 0, -deltaxy / 2 );
636 RotatePoint( pt, orient );
637 FlashPadCircle( pt + aPos, size.x, aTraceMode, aData );
638 }
639 else // Plot in outline mode.
640 {
641 sketchOval( aPos, size, orient, KiROUND( m_penDiameter ) );
642 }
643}
644
645
646void HPGL_PLOTTER::FlashPadCircle( const VECTOR2I& pos, int diametre,
647 OUTLINE_MODE trace_mode, void* aData )
648{
649 wxASSERT( m_outputFile );
650 VECTOR2D pos_dev = userToDeviceCoordinates( pos );
651 int radius = diametre / 2;
652
653 if( trace_mode == FILLED )
654 {
655 // if filled mode, the pen diameter is removed from diameter
656 // to keep the pad size
657 radius -= KiROUND( m_penDiameter ) / 2;
658
659 if( radius < 0 )
660 radius = 0;
661 }
662
663 double rsize = userToDeviceSize( radius );
664
665 if( trace_mode == FILLED ) // Plot in filled mode.
666 {
667 // A filled polygon uses always the current point to start the polygon.
668 // Gives a correct current starting point for the circle
669 MoveTo( VECTOR2I( pos.x + radius, pos.y ) );
670
671 // Plot filled area and its outline
672 startOrAppendItem( userToDeviceCoordinates( VECTOR2I( pos.x + radius, pos.y ) ),
673 wxString::Format( "PM 0; PA %.0f,%.0f;CI %.0f;%s",
674 pos_dev.x, pos_dev.y, rsize, hpgl_end_polygon_cmd ) );
677 }
678 else
679 {
680 // Draw outline only:
681 startOrAppendItem( pos_dev, wxString::Format( "CI %.0f;", rsize ) );
684 }
685
686 PenFinish();
687}
688
689
690void HPGL_PLOTTER::FlashPadRect( const VECTOR2I& aPos, const VECTOR2I& aPadSize,
691 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode, void* aData )
692{
693 // Build rect polygon:
694 std::vector<VECTOR2I> corners;
695
696 int dx = aPadSize.x / 2;
697 int dy = aPadSize.y / 2;
698
699 if( aTraceMode == FILLED )
700 {
701 // in filled mode, the pen diameter is removed from size
702 // to compensate the extra size due to this pen size
703 dx -= KiROUND( m_penDiameter ) / 2;
704 dx = std::max( dx, 0);
705 dy -= KiROUND( m_penDiameter ) / 2;
706 dy = std::max( dy, 0);
707 }
708
709
710 corners.emplace_back( - dx, - dy );
711 corners.emplace_back( - dx, + dy );
712 corners.emplace_back( + dx, + dy );
713 corners.emplace_back( + dx, - dy );
714
715 // Close polygon
716 corners.emplace_back( - dx, - dy );
717
718 for( unsigned ii = 0; ii < corners.size(); ii++ )
719 {
720 RotatePoint( corners[ii], aOrient );
721 corners[ii] += aPos;
722 }
723
724 PlotPoly( corners, aTraceMode == FILLED ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL );
725}
726
727
728void HPGL_PLOTTER::FlashPadRoundRect( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
729 int aCornerRadius, const EDA_ANGLE& aOrient,
730 OUTLINE_MODE aTraceMode, void* aData )
731{
732 SHAPE_POLY_SET outline;
733
734 VECTOR2I size = aSize;
735
736 if( aTraceMode == FILLED )
737 {
738 // In filled mode, the pen diameter is removed from size to keep the pad size.
739 size.x -= KiROUND( m_penDiameter ) / 2;
740 size.x = std::max( size.x, 0);
741 size.y -= KiROUND( m_penDiameter ) / 2;
742 size.y = std::max( size.y, 0);
743
744 // keep aCornerRadius to a value < min size x,y < 2:
745 aCornerRadius = std::min( aCornerRadius, std::min( size.x, size.y ) /2 );
746 }
747
748 TransformRoundChamferedRectToPolygon( outline, aPadPos, size, aOrient, aCornerRadius, 0.0, 0,
750
751 // TransformRoundRectToPolygon creates only one convex polygon
752 std::vector<VECTOR2I> cornerList;
753 SHAPE_LINE_CHAIN& poly = outline.Outline( 0 );
754 cornerList.reserve( poly.PointCount() );
755
756 for( int ii = 0; ii < poly.PointCount(); ++ii )
757 cornerList.emplace_back( poly.CPoint( ii ).x, poly.CPoint( ii ).y );
758
759 if( cornerList.back() != cornerList.front() )
760 cornerList.push_back( cornerList.front() );
761
762 PlotPoly( cornerList, aTraceMode == FILLED ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL );
763}
764
765
766void HPGL_PLOTTER::FlashPadCustom( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
767 const EDA_ANGLE& aOrient, SHAPE_POLY_SET* aPolygons,
768 OUTLINE_MODE aTraceMode, void* aData )
769{
770 std::vector<VECTOR2I> cornerList;
771
772 for( int cnt = 0; cnt < aPolygons->OutlineCount(); ++cnt )
773 {
774 SHAPE_LINE_CHAIN& poly = aPolygons->Outline( cnt );
775
776 cornerList.clear();
777 cornerList.reserve( poly.PointCount() );
778
779 for( int ii = 0; ii < poly.PointCount(); ++ii )
780 cornerList.emplace_back( poly.CPoint( ii ).x, poly.CPoint( ii ).y );
781
782 if( cornerList.back() != cornerList.front() )
783 cornerList.push_back( cornerList.front() );
784
785 PlotPoly( cornerList, aTraceMode == FILLED ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL );
786 }
787}
788
789
790void HPGL_PLOTTER::FlashPadTrapez( const VECTOR2I& aPadPos, const VECTOR2I* aCorners,
791 const EDA_ANGLE& aPadOrient, OUTLINE_MODE aTraceMode,
792 void* aData )
793{
794 std::vector<VECTOR2I> cornerList;
795 cornerList.reserve( 5 );
796
797 for( int ii = 0; ii < 4; ii++ )
798 {
799 VECTOR2I coord( aCorners[ii] );
800 RotatePoint( coord, aPadOrient );
801 coord += aPadPos;
802 cornerList.push_back( coord );
803 }
804
805 // Close polygon
806 cornerList.push_back( cornerList.front() );
807
808 PlotPoly( cornerList, aTraceMode == FILLED ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL );
809}
810
811
812void HPGL_PLOTTER::FlashRegularPolygon( const VECTOR2I& aShapePos, int aRadius, int aCornerCount,
813 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode,
814 void* aData )
815{
816 // Do nothing
817 wxASSERT( 0 );
818}
819
820
821bool HPGL_PLOTTER::startItem( const VECTOR2D& location )
822{
823 return startOrAppendItem( location, wxEmptyString );
824}
825
826
828{
829 m_current_item = nullptr;
830}
831
832
833bool HPGL_PLOTTER::startOrAppendItem( const VECTOR2D& location, wxString const& content )
834{
835 if( m_current_item == nullptr )
836 {
837 HPGL_ITEM item;
838 item.loc_start = location;
839 item.loc_end = location;
840 item.bbox = BOX2D( location );
841 item.pen = m_penNumber;
842 item.dashType = m_lineStyle;
843 item.content = content;
844 m_items.push_back( item );
845 m_current_item = &m_items.back();
846 return true;
847 }
848 else
849 {
850 m_current_item->content << content;
851 return false;
852 }
853}
854
855
856void HPGL_PLOTTER::sortItems( std::list<HPGL_ITEM>& items )
857{
858 if( items.size() < 2 )
859 return;
860
861 std::list<HPGL_ITEM> target;
862
863 // Plot items are sorted to improve print time on mechanical plotters. This
864 // means
865 // 1) Avoid excess pen-switching - once a pen is selected, keep printing
866 // with it until no more items using that pen remain.
867 // 2) Within the items for one pen, avoid bouncing back and forth around
868 // the page; items should be sequenced with nearby items.
869 //
870 // This is essentially a variant of the Traveling Salesman Problem where
871 // the cities are themselves edges that must be traversed. This is of course
872 // a famously NP-Hard problem and this particular variant has a monstrous
873 // number of "cities". For now, we're using a naive nearest-neighbor search,
874 // which is less than optimal but (usually!) better than nothing, very
875 // simple to implement, and fast enough.
876 //
877 // Items are moved one at a time from `items` into `target`, searching
878 // each time for the first one matching the above criteria. Then, all of
879 // `target` is moved back into `items`.
880
881 // Get the first one started
882 HPGL_ITEM last_item = items.front();
883 items.pop_front();
884 target.emplace_back( last_item );
885
886 while( !items.empty() )
887 {
888 auto best_it = items.begin();
889 double best_dist = dpoint_dist( last_item.loc_end, best_it->loc_start );
890
891 for( auto search_it = best_it; search_it != items.end(); search_it++ )
892 {
893 // Immediately forget an item as "best" if another one is a better pen match
894 if( best_it->pen != last_item.pen && search_it->pen == last_item.pen )
895 {
896 best_it = search_it;
897 continue;
898 }
899
900 double const dist = dpoint_dist( last_item.loc_end, search_it->loc_start );
901
902 if( dist < best_dist )
903 {
904 best_it = search_it;
905 best_dist = dist;
906 continue;
907 }
908 }
909
910 target.emplace_back( *best_it );
911 last_item = *best_it;
912 items.erase( best_it );
913 }
914
915 items.splice( items.begin(), target );
916}
917
918
920{
921 switch( aLineStyle )
922 {
923 case LINE_STYLE::DASH: return "LT 2 4 1;";
924 case LINE_STYLE::DOT: return "LT 1 1 1;";
925 case LINE_STYLE::DASHDOT: return "LT 4 6 1;";
926 case LINE_STYLE::DASHDOTDOT: return "LT 7 8 1;";
927 default: return "LT;";
928 }
929}
930
931
932static double dpoint_dist( const VECTOR2D& a, const VECTOR2D& b )
933{
934 VECTOR2D diff = a - b;
935 return sqrt( diff.x * diff.x + diff.y * diff.y );
936}
static const double PLUsPERDECIMIL
static double dpoint_dist(const VECTOR2D &a, const VECTOR2D &b)
Compute the distance between two VECTOR2D points.
static const char hpgl_end_polygon_cmd[]
@ ERROR_INSIDE
Definition: approximation.h:34
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
BOX2< VECTOR2D > BOX2D
Definition: box2.h:923
constexpr coord_type GetY() const
Definition: box2.h:208
constexpr size_type GetWidth() const
Definition: box2.h:214
constexpr coord_type GetX() const
Definition: box2.h:207
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:658
constexpr size_type GetHeight() const
Definition: box2.h:215
double Sin() const
Definition: eda_angle.h:170
double AsDegrees() const
Definition: eda_angle.h:113
double Cos() const
Definition: eda_angle.h:189
virtual void FlashPadRoundRect(const VECTOR2I &aPadPos, const VECTOR2I &aSize, int aCornerRadius, const EDA_ANGLE &aOrient, OUTLINE_MODE aTraceMode, void *aData) override
HPGL_ITEM * m_current_item
Definition: plotter_hpgl.h:228
virtual void SetDash(int aLineWidth, LINE_STYLE aLineStyle) override
HPGL supports dashed lines.
virtual void SetPenSpeed(int speed)
Definition: plotter_hpgl.h:88
bool startItem(const VECTOR2D &location)
Start a new HPGL_ITEM if necessary, keeping the current one if it exists.
double m_arcTargetChordLength
Definition: plotter_hpgl.h:221
void flushItem()
Flush the current HPGL_ITEM and clear out the current item pointer.
virtual void FlashPadTrapez(const VECTOR2I &aPadPos, const VECTOR2I *aCorners, const EDA_ANGLE &aPadOrient, OUTLINE_MODE aTraceMode, void *aData) override
Flash a trapezoidal pad.
void SetTargetChordLength(double chord_len)
Set the target length of chords used to draw approximated circles and arcs.
virtual void SetPenNumber(int number)
Definition: plotter_hpgl.h:93
static void sortItems(std::list< HPGL_ITEM > &items)
Sort a list of HPGL items to improve plotting speed on mechanical plotters.
virtual void FlashPadCircle(const VECTOR2I &aPadPos, int aDiameter, OUTLINE_MODE aTraceMode, void *aData) override
virtual bool StartPlot(const wxString &aPageNumber) override
At the start of the HPGL plot pen speed and number are requested.
LINE_STYLE m_lineStyle
Definition: plotter_hpgl.h:223
EDA_ANGLE m_arcMinChordDegrees
Definition: plotter_hpgl.h:222
virtual void SetCurrentLineWidth(int width, void *aData=nullptr) override
HPGL doesn't handle line thickness or color.
Definition: plotter_hpgl.h:75
virtual void FlashRegularPolygon(const VECTOR2I &aShapePos, int aDiameter, int aCornerCount, const EDA_ANGLE &aOrient, OUTLINE_MODE aTraceMode, void *aData) override
Flash a regular polygon.
virtual void PlotPoly(const std::vector< VECTOR2I > &aCornerList, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=nullptr) override
Draw a polygon ( filled or not ).
static const char * lineStyleCommand(LINE_STYLE aLineStyle)
Return the plot command corresponding to a line type.
virtual void Rect(const VECTOR2I &p1, const VECTOR2I &p2, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH) override
virtual void SetViewport(const VECTOR2I &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
bool m_useUserCoords
Definition: plotter_hpgl.h:224
std::list< HPGL_ITEM > m_items
Definition: plotter_hpgl.h:227
virtual void FlashPadCustom(const VECTOR2I &aPadPos, const VECTOR2I &aSize, const EDA_ANGLE &aOrient, SHAPE_POLY_SET *aPolygons, OUTLINE_MODE aTraceMode, void *aData) override
virtual void SetPenDiameter(double diameter)
virtual void FlashPadOval(const VECTOR2I &aPadPos, const VECTOR2I &aSize, const EDA_ANGLE &aPadOrient, OUTLINE_MODE aTraceMode, void *aData) override
double m_penDiameter
Definition: plotter_hpgl.h:220
virtual void Arc(const VECTOR2D &aCenter, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aAngle, double aRadius, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH) override
Plot an arc.
virtual void PenTo(const VECTOR2I &pos, char plume) override
Moveto/lineto primitive, moves the 'pen' to the specified direction.
bool m_fitUserCoords
Definition: plotter_hpgl.h:225
virtual void ThickSegment(const VECTOR2I &start, const VECTOR2I &end, int width, OUTLINE_MODE tracemode, void *aData) override
bool startOrAppendItem(const VECTOR2D &location, const wxString &content)
Start a new HPGL_ITEM with the given string if necessary, or append the string to the current item.
virtual void Circle(const VECTOR2I &aCenter, int aDiameter, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH) override
virtual bool EndPlot() override
HPGL end of plot: sort and emit graphics, pen return and release.
virtual void FlashPadRect(const VECTOR2I &aPadPos, const VECTOR2I &aSize, const EDA_ANGLE &aOrient, OUTLINE_MODE aTraceMode, void *aData) override
const VECTOR2D & GetSizeMils() const
Definition: page_info.h:144
PAGE_INFO m_pageInfo
Definition: plotter.h:676
bool m_plotMirror
Definition: plotter.h:656
static const int USE_DEFAULT_LINE_WIDTH
Definition: plotter.h:109
virtual void polyArc(const VECTOR2D &aCentre, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aAngle, double aRadius, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH)
Generic fallback: arc rendered as a polyline.
Definition: plotter.cpp:181
void MoveTo(const VECTOR2I &pos)
Definition: plotter.h:244
void FinishTo(const VECTOR2I &pos)
Definition: plotter.h:254
double m_iuPerDeviceUnit
Definition: plotter.h:653
VECTOR2I m_plotOffset
Definition: plotter.h:655
VECTOR2I m_penLastpos
Definition: plotter.h:669
virtual VECTOR2D userToDeviceCoordinates(const VECTOR2I &aCoordinate)
Modify coordinates according to the orientation, scale factor, and offsets trace.
Definition: plotter.cpp:91
VECTOR2I m_paperSize
Definition: plotter.h:677
virtual VECTOR2D userToDeviceSize(const VECTOR2I &size)
Modify size according to the plotter scale factors (VECTOR2I version, returns a VECTOR2D).
Definition: plotter.cpp:116
void sketchOval(const VECTOR2I &aPos, const VECTOR2I &aSize, const EDA_ANGLE &aOrient, int aWidth)
Definition: plotter.cpp:501
int GetPlotterArcHighDef() const
Definition: plotter.h:209
char m_penState
Definition: plotter.h:668
double m_plotScale
Plot scale - chosen by the user (even implicitly with 'fit in a4')
Definition: plotter.h:645
FILE * m_outputFile
Output file.
Definition: plotter.h:662
void LineTo(const VECTOR2I &pos)
Definition: plotter.h:249
void PenFinish()
Definition: plotter.h:260
void segmentAsOval(const VECTOR2I &start, const VECTOR2I &end, int width, OUTLINE_MODE tracemode)
Convert a thick segment and plot it as an oval.
Definition: plotter.cpp:486
double m_IUsPerDecimil
Definition: plotter.h:651
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
Represent a set of closed polygons.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int OutlineCount() const
Return the number of outlines in the set.
void TransformRoundChamferedRectToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aPosition, const VECTOR2I &aSize, const EDA_ANGLE &aRotation, int aCornerRadius, double aChamferRatio, int aChamferCorners, int aInflate, int aError, ERROR_LOC aErrorLoc)
Convert a rectangle with rounded corners and/or chamfered corners to a polygon.
static constexpr EDA_ANGLE ANGLE_90
Definition: eda_angle.h:403
@ DEGREES_T
Definition: eda_angle.h:31
static constexpr EDA_ANGLE ANGLE_45
Definition: eda_angle.h:402
static constexpr EDA_ANGLE ANGLE_360
Definition: eda_angle.h:407
FILL_T
Definition: eda_shape.h:55
static const bool FILLED
Definition: gr_basic.cpp:30
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:390
OUTLINE_MODE
Definition: outline_mode.h:25
Plotting engine (HPGL)
LINE_STYLE
Dashed line types.
Definition: stroke_params.h:46
wxString content
Line style for this command.
Definition: plotter_hpgl.h:206
bool pen_returns
Whether the pen returns to its original state after the command.
Definition: plotter_hpgl.h:202
BOX2D bbox
Bounding box of this item.
Definition: plotter_hpgl.h:190
LINE_STYLE dashType
Pen number for this command.
Definition: plotter_hpgl.h:205
bool lift_after
Whether the pen must be lifted after the command.
Definition: plotter_hpgl.h:198
bool lift_before
Whether the command should be executed with the pen lifted.
Definition: plotter_hpgl.h:193
VECTOR2D loc_start
Location the pen should start at.
Definition: plotter_hpgl.h:183
VECTOR2D loc_end
Location the pen will be at when it finishes.
Definition: plotter_hpgl.h:187
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:229
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690