Salome HOME
316d484a4e16730680d97453cd37a8b7a7f294f1
[modules/shaper.git] / src / PartSet / PartSet_Tools.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_Tools.h
4 // Created:     28 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #ifndef PartSet_Tools_H
8 #define PartSet_Tools_H
9
10 #include "PartSet.h"
11
12 #include <ModuleBase_ViewerPrs.h>
13 #include <gp_Pnt.hxx>
14
15 #include <QPoint>
16 #include <QList>
17
18 #include <ModelAPI_CompositeFeature.h>
19 #include <ModelAPI_Object.h>
20 #include <ModelAPI_Attribute.h>
21
22 #include <TopoDS_Shape.hxx>
23
24 #include <memory>
25
26 class Handle_V3d_View;
27 class ModuleBase_ViewerPrs;
28 class ModuleBase_IWorkshop;
29 class GeomDataAPI_Point2D;
30 class GeomAPI_Pln;
31 class GeomAPI_Pnt2d;
32 class GeomAPI_Pnt;
33 class GeomAPI_Edge;
34 class GeomAPI_Vertex;
35
36 /*!
37  * \class PartSet_Tools
38  * \ingroup Modules
39  * \brief The operation for the sketch feature creation
40  */
41 class PARTSET_EXPORT PartSet_Tools
42 {
43 public:
44   /// \enum ConstraintVisibleState types of constraints which visible state can be
45   /// changed in the sketch widget
46   enum ConstraintVisibleState
47   {
48     Geometrical = 0, // all constrains excepting dimensional
49     Dimensional,     // lenght, distance, radius and angle constraints
50     Any              // both, geometrical and dimensional, types of constraints
51   };
52
53  public:
54   /// Converts the 2D screen point to the 3D point on the view according to the point of view
55   /// \param thePoint a screen point
56   /// \param theView a 3D view
57   static gp_Pnt convertClickToPoint(QPoint thePoint, Handle_V3d_View theView);
58
59   /// \brief Converts the 3D point to the projected coodinates on the sketch plane.
60   /// \param thePoint the 3D point in the viewer
61   /// \param theSketch the sketch feature
62   /// \param theView a view 3d object
63   /// \param theX the X coordinate
64   /// \param theY the Y coordinate
65   static void convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
66                           Handle(V3d_View) theView,
67                           double& theX, double& theY);
68
69   /// \brief Converts the 3D point to the projected coodinates on the sketch plane.
70   /// \param theSketch the sketch feature
71   /// \param thePnt the 3D point in the viewer
72   /// \returns the converted point object
73   static std::shared_ptr<GeomAPI_Pnt2d> convertTo2D(FeaturePtr theSketch, const std::shared_ptr<GeomAPI_Pnt>& thePnt);
74
75   /// \brief Converts the 2D projected coodinates on the sketch plane to the 3D point.
76   /// \param theX the X coordinate
77   /// \param theY the Y coordinate
78   /// \param theSketch the sketch feature
79   static std::shared_ptr<GeomAPI_Pnt> convertTo3D(const double theX, const double theY, FeaturePtr theSketch);
80
81   /// Returns pointer to the root document.
82   static std::shared_ptr<ModelAPI_Document> document();
83
84   /// \brief Save the double to the feature. If the attribute is double, it is filled.
85   /// \param theFeature the feature
86   /// \param theX the horizontal coordinate
87   /// \param theAttribute the feature attribute
88   static void setFeatureValue(FeaturePtr theFeature, double theX, const std::string& theAttribute);
89
90   /// \brief Returns the feature double value if it is.
91   /// \param theFeature the feature
92   /// \param theAttribute the feature attribute
93   /// \param isValid an output parameter whether the value is valid
94   /// \returns the feature value
95   static double featureValue(FeaturePtr theFeature, const std::string& theAttribute, bool& isValid);
96
97   /// Find a feature in the attribute of the given feature. If the kind is not empty,
98   /// the return feature should be this type. Otherwise it is null
99   /// \param theFeature a source feature
100   /// \param theAttribute a name of the requried attribute attribute
101   /// \param theKind an output feature kind
102   /// \return the feature
103   static FeaturePtr feature(FeaturePtr theFeature, const std::string& theAttribute,
104                             const std::string& theKind);
105
106   /// Creates a constraint on two points
107   /// \param theSketch a sketch feature
108   /// \param thePoint1 the first point
109   /// \param thePoint2 the second point
110   static void createConstraint(CompositeFeaturePtr theSketch,
111                                std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
112                                std::shared_ptr<GeomDataAPI_Point2D> thePoint2);
113
114   /// Finds in the feature's sub-features first Point2D attribute with the given point coordinates
115   /// \param theFeature a feature with sub-feature attributes
116   /// \param thePoint a point to provided searched coordinates
117   /// \return found point or null
118   static std::shared_ptr<GeomDataAPI_Point2D> findFirstEqualPointInArgumentFeatures(
119                   const FeaturePtr& theFeature, const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
120
121   /// Finds in the feature first Point2D attribute with the given point coordinates
122   /// \param theFeature a feature with point attributes
123   /// \param thePoint a point to provided searched coordinates
124   /// \return found point or null
125   static std::shared_ptr<GeomDataAPI_Point2D> findFirstEqualPoint(const FeaturePtr& theFeature,
126                                        const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
127
128   /// Creates constrains of the current 
129   /// \param theSketch a sketch feature
130   /// \param theFeature a source feature
131   /// \param theAttribute a name of the requried attribute attribute
132   /// \param theClickedX the horizontal coordnate of the point
133   /// \param theClickedY the vertical coordnate of the point
134   static void setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
135                              const std::string& theAttribute, double theClickedX,
136                              double theClickedY);
137
138   /// Create a sketch plane instance
139   /// \param theSketch a sketch feature
140   /// \return API object of geom plane
141   static std::shared_ptr<GeomAPI_Pln> sketchPlane(CompositeFeaturePtr theSketch);
142
143   /// Create a point 3D on a basis of point 2D and sketch feature
144   /// \param thePoint2D a point on a sketch
145   /// \param theSketch a sketch feature
146   /// \return API object of point 3D
147   static std::shared_ptr<GeomAPI_Pnt> point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
148                                                 CompositeFeaturePtr theSketch);
149
150   /// Finds a line (arc or circle) by given edge
151   /// \param theShape an edge
152   /// \param theObject a selected result object
153   /// \param theSketch a sketch feature
154   /// \return result of found feature or NULL
155   static ResultPtr findFixedObjectByExternal(const TopoDS_Shape& theShape, 
156                                              const ObjectPtr& theObject, 
157                                              CompositeFeaturePtr theSketch);
158
159   /// Creates a line (arc or circle) by given edge
160   /// Created line will have fixed constraint
161   /// \param theShape an edge
162   /// \param theObject a selected result object
163   /// \param theSketch a sketch feature
164   /// \param theTemporary the created external object is temporary, execute is not performed for it
165   /// \return result of created feature
166   static ResultPtr createFixedObjectByExternal(const TopoDS_Shape& theShape, 
167                                                const ObjectPtr& theObject, 
168                                                CompositeFeaturePtr theSketch,
169                                                const bool theTemporary = false);
170
171   /// Checks whether the list of selected presentations contains the given one
172   /// \param theSelected a list of presentations
173   /// \param thePrs a presentation to be found
174   /// \return - result of check, true if the list contains the prs
175   static bool isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
176                                     const ModuleBase_ViewerPrs& thePrs);
177
178   /// Returns Result object if the given skietch contains external edge equal to the given
179   /// \param theSketch - the sketch feature
180   /// \param theEdge - the edge
181   /// \return result object with external edge if it is found
182   static ResultPtr findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge);
183
184   /// Returns Result object if the given sketch contains external vertex equal to the given
185   /// \param theSketch - the sketch feature
186   /// \param theVert - the vertex
187   /// \return result object with external vertex if it is found
188   static ResultPtr findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert);
189
190   /// Returns whether the selected presentation has a shape with the vertex type
191   /// \param thePrs a selected presentation
192   /// \param theSketch the sketch feature
193   /// \param theView a 3D view
194   /// \param theX the output horizontal coordinate of the point
195   /// \param theY the output vertical coordinate of the point
196   static bool hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch,
197                              Handle_V3d_View theView, double& theX, double& theY);
198
199
200   /**
201   * Find attribute of object which corresponds to the given shape
202   * \param theObj - an object
203   * \param theShape - a Shape
204   * \param theSketch - a Sketch to get a plane of converting to 2d
205   */
206   static AttributePtr findAttributeBy2dPoint(ObjectPtr theObj, const TopoDS_Shape theShape,
207                                              FeaturePtr theSketch);
208
209   /**
210   * Finds an attribute value in attribute reference attribute value
211   * \param theAttribute - an attribure reference filled with an attribute
212   * \param theWorkshop a reference to workshop
213   * \return a geometry shape
214   */
215   static GeomShapePtr findShapeBy2DPoint(const AttributePtr& theAttribute,
216                                          ModuleBase_IWorkshop* theWorkshop);
217
218   /**
219   * Returns point of coincidence feature
220   * \param theFeature the coincidence feature
221   * \param theAttribute the attribute name
222   */
223   static std::shared_ptr<GeomAPI_Pnt2d> getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
224                                                  const std::string& theAttribute);
225
226   /**
227   * Gets all references to the feature, take coincidence constraint features, get point 2d attributes
228   * and compare the point value to be equal with the given. Returns the first feature, which has
229   * equal points.
230   * \param theSearchInResults a flag whether the conicidence feature shoudl be searched in
231   * references of the feature results.
232   * \return the coincidence feature or null
233   */
234   static FeaturePtr findFirstCoincidence(const FeaturePtr& theFeature,
235                                          std::shared_ptr<GeomAPI_Pnt2d> thePoint,
236                                          const bool theSearchInResults = false);
237
238   /**
239   * Returns list of features connected in a councedence feature point
240   * \param theStartCoin the coincidence feature
241   * \param theList a list which collects lines features
242   * \param theAttr the attribute name
243   */
244   static void findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
245                                std::string theAttr);
246
247   /**
248   * Returns point of a coincedence
249   * \param theStartCoin the coincedence feature
250   */
251   static std::shared_ptr<GeomAPI_Pnt2d> getCoincedencePoint(FeaturePtr theStartCoin);
252 };
253
254 #endif