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