KiCad PCB EDA Suite
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-2022 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;
50 m_image = new BITMAP_BASE();
51 m_image->SetPixelSizeIu( (float) pcbIUScale.MilsToIU( 1000 ) / m_image->GetPPI() );
52}
53
54
55PCB_BITMAP::PCB_BITMAP( const PCB_BITMAP& aPCBBitmap ) : BOARD_ITEM( aPCBBitmap )
56{
57 m_pos = aPCBBitmap.m_pos;
58 m_image = new BITMAP_BASE( *aPCBBitmap.m_image );
59 m_image->SetPixelSizeIu( (float) pcbIUScale.MilsToIU( 1000 ) / m_image->GetPPI() );
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_image;
76 m_image = new BITMAP_BASE( *bitmap->m_image );
77 m_pos = bitmap->m_pos;
78 m_image->SetPixelSizeIu( (float) pcbIUScale.MilsToIU( 1000 ) / m_image->GetPPI() );
79 }
80
81 return *this;
82}
83
84
85bool PCB_BITMAP::ReadImageFile( const wxString& aFullFilename )
86{
87 return m_image->ReadImageFile( aFullFilename );
88}
89
90
92{
93 return new PCB_BITMAP( *this );
94}
95
96
98{
99 wxCHECK_RET( aItem->Type() == PCB_BITMAP_T,
100 wxString::Format( wxT( "PCB_BITMAP object cannot swap data with %s object." ),
101 aItem->GetClass() ) );
102
103 PCB_BITMAP* item = (PCB_BITMAP*) aItem;
104 std::swap( m_pos, item->m_pos );
105 std::swap( m_image, item->m_image );
106}
107
108
109double PCB_BITMAP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
110{
111 constexpr double HIDE = std::numeric_limits<double>::max();
112
113 // All bitmaps are drawn on LAYER_DRAW_BITMAPS, but their
114 // associated board layer controls their visibility.
115 if( !GetBoard()->IsLayerVisible( m_layer ) )
116 return HIDE;
117
118 return aView->IsLayerVisible( LAYER_DRAW_BITMAPS ) ? 0.0 : HIDE;
119}
120
121
123{
124 // Bitmaps are center origin, BOX2Is need top-left origin
125 VECTOR2I size = m_image->GetSize();
126 VECTOR2I topLeft = { m_pos.x - size.x / 2, m_pos.y - size.y / 2 };
127
128 return BOX2I( topLeft, size );
129}
130
131
132std::shared_ptr<SHAPE> PCB_BITMAP::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
133{
134 BOX2I box = GetBoundingBox();
135 return std::shared_ptr<SHAPE_RECT>( new SHAPE_RECT( box.GetCenter(), box.GetWidth(),
136 box.GetHeight() ) );
137}
138
139
141{
142 return m_image->GetSize();
143}
144
145
146void PCB_BITMAP::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
147{
148 if( aFlipLeftRight )
149 {
150 MIRROR( m_pos.x, aCentre.x );
151 m_image->Mirror( false );
152 }
153 else
154 {
155 MIRROR( m_pos.y, aCentre.y );
156 m_image->Mirror( true );
157 }
158}
159
160void PCB_BITMAP::Rotate( const VECTOR2I& aCenter, const EDA_ANGLE& aAngle )
161{
162 RotatePoint( m_pos, aCenter, aAngle );
163 m_image->Rotate( false );
164}
165
166
167#if defined( DEBUG )
168void PCB_BITMAP::Show( int nestLevel, std::ostream& os ) const
169{
170 // XML output:
171 wxString s = GetClass();
172
173 NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << "/>\n";
174}
175#endif
176
177
178bool PCB_BITMAP::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
179{
180 BOX2I rect = GetBoundingBox();
181
182 rect.Inflate( aAccuracy );
183
184 return rect.Contains( aPosition );
185}
186
187
188bool PCB_BITMAP::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
189{
190 BOX2I rect = aRect;
191
192 rect.Inflate( aAccuracy );
193
194 if( aContained )
195 return rect.Contains( GetBoundingBox() );
196
197 return rect.Intersects( GetBoundingBox() );
198}
199
200
202{
203 return BITMAPS::image;
204}
205
206
207void PCB_BITMAP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
208{
209 aList.emplace_back( _( "Bitmap" ), wxEmptyString );
210
211 aList.emplace_back( _( "Width" ), aFrame->MessageTextFromValue( GetSize().x ) );
212 aList.emplace_back( _( "Height" ), aFrame->MessageTextFromValue( GetSize().y ) );
213 aList.emplace_back( _( "Layer" ), LayerName( m_layer ) );
214}
215
216
217void PCB_BITMAP::ViewGetLayers( int aLayers[], int& aCount ) const
218{
219 aCount = 1;
220 aLayers[0] = BITMAP_LAYER_FOR( m_layer );
221}
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:51
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:67
bool ReadImageFile(const wxString &aFullFilename)
Reads and stores in memory an image file.
Definition: bitmap_base.cpp:88
int GetPPI() const
Definition: bitmap_base.h:134
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:58
PCB_LAYER_ID m_layer
Definition: board_item.h:329
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:43
const Vec GetCenter() const
Definition: box2.h:195
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
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:240
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
virtual wxString GetClass() const =0
Return the class name.
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
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_bitmap.cpp:91
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:132
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:97
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Definition: pcb_bitmap.cpp:217
wxString GetClass() const override
Return the class name.
Definition: pcb_bitmap.h:74
const VECTOR2I GetSize() const
Definition: pcb_bitmap.cpp:140
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_bitmap.cpp:109
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: pcb_bitmap.cpp:122
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:178
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:160
PCB_BITMAP & operator=(const BOARD_ITEM &aItem)
Definition: pcb_bitmap.cpp:63
VECTOR2I m_pos
Definition: pcb_bitmap.h:133
BITMAP_BASE * m_image
Definition: pcb_bitmap.h:134
void Flip(const VECTOR2I &aCentre, bool aFlipLeftRight) override
Flip this object, i.e.
Definition: pcb_bitmap.cpp:146
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:207
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: pcb_bitmap.cpp:201
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 _(s)
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
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
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