69 if( fs::is_regular_file( aPath ) && aPath.extension() ==
".kicad_pcb" )
71 aOut.push_back( aPath );
73 else if( fs::is_directory( aPath ) )
75 for(
const auto& entry : fs::recursive_directory_iterator( aPath ) )
77 if( entry.is_regular_file() && entry.path().extension() ==
".kicad_pcb" )
78 aOut.push_back( entry.path() );
86 std::string outputDir =
".";
87 std::vector<std::string> inputs;
89 for(
int i = 1; i < argc; i++ )
91 std::string arg = argv[i];
93 if( arg ==
"--output-dir" && i + 1 < argc )
95 outputDir = argv[++i];
99 inputs.push_back( arg );
105 std::cerr <<
"Usage: qa_pcbnew_tools extract_zone_fills [--output-dir <dir>] "
106 "<board_or_dir> [board_or_dir ...]\n";
110 fs::create_directories( outputDir );
112 std::vector<fs::path> boardFiles;
114 for(
const auto& input : inputs )
117 std::cout <<
"Found " << boardFiles.size() <<
" board files\n";
120 int boardsWithData = 0;
122 for(
const auto& boardPath : boardFiles )
124 std::unique_ptr<BOARD> board;
130 catch(
const std::exception& e )
132 std::cerr <<
"Error loading " << boardPath <<
": " << e.what() <<
"\n";
138 std::cerr <<
"Failed to load: " << boardPath <<
"\n";
142 std::ostringstream fileContent;
143 int zonesInBoard = 0;
144 std::string boardName = boardPath.stem().string();
146 fileContent <<
"(zone_fills\n";
147 fileContent <<
" (source \"" << boardPath.filename().string() <<
"\")\n";
149 for(
ZONE* zone : board->Zones() )
151 if( zone->GetIsRuleArea() )
156 auto poly = zone->GetFilledPolysList( layer );
158 if( !poly || poly->OutlineCount() == 0 )
161 fileContent <<
" (zone (layer \"" <<
LayerName( layer ).ToStdString() <<
"\")";
162 fileContent <<
" (net \"" << zone->GetNetname().ToStdString() <<
"\")";
163 fileContent <<
" (outline_count " << poly->OutlineCount() <<
")";
167 for(
int i = 0; i < poly->OutlineCount(); i++ )
169 const auto& polygon = poly->CPolygon( i );
172 vertexCount +=
chain.PointCount();
175 fileContent <<
" (vertex_count " << vertexCount <<
")\n";
176 fileContent <<
" (polyset\n";
178 fileContent <<
" )\n";
179 fileContent <<
" )\n";
185 fileContent <<
")\n";
187 if( zonesInBoard > 0 )
189 fs::path outPath = fs::path( outputDir ) / ( boardName +
".kicad_polys" );
190 std::ofstream out( outPath );
191 out << fileContent.str();
194 std::cout << boardPath.filename() <<
" -> " << zonesInBoard <<
" zones\n";
200 std::cout <<
"\nExtracted " <<
totalZones <<
" zones from "
201 << boardsWithData <<
" boards\n";
std::unique_ptr< BOARD > ReadBoardFromFileOrStream(const std::string &aFilename, std::istream &aFallback)
Read a board from a file, or another stream, as appropriate.