KiCad PCB EDA Suite
Loading...
Searching...
No Matches
odb_component.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 <wx/regex.h>
22
23#include "odb_component.h"
24#include "odb_util.h"
25#include "hash_eda.h"
26#include "pcb_io_odbpp.h"
27
28
30 const EDA_DATA::PACKAGE& aPkg )
31{
32 auto& comp = m_compList.emplace_back( m_compList.size(), aPkg.m_index );
33
34 comp.m_center = ODB::AddXY( aFp->GetPosition() );
35 EDA_ANGLE angle = aFp->GetOrientation();
36
37 if( angle != ANGLE_0 )
38 {
39 // odb Rotation is expressed in degrees and is always clockwise.
40 // while kicad EDA_ANGLE is anticlockwise.
41 angle = ANGLE_360 - angle;
42 comp.m_rot = ODB::Double2String( angle.Normalize().AsDegrees() );
43 }
44
45 if( aFp->IsFlipped() )
46 {
47 comp.m_mirror = wxT( "M" );
48 }
49
50 comp.m_comp_name = aFp->GetReference().ToAscii();
51 comp.m_part_name = wxString::Format( "%s_%s", aFp->GetFPID().GetFullLibraryName(),
52 aFp->GetFPID().GetLibItemName().wx_str() );
53
54 // ODB++ cannot handle spaces in these fields
55 ODB::RemoveWhitespace( comp.m_comp_name );
56 ODB::RemoveWhitespace( comp.m_part_name );
57
58 if( comp.m_comp_name.IsEmpty() )
59 {
60 // The spec requires a component name; some ODB++ parsers can't handle it being empty
61 comp.m_comp_name = wxString::Format( "UNNAMED%zu", m_compList.size() );
62 }
63
64 wxString base_comp_name = comp.m_comp_name;
65
66 if( !m_usedCompNames.insert( comp.m_comp_name ).second )
67 {
68 size_t suffix = 1;
69 wxString candidate;
70
71 do
72 {
73 candidate = wxString::Format( "%s_%zu", base_comp_name, suffix++ );
74 } while( !m_usedCompNames.insert( candidate ).second );
75
76 comp.m_comp_name = candidate;
77 }
78
79 for( PCB_FIELD* field : aFp->GetFields() )
80 {
81 if( field->GetId() == FIELD_T::REFERENCE )
82 continue;
83
84 wxString key = field->GetName();
86 comp.m_prp[key] = wxString::Format( "'%s'", field->GetText() );
87 }
88
89 if( aFp->IsDNP() )
90 {
91 AddSystemAttribute( comp, ODB_ATTR::NO_POP{ true } );
92 }
93
94 if( aFp->GetAttributes() & FP_SMD )
95 {
97 }
98 else if( aFp->GetAttributes() & FP_THROUGH_HOLE )
99 {
101 }
102 else
103 {
105 }
106
107 return comp;
108}
109
110
111void COMPONENTS_MANAGER::Write( std::ostream& ost ) const
112{
113 ost << "UNITS=" << PCB_IO_ODBPP::m_unitsStr << std::endl;
114
115 WriteAttributes( ost );
116
117 for( const auto& comp : m_compList )
118 {
119 comp.Write( ost );
120 }
121}
122
123
124void ODB_COMPONENT::Write( std::ostream& ost ) const
125{
126 ost << "# CMP " << m_index << std::endl;
127 ost << "CMP " << m_pkg_ref << " " << m_center.first << " " << m_center.second << " " << m_rot
128 << " " << m_mirror << " " << m_comp_name << " " << m_part_name;
129
130 WriteAttributes( ost );
131
132 ost << std::endl;
133
134 for( const auto& [key, value] : m_prp )
135 {
136 ost << "PRP " << key << " " << value << std::endl;
137 }
138
139 for( const auto& toep : m_toeprints )
140 {
141 toep.Write( ost );
142 }
143
144 ost << "#" << std::endl;
145}
146
147
148void ODB_COMPONENT::TOEPRINT::Write( std::ostream& ost ) const
149{
150 ost << "TOP " << m_pin_num << " " << m_center.first << " " << m_center.second << " " << m_rot
151 << " " << m_mirror << " " << m_net_num << " " << m_subnet_num << " " << m_toeprint_name
152 << std::endl;
153}
void WriteAttributes(std::ostream &ost, const std::string &prefix="") const
void AddSystemAttribute(Tr &r, Ta v)
void WriteAttributes(std::ostream &ost) const
std::list< ODB_COMPONENT > m_compList
ODB_COMPONENT & AddComponent(const FOOTPRINT *aFp, const EDA_DATA::PACKAGE &aPkg)
void Write(std::ostream &ost) const
std::set< wxString > m_usedCompNames
const size_t m_index
bool IsDNP() const
Definition footprint.h:789
EDA_ANGLE GetOrientation() const
Definition footprint.h:248
int GetAttributes() const
Definition footprint.h:327
bool IsFlipped() const
Definition footprint.h:434
const LIB_ID & GetFPID() const
Definition footprint.h:269
void GetFields(std::vector< PCB_FIELD * > &aVector, bool aVisibleOnly) const
Populate a std::vector with PCB_TEXTs.
const wxString & GetReference() const
Definition footprint.h:661
VECTOR2I GetPosition() const override
Definition footprint.h:245
const UTF8 & GetLibItemName() const
Definition lib_id.h:102
const wxString GetFullLibraryName() const
Definition lib_id.cpp:275
wxString m_mirror
const size_t m_index
! CMP index number on board to be used in SNT(TOP), 0~n-1
std::list< TOEPRINT > m_toeprints
std::pair< wxString, wxString > m_center
size_t m_pkg_ref
! package ref number from PKG in eda/data file, 0~n-1
wxString m_part_name
! Part identification is a single string of ASCII characters without spaces
void Write(std::ostream &ost) const
wxString m_comp_name
! Unique reference designator (component name)
std::map< wxString, wxString > m_prp
static std::string m_unitsStr
wxString wx_str() const
Definition utf8.cpp:45
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
static constexpr EDA_ANGLE ANGLE_360
Definition eda_angle.h:417
@ FP_SMD
Definition footprint.h:82
@ FP_THROUGH_HOLE
Definition footprint.h:81
Hashing functions for EDA_ITEMs.
void RemoveWhitespace(wxString &aStr)
Definition odb_util.cpp:119
std::pair< wxString, wxString > AddXY(const VECTOR2I &aVec)
Definition odb_util.cpp:167
wxString Double2String(double aVal)
Definition odb_util.cpp:127
size_t m_subnet_num
! Number of subnet (SNT record TOP) in the referenced net
std::pair< wxString, wxString > m_center
! Board location of the pin.
void Write(std::ostream &ost) const
size_t m_net_num
! Number of NET record in the eda/data file.
wxString m_mirror
! equal to CMP m_mirror.
const size_t m_pin_num
! index of PIN record in the eda/data file, 0~n-1.
wxString m_toeprint_name
! Name of the pad in PIN record
@ REFERENCE
Field Reference of part, i.e. "IC21".