Salome HOME
91b9405d81f1ad146f9ea8dd12722460999f2dae
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Trim.h
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef SketchPlugin_Trim_H_
21 #define SketchPlugin_Trim_H_
22
23 #include "SketchPlugin.h"
24 #include <SketchPlugin_Tools.h>
25
26 #include <GeomAPI_IPresentable.h>
27 #include <ModelAPI_IReentrant.h>
28 #include <SketchPlugin_Sketch.h>
29
30 class GeomDataAPI_Point2D;
31 class ModelAPI_Feature;
32 class ModelAPI_Result;
33 class ModelAPI_Object;
34
35 typedef std::pair<std::string, std::shared_ptr<GeomDataAPI_Point2D> > IdToPointPair;
36
37 /** \class SketchPlugin_Trim
38  *  \ingroup Plugins
39  *  \brief Feature for creation of a new constraint trimming object. Entities for split:
40  */
41 class SketchPlugin_Trim : public SketchPlugin_Feature, public GeomAPI_IPresentable,
42                           public ModelAPI_IReentrant
43 {
44  public:
45   /// Split constraint kind
46   inline static const std::string& ID()
47   {
48     static const std::string MY_TRIM_ID("SketchTrim");
49     return MY_TRIM_ID;
50   }
51   /// \brief Returns the kind of a feature
52   SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
53   {
54     static std::string MY_KIND = SketchPlugin_Trim::ID();
55     return MY_KIND;
56   }
57
58   /// The value parameter for the constraint
59   inline static const std::string& SELECTED_OBJECT()
60   {
61     static const std::string MY_SELECTED_OBJECT("SelectedObject");
62     return MY_SELECTED_OBJECT;
63   }
64
65   /// Start 2D point of the split segment
66   inline static const std::string& SELECTED_POINT()
67   {
68     static const std::string MY_SELECTED_POINT("SelectedPoint");
69     return MY_SELECTED_POINT;
70   }
71
72   /// The value parameter for the preview object
73   inline static const std::string& PREVIEW_OBJECT()
74   {
75     static const std::string MY_PREVIEW_OBJECT("PreviewObject");
76     return MY_PREVIEW_OBJECT;
77   }
78
79   /// Start 2D point of the split segment
80   inline static const std::string& PREVIEW_POINT()
81   {
82     static const std::string MY_PREVIEW_POINT("PreviewPoint");
83     return MY_PREVIEW_POINT;
84   }
85
86   /// \brief Creates a new part document if needed
87   SKETCHPLUGIN_EXPORT virtual void execute();
88
89   /// \brief Request for initialization of data model of the feature: adding all attributes
90   SKETCHPLUGIN_EXPORT virtual void initAttributes();
91
92   /// Reimplemented from ModelAPI_Feature::isMacro()
93   /// \returns true
94   SKETCHPLUGIN_EXPORT virtual bool isMacro() const { return true; }
95
96   /// Reimplemented from ModelAPI_Feature::isPreviewNeeded(). Returns false.
97   /// This is necessary to perform execute only by apply the feature
98   SKETCHPLUGIN_EXPORT virtual bool isPreviewNeeded() const { return false; }
99
100   /// \brief Use plugin manager for features creation
101   SketchPlugin_Trim();
102
103   /// Returns the AIS preview
104   SKETCHPLUGIN_EXPORT virtual AISObjectPtr getAISObject(AISObjectPtr thePrevious);
105
106   /// Apply information of the message to current object. It fills selected point and object
107   virtual std::string processEvent(const std::shared_ptr<Events_Message>& theMessage);
108
109 private:
110   bool setCoincidenceToAttribute(const AttributePtr& theAttribute,
111             const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences,
112             std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete);
113   /// Move tangency constraint to the feature if it is geometrically closely to it
114   /// \param theAttribute an attribute of a tangent constraint feature
115   /// \param theFeature a feature that can be set into the attribute
116   bool moveTangency(const AttributePtr& theAttribute, const FeaturePtr& theFeature);
117
118   /// Obtains those constraints of the feature that should be modified. output maps contain
119   /// point of coincidence and attribute id to be modified after split
120   /// \param theFeaturesToDelete [out] constrains that will be deleted after split
121   /// \param theFeaturesToUpdate [out] constrains that will be updated after split
122   void getConstraints(std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete,
123                       std::set<FeaturePtr>& theFeaturesToUpdate);
124
125   /// Remove references constraints from attribute of base feature refer to the given attribute
126   /// \param theAttribute an attribute
127   /// \param theModifiedAttributes modifiable container of attributes
128   void removeReferencesToAttribute(const AttributePtr& theAttribute,
129                   std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes);
130
131   /// Make the base object is splitted by the point attributes
132   /// \param theBaseRefAttributes container of references to the attributes of base feature
133   /// \param thePoints a list of points where coincidences will be build
134   /// \param theModifiedAttributes a container of attribute on base
135   /// feature to attribute on new feature
136   /// \return new line if it was created
137   FeaturePtr trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
138                 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
139                 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
140                 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
141                 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
142
143   /// Make the base object is splitted by the point attributes
144   /// \param thePoints a list of points where coincidences will be build
145   /// \return new line if it was created
146   FeaturePtr trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
147                const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
148                std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
149                std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
150                std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
151
152   /// Make the base object is splitted by the point attributes
153   /// \param thePoints a list of points where coincidences will be build
154   FeaturePtr trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
155                   const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
156                   std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
157                   std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
158
159   /// Correct the first and the second point to provide condition that the first is closer to
160   /// the start point and the second point - to the last end of current segment. To rearrange
161   /// them if this condition is not satisfied.
162   /// \param theStartPointAttr a start point of a segment
163   /// \param theEndPointAttr an end point of a segment
164   /// \param theFirstPoint a start point of a segment
165   /// \param theSecondPoint an end point of a segment
166   void arrangePointsOnLine(const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
167                            const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
168                            std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
169                            std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
170
171   /// Correct the first and the second point to provide condition that the first is closer to
172   /// the start point and the second point - to the last end of current segment. To rearrange
173   /// them if this condition is not satisfied.
174   /// \param theArc an arc to be split
175   /// \param theStartPointAttr a start point of a segment
176   /// \param theEndPointAttr an end point of a segment
177   /// \param theFirstPoint a start point of a segment
178   /// \param theSecondPoint an end point of a segment
179   void arrangePointsOnArc(const FeaturePtr& theArc,
180                           const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
181                           const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
182                           std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
183                           std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
184
185   /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
186   /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
187   /// \param theSourceAttribute an attribute of GeomDataAPI_Point2D to obtain data
188   void fillAttribute(const AttributePtr& theModifiedAttribute,
189                      const AttributePtr& theSourceAttribute);
190
191   /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
192   /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
193   /// \param thePoint a point value
194   void fillPointAttribute(const AttributePtr& theModifiedAttribute,
195                           const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
196
197   /// Creates a line feature filled by center of base feature and given points
198   /// \param theBaseFeature another arc feature
199   /// \param theFirstAttribute an attribute with coordinates for the start point
200   /// \param theSecondAttribute an attribute with coordinates for the end point
201   FeaturePtr createLineFeature(const FeaturePtr& theBaseFeature,
202                                const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
203                                const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
204
205   /// Creates an arc feature filled by center of base feature and given points
206   /// \param theBaseFeature another arc feature
207   /// \param theFirstAttribute an attribute with coordinates for the start point
208   /// \param theSecondAttribute an attribute with coordinates for the end point
209   FeaturePtr createArcFeature(const FeaturePtr& theBaseFeature,
210                               const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
211                               const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
212
213   /// Result result of the feature to build constraint with. For arc, circle it is an edge result.
214   /// \param theFeature a feature
215   /// \return result object
216   std::shared_ptr<ModelAPI_Result> getFeatureResult(
217                                     const std::shared_ptr<ModelAPI_Feature>& theFeature);
218
219 private:
220   void findShapePoints(const std::string& theObjectAttributeId,
221                        const std::string& thePointAttributeId,
222                        std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
223                        std::shared_ptr<GeomAPI_Pnt>& aLastPoint);
224
225   std::shared_ptr<GeomAPI_Pnt2d> convertPoint(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
226
227 private:
228   std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> > myCashedShapes;
229   std::map<std::shared_ptr<ModelAPI_Object>,
230            GeomAlgoAPI_ShapeTools::PointToRefsMap> myObjectToPoints;
231 };
232
233 #endif