KiCad PCB EDA Suite
ds_data_model.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 (C) 1992-2018 Jean-Pierre Charras <jp.charras at wanadoo.fr>.
5  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 
27 /*
28  * The WS_DATA_ITEM_* classes define the basic shapes of a drawing sheet (frame references
29  * and title block). The list of these items is stored in a DS_DATA_MODEL instance.
30  *
31  * These items cannot be drawn or plotted "as is". They must be converted to WS_DRAW_*
32  * types. When building the draw list:
33  * - the DS_DATA_MODEL is used to create a DS_DRAW_ITEM_LIST
34  * - coordinates are converted to draw/plot coordinates.
35  * - texts are expanded if they contain format symbols.
36  * - items with m_RepeatCount > 1 are created m_RepeatCount times.
37  *
38  * The DS_DATA_MODEL is created only once.
39  * The WS_DRAW_ITEM_*s are created and maintained by the PlEditor, but are created each time
40  * they're needed for drawing by the clients (Eeschema, Pcbnew, etc.)
41  *
42  * The DS_DATA_MODEL instance is created from a S expression which describes the page
43  * layout (can be the default page layout or a custom file). This format is also used
44  * for undo/redo storage (wrapped in a DS_PROXY_UNDO_ITEM).
45  */
46 
47 #include <kiface_i.h>
48 #include <title_block.h>
49 #include <common.h>
50 #include <eda_item.h>
54 
55 
56 // The layout shape used in the application
57 // It is accessible by DS_DATA_MODEL::GetTheInstance()
60 
62  m_WSunits2Iu( 1000.0 ),
63  m_DefaultLineWidth( 0.0 ),
64  m_DefaultTextSize( TB_DEFAULT_TEXTSIZE, TB_DEFAULT_TEXTSIZE ),
65  m_DefaultTextThickness( 0.0 ),
66  m_EditMode( false )
67 {
68  m_allowVoidList = false;
69  m_leftMargin = 10.0; // the left page margin in mm
70  m_rightMargin = 10.0; // the right page margin in mm
71  m_topMargin = 10.0; // the top page margin in mm
72  m_bottomMargin = 10.0; // the bottom page margin in mm
73 }
74 
75 /*
76  * static function: returns the instance of DS_DATA_MODEL used in the application
77  */
79 {
80  if( wksAltInstance )
81  return *wksAltInstance;
82  else
83  return wksTheInstance;
84 }
85 
93 {
94  wksAltInstance = aLayout;
95 }
96 
97 
98 void DS_DATA_MODEL::SetupDrawEnvironment( const PAGE_INFO& aPageInfo, double aMilsToIU )
99 {
100 #define MILS_TO_MM (25.4/1000)
101 
102  m_WSunits2Iu = aMilsToIU / MILS_TO_MM;
103 
104  // Left top corner position
105  DPOINT lt_corner;
106  lt_corner.x = GetLeftMargin();
107  lt_corner.y = GetTopMargin();
108  m_LT_Corner = lt_corner;
109 
110  // Right bottom corner position
111  DPOINT rb_corner;
112  rb_corner.x = ( aPageInfo.GetSizeMils().x * MILS_TO_MM ) - GetRightMargin();
113  rb_corner.y = ( aPageInfo.GetSizeMils().y * MILS_TO_MM ) - GetBottomMargin();
114  m_RB_Corner = rb_corner;
115 }
116 
117 
119 {
120  for( DS_DATA_ITEM* item : m_list )
121  delete item;
122 
123  m_list.clear();
124 }
125 
126 
128 {
129  m_list.push_back( aItem );
130 }
131 
132 
134 {
135  auto newEnd = std::remove( m_list.begin(), m_list.end(), aItem );
136  m_list.erase( newEnd, m_list.end() );
137 }
138 
139 
141 {
142  unsigned idx = 0;
143  while( idx < m_list.size() )
144  {
145  if( m_list[idx] == aItem )
146  return (int) idx;
147 
148  idx++;
149  }
150 
151  return -1;
152 }
153 
154 /* return the item from its index aIdx, or NULL if does not exist
155  */
156 DS_DATA_ITEM* DS_DATA_MODEL::GetItem( unsigned aIdx ) const
157 {
158  if( aIdx < m_list.size() )
159  return m_list[aIdx];
160  else
161  return nullptr;
162 }
163 
164 
165 const wxString DS_DATA_MODEL::MakeShortFileName( const wxString& aFullFileName,
166  const wxString& aProjectPath )
167 {
168  wxString shortFileName = aFullFileName;
169  wxFileName fn = aFullFileName;
170 
171  if( fn.IsRelative() )
172  return shortFileName;
173 
174  if( ! aProjectPath.IsEmpty() && aFullFileName.StartsWith( aProjectPath ) )
175  {
176  fn.MakeRelativeTo( aProjectPath );
177  shortFileName = fn.GetFullPath();
178  return shortFileName;
179  }
180 
181  wxString fileName = Kiface().KifaceSearch().FindValidPath( fn.GetFullName() );
182 
183  if( !fileName.IsEmpty() )
184  {
185  fn = fileName;
186  shortFileName = fn.GetFullName();
187  return shortFileName;
188  }
189 
190  return shortFileName;
191 }
192 
193 
194 const wxString DS_DATA_MODEL::MakeFullFileName( const wxString& aShortFileName,
195  const wxString& aProjectPath )
196 {
197  wxString fullFileName = ExpandEnvVarSubstitutions( aShortFileName, nullptr );
198 
199  if( fullFileName.IsEmpty() )
200  return fullFileName;
201 
202  wxFileName fn = fullFileName;
203 
204  if( fn.IsAbsolute() )
205  return fullFileName;
206 
207  // the path is not absolute: search it in project path, and then in
208  // kicad valid paths
209  if( !aProjectPath.IsEmpty() )
210  {
211  fn.MakeAbsolute( aProjectPath );
212 
213  if( wxFileExists( fn.GetFullPath() ) )
214  return fn.GetFullPath();
215  }
216 
217  fn = fullFileName;
218  wxString name = Kiface().KifaceSearch().FindValidPath( fn.GetFullName() );
219 
220  if( !name.IsEmpty() )
221  fullFileName = name;
222 
223  return fullFileName;
224 }
void ClearList()
Erase the list of items.
wxString FindValidPath(const wxString &aFileName) const
Definition: search_stack.h:70
std::vector< DS_DATA_ITEM * > m_list
Handle the graphic items list to draw/plot the frame and title block.
Definition: ds_data_model.h:38
static void SetAltInstance(DS_DATA_MODEL *aLayout=NULL)
Set an alternate instance of DS_DATA_MODEL.
double GetRightMargin()
Definition: ds_data_model.h:63
static DS_DATA_MODEL wksTheInstance
double GetTopMargin()
Definition: ds_data_model.h:66
#define TB_DEFAULT_TEXTSIZE
Definition: ds_data_item.h:35
double GetLeftMargin()
Definition: ds_data_model.h:60
DPOINT m_RB_Corner
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:273
double m_topMargin
DPOINT m_LT_Corner
int GetItemIndex(DS_DATA_ITEM *aItem) const
SEARCH_STACK & KifaceSearch()
Only for DSO specific 'non-library' files.
Definition: kiface_i.h:113
double m_bottomMargin
static DS_DATA_MODEL & GetTheInstance()
static function: returns the instance of DS_DATA_MODEL used in the application
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:53
double GetBottomMargin()
Definition: ds_data_model.h:69
#define MILS_TO_MM
static const wxString MakeShortFileName(const wxString &aFullFileName, const wxString &aProjectPath)
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
double m_leftMargin
double m_WSunits2Iu
const wxSize & GetSizeMils() const
Definition: page_info.h:135
double m_rightMargin
const char * name
Definition: DXF_plotter.cpp:59
DS_DATA_ITEM * GetItem(unsigned aIdx) const
Drawing sheet structure type definitions.
Definition: ds_data_item.h:95
static const wxString MakeFullFileName(const wxString &aShortFileName, const wxString &aProjectPath)
void SetupDrawEnvironment(const PAGE_INFO &aPageInfo, double aMilsToIU)
The common library.
void Remove(DS_DATA_ITEM *aItem)
void Append(DS_DATA_ITEM *aItem)
static DS_DATA_MODEL * wksAltInstance