1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #ifndef SketchPlugin_Trim_H_
22 #define SketchPlugin_Trim_H_
24 #include "SketchPlugin.h"
26 #include "GeomAPI_IPresentable.h"
27 #include <ModelAPI_IReentrant.h>
28 #include <SketchPlugin_Sketch.h>
30 class GeomDataAPI_Point2D;
31 class ModelAPI_Feature;
32 class ModelAPI_Result;
33 class ModelAPI_Object;
35 typedef std::pair<std::string, std::shared_ptr<GeomDataAPI_Point2D> > IdToPointPair;
37 /** \class SketchPlugin_Trim
39 * \brief Feature for creation of a new constraint trimming object. Entities for split:
41 class SketchPlugin_Trim : public SketchPlugin_Feature, public GeomAPI_IPresentable,
42 public ModelAPI_IReentrant
45 /// Split constraint kind
46 inline static const std::string& ID()
48 static const std::string MY_TRIM_ID("SketchTrim");
51 /// \brief Returns the kind of a feature
52 SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
54 static std::string MY_KIND = SketchPlugin_Trim::ID();
58 /// The value parameter for the constraint
59 inline static const std::string& SELECTED_OBJECT()
61 static const std::string MY_SELECTED_OBJECT("SelectedObject");
62 return MY_SELECTED_OBJECT;
65 /// Start 2D point of the split segment
66 inline static const std::string& SELECTED_POINT()
68 static const std::string MY_SELECTED_POINT("SelectedPoint");
69 return MY_SELECTED_POINT;
72 /// The value parameter for the preview object
73 inline static const std::string& PREVIEW_OBJECT()
75 static const std::string MY_PREVIEW_OBJECT("PreviewObject");
76 return MY_PREVIEW_OBJECT;
79 /// Start 2D point of the split segment
80 inline static const std::string& PREVIEW_POINT()
82 static const std::string MY_PREVIEW_POINT("PreviewPoint");
83 return MY_PREVIEW_POINT;
86 /// \brief Creates a new part document if needed
87 SKETCHPLUGIN_EXPORT virtual void execute();
89 /// \brief Request for initialization of data model of the feature: adding all attributes
90 SKETCHPLUGIN_EXPORT virtual void initAttributes();
92 /// Reimplemented from ModelAPI_Feature::isMacro()
94 SKETCHPLUGIN_EXPORT virtual bool isMacro() const { return true; }
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; }
100 /// \brief Use plugin manager for features creation
103 /// Returns the AIS preview
104 SKETCHPLUGIN_EXPORT virtual AISObjectPtr getAISObject(AISObjectPtr thePrevious);
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);
109 typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
110 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
111 std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
113 static void fillObjectShapes(const std::shared_ptr<ModelAPI_Object>& theObject,
114 const std::shared_ptr<ModelAPI_Object>& theSketch,
115 std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> >& theCashedShapes,
116 std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap>& theObjectToPoints);
119 bool setCoincidenceToAttribute(const AttributePtr& theAttribute,
120 const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences,
121 std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete);
122 /// Move tangency constraint to the feature if it is geometrically closely to it
123 /// \param theAttribute an attribute of a tangent constraint feature
124 /// \param theFeature a feature that can be set into the attribute
125 bool moveTangency(const AttributePtr& theAttribute, const FeaturePtr& theFeature);
127 GeomShapePtr getSubShape(const std::string& theObjectAttributeId,
128 const std::string& thePointAttributeId);
130 /// Returns geom point attribute of the feature bounds. It processes line or arc.
131 /// For circle feature, the result attributes are null
132 /// \param theFeature a source feature
133 /// \param theStartPointAttr an out attribute to start point
134 /// \param theStartPointAttr an out attribute to end point
135 void getFeaturePoints(const FeaturePtr& theFeature,
136 std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
137 std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr);
139 /// Obtains those constraints of the feature that should be modified. output maps contain
140 /// point of coincidence and attribute id to be modified after split
141 /// \param theFeaturesToDelete [out] constrains that will be deleted after split
142 /// \param theFeaturesToUpdate [out] constrains that will be updated after split
143 void getConstraints(std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete,
144 std::set<FeaturePtr>& theFeaturesToUpdate);
146 /// Obtains references to feature point attributes and to feature,
147 /// e.g. for feature line: 1st container is
148 /// <1st line point, list<entity_a in distance, entity_b in parallel> >
149 /// <2nd line point, list<> >
150 /// for feature circle 2nd container is <entity_a in Radius, entity_b in equal, ...>
151 /// \param theFeature an investigated feature
152 /// \param theRefs a container of list of referenced attributes
153 void getRefAttributes(const FeaturePtr& theFeature,
154 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
155 std::list<AttributePtr>& theRefsToFeature);
157 /// Move constraints from attribute of base feature to attribute after modification
158 /// \param theBaseRefAttributes container of references to the attributes of base feature
159 /// \param theModifiedAttributes container of attributes placed instead of base attributes
160 /// at the same place
161 void updateRefAttConstraints(
162 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
163 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
164 std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete);
166 /// Remove references constraints from attribute of base feature refer to the given attribute
167 /// \param theAttribute an attribute
168 /// \param theModifiedAttributes modifiable container of attributes
169 void removeReferencesToAttribute(const AttributePtr& theAttribute,
170 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes);
172 /// Updates line length if it exist in the list
173 /// \param theFeaturesToUpdate a constraints container
174 void updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate);
176 /// Make the base object is splitted by the point attributes
177 /// \param theBaseRefAttributes container of references to the attributes of base feature
178 /// \param thePoints a list of points where coincidences will be build
179 /// \param theModifiedAttributes a container of attribute on base
180 /// feature to attribute on new feature
181 /// \return new line if it was created
182 FeaturePtr trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
183 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
184 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
185 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
186 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
188 /// Make the base object is splitted by the point attributes
189 /// \param thePoints a list of points where coincidences will be build
190 /// \return new line if it was created
191 FeaturePtr trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
192 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
193 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
194 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
195 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
197 /// Make the base object is splitted by the point attributes
198 /// \param thePoints a list of points where coincidences will be build
199 FeaturePtr trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
200 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
201 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
202 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
204 /// Correct the first and the second point to provide condition that the first is closer to
205 /// the start point and the second point - to the last end of current segment. To rearrange
206 /// them if this condition is not satisfied.
207 /// \param theStartPointAttr a start point of a segment
208 /// \param theEndPointAttr an end point of a segment
209 /// \param theFirstPoint a start point of a segment
210 /// \param theSecondPoint an end point of a segment
211 void arrangePointsOnLine(const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
212 const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
213 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
214 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
216 /// Correct the first and the second point to provide condition that the first is closer to
217 /// the start point and the second point - to the last end of current segment. To rearrange
218 /// them if this condition is not satisfied.
219 /// \param theArc an arc to be split
220 /// \param theStartPointAttr a start point of a segment
221 /// \param theEndPointAttr an end point of a segment
222 /// \param theFirstPoint a start point of a segment
223 /// \param theSecondPoint an end point of a segment
224 void arrangePointsOnArc(const FeaturePtr& theArc,
225 const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
226 const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
227 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
228 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
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 theSourceAttribute an attribute of GeomDataAPI_Point2D to obtain data
233 void fillAttribute(const AttributePtr& theModifiedAttribute,
234 const AttributePtr& theSourceAttribute);
236 /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
237 /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
238 /// \param thePoint a point value
239 void fillPointAttribute(const AttributePtr& theModifiedAttribute,
240 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
242 /// Creates a line feature filled by center of base feature and given points
243 /// \param theBaseFeature another arc feature
244 /// \param theFirstAttribute an attribute with coordinates for the start point
245 /// \param theSecondAttribute an attribute with coordinates for the end point
246 FeaturePtr createLineFeature(const FeaturePtr& theBaseFeature,
247 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
248 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
250 /// Creates an arc feature filled by center of base feature and given points
251 /// \param theBaseFeature another arc feature
252 /// \param theFirstAttribute an attribute with coordinates for the start point
253 /// \param theSecondAttribute an attribute with coordinates for the end point
254 FeaturePtr createArcFeature(const FeaturePtr& theBaseFeature,
255 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
256 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
258 /// Add feature coincidence constraint between given attributes
259 /// \param theConstraintId a constraint index
260 /// \param theFirstAttribute an attribute of further coincidence
261 /// \param theSecondAttribute an attribute of further coincidence
262 std::shared_ptr<ModelAPI_Feature> createConstraint(const std::string& theConstraintId,
263 const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
264 const std::shared_ptr<ModelAPI_Attribute>& theSecondAttribute);
266 /// Add feature coincidence constraint between given attributes
267 /// \param theConstraintId a constraint index
268 /// \param theFirstAttribute an attribute of further coincidence
269 /// \param theSecondObject an object of further coincidence
270 std::shared_ptr<ModelAPI_Feature> createConstraintToObject(const std::string& theConstraintId,
271 const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
272 const std::shared_ptr<ModelAPI_Object>& theSecondObject);
274 /// Add feature coincidence constraint between given attributes
275 /// \param theConstraintId a constraint index
276 /// \param theFirstAttribute an attribute of further coincidence
277 /// \param theFirstAttribute an attribute of further coincidence
278 std::shared_ptr<ModelAPI_Feature> createConstraintForObjects(const std::string& theConstraintId,
279 const std::shared_ptr<ModelAPI_Object>& theFirstObject,
280 const std::shared_ptr<ModelAPI_Object>& theSecondObject);
282 /// Result result of the feature to build constraint with. For arc, circle it is an edge result.
283 /// \param theFeature a feature
284 /// \return result object
285 std::shared_ptr<ModelAPI_Result> getFeatureResult(
286 const std::shared_ptr<ModelAPI_Feature>& theFeature);
289 void findShapePoints(const std::string& theObjectAttributeId,
290 const std::string& thePointAttributeId,
291 std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
292 std::shared_ptr<GeomAPI_Pnt>& aLastPoint);
294 std::shared_ptr<GeomAPI_Pnt2d> convertPoint(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
297 std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> > myCashedShapes;
298 std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap> myObjectToPoints;