Salome HOME
db43d5fa6ec903bf4cb8e8523075357faa573149
[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   typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
93                    std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
94                              std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
95
96   static void fillObjectShapes(const std::shared_ptr<ModelAPI_Object>& theObject,
97     const std::shared_ptr<ModelAPI_Object>& theSketch,
98     std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> >& theCashedShapes,
99     std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap>& theObjectToPoints);
100
101 private:
102   GeomShapePtr getSubShape(const std::string& theObjectAttributeId,
103                            const std::string& thePointAttributeId);
104
105   /// Returns geom point attribute of the feature bounds. It processes line or arc.
106   /// For circle feature, the result attributes are null
107   /// \param theFeature a source feature
108   /// \param theStartPointAttr an out attribute to start point
109   /// \param theStartPointAttr an out attribute to end point
110   void getFeaturePoints(const FeaturePtr& theFeature,
111                         std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
112                         std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr);
113
114   /// Obtains those constraints of the feature that should be modified. output maps contain
115   /// point of coincidence and attribute id to be modified after split
116   /// \param theFeaturesToDelete [out] constrains that will be deleted after split
117   void getConstraints(std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete);
118
119   /// Obtains references to feature point attributes and to feature,
120   /// e.g. for feature line: 1st container is
121   ///             <1st line point, list<entity_a in distance, entity_b in parallel> >
122   ///             <2nd line point, list<> >
123   ///      for feature circle 2nd container is <entity_a in Radius, entity_b in equal, ...>
124   /// \param theFeature an investigated feature
125   /// \param theRefs a container of list of referenced attributes
126   void getRefAttributes(const FeaturePtr& theFeature,
127                         std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
128                         std::list<AttributePtr>& theRefsToFeature);
129
130   /// Obtains coincident features to the given object. It is collected in a container
131   /// by the coincident attribute
132   /// \param theObject an investigated object
133   /// \param theCoincidencesToBaseFeature a container of list of referenced attributes
134   void getCoincidencesToObject(const std::shared_ptr<ModelAPI_Object>& theObject,
135                                std::map<AttributePtr, FeaturePtr>& theCoincidencesToBaseFeature);
136
137   /// Move constraints from attribute of base feature to attribute after modification
138   /// \param theBaseRefAttributes container of references to the attributes of base feature
139   /// \param theModifiedAttributes container of attributes placed instead of base attributes
140   /// at the same place
141   void updateRefAttConstraints(
142                const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
143                const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
144                std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete);
145
146   /// Make the base object is splitted by the point attributes
147   /// \param theSplitFeature a result split feature
148   /// \param theBeforeFeature a feature between start point and the 1st point of split feature
149   /// \param theAfterFeature a feature between last point of split feature and the end point
150   /// \param thePoints a list of points where coincidences will be build
151   /// \param theModifiedAttributes a container of attribute on base
152   /// feature to attribute on new feature
153   void trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
154                 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
155                 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
156                 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
157
158   /// Make the base object is splitted by the point attributes
159   /// \param theSplitFeature a result split feature
160   /// \param theBeforeFeature a feature between start point and the 1st point of split feature
161   /// \param theAfterFeature a feature between last point of split feature and the end point
162   /// \param thePoints a list of points where coincidences will be build
163   void trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
164                const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
165                std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
166                std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
167
168   /// Make the base object is splitted by the point attributes
169   /// \param theSplitFeature a result split feature
170   /// \param theBeforeFeature a feature between start point and the 1st point of split feature
171   /// \param theAfterFeature a feature between last point of split feature and the end point
172   /// \param thePoints a list of points where coincidences will be build
173   FeaturePtr trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
174                   const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
175                   std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
176                   std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
177
178   /// Correct the first and the second point to provide condition that the first is closer to
179   /// the start point and the second point - to the last end of current segment. To rearrange
180   /// them if this condition is not satisfied.
181   /// \param theStartPointAttr a start point of a segment
182   /// \param theEndPointAttr an end point of a segment
183   /// \param theFirstPoint a start point of a segment
184   /// \param theSecondPoint an end point of a segment
185   void arrangePointsOnLine(const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
186                            const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
187                            std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
188                            std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
189
190   /// Correct the first and the second point to provide condition that the first is closer to
191   /// the start point and the second point - to the last end of current segment. To rearrange
192   /// them if this condition is not satisfied.
193   /// \param theArc an arc to be split
194   /// \param theStartPointAttr a start point of a segment
195   /// \param theEndPointAttr an end point of a segment
196   /// \param theFirstPoint a start point of a segment
197   /// \param theSecondPoint an end point of a segment
198   void arrangePointsOnArc(const FeaturePtr& theArc,
199                           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   /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
205   /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
206   /// \param theSourceAttribute an attribute of GeomDataAPI_Point2D to obtain data
207   void fillAttribute(const AttributePtr& theModifiedAttribute,
208                      const AttributePtr& theSourceAttribute);
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 thePoint a point value
213   void fillPointAttribute(const AttributePtr& theModifiedAttribute,
214                           const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
215
216   /// Creates a line feature filled by center of base feature and given points
217   /// \param theBaseFeature another arc feature
218   /// \param theFirstAttribute an attribute with coordinates for the start point
219   /// \param theSecondAttribute an attribute with coordinates for the end point
220   FeaturePtr createLineFeature(const FeaturePtr& theBaseFeature,
221                                const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
222                                const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
223
224   /// Creates an arc feature filled by center of base feature and given points
225   /// \param theBaseFeature another arc feature
226   /// \param theFirstAttribute an attribute with coordinates for the start point
227   /// \param theSecondAttribute an attribute with coordinates for the end point
228   FeaturePtr createArcFeature(const FeaturePtr& theBaseFeature,
229                               const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
230                               const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
231
232   /// Add feature coincidence constraint between given attributes
233   /// \param theConstraintId a constraint index
234   /// \param theFirstAttribute an attribute of further coincidence
235   /// \param theSecondAttribute an attribute of further coincidence
236   std::shared_ptr<ModelAPI_Feature> createConstraint(const std::string& theConstraintId,
237                         const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
238                         const std::shared_ptr<ModelAPI_Attribute>& theSecondAttribute);
239
240   /// Add feature coincidence constraint between given attributes
241   /// \param theConstraintId a constraint index
242   /// \param theFirstAttribute an attribute of further coincidence
243   /// \param theSecondObject an object of further coincidence
244   std::shared_ptr<ModelAPI_Feature> createConstraintToObject(const std::string& theConstraintId,
245                         const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
246                         const std::shared_ptr<ModelAPI_Object>& theSecondObject);
247
248   /// Add feature coincidence constraint between given attributes
249   /// \param theConstraintId a constraint index
250   /// \param theFirstAttribute an attribute of further coincidence
251   /// \param theFirstAttribute an attribute of further coincidence
252   std::shared_ptr<ModelAPI_Feature> createConstraintForObjects(const std::string& theConstraintId,
253                         const std::shared_ptr<ModelAPI_Object>& theFirstObject,
254                         const std::shared_ptr<ModelAPI_Object>& theSecondObject);
255
256   /// Result result of the feature to build constraint with. For arc, circle it is an edge result.
257   /// \param theFeature a feature
258   /// \return result object
259   std::shared_ptr<ModelAPI_Result> getFeatureResult(
260                                     const std::shared_ptr<ModelAPI_Feature>& theFeature);
261
262 private:
263   void findShapePoints(const std::string& theObjectAttributeId,
264                        const std::string& thePointAttributeId,
265                        std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
266                        std::shared_ptr<GeomAPI_Pnt>& aLastPoint);
267
268   std::shared_ptr<GeomAPI_Pnt2d> convertPoint(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
269
270 private:
271   std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> > myCashedShapes;
272   std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap> myObjectToPoints;
273 };
274
275 #endif