You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
7.6 KiB

  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. /**
  26. * @file pcb_bitmap.cpp
  27. */
  28. #include <pcb_draw_panel_gal.h>
  29. #include <plotters/plotter.h>
  30. #include <settings/color_settings.h>
  31. #include <bitmaps.h>
  32. #include <base_units.h>
  33. #include <common.h>
  34. #include <eda_draw_frame.h>
  35. #include <core/mirror.h>
  36. #include <board.h>
  37. #include <pcb_bitmap.h>
  38. #include <trigo.h>
  39. #include <geometry/shape_rect.h>
  40. #include <wx/mstream.h>
  41. PCB_BITMAP::PCB_BITMAP( BOARD_ITEM* aParent, const VECTOR2I& pos, PCB_LAYER_ID aLayer ) :
  42. BOARD_ITEM( aParent, PCB_BITMAP_T, aLayer )
  43. {
  44. m_pos = pos;
  45. m_bitmapBase = new BITMAP_BASE();
  46. m_bitmapBase->SetPixelSizeIu( (float) pcbIUScale.MilsToIU( 1000 ) / m_bitmapBase->GetPPI() );
  47. }
  48. PCB_BITMAP::PCB_BITMAP( const PCB_BITMAP& aPCBBitmap ) : BOARD_ITEM( aPCBBitmap )
  49. {
  50. m_pos = aPCBBitmap.m_pos;
  51. m_bitmapBase = new BITMAP_BASE( *aPCBBitmap.m_bitmapBase );
  52. m_bitmapBase->SetPixelSizeIu( (float) pcbIUScale.MilsToIU( 1000 ) / m_bitmapBase->GetPPI() );
  53. }
  54. PCB_BITMAP& PCB_BITMAP::operator=( const BOARD_ITEM& aItem )
  55. {
  56. wxCHECK_MSG( Type() == aItem.Type(), *this,
  57. wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " )
  58. + GetClass() );
  59. if( &aItem != this )
  60. {
  61. BOARD_ITEM::operator=( aItem );
  62. PCB_BITMAP* bitmap = (PCB_BITMAP*) &aItem;
  63. delete m_bitmapBase;
  64. m_bitmapBase = new BITMAP_BASE( *bitmap->m_bitmapBase );
  65. m_pos = bitmap->m_pos;
  66. m_bitmapBase->SetPixelSizeIu( (float) pcbIUScale.MilsToIU( 1000 ) / m_bitmapBase->GetPPI() );
  67. }
  68. return *this;
  69. }
  70. void PCB_BITMAP::SetImage( wxImage* aImage )
  71. {
  72. m_bitmapBase->SetImage( aImage );
  73. m_bitmapBase->SetPixelSizeIu( (float) pcbIUScale.MilsToIU( 1000 ) / m_bitmapBase->GetPPI() );
  74. }
  75. bool PCB_BITMAP::ReadImageFile( const wxString& aFullFilename )
  76. {
  77. if( m_bitmapBase->ReadImageFile( aFullFilename ) )
  78. {
  79. m_bitmapBase->SetPixelSizeIu( (float) pcbIUScale.MilsToIU( 1000 ) / m_bitmapBase->GetPPI() );
  80. return true;
  81. }
  82. return false;
  83. }
  84. EDA_ITEM* PCB_BITMAP::Clone() const
  85. {
  86. return new PCB_BITMAP( *this );
  87. }
  88. void PCB_BITMAP::swapData( BOARD_ITEM* aItem )
  89. {
  90. wxCHECK_RET( aItem->Type() == PCB_BITMAP_T,
  91. wxString::Format( wxT( "PCB_BITMAP object cannot swap data with %s object." ),
  92. aItem->GetClass() ) );
  93. PCB_BITMAP* item = (PCB_BITMAP*) aItem;
  94. std::swap( m_layer, item->m_layer );
  95. std::swap( m_isKnockout, item->m_isKnockout );
  96. std::swap( m_isLocked, item->m_isLocked );
  97. std::swap( m_flags, item->m_flags );
  98. std::swap( m_parent, item->m_parent );
  99. std::swap( m_forceVisible, item->m_forceVisible );
  100. std::swap( m_pos, item->m_pos );
  101. std::swap( m_bitmapBase, item->m_bitmapBase );
  102. }
  103. double PCB_BITMAP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
  104. {
  105. constexpr double HIDE = std::numeric_limits<double>::max();
  106. // All bitmaps are drawn on LAYER_DRAW_BITMAPS, but their
  107. // associated board layer controls their visibility.
  108. if( !GetBoard()->IsLayerVisible( m_layer ) )
  109. return HIDE;
  110. return aView->IsLayerVisible( LAYER_DRAW_BITMAPS ) ? 0.0 : HIDE;
  111. }
  112. const BOX2I PCB_BITMAP::GetBoundingBox() const
  113. {
  114. // Bitmaps are center origin, BOX2Is need top-left origin
  115. VECTOR2I size = m_bitmapBase->GetSize();
  116. VECTOR2I topLeft = { m_pos.x - size.x / 2, m_pos.y - size.y / 2 };
  117. return BOX2I( topLeft, size );
  118. }
  119. std::shared_ptr<SHAPE> PCB_BITMAP::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
  120. {
  121. BOX2I box = GetBoundingBox();
  122. return std::shared_ptr<SHAPE_RECT>( new SHAPE_RECT( box.GetPosition(), box.GetWidth(),
  123. box.GetHeight() ) );
  124. }
  125. const VECTOR2I PCB_BITMAP::GetSize() const
  126. {
  127. return m_bitmapBase->GetSize();
  128. }
  129. void PCB_BITMAP::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
  130. {
  131. if( aFlipLeftRight )
  132. {
  133. MIRROR( m_pos.x, aCentre.x );
  134. m_bitmapBase->Mirror( false );
  135. }
  136. else
  137. {
  138. MIRROR( m_pos.y, aCentre.y );
  139. m_bitmapBase->Mirror( true );
  140. }
  141. }
  142. void PCB_BITMAP::Rotate( const VECTOR2I& aCenter, const EDA_ANGLE& aAngle )
  143. {
  144. EDA_ANGLE norm( aAngle.AsDegrees(), DEGREES_T );
  145. RotatePoint( m_pos, aCenter, aAngle );
  146. norm.Normalize();
  147. // each call to m_bitmapBase->Rotate() rotates 90 degrees CCW
  148. for( double ang = 45.0; ang < norm.AsDegrees(); ang += 90.0 )
  149. m_bitmapBase->Rotate( false );
  150. }
  151. #if defined( DEBUG )
  152. void PCB_BITMAP::Show( int nestLevel, std::ostream& os ) const
  153. {
  154. // XML output:
  155. wxString s = GetClass();
  156. NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << "/>\n";
  157. }
  158. #endif
  159. bool PCB_BITMAP::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  160. {
  161. BOX2I rect = GetBoundingBox();
  162. rect.Inflate( aAccuracy );
  163. return rect.Contains( aPosition );
  164. }
  165. bool PCB_BITMAP::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  166. {
  167. BOX2I rect = aRect;
  168. rect.Inflate( aAccuracy );
  169. if( aContained )
  170. return rect.Contains( GetBoundingBox() );
  171. return rect.Intersects( GetBoundingBox() );
  172. }
  173. BITMAPS PCB_BITMAP::GetMenuImage() const
  174. {
  175. return BITMAPS::image;
  176. }
  177. void PCB_BITMAP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  178. {
  179. aList.emplace_back( _( "Bitmap" ), wxEmptyString );
  180. aList.emplace_back( _( "PPI" ), wxString::Format( wxT( "%d "), GetImage()->GetPPI() ) );
  181. aList.emplace_back( _( "Scale" ), wxString::Format( wxT( "%f "), GetImageScale() ) );
  182. aList.emplace_back( _( "Width" ), aFrame->MessageTextFromValue( GetSize().x ) );
  183. aList.emplace_back( _( "Height" ), aFrame->MessageTextFromValue( GetSize().y ) );
  184. aList.emplace_back( _( "Layer" ), LayerName( m_layer ) );
  185. }
  186. void PCB_BITMAP::ViewGetLayers( int aLayers[], int& aCount ) const
  187. {
  188. aCount = 1;
  189. aLayers[0] = BITMAP_LAYER_FOR( m_layer );
  190. }
  191. static struct PCB_BITMAP_DESC
  192. {
  193. PCB_BITMAP_DESC()
  194. {
  195. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  196. REGISTER_TYPE( PCB_BITMAP );
  197. propMgr.InheritsAfter( TYPE_HASH( PCB_BITMAP ), TYPE_HASH( BOARD_ITEM ) );
  198. const wxString groupBitmap = _HKI( "Bitmap Properties" );
  199. propMgr.AddProperty( new PROPERTY<PCB_BITMAP, double>( _HKI( "Scale" ),
  200. &PCB_BITMAP::SetImageScale, &PCB_BITMAP::GetImageScale ),
  201. groupBitmap );
  202. // For future use
  203. const wxString greyscale = _HKI( "Greyscale" );
  204. }
  205. } _PCB_BITMAP_DESC;