24#include <nlohmann/json.hpp>
42struct LEGACY_DRC_ERROR
44 const char* m_protoName;
45 const char* m_settingsKey;
46 bool m_requiresTen =
false;
51constexpr LEGACY_DRC_ERROR legacyDrcErrors[] = {
52 {
"DRCET_UNKNOWN",
nullptr },
53 {
"DRCET_UNCONNECTED_ITEMS",
"unconnected_items" },
54 {
"DRCET_SHORTING_ITEMS",
"shorting_items" },
55 {
"DRCET_ALLOWED_ITEMS",
"items_not_allowed" },
56 {
"DRCET_TEXT_ON_EDGECUTS",
"text_on_edge_cuts" },
57 {
"DRCET_CLEARANCE",
"clearance" },
58 {
"DRCET_CREEPAGE",
"creepage" },
59 {
"DRCET_TRACKS_CROSSING",
"tracks_crossing" },
60 {
"DRCET_EDGE_CLEARANCE",
"copper_edge_clearance" },
61 {
"DRCET_ZONES_INTERSECT",
"zones_intersect" },
62 {
"DRCET_ISOLATED_COPPER",
"isolated_copper" },
63 {
"DRCET_STARVED_THERMAL",
"starved_thermal" },
64 {
"DRCET_DANGLING_VIA",
"via_dangling" },
65 {
"DRCET_DANGLING_TRACK",
"track_dangling" },
66 {
"DRCET_DRILLED_HOLES_TOO_CLOSE",
"hole_to_hole" },
67 {
"DRCET_DRILLED_HOLES_COLOCATED",
"holes_co_located" },
68 {
"DRCET_HOLE_CLEARANCE",
"hole_clearance" },
69 {
"DRCET_CONNECTION_WIDTH",
"connection_width" },
70 {
"DRCET_TRACK_WIDTH",
"track_width" },
71 {
"DRCET_TRACK_ANGLE",
"track_angle" },
72 {
"DRCET_TRACK_SEGMENT_LENGTH",
"track_segment_length" },
73 {
"DRCET_ANNULAR_WIDTH",
"annular_width" },
74 {
"DRCET_DRILL_OUT_OF_RANGE",
"drill_out_of_range" },
75 {
"DRCET_VIA_DIAMETER",
"via_diameter" },
76 {
"DRCET_PADSTACK",
"padstack" },
77 {
"DRCET_PADSTACK_INVALID",
"padstack_invalid" },
78 {
"DRCET_MICROVIA_DRILL_OUT_OF_RANGE",
"microvia_drill_out_of_range" },
79 {
"DRCET_OVERLAPPING_FOOTPRINTS",
"courtyards_overlap" },
80 {
"DRCET_MISSING_COURTYARD",
"missing_courtyard" },
81 {
"DRCET_MALFORMED_COURTYARD",
"malformed_courtyard" },
82 {
"DRCET_PTH_IN_COURTYARD",
"pth_inside_courtyard" },
83 {
"DRCET_NPTH_IN_COURTYARD",
"npth_inside_courtyard" },
84 {
"DRCET_DISABLED_LAYER_ITEM",
"item_on_disabled_layer" },
85 {
"DRCET_INVALID_OUTLINE",
"invalid_outline" },
86 {
"DRCET_MISSING_FOOTPRINT",
"missing_footprint" },
87 {
"DRCET_DUPLICATE_FOOTPRINT",
"duplicate_footprints" },
88 {
"DRCET_NET_CONFLICT",
"net_conflict" },
89 {
"DRCET_EXTRA_FOOTPRINT",
"extra_footprint" },
90 {
"DRCET_SCHEMATIC_PARITY",
"footprint_symbol_mismatch" },
91 {
"DRCET_SCHEMATIC_FIELDS_PARITY",
"footprint_symbol_field_mismatch",
true },
92 {
"DRCET_FOOTPRINT_FILTERS",
"footprint_filters_mismatch" },
93 {
"DRCET_LIB_FOOTPRINT_ISSUES",
"lib_footprint_issues" },
94 {
"DRCET_LIB_FOOTPRINT_MISMATCH",
"lib_footprint_mismatch" },
95 {
"DRCET_UNRESOLVED_VARIABLE",
"unresolved_variable" },
96 {
"DRCET_ASSERTION_FAILURE",
"assertion_failure" },
97 {
"DRCET_GENERIC_WARNING",
"generic_warning" },
98 {
"DRCET_GENERIC_ERROR",
"generic_error" },
99 {
"DRCET_COPPER_SLIVER",
"copper_sliver" },
100 {
"DRCET_SILK_CLEARANCE",
"silk_overlap" },
101 {
"DRCET_SILK_MASK_CLEARANCE",
"silk_over_copper" },
102 {
"DRCET_SILK_EDGE_CLEARANCE",
"silk_edge_clearance" },
103 {
"DRCET_SOLDERMASK_BRIDGE",
"solder_mask_bridge" },
104 {
"DRCET_TEXT_HEIGHT",
"text_height" },
105 {
"DRCET_TEXT_THICKNESS",
"text_thickness" },
106 {
"DRCET_LENGTH_OUT_OF_RANGE",
"length_out_of_range" },
107 {
"DRCET_SKEW_OUT_OF_RANGE",
"skew_out_of_range" },
108 {
"DRCET_VIA_COUNT_OUT_OF_RANGE",
"too_many_vias" },
109 {
"DRCET_DIFF_PAIR_GAP_OUT_OF_RANGE",
"diff_pair_gap_out_of_range" },
110 {
"DRCET_DIFF_PAIR_UNCOUPLED_LENGTH_TOO_LONG",
"diff_pair_uncoupled_length_too_long" },
111 {
"DRCET_FOOTPRINT",
"footprint" },
112 {
"DRCET_FOOTPRINT_TYPE_MISMATCH",
"footprint_type_mismatch" },
113 {
"DRCET_PAD_TH_WITH_NO_HOLE",
"through_hole_pad_without_hole" },
114 {
"DRCET_MIRRORED_TEXT_ON_FRONT_LAYER",
"mirrored_text_on_front_layer" },
115 {
"DRCET_NONMIRRORED_TEXT_ON_BACK_LAYER",
"nonmirrored_text_on_back_layer" },
116 {
"DRCET_MISSING_TUNING_PROFILE",
"missing_tuning_profile",
true },
117 {
"DRCET_TUNING_PROFILE_IMPLICIT_RULES",
"tuning_profile_track_geometries",
true },
118 {
"DRCET_TRACK_ON_POST_MACHINED_LAYER",
"track_on_post_machined_layer",
true },
119 {
"DRCET_TRACK_NOT_CENTERED_ON_VIA",
"track_not_centered_on_via",
true },
123constexpr LEGACY_DRC_ERROR legacyErcErrors[] = {
124 {
"ERCET_UNKNOWN",
nullptr },
125 {
"ERCET_DUPLICATE_SHEET_NAME",
"duplicate_sheet_names" },
126 {
"ERCET_ENDPOINT_OFF_GRID",
"endpoint_off_grid" },
127 {
"ERCET_PIN_NOT_CONNECTED",
"pin_not_connected" },
128 {
"ERCET_PIN_NOT_DRIVEN",
"pin_not_driven" },
129 {
"ERCET_POWERPIN_NOT_DRIVEN",
"power_pin_not_driven" },
130 {
"ERCET_HIERARCHICAL_LABEL",
"hier_label_mismatch" },
131 {
"ERCET_NOCONNECT_CONNECTED",
"no_connect_connected" },
132 {
"ERCET_NOCONNECT_NOT_CONNECTED",
"no_connect_dangling" },
133 {
"ERCET_LABEL_NOT_CONNECTED",
"label_dangling" },
134 {
"ERCET_SIMILAR_LABELS",
"similar_labels" },
135 {
"ERCET_SIMILAR_POWER",
"similar_power" },
136 {
"ERCET_SIMILAR_LABEL_AND_POWER",
"similar_label_and_power" },
137 {
"ERCET_SINGLE_GLOBAL_LABEL",
"single_global_label" },
138 {
"ERCET_SAME_LOCAL_GLOBAL_LABEL",
"same_local_global_label" },
139 {
"ERCET_SAME_LOCAL_GLOBAL_POWER",
nullptr },
140 {
"ERCET_DIFFERENT_UNIT_FP",
"different_unit_footprint" },
141 {
"ERCET_MISSING_POWER_INPUT_PIN",
"missing_power_pin" },
142 {
"ERCET_MISSING_INPUT_PIN",
"missing_input_pin" },
143 {
"ERCET_MISSING_BIDI_PIN",
"missing_bidi_pin" },
144 {
"ERCET_MISSING_UNIT",
"missing_unit" },
145 {
"ERCET_DIFFERENT_UNIT_NET",
"different_unit_net" },
146 {
"ERCET_BUS_ALIAS_CONFLICT",
"bus_definition_conflict" },
147 {
"ERCET_DRIVER_CONFLICT",
"multiple_net_names" },
148 {
"ERCET_BUS_ENTRY_CONFLICT",
"net_not_bus_member" },
149 {
"ERCET_BUS_TO_BUS_CONFLICT",
"bus_to_bus_conflict" },
150 {
"ERCET_BUS_TO_NET_CONFLICT",
"bus_to_net_conflict" },
151 {
"ERCET_GROUND_PIN_NOT_GROUND",
"ground_pin_not_ground",
true },
152 {
"ERCET_LABEL_SINGLE_PIN",
"isolated_pin_label",
true },
153 {
"ERCET_UNRESOLVED_VARIABLE",
"unresolved_variable" },
154 {
"ERCET_UNDEFINED_NETCLASS",
"undefined_netclass" },
155 {
"ERCET_SIMULATION_MODEL",
"simulation_model_issue" },
156 {
"ERCET_WIRE_DANGLING",
"wire_dangling" },
157 {
"ERCET_LIB_SYMBOL_ISSUES",
"lib_symbol_issues" },
158 {
"ERCET_LIB_SYMBOL_MISMATCH",
"lib_symbol_mismatch" },
159 {
"ERCET_FOOTPRINT_LINK_ISSUES",
"footprint_link_issues" },
160 {
"ERCET_FOOTPRINT_FILTERS",
"footprint_filter" },
161 {
"ERCET_UNANNOTATED",
"unannotated" },
162 {
"ERCET_EXTRA_UNITS",
"extra_units" },
163 {
"ERCET_DIFFERENT_UNIT_VALUE",
"unit_value_mismatch" },
164 {
"ERCET_DUPLICATE_REFERENCE",
"duplicate_reference" },
165 {
"ERCET_BUS_ENTRY_NEEDED",
"bus_entry_needed" },
166 {
"ERCET_FOUR_WAY_JUNCTION",
"four_way_junction" },
167 {
"ERCET_LABEL_MULTIPLE_WIRES",
"label_multiple_wires" },
168 {
"ERCET_UNCONNECTED_WIRE_ENDPOINT",
"unconnected_wire_endpoint" },
169 {
"ERCET_STACKED_PIN_SYNTAX",
"stacked_pin_name",
true },
170 {
"ERCET_PIN_MAP_BAD_PAD",
nullptr },
171 {
"ERCET_PIN_MAP_UNMAPPED_PIN",
nullptr },
172 {
"ERCET_PIN_MAP_DUPLICATE_PAD",
nullptr },
173 {
"ERCET_PIN_MAP_STALE_PIN",
nullptr },
174 {
"ERCET_EMPTY_LABEL_NAME",
nullptr },
175 {
"ERCET_VARIANT_SYMBOL_INVALID",
nullptr },
176 {
"ERCET_VARIANT_SYMBOL_INCOMPATIBLE",
nullptr },
177 {
"ERCET_DUPLICATE_PIN_ERROR",
"duplicate_pins" },
178 {
"ERCET_PIN_TO_PIN_WARNING",
"pin_to_pin" },
179 {
"ERCET_PIN_TO_PIN_ERROR",
"pin_to_pin" },
180 {
"ERCET_ANNOTATION_ACTION",
nullptr },
181 {
"ERCET_GENERIC_WARNING",
"generic-warning" },
182 {
"ERCET_GENERIC_ERROR",
"generic-error" },
183 {
"ERCET_FIELD_NAME_WHITESPACE",
"field_name_whitespace",
true },
184 {
"ERCET_WIRED_IMPLICIT_POWER",
nullptr },
188bool isUuid(
const std::string& aText )
190 if( aText.size() != 36 )
193 for(
size_t i = 0; i < aText.size(); i++ )
195 if( i == 8 || i == 13 || i == 18 || i == 23 )
197 if( aText[i] !=
'-' )
200 else if( !std::isxdigit(
static_cast<unsigned char>( aText[i] ) ) )
210bool parseCoordinate(
const nlohmann::json& aValue, std::string& aText, int64_t aDivisor = 1 )
212 if( !aValue.is_number_integer() && !aValue.is_string() )
215 std::string
text = aValue.is_string() ? aValue.get<std::string>() : aValue.
dump();
217 auto parsed = std::from_chars(
text.data(),
text.data() +
text.size(), value );
219 if( parsed.ec != std::errc() || parsed.ptr !=
text.data() +
text.size() || value % aDivisor != 0
220 || value / aDivisor < std::numeric_limits<int32_t>::min()
221 || value / aDivisor > std::numeric_limits<int32_t>::max() )
226 aText = std::to_string( value / aDivisor );
231std::string legacyLayerName(
const nlohmann::json& aLayer,
bool aPreTen )
234 static constexpr const char* layers[] = {
235 nullptr,
nullptr,
nullptr,
"F.Cu",
"In1.Cu",
"In2.Cu",
"In3.Cu",
"In4.Cu",
"In5.Cu",
236 "In6.Cu",
"In7.Cu",
"In8.Cu",
"In9.Cu",
"In10.Cu",
"In11.Cu",
"In12.Cu",
"In13.Cu",
"In14.Cu",
237 "In15.Cu",
"In16.Cu",
"In17.Cu",
"In18.Cu",
"In19.Cu",
"In20.Cu",
"In21.Cu",
"In22.Cu",
"In23.Cu",
238 "In24.Cu",
"In25.Cu",
"In26.Cu",
"In27.Cu",
"In28.Cu",
"In29.Cu",
"In30.Cu",
"B.Cu",
"B.Adhes",
239 "F.Adhes",
"B.Paste",
"F.Paste",
"B.SilkS",
"F.SilkS",
"B.Mask",
"F.Mask",
"Dwgs.User",
"Cmts.User",
240 "Eco1.User",
"Eco2.User",
"Edge.Cuts",
"Margin",
"B.CrtYd",
"F.CrtYd",
"B.Fab",
"F.Fab",
"User.1",
241 "User.2",
"User.3",
"User.4",
"User.5",
"User.6",
"User.7",
"User.8",
"User.9",
nullptr,
242 "User.10",
"User.11",
"User.12",
"User.13",
"User.14",
"User.15",
"User.16",
"User.17",
"User.18",
243 "User.19",
"User.20",
"User.21",
"User.22",
"User.23",
"User.24",
"User.25",
"User.26",
"User.27",
244 "User.28",
"User.29",
"User.30",
"User.31",
"User.32",
"User.33",
"User.34",
"User.35",
"User.36",
245 "User.37",
"User.38",
"User.39",
"User.40",
"User.41",
"User.42",
"User.43",
"User.44",
"User.45"
250 if( aLayer.is_number_integer() && aLayer >= 0 && aLayer < std::size( layers ) )
251 index = aLayer.get<
int>();
252 else if( aLayer.is_string() )
254 std::string
name = aLayer.get<std::string>();
256 for(
size_t i = 3; i < std::size( layers ); i++ )
261 std::string proto =
"BL_" + std::string( layers[i] );
262 std::replace( proto.begin(), proto.end(),
'.',
'_' );
265 index =
static_cast<int>( i );
272 return layers[
index];
276bool downgradeProtoExclusion(
const nlohmann::json& aEntry,
bool aPreTen, nlohmann::json& aResult )
278 if( !aEntry.is_object() || !aEntry.contains(
"marker" ) || !aEntry[
"marker"].is_object()
279 || ( aEntry.contains(
"comment" ) && !aEntry[
"comment"].is_string() ) )
284 const nlohmann::json& marker = aEntry[
"marker"];
286 if( !marker.contains(
"error_type" ) )
289 const LEGACY_DRC_ERROR* error =
nullptr;
290 const nlohmann::json& type = marker[
"error_type"];
292 if( type.is_string() )
295 const std::string
name = type ==
"DRCET_LINE_WIDTH" ?
"DRCET_TEXT_THICKNESS" : type.get<std::string>();
297 for(
const LEGACY_DRC_ERROR& candidate : legacyDrcErrors )
299 if(
name == candidate.m_protoName )
303 else if( type.is_number_integer() && type >= 0 && type < std::size( legacyDrcErrors ) )
305 error = &legacyDrcErrors[type.get<
size_t>()];
308 if( !error || !error->m_settingsKey || ( aPreTen && error->m_requiresTen ) )
314 if( marker.contains(
"position" ) )
316 const nlohmann::json& position = marker[
"position"];
318 if( !position.is_object() || ( position.contains(
"x_nm" ) && !parseCoordinate( position[
"x_nm"], x ) )
319 || ( position.contains(
"y_nm" ) && !parseCoordinate( position[
"y_nm"], y ) ) )
325 std::vector<std::string> ids;
327 if( marker.contains(
"items" ) )
329 if( !marker[
"items"].is_array() || marker[
"items"].size() > 2 )
332 for(
const nlohmann::json& item : marker[
"items"] )
334 if( !item.is_object() || !item.contains(
"value" ) || !item[
"value"].is_string()
335 || !isUuid( item[
"value"].get<std::string>() ) )
340 ids.push_back( item[
"value"].get<std::string>() );
344 const std::string nil =
"00000000-0000-0000-0000-000000000000";
345 std::string
main = ids.empty() ? nil : ids[0];
346 std::string aux = ids.size() < 2 ? nil : ids[1];
347 std::string key = error->m_settingsKey;
348 std::string value = key +
'|' + x +
'|' + y +
'|';
350 if( key ==
"copper_sliver" || key ==
"generic_warning" || key ==
"generic_error" || key ==
"starved_thermal"
351 || ( key ==
"unconnected_items" && !aPreTen ) )
353 std::string layer = marker.contains(
"layer" ) ? legacyLayerName( marker[
"layer"], aPreTen ) :
"";
358 if( key ==
"unconnected_items" )
359 value += layer +
"|4|" +
main +
'|' + aux;
360 else if( key ==
"starved_thermal" )
361 value +=
main +
'|' + aux +
'|' + layer;
367 value +=
main +
'|' + layer;
374 if( key ==
"unresolved_variable" && ids.empty() )
377 value +=
main +
'|' + aux;
380 aResult = nlohmann::json::array( { value, aEntry.value(
"comment",
"" ) } );
385bool downgradeLegacyExclusion(
const nlohmann::json& aEntry,
bool aPreTen, nlohmann::json& aResult )
390 if( aEntry.is_string() )
391 marker = aEntry.get<std::string>();
392 else if( aEntry.is_array() && !aEntry.empty() && aEntry[0].is_string()
393 && ( aEntry.size() == 1 || ( aEntry.size() == 2 && aEntry[1].is_string() ) ) )
395 marker = aEntry[0].get<std::string>();
397 if( aEntry.size() == 2 )
398 comment = aEntry[1].get<std::string>();
403 std::vector<std::string> parts;
406 for(
size_t end = marker.find(
'|' );
end != std::string::npos;
end = marker.find(
'|', start ) )
408 parts.push_back( marker.substr( start,
end - start ) );
412 parts.push_back( marker.substr( start ) );
414 if( parts.size() < 5 )
417 bool supported = parts[0] ==
"hole_near_hole";
419 for(
const LEGACY_DRC_ERROR& error : legacyDrcErrors )
421 if( error.m_settingsKey && parts[0] == error.m_settingsKey && !( aPreTen && error.m_requiresTen ) )
425 std::string coordinate;
427 if( !supported || !parseCoordinate( parts[1], coordinate ) || !parseCoordinate( parts[2], coordinate ) )
430 if( parts[0] ==
"unconnected_items" && parts.size() == 7 )
432 if( !isUuid( parts[5] ) || !isUuid( parts[6] ) )
436 marker = parts[0] +
'|' + parts[1] +
'|' + parts[2] +
'|' + parts[5] +
'|' + parts[6];
438 else if( parts[0] ==
"starved_thermal" )
440 if( parts.size() != 6 || !isUuid( parts[3] ) || !isUuid( parts[4] ) )
443 else if( parts[0] ==
"copper_sliver" || parts[0] ==
"generic_warning" || parts[0] ==
"generic_error" )
445 if( parts.size() != 5 || !isUuid( parts[3] ) )
448 else if( parts.size() != 5
449 || !( ( isUuid( parts[3] ) && isUuid( parts[4] ) )
450 || ( parts[0] ==
"unresolved_variable" && parts[3].
empty() && parts[4].
empty() ) ) )
455 aResult = nlohmann::json::array( { marker, comment } );
460bool legacyErcPath(
const nlohmann::json& aPath, std::string& aText )
462 if( !aPath.is_object() || !aPath.contains(
"path" ) || !aPath[
"path"].is_array() || aPath[
"path"].empty() )
465 for(
const nlohmann::json& part : aPath[
"path"] )
467 if( !part.is_object() || !part.contains(
"value" ) || !part[
"value"].is_string()
468 || !isUuid( part[
"value"].get<std::string>() ) )
473 aText +=
'/' + part[
"value"].get<std::string>();
480bool ercChildNeedsTextDowngrade(
const wxString& aText,
const nlohmann::json& aDoc,
bool aPreTen,
481 std::set<std::string>& aVisiting )
483 bool changed = aPreTen && ( aText.Contains( wxT(
"\\${" ) ) || aText.Contains( wxT(
"\\@{" ) ) );
484 std::function<bool( wxString* )> scan = [&]( wxString* aToken )
486 const wxString token = aToken->AfterLast(
':' );
487 changed |= token.StartsWith( wxT(
"PROPERTY." ) ) || token == wxT(
"SYMBOL_IS_POWER" )
488 || token == wxT(
"SYMBOL_IS_LOCAL_POWER" );
490 if( aDoc.contains(
"text_variables" ) && aDoc[
"text_variables"].is_object() )
492 const nlohmann::json& variables = aDoc[
"text_variables"];
493 const std::string key = aToken->ToStdString( wxConvUTF8 );
495 if( variables.contains( key ) && variables[key].is_string() && aVisiting.insert( key ).second )
497 changed |= ercChildNeedsTextDowngrade( wxString::FromUTF8( variables[key].get<std::string>() ), aDoc,
498 aPreTen, aVisiting );
499 aVisiting.erase( key );
507 return changed || ( aPreTen && scanned.Contains( wxT(
"@{" ) ) );
511bool downgradeProtoErcExclusion(
const nlohmann::json& aEntry,
const nlohmann::json& aDoc,
bool aPreTen,
512 nlohmann::json& aResult )
514 if( !aEntry.contains(
"marker" ) || !aEntry[
"marker"].is_object()
515 || ( aEntry.contains(
"comment" ) && !aEntry[
"comment"].is_string() ) )
520 const nlohmann::json& marker = aEntry[
"marker"];
522 if( !marker.contains(
"error_type" ) )
525 const LEGACY_DRC_ERROR* error =
nullptr;
526 const nlohmann::json& type = marker[
"error_type"];
528 if( type.is_string() )
530 for(
const LEGACY_DRC_ERROR& candidate : legacyErcErrors )
532 if( type == candidate.m_protoName )
536 else if( type.is_number_integer() && type >= 0 && type < std::size( legacyErcErrors ) )
537 error = &legacyErcErrors[type.get<
size_t>()];
539 if( !error || !error->m_settingsKey || ( aPreTen && error->m_requiresTen ) )
545 if( marker.contains(
"position" ) )
547 const nlohmann::json& position = marker[
"position"];
550 if( !position.is_object() || ( position.contains(
"x_nm" ) && !parseCoordinate( position[
"x_nm"], x, 100 ) )
551 || ( position.contains(
"y_nm" ) && !parseCoordinate( position[
"y_nm"], y, 100 ) ) )
557 const std::string nil =
"00000000-0000-0000-0000-000000000000";
558 std::vector<std::string> ids;
560 if( marker.contains(
"items" ) )
562 if( !marker[
"items"].is_array() || marker[
"items"].size() > 2 )
565 for(
const nlohmann::json& item : marker[
"items"] )
567 if( !item.is_object() || !item.contains(
"value" ) || !item[
"value"].is_string()
568 || !isUuid( item[
"value"].get<std::string>() ) )
573 ids.push_back( item[
"value"].get<std::string>() );
577 std::string
main = ids.empty() ? nil : ids[0];
578 std::string aux = ids.size() < 2 ? nil : ids[1];
579 std::string key = error->m_settingsKey;
581 if( marker.contains(
"child" ) )
583 const nlohmann::json& child = marker[
"child"];
585 if( ( key !=
"generic-warning" && key !=
"generic-error" && key !=
"unresolved_variable" ) || ids.empty()
586 ||
main == nil || !child.is_object() || !child.contains(
"text_value" )
587 || !child[
"text_value"].is_string() )
592 aux = child[
"text_value"].get<std::string>();
594 std::set<std::string> visiting;
596 if( aux.empty() || aux.find(
'|' ) != std::string::npos || marker.contains(
"aux_item_sheet_path" )
597 || ercChildNeedsTextDowngrade( wxString::FromUTF8( aux ), aDoc, aPreTen, visiting ) )
601 std::string value = key +
'|' + x +
'|' + y +
'|' +
main +
'|' + aux;
603 if( marker.contains(
"aux_item_sheet_path" ) && !marker.contains(
"main_item_sheet_path" ) )
606 for(
const char*
name : {
"sheet_specific_path",
"main_item_sheet_path",
"aux_item_sheet_path" } )
610 if( marker.contains(
name ) && !legacyErcPath( marker[
name],
path ) )
616 aResult = nlohmann::json::array( { value, aEntry.value(
"comment",
"" ) } );
621bool downgradeLegacyErcExclusion(
const nlohmann::json& aEntry,
const nlohmann::json& aDoc,
bool aPreTen,
622 nlohmann::json& aResult )
627 if( aEntry.is_string() )
628 marker = aEntry.get<std::string>();
629 else if( aEntry.is_array() && !aEntry.empty() && aEntry[0].is_string()
630 && ( aEntry.size() == 1 || ( aEntry.size() == 2 && aEntry[1].is_string() ) ) )
632 marker = aEntry[0].get<std::string>();
634 if( aEntry.size() == 2 )
635 comment = aEntry[1].get<std::string>();
640 std::vector<std::string> parts;
643 for(
size_t end = marker.find(
'|' );
end != std::string::npos;
end = marker.find(
'|', start ) )
645 parts.push_back( marker.substr( start,
end - start ) );
649 parts.push_back( marker.substr( start ) );
651 if( parts.size() != 5 && parts.size() != 8 )
654 bool supported = aPreTen && parts[0] ==
"global_label_dangling";
656 for(
const LEGACY_DRC_ERROR& error : legacyErcErrors )
658 if( error.m_settingsKey && parts[0] == error.m_settingsKey && !( aPreTen && error.m_requiresTen ) )
662 std::string coordinate;
664 if( !supported || !parseCoordinate( parts[1], coordinate ) || !parseCoordinate( parts[2], coordinate )
665 || !isUuid( parts[3] ) )
670 bool childText = parts[0] ==
"generic-warning" || parts[0] ==
"generic-error" || parts[0] ==
"unresolved_variable";
672 if( !childText && !isUuid( parts[4] ) )
675 std::set<std::string> visiting;
677 if( childText && ercChildNeedsTextDowngrade( wxString::FromUTF8( parts[4] ), aDoc, aPreTen, visiting ) )
680 aResult = nlohmann::json::array( { marker, comment } );
687 if( !aDoc.contains(
"erc" ) || !aDoc[
"erc"].is_object() )
690 nlohmann::json& settings = aDoc[
"erc"];
692 if( settings.contains(
"erc_exclusions" ) && !settings[
"erc_exclusions"].is_array() )
694 if( !settings[
"erc_exclusions"].is_null() && !settings[
"erc_exclusions"].
empty() )
696 _(
"The target cannot represent this exclusion. It is removed." ), 1 );
698 settings[
"erc_exclusions"] = nlohmann::json::array();
701 if( settings.contains(
"erc_exclusions" ) && settings[
"erc_exclusions"].is_array() )
703 nlohmann::json exclusions = nlohmann::json::array();
705 for(
const nlohmann::json& entry : settings[
"erc_exclusions"] )
707 nlohmann::json converted;
709 bool ok = entry.is_object() ? downgradeProtoErcExclusion( entry, aDoc, aPreTen, converted )
710 : downgradeLegacyErcExclusion( entry, aDoc, aPreTen, converted );
713 exclusions.push_back( std::move( converted ) );
716 _(
"The target cannot represent this exclusion. It is removed." ), 1 );
719 settings[
"erc_exclusions"] = std::move( exclusions );
722 if( settings.contains(
"meta" ) && settings[
"meta"].is_object() )
723 settings[
"meta"][
"version"] = 0;
727bool hasNonDefaultSettings(
const nlohmann::json& aSettings,
const nlohmann::json& aDefaults )
729 if( aSettings == aDefaults )
732 if( !aSettings.is_object() )
733 return !aSettings.empty() && !aSettings.is_null();
735 for(
const auto& [key, value] : aSettings.items() )
737 if( !aDefaults.contains( key ) || value != aDefaults[key] )
745bool hasConfiguredProjectSettings(
const nlohmann::json& aSettings,
const nlohmann::json& aDefaults,
746 bool aSectionRoot =
true )
748 if( aSettings == aDefaults || aSettings.is_null() || aSettings.empty() )
751 if( !aSettings.is_object() || !aDefaults.is_object() )
754 for(
const auto& [key, value] : aSettings.items() )
756 if( aSectionRoot && key ==
"meta" )
759 if( aDefaults.contains( key ) )
761 if( hasConfiguredProjectSettings( value, aDefaults[key],
false ) )
764 else if( !value.is_null() && !value.empty() )
776 auto downgradePreset = [&]( nlohmann::json& aPreset )
778 if( !aPreset.is_object() )
781 if( aPreset.contains(
"filter_scope" ) && aPreset[
"filter_scope"] !=
"reference"
782 && aPreset.contains(
"filter_string" ) && aPreset[
"filter_string"].is_string()
783 && !aPreset[
"filter_string"].get<std::string>().empty() )
786 _(
"The target can only filter references. The field filter is removed." ), 1 );
787 aPreset[
"filter_string"] =
"";
790 aPreset.erase(
"filter_scope" );
793 auto downgradeFormat = [&]( nlohmann::json& aFormat )
795 if( !aFormat.is_object() )
798 if( aFormat.contains(
"include_byte_order_mark" ) && aFormat[
"include_byte_order_mark"] ==
true )
800 _(
"The target cannot retain the BOM byte order mark option." ), 1 );
802 aFormat.erase(
"include_byte_order_mark" );
805 if( aSettings.contains(
"bom_settings" ) )
806 downgradePreset( aSettings[
"bom_settings"] );
808 if( aSettings.contains(
"bom_presets" ) && aSettings[
"bom_presets"].is_array() )
810 for( nlohmann::json& preset : aSettings[
"bom_presets"] )
811 downgradePreset( preset );
814 if( aSettings.contains(
"bom_fmt_settings" ) )
815 downgradeFormat( aSettings[
"bom_fmt_settings"] );
817 if( aSettings.contains(
"bom_fmt_presets" ) && aSettings[
"bom_fmt_presets"].is_array() )
819 for( nlohmann::json& format : aSettings[
"bom_fmt_presets"] )
820 downgradeFormat( format );
827 if( aDoc.contains(
"schematic" ) && aDoc[
"schematic"].is_object() )
828 downgradeBomSettings( aDoc[
"schematic"], aReport );
830 if( !aDoc.contains(
"board" ) || !aDoc[
"board"].is_object() )
833 nlohmann::json& board = aDoc[
"board"];
835 if( board.contains(
"ipc2581" ) && board[
"ipc2581"].is_object() )
837 nlohmann::json& ipc = board[
"ipc2581"];
838 bool changed =
false;
839 const nlohmann::json defaults = {
840 {
"mode",
"" }, {
"sections",
"" }, {
"custom_sections",
false }, {
"net_names",
"" }, {
"ref_des",
"" }
843 for(
const auto& [key, value] : defaults.items() )
845 if( ipc.contains( key ) )
847 changed |= ipc[key] != value;
854 _(
"The target cannot retain the selected output sections or naming rules." ), 1 );
857 if( board.contains(
"idf_export" ) )
859 const nlohmann::json defaults = { {
"units", 0 },
860 {
"origin_mode", 2 },
861 {
"user_origin_x", 0.0 },
862 {
"user_origin_y", 0.0 },
863 {
"include_unspecified",
true },
864 {
"include_dnp",
true },
865 {
"calculate_height_from_models",
true },
866 {
"part_number_field",
"Value" } };
868 if( hasNonDefaultSettings( board[
"idf_export"], defaults ) )
870 _(
"The target cannot retain the selected IDF export settings." ), 1 );
872 board.erase(
"idf_export" );
875 if( board.contains(
"design_settings" ) && board[
"design_settings"].is_object() )
877 nlohmann::json& settings = board[
"design_settings"];
878 const nlohmann::json defaults = { {
"bom_export_filename",
"${PROJECTNAME}.csv" },
879 {
"bom_settings", BOM_PRESET::DefaultEditing() },
881 {
"bom_presets", nlohmann::json::array() },
882 {
"bom_fmt_presets", nlohmann::json::array() } };
883 bool changed =
false;
885 for(
const auto& [key, value] : defaults.items() )
887 if( !settings.contains( key ) )
890 changed |= hasNonDefaultSettings( settings[key], value );
891 settings.erase( key );
896 _(
"The target cannot retain the PCB BOM export settings." ), 1 );
903 if( !aDoc.contains(
"board" ) || !aDoc[
"board"].is_object() || !aDoc[
"board"].contains(
"design_settings" )
904 || !aDoc[
"board"][
"design_settings"].is_object() )
909 nlohmann::json& settings = aDoc[
"board"][
"design_settings"];
911 if( settings.contains(
"drc_exclusions" ) )
913 nlohmann::json exclusions = nlohmann::json::array();
915 if( !settings[
"drc_exclusions"].is_array() )
917 if( !settings[
"drc_exclusions"].is_null() && !settings[
"drc_exclusions"].
empty() )
919 _(
"The target cannot represent this exclusion. It is removed." ), 1 );
923 for(
const nlohmann::json& entry : settings[
"drc_exclusions"] )
925 nlohmann::json converted;
926 bool ok = entry.is_object() ? downgradeProtoExclusion( entry, aPreTen, converted )
927 : downgradeLegacyExclusion( entry, aPreTen, converted );
930 exclusions.push_back( std::move( converted ) );
933 _(
"The target cannot represent this exclusion. It is removed." ), 1 );
937 settings[
"drc_exclusions"] = std::move( exclusions );
940 if( settings.contains(
"via_stack_presets" ) )
942 if( !settings[
"via_stack_presets"].
empty() && !settings[
"via_stack_presets"].is_null() )
946 settings.erase(
"via_stack_presets" );
949 if( settings.contains(
"teardrop_parameters" ) && settings[
"teardrop_parameters"].is_array() )
951 for( nlohmann::json& entry : settings[
"teardrop_parameters"] )
953 if( !entry.is_object() )
956 if( entry.contains(
"td_enabled" ) && entry[
"td_enabled"] ==
true )
958 _(
"The target cannot retain enabled teardrop defaults." ), 1 );
960 entry.erase(
"td_enabled" );
965 auto restoreKey = [&settings](
const char* aParent,
const char* aCurrent,
const char* aLegacy )
967 if( !settings.contains( aParent ) || !settings[aParent].is_object() )
970 nlohmann::json& parent = settings[aParent];
972 if( !parent.contains( aCurrent ) )
975 parent[aLegacy] = parent[aCurrent];
976 parent.erase( aCurrent );
979 restoreKey(
"rule_severities",
"line_width",
"text_thickness" );
980 restoreKey(
"rules",
"min_silk_line_width",
"min_text_thickness" );
982 if( settings.contains(
"meta" ) && settings[
"meta"].is_object() )
983 settings[
"meta"][
"version"] = 2;
992 auto knownSchema = [&](
const nlohmann::json& aSettings,
int aMaximum,
const wxString& aName )
994 if( aSettings.is_object() && aSettings.contains(
"meta" ) && aSettings[
"meta"].is_object()
995 && aSettings[
"meta"].contains(
"version" ) && aSettings[
"meta"][
"version"].is_number()
996 && aSettings[
"meta"][
"version"] > aMaximum )
999 wxString::Format(
_(
"The %s schema is newer than the exporter has reviewed." ), aName ) );
1006 bool known = knownSchema( aDoc, reviewed.
m_Project,
_(
"project" ) );
1008 if( aDoc.contains(
"net_settings" ) )
1009 known &= knownSchema( aDoc[
"net_settings"], reviewed.
m_NetSettings,
_(
"net settings" ) );
1011 if( aDoc.contains(
"tuning_profiles" ) )
1012 known &= knownSchema( aDoc[
"tuning_profiles"], reviewed.
m_TuningProfiles,
_(
"tuning profiles" ) );
1014 if( aDoc.contains(
"component_class_settings" ) )
1016 _(
"component class settings" ) );
1018 if( aDoc.contains(
"erc" ) )
1019 known &= knownSchema( aDoc[
"erc"], reviewed.
m_Erc,
_(
"ERC settings" ) );
1021 if( aDoc.contains(
"board" ) && aDoc[
"board"].is_object() && aDoc[
"board"].contains(
"design_settings" ) )
1023 _(
"board design settings" ) );
1030 bool tuningLoss =
false;
1032 downgradeBoardSettings( aDoc, preTen, aReport );
1033 downgradeErcSettings( aDoc, preTen, aReport );
1034 downgradeOutputSettings( aDoc, aReport );
1036 if( aDoc.contains(
"net_settings" ) )
1038 nlohmann::json& net = aDoc[
"net_settings"];
1040 if( net.contains(
"net_chain_netclasses" ) )
1042 if( !net[
"net_chain_netclasses"].
empty() && !net[
"net_chain_netclasses"].is_null() )
1044 _(
"Removed from the project file." ), 1 );
1046 net.erase(
"net_chain_netclasses" );
1049 if( net.contains(
"net_chain_classes" ) )
1051 bool empty = net[
"net_chain_classes"].is_null() || net[
"net_chain_classes"].empty();
1053 net.erase(
"net_chain_classes" );
1064 if( net.contains(
"classes" ) && net[
"classes"].is_array() )
1066 for( nlohmann::json& netclass : net[
"classes"] )
1068 if( netclass.is_object() )
1070 if( netclass.contains(
"tuning_profile" ) && netclass[
"tuning_profile"].is_string()
1071 && !netclass[
"tuning_profile"].get_ref<
const std::string&>().empty() )
1076 netclass.erase(
"tuning_profile" );
1081 if( net.contains(
"meta" ) && net[
"meta"].is_object() )
1082 net[
"meta"][
"version"] = 4;
1086 if( aDoc.contains(
"tuning_profiles" ) )
1090 const nlohmann::json defaults = {
1091 {
"tuning_profiles_impedance_geometric", nlohmann::json::array() },
1093 tuningLoss |= hasConfiguredProjectSettings( aDoc[
"tuning_profiles"], defaults );
1094 aDoc.erase(
"tuning_profiles" );
1098 nlohmann::json& profiles = aDoc[
"tuning_profiles"];
1100 if( profiles.contains(
"tuning_profiles_impedance_geometric" )
1101 && profiles[
"tuning_profiles_impedance_geometric"].is_array() )
1103 for( nlohmann::json& entry : profiles[
"tuning_profiles_impedance_geometric"] )
1105 if( !entry.is_object() )
1108 if( entry.contains(
"frequency" ) && entry[
"frequency"].is_number()
1109 && entry[
"frequency"].get<
double>() != 1e9 )
1112 _(
"The target uses its default calculation frequency." ), 1 );
1115 if( entry.contains(
"model_solder_mask" ) && entry[
"model_solder_mask"].is_boolean()
1116 && entry[
"model_solder_mask"].get<
bool>() )
1119 _(
"The target cannot include solder mask in the calculation." ), 1 );
1122 entry.erase(
"frequency" );
1123 entry.erase(
"model_solder_mask" );
1125 if( entry.contains(
"net_chain_bridge_prop_delay" )
1126 && entry[
"net_chain_bridge_prop_delay"].is_number()
1127 && entry[
"net_chain_bridge_prop_delay"].get<
double>() != 0 )
1130 _(
"The target cannot model delay across net chain bridges." ), 1 );
1133 entry.erase(
"net_chain_bridge_prop_delay" );
1137 if( profiles.contains(
"meta" ) && profiles[
"meta"].is_object() )
1138 profiles[
"meta"][
"version"] = 0;
1145 _(
"Removed saved profiles or netclass profile selections from the project file." ), 1 );
1151 if( aDoc.contains(
"component_class_settings" ) )
1153 const nlohmann::json defaults = {
1154 {
"assignments", nlohmann::json::array() },
1155 {
"sheet_component_classes", { {
"enabled",
false } } },
1157 bool configured = hasConfiguredProjectSettings( aDoc[
"component_class_settings"], defaults );
1158 aDoc.erase(
"component_class_settings" );
1163 _(
"Removed from the project file." ), 1 );
1167 if( aDoc.contains(
"schematic" ) && aDoc[
"schematic"].is_object() )
1170 aDoc[
"schematic"].erase(
"top_level_sheets" );
1173 aDoc[
"schematic"].erase(
"bus_aliases" );
1176 aDoc[
"schematic"].erase(
"variants" );
1181 if( aDoc.contains(
"meta" ) && aDoc[
"meta"].is_object() )
1182 aDoc[
"meta"][
"version"] = 3;
What an export to a given target will keep, approximate, drop, or block.
void Add(DOWNGRADE_BUCKET aBucket, const wxString &aFeature, const wxString &aDetail=wxEmptyString, int aCount=1)
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, RESOLUTION_CONTEXT aContext)
@ BLOCK
Cannot be represented at all, export to this target is not possible.
@ DROP
No equivalent, left out.
static bool empty(const wxTextEntryBase *aCtrl)
void DowngradeProjectFileJson(nlohmann::json &aDoc, const DOWNGRADE_TARGET &aTarget, COMPATIBILITY_REPORT &aReport)
Migrate a .kicad_pro document down to what the target release expects.
static BOM_FMT_PRESET CSV()
An older KiCad release we can export a project back to.
int m_boardVersion
SEXPR_BOARD_FILE_VERSION for the target.
The settings schemas the project downgrade rules were written against.
int m_BoardDesignSettings
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.