Salome HOME
Copyright update 2021
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Feature.cpp
index dbaa6f8240e9b6461e7e2ace6f738ae8e0e3e111..6a1dcefc552958f7a434c57a472b6c44a2224abe 100644 (file)
@@ -1,48 +1,77 @@
+// Copyright (C) 2014-2021  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 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
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
 #include "SketchPlugin_Feature.h"
 #include "SketchPlugin_Sketch.h"
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Object.h>
-#include <ModelAPI_AttributeRefList.h>
+#include <ModelAPI_ResultConstruction.h>
+
+/// It is important.
+///
+/// Before writing a new method implementation in this file, please check the next rule:
+/// exported public methods must not be implemented in this source file. They should be inline and
+/// placed in the header file.
+/// Because it leads to the runtime problem on the Linux OS.
+///
+/// The reason is that this is an abstract interface. An interface of this class can be used in
+/// outside libraries through casting without a link to the current library.
 
 SketchPlugin_Feature::SketchPlugin_Feature()
 {
   mySketch = 0;
 }
 
-void SketchPlugin_Feature::setData(boost::shared_ptr<ModelAPI_Data> theData)
+SketchPlugin_Sketch* SketchPlugin_Feature::sketch()
 {
-  ModelAPI_Feature::setData(theData);
-
-  // find sketch that references to this feature
-  int aSketches = document()->size("Construction");
-  for(int a = 0; a < aSketches && !mySketch; a++) {
-    boost::shared_ptr<ModelAPI_Object> anObj = 
-      boost::dynamic_pointer_cast<ModelAPI_Object>(document()->feature("Construction", a));
-    if (anObj) {
-      boost::shared_ptr<SketchPlugin_Sketch> aSketch = 
-        boost::dynamic_pointer_cast<SketchPlugin_Sketch>(anObj->featureRef());
+  if (!mySketch) {
+    // find sketch that references to this feature
+    const std::set<AttributePtr>& aBackRefs = data()->refsToMe();
+    std::set<AttributePtr>::const_iterator aBackRef = aBackRefs.begin();
+    for(; aBackRef != aBackRefs.end(); aBackRef++) {
+      std::shared_ptr<SketchPlugin_Sketch> aSketch =
+        std::dynamic_pointer_cast<SketchPlugin_Sketch>((*aBackRef)->owner());
       if (aSketch) {
-        std::list<boost::shared_ptr<ModelAPI_Feature> > aList = 
-          aSketch->data()->reflist(SKETCH_ATTR_FEATURES)->list();
-        std::list<boost::shared_ptr<ModelAPI_Feature> >::iterator aSub = aList.begin();
-        for(; aSub != aList.end(); aSub++) {
-          if ((*aSub)->data()->isEqual(theData)) {
-            mySketch = aSketch.get();
-            break;
-          }
-        }
+        mySketch = aSketch.get();
+        break;
       }
     }
   }
+  return mySketch;
 }
 
-void SketchPlugin_Feature::setPreview(const boost::shared_ptr<GeomAPI_Shape>& theShape)
+void SketchPlugin_Feature::keepCurrentFeature()
 {
-  myPreview = theShape;
+  FeaturePtr aCurFeature = document()->currentFeature(true);
+  std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+      std::dynamic_pointer_cast<SketchPlugin_Feature>(aCurFeature);
+  std::shared_ptr<SketchPlugin_Sketch> aSketch =
+      std::dynamic_pointer_cast<SketchPlugin_Sketch>(aCurFeature);
+  if ((!aSketchFeature || aSketchFeature->sketch() != sketch()) &&
+      (!aSketch || aSketch.get() != sketch()))
+    myCurrentFeature = aCurFeature;
 }
 
-const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Feature::getPreview() const
+void SketchPlugin_Feature::restoreCurrentFeature()
 {
-  return myPreview;
+  if (myCurrentFeature)
+    document()->setCurrentFeature(myCurrentFeature, true);
+  myCurrentFeature = FeaturePtr();
 }