1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 1992-2021 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 <plotters/plotter_dxf.h>
27 #include <plotters/plotter_hpgl.h>
28 #include <plotters/plotters_pslike.h>
29 #include <plotters/plotter_gerber.h>
30 #include <drawing_sheet/ds_data_item.h>
31 #include <drawing_sheet/ds_draw_item.h>
32 #include <drawing_sheet/ds_painter.h>
33 #include <title_block.h>
34 #include <wx/filename.h>
35 
36 
GetDefaultPlotExtension(PLOT_FORMAT aFormat)37 wxString GetDefaultPlotExtension( PLOT_FORMAT aFormat )
38 {
39     switch( aFormat )
40     {
41     case PLOT_FORMAT::DXF:
42         return DXF_PLOTTER::GetDefaultFileExtension();
43     case PLOT_FORMAT::POST:
44         return PS_PLOTTER::GetDefaultFileExtension();
45     case PLOT_FORMAT::PDF:
46         return PDF_PLOTTER::GetDefaultFileExtension();
47     case PLOT_FORMAT::HPGL:
48         return HPGL_PLOTTER::GetDefaultFileExtension();
49     case PLOT_FORMAT::GERBER:
50         return GERBER_PLOTTER::GetDefaultFileExtension();
51     case PLOT_FORMAT::SVG:
52         return SVG_PLOTTER::GetDefaultFileExtension();
53     default:
54         wxASSERT( false );
55         return wxEmptyString;
56     }
57 }
58 
59 
PlotDrawingSheet(PLOTTER * plotter,const PROJECT * aProject,const TITLE_BLOCK & aTitleBlock,const PAGE_INFO & aPageInfo,const wxString & aSheetNumber,int aSheetCount,const wxString & aSheetDesc,const wxString & aFilename,COLOR4D aColor,bool aIsFirstPage)60 void PlotDrawingSheet( PLOTTER* plotter, const PROJECT* aProject, const TITLE_BLOCK& aTitleBlock,
61                        const PAGE_INFO& aPageInfo, const wxString& aSheetNumber, int aSheetCount,
62                        const wxString& aSheetDesc, const wxString& aFilename, COLOR4D aColor,
63                        bool aIsFirstPage )
64 {
65     /* Note: Page sizes values are given in mils
66      */
67     double   iusPerMil = plotter->GetIUsPerDecimil() * 10.0;
68     COLOR4D  plotColor = plotter->GetColorMode() ? aColor : COLOR4D::BLACK;
69     int      defaultPenWidth = plotter->RenderSettings()->GetDefaultPenWidth();
70 
71     if( plotColor == COLOR4D::UNSPECIFIED )
72         plotColor = COLOR4D( RED );
73 
74     plotter->SetColor( plotColor );
75     DS_DRAW_ITEM_LIST drawList;
76 
77     // Print only a short filename, if aFilename is the full filename
78     wxFileName fn( aFilename );
79 
80     // Prepare plot parameters
81     drawList.SetDefaultPenSize( PLOTTER::USE_DEFAULT_LINE_WIDTH );
82     drawList.SetMilsToIUfactor( iusPerMil );
83     drawList.SetPageNumber( aSheetNumber );
84     drawList.SetSheetCount( aSheetCount );
85     drawList.SetFileName( fn.GetFullName() );   // Print only the short filename
86     drawList.SetSheetName( aSheetDesc );
87     drawList.SetProject( aProject );
88     drawList.SetIsFirstPage( aIsFirstPage );
89 
90     drawList.BuildDrawItemsList( aPageInfo, aTitleBlock );
91 
92     // Draw item list
93     for( DS_DRAW_ITEM_BASE* item = drawList.GetFirst(); item; item = drawList.GetNext() )
94     {
95         plotter->SetCurrentLineWidth( PLOTTER::USE_DEFAULT_LINE_WIDTH );
96 
97         switch( item->Type() )
98         {
99         case WSG_LINE_T:
100         {
101             DS_DRAW_ITEM_LINE* line = (DS_DRAW_ITEM_LINE*) item;
102             plotter->SetCurrentLineWidth( std::max( line->GetPenWidth(), defaultPenWidth ) );
103             plotter->MoveTo( line->GetStart() );
104             plotter->FinishTo( line->GetEnd() );
105         }
106             break;
107 
108         case WSG_RECT_T:
109         {
110             DS_DRAW_ITEM_RECT* rect = (DS_DRAW_ITEM_RECT*) item;
111             int penWidth = std::max( rect->GetPenWidth(), defaultPenWidth );
112             plotter->Rect( rect->GetStart(), rect->GetEnd(), FILL_T::NO_FILL, penWidth );
113         }
114             break;
115 
116         case WSG_TEXT_T:
117         {
118             DS_DRAW_ITEM_TEXT* text = (DS_DRAW_ITEM_TEXT*) item;
119             int penWidth = std::max( text->GetEffectiveTextPenWidth(), defaultPenWidth );
120             plotter->Text( text->GetTextPos(), plotColor, text->GetShownText(),
121                            text->GetTextAngle(), text->GetTextSize(), text->GetHorizJustify(),
122                            text->GetVertJustify(), penWidth, text->IsItalic(), text->IsBold(),
123                            text->IsMultilineAllowed() );
124         }
125             break;
126 
127         case WSG_POLY_T:
128         {
129             DS_DRAW_ITEM_POLYPOLYGONS* poly = (DS_DRAW_ITEM_POLYPOLYGONS*) item;
130             int penWidth = std::max( poly->GetPenWidth(), defaultPenWidth );
131             std::vector<wxPoint> points;
132 
133             for( int idx = 0; idx < poly->GetPolygons().OutlineCount(); ++idx )
134             {
135                 points.clear();
136                 SHAPE_LINE_CHAIN& outline = poly->GetPolygons().Outline( idx );
137 
138                 for( int ii = 0; ii < outline.PointCount(); ii++ )
139                     points.emplace_back( outline.CPoint( ii ).x, outline.CPoint( ii ).y );
140 
141                 plotter->PlotPoly( points, FILL_T::FILLED_SHAPE, penWidth );
142             }
143         }
144             break;
145 
146         case WSG_BITMAP_T:
147         {
148             DS_DRAW_ITEM_BITMAP* drawItem = (DS_DRAW_ITEM_BITMAP*) item;
149             DS_DATA_ITEM_BITMAP* bitmap = (DS_DATA_ITEM_BITMAP*) drawItem->GetPeer();
150 
151             if( bitmap->m_ImageBitmap == nullptr )
152                 break;
153 
154             bitmap->m_ImageBitmap->PlotImage( plotter, drawItem->GetPosition(), plotColor,
155                                                   PLOTTER::USE_DEFAULT_LINE_WIDTH );
156         }
157             break;
158 
159         default:
160             wxFAIL_MSG( "PlotDrawingSheet(): Unknown drawing sheet item." );
161             break;
162         }
163     }
164 }
165