Salome HOME
6a1dcefc552958f7a434c57a472b6c44a2224abe
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Feature.cpp
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketchPlugin_Feature.h"
21 #include "SketchPlugin_Sketch.h"
22 #include <ModelAPI_Document.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Object.h>
25 #include <ModelAPI_ResultConstruction.h>
26
27 /// It is important.
28 ///
29 /// Before writing a new method implementation in this file, please check the next rule:
30 /// exported public methods must not be implemented in this source file. They should be inline and
31 /// placed in the header file.
32 /// Because it leads to the runtime problem on the Linux OS.
33 ///
34 /// The reason is that this is an abstract interface. An interface of this class can be used in
35 /// outside libraries through casting without a link to the current library.
36
37 SketchPlugin_Feature::SketchPlugin_Feature()
38 {
39   mySketch = 0;
40 }
41
42 SketchPlugin_Sketch* SketchPlugin_Feature::sketch()
43 {
44   if (!mySketch) {
45     // find sketch that references to this feature
46     const std::set<AttributePtr>& aBackRefs = data()->refsToMe();
47     std::set<AttributePtr>::const_iterator aBackRef = aBackRefs.begin();
48     for(; aBackRef != aBackRefs.end(); aBackRef++) {
49       std::shared_ptr<SketchPlugin_Sketch> aSketch =
50         std::dynamic_pointer_cast<SketchPlugin_Sketch>((*aBackRef)->owner());
51       if (aSketch) {
52         mySketch = aSketch.get();
53         break;
54       }
55     }
56   }
57   return mySketch;
58 }
59
60 void SketchPlugin_Feature::keepCurrentFeature()
61 {
62   FeaturePtr aCurFeature = document()->currentFeature(true);
63   std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
64       std::dynamic_pointer_cast<SketchPlugin_Feature>(aCurFeature);
65   std::shared_ptr<SketchPlugin_Sketch> aSketch =
66       std::dynamic_pointer_cast<SketchPlugin_Sketch>(aCurFeature);
67   if ((!aSketchFeature || aSketchFeature->sketch() != sketch()) &&
68       (!aSketch || aSketch.get() != sketch()))
69     myCurrentFeature = aCurFeature;
70 }
71
72 void SketchPlugin_Feature::restoreCurrentFeature()
73 {
74   if (myCurrentFeature)
75     document()->setCurrentFeature(myCurrentFeature, true);
76   myCurrentFeature = FeaturePtr();
77 }