KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape_file_io.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) 2013 CERN
5
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6
*
7
* @author Tomasz Wlostowski <
[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 <cassert>
24
#include <string>
25
26
#include <
geometry/shape.h
>
27
#include <
geometry/shape_file_io.h
>
28
29
30
SHAPE_FILE_IO::SHAPE_FILE_IO
()
31
{
32
m_groupActive
=
false
;
33
m_mode
=
IOM_WRITE
;
34
m_file
= stdout;
35
}
36
37
38
SHAPE_FILE_IO::SHAPE_FILE_IO
(
const
std::string& aFilename,
SHAPE_FILE_IO::IO_MODE
aMode )
39
{
40
m_groupActive
=
false
;
41
42
if
( aFilename.length() )
43
{
44
switch
( aMode )
45
{
46
case
IOM_READ
:
m_file
= fopen( aFilename.c_str(),
"rb"
);
break
;
47
case
IOM_WRITE
:
m_file
= fopen( aFilename.c_str(),
"wb"
);
break
;
48
case
IOM_APPEND
:
m_file
= fopen( aFilename.c_str(),
"ab"
);
break
;
49
default
:
50
return
;
51
}
52
}
53
else
54
{
55
m_file
=
nullptr
;
56
}
57
58
m_mode
= aMode;
59
// fixme: exceptions
60
}
61
62
63
SHAPE_FILE_IO::~SHAPE_FILE_IO
()
64
{
65
if
( !
m_file
)
66
return
;
67
68
if
(
m_groupActive
&&
m_mode
!=
IOM_READ
)
69
fprintf(
m_file
,
"endgroup\n"
);
70
71
if
(
m_file
!= stdout )
72
{
73
fclose(
m_file
);
74
}
75
}
76
77
78
SHAPE
*
SHAPE_FILE_IO::Read
()
79
{
80
/* char tmp[1024];
81
82
do {
83
84
if (fscanf(m_file, "%s", tmp) != 1)
85
return nullptr;
86
87
if( !strcmp( tmp, "shape" )
88
break;
89
}
90
91
int type;
92
93
SHAPE *rv = nullptr;
94
95
fscanf(m_file,"%d %s", &type, tmp);
96
97
switch(type)
98
{
99
case SHAPE::LINE_CHAIN:
100
rv = new SHAPE_LINE_CHAIN;
101
break;
102
}
103
104
if(!rv)
105
return nullptr;
106
107
rv.Parse ( )
108
109
fprintf(m_file,"shape %d %s %s\n", aShape->Type(), aName.c_str(), sh.c_str() );
110
*/
111
assert(
false
);
112
return
nullptr
;
113
}
114
115
116
void
SHAPE_FILE_IO::BeginGroup
(
const
std::string& aName )
117
{
118
assert(
m_mode
!=
IOM_READ
);
119
120
if
( !
m_file
)
121
return
;
122
123
fprintf(
m_file
,
"group %s\n"
, aName.c_str() );
124
m_groupActive
=
true
;
125
}
126
127
128
void
SHAPE_FILE_IO::EndGroup
()
129
{
130
assert(
m_mode
!=
IOM_READ
);
131
132
if
( !
m_file
|| !
m_groupActive
)
133
return
;
134
135
fprintf(
m_file
,
"endgroup\n"
);
136
m_groupActive
=
false
;
137
}
138
139
140
void
SHAPE_FILE_IO::Write
(
const
SHAPE
* aShape,
const
std::string& aName )
141
{
142
assert(
m_mode
!=
IOM_READ
);
143
144
if
( !
m_file
)
145
return
;
146
147
if
( !
m_groupActive
)
148
fprintf(
m_file
,
"group default\n"
);
149
150
std::string sh = aShape->
Format
();
151
152
fprintf(
m_file
,
"shape %d %s %s\n"
, aShape->
Type
(), aName.c_str(), sh.c_str() );
153
fflush(
m_file
);
154
}
SHAPE_BASE::Type
SHAPE_TYPE Type() const
Return the type of the shape.
Definition
shape.h:96
SHAPE_FILE_IO::SHAPE_FILE_IO
SHAPE_FILE_IO()
Definition
shape_file_io.cpp:30
SHAPE_FILE_IO::m_file
FILE * m_file
Definition
shape_file_io.h:62
SHAPE_FILE_IO::Write
void Write(const SHAPE *aShape, const std::string &aName="<noname>")
Definition
shape_file_io.cpp:140
SHAPE_FILE_IO::IO_MODE
IO_MODE
Definition
shape_file_io.h:39
SHAPE_FILE_IO::IOM_READ
@ IOM_READ
Definition
shape_file_io.h:40
SHAPE_FILE_IO::IOM_APPEND
@ IOM_APPEND
Definition
shape_file_io.h:41
SHAPE_FILE_IO::IOM_WRITE
@ IOM_WRITE
Definition
shape_file_io.h:42
SHAPE_FILE_IO::m_mode
IO_MODE m_mode
Definition
shape_file_io.h:64
SHAPE_FILE_IO::EndGroup
void EndGroup()
Definition
shape_file_io.cpp:128
SHAPE_FILE_IO::m_groupActive
bool m_groupActive
Definition
shape_file_io.h:63
SHAPE_FILE_IO::BeginGroup
void BeginGroup(const std::string &aName="<noname>")
Definition
shape_file_io.cpp:116
SHAPE_FILE_IO::Read
SHAPE * Read()
Definition
shape_file_io.cpp:78
SHAPE_FILE_IO::~SHAPE_FILE_IO
~SHAPE_FILE_IO()
Definition
shape_file_io.cpp:63
SHAPE
An abstract shape on 2D plane.
Definition
shape.h:124
SHAPE::Format
virtual const std::string Format(bool aCplusPlus=true) const
Definition
shape.cpp:43
shape.h
shape_file_io.h
src
libs
kimath
src
geometry
shape_file_io.cpp
Generated on Fri Jun 26 2026 00:05:38 for KiCad PCB EDA Suite by
1.13.2