KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_bitmap.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 (C) 2011 jean-pierre.charras
5 * Copyright (C) 2022 Mike Williams
6 * Copyright (C) 2011-2023 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
30#include <pcb_draw_panel_gal.h>
31#include <plotters/plotter.h>
33#include <bitmaps.h>
34#include <base_units.h>
35#include <common.h>
36#include <eda_draw_frame.h>
37#include <core/mirror.h>
38#include <board.h>
39#include <pcb_bitmap.h>
40#include <trigo.h>
41#include <geometry/shape_rect.h>
42
43#include <wx/mstream.h>
44
45
46PCB_BITMAP::PCB_BITMAP( BOARD_ITEM* aParent, const VECTOR2I& pos, PCB_LAYER_ID aLayer ) :
47 BOARD_ITEM( aParent, PCB_BITMAP_T, aLayer )
48{
49 m_pos = pos;
52}
53
54
55PCB_BITMAP::PCB_BITMAP( const PCB_BITMAP& aPCBBitmap ) : BOARD_ITEM( aPCBBitmap )
56{
57 m_pos = aPCBBitmap.m_pos;
58 m_bitmapBase = new BITMAP_BASE( *aPCBBitmap.m_bitmapBase );
60}
61
62
64{
65 wxCHECK_MSG( Type() == aItem.Type(), *this,
66 wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " )
67 + GetClass() );
68
69 if( &aItem != this )
70 {
71 BOARD_ITEM::operator=( aItem );
72
73 PCB_BITMAP* bitmap = (PCB_BITMAP*) &aItem;
74
75 delete m_bitmapBase;
76 m_bitmapBase = new BITMAP_BASE( *bitmap->m_bitmapBase );
77 m_pos = bitmap->m_pos;
79 }
80
81 return *this;
82}
83
84
85bool PCB_BITMAP::ReadImageFile( const wxString& aFullFilename )
86{
87 if( m_bitmapBase->ReadImageFile( aFullFilename ) )
88 {
90 return true;
91 }
92
93 return false;
94}
95
96
97bool PCB_BITMAP::ReadImageFile( wxMemoryBuffer& aBuffer )
98{
99 if( m_bitmapBase->ReadImageFile( aBuffer ) )
100 {
102 return true;
103 }
104
105 return false;
106}
107
108
110{
111 return new PCB_BITMAP( *this );
112}
113
114
116{
117 wxCHECK_RET( aItem->Type() == PCB_BITMAP_T,
118 wxString::Format( wxT( "PCB_BITMAP object cannot swap data with %s object." ),
119 aItem->GetClass() ) );
120
121 PCB_BITMAP* item = (PCB_BITMAP*) aItem;
122 std::swap( m_layer, item->m_layer );
123 std::swap( m_isKnockout, item->m_isKnockout );
124 std::swap( m_isLocked, item->m_isLocked );
125 std::swap( m_flags, item->m_flags );
126 std::swap( m_parent, item->m_parent );
127 std::swap( m_forceVisible, item->m_forceVisible );
128 std::swap( m_pos, item->m_pos );
129 std::swap( m_bitmapBase, item->m_bitmapBase );
130}
131
132
133double PCB_BITMAP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
134{
135 constexpr double HIDE = std::numeric_limits<double>::max();
136
137 // All bitmaps are drawn on LAYER_DRAW_BITMAPS, but their
138 // associated board layer controls their visibility.
139 if( !GetBoard()->IsLayerVisible( m_layer ) )
140 return HIDE;
141
142 return aView->IsLayerVisible( LAYER_DRAW_BITMAPS ) ? 0.0 : HIDE;
143}
144
145
147{
148 // Bitmaps are center origin, BOX2Is need top-left origin
149 VECTOR2I size = m_bitmapBase->GetSize();
150 VECTOR2I topLeft = { m_pos.x - size.x / 2, m_pos.y - size.y / 2 };
151
152 return BOX2I( topLeft, size );
153}
154
155
156std::shared_ptr<SHAPE> PCB_BITMAP::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
157{
158 BOX2I box = GetBoundingBox();
159 return std::shared_ptr<SHAPE_RECT>( new SHAPE_RECT( box.GetPosition(), box.GetWidth(),
160 box.GetHeight() ) );
161}
162
163
165{
166 return m_bitmapBase->GetSize();
167}
168
169
170void PCB_BITMAP::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
171{
172 if( aFlipLeftRight )
173 {
174 MIRROR( m_pos.x, aCentre.x );
175 m_bitmapBase->Mirror( false );
176 }
177 else
178 {
179 MIRROR( m_pos.y, aCentre.y );
180 m_bitmapBase->Mirror( true );
181 }
182}
183
184void PCB_BITMAP::Rotate( const VECTOR2I& aCenter, const EDA_ANGLE& aAngle )
185{
186 EDA_ANGLE norm( aAngle.AsDegrees(), DEGREES_T );
187
188 RotatePoint( m_pos, aCenter, aAngle );
189
190 norm.Normalize();
191
192 // each call to m_bitmapBase->Rotate() rotates 90 degrees CCW
193 for( double ang = 45.0; ang < norm.AsDegrees(); ang += 90.0 )
194 m_bitmapBase->Rotate( false );
195}
196
197
198#if defined( DEBUG )
199void PCB_BITMAP::Show( int nestLevel, std::ostream& os ) const
200{
201 // XML output:
202 wxString s = GetClass();
203
204 NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << "/>\n";
205}
206#endif
207
208
209bool PCB_BITMAP::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
210{
211 BOX2I rect = GetBoundingBox();
212
213 rect.Inflate( aAccuracy );
214
215 return rect.Contains( aPosition );
216}
217
218
219bool PCB_BITMAP::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
220{
221 BOX2I rect = aRect;
222
223 rect.Inflate( aAccuracy );
224
225 if( aContained )
226 return rect.Contains( GetBoundingBox() );
227
228 return rect.Intersects( GetBoundingBox() );
229}
230
231
233{
234 return BITMAPS::image;
235}
236
237
238void PCB_BITMAP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
239{
240 aList.emplace_back( _( "Bitmap" ), wxEmptyString );
241
242 aList.emplace_back( _( "PPI" ), wxString::Format( wxT( "%d "), GetImage()->GetPPI() ) );
243 aList.emplace_back( _( "Scale" ), wxString::Format( wxT( "%f "), GetImageScale() ) );
244
245 aList.emplace_back( _( "Width" ), aFrame->MessageTextFromValue( GetSize().x ) );
246 aList.emplace_back( _( "Height" ), aFrame->MessageTextFromValue( GetSize().y ) );
247 aList.emplace_back( _( "Layer" ), LayerName( m_layer ) );
248}
249
250
251void PCB_BITMAP::ViewGetLayers( int aLayers[], int& aCount ) const
252{
253 aCount = 1;
254 aLayers[0] = BITMAP_LAYER_FOR( m_layer );
255}
256
257
258static struct PCB_BITMAP_DESC
259{
261 {
265
266 const wxString groupBitmap = _HKI( "Bitmap Properties" );
267
268 propMgr.AddProperty( new PROPERTY<PCB_BITMAP, double>( _HKI( "Scale" ),
270 groupBitmap );
271
272 // For future use
273 const wxString greyscale = _HKI( "Greyscale" );
274 }
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
BOX2< VECTOR2I > BOX2I
Definition: box2.h:853
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:48
void Rotate(bool aRotateCCW)
Rotate image CW or CCW.
void Mirror(bool aVertically)
Mirror image vertically (i.e.
void SetPixelSizeIu(double aPixSize)
Definition: bitmap_base.h:65
bool ReadImageFile(const wxString &aFullFilename)
Reads and stores in memory an image file.
int GetPPI() const
Definition: bitmap_base.h:117
VECTOR2I GetSize() const
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
bool m_isKnockout
Definition: board_item.h:362
PCB_LAYER_ID m_layer
Definition: board_item.h:361
bool m_isLocked
Definition: board_item.h:364
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:45
const Vec & GetPosition() const
Definition: box2.h:185
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:270
coord_type GetHeight() const
Definition: box2.h:189
coord_type GetWidth() const
Definition: box2.h:188
bool Contains(const Vec &aPoint) const
Definition: box2.h:142
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:507
EDA_ANGLE Normalize()
Definition: eda_angle.h:249
double AsDegrees() const
Definition: eda_angle.h:149
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:85
EDA_ITEM & operator=(const EDA_ITEM &aItem)
Assign the members of aItem to another object.
Definition: eda_item.cpp:258
bool m_forceVisible
Definition: eda_item.h:486
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:487
virtual wxString GetClass() const =0
Return the class name.
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:485
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:409
Object to handle a bitmap image that can be inserted in a PCB.
Definition: pcb_bitmap.h:42
BITMAP_BASE * m_bitmapBase
Definition: pcb_bitmap.h:153
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_bitmap.cpp:109
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.
Definition: pcb_bitmap.cpp:156
void SetImageScale(double aScale)
Definition: pcb_bitmap.h:76
double GetImageScale() const
Definition: pcb_bitmap.h:74
PCB_BITMAP(BOARD_ITEM *aParent, const VECTOR2I &pos=VECTOR2I(0, 0), PCB_LAYER_ID aLayer=F_Cu)
Definition: pcb_bitmap.cpp:46
void swapData(BOARD_ITEM *aItem) override
Definition: pcb_bitmap.cpp:115
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Definition: pcb_bitmap.cpp:251
wxString GetClass() const override
Return the class name.
Definition: pcb_bitmap.h:83
const VECTOR2I GetSize() const
Definition: pcb_bitmap.cpp:164
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_bitmap.cpp:133
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: pcb_bitmap.cpp:146
const BITMAP_BASE * GetImage() const
Definition: pcb_bitmap.h:53
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
Definition: pcb_bitmap.cpp:209
bool ReadImageFile(const wxString &aFullFilename)
Read and store an image file.
Definition: pcb_bitmap.cpp:85
void Rotate(const VECTOR2I &aCenter, const EDA_ANGLE &aAngle) override
Rotate this object.
Definition: pcb_bitmap.cpp:184
PCB_BITMAP & operator=(const BOARD_ITEM &aItem)
Definition: pcb_bitmap.cpp:63
VECTOR2I m_pos
Definition: pcb_bitmap.h:152
void Flip(const VECTOR2I &aCentre, bool aFlipLeftRight) override
Flip this object, i.e.
Definition: pcb_bitmap.cpp:170
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Populate aList of MSG_PANEL_ITEM objects with it's internal state for display purposes.
Definition: pcb_bitmap.cpp:238
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: pcb_bitmap.cpp:232
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:74
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:76
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A lower-precision version of StringFromValue().
The common library.
#define _HKI(x)
#define _(s)
@ DEGREES_T
Definition: eda_angle.h:31
wxString LayerName(int aLayer)
Returns the default display name for a given layer.
Definition: layer_id.cpp:30
#define BITMAP_LAYER_FOR(boardLayer)
Macros for getting the extra layers for a given board layer.
Definition: layer_ids.h:268
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition: layer_ids.h:148
@ LAYER_DRAW_BITMAPS
to handle and draw images bitmaps
Definition: layer_ids.h:224
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
void MIRROR(T &aPoint, const T &aMirrorRef)
Updates aPoint with the mirror of aPoint relative to the aMirrorRef.
Definition: mirror.h:40
static struct PCB_BITMAP_DESC _PCB_BITMAP_DESC
#define TYPE_HASH(x)
Definition: property.h:64
#define REGISTER_TYPE(x)
Definition: property_mgr.h:356
constexpr int MilsToIU(int mils) const
Definition: base_units.h:94
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183
@ PCB_BITMAP_T
class PCB_BITMAP, bitmap on a layer
Definition: typeinfo.h:89