Salome HOME
Copyright update 2022
[modules/shaper.git] / src / ModelAPI / ModelAPI_Tools.h
1 // Copyright (C) 2014-2022  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 #include <ModelAPI_AttributeSelectionList.h>
25
26 class ModelAPI_CompositeFeature;
27 class ModelAPI_Document;
28 class ModelAPI_Feature;
29 class ModelAPI_Result;
30 class ModelAPI_ResultParameter;
31 class ModelAPI_ResultBody;
32
33 class GeomAlgoAPI_MakeShape;
34 class GeomAPI_Shape;
35 class GeomAPI_ShapeHierarchy;
36
37 #include <memory>
38 #include <vector>
39 #include <map>
40 #include <set>
41 #include <list>
42
43 namespace ModelAPI_Tools {
44 /// Returns shape from the given Result object
45 MODELAPI_EXPORT std::shared_ptr<GeomAPI_Shape> shape(
46                                    const std::shared_ptr<ModelAPI_Result>& theResult);
47
48 /*! Returns the feature error generated according to feature error and exec state
49  * \param theFeature a feature
50  * \return error value or empty string
51  */
52 MODELAPI_EXPORT std::string getFeatureError(const std::shared_ptr<ModelAPI_Feature>& theFeature);
53
54 struct ResultBaseAlgo {
55   std::shared_ptr<ModelAPI_ResultBody> resultBody;
56   std::shared_ptr<GeomAPI_Shape> baseShape;
57   std::shared_ptr<GeomAlgoAPI_MakeShape> makeShape;
58 };
59
60 MODELAPI_EXPORT void loadModifiedShapes(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
61   const std::list<std::shared_ptr<GeomAPI_Shape>>& theBaseShapes,
62   const std::list<std::shared_ptr<GeomAPI_Shape>>& theTools,
63   const std::shared_ptr<GeomAlgoAPI_MakeShape>& theMakeShape,
64   const std::shared_ptr<GeomAPI_Shape> theResultShape,
65   const std::string& theNamePrefix = "");
66
67 MODELAPI_EXPORT void loadModifiedShapes(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
68   const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
69   const std::shared_ptr<GeomAlgoAPI_MakeShape>& theMakeShape,
70   const std::string theName);
71 /// Stores deleted shapes.
72 MODELAPI_EXPORT void loadDeletedShapes(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
73   const std::shared_ptr<GeomAPI_Shape> theBaseShape,
74   const std::list<std::shared_ptr<GeomAPI_Shape>>& theTools,
75   const std::shared_ptr<GeomAlgoAPI_MakeShape>& theMakeShape,
76   const std::shared_ptr<GeomAPI_Shape> theResultShapesCompound);
77
78 /// Stores deleted shapes.
79 MODELAPI_EXPORT void loadDeletedShapes(std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
80   const std::list<std::shared_ptr<GeomAPI_Shape>>& theTools,
81   const std::shared_ptr<GeomAPI_Shape> theResultShapesCompound);
82
83 /*!
84  * Searches for variable with name \param theName in \param theDocument.
85  * If found, set it value in the \param outValue and returns true.
86  * theSearcher must be located later in the history than the found variable.
87  */
88 MODELAPI_EXPORT bool findVariable(const std::shared_ptr<ModelAPI_Document>& theDocument,
89                                   std::shared_ptr<ModelAPI_Feature> theSearcher,
90                                   const std::wstring& theName, double& outValue,
91                                   std::shared_ptr<ModelAPI_ResultParameter>& theParam);
92
93 /*!
94  * Searches for variable with name \param theName in the active document (Part), when
95  * in the root document (PartSet). If found, set it value in the \param outValue
96  * and returns true. If \param theDocument is empty active document is used.
97  * theSearcher must be located later in the history than the found variable.
98  */
99 MODELAPI_EXPORT bool findVariable(std::shared_ptr<ModelAPI_Feature> theSearcher,
100   const std::wstring& theName,
101   double& outValue, std::shared_ptr<ModelAPI_ResultParameter>& theParam,
102   const std::shared_ptr<ModelAPI_Document>& theDocument = std::shared_ptr<ModelAPI_Document>());
103
104 /*!
105  * Searches for Part result that contains the reference to the given document.
106  * The result must be presented in the tree.
107  * \param theMain document that contains the searched feature
108  * \param theSub document that is searched, the resulting feature references to it
109  * \returns null if not found
110  */
111 MODELAPI_EXPORT std::shared_ptr<ModelAPI_Result> findPartResult(
112                                               const std::shared_ptr<ModelAPI_Document>& theMain,
113                                               const std::shared_ptr<ModelAPI_Document>& theSub);
114
115 /*!
116  * Searches for Part the feature that contains in result the reference to the given document.
117  * The result may be disabled.
118  * \param theMain document that contains the searched feature
119  * \param theSub document that is searched, the resulting feature references to it
120  * \returns null if not found
121  */
122 MODELAPI_EXPORT std::shared_ptr<ModelAPI_Feature> findPartFeature(
123                                            const std::shared_ptr<ModelAPI_Document>& theMain,
124                                            const std::shared_ptr<ModelAPI_Document>& theSub);
125
126 /*!
127  * Returns the composite feature - parent of this feature.
128  * \param theFeature the sub-element of composite
129  * \returns null if it is not sub-element of composite
130  */
131 MODELAPI_EXPORT std::shared_ptr<ModelAPI_CompositeFeature> compositeOwner(
132                                         const std::shared_ptr<ModelAPI_Feature>& theFeature);
133
134 /*!
135  * Returns the result - parent of this result.
136  * \param theSub the sub-element of composit result
137  * \param theRoot if it is true, returns the root father
138  * \returns null if it is not sub-element of composite
139  */
140 MODELAPI_EXPORT std::shared_ptr<ModelAPI_ResultBody>
141   bodyOwner(const std::shared_ptr<ModelAPI_Result>& theSub, const bool theRoot = false);
142 /*!
143  * Returns index of this result in parent (if parent exists, returned by bodyOwner)
144  * \returns zero-base index, or -1 if not found
145  */
146 MODELAPI_EXPORT int bodyIndex(const std::shared_ptr<ModelAPI_Result>& theSub);
147
148 /*!
149 * Returns true if the result contains a not empty list of sub results.
150 * It processes result compsolid.
151 * \param theResult a result object
152 * \returns boolean value
153 */
154 MODELAPI_EXPORT bool hasSubResults(const std::shared_ptr<ModelAPI_Result>& theResult);
155
156 /*!
157 *  collects recursively all subs of the given result
158 */
159 MODELAPI_EXPORT void allSubs(const std::shared_ptr<ModelAPI_ResultBody>& theResult,
160                              std::list<std::shared_ptr<ModelAPI_Result> >& theResults,
161                              const bool theLowerOnly = false);
162
163 /*!
164 * Adds the results of the given feature to theResults list: including disabled and sub-results
165 */
166 MODELAPI_EXPORT void allResults(const std::shared_ptr<ModelAPI_Feature>& theFeature,
167                                 std::list<std::shared_ptr<ModelAPI_Result> >& theResults);
168
169 /*!
170  Returns true if there are no parts in the document, which are not activated
171  \param theNotActivatedNames out string which contains not activated names
172  \return a boolean value
173  */
174 MODELAPI_EXPORT bool allDocumentsActivated(std::wstring& theNotActivatedNames);
175
176 /*! Removes features from the document
177 * \param theFeatures a list of features to be removed
178 * \param theFlushRedisplay a boolean value if the redisplay signal should be flushed
179 * \return true if at least one feature is removed
180 */
181 MODELAPI_EXPORT bool removeFeaturesAndReferences(
182                        const std::set<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
183                                                  const bool theFlushRedisplay = false,
184                                                  const bool theUseComposite = false,
185                                                  const bool theUseRecursion = true);
186
187 /*! Removes features from the document
188 * \param theFeatures a list of features to be removed
189 * \param theFlushRedisplay a boolean value if the redisplay signal should be flushed
190 * \return true if at least one feature is removed
191 */
192 MODELAPI_EXPORT bool removeFeatures(
193                             const std::set<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
194                             const bool theFlushRedisplay);
195
196 /*! Build a map of references for the given set of features
197 * \param theFeatures a list of features
198 * \param theReferences a map of all references to the features
199 * \param theUseComposite state if the composite features of the reference should be in the map
200 * \param theUseRecursion state if references for features out of the sources feature
201          list are put into the result container. E.g. if theFeatures contains "Sketch_2", map will
202          contain references to this feature also.
203 */
204 MODELAPI_EXPORT void findAllReferences(
205   const std::set<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
206   std::map<std::shared_ptr<ModelAPI_Feature>,
207   std::set<std::shared_ptr<ModelAPI_Feature> > >& theReferences,
208   const bool theUseComposite = true,
209   const bool theUseRecursion = true);
210
211 /*! In the map of references find all features referenced to the source feature
212 * \param theFeatures a list of features to find references
213 * \param theReferences a map of all references to the features
214 * \param theFeaturesRefsTo an out list of referenced features
215 */
216 MODELAPI_EXPORT void findRefsToFeatures(
217   const std::set<std::shared_ptr<ModelAPI_Feature> >& aFeatures,
218   const std::map<std::shared_ptr<ModelAPI_Feature>,
219                  std::set<std::shared_ptr<ModelAPI_Feature> > >& aReferences,
220   std::set<std::shared_ptr<ModelAPI_Feature> >& aFeaturesRefsTo);
221
222 /*! Finds referenced of the feature to objects and collects concealed results.
223 * \param theFeatures a model feature
224 * \param theResults container for concealed results. It has no duplications
225 */
226 MODELAPI_EXPORT void getConcealedResults(const std::shared_ptr<ModelAPI_Feature>& theFeature,
227                                    std::list<std::shared_ptr<ModelAPI_Result> >& theResults);
228
229 /*! Return the default name of the result according the features it depends or name of the feature.
230  *  Return also whether the name is get from the concealing result of parent object
231  *  (means that concealing result has user-defined name).
232  * \param[in] theRecursive  recursively check the concealed results if they have user-defined names
233  */
234 MODELAPI_EXPORT std::pair<std::wstring, bool> getDefaultName(
235   const std::shared_ptr<ModelAPI_Result>& theResult,
236   const bool theInherited = true,
237   const bool theRecursive = false);
238
239 /*! Collect all parents for the given feature, including the Part
240  */
241 MODELAPI_EXPORT std::set<std::shared_ptr<ModelAPI_Feature> >
242     getParents(const std::shared_ptr<ModelAPI_Feature>& theFeature);
243
244 /*! Store shape and its parent shape to the hierarchy data structure
245  *  \param[in] theShape      the shape to store
246  *  \param[in] theContext    the result (parent shape) for the given shape
247  *  \param[out] theHierarchy container for shape hierarchy
248  */
249 MODELAPI_EXPORT void fillShapeHierarchy(
250     const std::shared_ptr<GeomAPI_Shape>& theShape,
251     const std::shared_ptr<ModelAPI_Result>& theContext,
252     GeomAPI_ShapeHierarchy& theHierarchy);
253
254 /*! Creates a remove result features with the given results
255 */
256 MODELAPI_EXPORT void removeResults(const std::list<std::shared_ptr<ModelAPI_Result> >& theResults);
257
258 /*! Returns current deflection in the given result
259 * \param theResult a result object
260 * \return a deflection value or -1 if it was not defined
261 */
262 MODELAPI_EXPORT double getDeflection(const std::shared_ptr<ModelAPI_Result>& theResult);
263
264 /*! Sets the deflection value
265 * \param theResult a result object
266 * \param a deflection value
267 */
268 MODELAPI_EXPORT void setDeflection(std::shared_ptr<ModelAPI_Result> theResult,
269   const double theDeflection);
270
271 /*! Returns current color of the current result
272 * \param[in] theResult a result object
273 * \param[out] theColor a color values if it is defined
274 */
275 MODELAPI_EXPORT void getColor(const std::shared_ptr<ModelAPI_Result>& theResult,
276   std::vector<int>& theColor);
277
278 /*! Set color of the result
279 * \param[in] theResult a result object
280 * \param[in] theColor a color values
281 */
282 MODELAPI_EXPORT void setColor(std::shared_ptr<ModelAPI_Result> theResult,
283   const std::vector<int>& theColor);
284
285 /*! Returns number of iso-lines of the current result
286 * \param[in] theResult a result object
287 * \param[out] theNbLines values of iso-lines
288 */
289 MODELAPI_EXPORT void getIsoLines(const std::shared_ptr<ModelAPI_Result>& theResult,
290   bool& isVisible, std::vector<int>& theNbLines);
291
292 /*! Set number of iso-lines of the result
293 * \param[in] theResult a result object
294 * \param[in] theIso nb iso-lines
295 */
296 MODELAPI_EXPORT void setIsoLines(std::shared_ptr<ModelAPI_Result> theResult,
297   const std::vector<int>& theIso);
298
299 /*! Set visibility of Iso lines
300 * \param[in] theResult a result object
301 * \param[in] theShow is a visibility flag
302 */
303 MODELAPI_EXPORT void showIsoLines(std::shared_ptr<ModelAPI_Result> theResult, bool theShow);
304
305 MODELAPI_EXPORT bool isShownIsoLines(std::shared_ptr<ModelAPI_Result> theResult);
306
307 /*! Returns current transparency in the given result
308 * \param theResult a result object
309 * \return a transparency value or -1 if it was not defined
310 */
311 MODELAPI_EXPORT double getTransparency(const std::shared_ptr<ModelAPI_Result>& theResult);
312
313 /*! Set transparency for the given result
314 * \param theResult a result object
315 * \param a transparency value
316 */
317 MODELAPI_EXPORT void setTransparency(std::shared_ptr<ModelAPI_Result> theResult,
318   double theTransparency);
319
320
321 /*! Copies all visualization attributes from one result to another.
322 * \param theSource a result that contains the copied attributes
323 * \param theDest a destination result that takes the visualization attributes
324 */
325 MODELAPI_EXPORT void copyVisualizationAttrs(std::shared_ptr<ModelAPI_Result> theSource,
326                                             std::shared_ptr<ModelAPI_Result> theDest);
327
328 /*! Copies image attribute from one result to another.
329 * \param theSource a result that contains the image data
330 * \param theDest a destination result that takes the image data
331 */
332 MODELAPI_EXPORT void copyImageAttribute(std::shared_ptr<ModelAPI_Result> theSource,
333                                         std::shared_ptr<ModelAPI_Result> theDest);
334
335 /*! Produces list of features that reference to the given target (directly or through sub-results)
336 * \param theTarget the referenced result
337 * \param theFeatureKind the resulting features filter: the feature kind or all for the empty string
338 * \param theSortResults to sort the resulting list of features by the features creation order
339 */
340 MODELAPI_EXPORT std::list<std::shared_ptr<ModelAPI_Feature> > referencedFeatures(
341   std::shared_ptr<ModelAPI_Result> theTarget, const std::string& theFeatureKind,
342   const bool theSortResults);
343
344 /*!
345  * Returns true if something in selection is presented in the results list
346  */
347 MODELAPI_EXPORT bool isInResults(AttributeSelectionListPtr theSelection,
348                                  const std::list<ResultPtr>& theResults,
349                                  std::set<ResultPtr>& theCashedResults);
350
351 /*! Returns a container with the current color value.
352 *   These are tree int values for RGB definition.
353 *   It returns the next random color.
354 * \param theValues vector of values
355 * \param theReset flag to call when disabling Auto-color
356 */
357 MODELAPI_EXPORT void findRandomColor(std::vector<int>& theValues, bool theReset = false);
358
359 }
360
361 #endif