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
36 if( aFp->GetOrientation() != ANGLE_0 )
37 {
38 // odb Rotation is expressed in degrees and is always clockwise.
39 // while kicad EDA_ANGLE is anticlockwise.
40
41 comp.m_rot =
42 ODB::Double2String( ( ANGLE_360 - aFp->GetOrientation() ).Normalize().AsDegrees() );
43 }
44
45 if( aFp->GetLayer() != F_Cu )
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 comp.m_comp_name.Trim().Trim( false );
56 comp.m_part_name.Trim().Trim( false );
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 wxRegEx spaces( "\\s" );
65 spaces.Replace( &comp.m_comp_name, "_" );
66 spaces.Replace( &comp.m_part_name, "_" );
67
68 return comp;
69}
70
71
72void COMPONENTS_MANAGER::Write( std::ostream& ost ) const
73{
74 ost << "UNITS=" << PCB_IO_ODBPP::m_unitsStr << std::endl;
75
76 WriteAttributes( ost );
77
78 for( const auto& comp : m_compList )
79 {
80 comp.Write( ost );
81 }
82}
83
84
85void ODB_COMPONENT::Write( std::ostream& ost ) const
86{
87 ost << "# CMP " << m_index << std::endl;
88 ost << "CMP " << m_pkg_ref << " " << m_center.first << " " << m_center.second << " " << m_rot
89 << " " << m_mirror << " " << m_comp_name << " " << m_part_name;
90
91 WriteAttributes( ost );
92
93 ost << std::endl;
94
95 for( const auto& toep : m_toeprints )
96 {
97 toep.Write( ost );
98 }
99
100 ost << "#" << std::endl;
101}
102
103
104void ODB_COMPONENT::TOEPRINT::Write( std::ostream& ost ) const
105{
106 ost << "TOP " << m_pin_num << " " << m_center.first << " " << m_center.second << " " << m_rot
107 << " " << m_mirror << " " << m_net_num << " " << m_subnet_num << " " << m_toeprint_name
108 << std::endl;
109}
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
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition: footprint.h:236
const LIB_ID & GetFPID() const
Definition: footprint.h:248
const wxString & GetReference() const
Definition: footprint.h:602
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
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
@ F_Cu
Definition: layer_ids.h:64
std::pair< wxString, wxString > AddXY(const VECTOR2I &aVec)
Definition: odb_util.cpp:158
wxString Double2String(double aVal)
Definition: odb_util.cpp:118
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