1 /**
2  * @file pcbnew/netlist.cpp
3  */
4 /*
5  * This program source code file is part of KiCad, a free EDA CAD application.
6  *
7  * Copyright (C) 1992-2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
8  * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
9  * Copyright (C) 2013-2016 Wayne Stambaugh <stambaughw@verizon.net>
10  * Copyright (C) 1992-2016 KiCad Developers, see change_log.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 <functional>
31 using namespace std::placeholders;
32 
33 #include <kiway.h>
34 #include <pcb_edit_frame.h>
35 #include <netlist_reader/pcb_netlist.h>
36 #include <netlist_reader/netlist_reader.h>
37 #include <reporter.h>
38 #include <lib_id.h>
39 #include <fp_lib_table.h>
40 #include <board.h>
41 #include <footprint.h>
42 #include <ratsnest/ratsnest_data.h>
43 #include <io_mgr.h>
44 #include "board_netlist_updater.h"
45 #include <tool/tool_manager.h>
46 #include <tools/pcb_actions.h>
47 #include <tools/pcb_selection_tool.h>
48 #include <project/project_file.h>  // LAST_PATH_TYPE
49 #include <wx/msgdlg.h>
50 
51 
52 extern void SpreadFootprints( std::vector<FOOTPRINT*>* aFootprints, wxPoint aSpreadAreaPosition );
53 
54 
ReadNetlistFromFile(const wxString & aFilename,NETLIST & aNetlist,REPORTER & aReporter)55 bool PCB_EDIT_FRAME::ReadNetlistFromFile( const wxString &aFilename, NETLIST& aNetlist,
56                                           REPORTER& aReporter )
57 {
58     wxString msg;
59 
60     try
61     {
62         std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
63                 &aNetlist, aFilename, wxEmptyString ) );
64 
65         if( !netlistReader.get() )
66         {
67             msg.Printf( _( "Cannot open netlist file '%s'." ), aFilename );
68             wxMessageBox( msg, _( "Netlist Load Error." ), wxOK | wxICON_ERROR, this );
69             return false;
70         }
71 
72         SetLastPath( LAST_PATH_NETLIST, aFilename );
73         netlistReader->LoadNetlist();
74         LoadFootprints( aNetlist, aReporter );
75     }
76     catch( const IO_ERROR& ioe )
77     {
78         msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
79         wxMessageBox( msg, _( "Netlist Load Error" ), wxOK | wxICON_ERROR );
80         return false;
81     }
82 
83     SetLastPath( LAST_PATH_NETLIST, aFilename );
84 
85     return true;
86 }
87 
88 
OnNetlistChanged(BOARD_NETLIST_UPDATER & aUpdater,bool * aRunDragCommand)89 void PCB_EDIT_FRAME::OnNetlistChanged( BOARD_NETLIST_UPDATER& aUpdater, bool* aRunDragCommand )
90 {
91     std::string dummyPayload;
92     Kiway().ExpressMail( FRAME_SCH, MAIL_SCH_CLEAN_NETCLASSES, dummyPayload, this );
93 
94     BOARD* board = GetBoard();
95 
96     SetMsgPanel( board );
97 
98     // Update rendered tracks and vias net labels
99     // TODO is there a way to extract information about which nets were modified?
100     for( auto track : board->Tracks() )
101         GetCanvas()->GetView()->Update( track );
102 
103     std::vector<FOOTPRINT*> newFootprints = aUpdater.GetAddedFootprints();
104 
105     // Spread new footprints.
106     wxPoint areaPosition = (wxPoint) GetCanvas()->GetViewControls()->GetCursorPosition();
107     EDA_RECT bbox = board->GetBoundingBox();
108 
109     GetToolManager()->RunAction( PCB_ACTIONS::selectionClear, true );
110 
111     SpreadFootprints( &newFootprints, areaPosition );
112 
113     // Start drag command for new footprints
114     if( !newFootprints.empty() )
115     {
116         for( FOOTPRINT* footprint : newFootprints )
117             GetToolManager()->RunAction( PCB_ACTIONS::selectItem, true, footprint );
118 
119         *aRunDragCommand = true;
120 
121         // Now fix a reference point to move the footprints.
122         // We use the first footprint in list as reference point
123         // The graphic cursor will be on this fp when moving the footprints.
124         PCB_SELECTION_TOOL* selTool = GetToolManager()->GetTool<PCB_SELECTION_TOOL>();
125         PCB_SELECTION&      selection = selTool->GetSelection();
126         selection.SetReferencePoint( newFootprints[0]->GetPosition() );
127     }
128 
129     Compile_Ratsnest( true );
130 
131     GetCanvas()->Refresh();
132 }
133 
134 
LoadFootprints(NETLIST & aNetlist,REPORTER & aReporter)135 void PCB_EDIT_FRAME::LoadFootprints( NETLIST& aNetlist, REPORTER& aReporter )
136 {
137     wxString   msg;
138     LIB_ID     lastFPID;
139     COMPONENT* component;
140     FOOTPRINT* footprint = nullptr;
141     FOOTPRINT* fpOnBoard = nullptr;
142 
143     if( aNetlist.IsEmpty() || Prj().PcbFootprintLibs()->IsEmpty() )
144         return;
145 
146     aNetlist.SortByFPID();
147 
148     for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
149     {
150         component = aNetlist.GetComponent( ii );
151 
152         // The FPID is ok as long as there is a footprint portion coming from eeschema.
153         if( !component->GetFPID().GetLibItemName().size() )
154         {
155             msg.Printf( _( "No footprint defined for symbol %s." ),
156                         component->GetReference() );
157             aReporter.Report( msg, RPT_SEVERITY_ERROR );
158 
159             continue;
160         }
161 
162         // Check if component footprint is already on BOARD and only load the footprint from
163         // the library if it's needed.  Nickname can be blank.
164         if( aNetlist.IsFindByTimeStamp() )
165         {
166             for( const KIID& uuid : component->GetKIIDs() )
167             {
168                 KIID_PATH path = component->GetPath();
169                 path.push_back( uuid );
170 
171                 if( ( fpOnBoard = m_pcb->FindFootprintByPath( path ) ) )
172                     break;
173             }
174         }
175         else
176             fpOnBoard = m_pcb->FindFootprintByReference( component->GetReference() );
177 
178         bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
179 
180         if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
181         {
182             msg.Printf( _( "Footprint of %s changed: board footprint '%s', netlist footprint '%s'." ),
183                         component->GetReference(),
184                         fpOnBoard->GetFPID().Format().wx_str(),
185                         component->GetFPID().Format().wx_str() );
186             aReporter.Report( msg, RPT_SEVERITY_WARNING );
187 
188             continue;
189         }
190 
191         if( !aNetlist.GetReplaceFootprints() )
192             footprintMisMatch = false;
193 
194         if( fpOnBoard && !footprintMisMatch )   // nothing else to do here
195             continue;
196 
197         if( component->GetFPID() != lastFPID )
198         {
199             footprint = nullptr;
200 
201             // The LIB_ID is ok as long as there is a footprint portion coming the library if
202             // it's needed.  Nickname can be blank.
203             if( !component->GetFPID().GetLibItemName().size() )
204             {
205                 msg.Printf( _( "%s footprint ID '%s' is not valid." ),
206                             component->GetReference(),
207                             component->GetFPID().Format().wx_str() );
208                 aReporter.Report( msg, RPT_SEVERITY_ERROR );
209 
210                 continue;
211             }
212 
213             // loadFootprint() can find a footprint with an empty nickname in fpid.
214             footprint = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
215 
216             if( footprint )
217             {
218                 lastFPID = component->GetFPID();
219             }
220             else
221             {
222                 msg.Printf( _( "%s footprint '%s' not found in any libraries in the footprint "
223                                "library table." ),
224                             component->GetReference(),
225                             component->GetFPID().GetLibItemName().wx_str() );
226                 aReporter.Report( msg, RPT_SEVERITY_ERROR );
227 
228                 continue;
229             }
230         }
231         else
232         {
233             // Footprint already loaded from a library, duplicate it (faster)
234             if( !footprint )
235                 continue;            // Footprint does not exist in any library.
236 
237             footprint = new FOOTPRINT( *footprint );
238             const_cast<KIID&>( footprint->m_Uuid ) = KIID();
239         }
240 
241         if( footprint )
242             component->SetFootprint( footprint );
243     }
244 }
245