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-2018 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 <sch_draw_panel.h>
34 #include <confirm.h>
35 #include <gestfich.h>
36 #include <eeschema_id.h>
37 #include <sch_screen.h>
39 
40 #include <general.h>
41 #include <lib_edit_frame.h>
42 #include <class_library.h>
44 
45 
46 void LIB_EDIT_FRAME::OnPlotCurrentComponent( wxCommandEvent& event )
47 {
48  wxString fullFileName;
49  wxString file_ext;
50  wxString mask;
51 
52  LIB_PART* part = GetCurPart();
53 
54  if( !part )
55  {
56  wxMessageBox( _( "No component" ) );
57  return;
58  }
59 
60  switch( event.GetId() )
61  {
63  {
64  mask = wxT( "*." ) + file_ext;
65  wxFileName fn( part->GetName() );
66  fn.SetExt( "png" );
67 
68  wxString projectPath = wxPathOnly( Prj().GetProjectFullName() );
69 
70  wxFileDialog dlg( this, _( "Image File Name" ), projectPath,
71  fn.GetFullName(), PngFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
72 
73  if( dlg.ShowModal() == wxID_CANCEL || dlg.GetPath().IsEmpty() )
74  return;
75 
76  // calling wxYield is mandatory under Linux, after closing the file selector dialog
77  // to refresh the screen before creating the PNG or JPEG image from screen
78  wxYield();
79  CreateImageFile( dlg.GetPath(), wxBITMAP_TYPE_PNG );
80  }
81  break;
82 
84  {
85  file_ext = wxT( "svg" );
86  mask = wxT( "*." ) + file_ext;
87  wxFileName fn( part->GetName() );
88  fn.SetExt( file_ext );
89 
90  wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() );
91 
92  fullFileName = EDA_FILE_SELECTOR( _( "Filename:" ), pro_dir,
93  fn.GetFullName(), file_ext, mask, this,
94  wxFD_SAVE, true );
95 
96  if( fullFileName.IsEmpty() )
97  return;
98 
99  PAGE_INFO pageSave = GetScreen()->GetPageSettings();
100  PAGE_INFO pageTemp = pageSave;
101 
102  wxSize componentSize = part->GetUnitBoundingBox( m_unit, m_convert ).GetSize();
103 
104  // Add a small margin to the plot bounding box
105  pageTemp.SetWidthMils( int( componentSize.x * 1.2 ) );
106  pageTemp.SetHeightMils( int( componentSize.y * 1.2 ) );
107 
108  GetScreen()->SetPageSettings( pageTemp );
109  SVG_PlotComponent( fullFileName );
110  GetScreen()->SetPageSettings( pageSave );
111  }
112  break;
113  }
114 }
115 
116 
117 void LIB_EDIT_FRAME::CreateImageFile( const wxString& aFileName, wxBitmapType aBitmapType )
118 {
119  if( !saveCanvasImageToFile( aFileName, aBitmapType ) )
120  {
121  wxString msg;
122  msg.Printf( _( "Can't save file \"%s\"." ), aFileName );
123  wxMessageBox( msg );
124  }
125 }
126 
127 
128 void LIB_EDIT_FRAME::SVG_PlotComponent( const wxString& aFullFileName )
129 {
130  const bool plotColor = true;
131  const PAGE_INFO& pageInfo = GetScreen()->GetPageSettings();
132 
133  SVG_PLOTTER* plotter = new SVG_PLOTTER();
134  plotter->SetPageSettings( pageInfo );
136  plotter->SetColorMode( plotColor );
137 
138  wxPoint plot_offset;
139  const double scale = 1.0;
140 
141  // Currently, plot units are in decimil
142  plotter->SetViewport( plot_offset, IU_PER_MILS/10, scale, false );
143 
144  // Init :
145  plotter->SetCreator( wxT( "Eeschema-SVG" ) );
146 
147  if( ! plotter->OpenFile( aFullFileName ) )
148  {
149  delete plotter;
150  return;
151  }
152 
153  LOCALE_IO toggle;
154 
155  plotter->StartPlot();
156 
157  LIB_PART* part = GetCurPart();
158 
159  if( part )
160  {
161  TRANSFORM temp; // Uses default transform
162  wxPoint plotPos;
163 
164  plotPos.x = pageInfo.GetWidthIU() /2;
165  plotPos.y = pageInfo.GetHeightIU()/2;
166 
167  part->Plot( plotter, GetUnit(), GetConvert(), plotPos, temp );
168 
169  // Plot lib fields, not plotted by m_component->Plot():
170  part->PlotLibFields( plotter, GetUnit(), GetConvert(), plotPos, temp );
171  }
172 
173  plotter->EndPlot();
174  delete plotter;
175 }
176 
177 
178 void LIB_EDIT_FRAME::PrintPage( wxDC* aDC, LSET aPrintMask, bool aPrintMirrorMode, void* aData )
179 {
180  LIB_PART* part = GetCurPart();
181 
182  if( !part )
183  return;
184 
185  wxSize pagesize = GetScreen()->GetPageSettings().GetSizeIU();
186 
187  /* Plot item centered to the page
188  * In libedit, the component is centered at 0,0 coordinates.
189  * So we must plot it with an offset = pagesize/2.
190  */
191  wxPoint plot_offset;
192  plot_offset.x = pagesize.x / 2;
193  plot_offset.y = pagesize.y / 2;
194 
195  part->Draw( m_canvas, aDC, plot_offset, m_unit, m_convert, PART_DRAW_OPTIONS::Default() );
196 }
void SVG_PlotComponent(const wxString &aFullFileName)
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
Get the bounding box for the symbol.
virtual void SetCreator(const wxString &aCreator)
Definition: plotter.h:161
static int m_convert
This file is part of the common library TODO brief description.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown...
Definition: common.h:179
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.
const wxSize GetSize() const
Definition: eda_rect.h:101
virtual bool OpenFile(const wxString &aFullFilename)
Open or create the plot file aFullFilename.
Definition: plotter.cpp:79
virtual void SetColorMode(bool aColorMode)
Plot in B/W or color.
Definition: plotter.h:126
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i...
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: sch_screen.h:127
virtual void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: plotter.cpp:537
void PlotLibFields(PLOTTER *aPlotter, int aUnit, int aConvert, const wxPoint &aOffset, const TRANSFORM &aTransform)
Plot Lib Fields only of the part to plotter.
bool saveCanvasImageToFile(const wxString &aFileName, wxBitmapType aBitmapType=wxBITMAP_TYPE_PNG)
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.
LIB_PART * GetCurPart() const
Return the current part being edited or NULL if none selected.
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.
Definition: PS_plotter.cpp:52
Class PAGE_INFO describes the page size and margins of a paper page on which to eventually print or p...
Definition: page_info.h:54
void CreateImageFile(const wxString &aFileName, wxBitmapType aBitmapType=wxBITMAP_TYPE_PNG)
Creates an image (screenshot) of the current symbol.
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
Define a library symbol object.
The common library.
void OnPlotCurrentComponent(wxCommandEvent &event)
Plot the current symbol 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
Print a page.
static PART_DRAW_OPTIONS Default()
void SetHeightMils(int aHeightInMils)
Definition: page_info.cpp:253
wxString PngFileWildcard()
Definitions for the Eeschema program SCH_SCREEN class.
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:123
const int scale
see class PGM_BASE
static int m_unit
void SetWidthMils(int aWidthInMils)
Definition: page_info.cpp:239
#define IU_PER_MILS
Definition: plotter.cpp:134
const wxString & GetName() const
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:126
Definition of class LIB_EDIT_FRAME.
Definition for part library class.