KiCad PCB EDA Suite
class_gerber_file_image_list.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 1992-2016 Jean-Pierre Charras jp.charras at wanadoo.fr
10  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, you may find one here:
24  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25  * or you may search the http://www.gnu.org website for the version 2 license,
26  * or you may write to the Free Software Foundation, Inc.,
27  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28  */
29 
30 #include <fctsys.h>
31 
32 #include <gerbview.h>
33 #include <gerbview_frame.h>
37 
38 #include <map>
39 
40 
41 // The global image list:
43 
44 
45 // GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files
48 {
50 
51  for( unsigned layer = 0; layer < GERBER_DRAWLAYERS_COUNT; ++layer )
52  m_GERBER_List.push_back( NULL );
53 }
54 
55 
57 {
59 }
60 
61 
63 {
64  return s_GERBER_List;
65 }
66 
67 
69 {
70  if( (unsigned)aIdx < m_GERBER_List.size() )
71  return m_GERBER_List[aIdx];
72 
73  return NULL;
74 }
75 
76 /* creates a new, empty GERBER_FILE_IMAGE* at index aIdx
77  * or at the first free location if aIdx < 0
78  * aIdx = the index of graphic layer to use, or -1 to uses the first free graphic layer
79  * return the index actually used, or -1 if no room to add image
80  */
82 {
83  int idx = aIdx;
84 
85  if( idx < 0 )
86  {
87  for( idx = 0; idx < (int)m_GERBER_List.size(); idx++ )
88  {
89  if( m_GERBER_List[idx] == NULL )
90  break;
91  }
92  }
93 
94  if( idx >= (int)m_GERBER_List.size() )
95  return -1; // No room
96 
97  m_GERBER_List[idx] = aGbrImage;
98 
99  return idx;
100 }
101 
102 
104 {
105  for( unsigned idx = 0; idx < m_GERBER_List.size(); ++idx )
106  DeleteImage( idx );
107 }
108 
109 
111 {
112  // Ensure the index is valid:
113  if( aIdx < 0 || aIdx >= int( m_GERBER_List.size() ) )
114  return;
115 
116  // delete image aIdx
117  GERBER_FILE_IMAGE* gbr_image = GetGbrImage( aIdx );
118 
119  delete gbr_image;
120  m_GERBER_List[ aIdx ] = NULL;
121 }
122 
123 // Build a name for image aIdx which can be used in layers manager
124 const wxString GERBER_FILE_IMAGE_LIST::GetDisplayName( int aIdx, bool aNameOnly )
125 {
126  wxString name;
127 
128  GERBER_FILE_IMAGE* gerber = NULL;
129 
130  if( aIdx >= 0 && aIdx < (int)m_GERBER_List.size() )
131  gerber = m_GERBER_List[aIdx];
132 
133  // if a file is loaded, build the name:
134  // <id> <short filename> <X2 FileFunction info> if a X2 FileFunction info is found
135  // or (if no FileFunction info)
136  // <id> <short filename> *
137  if( gerber )
138  {
139  wxFileName fn( gerber->m_FileName );
140  wxString filename = fn.GetFullName();
141 
142  // if the filename is too long, display a shortened name:
143  const int maxlen = 30;
144 
145  if( filename.Length() > maxlen )
146  {
147  wxString shortenedfn = filename.Left(2) + "..." + filename.Right(maxlen-5);
148  filename = shortenedfn;
149  }
150 
151  if( gerber->m_FileFunction )
152  {
153  if( gerber->m_FileFunction->IsCopper() )
154  {
155  name.Printf( "%s (%s, %s, %s)",
156  filename.GetData(),
157  GetChars( gerber->m_FileFunction->GetFileType() ),
158  GetChars( gerber->m_FileFunction->GetBrdLayerId() ),
159  GetChars( gerber->m_FileFunction->GetBrdLayerSide() ) );
160  }
161  if( gerber->m_FileFunction->IsDrillFile() )
162  {
163  name.Printf( "%s (%s,%s,%s,%s)",
164  filename.GetData(),
165  GetChars( gerber->m_FileFunction->GetFileType() ),
167  GetChars( gerber->m_FileFunction->GetLPType() ),
168  GetChars( gerber->m_FileFunction->GetRouteType() ) );
169  }
170  else
171  {
172  name.Printf( "%s (%s, %s)",
173  filename.GetData(),
174  GetChars( gerber->m_FileFunction->GetFileType() ),
175  GetChars( gerber->m_FileFunction->GetBrdLayerId() ) );
176  }
177  }
178  else
179  name = filename;
180 
181  if( aNameOnly )
182  return name;
183 
184  wxString fullname;
185 
186  fullname.Printf( "%d ", aIdx + 1 );
187  fullname << name;
188  return fullname;
189  }
190  else
191  name.Printf( _( "Graphic layer %d" ), aIdx + 1 );
192 
193  return name;
194 }
195 
196 
197 
198 // Helper function, for std::sort.
199 // Sort loaded images by Z order priority, if they have the X2 FileFormat info
200 // returns true if the first argument (ref) is ordered before the second (test).
201 static bool sortZorder( const GERBER_FILE_IMAGE* const& ref, const GERBER_FILE_IMAGE* const& test )
202 {
203  if( !ref && !test )
204  return false; // do not change order: no criteria to sort items
205 
206  if( !ref || !ref->m_InUse )
207  return false; // Not used: ref ordered after
208 
209  if( !test || !test->m_InUse )
210  return true; // Not used: ref ordered before
211 
212  if( !ref->m_FileFunction && !test->m_FileFunction )
213  return false; // do not change order: no criteria to sort items
214 
215  if( !ref->m_FileFunction )
216  return false;
217 
218  if( !test->m_FileFunction )
219  return true;
220 
221  if( ref->m_FileFunction->GetZOrder() != test->m_FileFunction->GetZOrder() )
222  return ref->m_FileFunction->GetZOrder() > test->m_FileFunction->GetZOrder();
223 
224  return ref->m_FileFunction->GetZSubOrder() > test->m_FileFunction->GetZSubOrder();
225 }
226 
228 {
229  std::sort( m_GERBER_List.begin(), m_GERBER_List.end(), sortZorder );
230 
231  // The image order has changed.
232  // Graphic layer numbering must be updated to match the widgets layer order
233 
234  // Store the old/new graphic layer info:
235  std::map <int, int> tab_lyr;
236 
237  for( unsigned layer = 0; layer < m_GERBER_List.size(); ++layer )
238  {
239  GERBER_FILE_IMAGE* gerber = m_GERBER_List[layer];
240 
241  if( !gerber )
242  continue;
243 
244  tab_lyr[gerber->m_GraphicLayer] = layer;
245  gerber->m_GraphicLayer = layer ;
246  }
247 }
const wxString & GetFileType()
the type of layer (Copper, Soldermask ... )
X2_ATTRIBUTE_FILEFUNCTION * m_FileFunction
void DeleteAllImages()
remove all loaded data in list, and delete all images.
const wxString GetDisplayName(int aIdx, bool aNameOnly=false)
void SortImagesByZOrder()
Sort loaded images by Z order priority, if they have the X2 FileFormat info (SortImagesByZOrder updat...
Class GERBER_FILE_IMAGE holds the Image data and parameters for one gerber file and layer parameters ...
int AddGbrImage(GERBER_FILE_IMAGE *aGbrImage, int aIdx)
Add a GERBER_FILE_IMAGE* at index aIdx or at the first free location if aIdx < 0. ...
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
bool IsCopper()
return true if the filefunction type is "Copper"
int GetZSubOrder()
the Order of the bdr copper layer, from front (Top) side to back (Bot) side
#define GERBER_DRAWLAYERS_COUNT
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
static GERBER_FILE_IMAGE_LIST & GetImagesList()
const wxString & GetBrdLayerId()
the brd layer identifier: Ln, only for Copper type or Top, Bot for other types
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
GERBER_FILE_IMAGE_LIST s_GERBER_List
const wxString & GetBrdLayerSide()
the brd layer Pos: Top, Bot, Inr same as GetBrdLayerId() for non copper type
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:165
const char * name
void DeleteImage(int aIdx)
delete the loaded data of image aIdx.
int GetZOrder()
the Order of the board layer, from front (Top) side to back (Bot) side
std::vector< GERBER_FILE_IMAGE * > m_GERBER_List
static bool sortZorder(const GERBER_FILE_IMAGE *const &ref, const GERBER_FILE_IMAGE *const &test)