53#include <wx/numformatter.h>
54#include <wx/mstream.h>
71 catch(
const std::exception& e )
73 std::cerr << e.what() << std::endl;
83 { wxS(
"ODB_VERSION_MAJOR" ), wxS(
"8" ) },
84 { wxS(
"ODB_VERSION_MINOR" ), wxS(
"1" ) },
85 { wxS(
"ODB_SOURCE" ), wxS(
"KiCad EDA" ) },
86 { wxS(
"CREATION_DATE" ), wxDateTime::Now().Format(
"%Y%m%d.%H%M%S" ) },
87 { wxS(
"SAVE_DATE" ), wxDateTime::Now().Format(
"%Y%m%d.%H%M%S" ) },
88 { wxS(
"SAVE_APP" ), wxString::Format( wxS(
"KiCad EDA %s" ),
GetBuildVersion() ) } };
125 std::vector<BOARD_STACKUP_ITEM*> layers = stackup.
GetList();
126 std::set<PCB_LAYER_ID> added_layers;
130 for(
int i = 0; i < stackup.
GetCount(); i++ )
134 for(
int sublayer_id = 0; sublayer_id < stackup_item->
GetSublayersCount(); sublayer_id++ )
138 if( ly_name.IsEmpty() )
144 ly_name = wxString::Format(
"DIELECTRIC_%d",
161 m_plugin->GetLayerNameList().emplace_back(
176 if( added_layers.find( layer ) != added_layers.end() )
180 added_layers.insert( layer );
285 m_plugin->GetLayerNameList().emplace_back( std::make_pair( aLayer, aMLayer.
m_layerName ) );
292 std::map<ODB_DRILL_SPAN, std::vector<BOARD_ITEM*>>& drill_layers =
295 std::map<std::pair<PCB_LAYER_ID, PCB_LAYER_ID>, std::vector<BOARD_ITEM*>>& slot_holes =
298 drill_layers.clear();
300 std::map<ODB_DRILL_SPAN, wxString>& span_names =
m_plugin->GetDrillSpanNameMap();
303 bool has_pth_layer =
false;
304 bool has_npth_layer =
false;
311 has_pth_layer =
true;
314 drill_layers[platedSpan].push_back(
via );
319 && ( secondary.
size.
x > 0 || secondary.
size.
y > 0 ) )
322 drill_layers[backSpan].push_back(
via );
329 if( fp->IsFlipped() )
334 for(
PAD*
pad : fp->Pads() )
337 has_pth_layer =
true;
339 has_npth_layer =
true;
341 if(
pad->HasHole() &&
pad->GetDrillSizeX() !=
pad->GetDrillSizeY() )
342 slot_holes[std::make_pair(
F_Cu,
B_Cu )].push_back(
pad );
343 else if(
pad->HasHole() )
346 drill_layers[padSpan].push_back(
pad );
354 drill_layers[npthSpan];
360 drill_layers[platedSpan];
363 int backdrillIndex = 1;
367 auto it = span_names.find( aSpan );
369 if( it != span_names.end() )
374 if( aSpan.m_IsBackdrill )
376 name.Printf( wxT(
"drill%d" ), backdrillIndex++ );
380 wxString platedLabel = aSpan.m_IsNonPlated ? wxT(
"non-plated" ) : wxT(
"plated" );
381 name.Printf( wxT(
"drill_%s_%s-%s" ), platedLabel,
382 m_board->GetLayerName( aSpan.TopLayer() ),
383 m_board->GetLayerName( aSpan.BottomLayer() ) );
387 span_names[aSpan] = legalName;
394 wxString dLayerName = assignName( aSpan );
400 matrix.
m_span.emplace( std::make_pair(
404 if( aSpan.m_IsBackdrill )
408 m_plugin->GetLayerNameList().emplace_back(
412 for(
const auto& entry : drill_layers )
414 InitDrillMatrix( entry.first );
425 if( aCompSide ==
F_Cu )
428 m_plugin->GetLayerNameList().emplace_back(
436 m_plugin->GetLayerNameList().emplace_back(
443 auto& auxilliary_layers =
m_plugin->GetAuxilliaryLayerItemsMap();
451 if(
via->Padstack().IsFilled().value_or(
false ) )
454 via->BottomLayer() )]
458 if(
via->Padstack().IsCapped().value_or(
false ) )
461 via->BottomLayer() )]
467 if(
via->Padstack().IsPlugged( layer ).value_or(
false ) )
474 if(
via->Padstack().IsCovered( layer ).value_or(
false ) )
481 if(
via->Padstack().IsTented( layer ).value_or(
false ) )
492 [&]( std::tuple<ODB_AUX_LAYER_TYPE, PCB_LAYER_ID, PCB_LAYER_ID> aLayerPair )
494 wxString featureName =
"";
495 switch( std::get<0>( aLayerPair ) )
509 dLayerName = wxString::Format(
"%s_%s-%s", featureName,
510 m_board->GetLayerName( std::get<1>( aLayerPair ) ),
511 m_board->GetLayerName( std::get<2>( aLayerPair ) ) );
515 if(
m_board->IsFrontLayer( std::get<1>( aLayerPair ) ) )
516 dLayerName = wxString::Format(
"%s_front", featureName );
517 else if(
m_board->IsBackLayer( std::get<1>( aLayerPair ) ) )
518 dLayerName = wxString::Format(
"%s_back", featureName );
530 matrix.
m_span.emplace( std::make_pair(
533 m_board->GetLayerName( std::get<2>( aLayerPair ) ) ) ) );
540 m_plugin->GetLayerNameList().emplace_back(
545 m_plugin->GetLayerNameList().emplace_back(
546 std::make_pair( std::get<1>( aLayerPair ), matrix.
m_layerName ) );
550 for(
const auto& [layer_pair, vec] : auxilliary_layers )
552 InitAuxMatrix( layer_pair );
560 std::map<wxString, std::vector<size_t>> name_to_indices;
566 name_to_indices[layerName].push_back( i );
570 for(
auto& [layerName, indices] : name_to_indices )
572 if( indices.size() > 1 )
575 for(
size_t count = 0; count < indices.size(); ++count )
577 size_t idx = indices[count];
578 wxString newLayerName = wxString::Format(
"%s_%zu",
m_matrixLayers[idx].m_layerName, count + 1 );
581 if( newLayerName.length() > 64 )
585 size_t suffixLen = wxString::Format(
"_%zu", count + 1 ).length();
587 if( suffixLen < baseName.length() )
589 baseName.Truncate( 64 - suffixLen );
590 newLayerName = wxString::Format(
"%s_%zu", baseName, count + 1 );
621 if( layer.m_addType.has_value() )
630 if( layer.m_diType.has_value() )
643 if( layer.m_span.has_value() )
655 std::map<
int, std::vector<BOARD_ITEM*>>& aMap,
656 const PCB_LAYER_ID& aLayerID,
const wxString& aLayerName ) :
660 m_featuresMgr = std::make_unique<FEATURES_MANAGER>( aBoard, aPlugin, aLayerName );
697 std::vector<BOARD_ITEM*>& vec =
m_layerItems[net->GetNetCode()];
699 std::stable_sort( vec.begin(), vec.end(),
702 if( a->GetParentFootprint() == b->GetParentFootprint() )
703 return a->Type() < b->Type();
705 return a->GetParentFootprint() < b->GetParentFootprint();
725 return m_compBot.value().AddComponent( aFp, aPkg );
734 return m_compTop.value().AddComponent( aFp, aPkg );
741 std::map<ODB_DRILL_SPAN, std::vector<BOARD_ITEM*>>& drill_layers =
744 std::map<std::pair<PCB_LAYER_ID, PCB_LAYER_ID>, std::vector<BOARD_ITEM*>>& slot_holes =
747 std::map<ODB_DRILL_SPAN, wxString>& span_names =
m_plugin->GetDrillSpanNameMap();
756 std::optional<ODB_DRILL_SPAN> matchedSpan;
758 for(
const auto& [span,
name] : span_names )
767 bool useLegacyMatching = !matchedSpan.has_value();
768 bool isBackdrillLayer = matchedSpan.has_value() && matchedSpan->m_IsBackdrill;
769 bool isNonPlatedLayer = matchedSpan.has_value() && matchedSpan->m_IsNonPlated;
770 bool isNPTHLayer = matchedSpan.has_value() && matchedSpan->m_IsNonPlated
771 && !matchedSpan->m_IsBackdrill;
773 if( matchedSpan.has_value() && isNPTHLayer )
775 auto slotIt = slot_holes.find( matchedSpan->Pair() );
777 if( slotIt != slot_holes.end() )
789 m_tools.value().AddDrillTools( wxT(
"NON_PLATED" ),
791 std::min(
pad->GetDrillSizeX(),
792 pad->GetDrillSizeY() ) ) );
798 else if( useLegacyMatching )
800 bool is_npth_layer =
false;
801 wxString plated_name = wxT(
"plated" );
805 is_npth_layer =
true;
806 plated_name = wxT(
"non-plated" );
809 for(
const auto& [layer_pair, vec] : slot_holes )
811 wxString dLayerName = wxString::Format( wxT(
"drill_%s_%s-%s" ), plated_name,
812 m_board->GetLayerName( layer_pair.first ),
813 m_board->GetLayerName( layer_pair.second ) );
832 : wxT(
"NON_PLATED" ),
834 std::min(
pad->GetDrillSizeX(),
pad->GetDrillSizeY() ) ) );
844 if( matchedSpan.has_value() )
846 auto drillIt = drill_layers.find( *matchedSpan );
848 if( drillIt != drill_layers.end() )
856 if( isBackdrillLayer )
860 int diameter = secondary.
size.
x;
862 if( secondary.
size.
y > 0 )
864 diameter = ( diameter > 0 ) ? std::min( diameter, secondary.
size.
y )
871 m_tools.value().AddDrillTools( wxT(
"NON_PLATED" ),
875 else if( isNonPlatedLayer )
877 m_tools.value().AddDrillTools( wxT(
"NON_PLATED" ),
882 m_tools.value().AddDrillTools( wxT(
"VIA" ),
894 if( isNPTHLayer && !padIsNPTH )
897 if( !isNonPlatedLayer && padIsNPTH )
900 int drillSize =
pad->GetDrillSizeX();
902 if(
pad->GetDrillSizeX() !=
pad->GetDrillSizeY() )
903 drillSize = std::min(
pad->GetDrillSizeX(),
pad->GetDrillSizeY() );
905 wxString typeLabel = ( padIsNPTH || isNonPlatedLayer ) ? wxT(
"NON_PLATED" )
907 wxString type2 = isBackdrillLayer ? wxT(
"BLIND" ) : wxT(
"STANDARD" );
919 bool is_npth_layer =
false;
920 wxString plated_name = wxT(
"plated" );
924 is_npth_layer =
true;
925 plated_name = wxT(
"non-plated" );
928 for(
const auto& [span, vec] : drill_layers )
930 wxString dLayerName = wxString::Format( wxT(
"drill_%s_%s-%s" ), plated_name,
931 m_board->GetLayerName( span.TopLayer() ),
932 m_board->GetLayerName( span.BottomLayer() ) );
938 if( item->Type() ==
PCB_VIA_T && !is_npth_layer )
942 m_tools.value().AddDrillTools( wxT(
"VIA" ),
959 : wxT(
"NON_PLATED" ),
974 auto& auxilliary_layers =
m_plugin->GetAuxilliaryLayerItemsMap();
981 for(
const auto& [layer_pair, vec] : auxilliary_layers )
983 wxString featureName =
"";
984 switch( std::get<0>( layer_pair ) )
995 bool drill_value =
false;
1000 dLayerName = wxString::Format(
"%s_%s-%s", featureName,
1001 m_board->GetLayerName( std::get<1>( layer_pair ) ),
1002 m_board->GetLayerName( std::get<2>( layer_pair ) ) );
1006 if(
m_board->IsFrontLayer( std::get<1>( layer_pair ) ) )
1007 dLayerName = wxString::Format(
"%s_front", featureName );
1008 else if(
m_board->IsBackLayer( std::get<1>( layer_pair ) ) )
1009 dLayerName = wxString::Format(
"%s_back", featureName );
1040 layer_entity_ptr->InitEntityData();
1069 m_compTop->Write( fileproxy.GetStream() );
1073 m_compBot->Write( fileproxy.GetStream() );
1082 m_featuresMgr->GenerateFeatureFile( fileproxy.GetStream() );
1096 m_tools.value().GenerateFile( fileproxy.GetStream() );
1122 if( fp->IsFlipped() )
1129 wxLogError(
_(
"Failed to add component data" ) );
1135 std::shared_ptr<FOOTPRINT> fp_pkg =
m_edaData.GetEdaFootprints().at( j );
1141 if( fp->Pads().empty() )
1146 for(
int i = 0; i < fp->Pads().size(); ++i )
1148 PAD*
pad = fp->Pads()[i];
1155 comp.m_index,
comp.m_toeprints.size() );
1157 m_plugin->GetPadSubnetMap().emplace(
pad, &subnet );
1161 auto& toep =
comp.m_toeprints.emplace_back( pin_ref );
1163 toep.m_net_num = eda_net.m_index;
1164 toep.m_subnet_num = subnet.m_index;
1169 (
ANGLE_360 -
pad->GetOrientation() ).Normalize().AsDegrees() );
1171 if(
pad->IsFlipped() )
1172 toep.m_mirror = wxT(
"M" );
1174 toep.m_mirror = wxT(
"N" );
1180 auto& eda_net =
m_edaData.GetNet( track->GetNetCode() );
1188 m_plugin->GetViaTraceSubnetMap().emplace( track, subnet );
1195 auto& eda_net =
m_edaData.GetNet( zone->GetNetCode() );
1200 m_plugin->GetPlaneSubnetMap().emplace( std::piecewise_construct,
1201 std::forward_as_tuple( layer, zone ),
1202 std::forward_as_tuple( &subnet ) );
1239 if( !
m_board->GetBoardPolygonOutlines( board_outline,
true ) )
1241 wxLogError(
"Failed to get board outline" );
1244 if( !
m_profile->AddContour( board_outline, 0 ) )
1246 wxLogError(
"Failed to add polygon to profile" );
1249 m_profile->GenerateProfileFeatures( fileproxy.GetStream() );
1261 {
"X_ORIGIN",
"0" },
1262 {
"Y_ORIGIN",
"0" },
1263 {
"TOP_ACTIVE",
"0" },
1264 {
"BOTTOM_ACTIVE",
"0" },
1265 {
"RIGHT_ACTIVE",
"0" },
1266 {
"LEFT_ACTIVE",
"0" },
1267 {
"AFFECTING_BOM",
"" },
1268 {
"AFFECTING_BOM_CHANGED",
"0" },
1273 for(
const auto& [key, value] :
m_stephdr )
1288 layerEntity->GenerateFiles( writer );
1297 m_edaData.Write( fileproxy.GetStream() );
1305 m_netlist.Write( fileproxy.GetStream() );
1317 catch(
const std::exception& e )
1319 std::cerr << e.what() << std::endl;
1332 std::map<PCB_LAYER_ID, std::map<int, std::vector<BOARD_ITEM*>>>& elements =
m_plugin->GetLayerElementsMap();
1334 std::for_each(
m_board->Tracks().begin(),
m_board->Tracks().end(),
1335 [&layers, &elements](
PCB_TRACK* aTrack )
1337 if( aTrack->Type() == PCB_VIA_T )
1339 PCB_VIA* via = static_cast<PCB_VIA*>( aTrack );
1341 for( PCB_LAYER_ID layer : layers )
1343 if( via->FlashLayer( layer ) )
1344 elements[layer][via->GetNetCode()].push_back( via );
1349 elements[aTrack->GetLayer()][aTrack->GetNetCode()].push_back( aTrack );
1353 std::for_each( m_board->Zones().begin(), m_board->Zones().end(),
1354 [&elements](
ZONE* zone )
1356 for( PCB_LAYER_ID layer : zone->GetLayerSet() )
1357 elements[layer][zone->GetNetCode()].push_back( zone );
1360 for(
BOARD_ITEM* item : m_board->Drawings() )
1363 elements[conn_it->GetLayer()][conn_it->GetNetCode()].push_back( conn_it );
1365 elements[item->GetLayer()][0].push_back( item );
1368 for(
FOOTPRINT* fp : m_board->Footprints() )
1370 for(
PCB_FIELD* field : fp->GetFields() )
1371 elements[field->GetLayer()][0].push_back( field );
1373 for(
BOARD_ITEM* item : fp->GraphicalItems() )
1374 elements[item->GetLayer()][0].push_back( item );
1376 for(
PAD*
pad : fp->Pads() )
1386 if( onSolderMaskLayer )
1389 if( onSolderPasteLayer )
1394 if( onCopperLayer && !
pad->IsOnCopperLayer() )
1397 if( onCopperLayer && !
pad->FlashLayer( layer ) )
1401 && ( padPlotsSize.
x <= 0 || padPlotsSize.
y <= 0 ) )
1406 elements[layer][
pad->GetNetCode()].push_back(
pad );
1411 for(
const auto& [layerID, layerName] : m_plugin->GetLayerNameList() )
1413 std::shared_ptr<ODB_LAYER_ENTITY> layer_entity_ptr = std::make_shared<ODB_LAYER_ENTITY>(
1414 m_board, m_plugin, elements[layerID], layerID, layerName );
1416 m_layerEntityMap.emplace( layerName, layer_entity_ptr );
@ BS_ITEM_TYPE_DIELECTRIC
wxString GetBuildVersion()
Get the full KiCad version string.
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
Container for design settings for a BOARD object.
BOARD_STACKUP & GetStackupDescriptor()
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Manage one layer needed to make a physical board.
wxString GetTypeName() const
int GetSublayersCount() const
wxString GetLayerName() const
PCB_LAYER_ID GetBrdLayerId() const
BOARD_STACKUP_ITEM_TYPE GetType() const
int GetDielectricLayerId() const
Manage layers needed to make a physical board.
const std::vector< BOARD_STACKUP_ITEM * > & GetList() const
bool SynchronizeWithBoard(BOARD_DESIGN_SETTINGS *aSettings)
Synchronize the BOARD_STACKUP_ITEM* list with the board.
Information pertinent to a Pcbnew printed circuit board.
const std::shared_ptr< PIN > GetEdaPkgPin(size_t aPadIndex) const
KICAD_T Type() const
Returns the type of object.
LSET is a set of PCB_LAYER_IDs.
static LSET AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Handle the data for a net.
Container for NETINFO_ITEM elements, which are the nets.
virtual std::string GetEntityName()=0
virtual bool CreateDirectoryTree(ODB_TREE_WRITER &writer)
ODB_ENTITY_BASE(BOARD *aBoard, PCB_IO_ODBPP *aPlugin)
void GenFeatures(ODB_TREE_WRITER &writer)
void GenComponents(ODB_TREE_WRITER &writer)
wxString m_matrixLayerName
void InitAuxilliaryData()
std::optional< COMPONENTS_MANAGER > m_compTop
virtual void GenerateFiles(ODB_TREE_WRITER &writer) override
std::optional< COMPONENTS_MANAGER > m_compBot
std::optional< ODB_DRILL_TOOLS > m_tools
void GenTools(ODB_TREE_WRITER &writer)
std::map< int, std::vector< BOARD_ITEM * > > m_layerItems
ODB_LAYER_ENTITY(BOARD *aBoard, PCB_IO_ODBPP *aPlugin, std::map< int, std::vector< BOARD_ITEM * > > &aMap, const PCB_LAYER_ID &aLayerID, const wxString &aLayerName)
ODB_COMPONENT & InitComponentData(const FOOTPRINT *aFp, const EDA_DATA::PACKAGE &aPkg)
virtual void InitEntityData() override
void GenAttrList(ODB_TREE_WRITER &writer)
std::unique_ptr< FEATURES_MANAGER > m_featuresMgr
void AddStep(const wxString &aStepName)
void InitMatrixLayerData()
std::vector< MATRIX_LAYER > m_matrixLayers
void AddMatrixLayerField(MATRIX_LAYER &aMLayer, PCB_LAYER_ID aLayer)
virtual void GenerateFiles(ODB_TREE_WRITER &writer) override
std::map< wxString, unsigned int > m_matrixSteps
void AddAuxilliaryMatrixLayer()
void EnsureUniqueLayerNames()
void AddDrillMatrixLayer()
virtual void InitEntityData() override
void AddCOMPMatrixLayer(PCB_LAYER_ID aCompSide)
std::vector< std::pair< wxString, wxString > > m_info
virtual void GenerateFiles(ODB_TREE_WRITER &writer) override
virtual std::string GetEntityName() override
std::unordered_map< wxString, wxString > m_stephdr
void GenerateProfileFile(ODB_TREE_WRITER &writer)
void GenerateStepHeaderFile(ODB_TREE_WRITER &writer)
void GenerateEdaFiles(ODB_TREE_WRITER &writer)
virtual void InitEntityData() override
void GenerateLayerFiles(ODB_TREE_WRITER &writer)
std::map< wxString, std::shared_ptr< ODB_LAYER_ENTITY > > m_layerEntityMap
std::unique_ptr< FEATURES_MANAGER > m_profile
virtual bool CreateDirectoryTree(ODB_TREE_WRITER &writer) override
void GenerateNetlistsFiles(ODB_TREE_WRITER &writer)
virtual void GenerateFiles(ODB_TREE_WRITER &writer) override
void WriteEquationLine(const std::string &var, int value)
ARRAY_PROXY MakeArrayProxy(const std::string &aStr)
void write_line_enum(const std::string &var, const T &value)
const wxString GetRootPath() const
void CreateEntityDirectory(const wxString &aPareDir, const wxString &aSubDir=wxEmptyString)
ODB_FILE_WRITER CreateFileProxy(const wxString &aFileName)
void SetCurrentPath(const wxString &aDir)
const wxString GetCurrentPath() const
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
static std::string m_unitsStr
Represent a set of closed polygons.
Handle a list of polygons defining a copper zone.
static constexpr EDA_ANGLE ANGLE_360
size_t hash_fp_item(const EDA_ITEM *aItem, int aFlags)
Calculate hash of an EDA_ITEM.
Hashing functions for EDA_ITEMs.
@ REL_COORD
Use coordinates relative to the parent object.
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
PCB_LAYER_ID
A quick note on layer IDs:
bool IsValidLayer(int aLayerId)
Test whether a given integer is a valid layer index, i.e.
std::pair< wxString, wxString > AddXY(const VECTOR2I &aVec)
wxString GenLegalEntityName(const wxString &aStr)
wxString Double2String(double aVal)
wxString SymDouble2String(double aVal)
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
@ PTH
Plated through hole pad.
std::optional< ODB_SUBTYPE > m_addType
std::optional< ODB_DIELECTRIC_TYPE > m_diType
std::optional< std::pair< wxString, wxString > > m_span
! The properties of a padstack drill. Drill position is always the pad position (origin).
VECTOR2I size
Drill diameter (x == y) or slot dimensions (x != y)
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
@ PCB_PAD_T
class PAD, a pad in a footprint
VECTOR2< int32_t > VECTOR2I