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