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, see <https://www.gnu.org/licenses/>.
21 */
22
23#include "graphics_importer.h"
24
25#include <eda_item.h>
26#include <eda_shape.h>
27
29
30#include <wx/log.h>
31
40
41
42bool GRAPHICS_IMPORTER::Load( const wxString& aFileName )
43{
44 m_items.clear();
45
46 if( !m_plugin )
47 {
48 wxASSERT_MSG( false, wxT( "Plugin must be set before load." ) );
49 return false;
50 }
51
52 m_plugin->SetImporter( this );
53
54 bool ret = m_plugin->Load( aFileName );
55
56 if( ret )
57 {
58 m_originalWidth = m_plugin->GetImageWidth();
59 m_originalHeight = m_plugin->GetImageHeight();
60 }
61
62 return ret;
63}
64
65
67{
68 if( !m_plugin )
69 {
70 wxASSERT_MSG( false, wxT( "Plugin must be set before import." ) );
71 return false;
72 }
73
74 SetScale( aScale );
75
76 m_plugin->SetImporter( this );
77
78 bool success = false;
79
80 try
81 {
82 success = m_plugin->Import();
83 }
84 catch( const std::bad_alloc& )
85 {
86 // Memory exhaustion; we'd report an error message but the chances of us being able to display
87 // it are small.
88 return false;
89 }
90
91 return success;
92}
93
94
96{
97 m_shapeFillRules.push_back( aFillRule );
98}
99
100
101void GRAPHICS_IMPORTER::addItem( std::unique_ptr<EDA_ITEM> aItem )
102{
103 m_items.emplace_back( std::move( aItem ) );
104}
105
106
107bool GRAPHICS_IMPORTER::setupSplineOrLine( EDA_SHAPE& aSpline, int aAccuracy )
108{
109 aSpline.SetShape( SHAPE_T::BEZIER );
110
111 bool degenerate = false;
112
113 SEG s_e{ aSpline.GetStart(), aSpline.GetEnd() };
114 SEG s_c1{ aSpline.GetStart(), aSpline.GetBezierC1() };
115 SEG e_c2{ aSpline.GetEnd(), aSpline.GetBezierC2() };
116
117 if( s_e.ApproxCollinear( s_c1 ) && s_e.ApproxCollinear( e_c2 ) )
118 degenerate = true;
119
120 if( !degenerate )
121 {
122 aSpline.RebuildBezierToSegmentsPointsList( aAccuracy );
123 if( aSpline.GetBezierPoints().size() <= 2 )
124 {
125 degenerate = true;
126 }
127 }
128
129 // If the spline is degenerated (i.e. a segment) add it as segment or discard it if
130 // null (i.e. very small) length
131 if( degenerate )
132 {
133 aSpline.SetShape( SHAPE_T::SEGMENT );
134
135 // segment smaller than MIN_SEG_LEN_ACCEPTABLE_NM nanometers are skipped.
136 constexpr int MIN_SEG_LEN_ACCEPTABLE_NM = 20;
137
138 if( s_e.Length() < MIN_SEG_LEN_ACCEPTABLE_NM )
139 return false;
140 }
141
142 return true;
143}
const VECTOR2I & GetBezierC2() const
Definition eda_shape.h:283
void RebuildBezierToSegmentsPointsList(int aMaxError)
Rebuild the m_bezierPoints vertex list that approximate the Bezier curve by a list of segments.
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:240
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:190
virtual void SetShape(SHAPE_T aShape)
Definition eda_shape.h:184
const std::vector< VECTOR2I > & GetBezierPoints() const
Definition eda_shape.h:404
const VECTOR2I & GetBezierC1() const
Definition eda_shape.h:280
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:38
@ SEGMENT
Definition eda_shape.h:46
VECTOR2< double > VECTOR2D
Definition vector2d.h:682