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