KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_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 The 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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
28#include "sch_bitmap.h"
29
30#include <bitmap_base.h>
31#include <bitmaps.h>
32#include <base_units.h>
33#include <common.h>
34#include <core/mirror.h>
35#include <eda_draw_frame.h>
37#include <plotters/plotter.h>
38#include <sch_draw_panel.h>
40#include <trigo.h>
41#include <api/api_utils.h>
42#include <api/schematic/schematic_types.pb.h>
43
44#include <properties/property.h>
46
47
49 SCH_ITEM( nullptr, SCH_BITMAP_T ),
51{
52 m_referenceImage.SetPosition( pos );
53 m_layer = LAYER_NOTES; // used only to draw/plot a rectangle,
54 // when a bitmap cannot be drawn or plotted
55}
56
57
58SCH_BITMAP::SCH_BITMAP( const SCH_BITMAP& aSchBitmap ) :
59 SCH_ITEM( aSchBitmap ),
61{
62}
63
64
66{
67 wxCHECK_MSG( Type() == aItem.Type(), *this,
68 wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
69 GetClass() );
70
71 if( &aItem != this )
72 {
73 SCH_ITEM::operator=( aItem );
74
75 const SCH_BITMAP& bitmap = static_cast<const SCH_BITMAP&>( aItem );
77 }
78
79 return *this;
80}
81
82
84{
85 return new SCH_BITMAP( *this );
86}
87
88
90{
91 wxCHECK_RET( aItem->Type() == SCH_BITMAP_T,
92 wxString::Format( wxT( "SCH_BITMAP object cannot swap data with %s object." ),
93 aItem->GetClass() ) );
94
95 SCH_BITMAP* item = (SCH_BITMAP*) aItem;
96 m_referenceImage.SwapData( item->m_referenceImage );
97}
98
99
101{
102 return m_referenceImage.GetBoundingBox();
103}
104
105
107{
108 return m_referenceImage.GetPosition();
109}
110
111
112void SCH_BITMAP::SetPosition( const VECTOR2I& aPosition )
113{
114 m_referenceImage.SetPosition( aPosition );
115}
116
117
118void SCH_BITMAP::Move( const VECTOR2I& aMoveVector )
119{
120 SetPosition( GetPosition() + aMoveVector );
121}
122
123
124void SCH_BITMAP::Serialize( google::protobuf::Any& aContainer ) const
125{
126 using namespace kiapi::common;
127
128 kiapi::schematic::types::SchematicImage image;
129
130 image.mutable_id()->set_value( m_Uuid.AsStdString() );
131 PackVector2( *image.mutable_position(), m_referenceImage.GetPosition(), schIUScale );
132 PackVector2( *image.mutable_transform_origin_offset(), m_referenceImage.GetTransformOriginOffset(), schIUScale );
133
134 image.mutable_image_scale()->set_value( m_referenceImage.GetImageScale() );
135 image.set_locked( IsLocked() ? types::LockedState::LS_LOCKED : types::LockedState::LS_UNLOCKED );
136
137 m_referenceImage.PackToBytes( *image.mutable_image_data() );
138
139 aContainer.PackFrom( image );
140}
141
142
143bool SCH_BITMAP::Deserialize( const google::protobuf::Any& aContainer )
144{
145 using namespace kiapi::common;
146
147 kiapi::schematic::types::SchematicImage image;
148
149 if( !aContainer.UnpackTo( &image ) )
150 return false;
151
152 const_cast<KIID&>( m_Uuid ) = KIID( image.id().value() );
153
154 if( !image.image_data().empty() )
155 {
156 if( !m_referenceImage.UnpackFromBytes( image.image_data() ) )
157 return false;
158 }
159
160 if( image.has_image_scale() )
161 m_referenceImage.SetImageScale( image.image_scale().value() );
162
163 SetPosition( UnpackVector2( image.position(), schIUScale ) );
164 m_referenceImage.SetTransformOriginOffset( UnpackVector2( image.transform_origin_offset(), schIUScale ) );
165
166 SetLocked( image.locked() == types::LockedState::LS_LOCKED );
167 return true;
168}
169
170
172{
174}
175
176
178{
180}
181
182
183void SCH_BITMAP::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
184{
185 m_referenceImage.Rotate( aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
186}
187
188
189#if defined(DEBUG)
190void SCH_BITMAP::Show( int nestLevel, std::ostream& os ) const
191{
192 // XML output:
193 wxString s = GetClass();
194
195 NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << GetPosition() << "/>\n";
196}
197#endif
198
199
200bool SCH_BITMAP::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
201{
202 return KIGEOM::BoxHitTest( aPosition, GetBoundingBox(), aAccuracy );
203}
204
205
206bool SCH_BITMAP::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
207{
208 return KIGEOM::BoxHitTest( aRect, GetBoundingBox(), aContained, aAccuracy );
209}
210
211
212bool SCH_BITMAP::HitTest( const SHAPE_LINE_CHAIN& aPoly, bool aContained ) const
213{
214 return KIGEOM::BoxHitTest( aPoly, GetBoundingBox(), aContained );
215}
216
217
218
219void SCH_BITMAP::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
220 int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
221{
222 if( aBackground )
223 {
224 RENDER_SETTINGS* cfg = aPlotter->RenderSettings();
225
226 m_referenceImage.GetImage().PlotImage( aPlotter, GetPosition(),
227 cfg->GetLayerColor( GetLayer() ),
228 cfg->GetDefaultPenWidth() );
229 }
230}
231
232
234{
235 return BITMAPS::image;
236}
237
238
239void SCH_BITMAP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
240{
241 aList.emplace_back( _( "Bitmap" ), wxEmptyString );
242
243 aList.emplace_back( _( "PPI" ),
244 wxString::Format( wxT( "%d " ), m_referenceImage.GetImage().GetPPI() ) );
245 aList.emplace_back( _( "Scale" ),
246 wxString::Format( wxT( "%f " ), m_referenceImage.GetImageScale() ) );
247
248 aList.emplace_back( _( "Width" ),
249 aFrame->MessageTextFromValue( m_referenceImage.GetSize().x ) );
250 aList.emplace_back( _( "Height" ),
251 aFrame->MessageTextFromValue( m_referenceImage.GetSize().y ) );
252}
253
254
255std::vector<int> SCH_BITMAP::ViewGetLayers() const
256{
258}
259
260
261bool SCH_BITMAP::operator==( const SCH_ITEM& aItem ) const
262{
263 if( Type() != aItem.Type() )
264 return false;
265
266 const SCH_BITMAP& bitmap = static_cast<const SCH_BITMAP&>( aItem );
267 return m_referenceImage == bitmap.m_referenceImage;
268}
269
270
271double SCH_BITMAP::Similarity( const SCH_ITEM& aItem ) const
272{
273 if( Type() != aItem.Type() )
274 return 0.0;
275
276 if( m_Uuid == aItem.m_Uuid )
277 return 1.0;
278
279 const SCH_BITMAP& bitmap = static_cast<const SCH_BITMAP&>( aItem );
280 return m_referenceImage.Similarity( bitmap.m_referenceImage );
281}
282
283
285{
286 return m_referenceImage.GetTransformOriginOffset().x;
287}
288
289
291{
292 VECTOR2I offset = m_referenceImage.GetTransformOriginOffset();
293 offset.x = aX;
294 m_referenceImage.SetTransformOriginOffset( offset );
295}
296
297
299{
300 return m_referenceImage.GetTransformOriginOffset().y;
301}
302
303
305{
306 VECTOR2I offset = m_referenceImage.GetTransformOriginOffset();
307 offset.y = aY;
308 m_referenceImage.SetTransformOriginOffset( offset );
309}
310
311
313{
314 return m_referenceImage.GetImageScale();
315}
316
317
318void SCH_BITMAP::SetImageScale( double aScale )
319{
320 m_referenceImage.SetImageScale( aScale );
321}
322
323
325{
326 return m_referenceImage.GetImage().GetSize().x;
327}
328
329
330void SCH_BITMAP::SetWidth( int aWidth )
331{
332 m_referenceImage.SetWidth( aWidth );
333}
334
335
337{
338 return m_referenceImage.GetImage().GetSize().y;
339}
340
341
342void SCH_BITMAP::SetHeight( int aHeight )
343{
344 m_referenceImage.SetHeight( aHeight );
345}
346
347
348static struct SCH_BITMAP_DESC
349{
351 {
355
356 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Position X" ),
359
360
361 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Position Y" ),
364
365 const wxString groupImage = _HKI( "Image Properties" );
366
367 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, double>( _HKI( "Scale" ),
369 groupImage );
370
371 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Transform Offset X" ),
375 groupImage );
376
377 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Transform Offset Y" ),
381 groupImage );
382
383 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Width" ),
386 groupImage );
387
388 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Height" ),
391 groupImage );
392 }
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:127
BITMAPS
A list of all bitmap identifiers.
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
The base class for create windows for drawing purpose.
const KIID m_Uuid
Definition eda_item.h:528
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:112
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:41
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
Definition kiid.h:48
Base plotter engine class.
Definition plotter.h:136
RENDER_SETTINGS * RenderSettings()
Definition plotter.h:167
Provide class metadata.Helper macro to map type hashes to names.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
Object to handle a bitmap image that can be inserted in a schematic.
Definition sch_bitmap.h:40
void Rotate(const VECTOR2I &aCenter, bool aRotateCCW) override
Rotate the item around aCenter 90 degrees in the clockwise direction.
double GetImageScale() const
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
SCH_BITMAP(const VECTOR2I &pos=VECTOR2I(0, 0))
void SetWidth(int aWidth)
VECTOR2I GetPosition() const override
int GetTransformOriginOffsetY() const
void SetHeight(int aHeight)
virtual std::vector< int > ViewGetLayers() const override
int GetWidth() const
int GetY() const
Definition sch_bitmap.h:60
void Plot(PLOTTER *aPlotter, bool aBackground, const SCH_PLOT_OPTS &aPlotOpts, int aUnit, int aBodyStyle, const VECTOR2I &aOffset, bool aDimmed) override
Plot the item to aPlotter.
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
wxString GetClass() const override
Return the class name.
Definition sch_bitmap.h:68
void MirrorVertically(int aCenter) override
Mirror item vertically about aCenter.
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.
void swapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
double Similarity(const SCH_ITEM &aOther) const override
Return a measure of how likely the other object is to represent the same object.
int GetHeight() const
void SetY(int aY)
Definition sch_bitmap.h:61
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
int GetX() const
Definition sch_bitmap.h:57
void SetTransformOriginOffsetX(int aX)
void SetPosition(const VECTOR2I &aPosition) override
void Move(const VECTOR2I &aMoveVector) override
Move the item by aMoveVector to a new position.
SCH_BITMAP & operator=(const SCH_ITEM &aItem)
void SetX(int aX)
Definition sch_bitmap.h:58
void MirrorHorizontally(int aCenter) override
Mirror item horizontally about aCenter.
bool operator==(const SCH_ITEM &aOther) const override
REFERENCE_IMAGE m_referenceImage
Definition sch_bitmap.h:140
int GetTransformOriginOffsetX() const
void SetTransformOriginOffsetY(int aY)
void SetImageScale(double aScale)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:168
void SetLocked(bool aLocked) override
Definition sch_item.h:257
SCH_ITEM & operator=(const SCH_ITEM &aPin)
Definition sch_item.cpp:82
bool IsLocked() const override
Definition sch_item.cpp:152
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition sch_item.h:344
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType, int aUnit=0, int aBodyStyle=0)
Definition sch_item.cpp:56
wxString GetClass() const override
Return the class name.
Definition sch_item.h:178
SCH_LAYER_ID m_layer
Definition sch_item.h:781
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
The common library.
#define _(s)
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:413
static constexpr EDA_ANGLE ANGLE_270
Definition eda_angle.h:416
a few functions useful in geometry calculations.
@ LAYER_DRAW_BITMAPS
Draw images.
Definition layer_ids.h:284
@ LAYER_NOTES
Definition layer_ids.h:469
@ LAYER_SELECTION_SHADOWS
Definition layer_ids.h:497
@ LEFT_RIGHT
Flip left to right (around the Y axis)
Definition mirror.h:28
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:29
bool BoxHitTest(const VECTOR2I &aHitPoint, const BOX2I &aHittee, int aAccuracy)
Perform a point-to-box hit test.
KICOMMON_API VECTOR2I UnpackVector2(const types::Vector2 &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput, const EDA_IU_SCALE &aScale)
#define _HKI(x)
Definition page_info.cpp:44
#define TYPE_HASH(x)
Definition property.h:74
@ PT_COORD
Coordinate expressed in distance units (mm/inch)
Definition property.h:65
#define REGISTER_TYPE(x)
static struct SCH_BITMAP_DESC _SCH_BITMAP_DESC
@ SCH_BITMAP_T
Definition typeinfo.h:165
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687