Salome HOME
Make Name of result the same as for feature
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Trim.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchPlugin_Trim.h"
22
23 #include <GeomAPI_Dir2d.h>
24 #include <GeomAPI_Edge.h>
25 #include <GeomAPI_Pnt2d.h>
26 #include <GeomAPI_XY.h>
27 #include <GeomDataAPI_Point2D.h>
28 #include <GeomAlgoAPI_ShapeTools.h>
29 #include <GeomAlgoAPI_CompoundBuilder.h>
30
31 #include <ModelAPI_AttributeReference.h>
32 #include <ModelAPI_AttributeString.h>
33 #include <ModelAPI_AttributeRefAttr.h>
34 #include <ModelAPI_Tools.h>
35 #include <ModelAPI_AttributeBoolean.h>
36
37 #include <ModelAPI_Validator.h>
38 #include <ModelAPI_Session.h>
39 #include <ModelAPI_AttributeDouble.h>
40
41 #include <ModelGeomAlgo_Shape.h>
42
43 #include <SketchPlugin_Arc.h>
44 #include <SketchPlugin_ConstraintMiddle.h>
45 #include <SketchPlugin_Circle.h>
46 #include <SketchPlugin_ConstraintCoincidence.h>
47 #include <SketchPlugin_ConstraintEqual.h>
48 //#include <SketchPlugin_ConstraintParallel.h>
49 #include <SketchPlugin_ConstraintTangent.h>
50 #include <SketchPlugin_ConstraintLength.h>
51 #include <SketchPlugin_ConstraintMirror.h>
52 #include <SketchPlugin_ConstraintCollinear.h>
53 #include <SketchPlugin_Line.h>
54 #include <SketchPlugin_MultiRotation.h>
55 #include <SketchPlugin_MultiTranslation.h>
56 #include <SketchPlugin_Point.h>
57 #include <SketchPlugin_Projection.h>
58
59 #include <ModelAPI_EventReentrantMessage.h>
60
61 #include <ModelAPI_Events.h>
62 #include <SketchPlugin_Line.h>
63 #include <SketchPlugin_Arc.h>
64 #include <SketchPlugin_Circle.h>
65
66 #include <ModelGeomAlgo_Point2D.h>
67 #include <Events_Loop.h>
68
69 #include <cmath>
70
71 //#define DEBUG_TRIM_METHODS
72 //#define DEBUG_TRIM
73
74 #ifdef DEBUG_TRIM
75 #include <iostream>
76 #endif
77
78 #ifdef DEBUG_TRIM_METHODS
79 #include <iostream>
80 #endif
81
82 static const double PI = 3.141592653589793238463;
83
84 static const std::string OPERATION_HIGHLIGHT_COLOR() { return "128, 0, 0"; }
85 static const std::string OPERATION_REMOVE_FEATURE_COLOR() { return "255, 174, 201"; }
86
87 SketchPlugin_Trim::SketchPlugin_Trim()
88 {
89 }
90
91 void SketchPlugin_Trim::initAttributes()
92 {
93   data()->addAttribute(SELECTED_OBJECT(), ModelAPI_AttributeReference::typeId());
94   data()->addAttribute(SELECTED_POINT(), GeomDataAPI_Point2D::typeId());
95
96   data()->addAttribute(PREVIEW_POINT(), GeomDataAPI_Point2D::typeId());
97   data()->addAttribute(PREVIEW_OBJECT(), ModelAPI_AttributeReference::typeId());
98
99   data()->attribute(PREVIEW_POINT())->setIsArgument(false);
100   data()->attribute(SELECTED_POINT())->setIsArgument(false);
101   data()->attribute(PREVIEW_OBJECT())->setIsArgument(false);
102
103   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_POINT());
104   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_OBJECT());
105 }
106
107 void SketchPlugin_Trim::findShapePoints(const std::string& theObjectAttributeId,
108                                         const std::string& thePointAttributeId,
109                                         std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
110                                         std::shared_ptr<GeomAPI_Pnt>& aLastPoint)
111 {
112   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
113                                             data()->attribute(theObjectAttributeId));
114   ObjectPtr aBaseObject = aBaseObjectAttr->value();
115
116   AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
117                                               data()->attribute(thePointAttributeId));
118   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
119   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
120                                                                anAttributePnt2d->y());
121
122   if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
123     fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
124
125   const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
126   if (!aShapes.empty()) {
127     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
128     for (; anIt != aLast; anIt++) {
129       GeomShapePtr aBaseShape = *anIt;
130       std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
131       if (ModelGeomAlgo_Point2D::isPointOnEdge(aBaseShape, anAttributePnt, aProjectedPoint)) {
132
133         if (aBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
134           std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aBaseShape));
135           //GeomAPI_Shape::Orientation anOrientation = anEdge->orientation();
136           //if (anOrientation == GeomAPI_Shape::REVERSED) {
137             aStartPoint = anEdge->lastPoint();
138             aLastPoint = anEdge->firstPoint();
139           //}
140           //else {
141             //aStartPoint = anEdge->firstPoint();
142             //aLastPoint = anEdge->lastPoint();
143           //}
144         }
145       }
146     }
147   }
148 #ifdef DEBUG_TRIM
149   std::cout << "<findShapePoints> => "
150     << std::endl << "Attribute point: "
151     << anAttributePnt->x() << ", " << anAttributePnt->y() << ", " << anAttributePnt->z() << "]"
152     << std::endl << "Start Point: ["
153     << aStartPoint->x() << ", " << aStartPoint->y() << ", " << aStartPoint->z() << "]"
154     << std::endl << "Last Point: ["
155     << aLastPoint->x() << ", " << aLastPoint->y() << ", " << aLastPoint->z() << "]"
156     << std::endl;
157 #endif
158 }
159
160 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Trim::convertPoint(
161                                                    const std::shared_ptr<GeomAPI_Pnt>& thePoint)
162 {
163   std::shared_ptr<GeomAPI_Pnt2d> aPoint;
164   if (!thePoint.get())
165     return aPoint;
166
167   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
168                                         data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
169   ObjectPtr aBaseObject = aBaseObjectAttr->value();
170   if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
171     fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
172
173   bool aFound = false;
174   const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
175   for (PointToRefsMap::const_iterator aPointIt = aRefsMap.begin();
176        aPointIt != aRefsMap.end() && !aFound; aPointIt++) {
177     if (aPointIt->first->isEqual(thePoint)) {
178       const std::pair<std::list<AttributePoint2DPtr >,
179                std::list<ObjectPtr > >& anInfo = aPointIt->second;
180       const std::list<AttributePoint2DPtr >& anAttributes = anInfo.first;
181       if (!anAttributes.empty()) {
182         aPoint = anAttributes.front()->pnt();
183         aFound = true;
184       }
185       else {
186         aPoint = sketch()->to2D(thePoint);
187         aFound = true;
188       }
189     }
190   }
191   if (!aFound) {
192     // returns an end of the shape to define direction of split if feature's attribute
193     // participates
194     aPoint = sketch()->to2D(thePoint);
195   }
196   return aPoint;
197 }
198
199 void SketchPlugin_Trim::execute()
200 {
201 #ifdef DEBUG_TRIM_METHODS
202   std::cout << "SketchPlugin_Trim::execute: " << data()->name() << std::endl;
203 #endif
204
205   SketchPlugin_Sketch* aSketch = sketch();
206   if (!aSketch) {
207     setError("Error: Sketch object is empty.");
208     return;
209   }
210
211   // Check the base objects are initialized.
212   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
213                                         data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
214   if(!aBaseObjectAttr->isInitialized()) {
215     setError("Error: Base object is not initialized.");
216     return;
217   }
218   ObjectPtr aBaseObject = aBaseObjectAttr->value();
219   if (!aBaseObject.get()) {
220     setError("Error: Base object is not initialized.");
221     return;
222   }
223   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
224
225   /// Remove reference of this feature to feature used in preview, it is not necessary anymore
226   /// as trim will be removed after execute
227   AttributeReferencePtr aPreviewObjectAttr =
228                      std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
229                      data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
230
231   ObjectPtr aPreviewObject = aPreviewObjectAttr->value();
232   AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
233                                            data()->attribute(PREVIEW_POINT()));
234   std::shared_ptr<GeomAPI_Pnt2d> aPreviewPnt2d = aPoint->pnt();
235   // nullify pointer of preview attribute
236   aPreviewObjectAttr->setValue(ResultPtr());
237
238   bool anIsEqualPreviewAndSelected = aPreviewObject == aBaseObject;
239
240   /// points of trim
241   std::shared_ptr<GeomAPI_Pnt> aStartShapePoint, aLastShapePoint;
242 #ifdef DEBUG_TRIM
243   std::cout << " Base Feature: " << aBaseFeature->data()->name() << std::endl;
244 #endif
245   findShapePoints(SELECTED_OBJECT(), SELECTED_POINT(), aStartShapePoint, aLastShapePoint);
246
247   std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint2d = convertPoint(aStartShapePoint);
248   std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint2d = convertPoint(aLastShapePoint);
249   /// find features that should be deleted (e.g. Middle Point) or updated (e.g. Length)
250   std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
251   getConstraints(aFeaturesToDelete, aFeaturesToUpdate);
252   // find references(attributes and features) to the base feature
253   std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
254   std::list<AttributePtr> aRefsToFeature;
255   getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
256 #ifdef DEBUG_TRIM
257   std::cout << "---- getRefAttributes ----" << std::endl;
258   std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
259     aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
260   std::cout << std::endl << "References to attributes of base feature [" <<
261     aBaseRefAttributes.size() << "]" << std::endl;
262   for (; aRefIt != aRefLast; aRefIt++) {
263     AttributePtr aBaseAttr = aRefIt->first;
264     std::list<AttributePtr> aRefAttributes = aRefIt->second;
265     std::string aRefsInfo;
266     std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
267                                             aRefAttrLast = aRefAttributes.end();
268     for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
269       if (!aRefsInfo.empty())
270         aRefsInfo.append(",");
271       AttributePtr aRAttr = *aRefAttrIt;
272       aRefsInfo.append(aRAttr->id());
273       FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
274       aRefsInfo.append("(" + aRFeature->name() + ") ");
275     }
276     std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
277       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
278     std::cout << aPointAttr->id().c_str() <<
279       ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
280   }
281   std::cout << std::endl;
282   std::cout << std::endl << "References to base feature [" <<
283     aRefsToFeature.size() << "]" << std::endl;
284   std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
285                                           aRefAttrLast = aRefsToFeature.end();
286   std::string aRefsInfo;
287   for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
288     if (!aRefsInfo.empty())
289       aRefsInfo.append(",");
290     AttributePtr aRAttr = *aRefAttrIt;
291     aRefsInfo.append(aRAttr->id());
292     FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
293     aRefsInfo.append("(" + aRFeature->name() + ") ");
294   }
295   std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
296   std::cout << "---- getRefAttributes:end ----" << std::endl;
297 #endif
298   std::set<AttributePoint2DPtr> aFurtherCoincidences;
299   std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
300   const std::string& aKind = aBaseFeature->getKind();
301   FeaturePtr aReplacingFeature, aNewFeature;
302   if (aKind == SketchPlugin_Circle::ID()) {
303     aReplacingFeature = trimCircle(aStartShapePoint2d, aLastShapePoint2d,
304                aFurtherCoincidences, aModifiedAttributes);
305
306     aFeaturesToDelete.insert(aBaseFeature);
307     // as circle is removed, erase it from dependencies(arguments) of this feature
308     // otherwise Trim feature will be removed with the circle before
309     // this operation is finished
310     aBaseObjectAttr->setObject(ResultPtr());
311   }
312   else if (aKind == SketchPlugin_Line::ID()) {
313     aNewFeature = trimLine(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
314                            aFurtherCoincidences, aModifiedAttributes);
315   }
316   else if (aKind == SketchPlugin_Arc::ID()) {
317     aNewFeature = trimArc(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
318                           aFurtherCoincidences, aModifiedAttributes);
319   }
320
321   // constraints to end points of trim feature
322   if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
323     fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
324
325   // create coincidence to objects, intersected the base object
326   const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
327   for (std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
328                                                      aLast = aFurtherCoincidences.end();
329        anIt != aLast; anIt++) {
330     AttributePoint2DPtr aPointAttribute = (*anIt);
331     std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
332
333 #ifdef DEBUG_TRIM
334     std::cout << "<compare Points> => " << std::endl
335             << "aPoint2d: [" << aPoint2d->x() << ", " << aPoint2d->y() << "]" << std::endl;
336     if (aStartShapePoint2d.get())
337       std::cout << "Start Point: [" << aStartShapePoint2d->x() << ", " << aStartShapePoint2d->y()
338                 << "]" << std::endl;
339     if (aLastShapePoint2d.get())
340       std::cout << "Last Point: [" << aLastShapePoint2d->x() << ", " << aLastShapePoint2d->y()
341                 << "]" << std::endl;
342 #endif
343
344     std::shared_ptr<GeomAPI_Pnt> aPoint;
345     if (aStartShapePoint2d.get() && aPoint2d->isEqual(aStartShapePoint2d))
346       aPoint = aStartShapePoint;
347     else if (aLastShapePoint2d.get() && aPoint2d->isEqual(aLastShapePoint2d))
348       aPoint = aLastShapePoint;
349
350     if (!aPoint.get())
351       continue;
352
353     std::pair<std::list<AttributePoint2DPtr >, std::list<ObjectPtr > > anInfo;
354     for (PointToRefsMap::const_iterator aRefIt = aRefsMap.begin(); aRefIt != aRefsMap.end();
355          aRefIt++)
356     {
357       if (aRefIt->first->isEqual(aPoint)) {
358         anInfo = aRefIt->second;
359         break;
360       }
361     }
362     const std::list<ObjectPtr>& anObjects = anInfo.second;
363     for (std::list<ObjectPtr>::const_iterator anObjectIt = anObjects.begin();
364       anObjectIt != anObjects.end(); anObjectIt++) {
365       createConstraintToObject(SketchPlugin_ConstraintCoincidence::ID(), aPointAttribute,
366                                *anObjectIt);
367     }
368   }
369
370   // move constraints from base feature to replacing feature: ignore coincidences to feature
371   // if attributes of coincidence participated in split
372   ResultPtr aReplacingResult;
373   if (aReplacingFeature.get()) {
374     aReplacingFeature->execute(); // need it to obtain result
375     aReplacingResult = getFeatureResult(aReplacingFeature);
376   }
377   for(std::list<AttributePtr>::const_iterator anIt = aRefsToFeature.begin(),
378                                           aLast = aRefsToFeature.end();
379       anIt != aLast; anIt++) {
380     AttributePtr anAttribute = *anIt;
381
382     if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences))
383       continue;
384
385     // move tangency constraint to the nearest feature if possible
386     if (aNewFeature.get() && moveTangency(anAttribute, aNewFeature))
387       continue;
388
389     if (aReplacingResult.get()) {
390       AttributeRefAttrPtr aRefAttr =
391           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
392       if (aRefAttr.get())
393         aRefAttr->setObject(aReplacingResult);
394       else {
395         AttributeReferencePtr aReferenceAttr =
396                              std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
397         if (aReferenceAttr.get())
398           aReferenceAttr->setObject(aReplacingResult);
399       }
400     }
401   }
402
403   updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes, aFeaturesToDelete);
404
405   // Wait all constraints being created, then send update events
406   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
407   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
408   if (isUpdateFlushed)
409     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
410
411   // delete constraints
412 #ifdef DEBUG_TRIM
413   if (aFeaturesToDelete.size() > 0) {
414     std::cout << "after SPlit: removeFeaturesAndReferences: " << std::endl;
415     std::string aValue;
416     for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
417          anIt != aFeaturesToDelete.end(); anIt++) {
418       FeaturePtr aFeature = *anIt;
419       std::cout << aFeature->data()->name() << std::endl;
420     }
421   }
422 #endif
423   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
424   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
425
426   updateFeaturesAfterTrim(aFeaturesToUpdate);
427
428   // Send events to update the sub-features by the solver.
429   if(isUpdateFlushed) {
430     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
431   }
432
433   if (anIsEqualPreviewAndSelected) {
434     // equal preview and selected objects
435     // nothing to do if the preview and selected objects are different
436     if (aReplacingResult.get()) { // base object was removed
437       aPreviewObject = aReplacingResult;
438       //aMessage->setSelectedObject(aReplacingResult);
439 #ifdef DEBUG_TRIM_METHODS
440       if (!aSelectedShape.get())
441         std::cout << "Set empty selected object" << std::endl;
442       else
443         std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
444 #endif
445     }
446     else {
447       aPreviewObject = ObjectPtr();
448
449       aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
450       aBaseObject = getFeatureResult(aBaseFeature);
451       std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
452                                                                 aPreviewPnt2d->y());
453       ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
454       if (aBaseResult) {
455         GeomShapePtr aShape = aBaseResult->shape();
456         std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
457         if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
458           aPreviewObject = aBaseResult;
459       }
460       if (!aPreviewObject.get() && aNewFeature.get()) {
461         ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
462         if (aNewFeatureResult.get()) {
463           GeomShapePtr aShape = aNewFeatureResult->shape();
464           std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
465           if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
466             aPreviewObject = aNewFeatureResult;
467         }
468       }
469     }
470   }
471   if (aPreviewObject.get()) {
472     std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
473       <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
474                                            ModelAPI_EventReentrantMessage::eventId(), this));
475     aMessage->setSelectedObject(aPreviewObject);
476     Events_Loop::loop()->send(aMessage);
477   }
478 #ifdef DEBUG_TRIM
479   std::cout << "SketchPlugin_Trim::done" << std::endl;
480 #endif
481 }
482
483 std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message>& theMessage)
484 {
485 #ifdef DEBUG_TRIM_METHODS
486   std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
487 #endif
488   std::string aFilledAttributeName;
489
490   std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
491         std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
492   if (aMessage.get()) {
493     ObjectPtr anObject = aMessage->selectedObject();
494     std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
495
496     if (anObject.get() && aPoint.get()) {
497       if (myCashedShapes.find(anObject) == myCashedShapes.end())
498         fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
499       const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
500       if (aShapes.size() > 1) {
501         std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
502                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
503                               data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
504         std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
505                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
506                               data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
507         aRefSelectedAttr->setValue(anObject);
508         aRefPreviewAttr->setValue(anObject);
509
510         std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
511                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
512                               data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
513         std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
514                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
515                               data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
516         aPointSelectedAttr->setValue(aPoint);
517         aPointPreviewAttr->setValue(aPoint);
518
519         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
520
521         GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
522   #ifdef DEBUG_TRIM_METHODS
523         if (!aSelectedShape.get())
524           std::cout << "Set empty selected object" << std::endl;
525         else
526           std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
527   #endif
528         aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
529       }
530     }
531   }
532   return aFilledAttributeName;
533 }
534
535 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
536                                 const std::set<AttributePoint2DPtr>& theFurtherCoincidences)
537 {
538   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
539   if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
540     return false;
541
542   AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
543   if (!aRefPointAttr.get())
544     return false;
545   std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
546
547   std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
548                                                 aLast = theFurtherCoincidences.end();
549   bool aFoundPoint = false;
550   for (; anIt != aLast && !aFoundPoint; anIt++) {
551     AttributePoint2DPtr aPointAttribute = (*anIt);
552     std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
553     if (aPoint2d->isEqual(aRefPnt2d)) {
554       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
555                                                                            theAttribute);
556       if (aRefAttr.get()) {
557         aRefAttr->setAttr(aPointAttribute);
558         aFoundPoint = true;
559       }
560     }
561   }
562   return aFoundPoint;
563 }
564
565 bool SketchPlugin_Trim::moveTangency(const AttributePtr& theAttribute,
566                                      const FeaturePtr& theFeature)
567 {
568   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
569   if (aFeature->getKind() != SketchPlugin_ConstraintTangent::ID())
570     return false;
571
572   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
573                                                                            theAttribute);
574   if (!aRefAttr.get())
575     return false;
576
577   // get shape of tangent object to the current
578   std::string aTangentAttr = SketchPlugin_Constraint::ENTITY_A();
579   if (aRefAttr->id() == SketchPlugin_Constraint::ENTITY_A())
580     aTangentAttr = SketchPlugin_Constraint::ENTITY_B();
581   AttributeRefAttrPtr aTangentRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
582                                                      aFeature->attribute(aTangentAttr));
583   FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentRefAttr->object());
584
585   // get shape of the feature of the attribute
586   FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aRefAttr->object());
587   anAttributeFeature->execute(); // the modified value should be applyed to recompute shape
588   PointToRefsMap aPointToAttributeOrObject;
589   std::list<FeaturePtr> aFeatures;
590   aFeatures.push_back(anAttributeFeature);
591   ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
592                                                    aPointToAttributeOrObject);
593   if (!aPointToAttributeOrObject.empty())
594     return true; // the attribute feature has a point of intersection, so we do not replace it
595
596   // get shape of the feature
597   aPointToAttributeOrObject.clear();
598   aFeatures.clear();
599   aFeatures.push_back(theFeature);
600   ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
601                                                    aPointToAttributeOrObject);
602   if (!aPointToAttributeOrObject.empty()) {
603     std::set<ResultPtr> anEdgeShapes;
604     ModelGeomAlgo_Shape::shapesOfType(theFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
605     if (!anEdgeShapes.empty()) {
606       ResultPtr aResult = *anEdgeShapes.begin();
607       if (aResult.get()) {
608         aRefAttr->setObject(aResult);
609         return true; // the attribute feature has a point of intersection, so we do not replace it
610       }
611     }
612   }
613   return false;
614 }
615
616 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
617 {
618 #ifdef DEBUG_TRIM_METHODS
619   std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
620 #endif
621
622   AISObjectPtr anAIS = thePrevious;
623
624   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
625   GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
626   if (aPreviewShape.get())
627     aShapes.push_back(aPreviewShape);
628   GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
629   if (aSelectedShape.get())
630     aShapes.push_back(aSelectedShape);
631
632   if (aShapes.empty())
633     return AISObjectPtr();
634
635   GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
636   if (!aBaseShape.get())
637     return AISObjectPtr();
638
639   if (aBaseShape.get()) {
640     if (!anAIS)
641       anAIS = AISObjectPtr(new GeomAPI_AISObject);
642     anAIS->createShape(aBaseShape);
643
644     std::vector<int> aColor;
645     aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
646     double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
647     int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
648     anAIS->setColor(aColor[0], aColor[1], aColor[2]);
649     // width when there is not base object should be extened in several points
650     // in order to see this preview over highlight
651     anAIS->setWidth(aWidth+4);
652     anAIS->setLineStyle(aLineStyle);
653   }
654   else
655     anAIS = AISObjectPtr();
656
657   return anAIS;
658 }
659
660 GeomShapePtr SketchPlugin_Trim::getSubShape(const std::string& theObjectAttributeId,
661                                             const std::string& thePointAttributeId)
662 {
663   GeomShapePtr aBaseShape;
664
665   AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
666                                                        data()->attribute(theObjectAttributeId));
667   ObjectPtr aBaseObject = anObjectAttr->value();
668   if (!aBaseObject.get())
669     return aBaseShape;
670
671   // point on feature
672   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
673                                            data()->attribute(thePointAttributeId));
674   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
675   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
676                                                                anAttributePnt2d->y());
677
678   if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
679     fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
680
681   const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
682   if (!aShapes.empty()) {
683     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
684     for (; anIt != aLast; anIt++) {
685       GeomShapePtr aShape = *anIt;
686       std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
687       if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
688         aBaseShape = aShape;
689     }
690   }
691   return aBaseShape;
692 }
693
694 void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
695                                          AttributePoint2DPtr& theStartPointAttr,
696                                          AttributePoint2DPtr& theEndPointAttr)
697 {
698   std::string aFeatureKind = theFeature->getKind();
699   std::string aStartAttributeName, anEndAttributeName;
700   if (aFeatureKind == SketchPlugin_Line::ID()) {
701     aStartAttributeName = SketchPlugin_Line::START_ID();
702     anEndAttributeName = SketchPlugin_Line::END_ID();
703   }
704   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
705     aStartAttributeName = SketchPlugin_Arc::START_ID();
706     anEndAttributeName = SketchPlugin_Arc::END_ID();
707   }
708   if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
709     theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
710                                          theFeature->attribute(aStartAttributeName));
711     theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
712                                          theFeature->attribute(anEndAttributeName));
713   }
714 }
715
716 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
717                                        std::set<FeaturePtr>& theFeaturesToUpdate)
718 {
719   std::shared_ptr<ModelAPI_Data> aData = data();
720
721   // Check the base objects are initialized.
722   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
723                                          aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
724   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
725   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
726
727   std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
728   std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
729   aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
730
731   std::set<AttributePtr>::const_iterator aIt;
732   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
733     std::shared_ptr<ModelAPI_Attribute> anAttr = (*aIt);
734     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
735     std::string aRefFeatureKind = aRefFeature->getKind();
736     std::string anAttributeId = anAttr->id();
737     if ((aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() &&
738          anAttributeId == SketchPlugin_ConstraintMirror::MIRROR_LIST_ID()) ||
739         (aRefFeatureKind == SketchPlugin_MultiRotation::ID() &&
740          anAttributeId == SketchPlugin_MultiRotation::ROTATION_LIST_ID()) ||
741         (aRefFeatureKind == SketchPlugin_MultiTranslation::ID() &&
742          anAttributeId == SketchPlugin_MultiTranslation::TRANSLATION_LIST_ID()) ||
743         aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
744       theFeaturesToDelete.insert(aRefFeature);
745     else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
746       theFeaturesToUpdate.insert(aRefFeature);
747   }
748 }
749
750 void SketchPlugin_Trim::getRefAttributes(const FeaturePtr& theFeature,
751                                     std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
752                                     std::list<AttributePtr>& theRefsToFeature)
753 {
754   theRefs.clear();
755
756   std::list<AttributePtr> aPointAttributes =
757     theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
758   std::set<AttributePtr> aPointAttributesSet;
759
760   std::list<AttributePtr>::const_iterator aPIt =
761     aPointAttributes.begin(), aPLast = aPointAttributes.end();
762   for (; aPIt != aPLast; aPIt++)
763     aPointAttributesSet.insert(*aPIt);
764
765   std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
766   std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
767   aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
768
769   std::set<AttributePtr>::const_iterator aIt;
770   for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
771     AttributePtr anAttr = (*aIt);
772     FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
773     if (anAttrFeature.get() != this &&
774         anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
775       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
776       if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
777         AttributePtr anAttrInRef = aRefAttr->attr();
778         if (anAttrInRef.get() &&
779             aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
780           if (theRefs.find(anAttrInRef) != theRefs.end())
781             theRefs[anAttrInRef].push_back(aRefAttr);
782           else {
783             std::list<AttributePtr> anAttrList;
784             anAttrList.push_back(aRefAttr);
785             theRefs[anAttrInRef] = anAttrList;
786           }
787         }
788       }
789       else { /// find attributes referenced to feature itself
790         theRefsToFeature.push_back(anAttr);
791       }
792     }
793   }
794 }
795
796 void SketchPlugin_Trim::updateRefAttConstraints(
797                     const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
798                     const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
799                     std::set<FeaturePtr>& theFeaturesToDelete)
800 {
801 #ifdef DEBUG_TRIM
802   std::cout << "SketchPlugin_Trim::updateRefAttConstraints" << std::endl;
803 #endif
804
805   std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
806     anIt = theModifiedAttributes.begin(),  aLast = theModifiedAttributes.end();
807   for (; anIt != aLast; anIt++) {
808     AttributePtr anAttribute = anIt->first;
809
810     /// not found in references
811     if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
812       continue;
813     std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
814     std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
815                                             aRLast = aRefAttributes.end();
816
817     AttributePtr aNewAttribute = anIt->second;
818     if (aNewAttribute.get()) {
819       for (; aRefIt != aRLast; aRefIt++) {
820         AttributeRefAttrPtr aRefAttr =
821                         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
822         if (aRefAttr.get()) {
823             aRefAttr->setAttr(aNewAttribute);
824         }
825       }
826     }
827   }
828 }
829
830 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
831                   std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
832 {
833   /// not found in references
834   if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
835     return;
836
837   std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
838   std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
839                                           aRLast = aRefAttributes.end();
840
841   std::set<FeaturePtr> aFeaturesToDelete;
842   for (; aRefIt != aRLast; aRefIt++) {
843     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
844     if (aRefAttr.get()) {
845       aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
846     }
847   }
848
849 #ifdef DEBUG_TRIM
850   // delete constraints
851   if (aFeaturesToDelete.size() > 0) {
852     std::cout << "removeReferencesToAttribute: " << std::endl;
853     std::string aValue;
854     for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
855          anIt != aFeaturesToDelete.end(); anIt++) {
856       FeaturePtr aFeature = *anIt;
857       std::cout << aFeature->data()->name() << std::endl;
858     }
859   }
860 #endif
861   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
862   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
863 }
864
865 void SketchPlugin_Trim::updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate)
866 {
867   std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
868                                        aLast = theFeaturesToUpdate.end();
869   for (; anIt != aLast; anIt++) {
870     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
871     std::string aRefFeatureKind = aRefFeature->getKind();
872     if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
873       std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
874                               std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
875       if (aLenghtFeature.get()) {
876         std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
877             ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
878         double aValue;
879         if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
880           aValueAttr->setValue(aValue);
881       }
882     }
883   }
884 }
885
886 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
887                   const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
888                   std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
889                   std::set<AttributePoint2DPtr>& thePoints,
890                   std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
891 {
892   FeaturePtr anNewFeature;
893
894   // Check the base objects are initialized.
895   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
896                                         data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
897   ObjectPtr aBaseObject = aBaseObjectAttr->value();
898   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
899
900   /// points of trim
901   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
902   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
903
904   std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
905   std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
906
907   std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
908   std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
909   arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
910                       aStartShapePoint, aLastShapePoint);
911 #ifdef DEBUG_TRIM
912   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
913   if (aStartShapePoint.get())
914     std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
915                                        aStartShapePoint->y() << "]" << std::endl;
916   std::cout << "1st point:   [" << aStartFeaturePoint->x() << ", " <<
917                                    aStartFeaturePoint->y() << "]" << std::endl;
918   if (aLastShapePoint.get())
919     std::cout << "2st point:   [" << aLastShapePoint->x() << ", " <<
920                                      aLastShapePoint->y() << "]" << std::endl;
921   std::cout << "End point:   [" << aLastFeaturePoint->x() << ", " <<
922                                    aLastFeaturePoint->y() << "]" << std::endl;
923 #endif
924
925   bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
926   bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
927   if (isStartPoint || isLastPoint) {
928     // result is one line: changed existing line
929     std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
930                                                   : SketchPlugin_Line::END_ID();
931     std::shared_ptr<GeomAPI_Pnt2d> aPoint;
932     if (aStartShapePoint.get() && aLastShapePoint.get())
933       aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
934     else
935       aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
936
937     // it is important to delete references before the feature modification because
938     // if deletion will be after the feature modification, solver returns the feature back
939     removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
940                                 theBaseRefAttributes);
941
942     fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
943     //theModifiedAttributes.insert(
944     //  std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
945
946     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
947                                (aBaseFeature->attribute(aModifiedAttribute)));
948   }
949   else {
950     // result is two lines: start line point - start shape point,
951     // last shape point - last line point
952     // create second line
953     anNewFeature = createLineFeature(aBaseFeature, aLastShapePoint, aLastFeaturePoint);
954     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
955                                (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
956
957     std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
958     theModifiedAttributes.insert(
959       std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
960                                    anNewFeature->attribute(SketchPlugin_Line::END_ID())));
961
962     // modify base arc
963     fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
964
965     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
966                                (aBaseFeature->attribute(aModifiedAttribute)));
967
968     // Collinear constraint for lines
969     createConstraintForObjects(SketchPlugin_ConstraintCollinear::ID(),
970                                getFeatureResult(aBaseFeature),
971                                getFeatureResult(anNewFeature));
972
973   }
974   return anNewFeature;
975 }
976
977 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
978                  const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
979                  std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
980                  std::set<AttributePoint2DPtr>& thePoints,
981                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
982 {
983   FeaturePtr anNewFeature;
984   // Check the base objects are initialized.
985   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
986                                         data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
987   ObjectPtr aBaseObject = aBaseObjectAttr->value();
988   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
989
990   /// points of trim
991   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
992   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
993
994   std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
995   std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
996
997   std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
998   std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
999   arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
1000                      aStartShapePoint, aLastShapePoint);
1001 #ifdef DEBUG_TRIM
1002   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1003   if (aStartShapePoint.get())
1004     std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1005                                        aStartShapePoint->y() << "]" << std::endl;
1006   std::cout << "Start arc attribute point:   [" << aStartArcPoint->x() << ", " <<
1007                                    aStartArcPoint->y() << "]" << std::endl;
1008   if (aLastShapePoint.get())
1009     std::cout << "Last shape point:   [" << aLastShapePoint->x() << ", " <<
1010                                      aLastShapePoint->y() << "]" << std::endl;
1011   std::cout << "Last arc attribute point:   [" << aLastArcPoint->x() << ", " <<
1012                                    aLastArcPoint->y() << "]" << std::endl;
1013 #endif
1014
1015   bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
1016   bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
1017   if (isStartPoint || isLastPoint) {
1018     // result is one arc: changed existing arc
1019     std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
1020                                                   : SketchPlugin_Arc::END_ID();
1021     std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1022     if (aStartShapePoint.get() && aLastShapePoint.get())
1023       aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
1024     else
1025       aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
1026
1027     removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
1028                                 theBaseRefAttributes);
1029
1030     fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
1031
1032     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1033                                (aBaseFeature->attribute(aModifiedAttribute)));
1034   }
1035   else {
1036     // result is two arcs: start arc point - start shape point, last shape point - last arc point
1037     // create second arc
1038     anNewFeature = createArcFeature(aBaseFeature, aLastShapePoint, aLastArcPoint);
1039     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1040                                (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1041
1042     std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
1043     theModifiedAttributes.insert(
1044       std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
1045                                    anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1046
1047     // modify base arc
1048     fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
1049
1050     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1051                                (aBaseFeature->attribute(aModifiedAttribute)));
1052
1053     // equal Radius constraint for arcs
1054     createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
1055                                getFeatureResult(aBaseFeature),
1056                                getFeatureResult(anNewFeature));
1057     // coincident centers constraint
1058     createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1059                      aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1060                      anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1061
1062 #ifdef DEBUG_TRIM
1063     std::cout << "Created arc on points:" << std::endl;
1064     std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1065                                            aStartShapePoint->y() << "]" << std::endl;
1066 #endif
1067   }
1068   return anNewFeature;
1069 }
1070
1071 FeaturePtr SketchPlugin_Trim::trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1072                                    const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1073                                    std::set<AttributePoint2DPtr>& thePoints,
1074                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1075 {
1076   // Check the base objects are initialized.
1077   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1078                                         data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
1079   ObjectPtr aBaseObject = aBaseObjectAttr->value();
1080   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1081
1082   /// points of trim
1083   //AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1084   //getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1085
1086   /// trim feature
1087   FeaturePtr anNewFeature = createArcFeature(aBaseFeature, theStartShapePoint, theLastShapePoint);
1088   // arc created by trim of circle is always correct, that means that it is not inversed
1089   anNewFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(false);
1090
1091   theModifiedAttributes.insert(
1092     std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1093                    anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1094
1095   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1096                              (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1097   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1098                              (anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1099
1100   return anNewFeature;
1101 }
1102
1103 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1104                                             const AttributePoint2DPtr& theEndPointAttr,
1105                                             std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1106                                             std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1107 {
1108   if (!theFirstPoint.get() || !theLastPoint.get())
1109     return;
1110
1111   // if first point is closer to last point, swap first and last values
1112   if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1113       theStartPointAttr->pnt()->distance(theLastPoint)) {
1114     std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1115     theFirstPoint = theLastPoint;
1116     theLastPoint = aTmpPoint;
1117   }
1118 }
1119
1120 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1121                                   const AttributePoint2DPtr& theStartPointAttr,
1122                                   const AttributePoint2DPtr& theEndPointAttr,
1123                                   std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1124                                   std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1125 {
1126   if (!theFirstPoint.get() || !theSecondPoint.get())
1127     return;
1128
1129   static const double anAngleTol = 1.e-12;
1130
1131   std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1132       theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1133   bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1134
1135   // collect directions to each point
1136   std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1137       new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1138   std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1139       new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1140   std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1141       new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1142
1143   // sort points by their angular values
1144   double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1145   double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1146   double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1147   if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1148     aFirstPtAngle += aPeriod;
1149   if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1150     aSecondPtAngle += aPeriod;
1151
1152   if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1153     std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1154     theFirstPoint = theSecondPoint;
1155     theSecondPoint = aTmpPoint;
1156   }
1157 }
1158
1159 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1160                                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1161 {
1162   std::string anAttributeType = theModifiedAttribute->attributeType();
1163   if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1164     AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1165                                               theModifiedAttribute);
1166     aModifiedAttribute->setValue(thePoint);
1167
1168 #ifdef DEBUG_TRIM
1169     FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1170     std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1171       theModifiedAttribute->id() <<
1172       "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1173 #endif
1174   }
1175 }
1176
1177
1178 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1179                                       const AttributePtr& theSourceAttribute)
1180 {
1181   std::string anAttributeType = theModifiedAttribute->attributeType();
1182   if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1183     AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1184                                               theModifiedAttribute);
1185     AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1186                                               theSourceAttribute);
1187
1188     if (aModifiedAttribute.get() && aSourceAttribute.get())
1189       aModifiedAttribute->setValue(aSourceAttribute->pnt());
1190   }
1191   else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1192     AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1193                                               theModifiedAttribute);
1194     AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1195                                               theSourceAttribute);
1196
1197     if (aModifiedAttribute.get() && aSourceAttribute.get())
1198       aModifiedAttribute->setValue(aSourceAttribute->value());
1199   }
1200   else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
1201     AttributeRefAttrPtr aRefAttributeToFill = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1202                                                                              theModifiedAttribute);
1203     AttributeRefAttrPtr aSourceRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1204                                          theSourceAttribute);
1205     if (!aSourceRefAttr.get())
1206       aRefAttributeToFill->setAttr(theSourceAttribute);
1207     else {
1208       if (aSourceRefAttr->isObject())
1209         aRefAttributeToFill->setObject(aSourceRefAttr->object());
1210       else
1211         aRefAttributeToFill->setAttr(aSourceRefAttr->attr());
1212     }
1213   }
1214 }
1215
1216 FeaturePtr SketchPlugin_Trim::createLineFeature(const FeaturePtr& theBaseFeature,
1217                                         const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1218                                         const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1219 {
1220 #ifdef DEBUG_TRIM
1221   std::cout << "---- createLineFeature ---" << std::endl;
1222 #endif
1223
1224   FeaturePtr aFeature;
1225   SketchPlugin_Sketch* aSketch = sketch();
1226   if (!aSketch || !theBaseFeature.get())
1227     return aFeature;
1228
1229   aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1230
1231   fillPointAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPoint);
1232   fillPointAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPoint);
1233
1234   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1235                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1236
1237   aFeature->execute(); // to obtain result
1238
1239 #ifdef DEBUG_TRIM
1240   std::cout << "---- createLineFeature:end ---" << std::endl;
1241 #endif
1242
1243   return aFeature;
1244 }
1245
1246 FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
1247                                                const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1248                                                const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1249 {
1250   FeaturePtr aFeature;
1251   SketchPlugin_Sketch* aSketch = sketch();
1252   if (!aSketch || !theBaseFeature.get())
1253     return aFeature;
1254
1255   std::string aCenterAttributeId;
1256   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1257     aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1258   else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1259     aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1260
1261   if (aCenterAttributeId.empty())
1262     return aFeature;
1263
1264 #ifdef DEBUG_TRIM
1265   std::cout << "---- createArcFeature ---" << std::endl;
1266 #endif
1267
1268   aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1269   // update fillet arc: make the arc correct for sure, so, it is not needed to process
1270   // the "attribute updated"
1271   // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1272   bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1273
1274   fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1275                 theBaseFeature->attribute(aCenterAttributeId));
1276   fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPoint);
1277   fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPoint);
1278
1279   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1280                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1281
1282   /// fill referersed state of created arc as it is on the base arc
1283   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1284     bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1285     aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1286   }
1287   aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
1288   aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1289
1290   #ifdef DEBUG_TRIM
1291   std::cout << "---- createArcFeature:end ---" << std::endl;
1292   #endif
1293
1294   return aFeature;
1295 }
1296
1297 FeaturePtr SketchPlugin_Trim::createConstraint(const std::string& theConstraintId,
1298                                                const AttributePtr& theFirstAttribute,
1299                                                const AttributePtr& theSecondAttribute)
1300 {
1301   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1302   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1303                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1304   aRefAttr->setAttr(theFirstAttribute);
1305
1306   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1307                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1308   aRefAttr->setAttr(theSecondAttribute);
1309
1310 #ifdef DEBUG_TRIM
1311   std::cout << "<createConstraint to attribute> :"
1312             << "first attribute - " << theFirstAttribute->id()
1313             << "second attribute - " << theSecondAttribute->id()
1314             << std::endl;
1315 #endif
1316
1317   return aConstraint;
1318 }
1319
1320 FeaturePtr SketchPlugin_Trim::createConstraintToObject(const std::string& theConstraintId,
1321                                                const AttributePtr& theFirstAttribute,
1322                                                const ObjectPtr& theSecondObject)
1323 {
1324   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1325   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1326                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1327   aRefAttr->setAttr(theFirstAttribute);
1328
1329   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1330                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1331   aRefAttr->setObject(theSecondObject);
1332
1333 #ifdef DEBUG_TRIM
1334   std::cout << "<createConstraint to attribute> :"
1335             << "first attribute - " << theFirstAttribute->id()
1336             << "second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
1337             << std::endl;
1338 #endif
1339
1340   return aConstraint;
1341 }
1342
1343 FeaturePtr SketchPlugin_Trim::createConstraintForObjects(
1344                                                     const std::string& theConstraintId,
1345                                                     const ObjectPtr& theFirstObject,
1346                                                     const ObjectPtr& theSecondObject)
1347 {
1348   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1349   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1350                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1351   aRefAttr->setObject(theFirstObject);
1352
1353   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1354                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1355   aRefAttr->setObject(theSecondObject);
1356
1357   return aConstraint;
1358 }
1359
1360 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
1361                                     const std::shared_ptr<ModelAPI_Feature>& theFeature)
1362 {
1363   std::shared_ptr<ModelAPI_Result> aResult;
1364
1365   std::string aFeatureKind = theFeature->getKind();
1366   if (aFeatureKind == SketchPlugin_Line::ID())
1367     aResult = theFeature->firstResult();
1368   else if (aFeatureKind == SketchPlugin_Arc::ID())
1369     aResult = theFeature->lastResult();
1370   else if (aFeatureKind == SketchPlugin_Circle::ID())
1371     aResult = theFeature->lastResult();
1372
1373   return aResult;
1374 }
1375
1376 //********************************************************************
1377 void SketchPlugin_Trim::fillObjectShapes(const ObjectPtr& theObject,
1378                 const ObjectPtr& theSketch,
1379                 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
1380                 std::map<ObjectPtr, PointToRefsMap>& theObjectToPoints)
1381 {
1382   PointToRefsMap aPointsInfo;
1383
1384   std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
1385   std::map<std::shared_ptr<GeomAPI_Pnt>,
1386                            std::list< AttributePoint2DPtr > > aPointToAttributes;
1387   std::map<std::shared_ptr<GeomAPI_Pnt>,
1388                            std::list< ObjectPtr > > aPointToObjects;
1389
1390   std::set<AttributePoint2DPtr > aRefAttributes;
1391   // current feature
1392   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1393   std::set<ResultPtr> anEdgeShapes;
1394   // edges on feature
1395   ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1396   if (!anEdgeShapes.empty()) {
1397     GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
1398
1399     // coincidences to the feature
1400     ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
1401                          aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1402     // layed on feature coincidences to divide it on several shapes
1403     std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1404     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1405         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1406     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1407         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1408     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1409         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1410     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1411
1412     ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
1413                                                 aX->dir(), aY, aPointsInfo);
1414
1415     std::list<FeaturePtr> aFeatures;
1416     CompositeFeaturePtr aSketchComposite =
1417                          std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
1418     for (int i = 0; i < aSketchComposite->numberOfSubs(); i++) {
1419       FeaturePtr aFeature = aSketchComposite->subFeature(i);
1420       if (aFeature.get() && aFeature->getKind() != SketchPlugin_Projection::ID())
1421         aFeatures.push_back(aFeature);
1422     }
1423     ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPointsInfo);
1424
1425     GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPointsInfo, aShapes);
1426   }
1427   theObjectToPoints[theObject] = aPointsInfo;
1428   theCashedShapes[theObject] = aShapes;
1429 }