1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2011 Wayne Stambaugh <stambaughw@gmail.com>
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 2022 CERN
*
* 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
*/
/// @todo The Boost entropy exception does not exist prior to 1.67. Once the minimum Boost
/// version is raise to 1.67 or greater, this version check can be removed.
#include <boost/version.hpp>
#if BOOST_VERSION >= 106700
#include <boost/uuid/entropy_error.hpp>
#endif
#include <3d_viewer/eda_3d_viewer_frame.h> // To include VIEWER3D_FRAMENAME
#include <advanced_config.h>
#include <base_units.h>
#include <board.h>
#include <cleanup_item.h>
#include <collectors.h>
#include <confirm.h>
#include <footprint.h>
#include <footprint_editor_settings.h>
#include <fp_lib_table.h>
#include <lset.h>
#include <kiface_base.h>
#include <pcb_painter.h>
#include <pcbnew_id.h>
#include <pcbnew_settings.h>
#include <pcb_base_frame.h>
#include <pcb_draw_panel_gal.h>
#include <pgm_base.h>
#include <project_pcb.h>
#include <wildcards_and_files_ext.h>
#include <zoom_defines.h>
#include <math/vector2d.h>
#include <math/vector2wx.h>
#include <widgets/msgpanel.h>
#include <wx/fswatcher.h>
#include <settings/settings_manager.h>
#include <settings/cvpcb_settings.h>
#include <tool/tool_manager.h>
#include <tool/tool_dispatcher.h>
#include <tools/pcb_actions.h>
#include <tool/grid_menu.h>
#include <ratsnest/ratsnest_view_item.h>
#include <navlib/nl_pcbnew_plugin.h>
using KIGFX::RENDER_SETTINGS;
using KIGFX::PCB_RENDER_SETTINGS;
wxDEFINE_EVENT( EDA_EVT_BOARD_CHANGED, wxCommandEvent );
PCB_BASE_FRAME::PCB_BASE_FRAME( KIWAY* aKiway, wxWindow* aParent, FRAME_T aFrameType,
const wxString& aTitle, const wxPoint& aPos, const wxSize& aSize,
long aStyle, const wxString& aFrameName ) :
EDA_DRAW_FRAME( aKiway, aParent, aFrameType, aTitle, aPos, aSize, aStyle, aFrameName,
pcbIUScale ),
m_pcb( nullptr ),
m_originTransforms( *this )
{
m_watcherDebounceTimer.Bind( wxEVT_TIMER, &PCB_BASE_FRAME::OnFpChangeDebounceTimer, this );
}
PCB_BASE_FRAME::~PCB_BASE_FRAME()
{
// Ensure m_canvasType is up to date, to save it in config
if( GetCanvas() )
m_canvasType = GetCanvas()->GetBackend();
delete m_pcb;
m_pcb = nullptr;
}
bool PCB_BASE_FRAME::canCloseWindow( wxCloseEvent& aEvent )
{
// Close modeless dialogs. They're trouble when they get destroyed after the frame and/or
// board.
wxWindow* viewer3D = Get3DViewerFrame();
if( viewer3D )
viewer3D->Close( true );
// Similarly, wxConvBrokenFileNames uses some statically allocated variables that make it
// crash when run later from a d'tor.
PROJECT_PCB::Cleanup3DCache( &Prj() );
return true;
}
void PCB_BASE_FRAME::handleActivateEvent( wxActivateEvent& aEvent )
{
EDA_DRAW_FRAME::handleActivateEvent( aEvent );
if( m_spaceMouse )
m_spaceMouse->SetFocus( aEvent.GetActive() );
}
void PCB_BASE_FRAME::handleIconizeEvent( wxIconizeEvent& aEvent )
{
EDA_DRAW_FRAME::handleIconizeEvent( aEvent );
if( m_spaceMouse && aEvent.IsIconized() )
m_spaceMouse->SetFocus( false );
}
EDA_3D_VIEWER_FRAME* PCB_BASE_FRAME::Get3DViewerFrame()
{
wxWindow* frame = FindWindowByName( QUALIFIED_VIEWER3D_FRAMENAME( this ) );
return dynamic_cast<EDA_3D_VIEWER_FRAME*>( frame );
}
void PCB_BASE_FRAME::Update3DView( bool aMarkDirty, bool aRefresh, const wxString* aTitle )
{
EDA_3D_VIEWER_FRAME* draw3DFrame = Get3DViewerFrame();
if( draw3DFrame )
{
if( aTitle )
draw3DFrame->SetTitle( *aTitle );
if( aMarkDirty )
draw3DFrame->ReloadRequest();
if( aRefresh )
draw3DFrame->Redraw();
}
}
void PCB_BASE_FRAME::SetBoard( BOARD* aBoard, PROGRESS_REPORTER* aReporter )
{
if( m_pcb != aBoard )
{
delete m_pcb;
m_pcb = aBoard;
if( GetBoard() )
GetBoard()->SetUserUnits( GetUserUnits() );
if( GetBoard() && GetCanvas() )
{
RENDER_SETTINGS* rs = GetCanvas()->GetView()->GetPainter()->GetSettings();
if( rs )
{
rs->SetDashLengthRatio( GetBoard()->GetPlotOptions().GetDashedLineDashRatio() );
rs->SetGapLengthRatio( GetBoard()->GetPlotOptions().GetDashedLineGapRatio() );
}
}
wxCommandEvent e( EDA_EVT_BOARD_CHANGED );
ProcessEventLocally( e );
for( wxEvtHandler* listener : m_boardChangeListeners )
{
wxCHECK2( listener, continue );
// Use the windows variant when handling event messages in case there is any special
// event handler pre and/or post processing specific to windows.
wxWindow* win = dynamic_cast<wxWindow*>( listener );
if( win )
win->HandleWindowEvent( e );
else
listener->SafelyProcessEvent( e );
}
}
}
void PCB_BASE_FRAME::AddBoardChangeListener( wxEvtHandler* aListener )
{
auto it = std::find( m_boardChangeListeners.begin(), m_boardChangeListeners.end(), aListener );
// Don't add duplicate listeners.
if( it == m_boardChangeListeners.end() )
m_boardChangeListeners.push_back( aListener );
}
void PCB_BASE_FRAME::RemoveBoardChangeListener( wxEvtHandler* aListener )
{
auto it = std::find( m_boardChangeListeners.begin(), m_boardChangeListeners.end(), aListener );
// Don't add duplicate listeners.
if( it != m_boardChangeListeners.end() )
m_boardChangeListeners.erase( it );
}
void PCB_BASE_FRAME::AddFootprintToBoard( FOOTPRINT* aFootprint )
{
if( aFootprint )
{
GetBoard()->Add( aFootprint, ADD_MODE::APPEND );
aFootprint->SetFlags( IS_NEW );
aFootprint->SetPosition( VECTOR2I( 0, 0 ) ); // cursor in GAL may not be initialized yet
// Put it on FRONT layer (note that it might be stored flipped if the lib is an archive
// built from a board)
if( aFootprint->IsFlipped() )
aFootprint->Flip( aFootprint->GetPosition(), GetPcbNewSettings()->m_FlipDirection );
// Place it in orientation 0 even if it is not saved with orientation 0 in lib (note that
// it might be stored in another orientation if the lib is an archive built from a board)
aFootprint->SetOrientation( ANGLE_0 );
GetBoard()->UpdateUserUnits( aFootprint, GetCanvas()->GetView() );
}
}
EDA_ITEM* PCB_BASE_FRAME::GetItem( const KIID& aId ) const
{
return GetBoard()->GetItem( aId );
}
void PCB_BASE_FRAME::FocusOnItem( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer )
{
std::vector<BOARD_ITEM*> items;
if( aItem )
items.push_back( aItem );
FocusOnItems( items, aLayer );
}
void PCB_BASE_FRAME::FocusOnItems( std::vector<BOARD_ITEM*> aItems, PCB_LAYER_ID aLayer )
{
static std::vector<KIID> lastBrightenedItemIDs;
BOARD_ITEM* lastItem = nullptr;
for( KIID lastBrightenedItemID : lastBrightenedItemIDs )
{
/// @todo The Boost entropy exception does not exist prior to 1.67. Once the minimum Boost
/// version is raise to 1.67 or greater, this version check can be removed.
#if BOOST_VERSION >= 106700
try
{
lastItem = GetBoard()->GetItem( lastBrightenedItemID );
}
catch( const boost::uuids::entropy_error& )
{
wxLogError( wxT( "A Boost UUID entropy exception was thrown in %s:%s." ),
__FILE__, __FUNCTION__ );
}
#else
lastItem = GetBoard()->GetItem( lastBrightenedItemID );
#endif
if( lastItem && lastItem != DELETED_BOARD_ITEM::GetInstance() )
{
lastItem->ClearBrightened();
lastItem->RunOnDescendants(
[&]( BOARD_ITEM* child )
{
child->ClearBrightened();
} );
GetCanvas()->GetView()->Update( lastItem );
lastBrightenedItemID = niluuid;
GetCanvas()->Refresh();
}
}
lastBrightenedItemIDs.clear();
if( aItems.empty() )
return;
VECTOR2I focusPt;
KIGFX::VIEW* view = GetCanvas()->GetView();
SHAPE_POLY_SET viewportPoly( view->GetViewport() );
for( wxWindow* dialog : findDialogs() )
{
wxPoint dialogPos = GetCanvas()->ScreenToClient( dialog->GetScreenPosition() );
SHAPE_POLY_SET dialogPoly( BOX2D( view->ToWorld( ToVECTOR2D( dialogPos ), true ),
view->ToWorld( ToVECTOR2D( dialog->GetSize() ), false ) ) );
try
{
viewportPoly.BooleanSubtract( dialogPoly );
}
catch( const std::exception& e )
{
wxFAIL_MSG( wxString::Format( wxT( "Clipper exception occurred: %s" ), e.what() ) );
}
}
SHAPE_POLY_SET itemPoly, clippedPoly;
for( BOARD_ITEM* item : aItems )
{
if( item && item != DELETED_BOARD_ITEM::GetInstance() )
{
item->SetBrightened();
item->RunOnDescendants(
[&]( BOARD_ITEM* child )
{
child->SetBrightened();
});
GetCanvas()->GetView()->Update( item );
lastBrightenedItemIDs.push_back( item->m_Uuid );
// Focus on the object's location. Prefer a visible part of the object to its anchor
// in order to keep from scrolling around.
focusPt = item->GetPosition();
if( aLayer == UNDEFINED_LAYER && item->GetLayerSet().any() )
aLayer = item->GetLayerSet().Seq()[0];
switch( item->Type() )
{
case PCB_FOOTPRINT_T:
try
{
itemPoly = static_cast<FOOTPRINT*>( item )->GetBoundingHull();
}
catch( const std::exception& e )
{
wxFAIL_MSG( wxString::Format( wxT( "Clipper exception occurred: %s" ),
e.what() ) );
}
break;
case PCB_PAD_T:
case PCB_MARKER_T:
case PCB_VIA_T:
FocusOnLocation( item->GetFocusPosition() );
GetCanvas()->Refresh();
return;
case PCB_SHAPE_T:
case PCB_FIELD_T:
case PCB_TEXT_T:
case PCB_TEXTBOX_T:
case PCB_TRACE_T:
case PCB_ARC_T:
case PCB_DIM_ALIGNED_T:
case PCB_DIM_LEADER_T:
case PCB_DIM_CENTER_T:
case PCB_DIM_RADIAL_T:
case PCB_DIM_ORTHOGONAL_T:
item->TransformShapeToPolygon( itemPoly, aLayer, 0, pcbIUScale.mmToIU( 0.1 ),
ERROR_INSIDE );
break;
case PCB_ZONE_T:
{
ZONE* zone = static_cast<ZONE*>( item );
#if 0
// Using the filled area shapes to find a Focus point can give good results, but
// unfortunately the calculations are highly time consuming, even for not very
// large areas (can be easily a few minutes for large areas).
// so we used only the zone outline that usually do not have too many vertices.
zone->TransformShapeToPolygon( itemPoly, aLayer, 0, pcbIUScale.mmToIU( 0.1 ),
ERROR_INSIDE );
if( itemPoly.IsEmpty() )
itemPoly = *zone->Outline();
#else
// much faster calculation time when using only the zone outlines
itemPoly = *zone->Outline();
#endif
break;
}
default:
{
BOX2I item_bbox = item->GetBoundingBox();
itemPoly.NewOutline();
itemPoly.Append( item_bbox.GetOrigin() );
itemPoly.Append( item_bbox.GetOrigin() + VECTOR2I( item_bbox.GetWidth(), 0 ) );
itemPoly.Append( item_bbox.GetOrigin() + VECTOR2I( 0, item_bbox.GetHeight() ) );
itemPoly.Append( item_bbox.GetOrigin() + VECTOR2I( item_bbox.GetWidth(),
item_bbox.GetHeight() ) );
break;
}
}
try
{
clippedPoly.BooleanIntersection( itemPoly, viewportPoly );
}
catch( const std::exception& e )
{
wxFAIL_MSG( wxString::Format( wxT( "Clipper exception occurred: %s" ), e.what() ) );
}
if( !clippedPoly.IsEmpty() )
itemPoly = clippedPoly;
}
}
/*
* Perform a step-wise deflate to find the visual-center-of-mass
*/
BOX2I bbox = itemPoly.BBox();
int step = std::min( bbox.GetWidth(), bbox.GetHeight() ) / 10;
while( !itemPoly.IsEmpty() )
{
focusPt = itemPoly.BBox().Centre();
try
{
itemPoly.Deflate( step, CORNER_STRATEGY::ALLOW_ACUTE_CORNERS, ARC_LOW_DEF );
}
catch( const std::exception& e )
{
wxFAIL_MSG( wxString::Format( wxT( "Clipper exception occurred: %s" ), e.what() ) );
}
}
FocusOnLocation( focusPt );
GetCanvas()->Refresh();
}
void PCB_BASE_FRAME::HideSolderMask()
{
KIGFX::PCB_VIEW* view = GetCanvas()->GetView();
if( view && GetBoard()->m_SolderMaskBridges && view->HasItem( GetBoard()->m_SolderMaskBridges ) )
view->Remove( GetBoard()->m_SolderMaskBridges );
}
void PCB_BASE_FRAME::ShowSolderMask()
{
KIGFX::PCB_VIEW* view = GetCanvas()->GetView();
if( view && GetBoard()->m_SolderMaskBridges )
{
if( view->HasItem( GetBoard()->m_SolderMaskBridges ) )
view->Remove( GetBoard()->m_SolderMaskBridges );
view->Add( GetBoard()->m_SolderMaskBridges );
}
}
void PCB_BASE_FRAME::SetPageSettings( const PAGE_INFO& aPageSettings )
{
m_pcb->SetPageSettings( aPageSettings );
if( GetScreen() )
GetScreen()->InitDataPoints( aPageSettings.GetSizeIU( pcbIUScale.IU_PER_MILS ) );
}
const PAGE_INFO& PCB_BASE_FRAME::GetPageSettings() const
{
return m_pcb->GetPageSettings();
}
const VECTOR2I PCB_BASE_FRAME::GetPageSizeIU() const
{
// this function is only needed because EDA_DRAW_FRAME is not compiled
// with either -DPCBNEW or -DEESCHEMA, so the virtual is used to route
// into an application specific source file.
return m_pcb->GetPageSettings().GetSizeIU( pcbIUScale.IU_PER_MILS );
}
const VECTOR2I& PCB_BASE_FRAME::GetGridOrigin() const
{
return m_pcb->GetDesignSettings().GetGridOrigin();
}
void PCB_BASE_FRAME::SetGridOrigin( const VECTOR2I& aPoint )
{
m_pcb->GetDesignSettings().SetGridOrigin( aPoint );
}
const VECTOR2I& PCB_BASE_FRAME::GetAuxOrigin() const
{
return m_pcb->GetDesignSettings().GetAuxOrigin();
}
const VECTOR2I PCB_BASE_FRAME::GetUserOrigin() const
{
VECTOR2I origin( 0, 0 );
switch( GetPcbNewSettings()->m_Display.m_DisplayOrigin )
{
case PCB_DISPLAY_ORIGIN::PCB_ORIGIN_PAGE: break;
case PCB_DISPLAY_ORIGIN::PCB_ORIGIN_AUX: origin = GetAuxOrigin(); break;
case PCB_DISPLAY_ORIGIN::PCB_ORIGIN_GRID: origin = GetGridOrigin(); break;
default: wxASSERT( false ); break;
}
return origin;
}
ORIGIN_TRANSFORMS& PCB_BASE_FRAME::GetOriginTransforms()
{
return m_originTransforms;
}
const TITLE_BLOCK& PCB_BASE_FRAME::GetTitleBlock() const
{
return m_pcb->GetTitleBlock();
}
void PCB_BASE_FRAME::SetTitleBlock( const TITLE_BLOCK& aTitleBlock )
{
m_pcb->SetTitleBlock( aTitleBlock );
}
BOARD_DESIGN_SETTINGS& PCB_BASE_FRAME::GetDesignSettings() const
{
return m_pcb->GetDesignSettings();
}
void PCB_BASE_FRAME::SetDrawBgColor( const COLOR4D& aColor )
{
m_drawBgColor= aColor;
m_auimgr.Update();
}
const PCB_PLOT_PARAMS& PCB_BASE_FRAME::GetPlotSettings() const
{
return m_pcb->GetPlotOptions();
}
void PCB_BASE_FRAME::SetPlotSettings( const PCB_PLOT_PARAMS& aSettings )
{
m_pcb->SetPlotOptions( aSettings );
// Plot Settings can also change the "tent vias" setting, which can affect the solder mask.
LSET visibleLayers = GetBoard()->GetVisibleLayers();
if( visibleLayers.test( F_Mask ) || visibleLayers.test( B_Mask ) )
{
GetCanvas()->GetView()->UpdateAllItemsConditionally(
[&]( KIGFX::VIEW_ITEM* aItem ) -> int
{
BOARD_ITEM* item = nullptr;
if( aItem->IsBOARD_ITEM() )
item = static_cast<BOARD_ITEM*>( aItem );
// Note: KIGFX::REPAINT isn't enough for things that go from invisible to
// visible as they won't be found in the view layer's itemset for re-painting.
if( item && item->Type() == PCB_VIA_T )
return KIGFX::ALL;
return 0;
} );
GetCanvas()->Refresh();
}
}
BOX2I PCB_BASE_FRAME::GetBoardBoundingBox( bool aBoardEdgesOnly ) const
{
BOX2I area = aBoardEdgesOnly ? m_pcb->GetBoardEdgesBoundingBox() : m_pcb->GetBoundingBox();
if( area.GetWidth() == 0 && area.GetHeight() == 0 )
{
VECTOR2I pageSize = GetPageSizeIU();
if( m_showBorderAndTitleBlock )
{
area.SetOrigin( 0, 0 );
area.SetEnd( pageSize.x, pageSize.y );
}
else
{
area.SetOrigin( -pageSize.x / 2, -pageSize.y / 2 );
area.SetEnd( pageSize.x / 2, pageSize.y / 2 );
}
}
return area;
}
const BOX2I PCB_BASE_FRAME::GetDocumentExtents( bool aIncludeAllVisible ) const
{
/* "Zoom to Fit" calls this with "aIncludeAllVisible" as true. Since that feature
* always ignored the page and border, this function returns a bbox without them
* as well when passed true. This technically is not all things visible, but it
* keeps behavior consistent.
*
* When passed false, this function returns a bbox of just the board edge. This
* allows things like fabrication text or anything else outside the board edge to
* be ignored, and just zooms up to the board itself.
*
* Calling "GetBoardBoundingBox(true)" when edge cuts are turned off will return
* the entire page and border, so we call "GetBoardBoundingBox(false)" instead.
*/
if( aIncludeAllVisible || !m_pcb->IsLayerVisible( Edge_Cuts ) )
return GetBoardBoundingBox( false );
else
return GetBoardBoundingBox( true );
}
// Virtual function
void PCB_BASE_FRAME::doReCreateMenuBar()
{
}
void PCB_BASE_FRAME::ShowChangedLanguage()
{
// call my base class
EDA_DRAW_FRAME::ShowChangedLanguage();
// tooltips in toolbars
RecreateToolbars();
EDA_3D_VIEWER_FRAME* viewer3D = Get3DViewerFrame();
if( viewer3D )
viewer3D->ShowChangedLanguage();
}
EDA_3D_VIEWER_FRAME* PCB_BASE_FRAME::CreateAndShow3D_Frame()
{
EDA_3D_VIEWER_FRAME* draw3DFrame = Get3DViewerFrame();
if( !draw3DFrame )
draw3DFrame = new EDA_3D_VIEWER_FRAME( &Kiway(), this, _( "3D Viewer" ) );
// Raising the window does not show the window on Windows if iconized. This should work
// on any platform.
if( draw3DFrame->IsIconized() )
draw3DFrame->Iconize( false );
draw3DFrame->Raise();
draw3DFrame->Show( true );
// Raising the window does not set the focus on Linux. This should work on any platform.
if( wxWindow::FindFocus() != draw3DFrame )
draw3DFrame->SetFocus();
// Allocate a slice of time to display the 3D frame
// a call to wxSafeYield() should be enough (and better), but on Linux we need
// to call wxYield()
// otherwise the activity messages are not displayed during the first board loading
wxYield();
// Note, the caller is responsible to load/update the board 3D view.
// after frame creation the board is not automatically created.
return draw3DFrame;
}
void PCB_BASE_FRAME::SwitchLayer( PCB_LAYER_ID layer )
{
PCB_LAYER_ID preslayer = GetActiveLayer();
auto& displ_opts = GetDisplayOptions();
// Check if the specified layer matches the present layer
if( layer == preslayer )
return;
// Copper layers cannot be selected unconditionally; how many of those layers are
// currently enabled needs to be checked.
if( IsCopperLayer( layer ) )
{
if( layer > m_pcb->GetCopperLayerStackMaxId() )
return;
}
// Is yet more checking required? E.g. when the layer to be selected is a non-copper
// layer, or when switching between a copper layer and a non-copper layer, or vice-versa?
// ...
SetActiveLayer( layer );
if( displ_opts.m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL )
GetCanvas()->Refresh();
}
GENERAL_COLLECTORS_GUIDE PCB_BASE_FRAME::GetCollectorsGuide()
{
GENERAL_COLLECTORS_GUIDE guide( m_pcb->GetVisibleLayers(), GetActiveLayer(),
GetCanvas()->GetView() );
// account for the globals
guide.SetIgnoreFPTextOnBack( !m_pcb->IsElementVisible( LAYER_FP_TEXT ) );
guide.SetIgnoreFPTextOnFront( !m_pcb->IsElementVisible( LAYER_FP_TEXT ) );
guide.SetIgnoreFootprintsOnBack( !m_pcb->IsElementVisible( LAYER_FOOTPRINTS_BK ) );
guide.SetIgnoreFootprintsOnFront( !m_pcb->IsElementVisible( LAYER_FOOTPRINTS_FR ) );
guide.SetIgnoreThroughHolePads( ! m_pcb->IsElementVisible( LAYER_PADS ) );
guide.SetIgnoreFPValues( !m_pcb->IsElementVisible( LAYER_FP_VALUES ) );
guide.SetIgnoreFPReferences( !m_pcb->IsElementVisible( LAYER_FP_REFERENCES ) );
guide.SetIgnoreThroughVias( ! m_pcb->IsElementVisible( LAYER_VIAS ) );
guide.SetIgnoreBlindBuriedVias( ! m_pcb->IsElementVisible( LAYER_VIAS ) );
guide.SetIgnoreMicroVias( ! m_pcb->IsElementVisible( LAYER_VIAS ) );
guide.SetIgnoreTracks( ! m_pcb->IsElementVisible( LAYER_TRACKS ) );
return guide;
}
void PCB_BASE_FRAME::UpdateStatusBar()
{
EDA_DRAW_FRAME::UpdateStatusBar();
BASE_SCREEN* screen = GetScreen();
if( !screen )
return;
wxString line;
VECTOR2D cursorPos = GetCanvas()->GetViewControls()->GetCursorPosition();
if( GetShowPolarCoords() ) // display polar coordinates
{
double dx = cursorPos.x - screen->m_LocalOrigin.x;
double dy = cursorPos.y - screen->m_LocalOrigin.y;
double theta = RAD2DEG( atan2( -dy, dx ) );
double ro = hypot( dx, dy );
line.Printf( wxT( "r %s theta %.3f" ),
MessageTextFromValue( ro, false ),
theta );
SetStatusText( line, 3 );
}
// Transform absolute coordinates for user origin preferences
double userXpos = m_originTransforms.ToDisplayAbsX( static_cast<double>( cursorPos.x ) );
double userYpos = m_originTransforms.ToDisplayAbsY( static_cast<double>( cursorPos.y ) );
// Display absolute coordinates:
line.Printf( wxT( "X %s Y %s" ),
MessageTextFromValue( userXpos, false ),
MessageTextFromValue( userYpos, false ) );
SetStatusText( line, 2 );
if( !GetShowPolarCoords() ) // display relative cartesian coordinates
{
// Calculate relative coordinates
double relXpos = cursorPos.x - screen->m_LocalOrigin.x;
double relYpos = cursorPos.y - screen->m_LocalOrigin.y;
// Transform relative coordinates for user origin preferences
userXpos = m_originTransforms.ToDisplayRelX( relXpos );
userYpos = m_originTransforms.ToDisplayRelY( relYpos );
line.Printf( wxT( "dx %s dy %s dist %s" ),
MessageTextFromValue( userXpos, false ),
MessageTextFromValue( userYpos, false ),
MessageTextFromValue( hypot( userXpos, userYpos ), false ) );
SetStatusText( line, 3 );
}
DisplayGridMsg();
}
void PCB_BASE_FRAME::unitsChangeRefresh()
{
EDA_DRAW_FRAME::unitsChangeRefresh(); // Update the status bar.
if( GetBoard() )
GetBoard()->SetUserUnits( GetUserUnits() );
UpdateGridSelectBox();
}
void PCB_BASE_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
{
EDA_DRAW_FRAME::LoadSettings( aCfg );
if( aCfg->m_Window.grid.grids.empty() )
aCfg->m_Window.grid.grids = aCfg->DefaultGridSizeList();
// Move legacy user grids to grid list
if( !aCfg->m_Window.grid.user_grid_x.empty() )
{
aCfg->m_Window.grid.grids.emplace_back( GRID{ "User Grid", aCfg->m_Window.grid.user_grid_x,
aCfg->m_Window.grid.user_grid_y } );
aCfg->m_Window.grid.user_grid_x = wxEmptyString;
aCfg->m_Window.grid.user_grid_y = wxEmptyString;
}
// Currently values read from config file are not used because the user cannot
// change this config
// if( aCfg->m_Window.zoom_factors.empty() )
{
if( ADVANCED_CFG::GetCfg().m_HyperZoom )
aCfg->m_Window.zoom_factors = { ZOOM_LIST_PCBNEW_HYPER };
else
aCfg->m_Window.zoom_factors = { ZOOM_LIST_PCBNEW };
}
// Some, but not all, derived classes have a PCBNEW_SETTINGS.
if( PCBNEW_SETTINGS* pcbnew_cfg = dynamic_cast<PCBNEW_SETTINGS*>( aCfg ) )
m_polarCoords = pcbnew_cfg->m_PolarCoords;
wxASSERT( GetCanvas() );
if( GetCanvas() )
{
RENDER_SETTINGS* rs = GetCanvas()->GetView()->GetPainter()->GetSettings();
if( rs )
{
rs->SetHighlightFactor( aCfg->m_Graphics.highlight_factor );
rs->SetSelectFactor( aCfg->m_Graphics.select_factor );
rs->SetDefaultFont( wxEmptyString ); // Always the KiCad font for PCBs
}
}
}
SEVERITY PCB_BASE_FRAME::GetSeverity( int aErrorCode ) const
{
if( aErrorCode >= CLEANUP_FIRST )
return RPT_SEVERITY_ACTION;
BOARD_DESIGN_SETTINGS& bds = GetBoard()->GetDesignSettings();
return bds.m_DRCSeverities[ aErrorCode ];
}
void PCB_BASE_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg )
{
EDA_DRAW_FRAME::SaveSettings( aCfg );
// Some, but not all derived classes have a PCBNEW_SETTINGS.
PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( aCfg );
if( cfg )
cfg->m_PolarCoords = m_polarCoords;
}
PCBNEW_SETTINGS* PCB_BASE_FRAME::GetPcbNewSettings() const
{
return Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" );
}
FOOTPRINT_EDITOR_SETTINGS* PCB_BASE_FRAME::GetFootprintEditorSettings() const
{
return Pgm().GetSettingsManager().GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" );
}
PCB_VIEWERS_SETTINGS_BASE* PCB_BASE_FRAME::GetViewerSettingsBase() const
{
switch( GetFrameType() )
{
case FRAME_PCB_EDITOR:
case FRAME_PCB_DISPLAY3D:
default:
return Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" );
case FRAME_FOOTPRINT_EDITOR:
case FRAME_FOOTPRINT_WIZARD:
return Pgm().GetSettingsManager().GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" );
case FRAME_FOOTPRINT_VIEWER:
case FRAME_FOOTPRINT_CHOOSER:
case FRAME_FOOTPRINT_PREVIEW:
case FRAME_CVPCB:
case FRAME_CVPCB_DISPLAY:
return Pgm().GetSettingsManager().GetAppSettings<CVPCB_SETTINGS>( "cvpcb" );
}
}
MAGNETIC_SETTINGS* PCB_BASE_FRAME::GetMagneticItemsSettings()
{
return &GetPcbNewSettings()->m_MagneticItems;
}
void PCB_BASE_FRAME::CommonSettingsChanged( int aFlags )
{
EDA_DRAW_FRAME::CommonSettingsChanged( aFlags );
KIGFX::VIEW* view = GetCanvas()->GetView();
KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( view->GetPainter() );
PCB_RENDER_SETTINGS* settings = painter->GetSettings();
settings->LoadColors( GetColorSettings( true ) );
settings->LoadDisplayOptions( GetDisplayOptions() );
settings->m_ForceShowFieldsWhenFPSelected = GetPcbNewSettings()->m_Display.m_ForceShowFieldsWhenFPSelected;
if( aFlags & TEXTVARS_CHANGED )
GetBoard()->SynchronizeProperties();
// Note: KIGFX::REPAINT isn't enough for things that go from invisible to visible as
// they won't be found in the view layer's itemset for re-painting.
GetCanvas()->GetView()->UpdateAllItemsConditionally(
[&]( KIGFX::VIEW_ITEM* aItem ) -> int
{
if( dynamic_cast<RATSNEST_VIEW_ITEM*>( aItem ) )
{
return KIGFX::ALL; // ratsnest display
}
else if( dynamic_cast<PCB_TRACK*>( aItem ) )
{
return KIGFX::REPAINT; // track, arc & via clearance display
}
else if( dynamic_cast<PAD*>( aItem ) )
{
return KIGFX::REPAINT; // pad clearance display
}
else if( EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( aItem ) )
{
if( text->HasTextVars() )
{
text->ClearRenderCache();
text->ClearBoundingBoxCache();
return KIGFX::GEOMETRY | KIGFX::REPAINT;
}
}
return 0;
} );
view->UpdateAllItems( KIGFX::COLOR );
RecreateToolbars();
// The 3D viewer isn't in the Kiway, so send its update manually
EDA_3D_VIEWER_FRAME* viewer = Get3DViewerFrame();
if( viewer )
viewer->CommonSettingsChanged( aFlags );
}
void PCB_BASE_FRAME::OnModify()
{
EDA_BASE_FRAME::OnModify();
GetScreen()->SetContentModified();
GetBoard()->IncrementTimeStamp();
if( m_isClosing )
return;
UpdateStatusBar();
UpdateMsgPanel();
}
void PCB_BASE_FRAME::rebuildConnectivity()
{
GetBoard()->BuildConnectivity();
GetToolManager()->PostEvent( EVENTS::ConnectivityChangedEvent );
GetCanvas()->RedrawRatsnest();
}
PCB_DRAW_PANEL_GAL* PCB_BASE_FRAME::GetCanvas() const
{
return static_cast<PCB_DRAW_PANEL_GAL*>( EDA_DRAW_FRAME::GetCanvas() );
}
void PCB_BASE_FRAME::ActivateGalCanvas()
{
EDA_DRAW_FRAME::ActivateGalCanvas();
EDA_DRAW_PANEL_GAL* canvas = GetCanvas();
KIGFX::VIEW* view = canvas->GetView();
if( m_toolManager )
{
m_toolManager->SetEnvironment( m_pcb, view, canvas->GetViewControls(), config(), this );
m_toolManager->ResetTools( TOOL_BASE::GAL_SWITCH );
}
KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( view->GetPainter() );
KIGFX::PCB_RENDER_SETTINGS* settings = painter->GetSettings();
const PCB_DISPLAY_OPTIONS& displ_opts = GetDisplayOptions();
settings->LoadDisplayOptions( displ_opts );
settings->LoadColors( GetColorSettings() );
settings->m_ForceShowFieldsWhenFPSelected = GetPcbNewSettings()->m_Display.m_ForceShowFieldsWhenFPSelected;
view->RecacheAllItems();
canvas->SetEventDispatcher( m_toolDispatcher );
canvas->StartDrawing();
try
{
if( !m_spaceMouse )
{
m_spaceMouse = std::make_unique<NL_PCBNEW_PLUGIN>( GetCanvas() );
}
}
catch( const std::system_error& e )
{
wxLogTrace( wxT( "KI_TRACE_NAVLIB" ), e.what() );
}
}
void PCB_BASE_FRAME::SetDisplayOptions( const PCB_DISPLAY_OPTIONS& aOptions, bool aRefresh )
{
bool hcChanged = m_displayOptions.m_ContrastModeDisplay != aOptions.m_ContrastModeDisplay;
bool hcVisChanged = m_displayOptions.m_ContrastModeDisplay == HIGH_CONTRAST_MODE::HIDDEN
|| aOptions.m_ContrastModeDisplay == HIGH_CONTRAST_MODE::HIDDEN;
m_displayOptions = aOptions;
EDA_DRAW_PANEL_GAL* canvas = GetCanvas();
KIGFX::PCB_VIEW* view = static_cast<KIGFX::PCB_VIEW*>( canvas->GetView() );
view->UpdateDisplayOptions( aOptions );
canvas->SetHighContrastLayer( GetActiveLayer() );
OnDisplayOptionsChanged();
// Vias on a restricted layer set must be redrawn when high contrast mode is changed
if( hcChanged )
{
bool showNetNames = false;
if( PCBNEW_SETTINGS* config = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
showNetNames = config->m_Display.m_NetNames > 0;
// Note: KIGFX::REPAINT isn't enough for things that go from invisible to visible as
// they won't be found in the view layer's itemset for re-painting.
GetCanvas()->GetView()->UpdateAllItemsConditionally(
[&]( KIGFX::VIEW_ITEM* aItem ) -> int
{
if( PCB_VIA* via = dynamic_cast<PCB_VIA*>( aItem ) )
{
if( via->GetViaType() != VIATYPE::THROUGH
|| via->GetRemoveUnconnected()
|| showNetNames )
{
return hcVisChanged ? KIGFX::ALL : KIGFX::REPAINT;
}
}
else if( PAD* pad = dynamic_cast<PAD*>( aItem ) )
{
if( pad->GetRemoveUnconnected()
|| showNetNames )
{
return hcVisChanged ? KIGFX::ALL : KIGFX::REPAINT;
}
}
return 0;
} );
}
if( aRefresh )
canvas->Refresh();
}
void PCB_BASE_FRAME::setFPWatcher( FOOTPRINT* aFootprint )
{
wxLogTrace( "KICAD_LIB_WATCH", "setFPWatcher" );
Unbind( wxEVT_FSWATCHER, &PCB_BASE_FRAME::OnFPChange, this );
if( m_watcher )
{
wxLogTrace( "KICAD_LIB_WATCH", "Remove watch" );
m_watcher->RemoveAll();
m_watcher->SetOwner( nullptr );
m_watcher.reset();
}
wxString libfullname;
FP_LIB_TABLE* tbl = PROJECT_PCB::PcbFootprintLibs( &Prj() );
if( !aFootprint || !tbl )
return;
try
{
const FP_LIB_TABLE_ROW* row = tbl->FindRow( aFootprint->GetFPID().GetLibNickname() );
if( !row )
return;
libfullname = row->GetFullURI( true );
}
catch( const std::exception& e )
{
DisplayInfoMessage( this, e.what() );
return;
}
catch( const IO_ERROR& error )
{
wxLogTrace( "KICAD_LIB_WATCH", "Error: %s", error.What() );
return;
}
m_watcherFileName.Assign( libfullname, aFootprint->GetFPID().GetLibItemName(),
FILEEXT::KiCadFootprintFileExtension );
if( !m_watcherFileName.FileExists() )
return;
m_watcherLastModified = m_watcherFileName.GetModificationTime();
Bind( wxEVT_FSWATCHER, &PCB_BASE_FRAME::OnFPChange, this );
m_watcher = std::make_unique<wxFileSystemWatcher>();
m_watcher->SetOwner( this );
wxFileName fn;
fn.AssignDir( m_watcherFileName.GetPath() );
fn.DontFollowLink();
wxLogTrace( "KICAD_LIB_WATCH", "Add watch: %s", fn.GetPath() );
{
// Silence OS errors that come from the watcher
wxLogNull silence;
m_watcher->Add( fn );
}
}
void PCB_BASE_FRAME::OnFPChange( wxFileSystemWatcherEvent& aEvent )
{
if( aEvent.GetPath() != m_watcherFileName.GetFullPath() )
return;
// Start the debounce timer (set to 1 second)
if( !m_watcherDebounceTimer.StartOnce( 1000 ) )
{
wxLogTrace( "KICAD_LIB_WATCH", "Failed to start the debounce timer" );
return;
}
}
void PCB_BASE_FRAME::OnFpChangeDebounceTimer( wxTimerEvent& aEvent )
{
wxLogTrace( "KICAD_LIB_WATCH", "OnFpChangeDebounceTimer" );
// Disable logging to avoid spurious messages and check if the file has changed
wxLog::EnableLogging( false );
wxDateTime lastModified = m_watcherFileName.GetModificationTime();
wxLog::EnableLogging( true );
if( lastModified == m_watcherLastModified || !lastModified.IsValid() )
return;
m_watcherLastModified = lastModified;
FOOTPRINT* fp = GetBoard()->GetFirstFootprint();
FP_LIB_TABLE* tbl = PROJECT_PCB::PcbFootprintLibs( &Prj() );
// When loading a footprint from a library in the footprint editor
// the items UUIDs must be keep and not reinitialized
bool keepUUID = IsType( FRAME_FOOTPRINT_EDITOR );
if( !fp || !tbl )
return;
if( !GetScreen()->IsContentModified()
|| IsOK( this, _( "The library containing the current footprint has changed.\n"
"Do you want to reload the footprint?" ) ) )
{
wxString fpname = fp->GetFPID().GetLibItemName();
wxString nickname = fp->GetFPID().GetLibNickname();
try
{
FOOTPRINT* newfp = tbl->FootprintLoad( nickname, fpname, keepUUID );
if( newfp )
{
std::vector<KIID> selectedItems;
for( const EDA_ITEM* item : GetCurrentSelection() )
{
wxString uuidStr = item->m_Uuid.AsString();
selectedItems.emplace_back( item->m_Uuid );
}
m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
ReloadFootprint( newfp );
newfp->ClearAllNets();
GetCanvas()->UpdateColors();
GetCanvas()->DisplayBoard( GetBoard() );
m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
std::vector<EDA_ITEM*> sel;
for( const KIID& uuid : selectedItems )
{
BOARD_ITEM* item = GetBoard()->GetItem( uuid );
if( item != DELETED_BOARD_ITEM::GetInstance() )
sel.push_back( item );
}
if( !sel.empty() )
m_toolManager->RunAction( PCB_ACTIONS::selectItems, &sel );
}
}
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.What() );
}
}
}
|