diff --git a/pcbnew/pcbframe.cpp b/pcbnew/pcbframe.cpp index 15822a9b17..0331d6b326 100644 --- a/pcbnew/pcbframe.cpp +++ b/pcbnew/pcbframe.cpp @@ -569,24 +569,7 @@ void PCB_EDIT_FRAME::ViewReloadBoard( const BOARD* aBoard ) const // Load modules and its additional elements for( MODULE* module = aBoard->m_Modules; module; module = module->Next() ) { - // Load module's pads - for( D_PAD* pad = module->Pads().GetFirst(); pad; pad = pad->Next() ) - { - view->Add( pad ); - } - - // Load module's drawing (mostly silkscreen) - for( BOARD_ITEM* drawing = module->GraphicalItems().GetFirst(); drawing; - drawing = drawing->Next() ) - { - view->Add( drawing ); - } - - // Load module's texts (name and value) - view->Add( &module->Reference() ); - view->Add( &module->Value() ); - - // Add the module itself + module->RunOnChildren( std::bind1st( std::mem_fun( &KIGFX::VIEW::Add ), view ) ); view->Add( module ); } diff --git a/pcbnew/tools/common_actions.cpp b/pcbnew/tools/common_actions.cpp index de860ccc06..f3ea912d31 100644 --- a/pcbnew/tools/common_actions.cpp +++ b/pcbnew/tools/common_actions.cpp @@ -80,3 +80,7 @@ TOOL_ACTION COMMON_ACTIONS::drawDimension( "pcbnew.InteractiveDrawing.dimension" TOOL_ACTION COMMON_ACTIONS::placeTarget( "pcbnew.InteractiveDrawing.placeTarget", AS_GLOBAL, 'C', "Add layer alignment target", "Add layer alignment target" ); + +TOOL_ACTION COMMON_ACTIONS::placeModule( "pcbnew.InteractiveDrawing.placeModule", + AS_GLOBAL, 'V', + "Add modules", "Add modules" ); diff --git a/pcbnew/tools/common_actions.h b/pcbnew/tools/common_actions.h index 732aa22e7d..fa07c45ce4 100644 --- a/pcbnew/tools/common_actions.h +++ b/pcbnew/tools/common_actions.h @@ -74,4 +74,7 @@ public: /// Activation of the drawing tool (placing a TARGET) static TOOL_ACTION placeTarget; + + /// Activation of the drawing tool (placing a MODULE) + static TOOL_ACTION placeModule; }; diff --git a/pcbnew/tools/drawing_tool.cpp b/pcbnew/tools/drawing_tool.cpp index 28c287ea7a..b9433db085 100644 --- a/pcbnew/tools/drawing_tool.cpp +++ b/pcbnew/tools/drawing_tool.cpp @@ -26,16 +26,20 @@ #include "common_actions.h" #include +#include + #include #include +#include +#include + #include #include #include #include #include -#include -#include -#include +#include +#include // TODO to be removed if we do not need the flags DRAWING_TOOL::DRAWING_TOOL() : TOOL_INTERACTIVE( "pcbnew.InteractiveDrawing" ) @@ -570,6 +574,8 @@ int DRAWING_TOOL::PlaceTarget( TOOL_EVENT& aEvent ) target->SetLayer( EDGE_N ); target->SetWidth( board->GetDesignSettings().m_EdgeSegmentWidth ); target->SetSize( Millimeter2iu( 5 ) ); + VECTOR2D cursorPos = view->ToWorld( controls->GetCursorPosition() ); + target->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) ); // Add a VIEW_GROUP that serves as a preview for the new item KIGFX::VIEW_GROUP preview( view ); @@ -630,6 +636,85 @@ int DRAWING_TOOL::PlaceTarget( TOOL_EVENT& aEvent ) } +int DRAWING_TOOL::PlaceModule( TOOL_EVENT& aEvent ) +{ + KIGFX::VIEW* view = getView(); + KIGFX::VIEW_CONTROLS* controls = getViewControls(); + BOARD* board = getModel( PCB_T ); + PCB_EDIT_FRAME* editFrame = getEditFrame(); + MODULE* module = editFrame->LoadModuleFromLibrary( wxEmptyString, + editFrame->GetFootprintLibraryTable(), true, NULL ); + if( module == NULL ) + { + setTransitions(); + return 0; + } + + // Init the new item attributes + VECTOR2D cursorPos = view->ToWorld( controls->GetCursorPosition() ); + module->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) ); + + // Add a VIEW_GROUP that serves as a preview for the new item + KIGFX::VIEW_GROUP preview( view ); + preview.Add( module ); + module->RunOnChildren( std::bind1st( std::mem_fun( &KIGFX::VIEW_GROUP::Add ), &preview ) ); + view->Add( &preview ); + preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); + + controls->SetSnapping( true ); + + Activate(); + + // Main loop: keep receiving events + while( OPT_TOOL_EVENT evt = Wait() ) + { + cursorPos = view->ToWorld( controls->GetCursorPosition() ); + + if( evt->IsCancel() ) + { + board->Delete( module ); + break; + } + + else if( evt->Category() == TC_COMMAND ) + { + if( evt->IsAction( &COMMON_ACTIONS::rotate ) ) + { + module->Rotate( module->GetPosition(), getEditFrame()->GetRotationAngle() ); + preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); + } + else if( evt->IsAction( &COMMON_ACTIONS::flip ) ) + { + module->Flip( module->GetPosition() ); + preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); + } + } + + else if( evt->IsClick( BUT_LEFT ) ) + { + module->RunOnChildren( std::bind1st( std::mem_fun( &KIGFX::VIEW::Add ), view ) ); + view->Add( module ); + module->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); + break; + } + + else if( evt->IsMotion() ) + { + module->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) ); + preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); + } + } + + controls->SetSnapping( false ); + controls->SetAutoPan( false ); + view->Remove( &preview ); + + setTransitions(); + + return 0; +} + + void DRAWING_TOOL::setTransitions() { Go( &DRAWING_TOOL::DrawLine, COMMON_ACTIONS::drawLine.MakeEvent() ); @@ -638,4 +723,5 @@ void DRAWING_TOOL::setTransitions() Go( &DRAWING_TOOL::DrawText, COMMON_ACTIONS::drawText.MakeEvent() ); Go( &DRAWING_TOOL::DrawDimension, COMMON_ACTIONS::drawDimension.MakeEvent() ); Go( &DRAWING_TOOL::PlaceTarget, COMMON_ACTIONS::placeTarget.MakeEvent() ); + Go( &DRAWING_TOOL::PlaceModule, COMMON_ACTIONS::placeModule.MakeEvent() ); } diff --git a/pcbnew/tools/drawing_tool.h b/pcbnew/tools/drawing_tool.h index c34109a2cf..1a33d4f7f8 100644 --- a/pcbnew/tools/drawing_tool.h +++ b/pcbnew/tools/drawing_tool.h @@ -61,6 +61,8 @@ public: int PlaceTarget( TOOL_EVENT& aEvent ); + int PlaceModule( TOOL_EVENT& aEvent ); + private: ///> Starts drawing a selected shape. int draw( STROKE_T aShape ); diff --git a/pcbnew/tools/pcb_tools.cpp b/pcbnew/tools/pcb_tools.cpp index 9202ddb4b5..7d2ed216be 100644 --- a/pcbnew/tools/pcb_tools.cpp +++ b/pcbnew/tools/pcb_tools.cpp @@ -61,6 +61,7 @@ void PCB_EDIT_FRAME::setupTools() m_toolManager->RegisterAction( &COMMON_ACTIONS::drawText ); m_toolManager->RegisterAction( &COMMON_ACTIONS::drawDimension ); m_toolManager->RegisterAction( &COMMON_ACTIONS::placeTarget ); + m_toolManager->RegisterAction( &COMMON_ACTIONS::placeModule ); // Register tools m_toolManager->RegisterTool( new SELECTION_TOOL );