Salome HOME
Issue #2481: Application error when create fillet
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Interface.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "ModelHighAPI_Interface.h"
22 //--------------------------------------------------------------------------------------
23 #include <Events_InfoMessage.h>
24
25 #include <ModelAPI_CompositeFeature.h>
26 #include <ModelAPI_Events.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_Result.h>
31
32 #include "ModelHighAPI_Selection.h"
33 //--------------------------------------------------------------------------------------
34 ModelHighAPI_Interface::ModelHighAPI_Interface(
35   const std::shared_ptr<ModelAPI_Feature> & theFeature)
36 : myFeature(theFeature)
37 {
38
39 }
40
41 ModelHighAPI_Interface::~ModelHighAPI_Interface()
42 {
43
44 }
45
46 //--------------------------------------------------------------------------------------
47 std::shared_ptr<ModelAPI_Feature> ModelHighAPI_Interface::feature() const
48 {
49   return myFeature;
50 }
51
52 std::shared_ptr<ModelHighAPI_Interface> ModelHighAPI_Interface::subFeature(const int theIndex) const
53 {
54   CompositeFeaturePtr aCompositeFeature =
55     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
56   if(!aCompositeFeature.get()) {
57     return InterfacePtr();
58   }
59
60   FeaturePtr aSubFeature = aCompositeFeature->subFeature(theIndex);
61   if(!aSubFeature.get()) {
62     return InterfacePtr();
63   }
64
65   return InterfacePtr(new ModelHighAPI_Interface(aSubFeature));
66 }
67
68 const std::string& ModelHighAPI_Interface::getKind() const
69 {
70   return feature()->getKind();
71 }
72
73 void ModelHighAPI_Interface::execute(bool isForce)
74 {
75   if (isForce) {
76     SessionPtr aMgr = ModelAPI_Session::get();
77     ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
78     FeaturePtr aFeature = feature();
79     if(aFactory->validate(aFeature))
80       aFeature->execute();
81   }
82
83   Events_Loop* aLoop = Events_Loop::loop();
84   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
85   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
86   //aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
87   //aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
88 }
89
90 void ModelHighAPI_Interface::setName(const std::string& theName)
91 {
92   feature()->data()->setName(theName);
93 }
94
95 std::string ModelHighAPI_Interface::name() const
96 {
97   return feature()->data()->name();
98 }
99
100 ModelHighAPI_Selection ModelHighAPI_Interface::result() const
101 {
102   std::list<ModelHighAPI_Selection> aResults = results();
103   if (aResults.empty())
104     return ModelHighAPI_Selection(std::shared_ptr<ModelAPI_Result>());
105   return aResults.front();
106 }
107
108 std::list<ModelHighAPI_Selection> ModelHighAPI_Interface::results() const
109 {
110   const_cast<ModelHighAPI_Interface*>(this)->execute();
111
112   std::list<ModelHighAPI_Selection> aSelectionList;
113
114   std::list<std::shared_ptr<ModelAPI_Result> > aResults = feature()->results();
115   for (auto it = aResults.begin(), end = aResults.end(); it != end; ++it) {
116     if (!(*it)->isDisabled())
117       aSelectionList.push_back(ModelHighAPI_Selection(*it));
118   }
119
120   return aSelectionList;
121 }
122
123 std::shared_ptr<ModelAPI_Result> ModelHighAPI_Interface::defaultResult() const
124 {
125   const_cast<ModelHighAPI_Interface*>(this)->execute();
126
127   return feature()->lastResult();
128 }
129
130 void ModelHighAPI_Interface::throwException(const std::string & theDescription)
131 {
132   Events_InfoMessage("ModelHighAPI_Interface", theDescription).send();
133 }
134
135 const std::string& ModelHighAPI_Interface::attributeGetter(const std::string& theAttrName)
136 {
137   return myAttrGetter[theAttrName];
138 }