Salome HOME
7fa6ffbc5cb151bbd1a12093036692795505202a
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Split.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_Split.h"
22
23 #include <Events_Message.h>
24
25 #include <GeomAPI_Dir2d.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Pnt2d.h>
28 #include <GeomAPI_XY.h>
29 #include <GeomDataAPI_Point2D.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomAlgoAPI_CompoundBuilder.h>
32
33 #include <ModelAPI_AttributeBoolean.h>
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_AttributeRefAttr.h>
36 #include <ModelAPI_AttributeReference.h>
37 #include <ModelAPI_AttributeString.h>
38 #include <ModelAPI_Events.h>
39 #include <ModelAPI_Validator.h>
40 #include <ModelAPI_Session.h>
41 #include <ModelAPI_Tools.h>
42
43 #include <ModelGeomAlgo_Shape.h>
44
45 #include <SketchPlugin_Arc.h>
46 #include <SketchPlugin_Circle.h>
47 #include <SketchPlugin_ConstraintCoincidence.h>
48 #include <SketchPlugin_ConstraintEqual.h>
49 #include <SketchPlugin_ConstraintLength.h>
50 #include <SketchPlugin_ConstraintMiddle.h>
51 #include <SketchPlugin_ConstraintMirror.h>
52 #include <SketchPlugin_ConstraintParallel.h>
53 #include <SketchPlugin_ConstraintTangent.h>
54 #include <SketchPlugin_Line.h>
55 #include <SketchPlugin_MultiRotation.h>
56 #include <SketchPlugin_MultiTranslation.h>
57 #include <SketchPlugin_Point.h>
58 #include <SketchPlugin_Tools.h>
59
60 #include <ModelGeomAlgo_Point2D.h>
61 #include <ModelAPI_EventReentrantMessage.h>
62 #include <Events_Loop.h>
63
64 #include <cmath>
65
66 //#define CREATE_CONSTRAINTS
67
68 #ifdef DEBUG_SPLIT
69 #include <iostream>
70 #endif
71
72 static const double PI = 3.141592653589793238463;
73
74 SketchPlugin_Split::SketchPlugin_Split()
75 {
76 }
77
78 void SketchPlugin_Split::initAttributes()
79 {
80   data()->addAttribute(SELECTED_OBJECT(), ModelAPI_AttributeReference::typeId());
81   data()->addAttribute(SELECTED_POINT(), GeomDataAPI_Point2D::typeId());
82
83   data()->addAttribute(PREVIEW_POINT(), GeomDataAPI_Point2D::typeId());
84   data()->addAttribute(PREVIEW_OBJECT(), ModelAPI_AttributeReference::typeId());
85
86   data()->attribute(PREVIEW_POINT())->setIsArgument(false);
87   data()->attribute(SELECTED_POINT())->setIsArgument(false);
88   data()->attribute(PREVIEW_OBJECT())->setIsArgument(false);
89
90   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_POINT());
91   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_OBJECT());
92
93   // TODO: remove
94   //data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeReference::typeId());
95   //data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
96   //data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
97 }
98
99 void SketchPlugin_Split::execute()
100 {
101   std::shared_ptr<ModelAPI_Data> aData = data();
102
103   // Check the base objects are initialized.
104   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
105                                                            data()->attribute(SELECTED_OBJECT()));
106   //ObjectPtr aBaseObject = anObjectAttr->value();
107   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
108   //                                          aData->attribute(SketchPlugin_Constraint::VALUE()));
109   if(!aBaseObjectAttr->isInitialized()) {
110     setError("Error: Base object is not initialized.");
111     return;
112   }
113   ObjectPtr aBaseObject = aBaseObjectAttr->value();
114   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
115   //  getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
116   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
117   //  getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
118   if (!aFirstPointAttrOfSplit.get() || !aFirstPointAttrOfSplit->isInitialized() ||
119       !aSecondPointAttrOfSplit.get() || !aSecondPointAttrOfSplit->isInitialized()) {
120     setError("Error: Sub-shape is not initialized.");
121     return;
122   }
123
124   /// Remove reference of this feature to feature used in preview, it is not necessary anymore
125   /// as trim will be removed after execute
126   AttributeReferencePtr aPreviewObjectAttr =
127                      std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
128                      data()->attribute(PREVIEW_OBJECT()));
129
130   ObjectPtr aPreviewObject = aPreviewObjectAttr->value();
131   AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
132                                            data()->attribute(PREVIEW_POINT()));
133   std::shared_ptr<GeomAPI_Pnt2d> aPreviewPnt2d = aPoint->pnt();
134   // nullify pointer of preview attribute
135   aPreviewObjectAttr->setValue(ResultPtr());
136   bool anIsEqualPreviewAndSelected = aPreviewObject == aBaseObject;
137
138   // Wait all constraints being created, then send update events
139   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
140   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
141   if (isUpdateFlushed)
142     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
143
144   // Find feature constraints
145   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
146   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
147   std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
148
149   //std::map<FeaturePtr, IdToPointPair> aTangentFeatures;
150   std::map<FeaturePtr, IdToPointPair> aCoincidenceToFeature;
151   getConstraints(aFeaturesToDelete, aFeaturesToUpdate, /*aTangentFeatures, */
152                  aCoincidenceToFeature);
153
154   std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
155   std::list<AttributePtr> aRefsToFeature;
156   getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
157
158   std::map<AttributePtr, AttributePtr> aBasePointModifiedAttributes;
159
160 #ifdef DEBUG_SPLIT
161   std::cout << std::endl;
162   std::cout << "SketchPlugin_Split::execute()" << std::endl;
163   std::cout << std::endl;
164
165   SketchPlugin_Sketch* aSketch = sketch();
166   std::cout << "SKETCH FEATURES (before split) [" << aSketch->numberOfSubs() << "]:" << std::endl;
167   for (int i = 0, aNbSubs = aSketch->numberOfSubs(); i < aNbSubs; i++) {
168     std::cout << getFeatureInfo(aSketch->subFeature(i), false) << std::endl;
169   }
170
171   std::cout << std::endl;
172   std::cout << "---- IN PARAMETERS ----" << std::endl;
173   std::cout << "Base feature:" << getFeatureInfo(aBaseFeature) << std::endl;
174   std::cout << std::endl;
175
176   if (!aCoincidenceToFeature.empty()) {
177     std::cout << "Coincidences to base feature[" <<
178       aCoincidenceToFeature.size() << "]: " << std::endl;
179     std::map<FeaturePtr, IdToPointPair>::const_iterator anIt = aCoincidenceToFeature.begin(),
180                                                         aLast = aCoincidenceToFeature.end();
181     for (int i = 1; anIt != aLast; anIt++, i++) {
182       FeaturePtr aFeature = (*anIt).first;
183       std::string anAttributeId = (*anIt).second.first;
184       std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = (*anIt).second.second;
185
186       std::cout << i << "-" << getFeatureInfo(aFeature) << std::endl;
187       std::cout <<     " -Attribute to correct:" << anAttributeId << std::endl;
188       std::cout <<     " -Point attribute:" <<
189         ModelGeomAlgo_Point2D::getPointAttributeInfo(aPointAttr) << std::endl;
190     }
191   }
192
193   std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
194     aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
195   std::cout << std::endl << "References to attributes of base feature [" <<
196     aBaseRefAttributes.size() << "]" << std::endl;
197   for (; aRefIt != aRefLast; aRefIt++) {
198     AttributePtr aBaseAttr = aRefIt->first;
199     std::list<AttributePtr> aRefAttributes = aRefIt->second;
200     std::string aRefsInfo;
201     std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
202                                             aRefAttrLast = aRefAttributes.end();
203     for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
204       if (!aRefsInfo.empty())
205         aRefsInfo.append(",");
206       AttributePtr aRAttr = *aRefAttrIt;
207       aRefsInfo.append(aRAttr->id());
208       FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
209       aRefsInfo.append("(" + aRFeature->name() + ") ");
210     }
211     std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
212       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
213     std::cout << aPointAttr->id().c_str() <<
214       ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
215   }
216   std::cout << std::endl;
217   std::cout << std::endl << "References to base feature [" <<
218     aRefsToFeature.size() << "]" << std::endl;
219   std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
220                                           aRefAttrLast = aRefsToFeature.end();
221   std::string aRefsInfo;
222   for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
223     if (!aRefsInfo.empty())
224       aRefsInfo.append(",");
225     AttributePtr aRAttr = *aRefAttrIt;
226     aRefsInfo.append(aRAttr->id());
227     FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
228     aRefsInfo.append("(" + aRFeature->name() + ") ");
229   }
230   std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
231
232
233   std::cout << std::endl;
234   std::cout << "---- SPLIT ----" << std::endl;
235   std::cout << std::endl;
236 #endif
237
238   std::string aFeatureKind = aBaseFeature->getKind();
239   FeaturePtr aSplitFeature, anAfterFeature;
240   std::set<AttributePoint2DPtr> aFurtherCoincidences;
241   std::set<FeaturePtr> aCreatedFeatures;
242   std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
243   FeaturePtr aReplacingFeature, aNewFeature;
244   if (aFeatureKind == SketchPlugin_Line::ID())
245     aNewFeature = splitLine(aSplitFeature, aBaseFeature, anAfterFeature,
246                             aFurtherCoincidences, aCreatedFeatures, aModifiedAttributes);
247   else if (aFeatureKind == SketchPlugin_Arc::ID())
248     aNewFeature = splitArc(aSplitFeature, aBaseFeature, anAfterFeature, aFurtherCoincidences,
249                            aCreatedFeatures, aModifiedAttributes);
250   if (aFeatureKind == SketchPlugin_Circle::ID()) {
251     FeaturePtr aCircleFeature = aBaseFeature;
252     aReplacingFeature = splitCircle(aSplitFeature, aBaseFeature, anAfterFeature,
253                                     aFurtherCoincidences, aCreatedFeatures, aModifiedAttributes);
254
255     updateRefFeatureConstraints(getFeatureResult(aBaseFeature), aRefsToFeature);
256
257     AttributePtr aCenterAttr = aCircleFeature->attribute(SketchPlugin_Circle::CENTER_ID());
258     aFeaturesToDelete.insert(aCircleFeature);
259     // as circle is removed, temporary fill this attribute*/
260     aBaseObjectAttr->setObject(ResultPtr());
261   }
262
263 #ifdef DEBUG_SPLIT
264   std::cout << "---- OUT PARAMETERS ----" << std::endl;
265   std::cout << "Base modified feature:" << getFeatureInfo(aBaseFeature) << std::endl;
266   std::cout << "Split feature:" << getFeatureInfo(aSplitFeature) << std::endl;
267   std::cout << "After feature:" << getFeatureInfo(anAfterFeature) << std::endl;
268   std::cout << std::endl;
269
270   std::cout << "Created features by split:[" << aCreatedFeatures.size() << "]" << std::endl;
271   std::set<FeaturePtr>::const_iterator aFIt = aCreatedFeatures.begin(),
272                                        aFLast = aCreatedFeatures.end();
273   for (; aFIt != aFLast; aFIt++) {
274     std::cout << getFeatureInfo(*aFIt) << std::endl;
275   }
276   std::cout << std::endl;
277
278   std::cout << "Attributes for further Coincidences:" << std::endl;
279   std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
280                                                 aLast = aFurtherCoincidences.end();
281   for (; anIt != aLast; anIt++) {
282     AttributePtr anAttribute = *anIt;
283     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
284     std::cout << ModelGeomAlgo_Point2D::getPointAttributeInfo(anAttribute)
285               << " [" << getFeatureInfo(aFeature, false) << "]" << std::endl;
286   }
287
288   std::cout << "Modifed attributes (constraints to attributes are moved here):" << std::endl;
289   std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
290     aPIt = aModifiedAttributes.begin(), aPLast = aModifiedAttributes.end();
291   std::string aResInfo;
292   for (; aPIt != aPLast; aPIt++) {
293     if (!aResInfo.empty())
294       aResInfo += "\n";
295
296     std::pair<AttributePtr, AttributePtr> aPair = *aPIt;
297
298     AttributePtr anAttr = aPair.first;
299     aResInfo.append(anAttr->id());
300     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->owner());
301     aResInfo.append("(" + aFeature->name() + ") ");
302
303     aResInfo.append("  - is modified to -  ");
304
305     anAttr = aPair.second;
306     aResInfo.append(anAttr->id());
307     aFeature = ModelAPI_Feature::feature(anAttr->owner());
308     aResInfo.append("(" + aFeature->name() + ") ");
309   }
310   std::cout << aResInfo << std::endl;
311 #endif
312
313   std::set<ResultPtr> aFeatureResults;
314   aFeatureResults.insert(getFeatureResult(aBaseFeature));
315   if (anAfterFeature.get() && anAfterFeature != aBaseFeature)
316     aFeatureResults.insert(getFeatureResult(anAfterFeature));
317
318   // coincidence to feature
319   updateCoincidenceConstraintsToFeature(aCoincidenceToFeature, aFurtherCoincidences,
320                                         aFeatureResults, aSplitFeature, aFeaturesToDelete);
321
322   updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes);
323
324   // delete constraints
325 #ifdef DEBUG_SPLIT
326   std::cout << "remove features and references:" << std::endl;
327   std::set<FeaturePtr>::const_iterator aDIt = aFeaturesToDelete.begin(),
328                                        aDLast = aFeaturesToDelete.end();
329   for (; aDIt != aDLast; aDIt++) {
330     std::cout << getFeatureInfo(*aDIt, false) << std::endl;
331     std::cout << std::endl;
332   }
333 #endif
334   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
335   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
336
337 #ifdef DEBUG_SPLIT
338   std::cout << "update features after split:" << std::endl;
339   std::set<FeaturePtr>::const_iterator anUIt = aFeaturesToUpdate.begin(),
340                                        anULast = aFeaturesToUpdate.end();
341   for (; anUIt != anULast; anUIt++) {
342     std::cout << getFeatureInfo(*anUIt, false) << std::endl;
343     std::cout << std::endl;
344   }
345 #endif
346   updateFeaturesAfterSplit(aFeaturesToUpdate);
347
348   // Send events to update the sub-features by the solver.
349   if(isUpdateFlushed) {
350     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
351   }
352
353     if (anIsEqualPreviewAndSelected) {
354     // equal preview and selected objects
355     // nothing to do if the preview and selected objects are different
356     ResultPtr aReplacingResult;
357     if (aReplacingFeature.get()) {
358       aReplacingFeature->execute(); // need it to obtain result
359       aReplacingResult = getFeatureResult(aReplacingFeature);
360     }
361     if (aReplacingResult.get()) { // base object was removed
362       aPreviewObject = aReplacingResult;
363       //aMessage->setSelectedObject(aReplacingResult);
364
365       //GeomShapePtr aSelectedShape = aReplacingResult->shape();
366       //std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
367       //                                                          aPreviewPnt2d->y());
368       //std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
369       //if (ModelGeomAlgo_Point2D::isPointOnEdge(aSelectedShape, aPreviewPnt, aProjectedPoint)) {
370         //bool aValue = true;
371       //}
372       //aBaseShape = aShape;
373
374 #ifdef DEBUG_SPLIT
375       if (!aSelectedShape.get())
376         std::cout << "Set empty selected object" << std::endl;
377       else
378         std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
379 #endif
380       //bool aValue = true;
381     }
382     else {
383       aPreviewObject = ObjectPtr();
384
385       aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
386       aBaseObject = getFeatureResult(aBaseFeature);
387       std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
388                                                                 aPreviewPnt2d->y());
389       ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
390       if (aBaseResult) {
391         GeomShapePtr aShape = aBaseResult->shape();
392         std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
393         if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
394           aPreviewObject = aBaseResult;
395       }
396       if (!aPreviewObject.get() && aNewFeature.get()) {
397         ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
398         if (aNewFeatureResult.get()) {
399           GeomShapePtr aShape = aNewFeatureResult->shape();
400           std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
401           if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
402             aPreviewObject = aNewFeatureResult;
403         }
404       }
405     }
406   }
407   if (aPreviewObject.get()) {
408     std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
409       <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
410                                            ModelAPI_EventReentrantMessage::eventId(), this));
411     aMessage->setSelectedObject(aPreviewObject);
412     Events_Loop::loop()->send(aMessage);
413   }
414
415
416 #ifdef DEBUG_SPLIT
417   std::cout << "SKETCH FEATURES (after split) [" << aSketch->numberOfSubs() << "]:" << std::endl;
418   for (int i = 0, aNbSubs = aSketch->numberOfSubs(); i < aNbSubs; i++) {
419     std::cout << getFeatureInfo(aSketch->subFeature(i), false) << std::endl;
420   }
421 #endif
422 }
423
424 // LCOV_EXCL_START
425 std::string SketchPlugin_Split::processEvent(const std::shared_ptr<Events_Message>& theMessage)
426 {
427 #ifdef DEBUG_SPLIT
428   std::cout << "SketchPlugin_Split::processEvent:" << data()->name() << std::endl;
429 #endif
430   std::string aFilledAttributeName;
431
432   std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
433         std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
434   if (aMessage.get()) {
435     ObjectPtr anObject = aMessage->selectedObject();
436     std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
437
438     if (anObject.get() && aPoint.get()) {
439       //if (myCashedShapes.find(anObject) == myCashedShapes.end())
440       //  fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
441       if (myCashedShapes.find(anObject) == myCashedShapes.end())
442         fillObjectShapes(anObject, sketch()->data()->owner());
443       const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
444       if (aShapes.size() > 1) {
445         std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
446                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
447                               data()->attribute(SELECTED_OBJECT()));
448         std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
449                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
450                               data()->attribute(PREVIEW_OBJECT()));
451         aRefSelectedAttr->setValue(anObject);
452         aRefPreviewAttr->setValue(anObject);
453
454         std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
455                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
456                               data()->attribute(SELECTED_POINT()));
457         std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
458                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
459                               data()->attribute(PREVIEW_POINT()));
460         aPointSelectedAttr->setValue(aPoint);
461         aPointPreviewAttr->setValue(aPoint);
462
463         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
464
465         GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
466         if (aSelectedShape.get()) {
467           aFilledAttributeName = SELECTED_OBJECT();
468         }
469         else {
470           // #2480 - sub shape is not initialized when split sketch
471           // If restarted operation use some selection on the shape that is split and
472           // result selectiona can not participate in new split(checked shape above is null),
473           // reset filled values of selection set in this method above
474           aRefSelectedAttr->setValue(ResultPtr());
475           aRefPreviewAttr->setValue(ResultPtr());
476         }
477   #ifdef DEBUG_SPLIT
478         if (!aSelectedShape.get())
479           std::cout << "Set empty selected object" << std::endl;
480         else
481           std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
482   #endif
483       }
484     }
485   }
486   return aFilledAttributeName;
487 }
488 // LCOV_EXCL_STOP
489
490 AISObjectPtr SketchPlugin_Split::getAISObject(AISObjectPtr thePrevious)
491 {
492   /*AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
493                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
494   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
495
496   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointOfRefAttr(
497                                         data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
498   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointOfRefAttr(
499                                         data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
500
501   if (aBaseObjectAttr->isInitialized() && aBaseFeature.get() &&
502       aFirstPointAttrOfSplit->isInitialized() &&
503       aSecondPointAttrOfSplit->isInitialized()) {
504
505     ResultPtr aResult = getFeatureResult(aBaseFeature);
506     GeomShapePtr aBaseShape = aResult->shape();
507     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
508
509     std::shared_ptr<GeomAPI_Pnt2d> aStartPnt2d = aFirstPointAttrOfSplit->pnt();
510     std::shared_ptr<GeomAPI_Pnt> aStartPoint = sketch()->to3D(aStartPnt2d->x(), aStartPnt2d->y());
511     aPoints.push_back(aStartPoint);
512
513     std::shared_ptr<GeomAPI_Pnt2d> aSecondPnt2d = aSecondPointAttrOfSplit->pnt();
514     std::shared_ptr<GeomAPI_Pnt> aSecondPoint =
515       sketch()->to3D(aSecondPnt2d->x(), aSecondPnt2d->y());
516     aPoints.push_back(aSecondPoint);
517
518     std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
519
520     GeomAlgoAPI_ShapeTools::splitShape_p(aBaseShape, aPoints, aSplitShapes);
521     std::shared_ptr<GeomAPI_Shape> aShape =
522       GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
523
524     AISObjectPtr anAIS = thePrevious;
525     if (aShape) {
526       if (!anAIS)
527         anAIS = AISObjectPtr(new GeomAPI_AISObject);
528       anAIS->createShape(aShape);
529       std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
530              aBaseFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID());
531
532       bool isConstruction = anAuxiliaryAttr.get() != NULL && anAuxiliaryAttr->value();
533
534       std::vector<int> aColor;
535       double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
536       int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
537       if (isConstruction) {
538         aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color");
539         aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH_AUXILIARY();
540         aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY();
541       }
542       else {
543         aColor = Config_PropManager::color("Visualization", "sketch_entity_color");
544       }
545       anAIS->setColor(aColor[0], aColor[1], aColor[2]);
546       anAIS->setWidth(aWidth + 1);
547       anAIS->setLineStyle(aLineStyle);
548     }
549     return anAIS;
550   }
551   return AISObjectPtr();*/
552 #ifdef DEBUG_SPLIT
553   std::cout << "SketchPlugin_Split::getAISObject: " << data()->name() << std::endl;
554 #endif
555
556   AISObjectPtr anAIS = thePrevious;
557
558   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
559   GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
560   if (aPreviewShape.get())
561     aShapes.push_back(aPreviewShape);
562   GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
563   if (aSelectedShape.get())
564     aShapes.push_back(aSelectedShape);
565
566   if (aShapes.empty())
567     return AISObjectPtr();
568
569   GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
570   if (!aBaseShape.get())
571     return AISObjectPtr();
572
573   if (aBaseShape.get()) {
574     if (!anAIS)
575       anAIS = AISObjectPtr(new GeomAPI_AISObject);
576     anAIS->createShape(aBaseShape);
577
578     std::vector<int> aColor;
579     aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
580     double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
581     int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
582     anAIS->setColor(aColor[0], aColor[1], aColor[2]);
583     // width when there is not base object should be extened in several points
584     // in order to see this preview over highlight
585     anAIS->setWidth(aWidth+4);
586     anAIS->setLineStyle(aLineStyle);
587   }
588   else
589     anAIS = AISObjectPtr();
590   return anAIS;
591 }
592
593 //********************************************************************
594 void SketchPlugin_Split::fillObjectShapes(const ObjectPtr& theObject,
595                                           const ObjectPtr& theSketch)
596 {
597   std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
598   std::map<std::shared_ptr<GeomDataAPI_Point2D>, std::shared_ptr<GeomAPI_Pnt> > aPointToAttributes;
599   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
600   // current feature
601   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
602   // edges on feature
603   std::set<ResultPtr> anEdgeResults;
604   ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeResults);
605   if (!anEdgeResults.empty()) {
606     GeomShapePtr aFeatureShape = (*anEdgeResults.begin())->shape();
607
608     // coincidences to the feature
609     ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
610                          aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
611     // layed on feature coincidences to divide it on several shapes
612     std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
613     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
614         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
615     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
616         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
617     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
618         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
619     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
620     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
621     ModelGeomAlgo_Point2D::getPointsInsideShape_p(aFeatureShape, aRefAttributes, aC->pnt(),
622                                                 aX->dir(), aY, aPoints, aPointToAttributes);
623
624     if (!aPoints.empty())
625       GeomAlgoAPI_ShapeTools::splitShape_p(aFeatureShape, aPoints, aShapes);
626   }
627   myCashedShapes[theObject] = aShapes;
628   myCashedReferences[theObject] = aPointToAttributes;
629 }
630
631 GeomShapePtr SketchPlugin_Split::getSubShape(const std::string& theObjectAttributeId,
632                                              const std::string& thePointAttributeId)
633 {
634   GeomShapePtr aBaseShape;
635
636   AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
637                                                        data()->attribute(theObjectAttributeId));
638   ObjectPtr aBaseObject = anObjectAttr->value();
639   if (!aBaseObject.get())
640     return aBaseShape;
641
642   // point on feature
643   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
644                                            data()->attribute(thePointAttributeId));
645   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
646   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
647                                                                anAttributePnt2d->y());
648
649 #ifdef TRIM_SHAPE
650   if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
651     fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
652
653   const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
654   if (!aShapes.empty()) {
655     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
656     for (; anIt != aLast; anIt++) {
657       GeomShapePtr aShape = *anIt;
658       std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
659       if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
660         aBaseShape = aShape;
661     }
662   }
663 #else
664   if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
665     fillObjectShapes(aBaseObject, sketch()->data()->owner());
666
667   std::shared_ptr<GeomAPI_Pnt> aStartPoint;
668   std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
669   const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
670   std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
671   for (; anIt != aLast; anIt++) {
672     GeomShapePtr aCurrentShape = *anIt;
673     std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
674     if (ModelGeomAlgo_Point2D::isPointOnEdge(aCurrentShape, anAttributePnt, aProjectedPoint)) {
675       if (aCurrentShape->shapeType() == GeomAPI_Shape::EDGE) {
676         std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aCurrentShape));
677         aStartPoint = anEdge->firstPoint();
678         aSecondPoint = anEdge->lastPoint();
679       }
680       break;
681     }
682   }
683
684   if (!aStartPoint.get() || !aSecondPoint.get())
685     return aBaseShape;
686
687   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
688   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
689   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject/*aBaseObjectAttr->value()*/);
690
691   //AttributePoint2DPtr aFirstPointAttrOfSplit = getPointOfRefAttr(
692   //                                      data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
693   //AttributePoint2DPtr aSecondPointAttrOfSplit = getPointOfRefAttr(
694   //                                      data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
695   if (anObjectAttr->isInitialized() && aBaseFeature.get() && aPointAttr->isInitialized()) {
696       //aFirstPointAttrOfSplit->isInitialized() &&
697       //aSecondPointAttrOfSplit->isInitialized()) {
698     ResultPtr aResult = getFeatureResult(aBaseFeature);
699     GeomShapePtr aResultShape = aResult->shape();
700     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
701
702     //std::shared_ptr<GeomAPI_Pnt2d> aStartPnt2d = aFirstPointAttrOfSplit->pnt();
703     //std::shared_ptr<GeomAPI_Pnt> aStartPoint = sketch()->to3D(aStartPnt2d->x(), aStartPnt2d->y());
704     aPoints.push_back(aStartPoint);
705
706     //std::shared_ptr<GeomAPI_Pnt2d> aSecondPnt2d = aSecondPointAttrOfSplit->pnt();
707     //std::shared_ptr<GeomAPI_Pnt> aSecondPoint =
708     //  sketch()->to3D(aSecondPnt2d->x(), aSecondPnt2d->y());
709     aPoints.push_back(aSecondPoint);
710
711     std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
712     GeomAlgoAPI_ShapeTools::splitShape_p(aResultShape, aPoints, aSplitShapes);
713     aBaseShape = GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
714 #endif
715   }
716   return aBaseShape;
717 }
718
719 void SketchPlugin_Split::getFeaturePoints(const FeaturePtr& theFeature,
720                                                     AttributePoint2DPtr& theStartPointAttr,
721                                                     AttributePoint2DPtr& theEndPointAttr)
722 {
723   std::string aFeatureKind = theFeature->getKind();
724   std::string aStartAttributeName, anEndAttributeName;
725   if (aFeatureKind == SketchPlugin_Line::ID()) {
726     aStartAttributeName = SketchPlugin_Line::START_ID();
727     anEndAttributeName = SketchPlugin_Line::END_ID();
728   }
729   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
730     aStartAttributeName = SketchPlugin_Arc::START_ID();
731     anEndAttributeName = SketchPlugin_Arc::END_ID();
732   }
733   if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
734     theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
735                                          theFeature->attribute(aStartAttributeName));
736     theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
737                                          theFeature->attribute(anEndAttributeName));
738   }
739 }
740
741 void SketchPlugin_Split::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
742                                     std::set<FeaturePtr>& theFeaturesToUpdate,
743                                     std::map<FeaturePtr, IdToPointPair>& theCoincidenceToFeature)
744 {
745   std::shared_ptr<ModelAPI_Data> aData = data();
746
747   // Check the base objects are initialized.
748   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
749                                                            data()->attribute(SELECTED_OBJECT()));
750   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
751   //                                          aData->attribute(SketchPlugin_Constraint::VALUE()));
752   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
753   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
754
755   std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
756   std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
757   aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
758
759   std::set<AttributePtr>::const_iterator aIt;
760   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
761     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
762     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
763     std::string aRefFeatureKind = aRefFeature->getKind();
764     if (aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() ||
765         aRefFeatureKind == SketchPlugin_MultiRotation::ID() ||
766         aRefFeatureKind == SketchPlugin_MultiTranslation::ID() ||
767         aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
768       theFeaturesToDelete.insert(aRefFeature);
769     else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
770       theFeaturesToUpdate.insert(aRefFeature);
771     else if (aRefFeatureKind == SketchPlugin_ConstraintCoincidence::ID()) {
772       std::string anAttributeToBeModified;
773       AttributePoint2DPtr aCoincidentPoint;
774       AttributeRefAttrPtr anAttrA = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_A());
775       AttributeRefAttrPtr anAttrB = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_B());
776       bool isToFeature = false;
777       if (anAttrA->isObject() || anAttrB->isObject()) { /// coincidence to base feature
778         FeaturePtr aFeature = anAttrA->isObject() ? ModelAPI_Feature::feature(anAttrA->object())
779                                                   : FeaturePtr();
780         isToFeature = aFeature.get() && aFeature == aBaseFeature;
781         anAttributeToBeModified = anAttrA->id();
782         if (!isToFeature) {
783           aFeature = anAttrB->isObject() ? ModelAPI_Feature::feature(anAttrB->object())
784                                          : FeaturePtr();
785           isToFeature = aFeature.get() && aFeature == aBaseFeature;
786           anAttributeToBeModified = anAttrB->id();
787         }
788         if (isToFeature)
789           aCoincidentPoint = SketchPlugin_ConstraintCoincidence::getPoint(aRefFeature);
790       }
791       if (!isToFeature) { /// coincidence to point on base feature
792         AttributePtr anAttribute;
793
794         if (!anAttrA->isObject()) {
795           AttributePtr aCurAttribute = anAttrA->attr();
796           if (aCurAttribute.get()) {
797             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
798             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
799               anAttribute = anAttrB->attr();
800               anAttributeToBeModified = anAttrA->id();
801             }
802           }
803         }
804         if (!anAttribute.get() && !anAttrB->isObject()) {
805           AttributePtr aCurAttribute = anAttrB->attr();
806           if (aCurAttribute.get()) {
807             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
808             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
809               anAttribute = anAttrA->attr();
810               anAttributeToBeModified = anAttrB->id();
811             }
812           }
813         }
814         if (anAttribute.get())
815           aCoincidentPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
816       }
817       if (aCoincidentPoint.get() && isToFeature)
818         theCoincidenceToFeature[aRefFeature] = std::make_pair(anAttributeToBeModified,
819                                                               aCoincidentPoint);
820     }
821   }
822 }
823
824 void SketchPlugin_Split::getRefAttributes(const FeaturePtr& theFeature,
825                                     std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
826                                     std::list<AttributePtr>& theRefsToFeature)
827 {
828   theRefs.clear();
829
830   std::list<AttributePtr> aPointAttributes =
831     theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
832   std::set<AttributePtr> aPointAttributesSet;
833
834   std::list<AttributePtr>::const_iterator aPIt =
835     aPointAttributes.begin(), aPLast = aPointAttributes.end();
836   for (; aPIt != aPLast; aPIt++)
837     aPointAttributesSet.insert(*aPIt);
838
839   std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
840   std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
841   aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
842
843   std::set<AttributePtr>::const_iterator aIt;
844   for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
845     AttributePtr anAttr = (*aIt);
846     FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
847     if (anAttrFeature.get() != this &&
848         anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
849       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
850       if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
851         AttributePtr anAttrInRef = aRefAttr->attr();
852         if (anAttrInRef.get() &&
853             aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
854           if (theRefs.find(anAttrInRef) != theRefs.end())
855             theRefs[anAttrInRef].push_back(aRefAttr);
856           else {
857             std::list<AttributePtr> anAttrList;
858             anAttrList.push_back(aRefAttr);
859             theRefs[anAttrInRef] = anAttrList;
860           }
861         }
862       }
863       else { /// find attributes referenced to feature itself
864         theRefsToFeature.push_back(anAttr);
865       }
866     }
867   }
868 }
869
870 void SketchPlugin_Split::updateCoincidenceConstraintsToFeature(
871       const std::map<std::shared_ptr<ModelAPI_Feature>, IdToPointPair>& theCoincidenceToFeature,
872       const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences,
873       const std::set<ResultPtr>& theFeatureResults,
874       const FeaturePtr& theSplitFeature,
875       std::set<FeaturePtr>& theFeaturesToDelete)
876 {
877   if (theCoincidenceToFeature.empty())
878     return;
879
880   // we should build coincidence constraints to end of the split feature
881   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aNewCoincidencesToSplitFeature;
882   AttributePoint2DPtr aStartPointAttr, anEndPointAttr;
883   getFeaturePoints(theSplitFeature, aStartPointAttr, anEndPointAttr);
884   if (theFurtherCoincidences.find(aStartPointAttr) == theFurtherCoincidences.end())
885     aNewCoincidencesToSplitFeature.insert(aStartPointAttr);
886   if (theFurtherCoincidences.find(anEndPointAttr) == theFurtherCoincidences.end())
887     aNewCoincidencesToSplitFeature.insert(anEndPointAttr);
888
889   std::map<FeaturePtr, IdToPointPair>::const_iterator aCIt = theCoincidenceToFeature.begin(),
890                                                             aCLast = theCoincidenceToFeature.end();
891 #ifdef DEBUG_SPLIT
892   std::cout << std::endl;
893   std::cout << "Coincidences to feature(modified):"<< std::endl;
894 #endif
895   for (; aCIt != aCLast; aCIt++) {
896     FeaturePtr aCoincFeature = aCIt->first;
897     std::string anAttributeId = aCIt->second.first;
898     std::string aSecondAttribute = anAttributeId == SketchPlugin_Constraint::ENTITY_A() ?
899         SketchPlugin_Constraint::ENTITY_B() : SketchPlugin_Constraint::ENTITY_A();
900
901     AttributePoint2DPtr aCoincPoint = aCIt->second.second;
902     std::set<AttributePoint2DPtr>::const_iterator aFCIt = theFurtherCoincidences.begin(),
903                                                   aFCLast = theFurtherCoincidences.end();
904     std::shared_ptr<GeomAPI_Pnt2d> aCoincPnt = aCoincPoint->pnt();
905     AttributePoint2DPtr aFeaturePointAttribute;
906     for (; aFCIt != aFCLast && !aFeaturePointAttribute.get(); aFCIt++) {
907       AttributePoint2DPtr aFCAttribute = *aFCIt;
908       if (aCoincPnt->isEqual(aFCAttribute->pnt()))
909         aFeaturePointAttribute = aFCAttribute;
910     }
911     if (aFeaturePointAttribute.get()) {
912       // create new constraint and remove the current
913       aCoincFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
914           SketchPlugin_ConstraintCoincidence::ID(),
915           aFeaturePointAttribute, aCoincFeature->refattr(aSecondAttribute)->attr());
916       theFeaturesToDelete.insert(aCIt->first);
917       // create new coincidences to split feature points
918       std::set<AttributePoint2DPtr>::const_iterator aSFIt = aNewCoincidencesToSplitFeature.begin(),
919                                                     aSFLast = aNewCoincidencesToSplitFeature.end();
920       for (; aSFIt != aSFLast; aSFIt++) {
921         AttributePoint2DPtr aSFAttribute = *aSFIt;
922         if (aCoincPnt->isEqual(aSFAttribute->pnt())) {
923           SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
924                            SketchPlugin_ConstraintCoincidence::ID(),
925                            aSFAttribute, aCoincFeature->refattr(aSecondAttribute)->attr());
926         }
927       }
928     }
929     else {
930       /// find feature by shape intersected the point
931       ResultPtr aResultForCoincidence = *(theFeatureResults.begin());
932
933       if (theFeatureResults.size() > 1) { // try to find point on additional feature
934         ResultPtr anAddtionalResult = *(theFeatureResults.begin()++);
935         GeomShapePtr aShape = anAddtionalResult->shape();
936
937         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCoincPoint->pnt();
938         std::shared_ptr<GeomAPI_Pnt> aPoint = sketch()->to3D(aPnt2d->x(), aPnt2d->y());
939
940         std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
941         if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPoint, aProjectedPoint))
942           aResultForCoincidence = anAddtionalResult;
943       }
944       aCoincFeature->refattr(anAttributeId)->setObject(aResultForCoincidence);
945     }
946 #ifdef DEBUG_SPLIT
947   std::cout << " -" << getFeatureInfo(aCoincFeature) << std::endl;
948 #endif
949   }
950 }
951
952 void SketchPlugin_Split::updateRefFeatureConstraints(
953                                                   const ResultPtr& theFeatureBaseResult,
954                                                   const std::list<AttributePtr>& theRefsToFeature)
955 {
956   std::list<AttributePtr>::const_iterator anIt = theRefsToFeature.begin(),
957                                           aLast = theRefsToFeature.end();
958   for (; anIt != aLast; anIt++) {
959     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIt);
960     if (aRefAttr.get())
961       aRefAttr->setObject(theFeatureBaseResult);
962   }
963 }
964
965 void SketchPlugin_Split::updateRefAttConstraints(
966                     const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
967                     const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes)
968 {
969 #ifdef DEBUG_SPLIT
970   std::cout << "SketchPlugin_Split::updateRefAttConstraints" << std::endl;
971 #endif
972
973   std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
974     anIt = theModifiedAttributes.begin(),  aLast = theModifiedAttributes.end();
975   for (; anIt != aLast; anIt++) {
976     AttributePtr anAttribute = anIt->first;
977
978     /// not found in references
979     if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
980       continue;
981     std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
982     std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
983                                             aRLast = aRefAttributes.end();
984
985     AttributePtr aNewAttribute = anIt->second;
986     for (; aRefIt != aRLast; aRefIt++) {
987       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
988       if (aRefAttr.get()) {
989         aRefAttr->setAttr(aNewAttribute);
990 #ifdef DEBUG_SPLIT
991         FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->owner());
992         std::cout << " -" << getFeatureInfo(aFeature) << std::endl;
993 #endif
994       }
995     }
996   }
997 }
998
999 FeaturePtr SketchPlugin_Split::splitLine(FeaturePtr& theSplitFeature,
1000                                              FeaturePtr& theBaseFeatureModified,
1001                                              FeaturePtr& theAfterFeature,
1002                                              std::set<AttributePoint2DPtr>& thePoints,
1003                                              std::set<FeaturePtr>& theCreatedFeatures,
1004                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1005 {
1006   FeaturePtr anNewFeature;
1007
1008   std::set<FeaturePtr> aCreatedFeatures;
1009   FeaturePtr aConstraintFeature;
1010   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1011
1012   SketchPlugin_Sketch* aSketch = sketch();
1013   if (!aSketch)
1014     return anNewFeature;
1015
1016   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1017                                                            data()->attribute(SELECTED_OBJECT()));
1018   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1019   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
1020   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1021   std::string aFeatureKind = aBaseFeature->getKind();
1022   if (aFeatureKind != SketchPlugin_Line::ID())
1023     return anNewFeature;
1024
1025   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1026     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1027   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1028     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1029   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1030
1031   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1032   if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
1033     setError("Error: Feature has no start and end points.");
1034     return anNewFeature;
1035   }
1036
1037   arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
1038                       aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1039
1040 #ifdef DEBUG_SPLIT
1041   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1042   std::cout << "Start point: " <<
1043     ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
1044   std::cout << "1st point:   " <<
1045     ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
1046   std::cout << "2nd point:   " <<
1047     ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
1048   std::cout << "End point:   " <<
1049     ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
1050 #endif
1051
1052   /// create a split feature
1053   theSplitFeature =
1054     createLineFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1055   theCreatedFeatures.insert(theSplitFeature);
1056
1057   // before split feature
1058   if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1059     theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
1060                                         theSplitFeature->attribute(SketchPlugin_Line::START_ID())));
1061   }
1062   else {
1063     theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
1064     /// move end arc point to start of split
1065   }
1066
1067   // after split feature
1068   if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
1069     FeaturePtr aFeature;
1070     if (!theBaseFeatureModified.get()) {
1071       aFeature = aBaseFeature; ///< use base feature to store all constraints here
1072       fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), aSecondPointAttrOfSplit);
1073       aFeature->execute(); // to update result
1074     }
1075     else {
1076       aFeature = createLineFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
1077       theCreatedFeatures.insert(aFeature);
1078       theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1079                                              aFeature->attribute(SketchPlugin_Line::END_ID())));
1080       anNewFeature = aFeature;
1081     }
1082     aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1083                      SketchPlugin_ConstraintCoincidence::ID(),
1084                      theSplitFeature->attribute(SketchPlugin_Line::END_ID()),
1085                      aFeature->attribute(SketchPlugin_Line::START_ID()));
1086     theCreatedFeatures.insert(aConstraintFeature);
1087
1088     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1089                                 (aFeature->attribute(SketchPlugin_Line::START_ID())));
1090     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1091                                 (aFeature->attribute(SketchPlugin_Line::END_ID())));
1092
1093     if (!theBaseFeatureModified.get())
1094       theBaseFeatureModified = aFeature;
1095     else
1096       theAfterFeature = aFeature;
1097   }
1098   else {
1099     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1100                                   (theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
1101     theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1102                                    theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
1103   }
1104   // base split, that is defined before split feature should be changed at end
1105   // (after the after feature creation). Otherwise modified value will be used in after feature
1106   // before split feature
1107   if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1108     /// move end arc point to start of split
1109     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
1110                                                     aFirstPointAttrOfSplit);
1111     theBaseFeatureModified->execute(); // to update result
1112     aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1113                      SketchPlugin_ConstraintCoincidence::ID(),
1114                      theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
1115                      theSplitFeature->attribute(SketchPlugin_Line::START_ID()));
1116     theCreatedFeatures.insert(aConstraintFeature);
1117
1118     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1119                              (theBaseFeatureModified->attribute(SketchPlugin_Line::START_ID())));
1120     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1121                                (theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID())));
1122   }
1123   else
1124     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1125                                        (theSplitFeature->attribute(SketchPlugin_Line::START_ID())));
1126
1127 #ifdef CREATE_CONSTRAINTS
1128   // additional constraints between split and base features
1129   aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1130           SketchPlugin_ConstraintParallel::ID(),
1131           getFeatureResult(aBaseFeature),
1132           getFeatureResult(theSplitFeature));
1133   theCreatedFeatures.insert(aConstraintFeature);
1134   if (theAfterFeature.get()) {
1135     aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1136           SketchPlugin_ConstraintParallel::ID(),
1137           getFeatureResult(aBaseFeature),
1138           getFeatureResult(theAfterFeature));
1139     theCreatedFeatures.insert(aConstraintFeature);
1140   }
1141 #endif
1142   return anNewFeature;
1143 }
1144
1145 FeaturePtr SketchPlugin_Split::splitArc(FeaturePtr& theSplitFeature,
1146                                             FeaturePtr& theBaseFeatureModified,
1147                                             FeaturePtr& theAfterFeature,
1148                                             std::set<AttributePoint2DPtr>& thePoints,
1149                                             std::set<FeaturePtr>& theCreatedFeatures,
1150                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1151 {
1152   FeaturePtr anNewFeature;
1153
1154   std::set<FeaturePtr> aCreatedFeatures;
1155   FeaturePtr aConstraintFeature;
1156   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1157
1158   SketchPlugin_Sketch* aSketch = sketch();
1159   if (!aSketch)
1160     return anNewFeature;
1161
1162   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1163                                                            data()->attribute(SELECTED_OBJECT()));
1164   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1165   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
1166   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1167   std::string aFeatureKind = aBaseFeature->getKind();
1168   if (aFeatureKind != SketchPlugin_Arc::ID())
1169     return anNewFeature;
1170
1171   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1172     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1173   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1174     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1175   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1176   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1177   if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
1178     setError("Error: Feature has no start and end points.");
1179     return anNewFeature;
1180   }
1181
1182   arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
1183                      aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1184 #ifdef DEBUG_SPLIT
1185   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1186   std::cout << "Start point: " <<
1187     ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
1188   std::cout << "1st point:   " <<
1189     ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
1190   std::cout << "2nd point:   " <<
1191     ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
1192   std::cout << "End point:   " <<
1193     ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
1194 #endif
1195
1196   /// split feature
1197   theSplitFeature = createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1198   theCreatedFeatures.insert(theSplitFeature);
1199
1200   // before split feature
1201   if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1202     theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
1203                                   theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
1204   }
1205   else {
1206     theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
1207     /// move end arc point to start of split
1208   }
1209
1210   // after split feature
1211   if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
1212     FeaturePtr aFeature;
1213     if (!theBaseFeatureModified.get()) {
1214       aFeature = aBaseFeature; ///< use base feature to store all constraints here
1215       fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), aSecondPointAttrOfSplit);
1216       aFeature->execute(); // to update result
1217     }
1218     else {
1219       aFeature = createArcFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
1220       theCreatedFeatures.insert(aFeature);
1221       theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1222                                                   aFeature->attribute(SketchPlugin_Arc::END_ID())));
1223       anNewFeature = aFeature;
1224     }
1225     aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1226                      SketchPlugin_ConstraintCoincidence::ID(),
1227                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()),
1228                      aFeature->attribute(SketchPlugin_Arc::START_ID()));
1229     theCreatedFeatures.insert(aConstraintFeature);
1230
1231     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1232                                 (aFeature->attribute(SketchPlugin_Arc::START_ID())));
1233     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1234                                 (aFeature->attribute(SketchPlugin_Arc::END_ID())));
1235
1236     if (!theBaseFeatureModified.get())
1237       theBaseFeatureModified = aFeature;
1238     else
1239       theAfterFeature = aFeature;
1240   }
1241   else {
1242     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1243                                   (theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
1244     theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1245                                    theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
1246   }
1247   // base split, that is defined before split feature should be changed at end
1248   // (after the after feature creation). Otherwise modified value will be used in after feature
1249   // before split feature
1250   if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1251     /// move end arc point to start of split
1252     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1253                                                     aFirstPointAttrOfSplit);
1254     theBaseFeatureModified->execute(); // to update result
1255     aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1256                      SketchPlugin_ConstraintCoincidence::ID(),
1257                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1258                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
1259     theCreatedFeatures.insert(aConstraintFeature);
1260
1261     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1262                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
1263     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1264                                (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
1265   }
1266   else
1267     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1268                                        (theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
1269
1270   // additional constraints between split and base features
1271 #ifdef CREATE_CONSTRAINTS
1272   aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1273           SketchPlugin_ConstraintEqual::ID(),
1274           getFeatureResult(aBaseFeature),
1275           getFeatureResult(theSplitFeature));
1276   theCreatedFeatures.insert(aConstraintFeature);
1277   aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1278           SketchPlugin_ConstraintTangent::ID(),
1279           getFeatureResult(theSplitFeature),
1280           getFeatureResult(aBaseFeature));
1281   theCreatedFeatures.insert(aConstraintFeature);
1282   if (theAfterFeature.get()) {
1283     aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1284                 SketchPlugin_ConstraintEqual::ID(),
1285                 getFeatureResult(aBaseFeature),
1286                 getFeatureResult(theAfterFeature));
1287     theCreatedFeatures.insert(aConstraintFeature);
1288     aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1289                 SketchPlugin_ConstraintTangent::ID(),
1290                 getFeatureResult(theSplitFeature),
1291                 getFeatureResult(theAfterFeature));
1292     theCreatedFeatures.insert(aConstraintFeature);
1293   }
1294 #endif
1295   return anNewFeature;
1296 }
1297
1298 FeaturePtr SketchPlugin_Split::splitCircle(FeaturePtr& theSplitFeature,
1299                                                FeaturePtr& theBaseFeatureModified,
1300                                                FeaturePtr& theAfterFeature,
1301                                                std::set<AttributePoint2DPtr>& thePoints,
1302                                                std::set<FeaturePtr>& theCreatedFeatures,
1303                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1304 {
1305   FeaturePtr anNewFeature;
1306
1307   std::set<FeaturePtr> aCreatedFeatures;
1308   FeaturePtr aConstraintFeature;
1309   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1310
1311   SketchPlugin_Sketch* aSketch = sketch();
1312   if (!aSketch)
1313     return anNewFeature;
1314
1315   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1316                                                            data()->attribute(SELECTED_OBJECT()));
1317   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1318   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
1319   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1320   std::string aFeatureKind = aBaseFeature->getKind();
1321   if (aFeatureKind != SketchPlugin_Circle::ID())
1322     return anNewFeature;
1323
1324   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1325     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1326   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1327     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1328
1329   /// split feature
1330   theSplitFeature =
1331     createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1332   bool aSplitReversed = std::dynamic_pointer_cast<SketchPlugin_Arc>(theSplitFeature)->isReversed();
1333   theCreatedFeatures.insert(theSplitFeature);
1334
1335   /// base feature is a left part of the circle
1336   theBaseFeatureModified = createArcFeature(aBaseFeature,
1337     aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1338   anNewFeature = theBaseFeatureModified;
1339   std::dynamic_pointer_cast<SketchPlugin_Arc>(
1340     theBaseFeatureModified)->setReversed(!aSplitReversed);
1341   theBaseFeatureModified->execute();
1342
1343   theModifiedAttributes.insert(
1344     std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1345                   theBaseFeatureModified->attribute(SketchPlugin_Arc::CENTER_ID())));
1346
1347   theCreatedFeatures.insert(theBaseFeatureModified);
1348
1349   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1350                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
1351   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1352                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
1353
1354   // additional constraints between split and base features
1355   aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1356                      SketchPlugin_ConstraintCoincidence::ID(),
1357                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1358                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()));
1359   theCreatedFeatures.insert(aConstraintFeature);
1360   aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1361                      SketchPlugin_ConstraintCoincidence::ID(),
1362                      theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID()),
1363                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
1364   theCreatedFeatures.insert(aConstraintFeature);
1365
1366 #ifdef CREATE_CONSTRAINTS
1367   aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1368                      SketchPlugin_ConstraintTangent::ID(),
1369                      getFeatureResult(theSplitFeature),
1370                      getFeatureResult(theBaseFeatureModified));
1371   theCreatedFeatures.insert(aConstraintFeature);
1372 #endif
1373   return anNewFeature;
1374 }
1375
1376 void SketchPlugin_Split::arrangePointsOnLine(
1377     const AttributePoint2DPtr& theStartPointAttr,
1378     const AttributePoint2DPtr& theEndPointAttr,
1379     AttributePoint2DPtr& theFirstPointAttr,
1380     AttributePoint2DPtr& theLastPointAttr) const
1381 {
1382   // if first point is closer to last point, swap first and last values
1383   if (theStartPointAttr->pnt()->distance(theFirstPointAttr->pnt()) >
1384       theStartPointAttr->pnt()->distance(theLastPointAttr->pnt())) {
1385     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1386     theFirstPointAttr = theLastPointAttr;
1387     theLastPointAttr = aTmpPoint;
1388   }
1389 }
1390
1391 void SketchPlugin_Split::arrangePointsOnArc(
1392     const FeaturePtr& theArc,
1393     const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
1394     const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
1395     std::shared_ptr<GeomDataAPI_Point2D>& theFirstPointAttr,
1396     std::shared_ptr<GeomDataAPI_Point2D>& theSecondPointAttr) const
1397 {
1398   static const double anAngleTol = 1.e-12;
1399
1400   std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1401       theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1402   bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1403
1404   // collect directions to each point
1405   std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1406       new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1407   std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1408       new GeomAPI_Dir2d(theFirstPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1409   std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1410       new GeomAPI_Dir2d(theSecondPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1411
1412   // sort points by their angular values
1413   double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1414   double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1415   double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1416   if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1417     aFirstPtAngle += aPeriod;
1418   if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1419     aSecondPtAngle += aPeriod;
1420
1421   if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1422     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1423     theFirstPointAttr = theSecondPointAttr;
1424     theSecondPointAttr = aTmpPoint;
1425   }
1426 }
1427
1428 void SketchPlugin_Split::fillAttribute(const AttributePtr& theModifiedAttribute,
1429                                                  const AttributePtr& theSourceAttribute)
1430 {
1431   std::string anAttributeType = theModifiedAttribute->attributeType();
1432   if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1433     AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1434                                               theModifiedAttribute);
1435     AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1436                                               theSourceAttribute);
1437
1438     if (aModifiedAttribute.get() && aSourceAttribute.get())
1439       aModifiedAttribute->setValue(aSourceAttribute->pnt());
1440   }
1441   else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1442     AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1443                                               theModifiedAttribute);
1444     AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1445                                               theSourceAttribute);
1446
1447     if (aModifiedAttribute.get() && aSourceAttribute.get())
1448       aModifiedAttribute->setValue(aSourceAttribute->value());
1449   }
1450 }
1451
1452 FeaturePtr SketchPlugin_Split::createLineFeature(const FeaturePtr& theBaseFeature,
1453                                                            const AttributePtr& theFirstPointAttr,
1454                                                            const AttributePtr& theSecondPointAttr)
1455 {
1456   FeaturePtr aFeature;
1457   SketchPlugin_Sketch* aSketch = sketch();
1458   if (!aSketch || !theBaseFeature.get())
1459     return aFeature;
1460
1461   aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1462
1463   fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPointAttr);
1464   fillAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPointAttr);
1465
1466   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1467                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1468
1469   aFeature->execute(); // to obtain result
1470
1471   return aFeature;
1472 }
1473
1474 FeaturePtr SketchPlugin_Split::createArcFeature(const FeaturePtr& theBaseFeature,
1475                                                           const AttributePtr& theFirstPointAttr,
1476                                                           const AttributePtr& theSecondPointAttr)
1477 {
1478   FeaturePtr aFeature;
1479   SketchPlugin_Sketch* aSketch = sketch();
1480   if (!aSketch || !theBaseFeature.get())
1481     return aFeature;
1482
1483   std::string aCenterAttributeId;
1484   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1485     aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1486   else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1487     aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1488
1489   if (aCenterAttributeId.empty())
1490     return aFeature;
1491
1492   aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1493   // update fillet arc: make the arc correct for sure, so, it is not needed to process
1494   // the "attribute updated"
1495   // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1496   bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1497
1498   fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1499                 theBaseFeature->attribute(aCenterAttributeId));
1500   fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPointAttr);
1501   fillAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPointAttr);
1502
1503   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1504                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1505
1506   /// fill referersed state of created arc as it is on the base arc
1507   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1508     bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1509     aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1510   }
1511   aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1512   aFeature->execute(); // to obtain result
1513
1514   return aFeature;
1515 }
1516
1517 void SketchPlugin_Split::updateFeaturesAfterSplit(
1518                                                    const std::set<FeaturePtr>& theFeaturesToUpdate)
1519 {
1520   std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
1521                                        aLast = theFeaturesToUpdate.end();
1522   for (; anIt != aLast; anIt++) {
1523     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1524     std::string aRefFeatureKind = aRefFeature->getKind();
1525     if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
1526       std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
1527                               std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
1528       if (aLenghtFeature.get()) {
1529         std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
1530             ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
1531         double aValue;
1532         if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
1533           aValueAttr->setValue(aValue);
1534       }
1535     }
1536   }
1537 }
1538
1539 std::shared_ptr<ModelAPI_Result> SketchPlugin_Split::getFeatureResult(
1540                                     const std::shared_ptr<ModelAPI_Feature>& theFeature)
1541 {
1542   std::shared_ptr<ModelAPI_Result> aResult;
1543
1544   std::string aFeatureKind = theFeature->getKind();
1545   if (aFeatureKind == SketchPlugin_Line::ID())
1546     aResult = theFeature->firstResult();
1547   else if (aFeatureKind == SketchPlugin_Arc::ID())
1548     aResult = theFeature->lastResult();
1549   else if (aFeatureKind == SketchPlugin_Circle::ID())
1550     aResult = theFeature->lastResult();
1551
1552   return aResult;
1553 }
1554
1555 #ifdef _DEBUG
1556 std::set<std::shared_ptr<ModelAPI_Attribute> > SketchPlugin_Split::getEdgeAttributes(
1557                                            const std::shared_ptr<ModelAPI_Feature>& theFeature)
1558 {
1559   std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes;
1560
1561   std::string aFeatureKind = theFeature->getKind();
1562   if (aFeatureKind == SketchPlugin_Line::ID()) {
1563     anAttributes.insert(theFeature->attribute(SketchPlugin_Line::START_ID()));
1564     anAttributes.insert(theFeature->attribute(SketchPlugin_Line::END_ID()));
1565   }
1566   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
1567     anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::START_ID()));
1568     anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::END_ID()));
1569   }
1570   else if (aFeatureKind == SketchPlugin_Circle::ID()) {
1571   }
1572
1573   return anAttributes;
1574 }
1575 #endif
1576
1577 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_Split::getPointAttribute
1578                                                               (const bool isFirstAttribute)
1579 {
1580   std::shared_ptr<GeomDataAPI_Point2D> anAttribute;
1581
1582   GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
1583   if (!aSelectedShape.get())
1584     return anAttribute;
1585
1586   if (aSelectedShape->shapeType() != GeomAPI_Shape::EDGE)
1587     return anAttribute;
1588
1589   AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1590                                                        data()->attribute(SELECTED_OBJECT()));
1591   ObjectPtr aBaseObject = anObjectAttr->value();
1592   if (!aBaseObject.get())
1593     return anAttribute;
1594
1595   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aSelectedShape));
1596
1597   std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
1598   std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
1599
1600   std::shared_ptr<GeomDataAPI_Point2D> aFirstPointAttr, aLastPointAttr;
1601   /// find the points in feature attributes
1602   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
1603   std::list<AttributePtr> a2DPointAttributes = aBaseFeature->data()->attributes(
1604                                                     GeomDataAPI_Point2D::typeId());
1605   std::list<AttributePtr>::const_iterator anIt = a2DPointAttributes.begin(),
1606                                           aLast = a2DPointAttributes.end();
1607   for (; anIt != aLast; anIt++) {
1608     std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
1609                                   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
1610     std::shared_ptr<GeomAPI_Pnt2d> aPoint2D = anAttributePoint->pnt();
1611     std::shared_ptr<GeomAPI_Pnt> aPoint3D = sketch()->to3D(aPoint2D->x(), aPoint2D->y());
1612     if (aFirstPnt->isEqual(aPoint3D))
1613       aFirstPointAttr = anAttributePoint;
1614     else if (aLastPnt->isEqual(aPoint3D))
1615       aLastPointAttr = anAttributePoint;
1616   }
1617
1618   /// find the points in coincident features
1619   PntToAttributesMap aRefAttributes = myCashedReferences[aBaseObject];
1620   PntToAttributesMap::const_iterator
1621     aRIt = aRefAttributes.begin(), aRLast = aRefAttributes.end();
1622   for (; aRIt != aRLast; aRIt++) {
1623     std::shared_ptr<GeomDataAPI_Point2D> anAttribute = aRIt->first;
1624     std::shared_ptr<GeomAPI_Pnt> aPoint = aRIt->second;
1625     if (!aFirstPointAttr.get() && aFirstPnt->isEqual(aPoint))
1626       aFirstPointAttr = anAttribute;
1627     if (!aLastPointAttr.get() && aLastPnt->isEqual(aPoint))
1628       aLastPointAttr = anAttribute;
1629     if (aFirstPointAttr.get() && aLastPointAttr.get())
1630       break;
1631   }
1632   if (!aFirstPointAttr.get() || !aLastPointAttr)
1633     return anAttribute;
1634
1635   return isFirstAttribute ? aFirstPointAttr : aLastPointAttr;
1636 }
1637
1638 #ifdef _DEBUG
1639 std::string SketchPlugin_Split::getFeatureInfo(const std::shared_ptr<ModelAPI_Feature>& theFeature,
1640                                                const bool isUseAttributesInfo)
1641 {
1642   std::string anInfo;
1643   if (!theFeature.get()) {
1644     return "none";
1645   }
1646
1647   if (theFeature->data()->isValid())
1648     anInfo.append(theFeature->data()->name().c_str());
1649
1650   if (isUseAttributesInfo) {
1651     std::string aPointsInfo = ModelGeomAlgo_Point2D::getPontAttributesInfo(theFeature,
1652                                                              getEdgeAttributes(theFeature));
1653     /// processing of feature with point 2d attributes, like line, arc, circle
1654     if (!aPointsInfo.empty()) {
1655       anInfo += ": ";
1656       anInfo += "\n";
1657       anInfo += aPointsInfo;
1658     }
1659     else { /// process constraint coincidence, find points in ref attr attributes
1660       std::list<AttributePtr> anAttrs = theFeature->data()->attributes(
1661                                                        ModelAPI_AttributeRefAttr::typeId());
1662       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
1663       std::string anAttributesInfo;
1664       for(; anIt != aLast; anIt++) {
1665         if (!anAttributesInfo.empty()) {
1666           anAttributesInfo.append(", ");
1667           anAttributesInfo += "\n";
1668         }
1669         AttributePtr anAttr = *anIt;
1670         std::string aValue = "not defined";
1671         std::string aType = anAttr->attributeType();
1672         if (aType == ModelAPI_AttributeRefAttr::typeId()) {
1673           std::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr =
1674                              std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
1675           if (aRefAttr.get()) {
1676             if (aRefAttr->isObject()) {
1677               FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1678               aValue = "<object:>" + getFeatureInfo(aFeature, false);
1679             }
1680             else {
1681               AttributePtr anAttribute = aRefAttr->attr();
1682               if (anAttribute.get()) {
1683                 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
1684                 aValue = "<attr:>" + ModelGeomAlgo_Point2D::getPointAttributeInfo(anAttribute) +
1685                          " [" + getFeatureInfo(aFeature, false) + "]";
1686               }
1687             }
1688           }
1689         }
1690         anAttributesInfo.append("    " + anAttr->id() + ": " + aValue);
1691       }
1692       if (!anAttributesInfo.empty())
1693         anInfo = anInfo + "\n" + anAttributesInfo;
1694     }
1695   }
1696   return anInfo;
1697 }
1698 #endif