-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Attacher.h
504 lines (437 loc) · 17.7 KB
/
Attacher.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/***************************************************************************
* Copyright (c) 2015 Victor Titov (DeepSOIC) <vv.titov@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 *
* *
***************************************************************************/
/**
*Attacher.h, Attacher.cpp contain the functionality of deriving placement
*from a set of geometric subelements. Examples are: sketch attachment, datum
*plane placement.
*/
#ifndef PARTATTACHER_H
#define PARTATTACHER_H
#include <GProp_GProps.hxx>
#include <App/DocumentObserver.h>
#include <App/GeoFeature.h>
#include <App/PropertyLinks.h>
#include <App/PropertyStandard.h>
#include <Base/Exception.h>
#include <Base/Placement.h>
#include "PartFeature.h"
namespace Attacher
{
class AttachEngine;
//Attention! The numbers associated to the modes are permanent, because they are what get written into files.
enum eMapMode {
mmDeactivated,
mmTranslate,
mmObjectXY,
mmObjectXZ,
mmObjectYZ,
mmFlatFace,
mmTangentPlane,
mmNormalToPath,
mmFrenetNB,
mmFrenetTN,
mmFrenetTB,
mmConcentric,
mmRevolutionSection,
mmThreePointsPlane,
mmThreePointsNormal,
mmFolding,
mm1AxisX,
mm1AxisY,
mm1AxisZ,
mm1AxisCurv,
mm1Directrix1,
mm1Directrix2,
mm1Asymptote1,
mm1Asymptote2,
mm1Tangent,
mm1Normal,
mm1Binormal,
mm1TangentU,
mm1TangentV,
mm1TwoPoints,
mm1Intersection,
mm1Proximity,
mm0Origin,
mm0Focus1,
mm0Focus2,
mm0OnEdge,
mm0CenterOfCurvature,
mm0CenterOfMass,
mm0Intersection,
mm0Vertex,
mm0ProximityPoint1,
mm0ProximityPoint2,
mm1AxisInertia1,
mm1AxisInertia2,
mm1AxisInertia3,
mmInertialCS,
mm1FaceNormal,
mmOZX,
mmOZY,
mmOXY,
mmOXZ,
mmOYZ,
mmOYX,
mmDummy_NumberOfModes//a value useful to check the validity of mode value
};//see also eMapModeStrings[] definition in .cpp
/**
* @brief The eRefType enum lists the types of references. If adding one, see
* also AttachEngine::eRefTypeStrings, AttachEngine::getShapeType(),
* AttachEngine::downgradeType(), AttacherTexts.cpp/getShTypeText()
*/
enum eRefType {
//topo //ranks: (number of times the type is downgradable)
rtAnything, //0
rtVertex, //1
rtEdge, //1
rtFace, //1
//edges:
rtLine, //2
rtCurve, //2
rtCircle, //3
rtConic, //3
rtEllipse, //4
rtParabola, //4
rtHyperbola, //4
//faces:
rtFlatFace, //2
rtSphericalFace, //2//flatface, shericalface are also surfaces of revolution, but the axis isn't defined.
rtSurfaceRev, //2
rtCylindricalFace, //3
rtToroidalFace, //3
rtConicalFace, //3
//shapes:
rtPart, //1
rtSolid, //2
rtWire, //2
rtDummy_numberOfShapeTypes,//a value useful to check the validity of value
rtFlagHasPlacement = 0x0100 //indicates that the linked shape is a whole FreeCAD object that has placement available.
};
using refTypeString = std::vector<eRefType>; //a sequence of ref types, according to AttachmentSupport contents for example
using refTypeStringList = std::vector<refTypeString>; //a set of type strings, defines which selection sets are supported by a certain mode
/**
* @brief The SuggestResult struct is a container for output information of AttachEngine mode suggesting routine.
*/
struct SuggestResult{
/**
* @brief message contains overall verdict of suggestor on current reference set
*/
enum eSuggestResult{
srOK, //references are valid for at least one mode
srLinkBroken, //failed to resolve out some of current references. Exception info is stored in SuggestResult::error.
srUnexpectedError,
srNoModesFit,//none of the available mapping modes accepts the set of topological type
srIncompatibleGeometry,//there is a mode that could fit, but geometry is wrong (e.g. a line is required, but a curve was passed).
};
eSuggestResult message;
/**
* @brief allApplicableModes. Vector array that will receive the list of
* all modes that are applicable to current set of references. It doesn't
* guarantee that all modes will work, it only checks that subelemnts are
* of right type.
*/
std::vector<eMapMode> allApplicableModes;
/**
* @brief bestFitMode is the mode that is the most specific to current
* references. Note that the mode may not be valid for current references;
* check if it's listed in allApplicableModes, or test if message == srOK.
*/
eMapMode bestFitMode;
/**
* @brief nextRefTypeHint: a hint of what can be added to references to
* achieve other modes.
*/
std::set<eRefType> nextRefTypeHint;
/**
* @brief reachableModes. List of modes that can be reached by selecting
* more references. Is a map, where key is the mode that can be reached,
* and value is a list of reference sequences that can be added to reach
* the mode (stuff already linked is omitted from these lists; only extra
* links needed are listed)
*/
std::map<eMapMode, refTypeStringList> reachableModes;
/**
* @brief references_Types: list of types of references, as queried when
* running suggesting routine.
*/
refTypeString references_Types;
Base::RuntimeError error;
};
/**
* @brief The AttachEngine class is the placement calculation routine, modes,
* hints and so on. It can be used separately, without deriving from
* AttachableObject.
*/
class PartExport AttachEngine : public Base::BaseClass
{
TYPESYSTEM_HEADER_WITH_OVERRIDE();
public: //methods
AttachEngine();
virtual void setUp(const App::PropertyLinkSubList &references,
eMapMode mapMode = mmDeactivated,
bool mapReverse = false,
double attachParameter = 0.0,
double surfU = 0.0, double surfV = 0.0,
const Base::Placement &attachmentOffset = Base::Placement());
virtual void setUp(const AttachEngine &another);
virtual AttachEngine* copy() const = 0;
Base::Placement calculateAttachedPlacement(
const Base::Placement &origPlacement, bool *subChanged=0);
virtual Base::Placement _calculateAttachedPlacement(
const std::vector<App::DocumentObject*> &objs,
const std::vector<std::string> &subs,
const Base::Placement &origPlacement) const = 0;
void setReferences(const App::PropertyLinkSubList &references);
void setReferences(const std::vector<App::SubObjectT> &references);
/**
* @brief placementFactory calculates placement from Z axis direction,
* optional X axis direction, and origin point.
*
* @param ZAxis (input) mandatory. Z axis of the returned placement will
* strictly coincide with ZAxis.
*
* @param XAxis (input) optional (i.e., can be zero). Sets the preferred X
* axis orientation. If it is not perpendicular to ZAxis, it will be forced
* to be. If XAxis is zero, the effect is equivalent to setting
* makeYVertical to true.
*
* @param Origin (input) mandatory.
*
* @param refOrg (input). The point that will be used in case any of
* useRefOrg_XX parameters is true.
*
* @param useRefOrg_Line (input). If true, Origin will be moved along ZAxis
* to be as close as possible to refOrg.
*
* @param useRefOrg_Plane (input). If true, Origin will be moved in
* XAxis-YAxis plane to be as close as possible to refOrg.
*
* @param makeYVertical (input). If true, XAxis is ignored, and X and Y
* axes are defined in order to make Y axis go as upwards as possible. If
* ZAxis is strictly upwards, XY will match global XY. If ZAxis is strictly
* downwards, XAxis will be the reversed global X axis.
*
* @param makeLegacyFlatFaceOrientation (input). Modifies the behavior of
* makeYVertical to match the logic that was used in mapping of sketches to
* flat faces in FreeCAD prior to introduction of Attacher. Set
* makeYVertical to true if using this.
*
* @return the resulting placement. ReverseXY property of Attacher will be automatically applied.
*/
Base::Placement placementFactory(const gp_Dir &ZAxis,
gp_Vec XAxis,
gp_Pnt Origin,
gp_Pnt refOrg = gp_Pnt(),
bool useRefOrg_Line = false,
bool useRefOrg_Plane = false,
bool makeYVertical = false,
bool makeLegacyFlatFaceOrientation = false,
Base::Placement* placeOfRef = nullptr) const;
/**
* @brief suggestMapModes is the procedure that knows everything about
* mapping modes. It returns the most appropriate mapping mode, as well as
* list of all modes that will accept the set of references. In case no modes apply,
* extra information regarding reasons is returned in msg.
*
* @param result (output). Returns results of suggestion, such as best fit
* mode, list of all modes that apply, hints, etc.
*/
virtual void suggestMapModes(SuggestResult &result) const;
/**
* @brief EnableAllModes enables all modes that have shape type lists filled. The function acts on modeEnabled array.
*/
void EnableAllSupportedModes();
~AttachEngine() override = default;
public://helper functions that may be useful outside of the class
/**
* @brief getShapeType by shape. Will never set rtFlagHasPlacement.
* @param sh
* @return
*/
static eRefType getShapeType(const TopoDS_Shape &sh);
/**
* @brief getShapeType by link content. Will include rtFlagHasPlacement, if applies.
* @param obj
* @param subshape (input). Can be empty string (then, whole object will be used for shape type testing)
* @return
*/
static eRefType getShapeType(const App::DocumentObject* obj,
const std::string &subshape);
/**
* @brief downgradeType converts a more-specific type into a less-specific
* type (e.g. rtCircle->rtCurve, rtCurve->rtEdge, rtEdge->rtAnything)
* @param type
* @return the downgraded type.
*/
static eRefType downgradeType(eRefType type);
/**
* @brief getTypeRank determines, how specific is the supplied shape type.
* The ranks are outlined in definition of eRefType. The ranks are defined
* by implementation of downgradeType().
* @param type
* @return number of times the type can be downgradeType() before it
* becomes rtAnything
*/
static int getTypeRank(eRefType type);
/**
* @brief isShapeOfType tests if a shape fulfills the requirement of a mode, and returns a score of how spot on was the requirement.
* @param shapeType (use return value of AttachEngine::getShapeType)
* @param requirement
* @return : -1 - doesn't fulfill,
* 0 - compatible topology, but incompatible specific (e.g. rtLine, rtCircle);
* 1 - valid by generic type (e.g. rtCircle is rtEdge),
* 2 and up - more and more specific match (according to rank of requirement)
*/
static int isShapeOfType(eRefType shapeType, eRefType requirement);
/**
* @brief getModeName
* @param mmode
* @return returns a string that identifies the attachment mode in enum property.
*/
static std::string getModeName(eMapMode mmode);
static eMapMode getModeByName(const std::string &modeName);
static std::string getRefTypeName(eRefType shapeType);
static eRefType getRefTypeByName(const std::string &typeName);
static GProp_GProps getInertialPropsOfShape(const std::vector<const TopoDS_Shape*> &shapes);
std::vector<App::DocumentObject*> getRefObjects() const;
const std::vector<std::string> &getSubValues() const {return subnames;}
/**
* @brief verifyReferencesAreSafe: checks if pointers in references still
* point to objects contained in open documents. This guarantees the links
* are valid. Throws Base::Exception if invalid links are found.
*/
static void verifyReferencesAreSafe(const App::PropertyLinkSubList& references);
public: //enums
static const char* eMapModeStrings[];
static const char* eRefTypeStrings[];
public: //members
std::string docName;
std::vector<std::string> objNames;
std::vector<std::string> subnames;
std::vector<std::string> shadowSubs;
eMapMode mapMode = mmDeactivated;
bool mapReverse = false;
double attachParameter = 0.0;
double surfU = 0.0, surfV = 0.0;
Base::Placement attachmentOffset;
/**
* @brief modeEnabled is an indicator, whether some mode is ever suggested
* or not. Set to false to suppress suggesting some mode, like so:
* modeEnabled[mmModeIDontLike] = false;
*/
std::vector<bool> modeEnabled;
std::vector<refTypeStringList> modeRefTypes; //a complete data structure, containing info on which modes support what selection
protected:
refTypeString cat(eRefType rt1){
refTypeString ret;
ret.push_back(rt1);
return ret;
}
refTypeString cat(eRefType rt1, eRefType rt2){
refTypeString ret;
ret.push_back(rt1);
ret.push_back(rt2);
return ret;
}
refTypeString cat(eRefType rt1, eRefType rt2, eRefType rt3){
refTypeString ret;
ret.push_back(rt1);
ret.push_back(rt2);
ret.push_back(rt3);
return ret;
}
refTypeString cat(eRefType rt1, eRefType rt2, eRefType rt3, eRefType rt4){
refTypeString ret;
ret.push_back(rt1);
ret.push_back(rt2);
ret.push_back(rt3);
ret.push_back(rt4);
return ret;
}
static void readLinks(const std::vector<App::DocumentObject*> &objs,
const std::vector<std::string> &subs, std::vector<App::GeoFeature *> &geofs,
std::vector<const TopoDS_Shape*>& shapes, std::vector<TopoDS_Shape> &storage,
std::vector<eRefType> &types);
static void throwWrongMode(eMapMode mmode);
};
class PartExport AttachEngine3D : public AttachEngine
{
TYPESYSTEM_HEADER_WITH_OVERRIDE();
public:
AttachEngine3D();
AttachEngine3D* copy() const override;
Base::Placement _calculateAttachedPlacement(
const std::vector<App::DocumentObject*> &objs,
const std::vector<std::string> &subs,
const Base::Placement &origPlacement) const override;
private:
double calculateFoldAngle(gp_Vec axA, gp_Vec axB, gp_Vec edA, gp_Vec edB) const;
};
//attacher specialized for datum planes
class PartExport AttachEnginePlane : public AttachEngine
{
TYPESYSTEM_HEADER_WITH_OVERRIDE();
public:
AttachEnginePlane();
AttachEnginePlane* copy() const override;
Base::Placement _calculateAttachedPlacement(
const std::vector<App::DocumentObject*> &objs,
const std::vector<std::string> &subs,
const Base::Placement &origPlacement) const override;
};
//attacher specialized for datum lines
class PartExport AttachEngineLine : public AttachEngine
{
TYPESYSTEM_HEADER_WITH_OVERRIDE();
public:
AttachEngineLine();
AttachEngineLine* copy() const override;
Base::Placement _calculateAttachedPlacement(
const std::vector<App::DocumentObject*> &objs,
const std::vector<std::string> &subs,
const Base::Placement &origPlacement) const override;
};
//attacher specialized for datum points
class PartExport AttachEnginePoint : public AttachEngine
{
TYPESYSTEM_HEADER_WITH_OVERRIDE();
public:
AttachEnginePoint();
AttachEnginePoint* copy() const override;
Base::Placement _calculateAttachedPlacement(
const std::vector<App::DocumentObject*> &objs,
const std::vector<std::string> &subs,
const Base::Placement &origPlacement) const override;
private:
gp_Pnt getProximityPoint(eMapMode mode, const TopoDS_Shape& s1, const TopoDS_Shape& s2) const;
};
//====================================================================
class ExceptionCancel : public Base::Exception
{
public:
ExceptionCancel() = default;
explicit ExceptionCancel(char* msg){this->setMessage(msg);}
~ExceptionCancel() noexcept override = default;
};
} // namespace Attacher
#endif // PARTATTACHER_H