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( aCornerList.size() <= 1 )
452 return;
453
454 // Width less than zero is occasionally used to create background-only
455 // polygons. Don't set that as the plotter line width, that'll cause
456 // trouble. Also, later, skip plotting the outline if this is the case.
457 if( aWidth > 0 )
458 {
459 SetCurrentLineWidth( aWidth );
460 }
461
462 MoveTo( aCornerList[0] );
463 startItem( userToDeviceCoordinates( aCornerList[0] ) );
464
465 if( aFill == FILL_T::FILLED_SHAPE || aFill == FILL_T::FILLED_WITH_COLOR )
466 {
467 // Draw the filled area
469
470 m_current_item->content << wxString( "PM 0;\n" ); // Start polygon
471
472 for( unsigned ii = 1; ii < aCornerList.size(); ++ii )
473 LineTo( aCornerList[ii] );
474
475 int ii = aCornerList.size() - 1;
476
477 if( aCornerList[ii] != aCornerList[0] )
478 LineTo( aCornerList[0] );
479
480 m_current_item->content << hpgl_end_polygon_cmd; // Close, fill polygon and draw outlines
482 }
483 else if( aWidth != 0 )
484 {
485 // Plot only the polygon outline.
486 for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
487 LineTo( aCornerList[ii] );
488
489 // Always close polygon if filled.
490 if( aFill != FILL_T::NO_FILL )
491 {
492 int ii = aCornerList.size() - 1;
493
494 if( aCornerList[ii] != aCornerList[0] )
495 LineTo( aCornerList[0] );
496 }
497 }
498
499 PenFinish();
500}
501
502
503void HPGL_PLOTTER::PenTo( const VECTOR2I& pos, char plume )
504{
505 wxASSERT( m_outputFile );
506
507 if( plume == 'Z' )
508 {
509 m_penState = 'Z';
510 flushItem();
511 return;
512 }
513
514 VECTOR2D pos_dev = userToDeviceCoordinates( pos );
516
517 if( plume == 'U' )
518 {
519 m_penState = 'U';
520 flushItem();
521 }
522 else if( plume == 'D' )
523 {
524 m_penState = 'D';
525 startOrAppendItem( lastpos_dev, wxString::Format( "PA %.0f,%.0f;", pos_dev.x, pos_dev.y ) );
526 m_current_item->loc_end = pos_dev;
527 m_current_item->bbox.Merge( pos_dev );
528 }
529
530 m_penLastpos = pos;
531}
532
533
534void HPGL_PLOTTER::SetDash( int aLineWidth, LINE_STYLE aLineStyle )
535{
536 m_lineStyle = aLineStyle;
537 flushItem();
538}
539
540
541void HPGL_PLOTTER::ThickSegment( const VECTOR2I& start, const VECTOR2I& end,
542 int width, OUTLINE_MODE tracemode, void* aData )
543{
544 wxASSERT( m_outputFile );
545
546 // Suppress overlap if pen is too big
547 if( m_penDiameter >= width )
548 {
549 MoveTo( start );
550 FinishTo( end );
551 }
552 else
553 {
554 segmentAsOval( start, end, width, tracemode );
555 }
556}
557
558
559void HPGL_PLOTTER::Arc( const VECTOR2D& aCenter, const EDA_ANGLE& aStartAngle,
560 const EDA_ANGLE& aAngle, double aRadius, FILL_T aFill, int aWidth )
561{
562 if( aRadius <= 0 )
563 return;
564
565 double const radius_device = userToDeviceSize( aRadius );
566 double const circumf_device = 2.0 * M_PI * radius_device;
567 double const target_chord_length = m_arcTargetChordLength;
568 EDA_ANGLE chord_angle = ANGLE_360 * target_chord_length / circumf_device;
569
570 chord_angle = std::max( m_arcMinChordDegrees, std::min( chord_angle, ANGLE_45 ) );
571
572 VECTOR2D centre_device = userToDeviceCoordinates( aCenter );
573 EDA_ANGLE angle = aAngle;
574
575 if( !m_plotMirror )
576 angle = -angle;
577
578 EDA_ANGLE startAngle = -aStartAngle;
579
580 // Calculate arc start point:
581 VECTOR2I cmap( aCenter.x + KiROUND( aRadius * startAngle.Cos() ),
582 aCenter.y - KiROUND( aRadius * startAngle.Sin() ) );
583 VECTOR2D cmap_dev = userToDeviceCoordinates( cmap );
584
585 startOrAppendItem( cmap_dev, wxString::Format( "AA %.0f,%.0f,%.0f,%g",
586 centre_device.x,
587 centre_device.y,
588 angle.AsDegrees(),
589 chord_angle.AsDegrees() ) );
590
591 // TODO We could compute the final position and full bounding box instead...
592 m_current_item->bbox.Merge( BOX2D( centre_device - radius_device,
593 VECTOR2D( radius_device * 2, radius_device * 2 ) ) );
595 flushItem();
596}
597
598
599void HPGL_PLOTTER::FlashPadOval( const VECTOR2I& aPos, const VECTOR2I& aSize,
600 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode, void* aData )
601{
602 wxASSERT( m_outputFile );
603
604 VECTOR2I size( aSize );
605 EDA_ANGLE orient( aOrient );
606
607 // The pad will be drawn as an oblong shape with size.y > size.x (Oval vertical orientation 0).
608 if( size.x > size.y )
609 {
610 std::swap( size.x, size.y );
611 orient += ANGLE_90;
612 }
613
614 if( aTraceMode == FILLED )
615 {
616 int deltaxy = size.y - size.x; // distance between centers of the oval
617
618 FlashPadRect( aPos, VECTOR2I( size.x, deltaxy + KiROUND( m_penDiameter ) ), orient,
619 aTraceMode, aData );
620
621 VECTOR2I pt( 0, deltaxy / 2 );
622 RotatePoint( pt, orient );
623 FlashPadCircle( pt + aPos, size.x, aTraceMode, aData );
624
625 pt = VECTOR2I( 0, -deltaxy / 2 );
626 RotatePoint( pt, orient );
627 FlashPadCircle( pt + aPos, size.x, aTraceMode, aData );
628 }
629 else // Plot in outline mode.
630 {
631 sketchOval( aPos, size, orient, KiROUND( m_penDiameter ) );
632 }
633}
634
635
636void HPGL_PLOTTER::FlashPadCircle( const VECTOR2I& pos, int diametre,
637 OUTLINE_MODE trace_mode, void* aData )
638{
639 wxASSERT( m_outputFile );
640 VECTOR2D pos_dev = userToDeviceCoordinates( pos );
641 int radius = diametre / 2;
642
643 if( trace_mode == FILLED )
644 {
645 // if filled mode, the pen diameter is removed from diameter
646 // to keep the pad size
647 radius -= KiROUND( m_penDiameter ) / 2;
648
649 if( radius < 0 )
650 radius = 0;
651 }
652
653 double rsize = userToDeviceSize( radius );
654
655 if( trace_mode == FILLED ) // Plot in filled mode.
656 {
657 // A filled polygon uses always the current point to start the polygon.
658 // Gives a correct current starting point for the circle
659 MoveTo( VECTOR2I( pos.x + radius, pos.y ) );
660
661 // Plot filled area and its outline
662 startOrAppendItem( userToDeviceCoordinates( VECTOR2I( pos.x + radius, pos.y ) ),
663 wxString::Format( "PM 0; PA %.0f,%.0f;CI %.0f;%s",
664 pos_dev.x, pos_dev.y, rsize, hpgl_end_polygon_cmd ) );
667 }
668 else
669 {
670 // Draw outline only:
671 startOrAppendItem( pos_dev, wxString::Format( "CI %.0f;", rsize ) );
674 }
675
676 PenFinish();
677}
678
679
680void HPGL_PLOTTER::FlashPadRect( const VECTOR2I& aPos, const VECTOR2I& aPadSize,
681 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode, void* aData )
682{
683 // Build rect polygon:
684 std::vector<VECTOR2I> corners;
685
686 int dx = aPadSize.x / 2;
687 int dy = aPadSize.y / 2;
688
689 if( aTraceMode == FILLED )
690 {
691 // in filled mode, the pen diameter is removed from size
692 // to compensate the extra size due to this pen size
693 dx -= KiROUND( m_penDiameter ) / 2;
694 dx = std::max( dx, 0);
695 dy -= KiROUND( m_penDiameter ) / 2;
696 dy = std::max( dy, 0);
697 }
698
699
700 corners.emplace_back( - dx, - dy );
701 corners.emplace_back( - dx, + dy );
702 corners.emplace_back( + dx, + dy );
703 corners.emplace_back( + dx, - dy );
704
705 // Close polygon
706 corners.emplace_back( - dx, - dy );
707
708 for( unsigned ii = 0; ii < corners.size(); ii++ )
709 {
710 RotatePoint( corners[ii], aOrient );
711 corners[ii] += aPos;
712 }
713
714 PlotPoly( corners, aTraceMode == FILLED ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL );
715}
716
717
718void HPGL_PLOTTER::FlashPadRoundRect( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
719 int aCornerRadius, const EDA_ANGLE& aOrient,
720 OUTLINE_MODE aTraceMode, void* aData )
721{
722 SHAPE_POLY_SET outline;
723
724 VECTOR2I size = aSize;
725
726 if( aTraceMode == FILLED )
727 {
728 // In filled mode, the pen diameter is removed from size to keep the pad size.
729 size.x -= KiROUND( m_penDiameter ) / 2;
730 size.x = std::max( size.x, 0);
731 size.y -= KiROUND( m_penDiameter ) / 2;
732 size.y = std::max( size.y, 0);
733
734 // keep aCornerRadius to a value < min size x,y < 2:
735 aCornerRadius = std::min( aCornerRadius, std::min( size.x, size.y ) /2 );
736 }
737
738 TransformRoundChamferedRectToPolygon( outline, aPadPos, size, aOrient, aCornerRadius, 0.0, 0,
740
741 // TransformRoundRectToPolygon creates only one convex polygon
742 std::vector<VECTOR2I> cornerList;
743 SHAPE_LINE_CHAIN& poly = outline.Outline( 0 );
744 cornerList.reserve( poly.PointCount() );
745
746 for( int ii = 0; ii < poly.PointCount(); ++ii )
747 cornerList.emplace_back( poly.CPoint( ii ).x, poly.CPoint( ii ).y );
748
749 if( cornerList.back() != cornerList.front() )
750 cornerList.push_back( cornerList.front() );
751
752 PlotPoly( cornerList, aTraceMode == FILLED ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL );
753}
754
755
756void HPGL_PLOTTER::FlashPadCustom( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
757 const EDA_ANGLE& aOrient, SHAPE_POLY_SET* aPolygons,
758 OUTLINE_MODE aTraceMode, void* aData )
759{
760 std::vector<VECTOR2I> cornerList;
761
762 for( int cnt = 0; cnt < aPolygons->OutlineCount(); ++cnt )
763 {
764 SHAPE_LINE_CHAIN& poly = aPolygons->Outline( cnt );
765
766 cornerList.clear();
767 cornerList.reserve( poly.PointCount() );
768
769 for( int ii = 0; ii < poly.PointCount(); ++ii )
770 cornerList.emplace_back( poly.CPoint( ii ).x, poly.CPoint( ii ).y );
771
772 if( cornerList.back() != cornerList.front() )
773 cornerList.push_back( cornerList.front() );
774
775 PlotPoly( cornerList, aTraceMode == FILLED ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL );
776 }
777}
778
779
780void HPGL_PLOTTER::FlashPadTrapez( const VECTOR2I& aPadPos, const VECTOR2I* aCorners,
781 const EDA_ANGLE& aPadOrient, OUTLINE_MODE aTraceMode,
782 void* aData )
783{
784 std::vector<VECTOR2I> cornerList;
785 cornerList.reserve( 5 );
786
787 for( int ii = 0; ii < 4; ii++ )
788 {
789 VECTOR2I coord( aCorners[ii] );
790 RotatePoint( coord, aPadOrient );
791 coord += aPadPos;
792 cornerList.push_back( coord );
793 }
794
795 // Close polygon
796 cornerList.push_back( cornerList.front() );
797
798 PlotPoly( cornerList, aTraceMode == FILLED ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL );
799}
800
801
802void HPGL_PLOTTER::FlashRegularPolygon( const VECTOR2I& aShapePos, int aRadius, int aCornerCount,
803 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode,
804 void* aData )
805{
806 // Do nothing
807 wxASSERT( 0 );
808}
809
810
811bool HPGL_PLOTTER::startItem( const VECTOR2D& location )
812{
813 return startOrAppendItem( location, wxEmptyString );
814}
815
816
818{
819 m_current_item = nullptr;
820}
821
822
823bool HPGL_PLOTTER::startOrAppendItem( const VECTOR2D& location, wxString const& content )
824{
825 if( m_current_item == nullptr )
826 {
827 HPGL_ITEM item;
828 item.loc_start = location;
829 item.loc_end = location;
830 item.bbox = BOX2D( location );
831 item.pen = m_penNumber;
832 item.dashType = m_lineStyle;
833 item.content = content;
834 m_items.push_back( item );
835 m_current_item = &m_items.back();
836 return true;
837 }
838 else
839 {
840 m_current_item->content << content;
841 return false;
842 }
843}
844
845
846void HPGL_PLOTTER::sortItems( std::list<HPGL_ITEM>& items )
847{
848 if( items.size() < 2 )
849 return;
850
851 std::list<HPGL_ITEM> target;
852
853 // Plot items are sorted to improve print time on mechanical plotters. This
854 // means
855 // 1) Avoid excess pen-switching - once a pen is selected, keep printing
856 // with it until no more items using that pen remain.
857 // 2) Within the items for one pen, avoid bouncing back and forth around
858 // the page; items should be sequenced with nearby items.
859 //
860 // This is essentially a variant of the Traveling Salesman Problem where
861 // the cities are themselves edges that must be traversed. This is of course
862 // a famously NP-Hard problem and this particular variant has a monstrous
863 // number of "cities". For now, we're using a naive nearest-neighbor search,
864 // which is less than optimal but (usually!) better than nothing, very
865 // simple to implement, and fast enough.
866 //
867 // Items are moved one at a time from `items` into `target`, searching
868 // each time for the first one matching the above criteria. Then, all of
869 // `target` is moved back into `items`.
870
871 // Get the first one started
872 HPGL_ITEM last_item = items.front();
873 items.pop_front();
874 target.emplace_back( last_item );
875
876 while( !items.empty() )
877 {
878 auto best_it = items.begin();
879 double best_dist = dpoint_dist( last_item.loc_end, best_it->loc_start );
880
881 for( auto search_it = best_it; search_it != items.end(); search_it++ )
882 {
883 // Immediately forget an item as "best" if another one is a better pen match
884 if( best_it->pen != last_item.pen && search_it->pen == last_item.pen )
885 {
886 best_it = search_it;
887 continue;
888 }
889
890 double const dist = dpoint_dist( last_item.loc_end, search_it->loc_start );
891
892 if( dist < best_dist )
893 {
894 best_it = search_it;
895 best_dist = dist;
896 continue;
897 }
898 }
899
900 target.emplace_back( *best_it );
901 last_item = *best_it;
902 items.erase( best_it );
903 }
904
905 items.splice( items.begin(), target );
906}
907
908
910{
911 switch( aLineStyle )
912 {
913 case LINE_STYLE::DASH: return "LT 2 4 1;";
914 case LINE_STYLE::DOT: return "LT 1 1 1;";
915 case LINE_STYLE::DASHDOT: return "LT 4 6 1;";
916 case LINE_STYLE::DASHDOTDOT: return "LT 7 8 1;";
917 default: return "LT;";
918 }
919}
920
921
922static double dpoint_dist( const VECTOR2D& a, const VECTOR2D& b )
923{
924 VECTOR2D diff = a - b;
925 return sqrt( diff.x * diff.x + diff.y * diff.y );
926}
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:206
double AsDegrees() const
Definition: eda_angle.h:149
double Cos() const
Definition: eda_angle.h:221
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:225
virtual void SetDash(int aLineWidth, LINE_STYLE aLineStyle) override
HPGL supports dashed lines.
virtual void SetPenSpeed(int speed)
Definition: plotter_hpgl.h:85
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:218
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:90
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:220
EDA_ANGLE m_arcMinChordDegrees
Definition: plotter_hpgl.h:219
virtual void SetCurrentLineWidth(int width, void *aData=nullptr) override
HPGL doesn't handle line thickness or color.
Definition: plotter_hpgl.h:72
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:221
std::list< HPGL_ITEM > m_items
Definition: plotter_hpgl.h:224
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:217
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:222
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:143
PAGE_INFO m_pageInfo
Definition: plotter.h:665
bool m_plotMirror
Definition: plotter.h:647
static const int USE_DEFAULT_LINE_WIDTH
Definition: plotter.h:108
void MoveTo(const VECTOR2I &pos)
Definition: plotter.h:242
void FinishTo(const VECTOR2I &pos)
Definition: plotter.h:252
double m_iuPerDeviceUnit
Definition: plotter.h:644
VECTOR2I m_plotOffset
Definition: plotter.h:646
VECTOR2I m_penLastpos
Definition: plotter.h:660
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:666
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:207
char m_penState
Definition: plotter.h:659
double m_plotScale
Plot scale - chosen by the user (even implicitly with 'fit in a4')
Definition: plotter.h:636
FILE * m_outputFile
Output file.
Definition: plotter.h:653
void LineTo(const VECTOR2I &pos)
Definition: plotter.h:247
void PenFinish()
Definition: plotter.h:258
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:642
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.
@ DEGREES_T
Definition: eda_angle.h:31
static constexpr EDA_ANGLE & ANGLE_360
Definition: eda_angle.h:443
static constexpr EDA_ANGLE & ANGLE_45
Definition: eda_angle.h:438
static constexpr EDA_ANGLE & ANGLE_90
Definition: eda_angle.h:439
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:203
bool pen_returns
Whether the pen returns to its original state after the command.
Definition: plotter_hpgl.h:199
BOX2D bbox
Bounding box of this item.
Definition: plotter_hpgl.h:187
LINE_STYLE dashType
Pen number for this command.
Definition: plotter_hpgl.h:202
bool lift_after
Whether the pen must be lifted after the command.
Definition: plotter_hpgl.h:195
bool lift_before
Whether the command should be executed with the pen lifted.
Definition: plotter_hpgl.h:190
VECTOR2D loc_start
Location the pen should start at.
Definition: plotter_hpgl.h:180
VECTOR2D loc_end
Location the pen will be at when it finishes.
Definition: plotter_hpgl.h:184
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