-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathDXFModule.cpp
More file actions
180 lines (139 loc) · 5.62 KB
/
DXFModule.cpp
File metadata and controls
180 lines (139 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/******************************************************************************\
CAMotics is an Open-Source simulation and CAM software.
Copyright (C) 2011-2019 Joseph Coffland <joseph@cauldrondevelopment.com>
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, see <http://www.gnu.org/licenses/>.
\******************************************************************************/
#include "DXFModule.h"
#include "TPLContext.h"
#include <dxf/Reader.h>
#include <dxf/Point.h>
#include <dxf/Line.h>
#include <dxf/Arc.h>
#include <dxf/PolyLine.h>
#include <dxf/Spline.h>
#include <cbang/os/SystemUtilities.h>
using namespace tplang;
using namespace cb;
using namespace std;
DXFModule::DXFModule(TPLContext &ctx) : js::NativeModule("_dxf"), ctx(ctx) {}
void DXFModule::define(js::Sink &exports) {
exports.insert("open(path)", this, &DXFModule::openCB);
exports.insert("POINT", DXF::Entity::DXF_POINT);
exports.insert("LINE", DXF::Entity::DXF_LINE);
exports.insert("ARC", DXF::Entity::DXF_ARC);
exports.insert("POLYLINE", DXF::Entity::DXF_POLYLINE);
exports.insert("SPLINE", DXF::Entity::DXF_SPLINE);
}
void DXFModule::openCB(const js::Value &args, js::Sink &sink) {
DXF::Reader reader;
reader.read(ctx.relativePath(args.getString("path")));
const DXF::Reader::layers_t &layers = reader.getLayers();
sink.beginDict();
DXF::Reader::layers_t::const_iterator it;
for (it = layers.begin(); it != layers.end(); it++) {
const DXF::Reader::layer_t &layer = it->second;
sink.insertList(it->first);
for (unsigned j = 0; j < layer.size(); j++) {
const DXF::Entity &entity = *layer[j];
sink.appendDict();
switch (entity.getType()) {
case DXF::Entity::DXF_POINT: {
const DXF::Point &point = dynamic_cast<const DXF::Point &>(entity);
sink.insert("x", point.x());
sink.insert("y", point.y());
sink.insert("z", point.z());
break;
}
case DXF::Entity::DXF_LINE: {
const DXF::Line &line = dynamic_cast<const DXF::Line &>(entity);
sink.insertDict("start");
sink.insert("x", line.getStart().x());
sink.insert("y", line.getStart().y());
sink.insert("z", line.getStart().z());
sink.endDict();
sink.insertDict("end");
sink.insert("x", line.getEnd().x());
sink.insert("y", line.getEnd().y());
sink.insert("z", line.getEnd().z());
sink.endDict();
break;
}
case DXF::Entity::DXF_ARC: {
const DXF::Arc &arc = dynamic_cast<const DXF::Arc &>(entity);
sink.insertDict("center");
sink.insert("x", arc.getCenter().x());
sink.insert("y", arc.getCenter().y());
sink.insert("z", arc.getCenter().z());
sink.endDict();
sink.insert("radius", arc.getRadius());
sink.insert("startAngle", arc.getStartAngle());
sink.insert("endAngle", arc.getEndAngle());
sink.insertBoolean("clockwise", arc.getClockwise());
break;
}
case DXF::Entity::DXF_POLYLINE: {
const DXF::PolyLine &polyLine =
dynamic_cast<const DXF::PolyLine &>(entity);
// TODO expose other flags
sink.insertBoolean("isClosed", polyLine.isClosed());
const vector<Vector3D> &vertices = polyLine.getVertices();
sink.insertList("vertices");
for (unsigned k = 0; k < vertices.size(); k++) {
sink.appendDict();
sink.insert("x", vertices[k].x());
sink.insert("y", vertices[k].y());
sink.insert("z", vertices[k].z());
sink.insert("type", DXF::Entity::DXF_POINT);
sink.endDict();
}
sink.endList();
break;
}
case DXF::Entity::DXF_SPLINE: {
const DXF::Spline &spline = dynamic_cast<const DXF::Spline &>(entity);
sink.insert("degree", spline.getDegree());
sink.insertBoolean("isClosed", spline.isClosed());
sink.insertBoolean("isPeriodic", spline.isPeriodic());
sink.insertBoolean("isRational", spline.isRational());
sink.insertBoolean("isPlanar", spline.isPlanar());
sink.insertBoolean("isLinear", spline.isLinear());
// Control points
const vector<Vector3D> &ctrlPts = spline.getControlPoints();
const std::vector<double> &weights = spline.getWeights();
sink.insertList("ctrlPts");
for (unsigned k = 0; k < ctrlPts.size(); k++) {
sink.appendDict();
sink.insert("x", ctrlPts[k].x());
sink.insert("y", ctrlPts[k].y());
sink.insert("z", ctrlPts[k].z());
sink.insert("w", weights[k]);
sink.insert("type", DXF::Entity::DXF_POINT);
sink.endDict();
}
sink.endList();
// Knots
const vector<double> &knots = spline.getKnots();
sink.insertList("knots");
for (unsigned k = 0; k < knots.size(); k++)
sink.append(knots[k]);
sink.endList();
break;
}
default: THROW("Invalid DXF entity type " << entity.getType());
}
sink.insert("type", entity.getType());
sink.endDict();
}
sink.endList();
}
sink.endDict();
}