Salome HOME
3e89ceee2fa5d2f315fa0435b15f4196c130fe8c
[modules/shaper.git] / src / PartSet / PartSet_Tools.h
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 email : webmaster.salome@opencascade.com<mailto: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
30 #include <ModelAPI_CompositeFeature.h>
31 #include <ModelAPI_Object.h>
32 #include <ModelAPI_Attribute.h>
33
34 #include <Events_Message.h>
35
36 #include <TopoDS_Shape.hxx>
37
38 #include <memory>
39
40 class V3d_View;
41 class ModuleBase_IViewWindow;
42 class ModuleBase_ViewerPrs;
43 class ModuleBase_IWorkshop;
44 class GeomDataAPI_Point2D;
45 class GeomAPI_Shape;
46 class GeomAPI_Pln;
47 class GeomAPI_Pnt2d;
48 class GeomAPI_Pnt;
49 class GeomAPI_Edge;
50 class GeomAPI_Vertex;
51 class ModelAPI_Result;
52
53 class QMouseEvent;
54
55 /*!
56  * \class PartSet_Tools
57  * \ingroup Modules
58  * \brief The operation for the sketch feature creation
59  */
60 class PARTSET_EXPORT PartSet_Tools
61 {
62 public:
63   /// \enum ConstraintVisibleState types of constraints which visible state can be
64   /// changed in the sketch widget
65   enum ConstraintVisibleState
66   {
67     Geometrical = 0, // all constrains excepting dimensional
68     Dimensional,     // lenght, distance, radius and angle constraints
69     Expressions,     // parameter text should be shown in dimensional constraint
70     Any              // both, geometrical and dimensional, types of constraints
71   };
72
73  public:
74   /// Returns default value of AIS presentation
75   /// \return integer value
76   static int getAISDefaultWidth();
77
78   /// Converts the 2D screen point to the 3D point on the view according to the point of view
79   /// \param thePoint a screen point
80   /// \param theView a 3D view
81   static gp_Pnt convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView);
82
83   /// \brief Converts the 3D point to the projected coodinates on the sketch plane.
84   /// \param thePoint the 3D point in the viewer
85   /// \param theSketch the sketch feature
86   /// \param theView a view 3d object
87   /// \param theX the X coordinate
88   /// \param theY the Y coordinate
89   static void convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
90                           Handle(V3d_View) theView,
91                           double& theX, double& theY);
92
93   /// \brief Converts the 3D point to the projected coodinates on the sketch plane.
94   /// \param theSketch the sketch feature
95   /// \param thePnt the 3D point in the viewer
96   /// \returns the converted point object
97   static std::shared_ptr<GeomAPI_Pnt2d> convertTo2D(FeaturePtr theSketch,
98                                                     const std::shared_ptr<GeomAPI_Pnt>& thePnt);
99
100   /// \brief Converts the 2D projected coodinates on the sketch plane to the 3D point.
101   /// \param theX the X coordinate
102   /// \param theY the Y coordinate
103   /// \param theSketch the sketch feature
104   static std::shared_ptr<GeomAPI_Pnt> convertTo3D(const double theX, const double theY,
105                                                   FeaturePtr theSketch);
106
107   /// Returns pointer to the root document.
108   static std::shared_ptr<ModelAPI_Document> document();
109
110   /// \brief Save the double to the feature. If the attribute is double, it is filled.
111   /// \param theFeature the feature
112   /// \param theX the horizontal coordinate
113   /// \param theAttribute the feature attribute
114   static void setFeatureValue(FeaturePtr theFeature, double theX, const std::string& theAttribute);
115
116   /// \brief Returns the feature double value if it is.
117   /// \param theFeature the feature
118   /// \param theAttribute the feature attribute
119   /// \param isValid an output parameter whether the value is valid
120   /// \returns the feature value
121   static double featureValue(FeaturePtr theFeature, const std::string& theAttribute, bool& isValid);
122
123   /// Find a feature in the attribute of the given feature. If the kind is not empty,
124   /// the return feature should be this type. Otherwise it is null
125   /// \param theFeature a source feature
126   /// \param theAttribute a name of the requried attribute attribute
127   /// \param theKind an output feature kind
128   /// \return the feature
129   static FeaturePtr feature(FeaturePtr theFeature, const std::string& theAttribute,
130                             const std::string& theKind);
131
132   /// Creates a constraint on two points
133   /// \param theSketch a sketch feature
134   /// \param thePoint1 the first point
135   /// \param thePoint2 the second point
136   static void createConstraint(CompositeFeaturePtr theSketch,
137                                std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
138                                std::shared_ptr<GeomDataAPI_Point2D> thePoint2);
139
140   /// Create a sketch plane instance
141   /// \param theSketch a sketch feature
142   /// \return API object of geom plane
143   static std::shared_ptr<GeomAPI_Pln> sketchPlane(CompositeFeaturePtr theSketch);
144
145   /// Create a point 3D on a basis of point 2D and sketch feature
146   /// \param thePoint2D a point on a sketch
147   /// \param theSketch a sketch feature
148   /// \return API object of point 3D
149   static std::shared_ptr<GeomAPI_Pnt> point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
150                                                 CompositeFeaturePtr theSketch);
151
152   /// Finds a line (arc or circle) by given edge
153   /// \param theShape an edge
154   /// \param theObject a selected result object
155   /// \param theSketch a sketch feature
156   /// \return result of found feature or NULL
157   static std::shared_ptr<ModelAPI_Result> findFixedObjectByExternal(
158                                              const TopoDS_Shape& theShape,
159                                              const ObjectPtr& theObject,
160                                              CompositeFeaturePtr theSketch);
161
162   /// Creates a line (arc or circle) by given edge
163   /// Created line will have fixed constraint
164   /// \param theShape an edge
165   /// \param theObject a selected result object
166   /// \param theSketch a sketch feature
167   /// \param theTemporary the created external object is temporary, execute is not performed for it
168   /// \return result of created feature
169   static std::shared_ptr<ModelAPI_Result> createFixedObjectByExternal(
170                                                const TopoDS_Shape& theShape,
171                                                const ObjectPtr& theObject,
172                                                CompositeFeaturePtr theSketch,
173                                                const bool theTemporary = false);
174
175     /// Checks whether the list of selected presentations contains the given one
176   /// \param theSelected a list of presentations
177   /// \param thePrs a presentation to be found
178   /// \return - result of check, true if the list contains the prs
179   static bool isContainPresentation(const QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theSelected,
180                                     const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs);
181
182   /// Returns Result object if the given skietch contains external edge equal to the given
183   /// \param theSketch - the sketch feature
184   /// \param theEdge - the edge
185   /// \return result object with external edge if it is found
186   static std::shared_ptr<ModelAPI_Result> findExternalEdge(CompositeFeaturePtr theSketch,
187                                                            std::shared_ptr<GeomAPI_Edge> theEdge);
188
189   /// Returns Result object if the given sketch contains external vertex equal to the given
190   /// \param theSketch - the sketch feature
191   /// \param theVert - the vertex
192   /// \return result object with external vertex if it is found
193   static std::shared_ptr<ModelAPI_Result> findExternalVertex(CompositeFeaturePtr theSketch,
194                                                         std::shared_ptr<GeomAPI_Vertex> theVert);
195
196   /// Returns whether the selected presentation has a shape with the vertex type
197   /// \param thePrs a selected presentation
198   /// \param theSketch the sketch feature
199   /// \param theView a 3D view
200   /// \param theX the output horizontal coordinate of the point
201   /// \param theY the output vertical coordinate of the point
202   static bool hasVertexShape(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs,
203                              FeaturePtr theSketch,
204                              Handle(V3d_View) theView, double& theX, double& theY);
205
206
207   /**
208   * Find attribute of object which corresponds to the given shape
209   * \param theObj - an object
210   * \param theShape - a Shape
211   * \param theSketch - a Sketch to get a plane of converting to 2d
212   */
213   static AttributePtr findAttributeBy2dPoint(ObjectPtr theObj, const TopoDS_Shape theShape,
214                                              FeaturePtr theSketch);
215
216   /**
217   * Finds an attribute value in attribute reference attribute value
218   * \param theAttribute - an attribure reference filled with an attribute
219   * \param theWorkshop a reference to workshop
220   * \return a geometry shape
221   */
222   static std::shared_ptr<GeomAPI_Shape> findShapeBy2DPoint(const AttributePtr& theAttribute,
223                                          ModuleBase_IWorkshop* theWorkshop);
224
225   /**
226   * Returns point of coincidence feature
227   * \param theFeature the coincidence feature
228   * \param theAttribute the attribute name
229   */
230   static std::shared_ptr<GeomAPI_Pnt2d> getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
231                                                  const std::string& theAttribute);
232
233   /**
234   * Convertes parameters into a geom point
235   * \theEvent a Qt event to find mouse position
236   * \param theWindow view window to define eye of view
237   * \param theSketch to convert 3D point coordinates into coorditates of the sketch plane
238   */
239   static std::shared_ptr<GeomAPI_Pnt2d> getPnt2d(QMouseEvent* theEvent,
240                                                  ModuleBase_IViewWindow* theWindow,
241                                                  const FeaturePtr& theSketch);
242
243   /**
244   * Gets all references to the feature, take coincidence constraint features, get point 2d attributes
245   * and compare the point value to be equal with the given. Returns the first feature, which has
246   * equal points.
247   * \param theFeature the coincidence feature
248   * \param thePoint a 2d point
249   * \return the coincidence feature or null
250   */
251   static FeaturePtr findFirstCoincidence(const FeaturePtr& theFeature,
252                                          std::shared_ptr<GeomAPI_Pnt2d> thePoint);
253
254   /**
255   * Returns list of features connected in a councedence feature point
256   * \param theStartCoin the coincidence feature
257   * \param theList a list which collects lines features
258   * \param theCoincidencies a list of coincidence features
259   * \param theAttr the attribute name
260   */
261   static void findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
262                                QList<FeaturePtr>& theCoincidencies,
263                                std::string theAttr, QList<bool>& theIsAttributes);
264
265   /**
266   * Returns point of a coincedence
267   * \param theStartCoin the coincedence feature
268   */
269   static std::shared_ptr<GeomAPI_Pnt2d> getCoincedencePoint(FeaturePtr theStartCoin);
270
271   /// Sends redisplay event for all sub-features of the composite. Flush it.
272   static void sendSubFeaturesEvent(const CompositeFeaturePtr& theComposite,
273                                    const Events_ID theId);
274
275   /**
276    * Returns true if the object is a sketch entity, where auxiliary attribute has true vlaue
277    * \param theObject a result or feature
278    * \return boolean result
279    */
280   static bool isAuxiliarySketchEntity(const ObjectPtr& theObject);
281 };
282
283 #endif