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