Salome HOME
Merge branch 'V9_2_2_BR'
[modules/shaper.git] / src / ModelAPI / ModelAPI_Tools.h
1 // Copyright (C) 2014-2019  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 #ifndef ModelAPI_Tools_HeaderFile
21 #define ModelAPI_Tools_HeaderFile
22
23 #include "ModelAPI.h"
24
25 class ModelAPI_CompositeFeature;
26 class ModelAPI_Document;
27 class ModelAPI_Feature;
28 class ModelAPI_Result;
29 class ModelAPI_ResultParameter;
30 class ModelAPI_ResultBody;
31
32 class GeomAPI_Shape;
33
34 #include <memory>
35 #include <vector>
36 #include <map>
37 #include <set>
38 #include <list>
39
40 namespace ModelAPI_Tools {
41 /// Returns shape from the given Result object
42 MODELAPI_EXPORT std::shared_ptr<GeomAPI_Shape> shape(
43                                    const std::shared_ptr<ModelAPI_Result>& theResult);
44
45 /*! Returns the feature error generated according to feature error and exec state
46  * \param theFeature a feature
47  * \return error value or empty string
48  */
49 MODELAPI_EXPORT std::string getFeatureError(const std::shared_ptr<ModelAPI_Feature>& theFeature);
50
51 /*!
52  * Searches for variable with name \param theName in \param theDocument. 
53  * If found, set it value in the \param outValue and returns true.
54  * theSearcher must be located later in the history than the found variable.
55  */
56 MODELAPI_EXPORT bool findVariable(const std::shared_ptr<ModelAPI_Document>& theDocument,
57                                   std::shared_ptr<ModelAPI_Feature> theSearcher,
58                                   const std::string& theName, double& outValue,
59                                   std::shared_ptr<ModelAPI_ResultParameter>& theParam);
60
61 /*!
62  * Searches for variable with name \param theName in the active document (Part), when
63  * in the root document (PartSet). If found, set it value in the \param outValue
64  * and returns true. If \param theDocument is empty active document is used.
65  * theSearcher must be located later in the history than the found variable.
66  */
67 MODELAPI_EXPORT bool findVariable(std::shared_ptr<ModelAPI_Feature> theSearcher,
68   const std::string& theName,
69   double& outValue, std::shared_ptr<ModelAPI_ResultParameter>& theParam,
70   const std::shared_ptr<ModelAPI_Document>& theDocument = std::shared_ptr<ModelAPI_Document>());
71
72 /*!
73  * Searches for Part result that contains the reference to the given document.
74  * The result must be presented in the tree.
75  * \param theMain document that contains the searched feature
76  * \param theSub document that is searched, the resulting feature references to it
77  * \returns null if not found
78  */
79 MODELAPI_EXPORT std::shared_ptr<ModelAPI_Result> findPartResult(
80                                               const std::shared_ptr<ModelAPI_Document>& theMain,
81                                               const std::shared_ptr<ModelAPI_Document>& theSub);
82
83 /*!
84  * Searches for Part the feature that contains in result the reference to the given document.
85  * The result may be disabled.
86  * \param theMain document that contains the searched feature
87  * \param theSub document that is searched, the resulting feature references to it
88  * \returns null if not found
89  */
90 MODELAPI_EXPORT std::shared_ptr<ModelAPI_Feature> findPartFeature(
91                                            const std::shared_ptr<ModelAPI_Document>& theMain,
92                                            const std::shared_ptr<ModelAPI_Document>& theSub);
93
94 /*!
95  * Returns the composite feature - parent of this feature.
96  * \param theFeature the sub-element of composite
97  * \returns null if it is not sub-element of composite
98  */
99 MODELAPI_EXPORT std::shared_ptr<ModelAPI_CompositeFeature> compositeOwner(
100                                         const std::shared_ptr<ModelAPI_Feature>& theFeature);
101
102 /*!
103  * Returns the result - parent of this result.
104  * \param theSub the sub-element of composit result
105  * \param theRoot if it is true, returns the root father
106  * \returns null if it is not sub-element of composite
107  */
108 MODELAPI_EXPORT std::shared_ptr<ModelAPI_ResultBody>
109   bodyOwner(const std::shared_ptr<ModelAPI_Result>& theSub, const bool theRoot = false);
110 /*!
111  * Returns index of this result in parent (if parent exists, returned by bodyOwner)
112  * \returns zero-base index, or -1 if not found
113  */
114 MODELAPI_EXPORT int bodyIndex(const std::shared_ptr<ModelAPI_Result>& theSub);
115
116 /*!
117 * Returns true if the result contains a not empty list of sub results.
118 * It processes result compsolid.
119 * \param theResult a result object
120 * \returns boolean value
121 */
122 MODELAPI_EXPORT bool hasSubResults(const std::shared_ptr<ModelAPI_Result>& theResult);
123
124 /*!
125 *  collects recursively all subs of the given result
126 */
127 MODELAPI_EXPORT void allSubs(const std::shared_ptr<ModelAPI_ResultBody>& theResult,
128                              std::list<std::shared_ptr<ModelAPI_Result> >& theResults);
129
130 /*!
131 * Adds the results of the given feature to theResults list: including disabled and sub-results
132 */
133 MODELAPI_EXPORT void allResults(const std::shared_ptr<ModelAPI_Feature>& theFeature,
134                                 std::list<std::shared_ptr<ModelAPI_Result> >& theResults);
135
136 /*!
137  Returns true if there are no parts in the document, which are not activated
138  \param theNotActivatedNames out string which contains not activated names
139  \return a boolean value
140  */
141 MODELAPI_EXPORT bool allDocumentsActivated(std::string& theNotActivatedNames);
142
143 /*! Removes features from the document
144 * \param theFeatures a list of features to be removed
145 * \param theFlushRedisplay a boolean value if the redisplay signal should be flushed
146 * \return true if at least one feature is removed
147 */
148 MODELAPI_EXPORT bool removeFeaturesAndReferences(
149                        const std::set<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
150                                                  const bool theFlushRedisplay = false,
151                                                  const bool theUseComposite = false,
152                                                  const bool theUseRecursion = true);
153
154 /*! Removes features from the document
155 * \param theFeatures a list of features to be removed
156 * \param theFlushRedisplay a boolean value if the redisplay signal should be flushed
157 * \return true if at least one feature is removed
158 */
159 MODELAPI_EXPORT bool removeFeatures(
160                             const std::set<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
161                             const bool theFlushRedisplay);
162
163 /*! Build a map of references for the given set of features
164 * \param theFeatures a list of features
165 * \param theReferences a map of all references to the features
166 * \param theUseComposite state if the composite features of the reference should be in the map
167 * \param theUseRecursion state if references for features out of the sources feature
168          list are put into the result container. E.g. if theFeatures contains "Sketch_2", map will
169          contain references to this feature also.
170 */
171 MODELAPI_EXPORT void findAllReferences(
172   const std::set<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
173   std::map<std::shared_ptr<ModelAPI_Feature>,
174   std::set<std::shared_ptr<ModelAPI_Feature> > >& theReferences,
175   const bool theUseComposite = true,
176   const bool theUseRecursion = true);
177
178 /*! In the map of references find all features referenced to the source feature
179 * \param theFeatures a list of features to find references
180 * \param theReferences a map of all references to the features
181 * \param theFeaturesRefsTo an out list of referenced features
182 */
183 MODELAPI_EXPORT void findRefsToFeatures(
184   const std::set<std::shared_ptr<ModelAPI_Feature> >& aFeatures,
185   const std::map<std::shared_ptr<ModelAPI_Feature>,
186                  std::set<std::shared_ptr<ModelAPI_Feature> > >& aReferences,
187   std::set<std::shared_ptr<ModelAPI_Feature> >& aFeaturesRefsTo);
188
189 /*! Finds referenced of the feature to objects and collects concealed results.
190 * \param theFeatures a model feature
191 * \param theResults container for concealed results. It has no duplications
192 */
193 MODELAPI_EXPORT void getConcealedResults(const std::shared_ptr<ModelAPI_Feature>& theFeature,
194                                    std::list<std::shared_ptr<ModelAPI_Result> >& theResults);
195
196 /*! Return the default name of the result according the features it depends or name of the feature.
197  *  Return also whether the name is get from the concealing result of parent object
198  *  (means that concealing result has user-defined name).
199  */
200 MODELAPI_EXPORT std::pair<std::string, bool> getDefaultName(
201   const std::shared_ptr<ModelAPI_Result>& theResult, const bool theInherited = true);
202
203 /*! Collect all parents for the given feature, including the Part
204  */
205 MODELAPI_EXPORT std::set<std::shared_ptr<ModelAPI_Feature> >
206     getParents(const std::shared_ptr<ModelAPI_Feature>& theFeature);
207 }
208
209 #endif