53#include <api/board/board_tasks.pb.h>
54#include <api/common/tasks.pb.h>
59using kiapi::board::tasks::DrcTaskResult;
60using kiapi::board::tasks::GetDrcTaskResult;
61using kiapi::board::tasks::StartDrcTask;
62using kiapi::board::tasks::StartRefillZonesTask;
63using kiapi::common::tasks::StartTaskResponse;
64using kiapi::common::tasks::TaskState;
65using kiapi::common::tasks::TaskStatusResponse;
66using kiapi::common::tasks::TS_CANCELLED;
67using kiapi::common::tasks::TS_FAILED;
68using kiapi::common::tasks::TS_RUNNING;
69using kiapi::common::tasks::TS_UNKNOWN;
100 std::unique_ptr<API_TASK_GUI_PROGRESS_REPORTER>
reporter;
113 std::unique_ptr<COMMIT> commit =
m_handler->createCommit();
159 void FillStatus( TaskStatusResponse& aStatus )
const override
181 bool aRequireZonesFilled ) :
195 std::shared_ptr<DRC_ENGINE> drcEngine =
m_board->GetDesignSettings().m_DRCEngine;
199 drcEngine->InitEngine(
m_board->GetDesignRulesPath() );
204 m_errorMessage =
"DRC incomplete: could not compile custom design rules";
215 units =
frame->GetUserUnits();
243 drcEngine->SetSchematicNetlist(
m_netlist.get() );
245 m_commit = std::make_unique<BOARD_COMMIT>( mgr );
247 drcEngine->SetViolationHandler(
248 [&](
const std::shared_ptr<DRC_ITEM>& aItem,
const VECTOR2I& aPos,
int aLayer,
249 const std::function<
void(
PCB_MARKER* )>& aPathGenerator )
252 aPathGenerator( marker );
257 m_board->RecordDRCExclusions();
258 m_board->DeleteMARKERs(
true,
true );
260 std::unique_ptr<API_TASK_GUI_PROGRESS_REPORTER>
reporter;
264 reporter = std::make_unique<API_TASK_GUI_PROGRESS_REPORTER>(
frame,
_(
"Running DRC" ), 1,
266 drcEngine->SetProgressReporter(
reporter.get() );
269 std::unique_ptr<DS_PROXY_VIEW_ITEM> headlessDrawingSheet;
273 drcEngine->SetDrawingSheet(
frame->GetCanvas()->GetDrawingSheet() );
277 headlessDrawingSheet = std::make_unique<DS_PROXY_VIEW_ITEM>(
280 headlessDrawingSheet->SetSheetName( std::string() );
281 headlessDrawingSheet->SetSheetPath( std::string() );
282 headlessDrawingSheet->SetIsFirstPage(
true );
283 headlessDrawingSheet->SetFileName(
TO_UTF8(
m_board->GetFileName() ) );
285 wxString variant =
m_board->GetCurrentVariant();
286 headlessDrawingSheet->SetVariantName(
TO_UTF8( variant ) );
287 headlessDrawingSheet->SetVariantDesc(
TO_UTF8(
m_board->GetVariantDescription( variant ) ) );
289 drcEngine->SetDrawingSheet( headlessDrawingSheet.get() );
298 drcEngine->ClearViolationHandler();
299 drcEngine->SetProgressReporter(
nullptr );
302 drcEngine->SetDrawingSheet(
nullptr );
304 headlessDrawingSheet.reset();
308 drcEngine->SetSchematicNetlist(
nullptr );
319 drcEngine->ClearViolationHandler();
320 drcEngine->SetProgressReporter(
nullptr );
323 drcEngine->SetDrawingSheet(
nullptr );
325 headlessDrawingSheet.reset();
329 drcEngine->SetSchematicNetlist(
nullptr );
339 std::unique_ptr<COMMIT> exclusionCommit =
m_handler->createCommit();
344 marker->GetRCItem()->SetItems(
frame->GetCanvas()->GetDrawingSheet() );
346 exclusionCommit->Add( marker );
350 m_board->UpdateRatsnestExclusions();
358 dialog->UpdateData();
360 frame->ShowSolderMask();
367 m_board->ResolveDRCExclusions(
false );
377 dialog->UpdateData();
379 frame->ShowSolderMask();
383 void FillStatus( TaskStatusResponse& aStatus )
const override
402 if( zone->GetIsRuleArea() )
405 if( !zone->IsFilled() || zone->NeedRefill() )
408 m_errorMessage =
"zone fills are not up to date; refill the zones "
409 "(StartRefillZonesTask) before running DRC";
424 _(
"Schematic parity tests require a fully annotated schematic." ) ) )
427 m_errorMessage =
"could not fetch schematic netlist for parity tests";
434 wxFileName schematicPath(
m_board->GetFileName() );
437 if( !schematicPath.FileExists() )
440 if( !schematicPath.FileExists() )
453 m_errorMessage =
"could not load schematic editor for parity tests";
462 m_errorMessage =
"could not load schematic editor for parity tests";
466 std::string netlistStr;
468 if( !( *netlister )( schematicPath.GetFullPath(), netlistStr, kiway )
472 m_errorMessage =
"could not fetch schematic netlist for parity tests";
486 m_errorMessage =
"could not parse schematic netlist for parity tests";
500 auto rank = []( SEVERITY aSeverity )
502 return aSeverity == RPT_SEVERITY_IGNORE
503 || aSeverity == RPT_SEVERITY_DEBUG
505 : static_cast<int>( aSeverity );
516 kiapi::board::DrcMarker markerMsg;
518 *
m_result.add_violations() = std::move( markerMsg );
523 int code = item.get().GetErrorCode();
554 if( !documentValidation )
555 return tl::unexpected( documentValidation.error() );
557 if( std::optional<ApiResponseStatus> busy =
checkForBusy() )
558 return tl::unexpected( *busy );
563 return tl::unexpected( toFill.error() );
565 auto task = std::make_shared<API_HANDLER_PCB::REFILL_ZONES_TASK>(
this, std::move( *toFill ) );
569 if( !token.has_value() )
572 e.set_status( ApiStatusCode::AS_BUSY );
573 e.set_error_message(
"another task is already running" );
574 return tl::unexpected( e );
577 StartTaskResponse response;
578 response.mutable_token()->set_value( *token );
587 if( !documentValidation )
588 return tl::unexpected( documentValidation.error() );
590 if( std::optional<ApiResponseStatus> busy =
checkForBusy() )
591 return tl::unexpected( *busy );
593 auto task = std::make_shared<API_HANDLER_PCB::DRC_TASK>(
this,
594 aCtx.
Request.report_all_track_errors(),
595 aCtx.
Request.test_schematic_parity(),
596 aCtx.
Request.require_zones_filled() );
600 if( !token.has_value() )
603 e.set_status( ApiStatusCode::AS_BUSY );
604 e.set_error_message(
"another task is already running" );
605 return tl::unexpected( e );
608 StartTaskResponse response;
609 response.mutable_token()->set_value( *token );
616 if( !aCtx.
Request.has_token() || aCtx.
Request.token().value().empty() )
619 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
620 e.set_error_message(
"no task token provided" );
621 return tl::unexpected( e );
624 std::optional<TaskStatusResponse> status =
630 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
631 e.set_error_message( fmt::format(
"unknown task token {}", aCtx.
Request.token().value() ) );
632 return tl::unexpected( e );
635 if( status->state() == TS_RUNNING )
638 e.set_status( ApiStatusCode::AS_BUSY );
639 e.set_error_message(
"the DRC task is still running" );
640 return tl::unexpected( e );
643 if( status->state() == TS_FAILED || status->state() == TS_CANCELLED )
646 e.set_status( ApiStatusCode::AS_INTERNAL_ERROR );
647 e.set_error_message( status->error_message().empty() ? std::string(
"DRC task failed" )
648 : status->error_message() );
649 return tl::unexpected( e );
652 std::shared_ptr<API_TASK> task =
658 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
659 e.set_error_message( fmt::format(
"no completed task with token {}", aCtx.
Request.token().value() ) );
660 return tl::unexpected( e );
668 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
669 e.set_error_message(
"task is not a DRC task" );
670 return tl::unexpected( e );
tl::expected< T, ApiResponseStatus > HANDLER_RESULT
constexpr EDA_IU_SCALE pcbIUScale
HANDLER_RESULT< bool > validateDocument(const DocumentSpecifier &aDocument)
virtual std::optional< ApiResponseStatus > checkForBusy()
Checks if the editor can accept commands.
std::string m_errorMessage
std::unique_ptr< NETLIST > m_netlist
void FillStatus(TaskStatusResponse &aStatus) const override
DRC_TASK(API_HANDLER_PCB *aHandler, bool aReportAllTrackErrors, bool aTestFootprints, bool aRequireZonesFilled)
void Run() override
The task runner; called on the main thread.
API_HANDLER_PCB * m_handler
std::vector< PCB_MARKER * > m_violationMarkers
std::unique_ptr< BOARD_COMMIT > m_commit
bool m_requireZonesFilled
DrcTaskResult Result() const
bool m_reportAllTrackErrors
void Run() override
The task runner; called on the main thread.
std::vector< ZONE * > m_zones
API_HANDLER_PCB * m_handler
REFILL_ZONES_TASK(API_HANDLER_PCB *aHandler, std::vector< ZONE * > &&aZones)
void FillStatus(TaskStatusResponse &aStatus) const override
std::string m_errorMessage
HANDLER_RESULT< kiapi::common::tasks::StartTaskResponse > handleStartDrcTask(const HANDLER_CONTEXT< kiapi::board::tasks::StartDrcTask > &aCtx)
API_HANDLER_PCB(PCB_EDIT_FRAME *aFrame)
HANDLER_RESULT< kiapi::common::tasks::StartTaskResponse > handleStartRefillZonesTask(const HANDLER_CONTEXT< kiapi::board::tasks::StartRefillZonesTask > &aCtx)
HANDLER_RESULT< std::vector< ZONE * > > resolveZonesToFill(const google::protobuf::RepeatedPtrField< kiapi::common::types::KIID > &aZoneIds) const
HANDLER_RESULT< kiapi::board::tasks::DrcTaskResult > handleGetDrcTaskResult(const HANDLER_CONTEXT< kiapi::board::tasks::GetDrcTaskResult > &aCtx)
PCB_EDIT_FRAME * frame() const
std::shared_ptr< API_TASK > GetFinishedTask(const std::string &aToken) const
Look up a task that has finished, for result retrieval.
std::optional< kiapi::common::tasks::TaskStatusResponse > GetStatus(const std::string &aToken) const
Get a snapshot of the task's current status.
static API_TASK_SCHEDULER & Instance()
std::optional< std::string > StartTask(const std::shared_ptr< API_TASK > &aTask, const wxString &aDescription)
Register a new task and begin running it.
SEVERITY GetSeverity(int aDRCErrorCode) const
Return the severity of a given DRC error code.
Information pertinent to a Pcbnew printed circuit board.
BOARD_DESIGN_SETTINGS & GetDesignSettings()
static std::vector< std::reference_wrapper< RC_ITEM > > GetItemsWithSeverities()
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Read the new s-expression based KiCad netlist format.
virtual void LoadNetlist() override
Load the contents of the netlist file into aNetlist.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
The main frame for Pcbnew.
SEVERITY GetSeverity() const override
static void ToProto(kiapi::board::DrcMarker &aMsg, const PCB_MARKER &aMarker)
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
bool Fill(const std::vector< ZONE * > &aZones, bool aCheck=false, wxWindow *aParent=nullptr)
Fills the given list of zones.
Handle a list of polygons defining a copper zone.
static const std::string LegacySchematicFileExtension
static const std::string KiCadSchematicFileExtension
Service::Type GetKifaceService(KIFACE *aKiface)
Look up a service exported by a KIFACE.
This file contains miscellaneous commonly used macros and functions.
#define MAIL_SCH_GET_NETLIST_CANCELLED
Reply payload for MAIL_SCH_GET_NETLIST when the user deliberately aborts netlist generation (for exam...
#define SKIP_CONNECTIVITY
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
RequestMessageType Request
Implement a participant in the KIWAY alchemy.
A filename or source description, a problem input line, a line number, a byte offset,...
IbisParser parser & reporter
VECTOR2< int32_t > VECTOR2I
Definition of file extensions used in Kicad.