KiCad PCB EDA Suite
Loading...
Searching...
No Matches
graphics_importer.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) 2016 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Maciej Suminski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#include "graphics_importer.h"
28
29#include <eda_item.h>
30#include <eda_shape.h>
31
33
34#include <wx/log.h>
35
37{
38 m_millimeterToIu = 1.0;
40 m_scale = VECTOR2D( 1.0, 1.0 );
41 m_originalWidth = 0.0;
42 m_originalHeight = 0.0;
43}
44
45
46bool GRAPHICS_IMPORTER::Load( const wxString& aFileName )
47{
48 m_items.clear();
49
50 if( !m_plugin )
51 {
52 wxASSERT_MSG( false, wxT( "Plugin must be set before load." ) );
53 return false;
54 }
55
56 m_plugin->SetImporter( this );
57
58 bool ret = m_plugin->Load( aFileName );
59
60 if( ret )
61 {
62 m_originalWidth = m_plugin->GetImageWidth();
63 m_originalHeight = m_plugin->GetImageHeight();
64 }
65
66 return ret;
67}
68
69
71{
72 if( !m_plugin )
73 {
74 wxASSERT_MSG( false, wxT( "Plugin must be set before import." ) );
75 return false;
76 }
77
78 SetScale( aScale );
79
80 m_plugin->SetImporter( this );
81
82 bool success = false;
83
84 try
85 {
86 success = m_plugin->Import();
87 }
88 catch( const std::bad_alloc& )
89 {
90 // Memory exhaustion
91 // TODO report back an error message
92 return false;
93 }
94
95 return success;
96}
97
98
100{
101 m_shapeFillRules.push_back( aFillRule );
102}
103
104
105void GRAPHICS_IMPORTER::addItem( std::unique_ptr<EDA_ITEM> aItem )
106{
107 m_items.emplace_back( std::move( aItem ) );
108}
109
110
111bool GRAPHICS_IMPORTER::setupSplineOrLine( EDA_SHAPE& aSpline, int aAccuracy )
112{
113 aSpline.SetShape( SHAPE_T::BEZIER );
114
115 bool degenerate = false;
116
117 SEG s_e{ aSpline.GetStart(), aSpline.GetEnd() };
118 SEG s_c1{ aSpline.GetStart(), aSpline.GetBezierC1() };
119 SEG e_c2{ aSpline.GetEnd(), aSpline.GetBezierC2() };
120
121 if( s_e.ApproxCollinear( s_c1 ) && s_e.ApproxCollinear( e_c2 ) )
122 degenerate = true;
123
124 if( !degenerate )
125 {
126 aSpline.RebuildBezierToSegmentsPointsList( aAccuracy );
127 if( aSpline.GetBezierPoints().size() <= 2 )
128 {
129 degenerate = true;
130 }
131 }
132
133 // If the spline is degenerated (i.e. a segment) add it as segment or discard it if
134 // null (i.e. very small) length
135 if( degenerate )
136 {
137 aSpline.SetShape( SHAPE_T::SEGMENT );
138
139 // segment smaller than MIN_SEG_LEN_ACCEPTABLE_NM nanometers are skipped.
140 constexpr int MIN_SEG_LEN_ACCEPTABLE_NM = 20;
141
142 if( s_e.Length() < MIN_SEG_LEN_ACCEPTABLE_NM )
143 return false;
144 }
145
146 return true;
147}
const VECTOR2I & GetBezierC2() const
Definition: eda_shape.h:213
void RebuildBezierToSegmentsPointsList(int aMaxError)
Rebuild the m_bezierPoints vertex list that approximate the Bezier curve by a list of segments.
Definition: eda_shape.cpp:739
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:174
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:137
void SetShape(SHAPE_T aShape)
Definition: eda_shape.h:131
const std::vector< VECTOR2I > & GetBezierPoints() const
Definition: eda_shape.h:274
const VECTOR2I & GetBezierC1() const
Definition: eda_shape.h:210
double m_originalHeight
Total image Height.
static constexpr unsigned int DEFAULT_LINE_WIDTH_DFX
Default line thickness (in mm).
virtual void NewShape(POLY_FILL_RULE aFillRule=PF_NONZERO)
VECTOR2D m_scale
Scale factor applied to the imported graphics.
std::vector< POLY_FILL_RULE > m_shapeFillRules
std::unique_ptr< GRAPHICS_IMPORT_PLUGIN > m_plugin
Plugin used to load a file.
void SetScale(const VECTOR2D &aScale)
Set the scale factor affecting the imported shapes.
bool Import(const VECTOR2D &aScale=VECTOR2D(1.0, 1.0))
Import shapes from loaded file.
void addItem(std::unique_ptr< EDA_ITEM > aItem)
Add an item to the imported shapes list.
bool Load(const wxString &aFileName)
Load file and get its basic data.
double m_millimeterToIu
Factor to convert millimeters to Internal Units.
bool setupSplineOrLine(EDA_SHAPE &aShape, int aAccuracy)
Configure a shape as a spline or a line segment if it's degenerate.
double m_originalWidth
Total image width.
std::list< std::unique_ptr< EDA_ITEM > > m_items
List of imported items.
double m_lineWidth
Default line thickness for the imported graphics.
Definition: seg.h:42
#define MIN_SEG_LEN_ACCEPTABLE_NM
VECTOR2< double > VECTOR2D
Definition: vector2d.h:694