KiCad PCB EDA Suite
Loading...
Searching...
No Matches
common_plot_functions.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
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
25#include <eda_item.h>
26#include <font/font.h>
33#include <string_utils.h>
34#include <title_block.h>
35#include <wx/filename.h>
36
37
51
52
53void PlotDrawingSheet( PLOTTER* plotter, const PROJECT* aProject, const TITLE_BLOCK& aTitleBlock,
54 const PAGE_INFO& aPageInfo, const std::map<wxString, wxString>* aProperties,
55 const wxString& aSheetNumber, int aSheetCount, const wxString& aSheetName,
56 const wxString& aSheetPath, const wxString& aFilename, COLOR4D aColor, bool aIsFirstPage,
57 const wxString& aVariantName, const wxString& aVariantDesc )
58{
59 /* Note: Page sizes values are given in mils
60 */
61 double iusPerMil = plotter->GetIUsPerDecimil() * 10.0;
62 COLOR4D plotColor = plotter->GetColorMode() ? aColor : COLOR4D::BLACK;
63 RENDER_SETTINGS* settings = plotter->RenderSettings();
64 int defaultPenWidth = settings->GetDefaultPenWidth();
65
66 if( plotColor == COLOR4D::UNSPECIFIED )
67 plotColor = COLOR4D( RED );
68
69 DS_DRAW_ITEM_LIST drawList( unityScale );
70
71 // Print only a short filename, if aFilename is the full filename
72 wxFileName fn( aFilename );
73
74 // Prepare plot parameters
75 drawList.SetDefaultPenSize( defaultPenWidth );
76 drawList.SetPlotterMilsToIUfactor( iusPerMil );
77 drawList.SetPageNumber( aSheetNumber );
78 drawList.SetSheetCount( aSheetCount );
79 drawList.SetFileName( fn.GetFullPath() );
80 drawList.SetSheetName( aSheetName );
81 drawList.SetSheetPath( aSheetPath );
82 drawList.SetSheetLayer( settings->GetLayerName() );
83 drawList.SetProject( aProject );
84 drawList.SetIsFirstPage( aIsFirstPage );
85 drawList.SetProperties( aProperties );
86 drawList.SetVariantName( aVariantName );
87 drawList.SetVariantDesc( aVariantDesc );
88
89 drawList.BuildDrawItemsList( aPageInfo, aTitleBlock );
90
91 try
92 {
93 // Draw bitmaps first
94 for( DS_DRAW_ITEM_BASE* item = drawList.GetFirst(); item; item = drawList.GetNext() )
95 {
96 if( item->Type() == WSG_BITMAP_T )
97 {
98 DS_DRAW_ITEM_BITMAP* drawItem = (DS_DRAW_ITEM_BITMAP*) item;
99 DS_DATA_ITEM_BITMAP* bitmap = (DS_DATA_ITEM_BITMAP*) drawItem->GetPeer();
100
101 if( bitmap->m_ImageBitmap == nullptr )
102 continue;
103
104 bitmap->m_ImageBitmap->PlotImage( plotter, drawItem->GetPosition(), plotColor,
105 defaultPenWidth );
106 }
107 }
108
109 // Draw other items
110 for( DS_DRAW_ITEM_BASE* item = drawList.GetFirst(); item; item = drawList.GetNext() )
111 {
112 if( item->Type() == WSG_BITMAP_T )
113 continue;
114
115 plotter->SetColor( plotColor );
116
117 switch( item->Type() )
118 {
119 case WSG_LINE_T:
120 {
121 DS_DRAW_ITEM_LINE* line = (DS_DRAW_ITEM_LINE*) item;
122 plotter->SetCurrentLineWidth( std::max( line->GetPenWidth(), defaultPenWidth ) );
123 plotter->MoveTo( line->GetStart() );
124 plotter->FinishTo( line->GetEnd() );
125 break;
126 }
127
128 case WSG_RECT_T:
129 {
130 DS_DRAW_ITEM_RECT* rect = (DS_DRAW_ITEM_RECT*) item;
131 plotter->SetCurrentLineWidth( std::max( rect->GetPenWidth(), defaultPenWidth ) );
132 plotter->MoveTo( rect->GetStart() );
133 plotter->LineTo( VECTOR2I( rect->GetEnd().x, rect->GetStart().y ) );
134 plotter->LineTo( VECTOR2I( rect->GetEnd().x, rect->GetEnd().y ) );
135 plotter->LineTo( VECTOR2I( rect->GetStart().x, rect->GetEnd().y ) );
136 plotter->FinishTo( rect->GetStart() );
137 break;
138 }
139
140 case WSG_TEXT_T:
141 {
143 KIFONT::FONT* font = text->GetDrawFont( settings );
144 COLOR4D color = plotColor;
145 wxString shownText( text->GetShownText( true ) );
146
147 if( plotter->GetColorMode() && text->GetTextColor() != COLOR4D::UNSPECIFIED )
148 color = text->GetTextColor();
149
150 int penWidth = std::max( text->GetEffectiveTextPenWidth(), defaultPenWidth );
151
152 // Some plotters (PDF plotter) do not handle multiline very well. So handle them here
153 if( text->IsMultilineAllowed() && shownText.Find( '\n' ) != wxNOT_FOUND )
154 {
155 std::vector<VECTOR2I> positions;
156 wxArrayString strings_list;
157 wxStringSplit( shownText, strings_list, '\n' );
158 positions.reserve( strings_list.Count() );
159
160 text->GetLinePositions( plotter->RenderSettings(), positions, (int) strings_list.Count() );
161
162 for( unsigned ii = 0; ii < strings_list.Count(); ii++ )
163 {
164 wxString& txt = strings_list.Item( ii );
165 plotter->Text( positions[ii], color, txt,
166 text->GetTextAngle(), text->GetTextSize(), text->GetHorizJustify(),
167 text->GetVertJustify(), penWidth, text->IsItalic(), text->IsBold(),
168 false, font, text->GetFontMetrics() );
169 }
170 }
171 else
172 {
173 plotter->Text( text->GetTextPos(), color, shownText,
174 text->GetTextAngle(), text->GetTextSize(), text->GetHorizJustify(),
175 text->GetVertJustify(), penWidth, text->IsItalic(), text->IsBold(),
176 text->IsMultilineAllowed(), font, text->GetFontMetrics() );
177 }
178 break;
179 }
180
181 case WSG_POLY_T:
182 {
184 int penWidth = std::max( poly->GetPenWidth(), defaultPenWidth );
185 std::vector<VECTOR2I> points;
186
187 for( int idx = 0; idx < poly->GetPolygons().OutlineCount(); ++idx )
188 {
189 points.clear();
190 SHAPE_LINE_CHAIN& outline = poly->GetPolygons().Outline( idx );
191
192 for( int ii = 0; ii < outline.PointCount(); ii++ )
193 points.emplace_back( outline.CPoint( ii ).x, outline.CPoint( ii ).y );
194
195 plotter->PlotPoly( points, FILL_T::FILLED_SHAPE, penWidth, nullptr );
196 }
197
198 break;
199 }
200
201 default:
202 wxFAIL_MSG( wxT( "PlotDrawingSheet(): Unknown drawing sheet item." ) );
203 break;
204 }
205 }
206 }
207 catch( ... )
208 {
209 wxFAIL_MSG( wxT( "PlotDrawingSheet(): Exception during plot." ) );
210 }
211}
constexpr EDA_IU_SCALE unityScale
Definition base_units.h:128
void PlotImage(PLOTTER *aPlotter, const VECTOR2I &aPos, const KIGFX::COLOR4D &aDefaultColor, int aDefaultPensize) const
Plot bitmap on plotter.
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:402
static const COLOR4D BLACK
Definition color4d.h:406
BITMAP_BASE * m_ImageBitmap
Base class to handle basic graphic items.
virtual int GetPenWidth() const
DS_DATA_ITEM * GetPeer() const
VECTOR2I GetPosition() const override
const VECTOR2I & GetStart() const
const VECTOR2I & GetEnd() const
Store the list of graphic items: rect, lines, polygons and texts to draw/plot the title block and fra...
void SetPlotterMilsToIUfactor(double aMils2Iu)
Set the scalar to convert pages units (mils) to plot units.
DS_DRAW_ITEM_BASE * GetFirst()
void SetVariantName(const wxString &aVariant)
Set the current variant name and description to draw/plot.
void BuildDrawItemsList(const PAGE_INFO &aPageInfo, const TITLE_BLOCK &aTitleBlock)
Drawing or plot the drawing sheet.
void SetSheetPath(const wxString &aSheetPath)
Set the sheet path to draw/plot.
void SetFileName(const wxString &aFileName)
Set the filename to draw/plot.
void SetDefaultPenSize(int aPenSize)
void SetVariantDesc(const wxString &aDesc)
void SetSheetName(const wxString &aSheetName)
Set the sheet name to draw/plot.
void SetIsFirstPage(bool aIsFirstPage)
Set if the page is the first page.
void SetProperties(const std::map< wxString, wxString > *aProps)
Set properties used for text variable resolution.
void SetSheetLayer(const wxString &aSheetLayer)
Set the sheet layer to draw/plot.
void SetSheetCount(int aSheetCount)
Set the value of the count of sheets, for basic inscriptions.
void SetPageNumber(const wxString &aPageNumber)
Set the value of the sheet number.
DS_DRAW_ITEM_BASE * GetNext()
void SetProject(const PROJECT *aProject)
SHAPE_POLY_SET & GetPolygons()
Non filled rectangle with thick segment.
const VECTOR2I & GetEnd() const
const VECTOR2I & GetStart() const
A graphic text.
static wxString GetDefaultFileExtension()
static wxString GetDefaultFileExtension()
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:98
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
const wxString & GetLayerName() const
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:79
static wxString GetDefaultFileExtension()
Base plotter engine class.
Definition plotter.h:137
void MoveTo(const VECTOR2I &pos)
Definition plotter.h:309
void FinishTo(const VECTOR2I &pos)
Definition plotter.h:319
RENDER_SETTINGS * RenderSettings()
Definition plotter.h:168
double GetIUsPerDecimil() const
The IUs per decimil are an essential scaling factor when plotting; they are set and saved when establ...
Definition plotter.h:272
bool GetColorMode() const
Definition plotter.h:165
virtual void SetCurrentLineWidth(int width, void *aData=nullptr)=0
Set the line width for the next drawing.
void LineTo(const VECTOR2I &pos)
Definition plotter.h:314
virtual void PlotPoly(const std::vector< VECTOR2I > &aCornerList, FILL_T aFill, int aWidth, void *aData)=0
Draw a polygon ( filled or not ).
virtual void Text(const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const EDA_ANGLE &aOrient, const VECTOR2I &aSize, enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify, int aPenWidth, bool aItalic, bool aBold, bool aMultilineAllowed, KIFONT::FONT *aFont, const KIFONT::METRICS &aFontMetrics, void *aData=nullptr)
Draw text with the plotter.
Definition plotter.cpp:646
virtual void SetColor(const COLOR4D &color)=0
static wxString GetDefaultFileExtension()
Definition plotter_png.h:50
Container for project specific data.
Definition project.h:66
static wxString GetDefaultFileExtension()
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.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
static wxString GetDefaultFileExtension()
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition title_block.h:41
@ RED
Definition color4d.h:59
void PlotDrawingSheet(PLOTTER *plotter, const PROJECT *aProject, const TITLE_BLOCK &aTitleBlock, const PAGE_INFO &aPageInfo, const std::map< wxString, wxString > *aProperties, const wxString &aSheetNumber, int aSheetCount, const wxString &aSheetName, const wxString &aSheetPath, const wxString &aFilename, COLOR4D aColor, bool aIsFirstPage, const wxString &aVariantName, const wxString &aVariantDesc)
wxString GetDefaultPlotExtension(PLOT_FORMAT aFormat)
Return the default plot extension for a format.
@ FILLED_SHAPE
Fill with object color.
Definition eda_shape.h:65
PLOT_FORMAT
The set of supported output plot formats.
Definition plotter.h:64
Plotting engines similar to ps (PostScript, Gerber, svg)
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
@ WSG_POLY_T
Definition typeinfo.h:219
@ WSG_LINE_T
Definition typeinfo.h:217
@ WSG_TEXT_T
Definition typeinfo.h:220
@ WSG_RECT_T
Definition typeinfo.h:218
@ WSG_BITMAP_T
Definition typeinfo.h:221
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687