diff --git a/pcbnew/tools/common_actions.cpp b/pcbnew/tools/common_actions.cpp index 0878657b1f..4649123d24 100644 --- a/pcbnew/tools/common_actions.cpp +++ b/pcbnew/tools/common_actions.cpp @@ -72,3 +72,7 @@ TOOL_ACTION COMMON_ACTIONS::drawArc( "pcbnew.InteractiveDrawing.arc", TOOL_ACTION COMMON_ACTIONS::drawText( "pcbnew.InteractiveDrawing.text", AS_GLOBAL, 'T', "Add a text", "Add a text" ); + +TOOL_ACTION COMMON_ACTIONS::drawDimension( "pcbnew.InteractiveDrawing.dimension", + AS_GLOBAL, 'X', + "Add a dimension", "Add a dimension" ); diff --git a/pcbnew/tools/common_actions.h b/pcbnew/tools/common_actions.h index 992d9cbdf8..c2e350a67d 100644 --- a/pcbnew/tools/common_actions.h +++ b/pcbnew/tools/common_actions.h @@ -68,4 +68,7 @@ public: /// Activation of the drawing tool (text) static TOOL_ACTION drawText; + + /// Activation of the drawing tool (dimension) + static TOOL_ACTION drawDimension; }; diff --git a/pcbnew/tools/drawing_tool.cpp b/pcbnew/tools/drawing_tool.cpp index 9950ea65e9..bfe59af214 100644 --- a/pcbnew/tools/drawing_tool.cpp +++ b/pcbnew/tools/drawing_tool.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -350,6 +351,11 @@ int DRAWING_TOOL::DrawText( TOOL_EVENT& aEvent ) // Init the new item attributes TEXTE_PCB* newText = getEditFrame()->CreateTextePcb( NULL ); + if( newText == NULL ) + { + setTransitions(); + return 0; + } // Add a VIEW_GROUP that serves as a preview for the new item KIGFX::VIEW_GROUP preview( view ); @@ -417,10 +423,146 @@ int DRAWING_TOOL::DrawText( TOOL_EVENT& aEvent ) } +int DRAWING_TOOL::DrawDimension( TOOL_EVENT& aEvent ) +{ + m_continous = false; + + int step = 0; + + KIGFX::VIEW* view = getView(); + KIGFX::VIEW_CONTROLS* controls = getViewControls(); + BOARD* board = getModel( PCB_T ); + DIMENSION* dimension = new DIMENSION( board ); + + // Init the new item attributes + dimension->Text().SetSize( board->GetDesignSettings().m_PcbTextSize ); + int width = board->GetDesignSettings().m_PcbTextWidth; + int maxthickness = Clamp_Text_PenSize( width, dimension->Text().GetSize() ); + + if( width > maxthickness ) + width = maxthickness; + + dimension->Text().SetThickness( width ); + dimension->SetWidth( width ); + dimension->SetFlags( IS_NEW ); + dimension->AdjustDimensionDetails(); + + // Add a VIEW_GROUP that serves as a preview for the new item + KIGFX::VIEW_GROUP preview( view ); + view->Add( &preview ); + + controls->ShowCursor( true ); + controls->SetSnapping( true ); + + Activate(); + + // Main loop: keep receiving events + while( OPT_TOOL_EVENT evt = Wait() ) + { + VECTOR2D cursorPos = view->ToWorld( controls->GetCursorPosition() ); + + if( evt->IsCancel() ) + { + delete dimension; + break; + } + + else if( evt->IsKeyUp() ) + { + int width = dimension->GetWidth(); + + // Modify the new item width + if( evt->KeyCode() == '-' && width > WIDTH_STEP ) + dimension->SetWidth( width - WIDTH_STEP ); + else if( evt->KeyCode() == '=' ) + dimension->SetWidth( width + WIDTH_STEP ); + + preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); + } + + else if( evt->IsClick( BUT_LEFT ) ) + { + switch( step ) + { + case 0: + { + LAYER_NUM layer = getEditFrame()->GetScreen()->m_Active_Layer; + + if( IsCopperLayer( layer ) ) + { + DisplayInfoMessage( NULL, _( "Graphic not allowed on Copper layers" ) ); + --step; + } + else + { + controls->SetAutoPan( true ); + + dimension->SetLayer( layer ); + dimension->SetOrigin( wxPoint( cursorPos.x, cursorPos.y ) ); + + preview.Add( dimension ); + } + } + break; + + case 2: + { + if( wxPoint( cursorPos.x, cursorPos.y ) != dimension->GetPosition() ) + { + view->Add( dimension ); + board->Add( dimension ); + dimension->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); + } + } + break; + } + + if( ++step == 3 ) + break; + } + + else if( evt->IsMotion() ) + { + switch( step ) + { + case 1: + dimension->SetEnd( wxPoint( cursorPos.x, cursorPos.y ) ); + break; + + case 2: + { + /* Calculating the direction of travel perpendicular to the selected axis. */ + double angle = dimension->GetAngle() + ( M_PI / 2 ); + + wxPoint pos( cursorPos.x, cursorPos.y ); + wxPoint delta( pos - dimension->m_featureLineDO ); + double height = ( delta.x * cos( angle ) ) + ( delta.y * sin( angle ) ); + dimension->SetHeight( height ); + } + break; + } + + // Show a preview of the item + preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); + } + } + + controls->ShowCursor( false ); + controls->SetSnapping( false ); + controls->SetAutoPan( false ); + view->Remove( &preview ); + + setTransitions(); + + return 0; +} + + void DRAWING_TOOL::setTransitions() { Go( &DRAWING_TOOL::DrawLine, COMMON_ACTIONS::drawLine.MakeEvent() ); Go( &DRAWING_TOOL::DrawCircle, COMMON_ACTIONS::drawCircle.MakeEvent() ); Go( &DRAWING_TOOL::DrawArc, COMMON_ACTIONS::drawArc.MakeEvent() ); Go( &DRAWING_TOOL::DrawText, COMMON_ACTIONS::drawText.MakeEvent() ); + Go( &DRAWING_TOOL::DrawDimension, COMMON_ACTIONS::drawDimension.MakeEvent() ); } diff --git a/pcbnew/tools/drawing_tool.h b/pcbnew/tools/drawing_tool.h index 4e969860db..3af4c4fee7 100644 --- a/pcbnew/tools/drawing_tool.h +++ b/pcbnew/tools/drawing_tool.h @@ -57,6 +57,8 @@ public: int DrawText( TOOL_EVENT& aEvent ); + int DrawDimension( 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 73f9f14a5f..e05d294a11 100644 --- a/pcbnew/tools/pcb_tools.cpp +++ b/pcbnew/tools/pcb_tools.cpp @@ -59,6 +59,7 @@ void PCB_EDIT_FRAME::setupTools() m_toolManager->RegisterAction( &COMMON_ACTIONS::drawCircle ); m_toolManager->RegisterAction( &COMMON_ACTIONS::drawArc ); m_toolManager->RegisterAction( &COMMON_ACTIONS::drawText ); + m_toolManager->RegisterAction( &COMMON_ACTIONS::drawDimension ); // Register tools m_toolManager->RegisterTool( new SELECTION_TOOL );