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