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