KiCad PCB EDA Suite
Loading...
Searching...
No Matches
legacy_tuning_pattern.h
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#pragma once
21
22#include <algorithm>
23#include <cmath>
24#include <iterator>
25#include <pcb_track.h>
27#include <ki_exception.h>
28#include <string_any_map.h>
29
31{
32
41
42
44{
45 const char* m_key;
47 bool m_v9;
48 bool m_v10;
49};
50
51
52// Property schemas from 9.0.0 ccafeabf1503 and 10.0.7 93a8a4827b0b.
53inline constexpr TUNING_PROPERTY TUNING_PROPERTIES[] = {
54 { "origin", TUNING_PROPERTY_TYPE::POINT, true, true },
55 { "update_order", TUNING_PROPERTY_TYPE::NUMBER, true, true },
56 { "tuning_mode", TUNING_PROPERTY_TYPE::TEXT, true, true },
57 { "initial_side", TUNING_PROPERTY_TYPE::TEXT, true, true },
58 { "last_status", TUNING_PROPERTY_TYPE::TEXT, true, true },
59 { "end", TUNING_PROPERTY_TYPE::POINT, true, true },
60 { "corner_radius_percent", TUNING_PROPERTY_TYPE::NUMBER, true, true },
61 { "single_sided", TUNING_PROPERTY_TYPE::BOOL, true, true },
62 { "rounded", TUNING_PROPERTY_TYPE::BOOL, true, true },
63 { "max_amplitude", TUNING_PROPERTY_TYPE::NUMBER, true, true },
64 { "min_amplitude", TUNING_PROPERTY_TYPE::NUMBER, true, true },
65 { "min_spacing", TUNING_PROPERTY_TYPE::NUMBER, true, true },
66 { "target_length_min", TUNING_PROPERTY_TYPE::NUMBER, true, true },
67 { "target_length", TUNING_PROPERTY_TYPE::NUMBER, true, true },
68 { "target_length_max", TUNING_PROPERTY_TYPE::NUMBER, true, true },
69 { "target_skew_min", TUNING_PROPERTY_TYPE::NUMBER, true, true },
70 { "target_skew", TUNING_PROPERTY_TYPE::NUMBER, true, true },
71 { "target_skew_max", TUNING_PROPERTY_TYPE::NUMBER, true, true },
72 { "last_track_width", TUNING_PROPERTY_TYPE::NUMBER, true, true },
73 { "last_diff_pair_gap", TUNING_PROPERTY_TYPE::NUMBER, true, true },
74 { "last_netname", TUNING_PROPERTY_TYPE::TEXT, true, true },
75 { "last_tuning", TUNING_PROPERTY_TYPE::TEXT, true, false },
76 { "override_custom_rules", TUNING_PROPERTY_TYPE::BOOL, true, true },
77 { "base_line", TUNING_PROPERTY_TYPE::LINE_CHAIN, true, true },
78 { "base_line_coupled", TUNING_PROPERTY_TYPE::LINE_CHAIN, true, true },
79 { "is_time_domain", TUNING_PROPERTY_TYPE::BOOL, false, true },
80 { "target_delay_min", TUNING_PROPERTY_TYPE::NUMBER, false, true },
81 { "target_delay", TUNING_PROPERTY_TYPE::NUMBER, false, true },
82 { "target_delay_max", TUNING_PROPERTY_TYPE::NUMBER, false, true },
83 { "last_tuning_length", TUNING_PROPERTY_TYPE::NUMBER, false, true },
84};
85
86
87inline bool IsReviewedTuningValue( const wxAny& aValue, TUNING_PROPERTY_TYPE aType )
88{
89 switch( aType )
90 {
92 {
93 double value = 0;
94 return ( aValue.CheckType<double>() || aValue.CheckType<int>() || aValue.CheckType<long>()
95 || aValue.CheckType<long long>() )
96 && aValue.GetAs( &value ) && std::isfinite( value );
97 }
98 case TUNING_PROPERTY_TYPE::BOOL: return aValue.CheckType<bool>();
99 case TUNING_PROPERTY_TYPE::TEXT: return aValue.CheckType<wxString>() || aValue.CheckType<std::string>();
100 case TUNING_PROPERTY_TYPE::POINT: return aValue.CheckType<VECTOR2I>();
101 case TUNING_PROPERTY_TYPE::LINE_CHAIN: return aValue.CheckType<SHAPE_LINE_CHAIN>();
102 }
103
104 return false;
105}
106
107
108inline bool AreTuningPropertiesReviewed( const STRING_ANY_MAP& aProperties )
109{
110 for( const auto& [key, value] : aProperties )
111 {
112 const auto schema = std::find_if( std::begin( TUNING_PROPERTIES ), std::end( TUNING_PROPERTIES ),
113 [&]( const TUNING_PROPERTY& aProperty )
114 {
115 return aProperty.m_v10 && key == aProperty.m_key;
116 } );
117
118 if( schema == std::end( TUNING_PROPERTIES ) || !IsReviewedTuningValue( value, schema->m_type ) )
119 return false;
120 }
121
122 return true;
123}
124
125
126inline STRING_ANY_MAP TuningPropertiesForEra( const PCB_GENERATOR& aGenerator, bool aV9 )
127{
128 const auto pattern = dynamic_cast<const PCB_TUNING_PATTERN*>( &aGenerator );
129
130 if( !pattern )
131 THROW_IO_ERROR( wxT( "The target format does not support this generator kind." ) );
132
133 const STRING_ANY_MAP current = pattern->GetProperties();
134
135 if( !AreTuningPropertiesReviewed( current ) )
136 THROW_IO_ERROR( wxT( "The tuning generator contains unreviewed properties." ) );
137
138 if( aV9 && pattern->GetSettings().m_isTimeDomain )
139 THROW_IO_ERROR( wxT( "Time-domain tuning editability must be removed before writing KiCad 9." ) );
140
142
143 for( const TUNING_PROPERTY& property : TUNING_PROPERTIES )
144 {
145 if( !( aV9 ? property.m_v9 : property.m_v10 ) )
146 continue;
147
148 if( aV9 && std::string( property.m_key ) == "last_tuning" )
149 {
150 result.set( property.m_key, pattern->GetTuningInfo() );
151 }
152 else if( const auto value = current.find( property.m_key ); value != current.end() )
153 {
154 result.emplace( value->first, value->second );
155 }
156 }
157
158 return result;
159}
160
161} // namespace KICAD_FORMAT::LEGACY
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
A name/value tuple with unique names and wxAny values.
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
constexpr TUNING_PROPERTY TUNING_PROPERTIES[]
bool IsReviewedTuningValue(const wxAny &aValue, TUNING_PROPERTY_TYPE aType)
bool AreTuningPropertiesReviewed(const STRING_ANY_MAP &aProperties)
STRING_ANY_MAP TuningPropertiesForEra(const PCB_GENERATOR &aGenerator, bool aV9)
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708