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, see <https://www.gnu.org/licenses/>.
19 */
20
24#include "sch_bitmap.h"
25
26#include <bitmap_base.h>
27#include <bitmaps.h>
28#include <base_units.h>
29#include <common.h>
30#include <core/mirror.h>
31#include <eda_draw_frame.h>
33#include <plotters/plotter.h>
34#include <sch_draw_panel.h>
36#include <trigo.h>
37#include <api/api_utils.h>
38#include <api/schematic/schematic_types.pb.h>
39
40#include <properties/property.h>
42
43
45 SCH_ITEM( nullptr, SCH_BITMAP_T ),
47{
48 m_referenceImage.SetPosition( pos );
49 m_layer = LAYER_NOTES; // used only to draw/plot a rectangle,
50 // when a bitmap cannot be drawn or plotted
51}
52
53
54SCH_BITMAP::SCH_BITMAP( const SCH_BITMAP& aSchBitmap ) :
55 SCH_ITEM( aSchBitmap ),
57{
58}
59
60
62{
63 wxCHECK_MSG( Type() == aItem.Type(), *this,
64 wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
65 GetClass() );
66
67 if( &aItem != this )
68 {
69 SCH_ITEM::operator=( aItem );
70
71 const SCH_BITMAP& bitmap = static_cast<const SCH_BITMAP&>( aItem );
73 }
74
75 return *this;
76}
77
78
80{
81 return new SCH_BITMAP( *this );
82}
83
84
86{
87 wxCHECK_RET( aItem->Type() == SCH_BITMAP_T,
88 wxString::Format( wxT( "SCH_BITMAP object cannot swap data with %s object." ),
89 aItem->GetClass() ) );
90
91 SCH_BITMAP* item = (SCH_BITMAP*) aItem;
92 m_referenceImage.SwapData( item->m_referenceImage );
93}
94
95
97{
98 return m_referenceImage.GetBoundingBox();
99}
100
101
103{
104 return m_referenceImage.GetPosition();
105}
106
107
108void SCH_BITMAP::SetPosition( const VECTOR2I& aPosition )
109{
110 m_referenceImage.SetPosition( aPosition );
111}
112
113
114void SCH_BITMAP::Move( const VECTOR2I& aMoveVector )
115{
116 SetPosition( GetPosition() + aMoveVector );
117}
118
119
120void SCH_BITMAP::Serialize( google::protobuf::Any& aContainer ) const
121{
122 using namespace kiapi::common;
123
124 kiapi::schematic::types::SchematicImage image;
125
126 image.mutable_id()->set_value( m_Uuid.AsStdString() );
127 PackVector2( *image.mutable_position(), m_referenceImage.GetPosition(), schIUScale );
128 PackVector2( *image.mutable_transform_origin_offset(), m_referenceImage.GetTransformOriginOffset(), schIUScale );
129
130 image.mutable_image_scale()->set_value( m_referenceImage.GetImageScale() );
131 image.set_locked( IsLocked() ? types::LockedState::LS_LOCKED : types::LockedState::LS_UNLOCKED );
132
133 m_referenceImage.PackToBytes( *image.mutable_image_data() );
134
135 aContainer.PackFrom( image );
136}
137
138
139bool SCH_BITMAP::Deserialize( const google::protobuf::Any& aContainer )
140{
141 using namespace kiapi::common;
142
143 kiapi::schematic::types::SchematicImage image;
144
145 if( !aContainer.UnpackTo( &image ) )
146 return false;
147
148 const_cast<KIID&>( m_Uuid ) = KIID( image.id().value() );
149
150 if( !image.image_data().empty() )
151 {
152 if( !m_referenceImage.UnpackFromBytes( image.image_data() ) )
153 return false;
154 }
155
156 if( image.has_image_scale() )
157 m_referenceImage.SetImageScale( image.image_scale().value() );
158
159 SetPosition( UnpackVector2( image.position(), schIUScale ) );
160 m_referenceImage.SetTransformOriginOffset( UnpackVector2( image.transform_origin_offset(), schIUScale ) );
161
162 SetLocked( image.locked() == types::LockedState::LS_LOCKED );
163 return true;
164}
165
166
168{
170}
171
172
174{
176}
177
178
179void SCH_BITMAP::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
180{
181 m_referenceImage.Rotate( aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
182}
183
184
185#if defined(DEBUG)
186void SCH_BITMAP::Show( int nestLevel, std::ostream& os ) const
187{
188 // XML output:
189 wxString s = GetClass();
190
191 NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << GetPosition() << "/>\n";
192}
193#endif
194
195
196bool SCH_BITMAP::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
197{
198 return KIGEOM::BoxHitTest( aPosition, GetBoundingBox(), aAccuracy );
199}
200
201
202bool SCH_BITMAP::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
203{
204 return KIGEOM::BoxHitTest( aRect, GetBoundingBox(), aContained, aAccuracy );
205}
206
207
208bool SCH_BITMAP::HitTest( const SHAPE_LINE_CHAIN& aPoly, bool aContained ) const
209{
210 return KIGEOM::BoxHitTest( aPoly, GetBoundingBox(), aContained );
211}
212
213
214
215void SCH_BITMAP::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
216 int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
217{
218 if( aBackground )
219 {
220 RENDER_SETTINGS* cfg = aPlotter->RenderSettings();
221
222 m_referenceImage.GetImage().PlotImage( aPlotter, GetPosition(),
223 cfg->GetLayerColor( GetLayer() ),
224 cfg->GetDefaultPenWidth() );
225 }
226}
227
228
230{
231 return BITMAPS::image;
232}
233
234
235void SCH_BITMAP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
236{
237 aList.emplace_back( _( "Bitmap" ), wxEmptyString );
238
239 aList.emplace_back( _( "PPI" ),
240 wxString::Format( wxT( "%d " ), m_referenceImage.GetImage().GetPPI() ) );
241 aList.emplace_back( _( "Scale" ),
242 wxString::Format( wxT( "%f " ), m_referenceImage.GetImageScale() ) );
243
244 aList.emplace_back( _( "Width" ),
245 aFrame->MessageTextFromValue( m_referenceImage.GetSize().x ) );
246 aList.emplace_back( _( "Height" ),
247 aFrame->MessageTextFromValue( m_referenceImage.GetSize().y ) );
248}
249
250
251std::vector<int> SCH_BITMAP::ViewGetLayers() const
252{
254}
255
256
257bool SCH_BITMAP::operator==( const SCH_ITEM& aItem ) const
258{
259 if( Type() != aItem.Type() )
260 return false;
261
262 const SCH_BITMAP& bitmap = static_cast<const SCH_BITMAP&>( aItem );
263 return m_referenceImage == bitmap.m_referenceImage;
264}
265
266
267double SCH_BITMAP::Similarity( const SCH_ITEM& aItem ) const
268{
269 if( Type() != aItem.Type() )
270 return 0.0;
271
272 if( m_Uuid == aItem.m_Uuid )
273 return 1.0;
274
275 const SCH_BITMAP& bitmap = static_cast<const SCH_BITMAP&>( aItem );
276 return m_referenceImage.Similarity( bitmap.m_referenceImage );
277}
278
279
281{
282 return m_referenceImage.GetTransformOriginOffset().x;
283}
284
285
287{
288 VECTOR2I offset = m_referenceImage.GetTransformOriginOffset();
289 offset.x = aX;
290 m_referenceImage.SetTransformOriginOffset( offset );
291}
292
293
295{
296 return m_referenceImage.GetTransformOriginOffset().y;
297}
298
299
301{
302 VECTOR2I offset = m_referenceImage.GetTransformOriginOffset();
303 offset.y = aY;
304 m_referenceImage.SetTransformOriginOffset( offset );
305}
306
307
309{
310 return m_referenceImage.GetImageScale();
311}
312
313
314void SCH_BITMAP::SetImageScale( double aScale )
315{
316 m_referenceImage.SetImageScale( aScale );
317}
318
319
321{
322 return m_referenceImage.GetImage().GetSize().x;
323}
324
325
326void SCH_BITMAP::SetWidth( int aWidth )
327{
328 m_referenceImage.SetWidth( aWidth );
329}
330
331
333{
334 return m_referenceImage.GetImage().GetSize().y;
335}
336
337
338void SCH_BITMAP::SetHeight( int aHeight )
339{
340 m_referenceImage.SetHeight( aHeight );
341}
342
343
344static struct SCH_BITMAP_DESC
345{
347 {
351
352 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Position X" ),
355
356
357 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Position Y" ),
360
361 const wxString groupImage = _HKI( "Image Properties" );
362
363 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, double>( _HKI( "Scale" ),
365 groupImage );
366
367 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Transform Offset X" ),
371 groupImage );
372
373 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Transform Offset Y" ),
377 groupImage );
378
379 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Width" ),
382 groupImage );
383
384 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Height" ),
387 groupImage );
388 }
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
BITMAPS
A list of all bitmap identifiers.
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
The base class for create windows for drawing purpose.
const KIID m_Uuid
Definition eda_item.h:531
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
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:44
Base plotter engine class.
Definition plotter.h:133
RENDER_SETTINGS * RenderSettings()
Definition plotter.h:164
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:36
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:56
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:64
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:57
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:53
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:54
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:136
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:162
void SetLocked(bool aLocked) override
Definition sch_item.h:251
SCH_ITEM & operator=(const SCH_ITEM &aPin)
Definition sch_item.cpp:78
bool IsLocked() const override
Definition sch_item.cpp:148
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition sch_item.h:338
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType, int aUnit=0, int aBodyStyle=0)
Definition sch_item.cpp:52
wxString GetClass() const override
Return the class name.
Definition sch_item.h:172
SCH_LAYER_ID m_layer
Definition sch_item.h:775
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:280
@ LAYER_NOTES
Definition layer_ids.h:465
@ LAYER_SELECTION_SHADOWS
Definition layer_ids.h:493
@ LEFT_RIGHT
Flip left to right (around the Y axis)
Definition mirror.h:24
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:25
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:40
#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:161
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683