KiCad PCB EDA Suite
libedit_plot_component.cpp
Go to the documentation of this file.
1 
5 /*
6  * This program source code file is part of KiCad, a free EDA CAD application.
7  *
8  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23  * or you may search the http://www.gnu.org website for the version 2 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27 
28 
29 #include <fctsys.h>
30 
31 #include <gr_basic.h>
32 #include <pgm_base.h>
33 #include <class_drawpanel.h>
34 #include <confirm.h>
35 #include <gestfich.h>
36 #include <eeschema_id.h>
37 #include <class_sch_screen.h>
38 
39 #include <general.h>
40 #include <libeditframe.h>
41 #include <class_library.h>
43 
44 
45 void LIB_EDIT_FRAME::OnPlotCurrentComponent( wxCommandEvent& event )
46 {
47  wxString fullFileName;
48  wxString file_ext;
49  wxString mask;
50 
51  LIB_PART* part = GetCurPart();
52 
53  if( !part )
54  {
55  wxMessageBox( _( "No component" ) );
56  return;
57  }
58 
59  switch( event.GetId() )
60  {
62  {
63  bool fmt_is_jpeg = false; // could be selectable later. so keep this option.
64 
65  file_ext = fmt_is_jpeg ? wxT( "jpg" ) : wxT( "png" );
66  mask = wxT( "*." ) + file_ext;
67  wxFileName fn( part->GetName() );
68  fn.SetExt( file_ext );
69 
70  wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() );
71 
72  fullFileName = EDA_FILE_SELECTOR( _( "Filename:" ), pro_dir,
73  fn.GetFullName(), file_ext, mask, this,
74  wxFD_SAVE, true );
75 
76  if( fullFileName.IsEmpty() )
77  return;
78 
79  // calling wxYield is mandatory under Linux, after closing the file selector dialog
80  // to refresh the screen before creating the PNG or JPEG image from screen
81  wxYield();
82  CreatePNGorJPEGFile( fullFileName, fmt_is_jpeg );
83  }
84  break;
85 
87  {
88  file_ext = wxT( "svg" );
89  mask = wxT( "*." ) + file_ext;
90  wxFileName fn( part->GetName() );
91  fn.SetExt( file_ext );
92 
93  wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() );
94 
95  fullFileName = EDA_FILE_SELECTOR( _( "Filename:" ), pro_dir,
96  fn.GetFullName(), file_ext, mask, this,
97  wxFD_SAVE, true );
98 
99  if( fullFileName.IsEmpty() )
100  return;
101 
102  PAGE_INFO pageSave = GetScreen()->GetPageSettings();
103  PAGE_INFO pageTemp = pageSave;
104 
105  wxSize componentSize = part->GetUnitBoundingBox( m_unit, m_convert ).GetSize();
106 
107  // Add a small margin to the plot bounding box
108  pageTemp.SetWidthMils( int( componentSize.x * 1.2 ) );
109  pageTemp.SetHeightMils( int( componentSize.y * 1.2 ) );
110 
111  GetScreen()->SetPageSettings( pageTemp );
112  SVG_PlotComponent( fullFileName );
113  GetScreen()->SetPageSettings( pageSave );
114  }
115  break;
116  }
117 }
118 
119 
120 void LIB_EDIT_FRAME::CreatePNGorJPEGFile( const wxString& aFileName, bool aFmt_jpeg )
121 {
122  wxSize image_size = m_canvas->GetClientSize();
123 
124  wxClientDC dc( m_canvas );
125  wxBitmap bitmap( image_size.x, image_size.y );
126  wxMemoryDC memdc;
127 
128  memdc.SelectObject( bitmap );
129  memdc.Blit( 0, 0, image_size.x, image_size.y, &dc, 0, 0 );
130  memdc.SelectObject( wxNullBitmap );
131 
132  wxImage image = bitmap.ConvertToImage();
133 
134  if( !image.SaveFile( aFileName, aFmt_jpeg ? wxBITMAP_TYPE_JPEG : wxBITMAP_TYPE_PNG ) )
135  {
136  wxString msg;
137  msg.Printf( _( "Can't save file <%s>" ), GetChars( aFileName ) );
138  wxMessageBox( msg );
139  }
140 
141  image.Destroy();
142 }
143 
144 
145 void LIB_EDIT_FRAME::SVG_PlotComponent( const wxString& aFullFileName )
146 {
147  const bool plotBW = false;
148  const PAGE_INFO& pageInfo = GetScreen()->GetPageSettings();
149 
150  SVG_PLOTTER* plotter = new SVG_PLOTTER();
151  plotter->SetPageSettings( pageInfo );
153  plotter->SetColorMode( plotBW );
154 
155  wxPoint plot_offset;
156  const double scale = 1.0;
157 
158  // Currently, plot units are in decimil
159  plotter->SetViewport( plot_offset, IU_PER_MILS/10, scale, false );
160 
161  // Init :
162  plotter->SetCreator( wxT( "Eeschema-SVG" ) );
163 
164  if( ! plotter->OpenFile( aFullFileName ) )
165  {
166  delete plotter;
167  return;
168  }
169 
170  LOCALE_IO toggle;
171 
172  plotter->StartPlot();
173 
174  LIB_PART* part = GetCurPart();
175 
176  if( part )
177  {
178  TRANSFORM temp; // Uses default transform
179  wxPoint plotPos;
180 
181  plotPos.x = pageInfo.GetWidthIU() /2;
182  plotPos.y = pageInfo.GetHeightIU()/2;
183 
184  part->Plot( plotter, GetUnit(), GetConvert(), plotPos, temp );
185 
186  // Plot lib fields, not plotted by m_component->Plot():
187  part->PlotLibFields( plotter, GetUnit(), GetConvert(), plotPos, temp );
188  }
189 
190  plotter->EndPlot();
191  delete plotter;
192 }
193 
194 void LIB_EDIT_FRAME::PrintPage( wxDC* aDC, LSET aPrintMask, bool aPrintMirrorMode, void* aData)
195 {
196  LIB_PART* part = GetCurPart();
197 
198  if( !part )
199  return;
200 
201  wxSize pagesize = GetScreen()->GetPageSettings().GetSizeIU();
202 
203  /* Plot item centered to the page
204  * In libedit, the component is centered at 0,0 coordinates.
205  * So we must plot it with an offset = pagesize/2.
206  */
207  wxPoint plot_offset;
208  plot_offset.x = pagesize.x/2;
209  plot_offset.y = pagesize.y/2;
210 
211  part->Draw( m_canvas, aDC, plot_offset, m_unit, m_convert, PART_DRAW_OPTIONS::Default() );
212 }
213 
214 
void SVG_PlotComponent(const wxString &aFullFileName)
Function SVG_PlotComponent Creates the SVG print file for the current edited component.
void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDc, const wxPoint &aOffset, int aMulti, int aConvert, const PART_DRAW_OPTIONS &aOpts)
Draw part.
const EDA_RECT GetUnitBoundingBox(int aUnit, int aConvert) const
Function GetBoundingBox.
virtual void SetCreator(const wxString &aCreator)
Definition: plot_common.h:151
static int m_convert
Definition: libeditframe.h:99
This file is part of the common library TODO brief description.
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
This file is part of the common library.
virtual bool OpenFile(const wxString &aFullFilename)
Open or create the plot file aFullFilename.
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i...
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
void SetPageSettings(const PAGE_INFO &aPageSettings)
virtual void SetPageSettings(const PAGE_INFO &aPageSettings)
void PlotLibFields(PLOTTER *aPlotter, int aUnit, int aConvert, const wxPoint &aOffset, const TRANSFORM &aTransform)
Plot Lib Fields only of the part to plotter.
virtual bool EndPlot() override
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
Class for tranforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
Class LSET is a set of PCB_LAYER_IDs.
void Plot(PLOTTER *aPlotter, int aUnit, int aConvert, const wxPoint &aOffset, const TRANSFORM &aTransform)
Plot lib part to plotter.
virtual void SetDefaultLineWidth(int width) override
Set the default line width.
Definitions for the Eeschema program SCH_SCREEN class.
const wxString & GetName() const
Class PAGE_INFO describes the page size and margins of a paper page on which to eventually print or p...
wxString EDA_FILE_SELECTOR(const wxString &aTitle, const wxString &aPath, const wxString &aFileName, const wxString &aExtension, const wxString &aWildcard, wxWindow *aParent, int aStyle, const bool aKeepWorkingDirectory, const wxPoint &aPosition, wxString *aMruPath)
Function EDA_FILE_SELECTOR.
Definition: gestfich.cpp:82
Class LIB_PART defines a library part object.
void OnPlotCurrentComponent(wxCommandEvent &event)
Function OnPlotCurrentComponent plot the current component in SVG or PNG format.
virtual bool StartPlot() override
The code within this function creates SVG files header.
virtual void PrintPage(wxDC *aDC, LSET aPrintMask, bool aPrintMirrorMode, void *aData=NULL) override
Virtual function PrintPage used to print a page.
static PART_DRAW_OPTIONS Default()
void SetHeightMils(int aHeightInMils)
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
const int scale
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
see class PGM_BASE
static int m_unit
Definition: libeditframe.h:95
void SetWidthMils(int aWidthInMils)
LIB_PART * GetCurPart()
Function GetCurPart returns the current part being edited, or NULL if none selected.
void CreatePNGorJPEGFile(const wxString &aFileName, bool aFmt_jpeg)
Function CreatePNGorJPEGFile creates an image (screenshot) of the current component in PNG or JPEG fo...
const PAGE_INFO & GetPageSettings() const
Definition for part library class.
const wxSize & GetSize() const
virtual void SetColorMode(bool _color_mode)
Definition: plot_common.h:116
Definition of class LIB_EDIT_FRAME.