From fa31d5308ac63f7575a8adc20065592597184b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20W=C5=82ostowski?= Date: Wed, 29 Nov 2017 20:06:22 +0100 Subject: [PATCH] Sample tool for generating board geometyr as polygons --- qa/CMakeLists.txt | 3 +- qa/polygon_generator/CMakeLists.txt | 92 ++++++++++++++ .../test_polygon_generator.cpp | 112 ++++++++++++++++++ 3 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 qa/polygon_generator/CMakeLists.txt create mode 100644 qa/polygon_generator/test_polygon_generator.cpp diff --git a/qa/CMakeLists.txt b/qa/CMakeLists.txt index 01b2033201..7bbfcbee52 100644 --- a/qa/CMakeLists.txt +++ b/qa/CMakeLists.txt @@ -12,4 +12,5 @@ endif() add_subdirectory( geometry ) add_subdirectory( pcb_test_window ) -add_subdirectory( polygon_triangulation ) \ No newline at end of file +add_subdirectory( polygon_triangulation ) +add_subdirectory( polygon_generator ) \ No newline at end of file diff --git a/qa/polygon_generator/CMakeLists.txt b/qa/polygon_generator/CMakeLists.txt new file mode 100644 index 0000000000..6c600f5227 --- /dev/null +++ b/qa/polygon_generator/CMakeLists.txt @@ -0,0 +1,92 @@ +# +# This program source code file is part of KiCad, a free EDA CAD application. +# +# Copyright (C) 2017 CERN +# @author Alejandro GarcĂ­a Montoro +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, you may find one here: +# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html +# or you may search the http://www.gnu.org website for the version 2 license, +# or you may write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + +#find_package(Boost COMPONENTS unit_test_framework REQUIRED) +#find_package( wxWidgets 3.0.0 COMPONENTS gl aui adv html core net base xml stc REQUIRED ) + +add_definitions(-DPCBNEW -DBOOST_TEST_DYN_LINK) + +if( BUILD_GITHUB_PLUGIN ) + set( GITHUB_PLUGIN_LIBRARIES github_plugin ) +endif() + +add_dependencies( pnsrouter pcbcommon pcad2kicadpcb ${GITHUB_PLUGIN_LIBRARIES} ) + +add_executable(test_polygon_generator + ../common/mocks.cpp + ../../common/base_units.cpp + test_polygon_generator.cpp +) + +include_directories( BEFORE ${INC_BEFORE} ) +include_directories( + ${CMAKE_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/include + ${CMAKE_SOURCE_DIR}/3d-viewer + ${CMAKE_SOURCE_DIR}/common + ${CMAKE_SOURCE_DIR}/pcbnew + ${CMAKE_SOURCE_DIR}/pcbnew/router + ${CMAKE_SOURCE_DIR}/pcbnew/tools + ${CMAKE_SOURCE_DIR}/pcbnew/dialogs + ${CMAKE_SOURCE_DIR}/polygon + ${CMAKE_SOURCE_DIR}/common/geometry + ${CMAKE_SOURCE_DIR}/qa/common + ${Boost_INCLUDE_DIR} + ${INC_AFTER} +) + +target_link_libraries( test_polygon_generator + polygon + pnsrouter + common + pcbcommon + bitmaps + polygon + pnsrouter + common + pcbcommon + bitmaps + polygon + pnsrouter + common + pcbcommon + bitmaps + polygon + pnsrouter + common + pcbcommon + bitmaps + gal + pcad2kicadpcb + common + pcbcommon + ${GITHUB_PLUGIN_LIBRARIES} + common + pcbcommon + ${Boost_FILESYSTEM_LIBRARY} + ${Boost_SYSTEM_LIBRARY} + ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} + ${wxWidgets_LIBRARIES} +) + + diff --git a/qa/polygon_generator/test_polygon_generator.cpp b/qa/polygon_generator/test_polygon_generator.cpp new file mode 100644 index 0000000000..caec0cb03e --- /dev/null +++ b/qa/polygon_generator/test_polygon_generator.cpp @@ -0,0 +1,112 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2017 CERN + * @author Tomasz Wlostowski + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +BOARD* loadBoard( const std::string& filename ) +{ + PLUGIN::RELEASER pi( new PCB_IO ); + BOARD* brd = nullptr; + + try + { + brd = pi->Load( wxString( filename.c_str() ), NULL, NULL ); + } + catch( const IO_ERROR& ioe ) + { + wxString msg = wxString::Format( _( "Error loading board.\n%s" ), + ioe.Problem() ); + + printf( "%s\n", (const char*) msg.mb_str() ); + return nullptr; + } + + return brd; +} + + +void process( const BOARD_CONNECTED_ITEM* item, int net ) +{ + if( item->GetNetCode() != net ) + return; + + SHAPE_POLY_SET pset; + + const int segsPerCircle = 64; + + double correctionFactor = 1.0 / cos( M_PI / (double) segsPerCircle ); + + + item->TransformShapeWithClearanceToPolygon( pset, 1, segsPerCircle, correctionFactor ); + + SHAPE_FILE_IO shapeIo; // default = stdout + shapeIo.Write( &pset ); +} + + +int main( int argc, char* argv[] ) +{ + if( argc < 2 ) + { + printf( "A sample tool for dumping board geometry as a set of polygons.\n" ); + printf( "usage : %s board_file.kicad_pcb\n\n", argv[0] ); + return -1; + } + + std::unique_ptr brd( loadBoard( argv[1] ) ); + + if( !brd ) + return -1; + + for( int net = 0; net < brd->GetNetCount(); net++ ) + { + printf( "net %d\n", net ); + + for( auto track : brd->Tracks() ) + process( track, net ); + + for( auto mod : brd->Modules() ) + { + for( auto pad : mod->Pads() ) + process( pad, net ); + } + + for( auto zone : brd->Zones() ) + process( zone, net ); + + printf( "endnet\n" ); + } +}