KiCad PCB EDA Suite
Loading...
Searching...
No Matches
idf_common.h
Go to the documentation of this file.
1
4
5/*
6 * This program source code file is part of KiCad, a free EDA CAD application.
7 *
8 * Copyright (C) 2013-2017 Cirilo Bernardo
9 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25#ifndef IDF_COMMON_H
26#define IDF_COMMON_H
27
28#include <list>
29#include <fstream>
30#include <exception>
31#include <string>
32#include <cmath>
33
34// differences in angle smaller than MIN_ANG are considered equal
35#define MIN_ANG (0.01)
36
37class IDF_POINT;
38class IDF_SEGMENT;
39class IDF_DRILL_DATA;
40class IDF_OUTLINE;
41class IDF_LIB;
42
43
44struct IDF_ERROR : std::exception
45{
46 std::string message;
47
48 IDF_ERROR( const char* aSourceFile, const char* aSourceMethod, int aSourceLine,
49 const std::string& aMessage ) noexcept;
50
51 virtual ~IDF_ERROR() noexcept;
52
53 virtual const char* what() const noexcept override;
54};
55
56
57namespace IDF3 {
58
62enum class FILE_STATE
63{
64 FILE_START = 0, // no data has been read; expecting .HEADER
65 FILE_HEADER, // header has been read; expecting .BOARD_OUTLINE
66 FILE_OUTLINE, // board outline has been read; most sections can be accepted
67 FILE_PLACEMENT, // placement has been read; no further sections can be accepted
68 FILE_INVALID, // file is invalid
69 FILE_ERROR // other errors while processing the file
70};
71
75enum class IDF_VERSION
76{
77 IDF_V2 = 0, // version 2 has read support only; files written as IDFv3
78 IDF_V3 // version 3 has full read/write support
79};
80
84enum class KEY_OWNER
85{
86 UNOWNED = 0, //< either MCAD or ECAD may modify a feature
87 MCAD, //< only MCAD may modify a feature
88 ECAD //< only ECAD may modify a feature
89};
90
94enum class KEY_HOLETYPE
95{
96 PIN = 0, //< drill hole is for a pin
97 VIA, //< drill hole is for a via
98 MTG, //< drill hole is for mounting
99 TOOL, //< drill hole is for tooling
100 OTHER //< user has specified a custom type
101};
102
106enum class KEY_PLATING
107{
108 PTH = 0, //< Plate-Through Hole
109 NPTH //< Non-Plate-Through Hole
110};
111
115enum class KEY_REFDES
116{
117 BOARD = 0, //< feature is associated with the board
118 NOREFDES, //< feature is associated with a component with no RefDes
119 PANEL, //< feature is associated with an IDF panel
120 REFDES //< reference designator as assigned by the CAD software
121};
122
126enum class CAD_TYPE
127{
128 CAD_ELEC = 0, //< An Electrical CAD is opening/modifying the file
129 CAD_MECH, //< A Mechanical CAD is opening/modifying the file
131};
132
145
162
166enum class COMP_TYPE
167{
168 COMP_ELEC = 0, //< Component library object is an electrical part
169 COMP_MECH, //< Component library object is a mechanical part
171};
172
176enum class IDF_UNIT
177{
178 UNIT_MM = 0, //< Units in the file are in millimeters
179 UNIT_THOU, //< Units in the file are in mils (aka thou)
180 UNIT_TNM, //< Deprecated Ten Nanometer Units from IDFv2
182};
183
188{
189 PS_UNPLACED = 0, //< component location on the board has not been specified
190 PS_PLACED, //< component location has been specified and may be modified by ECAD or MCAD
191 PS_MCAD, //< component location has been specified and may only be modified by MCAD
192 PS_ECAD, //< component location has been specified and may only be modified by ECAD
194};
195
203double CalcAngleRad( const IDF_POINT& aStartPoint, const IDF_POINT& aEndPoint );
204
212double CalcAngleDeg( const IDF_POINT& aStartPoint, const IDF_POINT& aEndPoint );
213
225void GetOutline( std::list<IDF_SEGMENT*>& aLines, IDF_OUTLINE& aOutline );
226
227#ifdef DEBUG_IDF
228 // prints out segment information for debug purposes
229 void PrintSeg( IDF_SEGMENT* aSegment );
230#endif
231}
232
233
238{
239public:
240 IDF_NOTE();
241
245 void SetText( const std::string& aText );
246
250 void SetPosition( double aXpos, double aYpos );
251
255 void SetSize( double aHeight, double aLength );
256
260 const std::string& GetText();
261
265 void GetPosition( double& aXpos, double& aYpos );
266
270 void GetSize( double& aHeight, double& aLength );
271
272private:
273 friend class IDF3_BOARD;
274
285 bool readNote( std::istream& aBoardFile, IDF3::FILE_STATE& aBoardState,
286 IDF3::IDF_UNIT aBoardUnit );
287
296 bool writeNote( std::ostream& aBoardFile, IDF3::IDF_UNIT aBoardUnit );
297
298 std::string text; // note text as per IDFv3
299 double xpos; // text X position as per IDFv3
300 double ypos; // text Y position as per IDFv3
301 double height; // text height as per IDFv3
302 double length; // text length as per IDFv3
303};
304
305
312{
313public:
318
330 IDF_DRILL_DATA( double aDrillDia, double aPosX, double aPosY,
331 IDF3::KEY_PLATING aPlating,
332 const std::string& aRefDes,
333 const std::string& aHoleType,
334 IDF3::KEY_OWNER aOwner );
335
345 bool Matches( double aDrillDia, double aPosX, double aPosY ) const;
346
350 double GetDrillDia() const;
351
355 double GetDrillXPos() const;
356
360 double GetDrillYPos() const;
361
366
371 const std::string& GetDrillRefDes();
372
377 const std::string& GetDrillHoleType();
378
380 {
381 return owner;
382 }
383
384private:
385 friend class IDF3_BOARD;
386 friend class IDF3_COMPONENT;
387
398 bool read( std::istream& aBoardFile, IDF3::IDF_UNIT aBoardUnit, IDF3::FILE_STATE aBoardState,
399 IDF3::IDF_VERSION aIdfVersion );
400
408 void write( std::ostream& aBoardFile, IDF3::IDF_UNIT aBoardUnit );
409
410 double dia;
411 double x;
412 double y;
416 std::string refdes;
417 std::string holetype;
419};
420
421
426{
427public:
429 {
430 x = 0.0;
431 y = 0.0;
432 }
433
434 IDF_POINT( double aX, double aY )
435 {
436 x = aX;
437 y = aY;
438 }
439
447 bool Matches( const IDF_POINT& aPoint, double aRadius = 1e-5 ) const;
448
455 double CalcDistance( const IDF_POINT& aPoint ) const;
456
457 double x; // < X coordinate
458 double y; // < Y coordinate
459};
460
461
468{
469public:
473 IDF_SEGMENT();
474
478 IDF_SEGMENT( const IDF_POINT& aStartPoint, const IDF_POINT& aEndPoint );
479
490 IDF_SEGMENT( const IDF_POINT& aStartPoint, const IDF_POINT& aEndPoint, double aAngle,
491 bool aFromKicad );
492
500 bool MatchesStart( const IDF_POINT& aPoint, double aRadius = 1e-3 );
501
509 bool MatchesEnd( const IDF_POINT& aPoint, double aRadius = 1e-3 );
510
514 bool IsCircle();
515
519 double GetMinX();
520
524 void SwapEnds();
525
526private:
533 void CalcCenterAndRadius();
534
535public:
538
541 double angle;
542 double offsetAngle;
543 double radius;
544};
545
546
551{
552public:
553 IDF_OUTLINE() { dir = 0.0; }
555
559 bool IsCCW();
560
564 bool IsCircle();
565
569 void Clear()
570 {
571 dir = 0.0;
572
573 while( !outline.empty() )
574 {
575 delete outline.front();
576 outline.pop_front();
577 }
578 }
579
583 size_t size()
584 {
585 return outline.size();
586 }
587
591 bool empty()
592 {
593 return outline.empty();
594 }
595
600 {
601 return outline.front();
602 }
603
608 {
609 return outline.back();
610 }
611
615 std::list<IDF_SEGMENT*>::iterator begin()
616 {
617 return outline.begin();
618 }
619
623 std::list<IDF_SEGMENT*>::iterator end()
624 {
625 return outline.end();
626 }
627
637 bool push( IDF_SEGMENT* item );
638
639private:
640 double dir; // accumulator to help determine winding direction
641 std::list<IDF_SEGMENT*> outline; // sequential segments comprising an outline
642};
643
644#endif // IDF_COMMON_H
A drilled hole.
Definition idf_common.h:312
void write(std::ostream &aBoardFile, IDF3::IDF_UNIT aBoardUnit)
Write a single line representing a hole within a .DRILLED_HOLES section.
friend class IDF3_BOARD
Definition idf_common.h:385
double GetDrillXPos() const
IDF3::KEY_PLATING GetDrillPlating()
double GetDrillYPos() const
std::string refdes
Definition idf_common.h:416
IDF3::KEY_REFDES kref
Definition idf_common.h:414
bool Matches(double aDrillDia, double aPosX, double aPosY) const
Return true if the given drill diameter and location matches the diameter and location of this IDF_DR...
double GetDrillDia() const
const std::string & GetDrillRefDes()
const std::string & GetDrillHoleType()
IDF3::KEY_OWNER GetDrillOwner() const
Definition idf_common.h:379
IDF_DRILL_DATA()
Create an empty drill entry which can be populated by the read() function.
std::string holetype
Definition idf_common.h:417
bool read(std::istream &aBoardFile, IDF3::IDF_UNIT aBoardUnit, IDF3::FILE_STATE aBoardState, IDF3::IDF_VERSION aIdfVersion)
Read a drill entry from an IDFv3 file.
IDF3::KEY_OWNER owner
Definition idf_common.h:418
friend class IDF3_COMPONENT
Definition idf_common.h:386
IDF3::KEY_PLATING plating
Definition idf_common.h:413
IDF3::KEY_HOLETYPE khole
Definition idf_common.h:415
const std::string & GetText()
friend class IDF3_BOARD
Definition idf_common.h:273
void SetSize(double aHeight, double aLength)
Set the height and length (mm) of the NOTE entry.
bool readNote(std::istream &aBoardFile, IDF3::FILE_STATE &aBoardState, IDF3::IDF_UNIT aBoardUnit)
Read a note entry from an IDFv3 file.
std::string text
Definition idf_common.h:298
double length
Definition idf_common.h:302
void GetPosition(double &aXpos, double &aYpos)
bool writeNote(std::ostream &aBoardFile, IDF3::IDF_UNIT aBoardUnit)
Write a note entry to an IDFv3 file.
void SetPosition(double aXpos, double aYpos)
Set the position (mm) of the NOTE entry.
double height
Definition idf_common.h:301
void GetSize(double &aHeight, double &aLength)
void SetText(const std::string &aText)
Set the text to be stored as a NOTE entry.
double xpos
Definition idf_common.h:299
double ypos
Definition idf_common.h:300
A segment and winding information for an IDF outline.
Definition idf_common.h:551
std::list< IDF_SEGMENT * >::iterator end()
Definition idf_common.h:623
std::list< IDF_SEGMENT * >::iterator begin()
Definition idf_common.h:615
bool empty()
Definition idf_common.h:591
size_t size()
Definition idf_common.h:583
IDF_SEGMENT *& back()
Definition idf_common.h:607
IDF_SEGMENT *& front()
Definition idf_common.h:599
std::list< IDF_SEGMENT * > outline
Definition idf_common.h:641
void Clear()
Clear the internal list of outline segments.
Definition idf_common.h:569
A point as used by the various IDF related classes.
Definition idf_common.h:426
double x
Definition idf_common.h:457
IDF_POINT(double aX, double aY)
Definition idf_common.h:434
double y
Definition idf_common.h:458
A segment as used in IDFv3 outlines.
Definition idf_common.h:468
double radius
radius of the arc or circle; internally calculated
Definition idf_common.h:543
void CalcCenterAndRadius()
IDF_POINT endPoint
end point coordinates in mm
Definition idf_common.h:537
bool MatchesEnd(const IDF_POINT &aPoint, double aRadius=1e-3)
Return true if the given coordinate is within a radius 'rad' of the end point.
IDF_POINT startPoint
starting point coordinates in mm
Definition idf_common.h:536
void SwapEnds()
Swap the start and end points and alters internal variables as necessary for arcs.
double angle
included angle (degrees) according to IDFv3 specification
Definition idf_common.h:541
IDF_SEGMENT()
Initialize the internal variables.
double GetMinX()
bool MatchesStart(const IDF_POINT &aPoint, double aRadius=1e-3)
Return true if the given coordinate is within a radius 'rad' of the start point.
double offsetAngle
angle between center and start of arc; internally calculated
Definition idf_common.h:542
IDF_POINT center
center of an arc or circle; internally calculated and not to be set by the user.
Definition idf_common.h:540
FILE_STATE
State values for the IDF parser's input.
Definition idf_common.h:63
KEY_REFDES
A component's Reference Designator.
Definition idf_common.h:116
KEY_PLATING
The plating condition of a hole.
Definition idf_common.h:107
IDF_PLACEMENT
The placement status of a component.
Definition idf_common.h:188
CAD_TYPE
The class of CAD program which is opening or modifying a file.
Definition idf_common.h:127
OUTLINE_TYPE
The class of outline.
Definition idf_common.h:150
IDF_VERSION
The supported IDF versions (3.0 and 2.0 ONLY).
Definition idf_common.h:76
IDF_UNIT
The native unit of the board and of component outlines.
Definition idf_common.h:177
IDF_LAYER
The various IDF layer classes and groupings.
Definition idf_common.h:137
KEY_OWNER
The type of CAD which has ownership an object.
Definition idf_common.h:85
KEY_HOLETYPE
The purpose of an IDF hole.
Definition idf_common.h:95
COMP_TYPE
Whether a component is a mechanical or electrical part.
Definition idf_common.h:167
std::string message
Definition idf_common.h:46
virtual const char * what() const noexcept override
IDF_ERROR(const char *aSourceFile, const char *aSourceMethod, int aSourceLine, const std::string &aMessage) noexcept
virtual ~IDF_ERROR() noexcept