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
85void PCB_BITMAP::SetImage( wxImage* aImage )
86{
87 m_bitmapBase->SetImage( aImage );
89}
90
91
92bool PCB_BITMAP::ReadImageFile( const wxString& aFullFilename )
93{
94 if( m_bitmapBase->ReadImageFile( aFullFilename ) )
95 {
97 return true;
98 }
99
100 return false;
101}
102
103
105{
106 return new PCB_BITMAP( *this );
107}
108
109
111{
112 wxCHECK_RET( aItem->Type() == PCB_BITMAP_T,
113 wxString::Format( wxT( "PCB_BITMAP object cannot swap data with %s object." ),
114 aItem->GetClass() ) );
115
116 PCB_BITMAP* item = (PCB_BITMAP*) aItem;
117 std::swap( m_layer, item->m_layer );
118 std::swap( m_isKnockout, item->m_isKnockout );
119 std::swap( m_isLocked, item->m_isLocked );
120 std::swap( m_flags, item->m_flags );
121 std::swap( m_parent, item->m_parent );
122 std::swap( m_forceVisible, item->m_forceVisible );
123 std::swap( m_pos, item->m_pos );
124 std::swap( m_bitmapBase, item->m_bitmapBase );
125}
126
127
128double PCB_BITMAP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
129{
130 constexpr double HIDE = std::numeric_limits<double>::max();
131
132 // All bitmaps are drawn on LAYER_DRAW_BITMAPS, but their
133 // associated board layer controls their visibility.
134 if( !GetBoard()->IsLayerVisible( m_layer ) )
135 return HIDE;
136
137 return aView->IsLayerVisible( LAYER_DRAW_BITMAPS ) ? 0.0 : HIDE;
138}
139
140
142{
143 // Bitmaps are center origin, BOX2Is need top-left origin
144 VECTOR2I size = m_bitmapBase->GetSize();
145 VECTOR2I topLeft = { m_pos.x - size.x / 2, m_pos.y - size.y / 2 };
146
147 return BOX2I( topLeft, size );
148}
149
150
151std::shared_ptr<SHAPE> PCB_BITMAP::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
152{
153 BOX2I box = GetBoundingBox();
154 return std::shared_ptr<SHAPE_RECT>( new SHAPE_RECT( box.GetPosition(), box.GetWidth(),
155 box.GetHeight() ) );
156}
157
158
160{
161 return m_bitmapBase->GetSize();
162}
163
164
165void PCB_BITMAP::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
166{
167 if( aFlipLeftRight )
168 {
169 MIRROR( m_pos.x, aCentre.x );
170 m_bitmapBase->Mirror( false );
171 }
172 else
173 {
174 MIRROR( m_pos.y, aCentre.y );
175 m_bitmapBase->Mirror( true );
176 }
177}
178
179void PCB_BITMAP::Rotate( const VECTOR2I& aCenter, const EDA_ANGLE& aAngle )
180{
181 EDA_ANGLE norm( aAngle.AsDegrees(), DEGREES_T );
182
183 RotatePoint( m_pos, aCenter, aAngle );
184
185 norm.Normalize();
186
187 // each call to m_bitmapBase->Rotate() rotates 90 degrees CCW
188 for( double ang = 45.0; ang < norm.AsDegrees(); ang += 90.0 )
189 m_bitmapBase->Rotate( false );
190}
191
192
193#if defined( DEBUG )
194void PCB_BITMAP::Show( int nestLevel, std::ostream& os ) const
195{
196 // XML output:
197 wxString s = GetClass();
198
199 NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << "/>\n";
200}
201#endif
202
203
204bool PCB_BITMAP::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
205{
206 BOX2I rect = GetBoundingBox();
207
208 rect.Inflate( aAccuracy );
209
210 return rect.Contains( aPosition );
211}
212
213
214bool PCB_BITMAP::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
215{
216 BOX2I rect = aRect;
217
218 rect.Inflate( aAccuracy );
219
220 if( aContained )
221 return rect.Contains( GetBoundingBox() );
222
223 return rect.Intersects( GetBoundingBox() );
224}
225
226
228{
229 return BITMAPS::image;
230}
231
232
233void PCB_BITMAP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
234{
235 aList.emplace_back( _( "Bitmap" ), wxEmptyString );
236
237 aList.emplace_back( _( "PPI" ), wxString::Format( wxT( "%d "), GetImage()->GetPPI() ) );
238 aList.emplace_back( _( "Scale" ), wxString::Format( wxT( "%f "), GetImageScale() ) );
239
240 aList.emplace_back( _( "Width" ), aFrame->MessageTextFromValue( GetSize().x ) );
241 aList.emplace_back( _( "Height" ), aFrame->MessageTextFromValue( GetSize().y ) );
242 aList.emplace_back( _( "Layer" ), LayerName( m_layer ) );
243}
244
245
246void PCB_BITMAP::ViewGetLayers( int aLayers[], int& aCount ) const
247{
248 aCount = 1;
249 aLayers[0] = BITMAP_LAYER_FOR( m_layer );
250}
251
252
253static struct PCB_BITMAP_DESC
254{
256 {
260
261 const wxString groupBitmap = _HKI( "Bitmap Properties" );
262
263 propMgr.AddProperty( new PROPERTY<PCB_BITMAP, double>( _HKI( "Scale" ),
265 groupBitmap );
266
267 // For future use
268 const wxString greyscale = _HKI( "Greyscale" );
269 }
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:847
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:52
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:69
void SetImage(wxImage *aImage)
Definition: bitmap_base.cpp:73
bool ReadImageFile(const wxString &aFullFilename)
Reads and stores in memory an image file.
int GetPPI() const
Definition: bitmap_base.h:123
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:71
bool m_isKnockout
Definition: board_item.h:346
PCB_LAYER_ID m_layer
Definition: board_item.h:345
bool m_isLocked
Definition: board_item.h:348
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:44
const Vec & GetPosition() const
Definition: box2.h:184
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:269
coord_type GetHeight() const
Definition: box2.h:188
coord_type GetWidth() const
Definition: box2.h:187
bool Contains(const Vec &aPoint) const
Definition: box2.h:141
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:506
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:239
bool m_forceVisible
Definition: eda_item.h:479
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:480
virtual wxString GetClass() const =0
Return the class name.
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:478
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:69
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:410
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:145
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_bitmap.cpp:104
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:151
void SetImageScale(double aScale)
Definition: pcb_bitmap.h:78
double GetImageScale() const
Definition: pcb_bitmap.h:76
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:110
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Definition: pcb_bitmap.cpp:246
wxString GetClass() const override
Return the class name.
Definition: pcb_bitmap.h:85
const VECTOR2I GetSize() const
Definition: pcb_bitmap.cpp:159
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_bitmap.cpp:128
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: pcb_bitmap.cpp:141
const BITMAP_BASE * GetImage() const
Definition: pcb_bitmap.h:53
void SetImage(wxImage *aImage)
Definition: pcb_bitmap.cpp:85
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:204
bool ReadImageFile(const wxString &aFullFilename)
Read and store an image file.
Definition: pcb_bitmap.cpp:92
void Rotate(const VECTOR2I &aCenter, const EDA_ANGLE &aAngle) override
Rotate this object.
Definition: pcb_bitmap.cpp:179
PCB_BITMAP & operator=(const BOARD_ITEM &aItem)
Definition: pcb_bitmap.cpp:63
VECTOR2I m_pos
Definition: pcb_bitmap.h:144
void Flip(const VECTOR2I &aCentre, bool aFlipLeftRight) override
Flip this object, i.e.
Definition: pcb_bitmap.cpp:165
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:233
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: pcb_bitmap.cpp:227
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:267
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition: layer_ids.h:147
@ LAYER_DRAW_BITMAPS
to handle and draw images bitmaps
Definition: layer_ids.h:223
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
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
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
#define TYPE_HASH(x)
Definition: property.h:63
#define REGISTER_TYPE(x)
Definition: property_mgr.h:328
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