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 (C) 2024 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 <build_version.h>
22
23#include "odb_eda_data.h"
24#include "hash_eda.h"
25#include "netinfo.h"
26#include "odb_feature.h"
27#include "base_units.h"
28#include "pcb_io_odbpp.h"
29
30
32{
33 auto& x = nets_map.emplace( std::piecewise_construct, std::forward_as_tuple( 0 ),
34 std::forward_as_tuple( nets.size(), "$NONE$" ) )
35 .first->second;
36
37 nets.push_back( &x );
38}
39
40
41void EDA_DATA::NET::Write( std::ostream& ost ) const
42{
43 ost << "NET " << m_name;
44
45 WriteAttributes( ost );
46
47 ost << std::endl;
48
49 for( const auto& subnet : subnets )
50 {
51 subnet->Write( ost );
52 }
53}
54
55
56void EDA_DATA::AddNET( const NETINFO_ITEM* aNet )
57{
58 if( nets_map.end() == nets_map.find( aNet->GetNetCode() ) )
59 {
60 wxString netName = aNet->GetNetname();
61 ODB::RemoveWhitespace( netName );
62
63 auto& net = nets_map.emplace( std::piecewise_construct,
64 std::forward_as_tuple( aNet->GetNetCode() ),
65 std::forward_as_tuple( nets.size(), netName ) )
66 .first->second;
67
68 nets.push_back( &net );
69
70 //TODO: netname check
71 }
72}
73
74
75void EDA_DATA::SUB_NET::Write( std::ostream& ost ) const
76{
77 ost << "SNT ";
78
79 WriteSubnet( ost );
80
81 ost << std::endl;
82
83 for( const auto& fid : feature_ids )
84 {
85 fid.Write( ost );
86 }
87}
88
89
90void EDA_DATA::FEATURE_ID::Write( std::ostream& ost ) const
91{
92 static const std::map<TYPE, std::string> type_map = {
93 { TYPE::COPPER, "C" },
94 { TYPE::HOLE, "H" },
95 };
96
97 ost << "FID " << type_map.at( type ) << " " << layer << " " << feature_id << std::endl;
98}
99
100
101void EDA_DATA::SUB_NET_VIA::WriteSubnet( std::ostream& ost ) const
102{
103 ost << "VIA";
104}
105
106
107void EDA_DATA::SUB_NET_TRACE::WriteSubnet( std::ostream& ost ) const
108{
109 ost << "TRC";
110}
111
112
113void EDA_DATA::SUB_NET_PLANE::WriteSubnet( std::ostream& ost ) const
114{
115 static const std::map<FILL_TYPE, std::string> fill_type_map = { { FILL_TYPE::SOLID, "S" },
116 { FILL_TYPE::OUTLINE, "O" } };
117
118 static const std::map<CUTOUT_TYPE, std::string> cutout_type_map = {
119 { CUTOUT_TYPE::CIRCLE, "C" },
120 { CUTOUT_TYPE::RECT, "R" },
121 { CUTOUT_TYPE::OCTAGON, "O" },
122 { CUTOUT_TYPE::EXACT, "E" }
123 };
124
125 ost << "PLN " << fill_type_map.at( fill_type ) << " " << cutout_type_map.at( cutout_type )
126 << " " << fill_size;
127}
128
129
130void EDA_DATA::SUB_NET_TOEPRINT::WriteSubnet( std::ostream& ost ) const
131{
132 static const std::map<SIDE, std::string> side_map = {
133 { SIDE::BOTTOM, "B" },
134 { SIDE::TOP, "T" },
135 };
136 ost << "TOP " << side_map.at( side ) << " " << comp_num << " " << toep_num;
137}
138
139
140void EDA_DATA::SUB_NET::AddFeatureID( FEATURE_ID::TYPE type, const wxString& layer,
141 size_t feature_id )
142{
143 feature_ids.emplace_back( type, m_edadata->GetLyrIdx( layer ), feature_id );
144}
145
146
147size_t EDA_DATA::GetLyrIdx( const wxString& aLayer )
148{
149 if( layers_map.count( aLayer ) )
150 {
151 return layers_map.at( aLayer );
152 }
153 else
154 {
155 auto idx = layers_map.size();
156 layers_map.emplace( aLayer, idx );
157 layers.push_back( aLayer );
158 return idx;
159 }
160}
161
162
163void OUTLINE_SQUARE::Write( std::ostream& ost ) const
164{
165 ost << "SQ " << ODB::Data2String( m_center.x ) << " " << ODB::Data2String( m_center.y ) << " "
166 << ODB::Data2String( m_halfSide ) << std::endl;
167}
168
169
170void OUTLINE_CIRCLE::Write( std::ostream& ost ) const
171{
172 ost << "CR " << ODB::Data2String( m_center.x ) << " " << ODB::Data2String( m_center.y ) << " "
173 << ODB::Data2String( m_radius ) << std::endl;
174}
175
176
177void OUTLINE_RECT::Write( std::ostream& ost ) const
178{
179 ost << "RC " << ODB::Data2String( m_lower_left.x ) << " " << ODB::Data2String( m_lower_left.y )
180 << " " << ODB::Data2String( m_width ) << " " << ODB::Data2String( m_height ) << std::endl;
181}
182
183
184void OUTLINE_CONTOUR::Write( std::ostream& ost ) const
185{
186 ost << "CT" << std::endl;
187 m_surfaces->WriteData( ost );
188 ost << "CE" << std::endl;
189}
190
191
193{
194 // ODBPP only need unique PACKAGE in PKG record in eda/data file.
195 // the PKG index can repeat to be ref in CMP record in component file.
196
197 std::shared_ptr<FOOTPRINT> fp( static_cast<FOOTPRINT*>( aFp->Clone() ) );
198 m_eda_footprints.emplace_back( fp );
199 fp->SetParentGroup( nullptr );
200 fp->SetPosition( { 0, 0 } );
201
202 if( aFp->IsFlipped() )
203 {
204 // ODB++ needs both flips
205 fp->Flip( fp->GetPosition(), FLIP_DIRECTION::LEFT_RIGHT );
206 fp->Flip( fp->GetPosition(), FLIP_DIRECTION::TOP_BOTTOM );
207 }
208
209 fp->SetOrientation( ANGLE_0 );
210
211 size_t hash = hash_fp_item( fp.get(), HASH_POS | REL_COORD );
212 size_t pkg_index = packages_map.size();
213 wxString fp_name = fp->GetFPID().GetLibItemName().wx_str();
214 ODB::RemoveWhitespace( fp_name );
215
216 auto [iter, success] = packages_map.emplace( hash, PACKAGE( pkg_index, fp_name ) );
217
218 if( !success )
219 {
220 return;
221 }
222
223 PACKAGE* pkg = &( iter->second );
224
225 packages.push_back( pkg );
226
227 BOX2I bbox = fp->GetBoundingBox();
228 pkg->m_xmin = bbox.GetPosition().x;
229 pkg->m_ymin = bbox.GetPosition().y;
230 pkg->m_xmax = bbox.GetEnd().x;
231 pkg->m_ymax = bbox.GetEnd().y;
232 pkg->m_pitch = UINT64_MAX;
233
234 if( fp->Pads().size() < 2 )
235 pkg->m_pitch = pcbIUScale.mmToIU( 1.0 ); // placeholder value
236
237 for( size_t i = 0; i < fp->Pads().size(); ++i )
238 {
239 const PAD* pad1 = fp->Pads()[i];
240
241 for( size_t j = i + 1; j < fp->Pads().size(); ++j )
242 {
243 const PAD* pad2 = fp->Pads()[j];
244 const uint64_t pin_dist = ( pad1->GetCenter() - pad2->GetCenter() ).EuclideanNorm();
245 pkg->m_pitch = std::min( pkg->m_pitch, pin_dist );
246 }
247 }
248
249 const SHAPE_POLY_SET& courtyard = fp->GetCourtyard( F_CrtYd );
250 const SHAPE_POLY_SET& courtyard_back = fp->GetCourtyard( B_CrtYd );
251 SHAPE_POLY_SET pkg_outline;
252
253 if( courtyard.OutlineCount() > 0 )
254 pkg_outline = courtyard;
255
256 if( courtyard_back.OutlineCount() > 0 )
257 {
258 pkg_outline = courtyard_back;
259 }
260
261 if( !courtyard.OutlineCount() && !courtyard_back.OutlineCount() )
262 {
263 pkg_outline = fp->GetBoundingHull();
264 }
265
266 // TODO: Here we put rect, square, and circle, all as polygon
267
268 if( pkg_outline.OutlineCount() > 0 )
269 {
270 for( int ii = 0; ii < pkg_outline.OutlineCount(); ++ii )
271 {
272 pkg->m_pkgOutlines.push_back(
273 std::make_unique<OUTLINE_CONTOUR>( pkg_outline.Polygon( ii ) ) );
274 }
275 }
276
277 for( size_t i = 0; i < fp->Pads().size(); ++i )
278 {
279 const PAD* pad = fp->Pads()[i];
280 pkg->AddPin( pad, i );
281 }
282
283 return;
284}
285
286
287void EDA_DATA::PACKAGE::AddPin( const PAD* aPad, size_t aPinNum )
288{
289 wxString name = aPad->GetNumber();
290
291 // ODB is unhappy with whitespace in most places
293
294 // Pins are required to have names, so if our pad doesn't have a name, we need to
295 // generate one that is unique
296
297 if( aPad->GetAttribute() == PAD_ATTRIB::NPTH )
298 name = wxString::Format( "NPTH%zu", aPinNum );
299 else if( name.empty() )
300 name = wxString::Format( "PAD%zu", aPinNum );
301
302 // // for SNT record, pad, net, pin
303 std::shared_ptr<PIN> pin = std::make_shared<PIN>( m_pinsVec.size(), name );
304 m_pinsVec.push_back( pin );
305
306 VECTOR2D relpos = aPad->GetFPRelativePosition();
307
308 pin->m_center = ODB::AddXY( relpos );
309
310 if( aPad->HasHole() )
311 {
312 pin->type = PIN::TYPE::THROUGH_HOLE;
313 }
314 else
315 {
316 pin->type = PIN::TYPE::SURFACE;
317 }
318
319 if( aPad->GetAttribute() == PAD_ATTRIB::NPTH )
321 else if( aPad->IsOnCopperLayer() )
323 else
325
326
327 if( ( aPad->HasHole() && aPad->IsOnCopperLayer() ) || aPad->GetAttribute() == PAD_ATTRIB::PTH )
328 {
330 }
331 else if( aPad->HasHole() && aPad->GetAttribute() == PAD_ATTRIB::NPTH )
332 {
333 pin->mtype = PIN::MOUNT_TYPE::HOLE;
334 }
335 else if( aPad->GetAttribute() == PAD_ATTRIB::SMD )
336 {
337 pin->mtype = PIN::MOUNT_TYPE::SMT;
338 }
339 else
340 {
342 }
343
344 const std::shared_ptr<SHAPE_POLY_SET>& polygons =
346
347 // TODO: Here we put all pad shapes as polygonl, we should switch by pad shape
348 // Note:pad only use polygons->Polygon(0),
349 if( polygons->OutlineCount() > 0 )
350 {
351 pin->m_pinOutlines.push_back( std::make_unique<OUTLINE_CONTOUR>( polygons->Polygon( 0 ) ) );
352 }
353}
354
355
356void EDA_DATA::PIN::Write( std::ostream& ost ) const
357{
358 static const std::map<TYPE, std::string> type_map = { { TYPE::SURFACE, "S" },
359 { TYPE::THROUGH_HOLE, "T" },
360 { TYPE::BLIND, "B" } };
361
362 static const std::map<ELECTRICAL_TYPE, std::string> etype_map = {
363 { ELECTRICAL_TYPE::ELECTRICAL, "E" },
364 { ELECTRICAL_TYPE::MECHANICAL, "M" },
365 { ELECTRICAL_TYPE::UNDEFINED, "U" }
366 };
367 static const std::map<MOUNT_TYPE, std::string> mtype_map = { { MOUNT_TYPE::THROUGH_HOLE, "T" },
368 { MOUNT_TYPE::HOLE, "H" },
369 { MOUNT_TYPE::SMT, "S" },
370 { MOUNT_TYPE::UNDEFINED, "U" } };
371
372 ost << "PIN " << m_name << " " << type_map.at( type ) << " " << m_center.first << " "
373 << m_center.second << " 0 " << etype_map.at( etype ) << " " << mtype_map.at( mtype )
374 << std::endl;
375
376 for( const auto& outline : m_pinOutlines )
377 {
378 outline->Write( ost );
379 }
380}
381
382
383void EDA_DATA::PACKAGE::Write( std::ostream& ost ) const
384{
385 ost << "PKG " << m_name << " " << ODB::Data2String( m_pitch ) << " "
386 << ODB::Data2String( m_xmin ) << " " << ODB::Data2String( m_ymin ) << " "
387 << ODB::Data2String( m_xmax ) << " " << ODB::Data2String( m_ymax ) << ";" << std::endl;
388
389 for( const auto& outline : m_pkgOutlines )
390 {
391 outline->Write( ost );
392 }
393
394 for( const auto& pin : m_pinsVec )
395 {
396 pin->Write( ost );
397 }
398}
399
400
401void EDA_DATA::Write( std::ostream& ost ) const
402{
403 ost << "# " << wxDateTime::Now().FormatISOCombined() << std::endl;
404 ost << "HDR KiCad EDA " << TO_UTF8( GetBuildVersion() ) << std::endl;
405 ost << "UNITS=" << PCB_IO_ODBPP::m_unitsStr << std::endl;
406 ost << "LYR";
407
408 for( const auto& layer : layers )
409 {
410 ost << " " << layer;
411 }
412
413 ost << std::endl;
414
415 WriteAttributes( ost, "#" );
416
417 for( const auto& net : nets )
418 {
419 ost << "#NET " << net->m_index << std::endl;
420 net->Write( ost );
421 }
422
423 size_t i = 0;
424 for( const auto* pkg : packages )
425 {
426 ost << "# PKG " << i << std::endl;
427 i++;
428 pkg->Write( ost );
429 ost << "#" << std::endl;
430 }
431}
const char * name
Definition: DXF_plotter.cpp:57
@ ERROR_INSIDE
Definition: approximation.h:34
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
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
Definition: board_item.cpp:327
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
Definition: odb_eda_data.h:157
wxString m_name
Definition: odb_eda_data.h:156
void AddPin(const PAD *aPad, size_t aPinNum)
void Write(std::ostream &ost) const
std::list< std::unique_ptr< PKG_OUTLINE > > m_pkgOutlines
Definition: odb_eda_data.h:238
void Write(std::ostream &ost) const
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
void Write(std::ostream &ost) const
void AddFeatureID(FEATURE_ID::TYPE type, const wxString &layer, size_t feature_id)
std::list< const PACKAGE * > packages
Definition: odb_eda_data.h:260
std::map< wxString, size_t > layers_map
Definition: odb_eda_data.h:262
std::list< const NET * > nets
Definition: odb_eda_data.h:257
std::map< size_t, NET > nets_map
Definition: odb_eda_data.h:256
std::vector< wxString > layers
Definition: odb_eda_data.h:263
std::map< size_t, PACKAGE > packages_map
Definition: odb_eda_data.h:259
void AddNET(const NETINFO_ITEM *aNet)
std::vector< std::shared_ptr< FOOTPRINT > > m_eda_footprints
Definition: odb_eda_data.h:264
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.
Definition: footprint.cpp:2075
bool IsFlipped() const
Definition: footprint.h:391
Handle the data for a net.
Definition: netinfo.h:56
const wxString & GetNetname() const
Definition: netinfo.h:114
int GetNetCode() const
Definition: netinfo.h:108
void Write(std::ostream &ost) const override
void Write(std::ostream &ost) const override
void Write(std::ostream &ost) const override
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:144
Definition: pad.h:54
PAD_ATTRIB GetAttribute() const
Definition: pad.h:442
const wxString & GetNumber() const
Definition: pad.h:134
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pad.h:321
bool IsOnCopperLayer() const override
Definition: pad.cpp:988
const std::shared_ptr< SHAPE_POLY_SET > & GetEffectivePolygon(PCB_LAYER_ID aLayer, ERROR_LOC aErrorLoc=ERROR_INSIDE) const
Definition: pad.cpp:476
bool HasHole() const override
Definition: pad.h:104
static std::string m_unitsStr
Definition: pcb_io_odbpp.h:144
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:401
size_t hash_fp_item(const EDA_ITEM *aItem, int aFlags)
Calculate hash of an EDA_ITEM.
Definition: hash_eda.cpp:55
@ HASH_POS
use coordinates relative to the parent object
Definition: hash_eda.h:43
@ REL_COORD
use coordinates relative to the shape position
Definition: hash_eda.h:46
@ F_CrtYd
Definition: layer_ids.h:116
@ B_CrtYd
Definition: layer_ids.h:115
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
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:398
constexpr int mmToIU(double mm) const
Definition: base_units.h:88