KiCad PCB EDA Suite
Loading...
Searching...
No Matches
odb_eda_data.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 * Author: SYSUEric <[email protected]>.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "odb_eda_data.h"
22
23#include <base_units.h>
24#include <build_version.h>
25#include <hash_eda.h>
26#include <string_utils.h>
27
28#include <netinfo.h>
29#include <odb_feature.h>
30#include <pcb_io_odbpp.h>
31
32
34{
35 auto& x = nets_map.emplace( std::piecewise_construct, std::forward_as_tuple( 0 ),
36 std::forward_as_tuple( nets.size(), "$NONE$" ) )
37 .first->second;
38
39 nets.push_back( &x );
40}
41
42
43void EDA_DATA::NET::Write( std::ostream& ost ) const
44{
45 ost << "NET " << m_name;
46
47 WriteAttributes( ost );
48
49 ost << std::endl;
50
51 for( const auto& subnet : subnets )
52 {
53 subnet->Write( ost );
54 }
55}
56
57
58void EDA_DATA::AddNET( const NETINFO_ITEM* aNet )
59{
60 if( nets_map.end() == nets_map.find( aNet->GetNetCode() ) )
61 {
62 wxString netName = aNet->GetNetname();
63 ODB::RemoveWhitespace( netName );
64
65 auto& net = nets_map.emplace( std::piecewise_construct,
66 std::forward_as_tuple( aNet->GetNetCode() ),
67 std::forward_as_tuple( nets.size(), netName ) )
68 .first->second;
69
70 nets.push_back( &net );
71
72 //TODO: netname check
73 }
74}
75
76
77void EDA_DATA::SUB_NET::Write( std::ostream& ost ) const
78{
79 ost << "SNT ";
80
81 WriteSubnet( ost );
82
83 ost << std::endl;
84
85 for( const auto& fid : feature_ids )
86 {
87 fid.Write( ost );
88 }
89}
90
91
92void EDA_DATA::FEATURE_ID::Write( std::ostream& ost ) const
93{
94 static const std::map<TYPE, std::string> type_map = {
95 { TYPE::COPPER, "C" },
96 { TYPE::HOLE, "H" },
97 };
98
99 ost << "FID " << type_map.at( type ) << " " << layer << " " << feature_id << std::endl;
100}
101
102
103void EDA_DATA::SUB_NET_VIA::WriteSubnet( std::ostream& ost ) const
104{
105 ost << "VIA";
106}
107
108
109void EDA_DATA::SUB_NET_TRACE::WriteSubnet( std::ostream& ost ) const
110{
111 ost << "TRC";
112}
113
114
115void EDA_DATA::SUB_NET_PLANE::WriteSubnet( std::ostream& ost ) const
116{
117 static const std::map<FILL_TYPE, std::string> fill_type_map = { { FILL_TYPE::SOLID, "S" },
118 { FILL_TYPE::OUTLINE, "O" } };
119
120 static const std::map<CUTOUT_TYPE, std::string> cutout_type_map = {
121 { CUTOUT_TYPE::CIRCLE, "C" },
122 { CUTOUT_TYPE::RECT, "R" },
123 { CUTOUT_TYPE::OCTAGON, "O" },
124 { CUTOUT_TYPE::EXACT, "E" }
125 };
126
127 ost << "PLN " << fill_type_map.at( fill_type ) << " " << cutout_type_map.at( cutout_type )
128 << " " << fill_size;
129}
130
131
132void EDA_DATA::SUB_NET_TOEPRINT::WriteSubnet( std::ostream& ost ) const
133{
134 static const std::map<SIDE, std::string> side_map = {
135 { SIDE::BOTTOM, "B" },
136 { SIDE::TOP, "T" },
137 };
138 ost << "TOP " << side_map.at( side ) << " " << comp_num << " " << toep_num;
139}
140
141
142void EDA_DATA::SUB_NET::AddFeatureID( FEATURE_ID::TYPE type, const wxString& layer,
143 size_t feature_id )
144{
145 feature_ids.emplace_back( type, m_edadata->GetLyrIdx( layer ), feature_id );
146}
147
148
149size_t EDA_DATA::GetLyrIdx( const wxString& aLayer )
150{
151 if( layers_map.count( aLayer ) )
152 {
153 return layers_map.at( aLayer );
154 }
155 else
156 {
157 auto idx = layers_map.size();
158 layers_map.emplace( aLayer, idx );
159 layers.push_back( aLayer );
160 return idx;
161 }
162}
163
164
165void OUTLINE_SQUARE::Write( std::ostream& ost ) const
166{
167 ost << "SQ " << ODB::Data2String( m_center.x ) << " " << ODB::Data2String( m_center.y ) << " "
168 << ODB::Data2String( m_halfSide ) << std::endl;
169}
170
171
172void OUTLINE_CIRCLE::Write( std::ostream& ost ) const
173{
174 ost << "CR " << ODB::Data2String( m_center.x ) << " " << ODB::Data2String( m_center.y ) << " "
175 << ODB::Data2String( m_radius ) << std::endl;
176}
177
178
179void OUTLINE_RECT::Write( std::ostream& ost ) const
180{
181 ost << "RC " << ODB::Data2String( m_lower_left.x ) << " " << ODB::Data2String( m_lower_left.y )
182 << " " << ODB::Data2String( m_width ) << " " << ODB::Data2String( m_height ) << std::endl;
183}
184
185
186void OUTLINE_CONTOUR::Write( std::ostream& ost ) const
187{
188 ost << "CT" << std::endl;
189 m_surfaces->WriteData( ost );
190 ost << "CE" << std::endl;
191}
192
193
195{
196 // ODBPP only need unique PACKAGE in PKG record in eda/data file.
197 // the PKG index can repeat to be ref in CMP record in component file.
198
199 std::shared_ptr<FOOTPRINT> fp( static_cast<FOOTPRINT*>( aFp->Clone() ) );
200 m_eda_footprints.emplace_back( fp );
201 fp->SetParentGroup( nullptr );
202 fp->SetPosition( { 0, 0 } );
203
204 if( aFp->IsFlipped() )
205 {
206 // ODB++ needs both flips
207 fp->Flip( fp->GetPosition(), FLIP_DIRECTION::LEFT_RIGHT );
208 fp->Flip( fp->GetPosition(), FLIP_DIRECTION::TOP_BOTTOM );
209 }
210
211 fp->SetOrientation( ANGLE_0 );
212
213 size_t hash = hash_fp_item( fp.get(), HASH_POS | REL_COORD );
214 size_t pkg_index = packages_map.size();
215 wxString fp_name = fp->GetFPID().GetLibItemName().wx_str();
216 ODB::RemoveWhitespace( fp_name );
217
218 if( fp_name.IsEmpty() )
219 fp_name = wxS( "__" );
220
221 auto [iter, success] = packages_map.emplace( hash, PACKAGE( pkg_index, fp_name ) );
222
223 if( !success )
224 {
225 return;
226 }
227
228 PACKAGE* pkg = &( iter->second );
229
230 packages.push_back( pkg );
231
232 BOX2I bbox = fp->GetBoundingBox();
233 pkg->m_xmin = bbox.GetPosition().x;
234 pkg->m_ymin = bbox.GetPosition().y;
235 pkg->m_xmax = bbox.GetEnd().x;
236 pkg->m_ymax = bbox.GetEnd().y;
237 pkg->m_pitch = UINT64_MAX;
238
239 if( fp->Pads().size() < 2 )
240 pkg->m_pitch = pcbIUScale.mmToIU( 1.0 ); // placeholder value
241
242 for( size_t i = 0; i < fp->Pads().size(); ++i )
243 {
244 const PAD* pad1 = fp->Pads()[i];
245
246 for( size_t j = i + 1; j < fp->Pads().size(); ++j )
247 {
248 const PAD* pad2 = fp->Pads()[j];
249 const uint64_t pin_dist = ( pad1->GetCenter() - pad2->GetCenter() ).EuclideanNorm();
250 pkg->m_pitch = std::min( pkg->m_pitch, pin_dist );
251 }
252 }
253
254 const SHAPE_POLY_SET& courtyard = fp->GetCourtyard( F_CrtYd );
255 const SHAPE_POLY_SET& courtyard_back = fp->GetCourtyard( B_CrtYd );
256 SHAPE_POLY_SET pkg_outline;
257
258 if( courtyard.OutlineCount() > 0 )
259 pkg_outline = courtyard;
260
261 if( courtyard_back.OutlineCount() > 0 )
262 {
263 pkg_outline = courtyard_back;
264 }
265
266 if( !courtyard.OutlineCount() && !courtyard_back.OutlineCount() )
267 {
268 pkg_outline = fp->GetBoundingHull();
269 }
270
271 // TODO: Here we put rect, square, and circle, all as polygon
272
273 if( pkg_outline.OutlineCount() > 0 )
274 {
275 for( int ii = 0; ii < pkg_outline.OutlineCount(); ++ii )
276 {
277 pkg->m_pkgOutlines.push_back(
278 std::make_unique<OUTLINE_CONTOUR>( pkg_outline.Polygon( ii ) ) );
279 }
280 }
281
282 for( size_t i = 0; i < fp->Pads().size(); ++i )
283 {
284 const PAD* pad = fp->Pads()[i];
285 pkg->AddPin( pad, i );
286 }
287
288 return;
289}
290
291
292void EDA_DATA::PACKAGE::AddPin( const PAD* aPad, size_t aPinNum )
293{
294 wxString name = aPad->GetNumber();
295
296 // ODB is unhappy with whitespace in most places
298
299 // Pins are required to have names, so if our pad doesn't have a name, we need to
300 // generate one that is unique
301
302 if( aPad->GetAttribute() == PAD_ATTRIB::NPTH )
303 name = wxString::Format( "NPTH%zu", aPinNum );
304 else if( name.empty() )
305 name = wxString::Format( "PAD%zu", aPinNum );
306
307 // // for SNT record, pad, net, pin
308 std::shared_ptr<PIN> pin = std::make_shared<PIN>( m_pinsVec.size(), name );
309 m_pinsVec.push_back( pin );
310
311 VECTOR2D relpos = aPad->GetFPRelativePosition();
312
313 pin->m_center = ODB::AddXY( relpos );
314
315 if( aPad->HasHole() )
316 {
317 pin->type = PIN::TYPE::THROUGH_HOLE;
318 }
319 else
320 {
321 pin->type = PIN::TYPE::SURFACE;
322 }
323
324 if( aPad->GetAttribute() == PAD_ATTRIB::NPTH )
326 else if( aPad->IsOnCopperLayer() )
328 else
330
331
332 if( ( aPad->HasHole() && aPad->IsOnCopperLayer() ) || aPad->GetAttribute() == PAD_ATTRIB::PTH )
333 {
335 }
336 else if( aPad->HasHole() && aPad->GetAttribute() == PAD_ATTRIB::NPTH )
337 {
338 pin->mtype = PIN::MOUNT_TYPE::HOLE;
339 }
340 else if( aPad->GetAttribute() == PAD_ATTRIB::SMD )
341 {
342 pin->mtype = PIN::MOUNT_TYPE::SMT;
343 }
344 else
345 {
347 }
348
349 const std::shared_ptr<SHAPE_POLY_SET>& polygons =
351
352 // TODO: Here we put all pad shapes as polygonl, we should switch by pad shape
353 // Note:pad only use polygons->Polygon(0),
354 if( polygons->OutlineCount() > 0 )
355 {
356 pin->m_pinOutlines.push_back( std::make_unique<OUTLINE_CONTOUR>( polygons->Polygon( 0 ) ) );
357 }
358}
359
360
361void EDA_DATA::PIN::Write( std::ostream& ost ) const
362{
363 static const std::map<TYPE, std::string> type_map = { { TYPE::SURFACE, "S" },
364 { TYPE::THROUGH_HOLE, "T" },
365 { TYPE::BLIND, "B" } };
366
367 static const std::map<ELECTRICAL_TYPE, std::string> etype_map = {
371 };
372 static const std::map<MOUNT_TYPE, std::string> mtype_map = { { MOUNT_TYPE::THROUGH_HOLE, "T" },
373 { MOUNT_TYPE::HOLE, "H" },
374 { MOUNT_TYPE::SMT, "S" },
375 { MOUNT_TYPE::UNDEFINED, "U" } };
376
377 ost << "PIN " << m_name << " " << type_map.at( type ) << " " << m_center.first << " "
378 << m_center.second << " 0 " << etype_map.at( etype ) << " " << mtype_map.at( mtype )
379 << std::endl;
380
381 for( const auto& outline : m_pinOutlines )
382 {
383 outline->Write( ost );
384 }
385}
386
387
388void EDA_DATA::PACKAGE::Write( std::ostream& ost ) const
389{
390 ost << "PKG " << m_name << " " << ODB::Data2String( m_pitch ) << " "
391 << ODB::Data2String( m_xmin ) << " " << ODB::Data2String( m_ymin ) << " "
392 << ODB::Data2String( m_xmax ) << " " << ODB::Data2String( m_ymax ) << ";" << std::endl;
393
394 for( const auto& outline : m_pkgOutlines )
395 {
396 outline->Write( ost );
397 }
398
399 for( const auto& pin : m_pinsVec )
400 {
401 pin->Write( ost );
402 }
403}
404
405
406void EDA_DATA::Write( std::ostream& ost ) const
407{
408 ost << "# " << wxDateTime::Now().FormatISOCombined() << std::endl;
409 ost << "HDR KiCad EDA " << TO_UTF8( GetBuildVersion() ) << std::endl;
410 ost << "UNITS=" << PCB_IO_ODBPP::m_unitsStr << std::endl;
411 ost << "LYR";
412
413 for( const auto& layer : layers )
414 {
415 ost << " " << layer;
416 }
417
418 ost << std::endl;
419
420 WriteAttributes( ost, "#" );
421
422 for( const auto& net : nets )
423 {
424 ost << "#NET " << net->m_index << std::endl;
425 net->Write( ost );
426 }
427
428 size_t i = 0;
429 for( const auto* pkg : packages )
430 {
431 ost << "# PKG " << i << std::endl;
432 i++;
433 pkg->Write( ost );
434 ost << "#" << std::endl;
435 }
436}
const char * name
@ ERROR_INSIDE
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
wxString GetBuildVersion()
Get the full KiCad version string.
void WriteAttributes(std::ostream &ost, const std::string &prefix="") const
void WriteAttributes(std::ostream &ost) const
VECTOR2I GetFPRelativePosition() const
constexpr const Vec & GetPosition() const
Definition box2.h:211
constexpr const Vec GetEnd() const
Definition box2.h:212
void Write(std::ostream &ost) const
void Write(std::ostream &ost) const
std::list< std::unique_ptr< SUB_NET > > subnets
wxString m_name
void AddPin(const PAD *aPad, size_t aPinNum)
std::vector< std::shared_ptr< PIN > > m_pinsVec
wxString m_name
<! Reference number of the package to be used in CMP.
void Write(std::ostream &ost) const
std::list< std::unique_ptr< PKG_OUTLINE > > m_pkgOutlines
std::list< std::unique_ptr< PKG_OUTLINE > > m_pinOutlines
std::pair< wxString, wxString > m_center
void Write(std::ostream &ost) const
wxString m_name
ELECTRICAL_TYPE etype
MOUNT_TYPE mtype
void WriteSubnet(std::ostream &ost) const override
void WriteSubnet(std::ostream &ost) const override
void WriteSubnet(std::ostream &ost) const override
void WriteSubnet(std::ostream &ost) const override
std::list< FEATURE_ID > feature_ids
EDA_DATA * m_edadata
void Write(std::ostream &ost) const
void AddFeatureID(FEATURE_ID::TYPE type, const wxString &layer, size_t feature_id)
virtual void WriteSubnet(std::ostream &ost) const =0
std::list< const PACKAGE * > packages
std::map< wxString, size_t > layers_map
std::list< const NET * > nets
std::map< size_t, NET > nets_map
std::vector< wxString > layers
std::map< size_t, PACKAGE > packages_map
void AddNET(const NETINFO_ITEM *aNet)
std::vector< std::shared_ptr< FOOTPRINT > > m_eda_footprints
size_t GetLyrIdx(const wxString &aLayerName)
void AddPackage(const FOOTPRINT *aFp)
void Write(std::ostream &ost) const
EDA_ITEM * Clone() const override
Invoke a function on all children.
bool IsFlipped() const
Definition footprint.h:434
Handle the data for a net.
Definition netinfo.h:54
const wxString & GetNetname() const
Definition netinfo.h:112
int GetNetCode() const
Definition netinfo.h:106
void Write(std::ostream &ost) const override
VECTOR2I m_center
void Write(std::ostream &ost) const override
std::unique_ptr< ODB_SURFACE_DATA > m_surfaces
VECTOR2I m_lower_left
void Write(std::ostream &ost) const override
VECTOR2I m_center
void Write(std::ostream &ost) const override
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition padstack.h:145
Definition pad.h:54
PAD_ATTRIB GetAttribute() const
Definition pad.h:440
const wxString & GetNumber() const
Definition pad.h:136
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition pad.h:319
bool IsOnCopperLayer() const override
Definition pad.cpp:1083
const std::shared_ptr< SHAPE_POLY_SET > & GetEffectivePolygon(PCB_LAYER_ID aLayer, ERROR_LOC aErrorLoc=ERROR_INSIDE) const
Definition pad.cpp:525
bool HasHole() const override
Definition pad.h:106
static std::string m_unitsStr
Represent a set of closed polygons.
POLYGON & Polygon(int aIndex)
Return the aIndex-th subpolygon in the set.
int OutlineCount() const
Return the number of outlines in the set.
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
size_t hash_fp_item(const EDA_ITEM *aItem, int aFlags)
Calculate hash of an EDA_ITEM.
Definition hash_eda.cpp:57
Hashing functions for EDA_ITEMs.
@ HASH_POS
Definition hash_eda.h:47
@ REL_COORD
Use coordinates relative to the parent object.
Definition hash_eda.h:50
@ F_CrtYd
Definition layer_ids.h:116
@ B_CrtYd
Definition layer_ids.h:115
@ LEFT_RIGHT
Flip left to right (around the Y axis)
Definition mirror.h:28
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:29
wxString Data2String(double aVal)
Definition odb_util.cpp:161
void RemoveWhitespace(wxString &aStr)
Definition odb_util.cpp:119
std::pair< wxString, wxString > AddXY(const VECTOR2I &aVec)
Definition odb_util.cpp:167
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:87
@ SMD
Smd pad, appears on the solder paste layer (default)
Definition padstack.h:83
@ PTH
Plated through hole pad.
Definition padstack.h:82
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
VECTOR2< double > VECTOR2D
Definition vector2d.h:694