1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 1992-2011 Jean-Pierre Charras.
5  * Copyright (C) 2013 Wayne Stambaugh <stambaughw@gmail.com>.
6  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
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 #include <string_utils.h>
28 #include "pcb_netlist.h"
29 #include <footprint.h>
30 
31 
Format(OUTPUTFORMATTER * aOut,int aNestLevel,int aCtl)32 int COMPONENT_NET::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
33 {
34     return aOut->Print( aNestLevel, "(pin_net %s %s)",
35                         aOut->Quotew( m_pinName ).c_str(),
36                         aOut->Quotew( m_netName ).c_str() );
37 }
38 
39 
SetFootprint(FOOTPRINT * aFootprint)40 void COMPONENT::SetFootprint( FOOTPRINT* aFootprint )
41 {
42     m_footprint.reset( aFootprint );
43     KIID_PATH path = m_path;
44 
45     if( !m_kiids.empty() )
46         path.push_back( m_kiids.front() );
47 
48     if( aFootprint == nullptr )
49         return;
50 
51     aFootprint->SetReference( m_reference );
52     aFootprint->SetValue( m_value );
53     aFootprint->SetFPID( m_fpid );
54     aFootprint->SetPath( path );
55     aFootprint->SetProperties( m_properties );
56 }
57 
58 
59 COMPONENT_NET COMPONENT::m_emptyNet;
60 
61 
GetNet(const wxString & aPinName) const62 const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName ) const
63 {
64     for( const COMPONENT_NET& net : m_nets )
65     {
66         if( net.GetPinName() == aPinName )
67             return net;
68     }
69 
70     return m_emptyNet;
71 }
72 
73 
Format(OUTPUTFORMATTER * aOut,int aNestLevel,int aCtl)74 void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
75 {
76     int nl = aNestLevel;
77 
78     aOut->Print( nl, "(ref %s ",      aOut->Quotew( m_reference ).c_str() );
79     aOut->Print( 0, "(fpid %s)\n",    aOut->Quotew( m_fpid.Format() ).c_str() );
80 
81     if( !( aCtl & CTL_OMIT_EXTRA ) )
82     {
83         aOut->Print( nl+1, "(value %s)\n",    aOut->Quotew( m_value ).c_str() );
84         aOut->Print( nl+1, "(name %s)\n",     aOut->Quotew( m_name ).c_str() );
85         aOut->Print( nl+1, "(library %s)\n",  aOut->Quotew( m_library ).c_str() );
86 
87         wxString path;
88 
89         for( const KIID& pathStep : m_path )
90             path += '/' + pathStep.AsString();
91 
92         if( !( aCtl & CTL_OMIT_FP_UUID ) && !m_kiids.empty() )
93             path += '/' + m_kiids.front().AsString();
94 
95         aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
96     }
97 
98     if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
99     {
100         aOut->Print( nl+1, "(fp_filters" );
101 
102         for( unsigned i = 0;  i < m_footprintFilters.GetCount();  ++i )
103             aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
104 
105         aOut->Print( 0, ")\n" );
106     }
107 
108     if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
109     {
110         int llen = aOut->Print( nl+1, "(nets " );
111 
112         for( unsigned i = 0;  i < m_nets.size();  ++i )
113         {
114             if( llen > 80 )
115             {
116                 aOut->Print( 0, "\n" );
117                 llen = aOut->Print( nl+1, "  " );
118             }
119 
120             llen += m_nets[i].Format( aOut, 0, aCtl );
121         }
122 
123         aOut->Print( 0, ")\n" );
124     }
125 
126     aOut->Print( nl, ")\n" );    // </ref>
127 }
128 
129 
Format(const char * aDocName,OUTPUTFORMATTER * aOut,int aNestLevel,int aCtl)130 void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
131 {
132     int nl = aNestLevel;
133 
134     aOut->Print( nl, "(%s\n", aDocName );
135 
136     for( unsigned i = 0;  i < m_components.size();  i++ )
137     {
138         m_components[i].Format( aOut, nl+1, aCtl );
139     }
140 
141     aOut->Print( nl, ")\n" );
142 }
143 
144 
AddComponent(COMPONENT * aComponent)145 void NETLIST::AddComponent( COMPONENT* aComponent )
146 {
147     m_components.push_back( aComponent );
148 }
149 
150 
GetComponentByReference(const wxString & aReference)151 COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
152 {
153     COMPONENT* component = nullptr;
154 
155     for( unsigned i = 0;  i < m_components.size();  i++ )
156     {
157         if( m_components[i].GetReference() == aReference )
158         {
159             component = &m_components[i];
160             break;
161         }
162     }
163 
164     return component;
165 }
166 
167 
GetComponentByPath(const KIID_PATH & aUuidPath)168 COMPONENT* NETLIST::GetComponentByPath( const KIID_PATH& aUuidPath )
169 {
170     if( aUuidPath.empty() )
171         return nullptr;
172 
173     KIID comp_uuid = aUuidPath.back();
174     KIID_PATH base = aUuidPath;
175 
176     if( !base.empty() )
177         base.pop_back();
178 
179     for( COMPONENT& component : m_components )
180     {
181         const std::vector<KIID>& kiids = component.GetKIIDs();
182 
183         if( base != component.GetPath() )
184             continue;
185 
186         if( std::find( kiids.begin(), kiids.end(), comp_uuid ) != kiids.end() )
187             return &component;
188     }
189 
190     return nullptr;
191 }
192 
193 
194 /**
195  * A helper function used to sort the component list used by loadNewModules.
196  */
ByFPID(const COMPONENT & ref,const COMPONENT & cmp)197 static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
198 {
199     return ref.GetFPID() > cmp.GetFPID();
200 }
201 
202 
SortByFPID()203 void NETLIST::SortByFPID()
204 {
205     m_components.sort( ByFPID );
206 }
207 
208 
209 /**
210  * Compare two #COMPONENT objects by reference designator.
211  */
operator <(const COMPONENT & item1,const COMPONENT & item2)212 bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
213 {
214     return StrNumCmp( item1.GetReference(), item2.GetReference(), true ) < 0;
215 }
216 
217 
SortByReference()218 void NETLIST::SortByReference()
219 {
220     m_components.sort();
221 }
222 
223 
AnyFootprintsLinked() const224 bool NETLIST::AnyFootprintsLinked() const
225 {
226     for( unsigned i = 0;  i < m_components.size();  i++ )
227     {
228         if( !m_components[i].GetFPID().empty() )
229             return true;
230     }
231 
232     return false;
233 }
234 
235 
236