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 VECTOR2I 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 double const radius_device = userToDeviceSize( aRadius );
569 double const circumf_device = 2.0 * M_PI * radius_device;
570 double const target_chord_length = m_arcTargetChordLength;
571 EDA_ANGLE chord_angle = ANGLE_360 * target_chord_length / circumf_device;
572
573 chord_angle = std::max( m_arcMinChordDegrees, std::min( chord_angle, ANGLE_45 ) );
574
575 VECTOR2D centre_device = userToDeviceCoordinates( aCenter );
576 EDA_ANGLE angle = aAngle;
577
578 if( !m_plotMirror )
579 angle = -angle;
580
581 EDA_ANGLE startAngle = -aStartAngle;
582
583 // Calculate arc start point:
584 VECTOR2I cmap( KiROUND( aCenter.x + aRadius * startAngle.Cos() ),
585 KiROUND( aCenter.y - aRadius * startAngle.Sin() ) );
586 VECTOR2D cmap_dev = userToDeviceCoordinates( cmap );
587
588 startOrAppendItem( cmap_dev, wxString::Format( "AA %.0f,%.0f,%g,%g",
589 centre_device.x,
590 centre_device.y,
591 angle.AsDegrees(),
592 chord_angle.AsDegrees() ) );
593
594 // TODO We could compute the final position and full bounding box instead...
595 m_current_item->bbox.Merge( BOX2D( centre_device - radius_device,
596 VECTOR2D( radius_device * 2, radius_device * 2 ) ) );
598 flushItem();
599}
600
601
602void HPGL_PLOTTER::FlashPadOval( const VECTOR2I& aPos, const VECTOR2I& aSize,
603 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode, void* aData )
604{
605 wxASSERT( m_outputFile );
606
607 VECTOR2I size( aSize );
608 EDA_ANGLE orient( aOrient );
609
610 // The pad will be drawn as an oblong shape with size.y > size.x (Oval vertical orientation 0).
611 if( size.x > size.y )
612 {
613 std::swap( size.x, size.y );
614 orient += ANGLE_90;
615 }
616
617 if( aTraceMode == FILLED )
618 {
619 int deltaxy = size.y - size.x; // distance between centers of the oval
620
621 FlashPadRect( aPos, VECTOR2I( size.x, deltaxy + KiROUND( m_penDiameter ) ), orient,
622 aTraceMode, aData );
623
624 VECTOR2I pt( 0, deltaxy / 2 );
625 RotatePoint( pt, orient );
626 FlashPadCircle( pt + aPos, size.x, aTraceMode, aData );
627
628 pt = VECTOR2I( 0, -deltaxy / 2 );
629 RotatePoint( pt, orient );
630 FlashPadCircle( pt + aPos, size.x, aTraceMode, aData );
631 }
632 else // Plot in outline mode.
633 {
634 sketchOval( aPos, size, orient, KiROUND( m_penDiameter ) );
635 }
636}
637
638
639void HPGL_PLOTTER::FlashPadCircle( const VECTOR2I& pos, int diametre,
640 OUTLINE_MODE trace_mode, void* aData )
641{
642 wxASSERT( m_outputFile );
643 VECTOR2D pos_dev = userToDeviceCoordinates( pos );
644 int radius = diametre / 2;
645
646 if( trace_mode == FILLED )
647 {
648 // if filled mode, the pen diameter is removed from diameter
649 // to keep the pad size
650 radius -= KiROUND( m_penDiameter ) / 2;
651
652 if( radius < 0 )
653 radius = 0;
654 }
655
656 double rsize = userToDeviceSize( radius );
657
658 if( trace_mode == FILLED ) // Plot in filled mode.
659 {
660 // A filled polygon uses always the current point to start the polygon.
661 // Gives a correct current starting point for the circle
662 MoveTo( VECTOR2I( pos.x + radius, pos.y ) );
663
664 // Plot filled area and its outline
665 startOrAppendItem( userToDeviceCoordinates( VECTOR2I( pos.x + radius, pos.y ) ),
666 wxString::Format( "PM 0; PA %.0f,%.0f;CI %.0f;%s",
667 pos_dev.x, pos_dev.y, rsize, hpgl_end_polygon_cmd ) );
670 }
671 else
672 {
673 // Draw outline only:
674 startOrAppendItem( pos_dev, wxString::Format( "CI %.0f;", rsize ) );
677 }
678
679 PenFinish();
680}
681
682
683void HPGL_PLOTTER::FlashPadRect( const VECTOR2I& aPos, const VECTOR2I& aPadSize,
684 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode, void* aData )
685{
686 // Build rect polygon:
687 std::vector<VECTOR2I> corners;
688
689 int dx = aPadSize.x / 2;
690 int dy = aPadSize.y / 2;
691
692 if( aTraceMode == FILLED )
693 {
694 // in filled mode, the pen diameter is removed from size
695 // to compensate the extra size due to this pen size
696 dx -= KiROUND( m_penDiameter ) / 2;
697 dx = std::max( dx, 0);
698 dy -= KiROUND( m_penDiameter ) / 2;
699 dy = std::max( dy, 0);
700 }
701
702
703 corners.emplace_back( - dx, - dy );
704 corners.emplace_back( - dx, + dy );
705 corners.emplace_back( + dx, + dy );
706 corners.emplace_back( + dx, - dy );
707
708 // Close polygon
709 corners.emplace_back( - dx, - dy );
710
711 for( unsigned ii = 0; ii < corners.size(); ii++ )
712 {
713 RotatePoint( corners[ii], aOrient );
714 corners[ii] += aPos;
715 }
716
717 PlotPoly( corners, aTraceMode == FILLED ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL );
718}
719
720
721void HPGL_PLOTTER::FlashPadRoundRect( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
722 int aCornerRadius, const EDA_ANGLE& aOrient,
723 OUTLINE_MODE aTraceMode, void* aData )
724{
725 SHAPE_POLY_SET outline;
726
727 VECTOR2I size = aSize;
728
729 if( aTraceMode == FILLED )
730 {
731 // In filled mode, the pen diameter is removed from size to keep the pad size.
732 size.x -= KiROUND( m_penDiameter ) / 2;
733 size.x = std::max( size.x, 0);
734 size.y -= KiROUND( m_penDiameter ) / 2;
735 size.y = std::max( size.y, 0);
736
737 // keep aCornerRadius to a value < min size x,y < 2:
738 aCornerRadius = std::min( aCornerRadius, std::min( size.x, size.y ) /2 );
739 }
740
741 TransformRoundChamferedRectToPolygon( outline, aPadPos, size, aOrient, aCornerRadius, 0.0, 0,
743
744 // TransformRoundRectToPolygon creates only one convex polygon
745 std::vector<VECTOR2I> cornerList;
746 SHAPE_LINE_CHAIN& poly = outline.Outline( 0 );
747 cornerList.reserve( poly.PointCount() );
748
749 for( int ii = 0; ii < poly.PointCount(); ++ii )
750 cornerList.emplace_back( poly.CPoint( ii ).x, poly.CPoint( ii ).y );
751
752 if( cornerList.back() != cornerList.front() )
753 cornerList.push_back( cornerList.front() );
754
755 PlotPoly( cornerList, aTraceMode == FILLED ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL );
756}
757
758
759void HPGL_PLOTTER::FlashPadCustom( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
760 const EDA_ANGLE& aOrient, SHAPE_POLY_SET* aPolygons,
761 OUTLINE_MODE aTraceMode, void* aData )
762{
763 std::vector<VECTOR2I> cornerList;
764
765 for( int cnt = 0; cnt < aPolygons->OutlineCount(); ++cnt )
766 {
767 SHAPE_LINE_CHAIN& poly = aPolygons->Outline( cnt );
768
769 cornerList.clear();
770 cornerList.reserve( poly.PointCount() );
771
772 for( int ii = 0; ii < poly.PointCount(); ++ii )
773 cornerList.emplace_back( poly.CPoint( ii ).x, poly.CPoint( ii ).y );
774
775 if( cornerList.back() != cornerList.front() )
776 cornerList.push_back( cornerList.front() );
777
778 PlotPoly( cornerList, aTraceMode == FILLED ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL );
779 }
780}
781
782
783void HPGL_PLOTTER::FlashPadTrapez( const VECTOR2I& aPadPos, const VECTOR2I* aCorners,
784 const EDA_ANGLE& aPadOrient, OUTLINE_MODE aTraceMode,
785 void* aData )
786{
787 std::vector<VECTOR2I> cornerList;
788 cornerList.reserve( 5 );
789
790 for( int ii = 0; ii < 4; ii++ )
791 {
792 VECTOR2I coord( aCorners[ii] );
793 RotatePoint( coord, aPadOrient );
794 coord += aPadPos;
795 cornerList.push_back( coord );
796 }
797
798 // Close polygon
799 cornerList.push_back( cornerList.front() );
800
801 PlotPoly( cornerList, aTraceMode == FILLED ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL );
802}
803
804
805void HPGL_PLOTTER::FlashRegularPolygon( const VECTOR2I& aShapePos, int aRadius, int aCornerCount,
806 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode,
807 void* aData )
808{
809 // Do nothing
810 wxASSERT( 0 );
811}
812
813
814bool HPGL_PLOTTER::startItem( const VECTOR2D& location )
815{
816 return startOrAppendItem( location, wxEmptyString );
817}
818
819
821{
822 m_current_item = nullptr;
823}
824
825
826bool HPGL_PLOTTER::startOrAppendItem( const VECTOR2D& location, wxString const& content )
827{
828 if( m_current_item == nullptr )
829 {
830 HPGL_ITEM item;
831 item.loc_start = location;
832 item.loc_end = location;
833 item.bbox = BOX2D( location );
834 item.pen = m_penNumber;
835 item.dashType = m_lineStyle;
836 item.content = content;
837 m_items.push_back( item );
838 m_current_item = &m_items.back();
839 return true;
840 }
841 else
842 {
843 m_current_item->content << content;
844 return false;
845 }
846}
847
848
849void HPGL_PLOTTER::sortItems( std::list<HPGL_ITEM>& items )
850{
851 if( items.size() < 2 )
852 return;
853
854 std::list<HPGL_ITEM> target;
855
856 // Plot items are sorted to improve print time on mechanical plotters. This
857 // means
858 // 1) Avoid excess pen-switching - once a pen is selected, keep printing
859 // with it until no more items using that pen remain.
860 // 2) Within the items for one pen, avoid bouncing back and forth around
861 // the page; items should be sequenced with nearby items.
862 //
863 // This is essentially a variant of the Traveling Salesman Problem where
864 // the cities are themselves edges that must be traversed. This is of course
865 // a famously NP-Hard problem and this particular variant has a monstrous
866 // number of "cities". For now, we're using a naive nearest-neighbor search,
867 // which is less than optimal but (usually!) better than nothing, very
868 // simple to implement, and fast enough.
869 //
870 // Items are moved one at a time from `items` into `target`, searching
871 // each time for the first one matching the above criteria. Then, all of
872 // `target` is moved back into `items`.
873
874 // Get the first one started
875 HPGL_ITEM last_item = items.front();
876 items.pop_front();
877 target.emplace_back( last_item );
878
879 while( !items.empty() )
880 {
881 auto best_it = items.begin();
882 double best_dist = dpoint_dist( last_item.loc_end, best_it->loc_start );
883
884 for( auto search_it = best_it; search_it != items.end(); search_it++ )
885 {
886 // Immediately forget an item as "best" if another one is a better pen match
887 if( best_it->pen != last_item.pen && search_it->pen == last_item.pen )
888 {
889 best_it = search_it;
890 continue;
891 }
892
893 double const dist = dpoint_dist( last_item.loc_end, search_it->loc_start );
894
895 if( dist < best_dist )
896 {
897 best_it = search_it;
898 best_dist = dist;
899 continue;
900 }
901 }
902
903 target.emplace_back( *best_it );
904 last_item = *best_it;
905 items.erase( best_it );
906 }
907
908 items.splice( items.begin(), target );
909}
910
911
913{
914 switch( aLineStyle )
915 {
916 case LINE_STYLE::DASH: return "LT 2 4 1;";
917 case LINE_STYLE::DOT: return "LT 1 1 1;";
918 case LINE_STYLE::DASHDOT: return "LT 4 6 1;";
919 case LINE_STYLE::DASHDOTDOT: return "LT 7 8 1;";
920 default: return "LT;";
921 }
922}
923
924
925static double dpoint_dist( const VECTOR2D& a, const VECTOR2D& b )
926{
927 VECTOR2D diff = a - b;
928 return sqrt( diff.x * diff.x + diff.y * diff.y );
929}
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[]
BOX2< VECTOR2D > BOX2D
Definition: box2.h:854
coord_type GetHeight() const
Definition: box2.h:189
coord_type GetY() const
Definition: box2.h:182
coord_type GetWidth() const
Definition: box2.h:188
coord_type GetX() const
Definition: box2.h:181
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:589
double Sin() const
Definition: eda_angle.h:212
double AsDegrees() const
Definition: eda_angle.h:155
double Cos() const
Definition: eda_angle.h:227
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:675
bool m_plotMirror
Definition: plotter.h:655
static const int USE_DEFAULT_LINE_WIDTH
Definition: plotter.h:108
void MoveTo(const VECTOR2I &pos)
Definition: plotter.h:243
void FinishTo(const VECTOR2I &pos)
Definition: plotter.h:253
double m_iuPerDeviceUnit
Definition: plotter.h:652
VECTOR2I m_plotOffset
Definition: plotter.h:654
VECTOR2I m_penLastpos
Definition: plotter.h:668
virtual VECTOR2D userToDeviceCoordinates(const VECTOR2I &aCoordinate)
Modify coordinates according to the orientation, scale factor, and offsets trace.
Definition: plotter.cpp:90
VECTOR2I m_paperSize
Definition: plotter.h:676
virtual VECTOR2D userToDeviceSize(const VECTOR2I &size)
Modify size according to the plotter scale factors (VECTOR2I version, returns a VECTOR2D).
Definition: plotter.cpp:115
void sketchOval(const VECTOR2I &aPos, const VECTOR2I &aSize, const EDA_ANGLE &aOrient, int aWidth)
Definition: plotter.cpp:501
int GetPlotterArcHighDef() const
Definition: plotter.h:208
char m_penState
Definition: plotter.h:667
double m_plotScale
Plot scale - chosen by the user (even implicitly with 'fit in a4')
Definition: plotter.h:644
FILE * m_outputFile
Output file.
Definition: plotter.h:661
void LineTo(const VECTOR2I &pos)
Definition: plotter.h:248
void PenFinish()
Definition: plotter.h:259
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:650
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:437
@ DEGREES_T
Definition: eda_angle.h:31
static constexpr EDA_ANGLE ANGLE_45
Definition: eda_angle.h:436
static constexpr EDA_ANGLE ANGLE_360
Definition: eda_angle.h:441
FILL_T
Definition: eda_shape.h:54
@ ERROR_INSIDE
static const bool FILLED
Definition: gr_basic.cpp:30
OUTLINE_MODE
Definition: outline_mode.h:25
Plotting engine (HPGL)
LINE_STYLE
Dashed line types.
Definition: stroke_params.h:48
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:228
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85
VECTOR2< double > VECTOR2D
Definition: vector2d.h:587
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588