KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_barcode.h
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 (C) 2020 Thomas Pointhuber <[email protected]>
5 * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
25
26#ifndef BARCODE_H_
27#define BARCODE_H_
28
29
30#include <board_item.h>
32#include <pcb_text.h>
33#include <memory>
34
35class LINE_READER;
36class MSG_PANEL_ITEM;
37
38
39enum class BARCODE_T : int
40{
46};
47
48enum class BARCODE_ECC_T : int
49{
50 L = 1, // Low
51 M = 2, // Medium
52 Q = 3, // Quartile
53 H = 4 // High
54};
55
58
59
71
72
73class PCB_BARCODE : public BOARD_ITEM
74{
75public:
81 PCB_BARCODE( BOARD_ITEM* aParent );
82
86 PCB_BARCODE( const PCB_BARCODE& aOther );
87
94 PCB_BARCODE& operator=( const PCB_BARCODE& aOther );
95
100
106 static inline bool ClassOf( const EDA_ITEM* aItem ) { return aItem && PCB_BARCODE_T == aItem->Type(); }
107
111 VECTOR2I GetPosition() const override;
112 void SetPosition( const VECTOR2I& aPos ) override;
113
119 void SetTextSize( int aTextSize );
120
121 int GetTextSize() const;
122
128 void SetLayer( PCB_LAYER_ID aLayer ) override;
129
130 void Serialize( google::protobuf::Any& aContainer ) const override;
131 bool Deserialize( const google::protobuf::Any& aContainer ) override;
132
136 int GetWidth() const { return m_width; }
137 void SetWidth( int aWidth ) { m_width = aWidth; }
138
142 int GetHeight() const { return m_height; }
143 void SetHeight( int aHeight ) { m_height = aHeight; }
144
148 const VECTOR2I& GetMargin() const { return m_margin; }
149 void SetMargin( const VECTOR2I& aMargin ) { m_margin = aMargin; }
150
157 {
159 return m_cache->poly;
160 }
161
166 {
168 return m_cache->symbolPoly;
169 }
170
175 {
177 return m_cache->textPoly;
178 }
179
193 void TransformShapeToPolygon( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer, int aClearance,
194 int aMaxError, ERROR_LOC aErrorLoc = ERROR_INSIDE,
195 bool ignoreLineWidth = false ) const override;
196
197
198 // @copydoc BOARD_ITEM::GetEffectiveShape
199 virtual std::shared_ptr<SHAPE> GetEffectiveShape( PCB_LAYER_ID aLayer = UNDEFINED_LAYER,
200 FLASHING aFlash = FLASHING::DEFAULT ) const override;
201
202 /*
203 * Add two rectangular polygons separately bounding the barcode's symbol and the barcode's text.
204 */
205 void GetBoundingHull( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer, int aClearance,
206 int aMaxError, ERROR_LOC aErrorLoc = ERROR_INSIDE ) const;
207
214 void ComputeBarcode() const;
215
222 void ComputeTextPoly() const;
223
228 void AssembleBarcode() const;
229
235 void SetText( const wxString& aText );
236 wxString GetText() const;
237 wxString GetShownText() const;
238
242 const std::vector<TEXT_VAR_REF_KEY>& GetTextVarReferences() const { return m_text.GetTextVarReferences(); }
243
253 void Move( const VECTOR2I& offset ) override;
254
262 void Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle ) override;
263
271 void Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipLeftRight ) override;
272
273 void OnFootprintRescaled( double aRatioX, double aRatioY, double aLinearFactor, const VECTOR2I& aAnchor,
274 const EDA_ANGLE& aParentRotate ) override;
275
276 const VECTOR2I& GetLibraryPos() const { return m_libPos; }
277 const EDA_ANGLE& GetLibraryAngle() const { return m_libAngle; }
278
279 void StyleFromSettings( const BOARD_DESIGN_SETTINGS& settings, bool aCheckSide ) override;
280
284 VECTOR2I GetCenter() const override { return GetPosition(); }
285
292 void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
293
301 bool HitTest( const VECTOR2I& aPosition, int aAccuracy ) const override;
302
310 bool HitTest( const BOX2I& aRect, bool aContained, int aAccuracy = 0 ) const override;
311
315 wxString GetClass() const override { return wxT( "BARCODE" ); }
316
317 // Virtual function
323 const BOX2I GetBoundingBox() const override;
324
332 wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const override;
333
337 BITMAPS GetMenuImage() const override;
338
344 EDA_ITEM* Clone() const override;
345
346 void swapData( BOARD_ITEM* aImage ) override;
347
351 virtual const BOX2I ViewBBox() const override;
352
353 double ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const override;
354
361 double Similarity( const BOARD_ITEM& aItem ) const override;
362
363 static int Compare( const PCB_BARCODE* aBarcode, const PCB_BARCODE* aOther );
364
371 bool operator==( const BOARD_ITEM& aItem ) const override;
372
373 bool operator==( const PCB_BARCODE& aBarcode ) const;
374
378 BARCODE_T GetKind() const { return m_kind; }
379 void SetKind( BARCODE_T aKind );
380 void SetBarcodeKind( BARCODE_T aKind ); // Includes re-compute
381
382 bool KeepSquare() const
383 {
384 return m_kind == BARCODE_T::QR_CODE
387 }
388
394 void SetErrorCorrection( BARCODE_ECC_T aErrorCorrection );
396 void SetBarcodeErrorCorrection( BARCODE_ECC_T aErrorCorrection ); // Includes re-compute
397
398 void SetBarcodeText( const wxString& aText ) { SetText( aText ); AssembleBarcode(); }
399 void SetShowText( bool aShow ) { m_text.SetVisible( aShow ); AssembleBarcode(); }
400 bool GetShowText() const { return m_text.IsVisible(); }
401
402 void SetBarcodeWidth( int aWidth );
403 void SetBarcodeHeight( int aHeight );
404
405 EDA_ANGLE GetAngle() const;
406 double GetOrientation() const { return GetAngle().AsDegrees(); }
407 void SetOrientation( double aDegrees )
408 {
409 EDA_ANGLE newAngle( aDegrees, DEGREES_T );
410 EDA_ANGLE oldAngle = GetAngle();
411
412 if( newAngle != oldAngle )
413 {
414 Rotate( GetPosition(), newAngle - oldAngle );
415 }
416 }
417
418 int GetMarginX() const { return m_margin.x; }
419 int GetMarginY() const { return m_margin.y; }
420
421 void SetMarginX( int aX )
422 {
423 aX = std::max( pcbIUScale.mmToIU( 1 ), aX );
424 m_margin.x = aX;
426 }
427
428 void SetMarginY( int aY )
429 {
430 aY = std::max( pcbIUScale.mmToIU( 1 ), aY );
431 m_margin.y = aY;
433 }
434
435 bool IsKnockout() const override { return BOARD_ITEM::IsKnockout(); }
436 void SetIsKnockout( bool aEnable ) override
437 {
438 BOARD_ITEM::SetIsKnockout( aEnable );
440 }
441
442 const wxString& GetLastError() const
443 {
445 return m_cache->lastError;
446 }
447
448private:
457
458 mutable std::unique_ptr<PCB_BARCODE_CACHE> m_cache;
459
463 size_t computeCacheKey() const;
464
468 void rescaleSymbolPoly( const VECTOR2I& aTopLeft, const VECTOR2I& aBotRight ) const;
469};
470
471#endif // DIMENSION_H_
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
@ ERROR_INSIDE
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
BITMAPS
A list of all bitmap identifiers.
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
Container for design settings for a BOARD object.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
BOARD_ITEM(BOARD_ITEM *aParent, KICAD_T idtype, PCB_LAYER_ID aLayer=F_Cu)
Definition board_item.h:83
virtual bool IsKnockout() const
Definition board_item.h:352
virtual void SetIsKnockout(bool aKnockout)
Definition board_item.h:353
double AsDegrees() const
Definition eda_angle.h:116
The base class for create windows for drawing purpose.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:37
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:63
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition richio.h:62
EDA_MSG_PANEL items for displaying messages.
Definition msgpanel.h:50
void SetKind(BARCODE_T aKind)
EDA_ANGLE m_libAngle
Angle, FP-relative when in a footprint, board absolute otherwise.
void SetTextSize(int aTextSize)
Change the height of the human-readable text displayed below the barcode.
VECTOR2I GetCenter() const override
Get the centre of the barcode (alias for GetPosition).
double GetOrientation() const
const VECTOR2I & GetLibraryPos() const
std::unique_ptr< PCB_BARCODE_CACHE > m_cache
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
const VECTOR2I & GetMargin() const
Get the barcode margin (in internal units).
~PCB_BARCODE()
Destructor.
const BOX2I GetBoundingBox() const override
Get the axis-aligned bounding box of the barcode including text.
void SetBarcodeErrorCorrection(BARCODE_ECC_T aErrorCorrection)
virtual const BOX2I ViewBBox() const override
Get the bbox used for drawing/view culling, may include additional view-only extents.
void SetMargin(const VECTOR2I &aMargin)
EDA_ITEM * Clone() const override
Create a copy of this item.
void SetErrorCorrection(BARCODE_ECC_T aErrorCorrection)
Set the error correction level used for QR codes.
void SetBarcodeHeight(int aHeight)
void SetShowText(bool aShow)
const EDA_ANGLE & GetLibraryAngle() const
void SetMarginY(int aY)
void StyleFromSettings(const BOARD_DESIGN_SETTINGS &settings, bool aCheckSide) override
bool KeepSquare() const
void SetBarcodeText(const wxString &aText)
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Produce a short human-readable description of the item for UI lists.
void SetWidth(int aWidth)
void AssembleBarcode() const
Assemble the barcode polygon and text polygons into a single polygonal representation.
void SetBarcodeKind(BARCODE_T aKind)
void GetBoundingHull(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aMaxError, ERROR_LOC aErrorLoc=ERROR_INSIDE) const
void SetHeight(int aHeight)
PCB_BARCODE & operator=(const PCB_BARCODE &aOther)
Copy assignment operator.
VECTOR2I GetPosition() const override
Get the position (center) of the barcode in internal units.
void ComputeTextPoly() const
Generate the internal polygon representation for the human-readable text.
const std::vector< TEXT_VAR_REF_KEY > & GetTextVarReferences() const
Return the text variable references used by the barcode's content string.
wxString GetText() const
void SetPosition(const VECTOR2I &aPos) override
void SetMarginX(int aX)
double ViewGetLOD(int aLayer, const KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
void SetOrientation(double aDegrees)
double Similarity(const BOARD_ITEM &aItem) const override
Compute a simple similarity score between this barcode and another board item.
int m_height
Barcode height.
PCB_TEXT m_text
void SetLayer(PCB_LAYER_ID aLayer) override
Set the drawing layer for the barcode and its text.
bool operator==(const BOARD_ITEM &aItem) const override
Equality comparison operator for board-level deduplication.
VECTOR2I m_margin
Margin around the barcode (only valid for knockout)
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Populate message panel information entries (e.g.
wxString GetClass() const override
Return the class name for display/debugging purposes.
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
int GetTextSize() const
void ComputeBarcode() const
Generate the internal polygon representation for the current barcode text, kind and error correction.
void SetBarcodeWidth(int aWidth)
bool IsKnockout() const override
int GetHeight() const
Get the barcode height (in internal units).
BITMAPS GetMenuImage() const override
Icon to show in context menus/toolbars for this item type.
wxString GetShownText() const
size_t computeCacheKey() const
Compute a hash of all cache-key inputs (shown text + geometry parameters + layer).
BARCODE_ECC_T m_errorCorrection
Error correction level for QR codes.
void SetIsKnockout(bool aEnable) override
static bool ClassOf(const EDA_ITEM *aItem)
Type-check helper.
void rescaleSymbolPoly(const VECTOR2I &aTopLeft, const VECTOR2I &aBotRight) const
Scale and translate the symbol polygon to fill the given bounding rectangle.
const SHAPE_POLY_SET & GetPolyShape() const
Access the underlying polygonal representation generated for the barcode.
bool HitTest(const VECTOR2I &aPosition, int aAccuracy) const override
Hit-test a point against the barcode (text and symbol area).
PCB_BARCODE(BOARD_ITEM *aParent)
Construct a PCB_BARCODE.
const wxString & GetLastError() const
void OnFootprintRescaled(double aRatioX, double aRatioY, double aLinearFactor, const VECTOR2I &aAnchor, const EDA_ANGLE &aParentRotate) override
Apply a parent footprint scale to this item.
int m_width
Barcode width.
const SHAPE_POLY_SET & GetTextPoly() const
Access the cached polygon for the human-readable text only (already scaled/placed).
const SHAPE_POLY_SET & GetSymbolPoly() const
Access the cached polygon for the barcode symbol only (no text, no margins/knockout).
VECTOR2I m_libPos
Position, FP-relative when in a footprint, board absolute otherwise.
int GetMarginY() const
void swapData(BOARD_ITEM *aImage) override
BARCODE_ECC_T GetErrorCorrection() const
bool GetShowText() const
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate the barcode around a given centre by the given angle.
int GetMarginX() const
BARCODE_T m_kind
static int Compare(const PCB_BARCODE *aBarcode, const PCB_BARCODE *aOther)
void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aMaxError, ERROR_LOC aErrorLoc=ERROR_INSIDE, bool ignoreLineWidth=false) const override
Convert the barcode (text + symbol shapes) to polygonal geometry suitable for filling/collision tests...
EDA_ANGLE GetAngle() const
void Flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipLeftRight) override
Flip the barcode horizontally or vertically around a centre point.
BARCODE_T GetKind() const
Returns the type of the barcode (QR, CODE_39, etc.).
int GetWidth() const
Get the barcode width (in internal units).
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
void SetText(const wxString &aText)
Set the barcode content text to encode.
void Move(const VECTOR2I &offset) override
Function Move.
Represent a set of closed polygons.
@ DEGREES_T
Definition eda_angle.h:31
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition layer_ids.h:180
@ DEFAULT
Flashing follows connectivity.
Definition layer_ids.h:181
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ UNDEFINED_LAYER
Definition layer_ids.h:57
#define H(x, y, z)
Definition md5_hash.cpp:17
FLIP_DIRECTION
Definition mirror.h:23
BARCODE_ECC_T
Definition pcb_barcode.h:49
BARCODE_T
Definition pcb_barcode.h:40
#define DECLARE_ENUM_TO_WXANY(type)
Definition property.h:789
SHAPE_POLY_SET poly
Definition pcb_barcode.h:65
SHAPE_POLY_SET textPoly
Definition pcb_barcode.h:67
size_t keyHash
Hash of all cache-key inputs (via computeCacheKey)
Definition pcb_barcode.h:62
SHAPE_POLY_SET symbolPoly
Definition pcb_barcode.h:66
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:94
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683