/
Area.h
469 lines (387 loc) · 15.9 KB
/
Area.h
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/****************************************************************************
* Copyright (c) 2017 Zheng, Lei (realthunder) <realthunder.dev@gmail.com>*
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
****************************************************************************/
#ifndef PATH_AREA_H
#define PATH_AREA_H
#include <QCoreApplication>
#include <chrono>
#include <memory>
#include <vector>
#include <list>
#include <TopoDS.hxx>
#include <gp_Pln.hxx>
#include <gp_Circ.hxx>
#include <gp_GTrsf.hxx>
#include <Base/Console.h>
#include <Mod/Part/App/TopoShape.h>
#include "Path.h"
#include "AreaParams.h"
namespace Part {
extern PartExport Py::Object shape2pyshape(const TopoDS_Shape &shape);
}
#define _AREA_LOG(_l,_msg) do {\
if(Area::_l##Enabled()){\
std::stringstream str;\
str << _msg;\
const char *_f = strrchr(__FILE__, '/');\
Base::Console()._l("%s:(%d): %s\n",_f?_f+1:__FILE__,__LINE__,str.str().c_str());\
}\
QCoreApplication::sendPostedEvents();\
if(Area::aborting()) {\
Area::abort(false);\
throw Base::AbortException("operation aborted");\
}\
}while(0)
#define AREA_LOG(_msg) _AREA_LOG(Log,_msg)
#define AREA_WARN(_msg) _AREA_LOG(Warning,_msg)
#define AREA_ERR(_msg) _AREA_LOG(Error,_msg)
#define AREA_XYZ(_pt) '('<<(_pt).X()<<", " << (_pt).Y()<<", " << (_pt).Z()<<')'
#define AREA_XY(_pt) '('<<(_pt).x<<", " << (_pt).y<<')'
#define AREA_TRACE(_msg) do{\
if(Area::TraceEnabled()) AREA_LOG(_msg);\
}while(0)
#define AREA_DBG AREA_WARN
#define AREA_TIME_ENABLE
#ifdef AREA_TIME_ENABLE
#define TIME_UNIT duration<double>
#define TIME_CLOCK high_resolution_clock
#define TIME_POINT std::chrono::TIME_CLOCK::time_point
#define TIME_DURATION std::chrono::TIME_UNIT
#define _TIME_INIT(_t) _t=std::chrono::TIME_CLOCK::now()
#define TIME_INIT(_t) TIME_POINT _TIME_INIT(_t)
#define TIME_INIT2(_t1,_t2) TIME_INIT(_t1),_t2=_t1
#define TIME_INIT3(_t1,_t2,_t3) TIME_INIT(_t1),_t2=_t1,_t3=_t1
#define _DURATION_PRINT(_l,_d,_msg) \
AREA_##_l(_msg<< " time: " << _d.count()<<'s');
#define DURATION_PRINT(_d,_msg) _DURATION_PRINT(LOG,_d,_msg)
#define TIME_PRINT(_t,_msg) \
DURATION_PRINT(Path::getDuration(_t),_msg);
#define TIME_TRACE(_t,_msg) \
_DURATION_PRINT(TRACE,Path::getDuration(_t),_msg);
#define DURATION_INIT(_d) \
TIME_DURATION _d(0)
#define DURATION_INIT2(_d1,_d2) DURATION_INIT(_d1),_d2(0)
namespace Path {
inline std::chrono::TIME_UNIT getDuration(TIME_POINT &t)
{
auto tnow = std::chrono::TIME_CLOCK::now();
auto d = std::chrono::duration_cast<std::chrono::TIME_UNIT>(tnow-t);
t = tnow;
return d;
}
}
#define DURATION_PLUS(_d,_t) _d += Path::getDuration(_t)
#else
#define TIME_POINT
#define _TIME_INIT(...) do{}while(0)
#define TIME_INIT(...) do{}while(0)
#define TIME_INIT2(...) do{}while(0)
#define TIME_INIT3(...) do{}while(0)
#define TIME_PRINT(...) do{}while(0)
#define DURATION_PRINT(...) do{}while(0)
#define DURATION_INIT(...) do{}while(0)
#define DURATION_INIT2(...) do{}while(0)
#define DURATION_PLUS(...) do{}while(0)
#endif
class CArea;
class CCurve;
namespace Path
{
/** Store libarea algorithm configuration */
struct PathExport CAreaParams {
PARAM_DECLARE(PARAM_FNAME,AREA_PARAMS_CAREA)
CAreaParams();
};
/** Store all Area configurations */
struct PathExport AreaParams: CAreaParams {
PARAM_DECLARE(PARAM_FNAME,AREA_PARAMS_AREA)
bool operator==(const AreaParams &other) const {
#define AREA_COMPARE(_param) \
if(PARAM_FIELD(NAME,_param)!=other.PARAM_FIELD(NAME,_param)) return false;
PARAM_FOREACH(AREA_COMPARE,AREA_PARAMS_CAREA)
PARAM_FOREACH(AREA_COMPARE,AREA_PARAMS_AREA)
return true;
}
bool operator!=(const AreaParams &other) const {
return !(*this == other);
}
AreaParams();
};
struct PathExport AreaStaticParams: AreaParams {
PARAM_DECLARE(PARAM_FNAME,AREA_PARAMS_EXTRA_CONF);
AreaStaticParams();
};
/** libarea configurator
*
* It is kind of troublesome with the fact that libarea uses static variables to
* config its algorithm. CAreaConfig makes it easy to safely customize libarea.
*/
struct PathExport CAreaConfig {
/** For saving current libarea settings */
PARAM_DECLARE(PARAM_FNAME,AREA_PARAMS_CAREA)
/** The constructor automatically saves current setting and apply user defined ones
*
* \arg \c p user defined configurations
* \arg \c noFitArgs if true, will override and disable arc fitting. Because
* arc unfiting and fitting is lossy. And repeatedly perform these operation
* may cause shape deformation. So it is best to delay arc fitting until the
* final step*/
CAreaConfig(const CAreaParams &p, bool noFitArcs=true);
/** The destructor restores the setting, and thus exception safe. */
~CAreaConfig();
};
/** Base class for FreeCAD wrapping of libarea */
class PathExport Area: public Base::BaseClass {
TYPESYSTEM_HEADER();
public:
struct Shape {
short op;
TopoDS_Shape shape;
Shape(short opCode, const TopoDS_Shape &s)
:op(opCode)
,shape(s)
{}
};
protected:
std::list<Shape> myShapes;
std::unique_ptr<CArea> myArea;
std::unique_ptr<CArea> myAreaOpen;
gp_Trsf myTrsf;
AreaParams myParams;
TopoDS_Shape myShapePlane;
TopoDS_Shape myWorkPlane;
TopoDS_Shape myShape;
std::vector<std::shared_ptr<Area> > mySections;
bool myHaveFace;
bool myHaveSolid;
bool myShapeDone;
int mySkippedShapes;
static bool s_aborting;
static AreaStaticParams s_params;
/** Called internally to combine children shapes for further processing */
void build();
/** Called by build() to add children shape
*
* Mainly for checking if there is any faces for auto fill*/
void addToBuild(CArea &area, const TopoDS_Shape &shape);
/** Called internally to obtain the combained children shapes */
TopoDS_Shape toShape(CArea &area, short fill, int reorient=0);
/** Obtain a list of offseted areas
*
* See #AREA_PARAMS_OFFSET for description of the arguments.
*/
void makeOffset(std::list<std::shared_ptr<CArea> > &areas,
PARAM_ARGS_DEF(PARAM_FARG,AREA_PARAMS_OFFSET));
/** Make a pocket of the combined shape
*
* User #AREA_PARAMS_POCKET setting in myParams.
*/
TopoDS_Shape makePocket();
void explode(const TopoDS_Shape &shape);
TopoDS_Shape findPlane(const TopoDS_Shape &shape, gp_Trsf &trsf);
std::list<TopoDS_Wire> project(const TopoDS_Shape &solid);
public:
/** Declare all parameters defined in #AREA_PARAMS_ALL as member variable */
PARAM_ENUM_DECLARE(AREA_PARAMS_ALL)
Area(const AreaParams *params = NULL);
Area(const Area &other, bool deep_copy=true);
virtual ~Area();
bool isBuilt() const;
/** Set a working plane
*
* \arg \c shape: a shape defining a working plane.
*
* The supplied shape does not need to be planar. Area will try to find planar
* sub-shape (face, wire or edge). If more than one planar sub-shape is found,
* it will prefer the top plane parallel to XY0 plane.
*
* If no working plane are set, Area will try to find a working plane from
* the added children shape using the same algorithm
*/
void setPlane(const TopoDS_Shape &shape);
/** Return the current active workplane
*
* \arg \c trsf: optional return of a transformation matrix that will bring the
* found plane to XY0 plane.
*
* If no workplane is set using setPlane(), the active workplane is derived from
* the added children shapes using the same algorithm empolyed by setPlane().
*/
TopoDS_Shape getPlane(gp_Trsf *trsf = NULL);
/** Add a child shape with given operation code
*
* No validation is done at this point. Exception will be thrown when asking
* for output shape, if any of the children shapes is not valid or not
* coplanar
*
* \arg \c shape: the child shape
* \arg \c op: operation code, see #AREA_PARAMS_OPCODE
*/
void add(const TopoDS_Shape &shape,PARAM_ARGS_DEF(PARAM_FARG,AREA_PARAMS_OPCODE));
/** Generate an offset of the combined shape
*
* See #AREA_PARAMS_OFFSET for description of the arguments.
* If more than one offset is requested, a compound shape is return
* containing all offset shapes as wires regardless of \c Fill setting.
*/
TopoDS_Shape makeOffset(int index=-1, PARAM_ARGS_DEF(PARAM_FARG,AREA_PARAMS_OFFSET), int reoirent=0);
/** Make a pocket of the combined shape
*
* See #AREA_PARAMS_POCKET for description of the arguments.
*/
TopoDS_Shape makePocket(int index=-1, PARAM_ARGS_DEF(PARAM_FARG,AREA_PARAMS_POCKET));
/** Make a pocket of the combined shape
*
* \arg \c heights: optional customized heights of each section. The
* meaning of each height depends on section mode. If none is given,
* the section heights is determined by the section settings in this
* Area object (configured through setParams()).
* \arg \c plane: the section plane if the section mode is
* SectionModeWorkplane, otherwise ignored
*
* See #AREA_PARAMS_EXTRA for description of the arguments. Currently, there
* is only one argument, namely \c mode for section mode.
*/
std::vector<std::shared_ptr<Area> > makeSections(
PARAM_ARGS_DEF(PARAM_FARG,AREA_PARAMS_SECTION_EXTRA),
const std::vector<double> &heights = std::vector<double>(),
const TopoDS_Shape &plane = TopoDS_Shape());
/** Config this Area object */
void setParams(const AreaParams ¶ms);
const std::list<Shape> getChildren() const {
return myShapes;
}
/** Get the current configuration */
const AreaParams &getParams() const {
return myParams;
}
/** Clean internal caches
*
* The combained shapes is cached internally to make other operation more
* efficient, such as makeOffset() and makePocket()
*
* \arg \c deleteShapes: if true, delete all children shapes.
*/
void clean(bool deleteShapes=false);
/** Get the combined shape
* \arg \c index: index of the section, -1 for all sections. No effect on
* non-sectioned area.
*/
TopoDS_Shape getShape(int index=-1);
/** Return the number of sections */
std::size_t getSectionCount() {
build();
return mySections.size();
}
/** Add a OCC wire shape to CArea
*
* \arg \c area: output converted curved object to here
* \arg \c wire: input wire object
* \arg \c trsf: optional transform matrix to transform the wire shape into
* XY0 plane.
* \arg \c deflection: for discretizing non circular curves
* \arg \c to_edges: if true, discretize all curves, and insert as open
* line segments
* */
static void addWire(CArea &area, const TopoDS_Wire &wire, const gp_Trsf *trsf=NULL,
double deflection=0.01, bool to_edges=false);
/** Add a OCC generic shape to CArea
*
* \arg \c area: output converted curved object to here
* \arg \c shape: input shape object
* \arg \c trsf: optional transform matrix to transform the wire shape into
* XY0 plane.
* \arg \c deflection: for defecting non circular curves
* \arg \c plane: a shape for testing coplanar
* \arg \c force_coplaner: if true, discard non-coplanar shapes.
* \arg \c areaOpen: for collecting open curves. If not supplied, open
* curves are added to \c area
* \arg \c to_edges: separate open wires to individual edges
* \arg \c reorient: reorient closed wires for wire only shape
*
* \return Returns the number of non coplaner. Planar testing only happens
* if \c plane is supplied
* */
static int addShape(CArea &area, const TopoDS_Shape &shape, const gp_Trsf *trsf=NULL,
double deflection=0.01,const TopoDS_Shape *plane = NULL,
bool force_coplanar=true, CArea *areaOpen=NULL, bool to_edges=false,
bool reorient=true);
/** Convert curves in CArea into an OCC shape
*
* \arg \c area: input area object
* \arg \c fill: if true, create a face object from the wires
* \arg \c trsf: optional transform matrix to transform the shape back into
* its original position.
* */
static TopoDS_Shape toShape(const CArea &area, bool fill,
const gp_Trsf *trsf=NULL, int reoirent=0);
/** Convert a single curve into an OCC wire
*
* \arg \c curve: input curve object
* \arg \c trsf: optional transform matrix to transform the shape back into
* its original position.
* */
static TopoDS_Wire toShape(const CCurve &curve, const gp_Trsf *trsf=NULL, int reorient=0);
/** Check if two OCC shape is coplanar */
static bool isCoplanar(const TopoDS_Shape &s1, const TopoDS_Shape &s2);
/** Group shapes by their plane, and return a list of sorted wires
*
* The output wires is ordered by its occupied plane, and sorted to
* minimize traval distance
*
* \arg \c shapes: input list of shapes.
* \arg \c pstart: optional start point
* \arg \c pend: optional output containing the ending point of the returned
* \arg \c arc_plane: optional arc plane selection, if given the found plane
* will be returned. See #AREA_PARAMS_ARC_PLANE for more details.
*
* See #AREA_PARAMS_SORT for other arguments
*
* \return sorted wires
*/
static std::list<TopoDS_Shape> sortWires(const std::list<TopoDS_Shape> &shapes,
const gp_Pnt *pstart=NULL, gp_Pnt *pend=NULL, short *arc_plane = NULL,
PARAM_ARGS_DEF(PARAM_FARG,AREA_PARAMS_SORT));
/** Convert a list of wires to gcode
*
* \arg \c path: output toolpath
* \arg \c shapes: input list of shapes
* \arg \c pstart: output start point,
* \arg \c pend: optional output containing the ending point of the returned
*
* See #AREA_PARAMS_PATH for other arguments
*/
static void toPath(Toolpath &path, const std::list<TopoDS_Shape> &shapes,
const gp_Pnt *pstart=NULL, gp_Pnt *pend=NULL,
PARAM_ARGS_DEF(PARAM_FARG,AREA_PARAMS_PATH));
static void setWireOrientation(TopoDS_Wire& wire, const gp_Dir &dir, bool ccw);
PARAM_ENUM_DECLARE(AREA_PARAMS_PATH)
static void abort(bool aborting);
static bool aborting();
static void setDefaultParams(const AreaStaticParams ¶ms);
static const AreaStaticParams &getDefaultParams();
#define AREA_LOG_CHECK_DECLARE(_1,_2,_elem) \
static bool BOOST_PP_CAT(_elem,Enabled)();
BOOST_PP_SEQ_FOR_EACH(AREA_LOG_CHECK_DECLARE,_,AREA_PARAM_LOG_LEVEL)
PARAM_ENUM_DECLARE(AREA_PARAMS_LOG_LEVEL)
};
} //namespace Path
#endif //PATH_AREA_H