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#include <wx/log.h>
23
24#include <board.h>
25
26#include "odb_component.h"
27#include "odb_util.h"
28#include "hash_eda.h"
29#include "pcb_io_odbpp.h"
30
31
33 const EDA_DATA::PACKAGE& aPkg )
34{
35 auto& comp = m_compList.emplace_back( m_compList.size(), aPkg.m_index );
36
37 comp.m_center = ODB::AddXY( aFp->GetPosition() );
38 EDA_ANGLE angle = aFp->GetOrientation();
39
40 if( angle != ANGLE_0 )
41 {
42 // odb Rotation is expressed in degrees and is always clockwise.
43 // while kicad EDA_ANGLE is anticlockwise.
44 angle = ANGLE_360 - angle;
45 comp.m_rot = ODB::Double2String( angle.Normalize().AsDegrees() );
46 }
47
48 if( aFp->IsFlipped() )
49 {
50 comp.m_mirror = wxT( "M" );
51 }
52
53 wxString originalRef = aFp->GetReference();
54 comp.m_comp_name = ODB::GenLegalComponentName( originalRef );
55
56 comp.m_part_name = wxString::Format( "%s_%s", aFp->GetFPID().GetFullLibraryName(),
57 aFp->GetFPID().GetLibItemName().wx_str() );
58
59 // ODB++ cannot handle spaces in these fields
60 ODB::RemoveWhitespace( comp.m_part_name );
61
62 if( comp.m_comp_name.IsEmpty() )
63 {
64 // The spec requires a component name; some ODB++ parsers can't handle it being empty
65 comp.m_comp_name = wxString::Format( "UNNAMED%zu", m_compList.size() );
66 }
67
68 // Warn if non-ASCII characters were converted
69 if( comp.m_comp_name != originalRef )
70 {
71 wxLogWarning( _( "Component '%s' has non-ASCII characters in its designator; "
72 "converted to '%s' for ODB++ export." ),
73 originalRef, comp.m_comp_name );
74 }
75
76 wxString base_comp_name = comp.m_comp_name;
77
78 if( !m_usedCompNames.insert( comp.m_comp_name ).second )
79 {
80 size_t suffix = 1;
81 wxString candidate;
82
83 do
84 {
85 candidate = wxString::Format( "%s_%zu", base_comp_name, suffix++ );
86 } while( !m_usedCompNames.insert( candidate ).second );
87
88 wxLogWarning( _( "Component '%s' has an ambiguous designator after conversion; "
89 "renamed to '%s' for ODB++ export." ),
90 originalRef, candidate );
91
92 comp.m_comp_name = candidate;
93 }
94
95 for( PCB_FIELD* field : aFp->GetFields() )
96 {
97 if( field->GetId() == FIELD_T::REFERENCE )
98 continue;
99
100 wxString key = field->GetName();
102 comp.m_prp[key] = wxString::Format( "'%s'", field->GetShownText( false ) );
103 }
104
105 if( aFp->GetDNPForVariant( aFp->GetBoard() ? aFp->GetBoard()->GetCurrentVariant() : wxString() ) )
106 {
107 AddSystemAttribute( comp, ODB_ATTR::NO_POP{ true } );
108 }
109
110 if( aFp->GetAttributes() & FP_SMD )
111 {
113 }
114 else if( aFp->GetAttributes() & FP_THROUGH_HOLE )
115 {
117 }
118 else
119 {
121 }
122
123 return comp;
124}
125
126
127void COMPONENTS_MANAGER::Write( std::ostream& ost ) const
128{
129 ost << "UNITS=" << PCB_IO_ODBPP::m_unitsStr << std::endl;
130
131 WriteAttributes( ost );
132
133 for( const auto& comp : m_compList )
134 {
135 comp.Write( ost );
136 }
137}
138
139
140void ODB_COMPONENT::Write( std::ostream& ost ) const
141{
142 ost << "# CMP " << m_index << std::endl;
143 ost << "CMP " << m_pkg_ref << " " << m_center.first << " " << m_center.second << " " << m_rot
144 << " " << m_mirror << " " << m_comp_name << " " << m_part_name;
145
146 WriteAttributes( ost );
147
148 ost << std::endl;
149
150 for( const auto& [key, value] : m_prp )
151 {
152 ost << "PRP " << key << " " << value << std::endl;
153 }
154
155 for( const auto& toep : m_toeprints )
156 {
157 toep.Write( ost );
158 }
159
160 ost << "#" << std::endl;
161}
162
163
164void ODB_COMPONENT::TOEPRINT::Write( std::ostream& ost ) const
165{
166 ost << "TOP " << m_pin_num << " " << m_center.first << " " << m_center.second << " " << m_rot
167 << " " << m_mirror << " " << m_net_num << " " << m_subnet_num << " " << m_toeprint_name
168 << std::endl;
169}
void WriteAttributes(std::ostream &ost, const std::string &prefix="") const
void AddSystemAttribute(Tr &r, Ta v)
void WriteAttributes(std::ostream &ost) const
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
wxString GetCurrentVariant() const
Definition board.h:404
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
EDA_ANGLE GetOrientation() const
Definition footprint.h:328
int GetAttributes() const
Definition footprint.h:407
bool IsFlipped() const
Definition footprint.h:514
const LIB_ID & GetFPID() const
Definition footprint.h:349
bool GetDNPForVariant(const wxString &aVariantName) const
Get the DNP status for a specific variant.
void GetFields(std::vector< PCB_FIELD * > &aVector, bool aVisibleOnly) const
Populate a std::vector with PCB_TEXTs.
const wxString & GetReference() const
Definition footprint.h:741
VECTOR2I GetPosition() const override
Definition footprint.h:325
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
#define _(s)
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:83
@ FP_THROUGH_HOLE
Definition footprint.h:82
Hashing functions for EDA_ITEMs.
wxString GenLegalComponentName(const wxString &aStr)
Definition odb_util.cpp:79
void RemoveWhitespace(wxString &aStr)
Definition odb_util.cpp:143
std::pair< wxString, wxString > AddXY(const VECTOR2I &aVec)
Definition odb_util.cpp:191
wxString Double2String(double aVal)
Definition odb_util.cpp:151
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".
KIBIS_COMPONENT * comp