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 (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 <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 for( PCB_FIELD* field : aFp->Fields() )
65 {
66 if( field->GetId() == REFERENCE_FIELD )
67 continue;
68
69 wxString key = field->GetName();
71 comp.m_prp[key] = wxString::Format( "'%s'", field->GetText() );
72 }
73
74 return comp;
75}
76
77
78void COMPONENTS_MANAGER::Write( std::ostream& ost ) const
79{
80 ost << "UNITS=" << PCB_IO_ODBPP::m_unitsStr << std::endl;
81
82 WriteAttributes( ost );
83
84 for( const auto& comp : m_compList )
85 {
86 comp.Write( ost );
87 }
88}
89
90
91void ODB_COMPONENT::Write( std::ostream& ost ) const
92{
93 ost << "# CMP " << m_index << std::endl;
94 ost << "CMP " << m_pkg_ref << " " << m_center.first << " " << m_center.second << " " << m_rot
95 << " " << m_mirror << " " << m_comp_name << " " << m_part_name;
96
97 WriteAttributes( ost );
98
99 ost << std::endl;
100
101 for( const auto& [key, value] : m_prp )
102 {
103 ost << "PRP " << key << " " << value << std::endl;
104 }
105
106 for( const auto& toep : m_toeprints )
107 {
108 toep.Write( ost );
109 }
110
111 ost << "#" << std::endl;
112}
113
114
115void ODB_COMPONENT::TOEPRINT::Write( std::ostream& ost ) const
116{
117 ost << "TOP " << m_pin_num << " " << m_center.first << " " << m_center.second << " " << m_rot
118 << " " << m_mirror << " " << m_net_num << " " << m_subnet_num << " " << m_toeprint_name
119 << std::endl;
120}
void WriteAttributes(std::ostream &ost, const std::string &prefix="") const
void WriteAttributes(std::ostream &ost) const
std::list< ODB_COMPONENT > m_compList
Definition: odb_component.h:44
ODB_COMPONENT & AddComponent(const FOOTPRINT *aFp, const EDA_DATA::PACKAGE &aPkg)
void Write(std::ostream &ost) const
const size_t m_index
Definition: odb_eda_data.h:232
EDA_ANGLE GetOrientation() const
Definition: footprint.h:227
bool IsFlipped() const
Definition: footprint.h:391
const LIB_ID & GetFPID() const
Definition: footprint.h:248
PCB_FIELDS & Fields()
Definition: footprint.h:203
const wxString & GetReference() const
Definition: footprint.h:622
VECTOR2I GetPosition() const override
Definition: footprint.h:224
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
const wxString GetFullLibraryName() const
Definition: lib_id.cpp:277
wxString m_mirror
Definition: odb_component.h:57
const size_t m_index
! CMP index number on board to be used in SNT(TOP), 0~n-1
Definition: odb_component.h:53
wxString m_rot
Definition: odb_component.h:56
std::list< TOEPRINT > m_toeprints
Definition: odb_component.h:93
std::pair< wxString, wxString > m_center
Definition: odb_component.h:55
size_t m_pkg_ref
! package ref number from PKG in eda/data file, 0~n-1
Definition: odb_component.h:54
wxString m_part_name
! Part identification is a single string of ASCII characters without spaces
Definition: odb_component.h:62
void Write(std::ostream &ost) const
wxString m_comp_name
! Unique reference designator (component name)
Definition: odb_component.h:59
std::map< wxString, wxString > m_prp
Definition: odb_component.h:64
static std::string m_unitsStr
Definition: pcb_io_odbpp.h:144
wxString wx_str() const
Definition: utf8.cpp:45
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:401
static constexpr EDA_ANGLE ANGLE_360
Definition: eda_angle.h:407
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
Definition: odb_component.h:85
std::pair< wxString, wxString > m_center
! Board location of the pin.
Definition: odb_component.h:76
void Write(std::ostream &ost) const
size_t m_net_num
! Number of NET record in the eda/data file.
Definition: odb_component.h:83
wxString m_mirror
! equal to CMP m_mirror.
Definition: odb_component.h:81
const size_t m_pin_num
! index of PIN record in the eda/data file, 0~n-1.
Definition: odb_component.h:74
wxString m_toeprint_name
! Name of the pad in PIN record
Definition: odb_component.h:87
wxString m_rot
! Rotation, clockwise, it equals to the actual PAD rotation, ! not CMP m_rot.
Definition: odb_component.h:78
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".