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