Salome HOME
219eb68e9d11999d8f151d198061e92e715327d0
[modules/shaper.git] / src / PartSet / PartSet_Tools.h
1 // Copyright (C) 2014-2023  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 PartSet_Tools_H
21 #define PartSet_Tools_H
22
23 #include "PartSet.h"
24
25 #include <gp_Pnt.hxx>
26
27 #include <QPoint>
28 #include <QList>
29 #include <QCursor>
30
31 #include <ModelAPI_CompositeFeature.h>
32 #include <ModelAPI_Object.h>
33 #include <ModelAPI_Attribute.h>
34 #include <ModelAPI_AttributeSelection.h>
35
36 #include <Events_Message.h>
37
38 #include <TopoDS_Shape.hxx>
39
40 #include <memory>
41
42 class V3d_View;
43 class ModuleBase_IViewWindow;
44 class ModuleBase_ViewerPrs;
45 class ModuleBase_IWorkshop;
46 class GeomDataAPI_Point2D;
47 class GeomAPI_Shape;
48 class GeomAPI_Pln;
49 class GeomAPI_Pnt2d;
50 class GeomAPI_Pnt;
51 class GeomAPI_Edge;
52 class GeomAPI_Vertex;
53 class ModelAPI_Result;
54
55 class QMouseEvent;
56
57 /*!
58  * \class PartSet_Tools
59  * \ingroup Modules
60  * \brief The operation for the sketch feature creation
61  */
62 class PARTSET_EXPORT PartSet_Tools
63 {
64 public:
65   /// \enum ConstraintVisibleState types of constraints which visible state can be
66   /// changed in the sketch widget
67   enum ConstraintVisibleState
68   {
69     Geometrical = 0, // all constrains excepting dimensional
70     Dimensional,     // lenght, distance, radius and angle constraints
71     Expressions,     // parameter text should be shown in dimensional constraint
72     Any              // both, geometrical and dimensional, types of constraints
73   };
74
75  public:
76   /// Returns default value of AIS presentation
77   /// \return integer value
78   static int getAISDefaultWidth();
79
80   /// Converts the 2D screen point to the 3D point on the view according to the point of view
81   /// \param thePoint a screen point
82   /// \param theView a 3D view
83   static gp_Pnt convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView);
84
85   /// \brief Converts the 3D point to the projected coodinates on the sketch plane.
86   /// \param thePoint the 3D point in the viewer
87   /// \param theSketch the sketch feature
88   /// \param theView a view 3d object
89   /// \param theX the X coordinate
90   /// \param theY the Y coordinate
91   static void convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
92                           Handle(V3d_View) theView,
93                           double& theX, double& theY);
94
95   /// \brief Converts the 3D point to the projected coodinates on the sketch plane.
96   /// \param theSketch the sketch feature
97   /// \param thePnt the 3D point in the viewer
98   /// \returns the converted point object
99   static std::shared_ptr<GeomAPI_Pnt2d> convertTo2D(FeaturePtr theSketch,
100                                                     const std::shared_ptr<GeomAPI_Pnt>& thePnt);
101
102   /// \brief Converts the 2D projected coodinates on the sketch plane to the 3D point.
103   /// \param theX the X coordinate
104   /// \param theY the Y coordinate
105   /// \param theSketch the sketch feature
106   static std::shared_ptr<GeomAPI_Pnt> convertTo3D(const double theX, const double theY,
107                                                   FeaturePtr theSketch);
108
109   /// Returns pointer to the root document.
110   static std::shared_ptr<ModelAPI_Document> document();
111
112   /// \brief Save the double to the feature. If the attribute is double, it is filled.
113   /// \param theFeature the feature
114   /// \param theX the horizontal coordinate
115   /// \param theAttribute the feature attribute
116   static void setFeatureValue(FeaturePtr theFeature, double theX, const std::string& theAttribute);
117
118   /// \brief Returns the feature double value if it is.
119   /// \param theFeature the feature
120   /// \param theAttribute the feature attribute
121   /// \param isValid an output parameter whether the value is valid
122   /// \returns the feature value
123   static double featureValue(FeaturePtr theFeature, const std::string& theAttribute, bool& isValid);
124
125   /// Find a feature in the attribute of the given feature. If the kind is not empty,
126   /// the return feature should be this type. Otherwise it is null
127   /// \param theFeature a source feature
128   /// \param theAttribute a name of the requried attribute attribute
129   /// \param theKind an output feature kind
130   /// \return the feature
131   static FeaturePtr feature(FeaturePtr theFeature, const std::string& theAttribute,
132                             const std::string& theKind);
133
134   /// Creates a constraint on two points
135   /// \param theSketch a sketch feature
136   /// \param thePoint1 the first point
137   /// \param thePoint2 the second point
138   static void createConstraint(CompositeFeaturePtr theSketch,
139                                std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
140                                std::shared_ptr<GeomDataAPI_Point2D> thePoint2);
141
142   /// Create a sketch plane instance
143   /// \param theSketch a sketch feature
144   /// \return API object of geom plane
145   static std::shared_ptr<GeomAPI_Pln> sketchPlane(CompositeFeaturePtr theSketch);
146
147   /// Create a sketch plane instance
148   /// \param theSketch a sketch feature
149   /// \return API object of geom plane
150   static void nullifySketchPlane(CompositeFeaturePtr theSketch);
151
152   /// Create a point 3D on a basis of point 2D and sketch feature
153   /// \param thePoint2D a point on a sketch
154   /// \param theSketch a sketch feature
155   /// \return API object of point 3D
156   static std::shared_ptr<GeomAPI_Pnt> point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
157                                                 CompositeFeaturePtr theSketch);
158
159   /// Finds a line (arc or circle) by given edge
160   /// \param theShape an edge
161   /// \param theObject a selected result object
162   /// \param theSketch a sketch feature
163   /// \return result of found feature or NULL
164   static std::shared_ptr<ModelAPI_Result> findFixedObjectByExternal(
165                                              const TopoDS_Shape& theShape,
166                                              const ObjectPtr& theObject,
167                                              CompositeFeaturePtr theSketch);
168
169   /// Creates a line (arc or circle) by given edge
170   /// Created line will have fixed constraint
171   /// \param theShape an edge
172   /// \param theObject a selected result object
173   /// \param theSketch a sketch feature
174   /// \param theTemporary the created external object is temporary, execute is not performed for it
175   /// \param theCreatedFeature a new projection feature
176   /// \return result of created feature
177   static std::shared_ptr<ModelAPI_Result> createFixedObjectByExternal(
178                                                const std::shared_ptr<GeomAPI_Shape>& theShape,
179                                                const ObjectPtr& theObject,
180                                                CompositeFeaturePtr theSketch,
181                                                const bool theTemporary,
182                                                FeaturePtr& theCreatedFeature);
183
184     /// Checks whether the list of selected presentations contains the given one
185   /// \param theSelected a list of presentations
186   /// \param thePrs a presentation to be found
187   /// \return - result of check, true if the list contains the prs
188   static bool isContainPresentation(const QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theSelected,
189                                     const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs);
190
191   /**
192   * Find attribute of object which corresponds to the given shape
193   * \param theObj - an object
194   * \param theShape - a Shape
195   * \param theSketch - a Sketch to get a plane of converting to 2d
196   * \return Found attribute and index of point if the attribute is an array
197   */
198   static std::pair<AttributePtr, int> findAttributeBy2dPoint(ObjectPtr theObj,
199                                                              const TopoDS_Shape theShape,
200                                                              FeaturePtr theSketch);
201
202   /**
203   * Finds an attribute value in attribute reference attribute value
204   * \param theAttribute - an attribure reference filled with an attribute
205   * \param theWorkshop a reference to workshop
206   * \return a geometry shape
207   */
208   static std::shared_ptr<GeomAPI_Shape> findShapeBy2DPoint(const AttributePtr& theAttribute,
209                                          ModuleBase_IWorkshop* theWorkshop);
210
211   /**
212   * Returns point of coincidence feature
213   * \param theFeature the coincidence feature
214   * \param theAttribute the attribute name
215   */
216   static std::shared_ptr<GeomAPI_Pnt2d> getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
217                                                  const std::string& theAttribute);
218
219   /**
220   * Convertes parameters into a geom point
221   * \param theEvent a Qt event to find mouse position
222   * \param theWindow view window to define eye of view
223   * \param theSketch to convert 3D point coordinates into coorditates of the sketch plane
224   */
225   static std::shared_ptr<GeomAPI_Pnt2d> getPnt2d(QMouseEvent* theEvent,
226                                                  ModuleBase_IViewWindow* theWindow,
227                                                  const FeaturePtr& theSketch);
228
229   /** Returns point 2d from selected shape
230    *  \param theView a view window
231    *  \param theShape a vertex shape
232    *  \param theX an output value of X coordinate
233    *  \param theY an output value of Y coordinate
234    */
235   static std::shared_ptr<GeomAPI_Pnt2d> getPnt2d(const Handle(V3d_View)& theView,
236                                                  const TopoDS_Shape& theShape,
237                                                  const FeaturePtr& theSketch);
238
239   /**
240   * Gets all references to the feature, take coincidence constraint features, get point 2d attributes
241   * and compare the point value to be equal with the given. Returns the first feature, which has
242   * equal points.
243   * \param theFeature the coincidence feature
244   * \param thePoint a 2d point
245   * \return the coincidence feature or null
246   */
247   static FeaturePtr findFirstCoincidence(const FeaturePtr& theFeature,
248                                          std::shared_ptr<GeomAPI_Pnt2d> thePoint);
249
250   /**
251   * Returns list of features connected in a councedence feature point
252   * \param theStartCoin the coincidence feature
253   * \param theList a list which collects lines features
254   * \param theCoincidencies a list of coincidence features
255   * \param theAttr the attribute name
256   */
257   static void findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
258                                QList<FeaturePtr>& theCoincidencies,
259                                std::string theAttr, QList<bool>& theIsAttributes);
260
261   /*
262   * Finds and returns feature reerenced to the paramenter feature with the given name if found
263   * \param theFeature a source feature where refsToMe is obtained
264   * \param theFeatureId an indentifier of the searched feature
265   */
266   static FeaturePtr findRefsToMeFeature(FeaturePtr theFeature, const std::string& theFeatureId)
267   {
268     if (!theFeature.get())
269       return FeaturePtr();
270
271     // find first projected feature and edit it
272     const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
273     std::set<AttributePtr>::const_iterator anIt;
274     for (anIt = aRefsList.cbegin(); anIt != aRefsList.cend(); ++anIt) {
275       FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*anIt)->owner());
276       if (aRefFeature && aRefFeature->getKind() == theFeatureId)
277         return aRefFeature;
278     }
279     return FeaturePtr();
280   }
281
282   /**
283   * Returns point of a coincedence
284   * \param theStartCoin the coincedence feature
285   */
286   static std::shared_ptr<GeomAPI_Pnt2d> getCoincedencePoint(FeaturePtr theStartCoin);
287
288   /// Sends redisplay event for all sub-features of the composite. Flush it.
289   static void sendSubFeaturesEvent(const CompositeFeaturePtr& theComposite,
290                                    const Events_ID theId);
291
292   /**
293    * Returns true if the object is a sketch entity, where auxiliary attribute has true value
294    * \param theObject a result or feature
295    * \return boolean result
296    */
297   static bool isAuxiliarySketchEntity(const ObjectPtr& theObject);
298
299   /**
300    * Returns true if the object is a sketch entity produced by projection or intersection feature
301    * and if it should not be included into the sketch result
302    */
303   static bool isIncludeIntoSketchResult(const ObjectPtr& theObject);
304
305   static ResultPtr createFixedByExternalCenter(const ObjectPtr& theObject,
306                                                const std::shared_ptr<GeomAPI_Edge>& theEdge,
307                                                ModelAPI_AttributeSelection::CenterType theType,
308                                                const CompositeFeaturePtr& theSketch,
309                                                bool theTemporary,
310                                                FeaturePtr& theCreatedFeature);
311
312
313   static void getFirstAndLastIndexInFolder(const ObjectPtr& theFolder,
314     int& theFirst, int& theLast);
315
316
317   /**
318   * Returns default color value for the given object
319   */
320   static void getDefaultColor(ObjectPtr theObject, const bool isEmptyColorValid,
321     std::vector<int>& theColor);
322
323   /**
324   * Returns default deflection value for the given object
325   */
326   static double getDefaultDeflection(const ObjectPtr& theObject);
327
328
329   /**
330   * Returns default transparency value
331   */
332   static double getDefaultTransparency();
333
334   /**
335   * Returns cursor according to (SKETCH_TAB_NAME, "operation_cursor") property value
336   */
337   static QCursor getOperationCursor();
338 };
339
340 #endif