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 /// 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);
126 GeomShapePtr getSubShape(const std::string& theObjectAttributeId,
127 const std::string& thePointAttributeId);
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);
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);
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);
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);
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);
171 /// Updates line length if it exist in the list
172 /// \param theFeaturesToUpdate a constraints container
173 void updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate);
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);
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);
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);
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;
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;
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);
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);
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);
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);
257 /// Add feature coincidence constraint between given attributes
258 /// \param theConstraintId a constraint index
259 /// \param theFirstAttribute an attribute of further coincidence
260 /// \param theSecondAttribute an attribute of further coincidence
261 std::shared_ptr<ModelAPI_Feature> createConstraint(const std::string& theConstraintId,
262 const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
263 const std::shared_ptr<ModelAPI_Attribute>& theSecondAttribute);
265 /// Add feature coincidence constraint between given attributes
266 /// \param theConstraintId a constraint index
267 /// \param theFirstAttribute an attribute of further coincidence
268 /// \param theSecondObject an object of further coincidence
269 std::shared_ptr<ModelAPI_Feature> createConstraintToObject(const std::string& theConstraintId,
270 const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
271 const std::shared_ptr<ModelAPI_Object>& theSecondObject);
273 /// Add feature coincidence constraint between given attributes
274 /// \param theConstraintId a constraint index
275 /// \param theFirstAttribute an attribute of further coincidence
276 /// \param theFirstAttribute an attribute of further coincidence
277 std::shared_ptr<ModelAPI_Feature> createConstraintForObjects(const std::string& theConstraintId,
278 const std::shared_ptr<ModelAPI_Object>& theFirstObject,
279 const std::shared_ptr<ModelAPI_Object>& theSecondObject);
281 /// Result result of the feature to build constraint with. For arc, circle it is an edge result.
282 /// \param theFeature a feature
283 /// \return result object
284 std::shared_ptr<ModelAPI_Result> getFeatureResult(
285 const std::shared_ptr<ModelAPI_Feature>& theFeature);
288 void findShapePoints(const std::string& theObjectAttributeId,
289 const std::string& thePointAttributeId,
290 std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
291 std::shared_ptr<GeomAPI_Pnt>& aLastPoint);
293 std::shared_ptr<GeomAPI_Pnt2d> convertPoint(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
296 std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> > myCashedShapes;
297 std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap> myObjectToPoints;