KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_text_mirroring.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <macros.h>
25#include <pcb_field.h>
26#include <pcb_text.h>
27#include <pcb_textbox.h>
28#include <drc/drc_engine.h>
29#include <drc/drc_item.h>
30#include <drc/drc_rule.h>
32#include <drc/drc_rtree.h>
33#include <footprint.h>
34
35
36/*
37 Text mirroring tests.
38 Errors generated:
39 - DRCE_MIRRORED_TEXT_ON_FRONT_LAYER
40 - DRCE_NONMIRRORED_TEXT_ON_BACK_LAYER
41*/
42
44{
45public:
47 {
48 }
49
51 {
52 }
53
54 virtual bool Run() override;
55
56 virtual const wxString GetName() const override
57 {
58 return wxT( "text_mirroring" );
59 };
60
61 virtual const wxString GetDescription() const override
62 {
63 return wxT( "Tests mirrored text on top layer and non-mirrored text on bottom layer" );
64 }
65};
66
67
69{
72 {
73 reportAux( wxT( "Text mirroring violations ignored. Tests not run." ) );
74 return true; // continue with other tests
75 }
76
77 if( !reportPhase( _( "Checking text mirroring..." ) ) )
78 return false; // DRC cancelled
79
80 LSET topLayers( { F_Cu, F_SilkS, F_Mask, F_Fab } );
81 LSET bottomLayers( { B_Cu, B_SilkS, B_Mask, B_Fab } );
82
83 auto checkTextMirroring = [&]( BOARD_ITEM* item, EDA_TEXT* text, PCB_LAYER_ID layerId,
84 bool isMirrored, int errorCode ) -> bool
85 {
86 if( m_drcEngine->IsErrorLimitExceeded( errorCode ) )
87 return false;
88
89 bool layerMatch = ( isMirrored && topLayers.Contains( layerId ) )
90 || ( !isMirrored && bottomLayers.Contains( layerId ) );
91
92 if( layerMatch && text->IsMirrored() == isMirrored )
93 {
94 auto drcItem = DRC_ITEM::Create( errorCode );
95
96 drcItem->SetErrorMessage( drcItem->GetErrorText() );
97 drcItem->SetItems( item );
98
99 reportViolation( drcItem, item->GetPosition(), layerId );
100 }
101
102 return true;
103 };
104
105 const int progressDelta = 250;
106 int count = 0;
107 int progressIndex = 0;
108 static const std::vector<KICAD_T> itemTypes = { PCB_FIELD_T, PCB_TEXT_T, PCB_TEXTBOX_T };
109
110 forEachGeometryItem( itemTypes, topLayers | bottomLayers,
111 [&]( BOARD_ITEM* item ) -> bool
112 {
113 ++count;
114 return true;
115 } );
116
117 forEachGeometryItem( itemTypes, topLayers | bottomLayers,
118 [&]( BOARD_ITEM* item ) -> bool
119 {
120 if( !reportProgress( progressIndex++, count, progressDelta ) )
121 return false;
122
123 EDA_TEXT* text = nullptr;
124
125 switch( item->Type() )
126 {
127 case PCB_FIELD_T: text = static_cast<PCB_FIELD*>( item ); break;
128 case PCB_TEXT_T: text = static_cast<PCB_TEXT*>( item ); break;
129 case PCB_TEXTBOX_T: text = static_cast<PCB_TEXTBOX*>( item ); break;
130 default: UNIMPLEMENTED_FOR( item->GetClass() ); break;
131 }
132
133 if( !text || !text->IsVisible()
135 || !m_drcEngine->GetBoard()->IsLayerVisible( item->GetLayer() ) )
136 return true;
137
138 if( !checkTextMirroring( item, text, item->GetLayer(), true, DRCE_MIRRORED_TEXT_ON_FRONT_LAYER ) ||
139 !checkTextMirroring( item, text, item->GetLayer(), false, DRCE_NONMIRRORED_TEXT_ON_BACK_LAYER ) )
140 {
141 return false;
142 }
143
144 return true;
145 } );
146
148
149 return !m_drcEngine->IsCancelled();
150}
151
152
153namespace detail
154{
156}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:239
bool IsLayerEnabled(PCB_LAYER_ID aLayer) const
A proxy function that calls the correspondent function in m_BoardSettings tests whether a given layer...
Definition: board.cpp:843
bool IsLayerVisible(PCB_LAYER_ID aLayer) const
A proxy function that calls the correspondent function in m_BoardSettings tests whether a given layer...
Definition: board.cpp:823
BOARD * GetBoard() const
Definition: drc_engine.h:96
bool IsErrorLimitExceeded(int error_code)
bool IsCancelled() const
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:395
virtual const wxString GetDescription() const override
virtual const wxString GetName() const override
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
virtual bool reportPhase(const wxString &aStageName)
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, LSET aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer, DRC_CUSTOM_MARKER_HANDLER *aCustomHandler=nullptr)
DRC_ENGINE * m_drcEngine
void reportAux(const wxString &aMsg)
virtual void reportRuleStatistics()
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:244
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:80
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:37
@ DRCE_MIRRORED_TEXT_ON_FRONT_LAYER
Definition: drc_item.h:109
@ DRCE_NONMIRRORED_TEXT_ON_BACK_LAYER
Definition: drc_item.h:110
#define _(s)
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ B_Mask
Definition: layer_ids.h:98
@ B_Cu
Definition: layer_ids.h:65
@ F_Mask
Definition: layer_ids.h:97
@ F_Fab
Definition: layer_ids.h:119
@ F_SilkS
Definition: layer_ids.h:100
@ B_SilkS
Definition: layer_ids.h:101
@ F_Cu
Definition: layer_ids.h:64
@ B_Fab
Definition: layer_ids.h:118
This file contains miscellaneous commonly used macros and functions.
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:93
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90