Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Trim.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_Trim.h
4 // Created: 22 Feb 2017
5 // Author:  Natalia ERMOLAEVA
6
7 #ifndef SketchPlugin_Trim_H_
8 #define SketchPlugin_Trim_H_
9
10 #include <ModelAPI_IReentrant.h>
11
12 #include "SketchPlugin.h"
13 #include "SketchPlugin_ConstraintBase.h"
14 #include <SketchPlugin_Sketch.h>
15
16 class GeomDataAPI_Point2D;
17 class ModelAPI_Feature;
18 class ModelAPI_Result;
19 class ModelAPI_Object;
20
21 typedef std::pair<std::string, std::shared_ptr<GeomDataAPI_Point2D> > IdToPointPair;
22
23 /** \class SketchPlugin_Trim
24  *  \ingroup Plugins
25  *  \brief Feature for creation of a new constraint trimming object. Entities for split:
26  */
27 class SketchPlugin_Trim : public SketchPlugin_Feature, public GeomAPI_IPresentable,
28                           public ModelAPI_IReentrant
29 {
30  public:
31   /// Split constraint kind
32   inline static const std::string& ID()
33   {
34     static const std::string MY_TRIM_ID("SketchTrim");
35     return MY_TRIM_ID;
36   }
37   /// \brief Returns the kind of a feature
38   SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
39   {
40     static std::string MY_KIND = SketchPlugin_Trim::ID();
41     return MY_KIND;
42   }
43
44   /// The value parameter for the constraint
45   inline static const std::string& SELECTED_OBJECT()
46   {
47     static const std::string MY_SELECTED_OBJECT("SelectedObject");
48     return MY_SELECTED_OBJECT;
49   }
50
51   /// Start 2D point of the split segment
52   inline static const std::string& SELECTED_POINT()
53   {
54     static const std::string MY_SELECTED_POINT("SelectedPoint");
55     return MY_SELECTED_POINT;
56   }
57
58   /// The value parameter for the preview object
59   inline static const std::string& PREVIEW_OBJECT()
60   {
61     static const std::string MY_PREVIEW_OBJECT("PreviewObject");
62     return MY_PREVIEW_OBJECT;
63   }
64
65   /// Start 2D point of the split segment
66   inline static const std::string& PREVIEW_POINT()
67   {
68     static const std::string MY_PREVIEW_POINT("PreviewPoint");
69     return MY_PREVIEW_POINT;
70   }
71
72   /// \brief Creates a new part document if needed
73   SKETCHPLUGIN_EXPORT virtual void execute();
74
75   /// \brief Request for initialization of data model of the feature: adding all attributes
76   SKETCHPLUGIN_EXPORT virtual void initAttributes();
77
78   /// Reimplemented from ModelAPI_Feature::isMacro()
79   /// \returns true
80   SKETCHPLUGIN_EXPORT virtual bool isMacro() const;
81
82   /// Reimplemented from ModelAPI_Feature::isPreviewNeeded(). Returns false.
83   /// This is necessary to perform execute only by apply the feature
84   SKETCHPLUGIN_EXPORT virtual bool isPreviewNeeded() const { return false; }
85
86   /// \brief Use plugin manager for features creation
87   SketchPlugin_Trim();
88
89   /// Returns the AIS preview
90   SKETCHPLUGIN_EXPORT virtual AISObjectPtr getAISObject(AISObjectPtr thePrevious);
91
92   /// Moves the feature : Empty
93   SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY) {};
94
95   /// Apply information of the message to current object. It fills selected point and object
96   virtual std::string processEvent(const std::shared_ptr<Events_Message>& theMessage);
97
98   typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
99                    std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
100                              std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
101
102   static void fillObjectShapes(const std::shared_ptr<ModelAPI_Object>& theObject,
103     const std::shared_ptr<ModelAPI_Object>& theSketch,
104     std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> >& theCashedShapes,
105     std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap>& theObjectToPoints);
106
107 private:
108   bool setCoincidenceToAttribute(const AttributePtr& theAttribute,
109             const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences);
110
111   bool replaceCoincidenceAttribute(const AttributePtr& theCoincidenceAttribute,
112             const std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
113
114   GeomShapePtr getSubShape(const std::string& theObjectAttributeId,
115                            const std::string& thePointAttributeId);
116
117   /// Returns geom point attribute of the feature bounds. It processes line or arc.
118   /// For circle feature, the result attributes are null
119   /// \param theFeature a source feature
120   /// \param theStartPointAttr an out attribute to start point
121   /// \param theStartPointAttr an out attribute to end point
122   void getFeaturePoints(const FeaturePtr& theFeature,
123                         std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
124                         std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr);
125
126   /// Obtains those constraints of the feature that should be modified. output maps contain
127   /// point of coincidence and attribute id to be modified after split
128   /// \param theFeaturesToDelete [out] constrains that will be deleted after split
129   void getConstraints(std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete);
130
131   /// Obtains references to feature point attributes and to feature,
132   /// e.g. for feature line: 1st container is
133   ///             <1st line point, list<entity_a in distance, entity_b in parallel> >
134   ///             <2nd line point, list<> >
135   ///      for feature circle 2nd container is <entity_a in Radius, entity_b in equal, ...>
136   /// \param theFeature an investigated feature
137   /// \param theRefs a container of list of referenced attributes
138   void getRefAttributes(const FeaturePtr& theFeature,
139                         std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
140                         std::list<AttributePtr>& theRefsToFeature);
141
142   /// Obtains coincident features to the given object. It is collected in a container
143   /// by the coincident attribute
144   /// \param theObject an investigated object
145   /// \param theCoincidencesToBaseFeature a container of list of referenced attributes
146   //void getCoincidencesToObject(const std::shared_ptr<ModelAPI_Object>& theObject,
147   //                             std::map<AttributePtr, FeaturePtr>& theCoincidencesToBaseFeature);
148
149   /// Move constraints from attribute of base feature to attribute after modification
150   /// \param theBaseRefAttributes container of references to the attributes of base feature
151   /// \param theModifiedAttributes container of attributes placed instead of base attributes
152   /// at the same place
153   void updateRefAttConstraints(
154                const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
155                const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
156                std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete);
157
158   /// Remove references constraints from attribute of base feature refer to the given attribute
159   /// \param theAttribute an attribute
160   /// \param theModifiedAttributes modifiable container of attributes
161   void removeReferencesToAttribute(const AttributePtr& theAttribute,
162                   std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes);
163
164   /// Make the base object is splitted by the point attributes
165   /// \param theBaseRefAttributes container of references to the attributes of base feature
166   /// \param thePoints a list of points where coincidences will be build
167   /// \param theModifiedAttributes a container of attribute on base
168   /// feature to attribute on new feature
169   /// \return new line if it was created
170   FeaturePtr trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
171                 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
172                 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
173                 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
174                 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
175
176   /// Make the base object is splitted by the point attributes
177   /// \param thePoints a list of points where coincidences will be build
178   /// \return new line if it was created
179   FeaturePtr trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
180                const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
181                std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
182                std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
183                std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
184
185   /// Make the base object is splitted by the point attributes
186   /// \param thePoints a list of points where coincidences will be build
187   FeaturePtr trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
188                   const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
189                   std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
190                   std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
191
192   /// Correct the first and the second point to provide condition that the first is closer to
193   /// the start point and the second point - to the last end of current segment. To rearrange
194   /// them if this condition is not satisfied.
195   /// \param theStartPointAttr a start point of a segment
196   /// \param theEndPointAttr an end point of a segment
197   /// \param theFirstPoint a start point of a segment
198   /// \param theSecondPoint an end point of a segment
199   void arrangePointsOnLine(const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
200                            const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
201                            std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
202                            std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
203
204   /// Correct the first and the second point to provide condition that the first is closer to
205   /// the start point and the second point - to the last end of current segment. To rearrange
206   /// them if this condition is not satisfied.
207   /// \param theArc an arc to be split
208   /// \param theStartPointAttr a start point of a segment
209   /// \param theEndPointAttr an end point of a segment
210   /// \param theFirstPoint a start point of a segment
211   /// \param theSecondPoint an end point of a segment
212   void arrangePointsOnArc(const FeaturePtr& theArc,
213                           const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
214                           const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
215                           std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
216                           std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
217
218   /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
219   /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
220   /// \param theSourceAttribute an attribute of GeomDataAPI_Point2D to obtain data
221   void fillAttribute(const AttributePtr& theModifiedAttribute,
222                      const AttributePtr& theSourceAttribute);
223
224   /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
225   /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
226   /// \param thePoint a point value
227   void fillPointAttribute(const AttributePtr& theModifiedAttribute,
228                           const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
229
230   /// Creates a line feature filled by center of base feature and given points
231   /// \param theBaseFeature another arc feature
232   /// \param theFirstAttribute an attribute with coordinates for the start point
233   /// \param theSecondAttribute an attribute with coordinates for the end point
234   FeaturePtr createLineFeature(const FeaturePtr& theBaseFeature,
235                                const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
236                                const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
237
238   /// Creates an arc feature filled by center of base feature and given points
239   /// \param theBaseFeature another arc feature
240   /// \param theFirstAttribute an attribute with coordinates for the start point
241   /// \param theSecondAttribute an attribute with coordinates for the end point
242   FeaturePtr createArcFeature(const FeaturePtr& theBaseFeature,
243                               const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
244                               const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
245
246   /// Add feature coincidence constraint between given attributes
247   /// \param theConstraintId a constraint index
248   /// \param theFirstAttribute an attribute of further coincidence
249   /// \param theSecondAttribute an attribute of further coincidence
250   std::shared_ptr<ModelAPI_Feature> createConstraint(const std::string& theConstraintId,
251                         const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
252                         const std::shared_ptr<ModelAPI_Attribute>& theSecondAttribute);
253
254   /// Add feature coincidence constraint between given attributes
255   /// \param theConstraintId a constraint index
256   /// \param theFirstAttribute an attribute of further coincidence
257   /// \param theSecondObject an object of further coincidence
258   std::shared_ptr<ModelAPI_Feature> createConstraintToObject(const std::string& theConstraintId,
259                         const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
260                         const std::shared_ptr<ModelAPI_Object>& theSecondObject);
261
262   /// Add feature coincidence constraint between given attributes
263   /// \param theConstraintId a constraint index
264   /// \param theFirstAttribute an attribute of further coincidence
265   /// \param theFirstAttribute an attribute of further coincidence
266   std::shared_ptr<ModelAPI_Feature> createConstraintForObjects(const std::string& theConstraintId,
267                         const std::shared_ptr<ModelAPI_Object>& theFirstObject,
268                         const std::shared_ptr<ModelAPI_Object>& theSecondObject);
269
270   /// Result result of the feature to build constraint with. For arc, circle it is an edge result.
271   /// \param theFeature a feature
272   /// \return result object
273   std::shared_ptr<ModelAPI_Result> getFeatureResult(
274                                     const std::shared_ptr<ModelAPI_Feature>& theFeature);
275
276 private:
277   void findShapePoints(const std::string& theObjectAttributeId,
278                        const std::string& thePointAttributeId,
279                        std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
280                        std::shared_ptr<GeomAPI_Pnt>& aLastPoint);
281
282   std::shared_ptr<GeomAPI_Pnt2d> convertPoint(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
283
284 private:
285   std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> > myCashedShapes;
286   std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap> myObjectToPoints;
287 };
288
289 #endif