KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pads_unit_converter.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 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "pads_unit_converter.h"
21
22#include <cmath>
23
31
32
34{
35 m_unitType = aUnitType;
37}
38
39
41{
42 m_basicUnitsMode = aEnabled;
44}
45
46
48{
49 m_basicUnitsScale = aScale;
50
53}
54
55
56bool PADS_UNIT_CONVERTER::ParseFileHeader( const std::string& aHeader )
57{
58 if( aHeader.find( "BASIC" ) != std::string::npos ||
59 aHeader.find( "basic" ) != std::string::npos )
60 {
61 SetBasicUnitsMode( true );
62 return true;
63 }
64
65 if( aHeader.find( "MILS" ) != std::string::npos ||
66 aHeader.find( "mils" ) != std::string::npos )
67 {
68 SetBasicUnitsMode( false );
70 return true;
71 }
72
73 if( aHeader.find( "METRIC" ) != std::string::npos ||
74 aHeader.find( "metric" ) != std::string::npos )
75 {
76 SetBasicUnitsMode( false );
78 return true;
79 }
80
81 if( aHeader.find( "INCHES" ) != std::string::npos ||
82 aHeader.find( "inches" ) != std::string::npos )
83 {
84 SetBasicUnitsMode( false );
86 return true;
87 }
88
89 return false;
90}
91
92
93std::optional<PADS_UNIT_TYPE> PADS_UNIT_CONVERTER::ParseUnitCode( const std::string& aUnitCode )
94{
95 if( aUnitCode.empty() )
96 return std::nullopt;
97
98 // "D" is the PADS default code and means MILS.
99 if( aUnitCode == "M" || aUnitCode == "m" || aUnitCode == "D" || aUnitCode == "d" ||
100 aUnitCode == "MILS" || aUnitCode == "mils" || aUnitCode == "MIL" || aUnitCode == "mil" )
101 {
103 }
104
105 if( aUnitCode == "MM" || aUnitCode == "mm" ||
106 aUnitCode == "METRIC" || aUnitCode == "metric" )
107 {
109 }
110
111 if( aUnitCode == "I" || aUnitCode == "i" ||
112 aUnitCode == "INCHES" || aUnitCode == "inches" || aUnitCode == "INCH" || aUnitCode == "inch" )
113 {
115 }
116
117 // "N" means no override.
118 if( aUnitCode == "N" || aUnitCode == "n" )
119 return std::nullopt;
120
121 return std::nullopt;
122}
123
124
125bool PADS_UNIT_CONVERTER::PushUnitOverride( const std::string& aUnitCode )
126{
127 std::optional<PADS_UNIT_TYPE> unitType = ParseUnitCode( aUnitCode );
128
129 if( !unitType.has_value() )
130 return false;
131
132 m_unitOverrideStack.push_back( *unitType );
134 return true;
135}
136
137
139{
140 if( !m_unitOverrideStack.empty() )
141 {
142 m_unitOverrideStack.pop_back();
144 }
145}
146
147
149{
150 if( m_basicUnitsMode )
151 {
153 return;
154 }
155
156 PADS_UNIT_TYPE effectiveType = m_unitOverrideStack.empty()
157 ? m_unitType
158 : m_unitOverrideStack.back();
159
160 switch( effectiveType )
161 {
164 break;
165
168 break;
169
172 break;
173 }
174}
175
176
177int64_t PADS_UNIT_CONVERTER::ToNanometers( double aValue ) const
178{
179 return static_cast<int64_t>( std::round( aValue * m_scaleFactor ) );
180}
181
182
183int64_t PADS_UNIT_CONVERTER::ToNanometersSize( double aValue ) const
184{
185 return static_cast<int64_t>( std::round( aValue * m_scaleFactor ) );
186}
static constexpr double MILS_TO_NM
static constexpr double INCHES_TO_NM
std::vector< PADS_UNIT_TYPE > m_unitOverrideStack
void SetBaseUnits(PADS_UNIT_TYPE aUnitType)
bool PushUnitOverride(const std::string &aUnitCode)
Push a unit override onto the stack, temporarily overriding the base units for subsequent conversions...
static constexpr double BASIC_TO_NM
void SetBasicUnitsMode(bool aEnabled)
Enable or disable BASIC units mode.
int64_t ToNanometersSize(double aValue) const
Convert a size value (width, height, radius) in PADS file units to nanometers.
bool ParseFileHeader(const std::string &aHeader)
Parse a PADS file header string and configure units accordingly.
static std::optional< PADS_UNIT_TYPE > ParseUnitCode(const std::string &aUnitCode)
Parse a PADS unit override code ("M"/"D" mils, "MM" metric, "I" inches), returning an empty optional ...
static constexpr double MM_TO_NM
void SetBasicUnitsScale(double aScale)
Set a custom scale for BASIC units, in nanometers per BASIC unit.
void PopUnitOverride()
Pop the most recent unit override, reverting to the previous setting.
int64_t ToNanometers(double aValue) const
Convert a coordinate value in PADS file units to nanometers.
@ MILS
Thousandths of an inch.
@ METRIC
Millimeters.