]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Trim.h
Salome HOME
Issue #2109 trim - wrong result due to constarints
[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   /// \param theFeaturesToUpdate [out] constrains that will be updated after split
130   void getConstraints(std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete,
131                       std::set<FeaturePtr>& theFeaturesToUpdate);
132
133   /// Obtains references to feature point attributes and to feature,
134   /// e.g. for feature line: 1st container is
135   ///             <1st line point, list<entity_a in distance, entity_b in parallel> >
136   ///             <2nd line point, list<> >
137   ///      for feature circle 2nd container is <entity_a in Radius, entity_b in equal, ...>
138   /// \param theFeature an investigated feature
139   /// \param theRefs a container of list of referenced attributes
140   void getRefAttributes(const FeaturePtr& theFeature,
141                         std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
142                         std::list<AttributePtr>& theRefsToFeature);
143
144   /// Obtains coincident features to the given object. It is collected in a container
145   /// by the coincident attribute
146   /// \param theObject an investigated object
147   /// \param theCoincidencesToBaseFeature a container of list of referenced attributes
148   //void getCoincidencesToObject(const std::shared_ptr<ModelAPI_Object>& theObject,
149   //                             std::map<AttributePtr, FeaturePtr>& theCoincidencesToBaseFeature);
150
151   /// Move constraints from attribute of base feature to attribute after modification
152   /// \param theBaseRefAttributes container of references to the attributes of base feature
153   /// \param theModifiedAttributes container of attributes placed instead of base attributes
154   /// at the same place
155   void updateRefAttConstraints(
156                const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
157                const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
158                std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete);
159
160   /// Remove references constraints from attribute of base feature refer to the given attribute
161   /// \param theAttribute an attribute
162   /// \param theModifiedAttributes modifiable container of attributes
163   void removeReferencesToAttribute(const AttributePtr& theAttribute,
164                   std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes);
165
166    /// Updates line length if it exist in the list
167   /// \param theFeaturesToUpdate a constraints container
168   void updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate);
169
170   /// Make the base object is splitted by the point attributes
171   /// \param theBaseRefAttributes container of references to the attributes of base feature
172   /// \param thePoints a list of points where coincidences will be build
173   /// \param theModifiedAttributes a container of attribute on base
174   /// feature to attribute on new feature
175   /// \return new line if it was created
176   FeaturePtr trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
177                 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
178                 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
179                 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
180                 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
181
182   /// Make the base object is splitted by the point attributes
183   /// \param thePoints a list of points where coincidences will be build
184   /// \return new line if it was created
185   FeaturePtr trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
186                const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
187                std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
188                std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
189                std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
190
191   /// Make the base object is splitted by the point attributes
192   /// \param thePoints a list of points where coincidences will be build
193   FeaturePtr trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
194                   const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
195                   std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
196                   std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
197
198   /// Correct the first and the second point to provide condition that the first is closer to
199   /// the start point and the second point - to the last end of current segment. To rearrange
200   /// them if this condition is not satisfied.
201   /// \param theStartPointAttr a start point of a segment
202   /// \param theEndPointAttr an end point of a segment
203   /// \param theFirstPoint a start point of a segment
204   /// \param theSecondPoint an end point of a segment
205   void arrangePointsOnLine(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   /// Correct the first and the second point to provide condition that the first is closer to
211   /// the start point and the second point - to the last end of current segment. To rearrange
212   /// them if this condition is not satisfied.
213   /// \param theArc an arc to be split
214   /// \param theStartPointAttr a start point of a segment
215   /// \param theEndPointAttr an end point of a segment
216   /// \param theFirstPoint a start point of a segment
217   /// \param theSecondPoint an end point of a segment
218   void arrangePointsOnArc(const FeaturePtr& theArc,
219                           const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
220                           const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
221                           std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
222                           std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
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 theSourceAttribute an attribute of GeomDataAPI_Point2D to obtain data
227   void fillAttribute(const AttributePtr& theModifiedAttribute,
228                      const AttributePtr& theSourceAttribute);
229
230   /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
231   /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
232   /// \param thePoint a point value
233   void fillPointAttribute(const AttributePtr& theModifiedAttribute,
234                           const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
235
236   /// Creates a line feature filled by center of base feature and given points
237   /// \param theBaseFeature another arc feature
238   /// \param theFirstAttribute an attribute with coordinates for the start point
239   /// \param theSecondAttribute an attribute with coordinates for the end point
240   FeaturePtr createLineFeature(const FeaturePtr& theBaseFeature,
241                                const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
242                                const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
243
244   /// Creates an arc feature filled by center of base feature and given points
245   /// \param theBaseFeature another arc feature
246   /// \param theFirstAttribute an attribute with coordinates for the start point
247   /// \param theSecondAttribute an attribute with coordinates for the end point
248   FeaturePtr createArcFeature(const FeaturePtr& theBaseFeature,
249                               const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
250                               const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
251
252   /// Add feature coincidence constraint between given attributes
253   /// \param theConstraintId a constraint index
254   /// \param theFirstAttribute an attribute of further coincidence
255   /// \param theSecondAttribute an attribute of further coincidence
256   std::shared_ptr<ModelAPI_Feature> createConstraint(const std::string& theConstraintId,
257                         const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
258                         const std::shared_ptr<ModelAPI_Attribute>& theSecondAttribute);
259
260   /// Add feature coincidence constraint between given attributes
261   /// \param theConstraintId a constraint index
262   /// \param theFirstAttribute an attribute of further coincidence
263   /// \param theSecondObject an object of further coincidence
264   std::shared_ptr<ModelAPI_Feature> createConstraintToObject(const std::string& theConstraintId,
265                         const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
266                         const std::shared_ptr<ModelAPI_Object>& theSecondObject);
267
268   /// Add feature coincidence constraint between given attributes
269   /// \param theConstraintId a constraint index
270   /// \param theFirstAttribute an attribute of further coincidence
271   /// \param theFirstAttribute an attribute of further coincidence
272   std::shared_ptr<ModelAPI_Feature> createConstraintForObjects(const std::string& theConstraintId,
273                         const std::shared_ptr<ModelAPI_Object>& theFirstObject,
274                         const std::shared_ptr<ModelAPI_Object>& theSecondObject);
275
276   /// Result result of the feature to build constraint with. For arc, circle it is an edge result.
277   /// \param theFeature a feature
278   /// \return result object
279   std::shared_ptr<ModelAPI_Result> getFeatureResult(
280                                     const std::shared_ptr<ModelAPI_Feature>& theFeature);
281
282 private:
283   void findShapePoints(const std::string& theObjectAttributeId,
284                        const std::string& thePointAttributeId,
285                        std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
286                        std::shared_ptr<GeomAPI_Pnt>& aLastPoint);
287
288   std::shared_ptr<GeomAPI_Pnt2d> convertPoint(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
289
290 private:
291   std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> > myCashedShapes;
292   std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap> myObjectToPoints;
293 };
294
295 #endif