Salome HOME
Issue #2299: Wrong face support of sketch in script from python dump
[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, aFeaturesToDelete))
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                                 std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete)
538 {
539   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
540   if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
541     return false;
542
543   AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
544   if (!aRefPointAttr.get())
545     return false;
546   std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
547
548   std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
549                                                 aLast = theFurtherCoincidences.end();
550   bool aFoundPoint = false;
551   for (; anIt != aLast && !aFoundPoint; anIt++) {
552     AttributePoint2DPtr aPointAttribute = (*anIt);
553     std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
554     if (aPoint2d->isEqual(aRefPnt2d)) {
555       // create new coincidence and then remove the old one
556       createConstraint(SketchPlugin_ConstraintCoincidence::ID(), aRefPointAttr, aPointAttribute);
557       theFeaturesToDelete.insert(aFeature);
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() && aFeature->getKind() != SketchPlugin_Projection::ID())
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 }