2018-10-13 18:37:28 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 CERN
|
2023-08-18 18:27:48 +00:00
|
|
|
* Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2021-01-27 22:15:38 +00:00
|
|
|
*
|
2018-10-13 18:37:28 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GRAPHICS_IMPORTER_H
|
|
|
|
#define GRAPHICS_IMPORTER_H
|
|
|
|
|
|
|
|
#include "graphics_import_mgr.h"
|
|
|
|
#include "graphics_import_plugin.h"
|
|
|
|
|
|
|
|
#include <eda_text.h>
|
|
|
|
#include <math/vector2d.h>
|
2023-08-18 18:27:48 +00:00
|
|
|
#include <gal/color4d.h>
|
2023-10-09 00:54:34 +00:00
|
|
|
#include <stroke_params.h>
|
2018-10-13 18:37:28 +00:00
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
class EDA_ITEM;
|
|
|
|
|
2023-10-09 03:56:13 +00:00
|
|
|
/**
|
|
|
|
* A clone of IMPORTED_STROKE, but with floating-point width.
|
|
|
|
*/
|
|
|
|
class IMPORTED_STROKE
|
|
|
|
{
|
|
|
|
public:
|
2023-11-25 13:05:45 +00:00
|
|
|
IMPORTED_STROKE( double aWidth = 0, LINE_STYLE aPlotStyle = LINE_STYLE::DEFAULT,
|
2023-10-09 03:56:13 +00:00
|
|
|
const KIGFX::COLOR4D& aColor = KIGFX::COLOR4D::UNSPECIFIED ) :
|
|
|
|
m_width( aWidth ),
|
|
|
|
m_plotstyle( aPlotStyle ), m_color( aColor )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
double GetWidth() const { return m_width; }
|
|
|
|
void SetWidth( double aWidth ) { m_width = aWidth; }
|
|
|
|
|
2023-11-25 13:05:45 +00:00
|
|
|
LINE_STYLE GetPlotStyle() const { return m_plotstyle; }
|
|
|
|
void SetPlotStyle( LINE_STYLE aPlotStyle ) { m_plotstyle = aPlotStyle; }
|
2023-10-09 03:56:13 +00:00
|
|
|
|
|
|
|
KIGFX::COLOR4D GetColor() const { return m_color; }
|
|
|
|
void SetColor( const KIGFX::COLOR4D& aColor ) { m_color = aColor; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
double m_width;
|
2023-11-25 13:05:45 +00:00
|
|
|
LINE_STYLE m_plotstyle;
|
2023-10-09 03:56:13 +00:00
|
|
|
KIGFX::COLOR4D m_color;
|
|
|
|
};
|
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
/**
|
2019-04-03 15:29:17 +00:00
|
|
|
* Interface that creates objects representing shapes for a given data model.
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
|
|
|
class GRAPHICS_IMPORTER
|
|
|
|
{
|
|
|
|
public:
|
2021-12-30 02:47:54 +00:00
|
|
|
enum POLY_FILL_RULE
|
|
|
|
{
|
|
|
|
PF_NONZERO = 0,
|
|
|
|
PF_EVEN_ODD
|
|
|
|
};
|
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
GRAPHICS_IMPORTER();
|
|
|
|
|
|
|
|
virtual ~GRAPHICS_IMPORTER()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-03 15:29:17 +00:00
|
|
|
* Set the import plugin used to obtain shapes from a file.
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
|
|
|
void SetPlugin( std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> aPlugin )
|
|
|
|
{
|
|
|
|
m_plugin = std::move( aPlugin );
|
2019-04-03 15:29:17 +00:00
|
|
|
|
|
|
|
if( m_plugin )
|
|
|
|
m_plugin->SetImporter( this );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-03 15:29:17 +00:00
|
|
|
* Load file and get its basic data
|
2018-10-13 18:37:28 +00:00
|
|
|
*
|
|
|
|
*/
|
2021-07-27 12:22:27 +00:00
|
|
|
bool Load( const wxString& aFileName );
|
2018-10-13 18:37:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-04-03 15:29:17 +00:00
|
|
|
* Import shapes from loaded file.
|
2018-10-13 18:37:28 +00:00
|
|
|
*
|
|
|
|
* It is important to have the file loaded before importing.
|
2018-11-05 16:04:05 +00:00
|
|
|
*
|
|
|
|
* @param aScale allow import graphic items with a non 1:1 import ratio
|
2023-08-18 18:27:48 +00:00
|
|
|
* VECTOR2D( 1.0, 1.0 ) to import graphics with their actual size.
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
2023-08-18 18:27:48 +00:00
|
|
|
bool Import( const VECTOR2D& aScale = VECTOR2D( 1.0, 1.0 ) );
|
2018-11-05 16:04:05 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-03 15:29:17 +00:00
|
|
|
* Collect warning and error messages after loading/importing.
|
|
|
|
*
|
2018-11-05 16:04:05 +00:00
|
|
|
* @return the list of messages in one string. Each message ends by '\n'
|
|
|
|
*/
|
2021-03-24 04:29:00 +00:00
|
|
|
const wxString& GetMessages() const
|
2018-11-05 16:04:05 +00:00
|
|
|
{
|
|
|
|
return m_plugin->GetMessages();
|
|
|
|
}
|
2018-10-13 18:37:28 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-03 15:29:17 +00:00
|
|
|
* Get original image Width.
|
2018-10-13 18:37:28 +00:00
|
|
|
*
|
2018-11-05 16:04:05 +00:00
|
|
|
* @return Width of the loaded image in mm.
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
2018-11-05 16:04:05 +00:00
|
|
|
double GetImageWidthMM() const
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
return m_originalWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-03 15:29:17 +00:00
|
|
|
* Get original image Height
|
2018-10-13 18:37:28 +00:00
|
|
|
*
|
2018-11-05 16:04:05 +00:00
|
|
|
* @return Height of the loaded image in mm.
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
2018-11-05 16:04:05 +00:00
|
|
|
double GetImageHeightMM() const
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
return m_originalHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-27 22:15:38 +00:00
|
|
|
* Set the line width for the imported outlines (in mm).
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
2018-11-05 16:04:05 +00:00
|
|
|
void SetLineWidthMM( double aWidth )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2018-11-05 16:04:05 +00:00
|
|
|
m_lineWidth = aWidth;
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-27 22:15:38 +00:00
|
|
|
* Return the line width used for importing the outlines (in mm).
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
2018-11-05 16:04:05 +00:00
|
|
|
double GetLineWidthMM() const
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
return m_lineWidth;
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:29:17 +00:00
|
|
|
/**
|
|
|
|
* @return the scale factor affecting the imported shapes.
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
2023-08-18 18:27:48 +00:00
|
|
|
VECTOR2D GetScale() const
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
return m_scale;
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:29:17 +00:00
|
|
|
/**
|
2021-01-27 22:15:38 +00:00
|
|
|
* @return the offset in millimeters to add to coordinates when importing graphic items.
|
2018-11-05 16:04:05 +00:00
|
|
|
*/
|
|
|
|
const VECTOR2D& GetImportOffsetMM() const
|
|
|
|
{
|
|
|
|
return m_offsetCoordmm;
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:29:17 +00:00
|
|
|
/**
|
2021-01-27 22:15:38 +00:00
|
|
|
* Set the offset in millimeters to add to coordinates when importing graphic items.
|
2018-11-05 16:04:05 +00:00
|
|
|
*/
|
|
|
|
void SetImportOffsetMM( const VECTOR2D& aOffset )
|
|
|
|
{
|
|
|
|
m_offsetCoordmm = aOffset;
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:29:17 +00:00
|
|
|
/**
|
|
|
|
* Set the scale factor affecting the imported shapes.
|
2021-01-27 22:15:38 +00:00
|
|
|
*
|
|
|
|
* This allows conversion between imported shapes units and millimeters.
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
2023-08-18 18:27:48 +00:00
|
|
|
void SetScale( const VECTOR2D& aScale )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
m_scale = aScale;
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:29:17 +00:00
|
|
|
/**
|
|
|
|
* @return the conversion factor from mm to internal unit
|
2018-11-05 16:04:05 +00:00
|
|
|
*/
|
|
|
|
double GetMillimeterToIuFactor()
|
|
|
|
{
|
|
|
|
return m_millimeterToIu;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the overall scale factor to convert the imported shapes dimension to mm.
|
|
|
|
*/
|
2023-08-18 18:27:48 +00:00
|
|
|
VECTOR2D ImportScalingFactor() const
|
2018-11-05 16:04:05 +00:00
|
|
|
{
|
|
|
|
return m_scale * m_millimeterToIu;
|
|
|
|
}
|
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
/**
|
2019-04-03 15:29:17 +00:00
|
|
|
* Return the list of objects representing the imported shapes.
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
|
|
|
std::list<std::unique_ptr<EDA_ITEM>>& GetItems()
|
|
|
|
{
|
|
|
|
return m_items;
|
|
|
|
}
|
|
|
|
|
2022-04-02 13:57:30 +00:00
|
|
|
/**
|
|
|
|
* Empties out the imported shapes list
|
|
|
|
*/
|
|
|
|
void ClearItems()
|
|
|
|
{
|
|
|
|
m_items.clear();
|
|
|
|
}
|
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
///< Default line thickness (in mm)
|
2018-10-13 18:37:28 +00:00
|
|
|
static constexpr unsigned int DEFAULT_LINE_WIDTH_DFX = 1;
|
|
|
|
|
2021-12-30 02:47:54 +00:00
|
|
|
virtual void NewShape( POLY_FILL_RULE aFillRule = PF_NONZERO );
|
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
/**
|
2019-04-03 15:29:17 +00:00
|
|
|
* Create an object representing a line segment.
|
|
|
|
*
|
2018-11-05 16:04:05 +00:00
|
|
|
* @param aOrigin is the segment origin point expressed in mm.
|
|
|
|
* @param aEnd is the segment end point expressed in mm.
|
2023-10-09 00:54:34 +00:00
|
|
|
* @param aStroke is the shape stroke parameters.
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
2023-10-09 00:54:34 +00:00
|
|
|
virtual void AddLine( const VECTOR2D& aOrigin, const VECTOR2D& aEnd,
|
2023-10-09 03:56:13 +00:00
|
|
|
const IMPORTED_STROKE& aStroke ) = 0;
|
2018-10-13 18:37:28 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-03 15:29:17 +00:00
|
|
|
* Create an object representing a circle.
|
|
|
|
*
|
2018-11-05 16:04:05 +00:00
|
|
|
* @param aCenter is the circle center point expressed in mm.
|
|
|
|
* @param aRadius is the circle radius expressed in mm.
|
2023-10-09 00:54:34 +00:00
|
|
|
* @param aStroke is the shape stroke parameters.
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
2023-10-09 03:56:13 +00:00
|
|
|
virtual void AddCircle( const VECTOR2D& aCenter, double aRadius, const IMPORTED_STROKE& aStroke,
|
2023-10-09 00:54:34 +00:00
|
|
|
bool aFilled, const COLOR4D& aFillColor ) = 0;
|
2018-10-13 18:37:28 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-03 15:29:17 +00:00
|
|
|
* Create an object representing an arc.
|
|
|
|
*
|
2018-11-05 16:04:05 +00:00
|
|
|
* @param aCenter is the arc center point expressed in mm.
|
|
|
|
* @param aStart is the arc arm end point expressed in mm.
|
2018-10-13 18:37:28 +00:00
|
|
|
* Its length is the arc radius.
|
2022-01-20 23:08:48 +00:00
|
|
|
* @param aAngle is the arc angle.
|
2023-10-09 00:54:34 +00:00
|
|
|
* @param aStroke is the shape stroke parameters.
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
2022-01-20 23:08:48 +00:00
|
|
|
virtual void AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart, const EDA_ANGLE& aAngle,
|
2023-10-09 03:56:13 +00:00
|
|
|
const IMPORTED_STROKE& aStroke ) = 0;
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
/**
|
|
|
|
* Create an object representing a polygon.
|
|
|
|
*
|
|
|
|
* @param aVertices is the array of vertices.
|
|
|
|
* @param aWidth is the stroke width.
|
|
|
|
* @param aStroke is the shape stroke parameters.
|
|
|
|
* @param aFillColor is the fill color.
|
|
|
|
*/
|
2023-10-09 03:56:13 +00:00
|
|
|
virtual void AddPolygon( const std::vector<VECTOR2D>& aVertices, const IMPORTED_STROKE& aStroke,
|
2023-10-09 00:54:34 +00:00
|
|
|
bool aFilled, const COLOR4D& aFillColor ) = 0;
|
2018-10-13 18:37:28 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-03 15:29:17 +00:00
|
|
|
* Create an object representing a text.
|
|
|
|
*
|
2018-10-13 18:37:28 +00:00
|
|
|
* @param aOrigin is the text position.
|
|
|
|
* @param aText is the displayed text.
|
2018-11-05 16:04:05 +00:00
|
|
|
* @param aHeight is the text height expressed in mm.
|
|
|
|
* @param aWidth is the text width expressed in mm.
|
2019-01-22 18:59:43 +00:00
|
|
|
* @param aOrientation is the text orientation angle expressed in degrees.
|
2018-10-13 18:37:28 +00:00
|
|
|
* @param aHJustify is the text horizontal justification.
|
|
|
|
* @param aVJustify is the text vertical justification.
|
2018-11-05 16:04:05 +00:00
|
|
|
* @param aWidth is the segment thickness in mm. Use -1 for default line thickness
|
2023-08-18 18:27:48 +00:00
|
|
|
* @param aColor is the shape color
|
2018-10-13 18:37:28 +00:00
|
|
|
*/
|
2021-12-28 22:13:54 +00:00
|
|
|
virtual void AddText( const VECTOR2D& aOrigin, const wxString& aText, double aHeight,
|
|
|
|
double aWidth, double aThickness, double aOrientation,
|
2023-08-18 18:27:48 +00:00
|
|
|
GR_TEXT_H_ALIGN_T aHJustify, GR_TEXT_V_ALIGN_T aVJustify,
|
|
|
|
const COLOR4D& aColor ) = 0;
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2018-11-05 16:04:05 +00:00
|
|
|
/**
|
2019-04-03 15:29:17 +00:00
|
|
|
* Create an object representing an arc.
|
|
|
|
*
|
2018-11-05 16:04:05 +00:00
|
|
|
* @param aStart is the curve start point expressed in mm.
|
|
|
|
* @param aBezierControl1 is the first Bezier control point expressed in mm.
|
|
|
|
* @param aBezierControl2 is the second Bezier control point expressed in mm.
|
|
|
|
* @param aEnd is the curve end point expressed in mm.
|
2023-10-09 00:54:34 +00:00
|
|
|
* @param aStroke is the shape stroke parameters.
|
2018-11-05 16:04:05 +00:00
|
|
|
*/
|
2018-11-01 09:47:41 +00:00
|
|
|
virtual void AddSpline( const VECTOR2D& aStart, const VECTOR2D& aBezierControl1,
|
2023-10-09 00:54:34 +00:00
|
|
|
const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
|
2023-10-09 03:56:13 +00:00
|
|
|
const IMPORTED_STROKE& aStroke ) = 0;
|
2018-11-01 09:47:41 +00:00
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
protected:
|
2021-01-27 22:15:38 +00:00
|
|
|
///< Add an item to the imported shapes list.
|
2018-10-13 18:37:28 +00:00
|
|
|
void addItem( std::unique_ptr<EDA_ITEM> aItem )
|
|
|
|
{
|
|
|
|
m_items.emplace_back( std::move( aItem ) );
|
|
|
|
}
|
|
|
|
|
2021-06-04 17:44:22 +00:00
|
|
|
///< factor to convert millimeters to Internal Units
|
|
|
|
double m_millimeterToIu;
|
|
|
|
|
|
|
|
///< Offset (in mm) for imported coordinates
|
|
|
|
VECTOR2D m_offsetCoordmm;
|
|
|
|
|
2021-12-30 02:47:54 +00:00
|
|
|
std::vector<POLY_FILL_RULE> m_shapeFillRules;
|
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
private:
|
2021-01-27 22:15:38 +00:00
|
|
|
///< List of imported items
|
2018-10-13 18:37:28 +00:00
|
|
|
std::list<std::unique_ptr<EDA_ITEM>> m_items;
|
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
///< Plugin used to load a file
|
2018-10-13 18:37:28 +00:00
|
|
|
std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> m_plugin;
|
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
///< Total image width
|
2018-11-05 16:04:05 +00:00
|
|
|
double m_originalWidth;
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
///< Total image Height;
|
2018-11-05 16:04:05 +00:00
|
|
|
double m_originalHeight;
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2019-04-03 15:29:17 +00:00
|
|
|
/**
|
|
|
|
* Scale factor applied to the imported graphics.
|
2018-11-05 16:04:05 +00:00
|
|
|
* 1.0 does not change the size of imported items
|
|
|
|
* scale < 1.0 reduce the size of imported items
|
|
|
|
*/
|
2023-08-18 18:27:48 +00:00
|
|
|
VECTOR2D m_scale;
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
///< Default line thickness for the imported graphics
|
2019-04-03 15:29:17 +00:00
|
|
|
double m_lineWidth;
|
2018-10-13 18:37:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* GRAPHICS_IMPORTER_H */
|