diff --git a/api/proto/board/board_commands.proto b/api/proto/board/board_commands.proto index bb5df9f425..3021a71514 100644 --- a/api/proto/board/board_commands.proto +++ b/api/proto/board/board_commands.proto @@ -58,6 +58,30 @@ message GraphicsDefaultsResponse kiapi.board.GraphicsDefaults defaults = 1; } +enum BoardOriginType +{ + BOT_UNKNOWN = 0; + BOT_GRID = 1; + BOT_DRILL = 2; +} + +// Returns a Vector2 with the specified origin point +message GetBoardOrigin +{ + kiapi.common.types.DocumentSpecifier board = 1; + + BoardOriginType type = 2; +} + +message SetBoardOrigin +{ + kiapi.common.types.DocumentSpecifier board = 1; + + BoardOriginType type = 2; + + kiapi.common.types.Vector2 origin = 3; +} + /* * Net management */ diff --git a/include/board_design_settings.h b/include/board_design_settings.h index 34f19c2559..fdd515d0a7 100644 --- a/include/board_design_settings.h +++ b/include/board_design_settings.h @@ -670,10 +670,10 @@ public: int GetLayerClass( PCB_LAYER_ID aLayer ) const; void SetAuxOrigin( const VECTOR2I& aOrigin ) { m_auxOrigin = aOrigin; } - const VECTOR2I& GetAuxOrigin() { return m_auxOrigin; } + const VECTOR2I& GetAuxOrigin() const { return m_auxOrigin; } void SetGridOrigin( const VECTOR2I& aOrigin ) { m_gridOrigin = aOrigin; } - const VECTOR2I& GetGridOrigin() { return m_gridOrigin; } + const VECTOR2I& GetGridOrigin() const { return m_gridOrigin; } void SetDefaultMasterPad(); diff --git a/pcbnew/api/api_handler_pcb.cpp b/pcbnew/api/api_handler_pcb.cpp index 7215432710..1a1f8f95d2 100644 --- a/pcbnew/api/api_handler_pcb.cpp +++ b/pcbnew/api/api_handler_pcb.cpp @@ -82,6 +82,8 @@ API_HANDLER_PCB::API_HANDLER_PCB( PCB_EDIT_FRAME* aFrame ) : &API_HANDLER_PCB::handleGetTitleBlockInfo ); registerHandler( &API_HANDLER_PCB::handleExpandTextVariables ); + registerHandler( &API_HANDLER_PCB::handleGetBoardOrigin ); + registerHandler( &API_HANDLER_PCB::handleSetBoardOrigin ); registerHandler( &API_HANDLER_PCB::handleInteractiveMoveItems ); registerHandler( &API_HANDLER_PCB::handleGetNets ); @@ -869,6 +871,92 @@ HANDLER_RESULT API_HANDLER_PCB::handleGetGraphicsDefau } +HANDLER_RESULT API_HANDLER_PCB::handleGetBoardOrigin( + const HANDLER_CONTEXT& aCtx ) +{ + if( HANDLER_RESULT documentValidation = validateDocument( aCtx.Request.board() ); + !documentValidation ) + { + return tl::unexpected( documentValidation.error() ); + } + + VECTOR2I origin; + const BOARD_DESIGN_SETTINGS& settings = frame()->GetBoard()->GetDesignSettings(); + + switch( aCtx.Request.type() ) + { + case BOT_GRID: + origin = settings.GetGridOrigin(); + break; + + case BOT_DRILL: + origin = settings.GetAuxOrigin(); + break; + + default: + case BOT_UNKNOWN: + { + ApiResponseStatus e; + e.set_status( ApiStatusCode::AS_BAD_REQUEST ); + e.set_error_message( "Unexpected origin type" ); + return tl::unexpected( e ); + } + } + + types::Vector2 reply; + PackVector2( reply, origin ); + return reply; +} + +HANDLER_RESULT API_HANDLER_PCB::handleSetBoardOrigin( + const HANDLER_CONTEXT& aCtx ) +{ + if( std::optional busy = checkForBusy() ) + return tl::unexpected( *busy ); + + if( HANDLER_RESULT documentValidation = validateDocument( aCtx.Request.board() ); + !documentValidation ) + { + return tl::unexpected( documentValidation.error() ); + } + + BOARD_DESIGN_SETTINGS& settings = frame()->GetBoard()->GetDesignSettings(); + VECTOR2I origin = UnpackVector2( aCtx.Request.origin() ); + + switch( aCtx.Request.type() ) + { + case BOT_GRID: + settings.SetGridOrigin( origin ); + frame()->Refresh(); + break; + + case BOT_DRILL: + { + PCB_EDIT_FRAME* f = frame(); + + frame()->CallAfter( [f, origin]() + { + TOOL_MANAGER* mgr = f->GetToolManager(); + mgr->RunAction( PCB_ACTIONS::drillSetOrigin, origin ); + f->Refresh(); + } ); + break; + } + + default: + case BOT_UNKNOWN: + { + ApiResponseStatus e; + e.set_status( ApiStatusCode::AS_BAD_REQUEST ); + e.set_error_message( "Unexpected origin type" ); + return tl::unexpected( e ); + } + } + + return Empty(); +} + + HANDLER_RESULT API_HANDLER_PCB::handleGetBoundingBox( const HANDLER_CONTEXT& aCtx ) { @@ -913,10 +1001,11 @@ HANDLER_RESULT API_HANDLER_PCB::handleGetBoundingBox( HANDLER_RESULT API_HANDLER_PCB::handleGetPadShapeAsPolygon( const HANDLER_CONTEXT& aCtx ) { - HANDLER_RESULT documentValidation = validateDocument( aCtx.Request.board() ); - - if( !documentValidation ) + if( HANDLER_RESULT documentValidation = validateDocument( aCtx.Request.board() ); + !documentValidation ) + { return tl::unexpected( documentValidation.error() ); + } PadShapeAsPolygonResponse response; PCB_LAYER_ID layer = FromProtoEnum( aCtx.Request.layer() ); diff --git a/pcbnew/api/api_handler_pcb.h b/pcbnew/api/api_handler_pcb.h index ac437854cb..e02626dfd5 100644 --- a/pcbnew/api/api_handler_pcb.h +++ b/pcbnew/api/api_handler_pcb.h @@ -92,6 +92,11 @@ private: HANDLER_RESULT handleGetGraphicsDefaults( const HANDLER_CONTEXT& aCtx ); + HANDLER_RESULT handleGetBoardOrigin( + const HANDLER_CONTEXT& aCtx ); + + HANDLER_RESULT handleSetBoardOrigin( const HANDLER_CONTEXT& aCtx ); + HANDLER_RESULT handleGetBoundingBox( const HANDLER_CONTEXT& aCtx ); diff --git a/pcbnew/tools/board_editor_control.cpp b/pcbnew/tools/board_editor_control.cpp index e7a2618e75..22d87d8552 100644 --- a/pcbnew/tools/board_editor_control.cpp +++ b/pcbnew/tools/board_editor_control.cpp @@ -1650,6 +1650,14 @@ int BOARD_EDITOR_CONTROL::DrillOrigin( const TOOL_EVENT& aEvent ) return 0; } + if( aEvent.IsAction( &PCB_ACTIONS::drillSetOrigin ) ) + { + VECTOR2I origin = aEvent.Parameter(); + m_frame->SaveCopyInUndoList( m_placeOrigin.get(), UNDO_REDO::GRIDORIGIN ); + DoSetDrillOrigin( getView(), m_frame, m_placeOrigin.get(), origin ); + return 0; + } + PCB_PICKER_TOOL* picker = m_toolMgr->GetTool(); // Deactivate other tools; particularly important if another PICKER is currently running @@ -1719,6 +1727,7 @@ void BOARD_EDITOR_CONTROL::setTransitions() Go( &BOARD_EDITOR_CONTROL::PlaceFootprint, PCB_ACTIONS::placeFootprint.MakeEvent() ); Go( &BOARD_EDITOR_CONTROL::DrillOrigin, PCB_ACTIONS::drillOrigin.MakeEvent() ); Go( &BOARD_EDITOR_CONTROL::DrillOrigin, PCB_ACTIONS::drillResetOrigin.MakeEvent() ); + Go( &BOARD_EDITOR_CONTROL::DrillOrigin, PCB_ACTIONS::drillSetOrigin.MakeEvent() ); Go( &BOARD_EDITOR_CONTROL::EditFpInFpEditor, PCB_ACTIONS::editFpInFpEditor.MakeEvent() ); Go( &BOARD_EDITOR_CONTROL::EditFpInFpEditor, PCB_ACTIONS::editLibFpInFpEditor.MakeEvent() ); diff --git a/pcbnew/tools/pcb_actions.cpp b/pcbnew/tools/pcb_actions.cpp index eebb05d828..6c76d20415 100644 --- a/pcbnew/tools/pcb_actions.cpp +++ b/pcbnew/tools/pcb_actions.cpp @@ -1153,6 +1153,11 @@ TOOL_ACTION PCB_ACTIONS::drillResetOrigin( TOOL_ACTION_ARGS() .LegacyHotkeyName( "Reset Drill Origin" ) .FriendlyName( _( "Reset Drill Origin" ) ) ); +TOOL_ACTION PCB_ACTIONS::drillSetOrigin( TOOL_ACTION_ARGS() + .Name( "pcbnew.EditorControl.drillSetOrigin" ) + .Scope( AS_CONTEXT ) + .Parameter( VECTOR2I() ) ); + TOOL_ACTION PCB_ACTIONS::toggleLock( TOOL_ACTION_ARGS() .Name( "pcbnew.EditorControl.toggleLock" ) .Scope( AS_GLOBAL ) diff --git a/pcbnew/tools/pcb_actions.h b/pcbnew/tools/pcb_actions.h index 9a70890a9e..16d39c29c9 100644 --- a/pcbnew/tools/pcb_actions.h +++ b/pcbnew/tools/pcb_actions.h @@ -541,7 +541,7 @@ public: static TOOL_ACTION measureTool; static TOOL_ACTION drillOrigin; static TOOL_ACTION drillResetOrigin; - static TOOL_ACTION placeFileOrigin; + static TOOL_ACTION drillSetOrigin; static TOOL_ACTION appendBoard; static TOOL_ACTION showEeschema; static TOOL_ACTION boardStatistics;