Salome HOME
b254d63d4bc890b7e9ca5d45fcb1cef3c6c77f37
[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
59 #include <ModelGeomAlgo_Point2D.h>
60 #include <ModelAPI_EventReentrantMessage.h>
61 #include <Events_Loop.h>
62
63 #include <cmath>
64
65 //#define CREATE_CONSTRAINTS
66
67 //#define DEBUG_SPLIT
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_TRIM_METHODS
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 std::string SketchPlugin_Split::processEvent(const std::shared_ptr<Events_Message>& theMessage)
425 {
426 #ifdef DEBUG_TRIM_METHODS
427   std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
428 #endif
429   std::string aFilledAttributeName;
430
431   std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
432         std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
433   if (aMessage.get()) {
434     ObjectPtr anObject = aMessage->selectedObject();
435     std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
436
437     if (anObject.get() && aPoint.get()) {
438       //if (myCashedShapes.find(anObject) == myCashedShapes.end())
439       //  fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
440       if (myCashedShapes.find(anObject) == myCashedShapes.end())
441         fillObjectShapes(anObject, sketch()->data()->owner());
442       const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
443       if (aShapes.size() > 1) {
444         std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
445                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
446                               data()->attribute(SELECTED_OBJECT()));
447         std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
448                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
449                               data()->attribute(PREVIEW_OBJECT()));
450         aRefSelectedAttr->setValue(anObject);
451         aRefPreviewAttr->setValue(anObject);
452
453         std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
454                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
455                               data()->attribute(SELECTED_POINT()));
456         std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
457                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
458                               data()->attribute(PREVIEW_POINT()));
459         aPointSelectedAttr->setValue(aPoint);
460         aPointPreviewAttr->setValue(aPoint);
461
462         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
463
464         GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
465   #ifdef DEBUG_TRIM_METHODS
466         if (!aSelectedShape.get())
467           std::cout << "Set empty selected object" << std::endl;
468         else
469           std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
470   #endif
471         aFilledAttributeName = SELECTED_OBJECT();
472       }
473     }
474   }
475   return aFilledAttributeName;
476 }
477
478 AISObjectPtr SketchPlugin_Split::getAISObject(AISObjectPtr thePrevious)
479 {
480   /*AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
481                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
482   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
483
484   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointOfRefAttr(
485                                         data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
486   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointOfRefAttr(
487                                         data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
488
489   if (aBaseObjectAttr->isInitialized() && aBaseFeature.get() &&
490       aFirstPointAttrOfSplit->isInitialized() &&
491       aSecondPointAttrOfSplit->isInitialized()) {
492
493     ResultPtr aResult = getFeatureResult(aBaseFeature);
494     GeomShapePtr aBaseShape = aResult->shape();
495     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
496
497     std::shared_ptr<GeomAPI_Pnt2d> aStartPnt2d = aFirstPointAttrOfSplit->pnt();
498     std::shared_ptr<GeomAPI_Pnt> aStartPoint = sketch()->to3D(aStartPnt2d->x(), aStartPnt2d->y());
499     aPoints.push_back(aStartPoint);
500
501     std::shared_ptr<GeomAPI_Pnt2d> aSecondPnt2d = aSecondPointAttrOfSplit->pnt();
502     std::shared_ptr<GeomAPI_Pnt> aSecondPoint =
503       sketch()->to3D(aSecondPnt2d->x(), aSecondPnt2d->y());
504     aPoints.push_back(aSecondPoint);
505
506     std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
507
508     GeomAlgoAPI_ShapeTools::splitShape_p(aBaseShape, aPoints, aSplitShapes);
509     std::shared_ptr<GeomAPI_Shape> aShape =
510       GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
511
512     AISObjectPtr anAIS = thePrevious;
513     if (aShape) {
514       if (!anAIS)
515         anAIS = AISObjectPtr(new GeomAPI_AISObject);
516       anAIS->createShape(aShape);
517       std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
518              aBaseFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID());
519
520       bool isConstruction = anAuxiliaryAttr.get() != NULL && anAuxiliaryAttr->value();
521
522       std::vector<int> aColor;
523       double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
524       int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
525       if (isConstruction) {
526         aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color");
527         aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH_AUXILIARY();
528         aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY();
529       }
530       else {
531         aColor = Config_PropManager::color("Visualization", "sketch_entity_color");
532       }
533       anAIS->setColor(aColor[0], aColor[1], aColor[2]);
534       anAIS->setWidth(aWidth + 1);
535       anAIS->setLineStyle(aLineStyle);
536     }
537     return anAIS;
538   }
539   return AISObjectPtr();*/
540 #ifdef DEBUG_TRIM_METHODS
541   std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
542 #endif
543
544   AISObjectPtr anAIS = thePrevious;
545
546   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
547   GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
548   if (aPreviewShape.get())
549     aShapes.push_back(aPreviewShape);
550   GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
551   if (aSelectedShape.get())
552     aShapes.push_back(aSelectedShape);
553
554   if (aShapes.empty())
555     return AISObjectPtr();
556
557   GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
558   if (!aBaseShape.get())
559     return AISObjectPtr();
560
561   if (aBaseShape.get()) {
562     if (!anAIS)
563       anAIS = AISObjectPtr(new GeomAPI_AISObject);
564     anAIS->createShape(aBaseShape);
565
566     std::vector<int> aColor;
567     aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
568     double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
569     int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
570     anAIS->setColor(aColor[0], aColor[1], aColor[2]);
571     // width when there is not base object should be extened in several points
572     // in order to see this preview over highlight
573     anAIS->setWidth(aWidth+4);
574     anAIS->setLineStyle(aLineStyle);
575   }
576   else
577     anAIS = AISObjectPtr();
578   return anAIS;
579 }
580
581 //********************************************************************
582 void SketchPlugin_Split::fillObjectShapes(const ObjectPtr& theObject,
583                                           const ObjectPtr& theSketch)
584 {
585   std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
586   std::map<std::shared_ptr<GeomDataAPI_Point2D>, std::shared_ptr<GeomAPI_Pnt> > aPointToAttributes;
587   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
588   // current feature
589   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
590   // edges on feature
591   std::set<ResultPtr> anEdgeResults;
592   ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeResults);
593   if (!anEdgeResults.empty()) {
594     GeomShapePtr aFeatureShape = (*anEdgeResults.begin())->shape();
595
596     // coincidences to the feature
597     ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
598                          aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
599     // layed on feature coincidences to divide it on several shapes
600     std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
601     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
602         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
603     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
604         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
605     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
606         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
607     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
608     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
609     ModelGeomAlgo_Point2D::getPointsInsideShape_p(aFeatureShape, aRefAttributes, aC->pnt(),
610                                                 aX->dir(), aY, aPoints, aPointToAttributes);
611
612     GeomAlgoAPI_ShapeTools::splitShape_p(aFeatureShape, aPoints, aShapes);
613   }
614   myCashedShapes[theObject] = aShapes;
615   myCashedReferences[theObject] = aPointToAttributes;
616 }
617
618 GeomShapePtr SketchPlugin_Split::getSubShape(const std::string& theObjectAttributeId,
619                                              const std::string& thePointAttributeId)
620 {
621   GeomShapePtr aBaseShape;
622
623   AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
624                                                        data()->attribute(theObjectAttributeId));
625   ObjectPtr aBaseObject = anObjectAttr->value();
626   if (!aBaseObject.get())
627     return aBaseShape;
628
629   // point on feature
630   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
631                                            data()->attribute(thePointAttributeId));
632   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
633   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
634                                                                anAttributePnt2d->y());
635
636 #ifdef TRIM_SHAPE
637   if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
638     fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
639
640   const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
641   if (!aShapes.empty()) {
642     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
643     for (; anIt != aLast; anIt++) {
644       GeomShapePtr aShape = *anIt;
645       std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
646       if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
647         aBaseShape = aShape;
648     }
649   }
650 #else
651   if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
652     fillObjectShapes(aBaseObject, sketch()->data()->owner());
653
654   std::shared_ptr<GeomAPI_Pnt> aStartPoint;
655   std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
656   const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
657   std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
658   for (; anIt != aLast; anIt++) {
659     GeomShapePtr aCurrentShape = *anIt;
660     std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
661     if (ModelGeomAlgo_Point2D::isPointOnEdge(aCurrentShape, anAttributePnt, aProjectedPoint)) {
662       if (aCurrentShape->shapeType() == GeomAPI_Shape::EDGE) {
663         std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aCurrentShape));
664         aStartPoint = anEdge->firstPoint();
665         aSecondPoint = anEdge->lastPoint();
666       }
667       break;
668     }
669   }
670
671   if (!aStartPoint.get() || !aSecondPoint.get())
672     return aBaseShape;
673
674   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
675   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
676   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject/*aBaseObjectAttr->value()*/);
677
678   //AttributePoint2DPtr aFirstPointAttrOfSplit = getPointOfRefAttr(
679   //                                      data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
680   //AttributePoint2DPtr aSecondPointAttrOfSplit = getPointOfRefAttr(
681   //                                      data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
682   if (anObjectAttr->isInitialized() && aBaseFeature.get() && aPointAttr->isInitialized()) {
683       //aFirstPointAttrOfSplit->isInitialized() &&
684       //aSecondPointAttrOfSplit->isInitialized()) {
685     ResultPtr aResult = getFeatureResult(aBaseFeature);
686     GeomShapePtr aResultShape = aResult->shape();
687     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
688
689     //std::shared_ptr<GeomAPI_Pnt2d> aStartPnt2d = aFirstPointAttrOfSplit->pnt();
690     //std::shared_ptr<GeomAPI_Pnt> aStartPoint = sketch()->to3D(aStartPnt2d->x(), aStartPnt2d->y());
691     aPoints.push_back(aStartPoint);
692
693     //std::shared_ptr<GeomAPI_Pnt2d> aSecondPnt2d = aSecondPointAttrOfSplit->pnt();
694     //std::shared_ptr<GeomAPI_Pnt> aSecondPoint =
695     //  sketch()->to3D(aSecondPnt2d->x(), aSecondPnt2d->y());
696     aPoints.push_back(aSecondPoint);
697
698     std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
699     GeomAlgoAPI_ShapeTools::splitShape_p(aResultShape, aPoints, aSplitShapes);
700     aBaseShape = GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
701 #endif
702   }
703   return aBaseShape;
704 }
705
706 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_Split::getPointOfRefAttr(
707                                                       const AttributePtr& theAttribute)
708 {
709   AttributePoint2DPtr aPointAttribute;
710
711   if (theAttribute->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
712     AttributeRefAttrPtr aRefAttr =
713       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
714     if (aRefAttr.get() && aRefAttr->isInitialized()) {
715       AttributePtr anAttribute = aRefAttr->attr();
716       if (anAttribute.get() && anAttribute->attributeType() == GeomDataAPI_Point2D::typeId())
717         aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
718     }
719   }
720   return aPointAttribute;
721 }
722
723 void SketchPlugin_Split::getFeaturePoints(const FeaturePtr& theFeature,
724                                                     AttributePoint2DPtr& theStartPointAttr,
725                                                     AttributePoint2DPtr& theEndPointAttr)
726 {
727   std::string aFeatureKind = theFeature->getKind();
728   std::string aStartAttributeName, anEndAttributeName;
729   if (aFeatureKind == SketchPlugin_Line::ID()) {
730     aStartAttributeName = SketchPlugin_Line::START_ID();
731     anEndAttributeName = SketchPlugin_Line::END_ID();
732   }
733   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
734     aStartAttributeName = SketchPlugin_Arc::START_ID();
735     anEndAttributeName = SketchPlugin_Arc::END_ID();
736   }
737   if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
738     theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
739                                          theFeature->attribute(aStartAttributeName));
740     theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
741                                          theFeature->attribute(anEndAttributeName));
742   }
743 }
744
745 void SketchPlugin_Split::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
746                                     std::set<FeaturePtr>& theFeaturesToUpdate,
747                                     std::map<FeaturePtr, IdToPointPair>& theCoincidenceToFeature)
748 {
749   std::shared_ptr<ModelAPI_Data> aData = data();
750
751   // Check the base objects are initialized.
752   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
753                                                            data()->attribute(SELECTED_OBJECT()));
754   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
755   //                                          aData->attribute(SketchPlugin_Constraint::VALUE()));
756   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
757   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
758
759   std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
760   std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
761   aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
762
763   std::set<AttributePtr>::const_iterator aIt;
764   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
765     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
766     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
767     std::string aRefFeatureKind = aRefFeature->getKind();
768     if (aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() ||
769         aRefFeatureKind == SketchPlugin_MultiRotation::ID() ||
770         aRefFeatureKind == SketchPlugin_MultiTranslation::ID() ||
771         aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
772       theFeaturesToDelete.insert(aRefFeature);
773     else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
774       theFeaturesToUpdate.insert(aRefFeature);
775     else if (aRefFeatureKind == SketchPlugin_ConstraintCoincidence::ID()) {
776       std::string anAttributeToBeModified;
777       AttributePoint2DPtr aCoincidentPoint;
778       AttributeRefAttrPtr anAttrA = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_A());
779       AttributeRefAttrPtr anAttrB = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_B());
780       bool isToFeature = false;
781       if (anAttrA->isObject() || anAttrB->isObject()) { /// coincidence to base feature
782         FeaturePtr aFeature = anAttrA->isObject() ? ModelAPI_Feature::feature(anAttrA->object())
783                                                   : FeaturePtr();
784         isToFeature = aFeature.get() && aFeature == aBaseFeature;
785         anAttributeToBeModified = anAttrA->id();
786         if (!isToFeature) {
787           aFeature = anAttrB->isObject() ? ModelAPI_Feature::feature(anAttrB->object())
788                                          : FeaturePtr();
789           isToFeature = aFeature.get() && aFeature == aBaseFeature;
790           anAttributeToBeModified = anAttrB->id();
791         }
792         if (isToFeature)
793           aCoincidentPoint = SketchPlugin_ConstraintCoincidence::getPoint(aRefFeature);
794       }
795       if (!isToFeature) { /// coincidence to point on base feature
796         AttributePtr anAttribute;
797
798         if (!anAttrA->isObject()) {
799           AttributePtr aCurAttribute = anAttrA->attr();
800           if (aCurAttribute.get()) {
801             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
802             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
803               anAttribute = anAttrB->attr();
804               anAttributeToBeModified = anAttrA->id();
805             }
806           }
807         }
808         if (!anAttribute.get() && !anAttrB->isObject()) {
809           AttributePtr aCurAttribute = anAttrB->attr();
810           if (aCurAttribute.get()) {
811             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
812             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
813               anAttribute = anAttrA->attr();
814               anAttributeToBeModified = anAttrB->id();
815             }
816           }
817         }
818         if (anAttribute.get())
819           aCoincidentPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
820       }
821       if (aCoincidentPoint.get() && isToFeature)
822         theCoincidenceToFeature[aRefFeature] = std::make_pair(anAttributeToBeModified,
823                                                               aCoincidentPoint);
824     }
825   }
826 }
827
828 void SketchPlugin_Split::getRefAttributes(const FeaturePtr& theFeature,
829                                     std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
830                                     std::list<AttributePtr>& theRefsToFeature)
831 {
832   theRefs.clear();
833
834   std::list<AttributePtr> aPointAttributes =
835     theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
836   std::set<AttributePtr> aPointAttributesSet;
837
838   std::list<AttributePtr>::const_iterator aPIt =
839     aPointAttributes.begin(), aPLast = aPointAttributes.end();
840   for (; aPIt != aPLast; aPIt++)
841     aPointAttributesSet.insert(*aPIt);
842
843   std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
844   std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
845   aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
846
847   std::set<AttributePtr>::const_iterator aIt;
848   for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
849     AttributePtr anAttr = (*aIt);
850     FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
851     if (anAttrFeature.get() != this &&
852         anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
853       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
854       if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
855         AttributePtr anAttrInRef = aRefAttr->attr();
856         if (anAttrInRef.get() &&
857             aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
858           if (theRefs.find(anAttrInRef) != theRefs.end())
859             theRefs[anAttrInRef].push_back(aRefAttr);
860           else {
861             std::list<AttributePtr> anAttrList;
862             anAttrList.push_back(aRefAttr);
863             theRefs[anAttrInRef] = anAttrList;
864           }
865         }
866       }
867       else { /// find attributes referenced to feature itself
868         theRefsToFeature.push_back(anAttr);
869       }
870     }
871   }
872 }
873
874 void SketchPlugin_Split::updateCoincidenceConstraintsToFeature(
875       const std::map<std::shared_ptr<ModelAPI_Feature>, IdToPointPair>& theCoincidenceToFeature,
876       const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences,
877       const std::set<ResultPtr>& theFeatureResults,
878       const FeaturePtr& theSplitFeature,
879       std::set<FeaturePtr>& theFeaturesToDelete)
880 {
881   if (theCoincidenceToFeature.empty())
882     return;
883
884   // we should build coincidence constraints to end of the split feature
885   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aNewCoincidencesToSplitFeature;
886   AttributePoint2DPtr aStartPointAttr, anEndPointAttr;
887   getFeaturePoints(theSplitFeature, aStartPointAttr, anEndPointAttr);
888   if (theFurtherCoincidences.find(aStartPointAttr) == theFurtherCoincidences.end())
889     aNewCoincidencesToSplitFeature.insert(aStartPointAttr);
890   if (theFurtherCoincidences.find(anEndPointAttr) == theFurtherCoincidences.end())
891     aNewCoincidencesToSplitFeature.insert(anEndPointAttr);
892
893   std::map<FeaturePtr, IdToPointPair>::const_iterator aCIt = theCoincidenceToFeature.begin(),
894                                                             aCLast = theCoincidenceToFeature.end();
895 #ifdef DEBUG_SPLIT
896   std::cout << std::endl;
897   std::cout << "Coincidences to feature(modified):"<< std::endl;
898 #endif
899   for (; aCIt != aCLast; aCIt++) {
900     FeaturePtr aCoincFeature = aCIt->first;
901     std::string anAttributeId = aCIt->second.first;
902     std::string aSecondAttribute = anAttributeId == SketchPlugin_Constraint::ENTITY_A() ?
903         SketchPlugin_Constraint::ENTITY_B() : SketchPlugin_Constraint::ENTITY_A();
904
905     AttributePoint2DPtr aCoincPoint = aCIt->second.second;
906     std::set<AttributePoint2DPtr>::const_iterator aFCIt = theFurtherCoincidences.begin(),
907                                                   aFCLast = theFurtherCoincidences.end();
908     std::shared_ptr<GeomAPI_Pnt2d> aCoincPnt = aCoincPoint->pnt();
909     AttributePoint2DPtr aFeaturePointAttribute;
910     for (; aFCIt != aFCLast && !aFeaturePointAttribute.get(); aFCIt++) {
911       AttributePoint2DPtr aFCAttribute = *aFCIt;
912       if (aCoincPnt->isEqual(aFCAttribute->pnt()))
913         aFeaturePointAttribute = aFCAttribute;
914     }
915     if (aFeaturePointAttribute.get()) {
916       // create new constraint and remove the current
917       aCoincFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
918           aFeaturePointAttribute, aCoincFeature->refattr(aSecondAttribute)->attr());
919       theFeaturesToDelete.insert(aCIt->first);
920       // create new coincidences to split feature points
921       std::set<AttributePoint2DPtr>::const_iterator aSFIt = aNewCoincidencesToSplitFeature.begin(),
922                                                     aSFLast = aNewCoincidencesToSplitFeature.end();
923       for (; aSFIt != aSFLast; aSFIt++) {
924         AttributePoint2DPtr aSFAttribute = *aSFIt;
925         if (aCoincPnt->isEqual(aSFAttribute->pnt())) {
926           createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
927                            aSFAttribute, aCoincFeature->refattr(aSecondAttribute)->attr());
928         }
929       }
930     }
931     else {
932       /// find feature by shape intersected the point
933       ResultPtr aResultForCoincidence = *(theFeatureResults.begin());
934
935       if (theFeatureResults.size() > 1) { // try to find point on additional feature
936         ResultPtr anAddtionalResult = *(theFeatureResults.begin()++);
937         GeomShapePtr aShape = anAddtionalResult->shape();
938
939         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCoincPoint->pnt();
940         std::shared_ptr<GeomAPI_Pnt> aPoint = sketch()->to3D(aPnt2d->x(), aPnt2d->y());
941
942         std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
943         if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPoint, aProjectedPoint))
944           aResultForCoincidence = anAddtionalResult;
945       }
946       aCoincFeature->refattr(anAttributeId)->setObject(aResultForCoincidence);
947     }
948 #ifdef DEBUG_SPLIT
949   std::cout << " -" << getFeatureInfo(aCoincFeature) << std::endl;
950 #endif
951   }
952 }
953
954 void SketchPlugin_Split::updateRefFeatureConstraints(
955                                                   const ResultPtr& theFeatureBaseResult,
956                                                   const std::list<AttributePtr>& theRefsToFeature)
957 {
958   std::list<AttributePtr>::const_iterator anIt = theRefsToFeature.begin(),
959                                           aLast = theRefsToFeature.end();
960   for (; anIt != aLast; anIt++) {
961     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIt);
962     if (aRefAttr.get())
963       aRefAttr->setObject(theFeatureBaseResult);
964   }
965 }
966
967 void SketchPlugin_Split::updateRefAttConstraints(
968                     const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
969                     const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes)
970 {
971 #ifdef DEBUG_SPLIT
972   std::cout << "SketchPlugin_Split::updateRefAttConstraints" << std::endl;
973 #endif
974
975   std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
976     anIt = theModifiedAttributes.begin(),  aLast = theModifiedAttributes.end();
977   for (; anIt != aLast; anIt++) {
978     AttributePtr anAttribute = anIt->first;
979
980     /// not found in references
981     if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
982       continue;
983     std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
984     std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
985                                             aRLast = aRefAttributes.end();
986
987     AttributePtr aNewAttribute = anIt->second;
988     for (; aRefIt != aRLast; aRefIt++) {
989       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
990       if (aRefAttr.get()) {
991         aRefAttr->setAttr(aNewAttribute);
992 #ifdef DEBUG_SPLIT
993         FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->owner());
994         std::cout << " -" << getFeatureInfo(aFeature) << std::endl;
995 #endif
996       }
997     }
998   }
999 }
1000
1001 FeaturePtr SketchPlugin_Split::splitLine(FeaturePtr& theSplitFeature,
1002                                              FeaturePtr& theBaseFeatureModified,
1003                                              FeaturePtr& theAfterFeature,
1004                                              std::set<AttributePoint2DPtr>& thePoints,
1005                                              std::set<FeaturePtr>& theCreatedFeatures,
1006                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1007 {
1008   FeaturePtr anNewFeature;
1009
1010   std::set<FeaturePtr> aCreatedFeatures;
1011   FeaturePtr aConstraintFeature;
1012   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1013
1014   SketchPlugin_Sketch* aSketch = sketch();
1015   if (!aSketch)
1016     return anNewFeature;
1017
1018   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1019                                                            data()->attribute(SELECTED_OBJECT()));
1020   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1021   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
1022   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1023   std::string aFeatureKind = aBaseFeature->getKind();
1024   if (aFeatureKind != SketchPlugin_Line::ID())
1025     return anNewFeature;
1026
1027   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1028     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1029   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1030     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1031   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1032
1033   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1034   if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
1035     setError("Error: Feature has no start and end points.");
1036     return anNewFeature;
1037   }
1038
1039   arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
1040                       aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1041
1042 #ifdef DEBUG_SPLIT
1043   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1044   std::cout << "Start point: " <<
1045     ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
1046   std::cout << "1st point:   " <<
1047     ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
1048   std::cout << "2nd point:   " <<
1049     ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
1050   std::cout << "End point:   " <<
1051     ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
1052 #endif
1053
1054   /// create a split feature
1055   theSplitFeature =
1056     createLineFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1057   theCreatedFeatures.insert(theSplitFeature);
1058
1059   // before split feature
1060   if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1061     theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
1062                                         theSplitFeature->attribute(SketchPlugin_Line::START_ID())));
1063   }
1064   else {
1065     theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
1066     /// move end arc point to start of split
1067   }
1068
1069   // after split feature
1070   if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
1071     FeaturePtr aFeature;
1072     if (!theBaseFeatureModified.get()) {
1073       aFeature = aBaseFeature; ///< use base feature to store all constraints here
1074       fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), aSecondPointAttrOfSplit);
1075       aFeature->execute(); // to update result
1076     }
1077     else {
1078       aFeature = createLineFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
1079       theCreatedFeatures.insert(aFeature);
1080       theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1081                                              aFeature->attribute(SketchPlugin_Line::END_ID())));
1082       anNewFeature = aFeature;
1083     }
1084     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1085                      theSplitFeature->attribute(SketchPlugin_Line::END_ID()),
1086                      aFeature->attribute(SketchPlugin_Line::START_ID()));
1087     theCreatedFeatures.insert(aConstraintFeature);
1088
1089     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1090                                 (aFeature->attribute(SketchPlugin_Line::START_ID())));
1091     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1092                                 (aFeature->attribute(SketchPlugin_Line::END_ID())));
1093
1094     if (!theBaseFeatureModified.get())
1095       theBaseFeatureModified = aFeature;
1096     else
1097       theAfterFeature = aFeature;
1098   }
1099   else {
1100     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1101                                   (theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
1102     theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1103                                    theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
1104   }
1105   // base split, that is defined before split feature should be changed at end
1106   // (after the after feature creation). Otherwise modified value will be used in after feature
1107   // before split feature
1108   if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1109     /// move end arc point to start of split
1110     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
1111                                                     aFirstPointAttrOfSplit);
1112     theBaseFeatureModified->execute(); // to update result
1113     aConstraintFeature = createConstraint(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 = createConstraintForObjects(SketchPlugin_ConstraintParallel::ID(),
1130                                                        getFeatureResult(aBaseFeature),
1131                                                        getFeatureResult(theSplitFeature));
1132   theCreatedFeatures.insert(aConstraintFeature);
1133   if (theAfterFeature.get()) {
1134     aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintParallel::ID(),
1135                                                     getFeatureResult(aBaseFeature),
1136                                                     getFeatureResult(theAfterFeature));
1137     theCreatedFeatures.insert(aConstraintFeature);
1138   }
1139 #endif
1140   return anNewFeature;
1141 }
1142
1143 FeaturePtr SketchPlugin_Split::splitArc(FeaturePtr& theSplitFeature,
1144                                             FeaturePtr& theBaseFeatureModified,
1145                                             FeaturePtr& theAfterFeature,
1146                                             std::set<AttributePoint2DPtr>& thePoints,
1147                                             std::set<FeaturePtr>& theCreatedFeatures,
1148                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1149 {
1150   FeaturePtr anNewFeature;
1151
1152   std::set<FeaturePtr> aCreatedFeatures;
1153   FeaturePtr aConstraintFeature;
1154   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1155
1156   SketchPlugin_Sketch* aSketch = sketch();
1157   if (!aSketch)
1158     return anNewFeature;
1159
1160   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1161                                                            data()->attribute(SELECTED_OBJECT()));
1162   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1163   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
1164   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1165   std::string aFeatureKind = aBaseFeature->getKind();
1166   if (aFeatureKind != SketchPlugin_Arc::ID())
1167     return anNewFeature;
1168
1169   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1170     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1171   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1172     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1173   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1174   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1175   if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
1176     setError("Error: Feature has no start and end points.");
1177     return anNewFeature;
1178   }
1179
1180   arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
1181                      aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1182 #ifdef DEBUG_SPLIT
1183   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1184   std::cout << "Start point: " <<
1185     ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
1186   std::cout << "1st point:   " <<
1187     ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
1188   std::cout << "2nd point:   " <<
1189     ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
1190   std::cout << "End point:   " <<
1191     ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
1192 #endif
1193
1194   /// split feature
1195   theSplitFeature = createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1196   theCreatedFeatures.insert(theSplitFeature);
1197
1198   // before split feature
1199   if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1200     theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
1201                                   theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
1202   }
1203   else {
1204     theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
1205     /// move end arc point to start of split
1206   }
1207
1208   // after split feature
1209   if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
1210     FeaturePtr aFeature;
1211     if (!theBaseFeatureModified.get()) {
1212       aFeature = aBaseFeature; ///< use base feature to store all constraints here
1213       fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), aSecondPointAttrOfSplit);
1214       aFeature->execute(); // to update result
1215     }
1216     else {
1217       aFeature = createArcFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
1218       theCreatedFeatures.insert(aFeature);
1219       theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1220                                                   aFeature->attribute(SketchPlugin_Arc::END_ID())));
1221       anNewFeature = aFeature;
1222     }
1223     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1224                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()),
1225                      aFeature->attribute(SketchPlugin_Arc::START_ID()));
1226     theCreatedFeatures.insert(aConstraintFeature);
1227
1228     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1229                                 (aFeature->attribute(SketchPlugin_Arc::START_ID())));
1230     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1231                                 (aFeature->attribute(SketchPlugin_Arc::END_ID())));
1232
1233     if (!theBaseFeatureModified.get())
1234       theBaseFeatureModified = aFeature;
1235     else
1236       theAfterFeature = aFeature;
1237   }
1238   else {
1239     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1240                                   (theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
1241     theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1242                                    theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
1243   }
1244   // base split, that is defined before split feature should be changed at end
1245   // (after the after feature creation). Otherwise modified value will be used in after feature
1246   // before split feature
1247   if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1248     /// move end arc point to start of split
1249     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1250                                                     aFirstPointAttrOfSplit);
1251     theBaseFeatureModified->execute(); // to update result
1252     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1253                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1254                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
1255     theCreatedFeatures.insert(aConstraintFeature);
1256
1257     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1258                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
1259     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1260                                (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
1261   }
1262   else
1263     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1264                                        (theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
1265
1266   // additional constraints between split and base features
1267 #ifdef CREATE_CONSTRAINTS
1268   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
1269                                                        getFeatureResult(aBaseFeature),
1270                                                        getFeatureResult(theSplitFeature));
1271   theCreatedFeatures.insert(aConstraintFeature);
1272   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
1273                                                        getFeatureResult(theSplitFeature),
1274                                                        getFeatureResult(aBaseFeature));
1275   theCreatedFeatures.insert(aConstraintFeature);
1276   if (theAfterFeature.get()) {
1277     aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
1278                                                     getFeatureResult(aBaseFeature),
1279                                                     getFeatureResult(theAfterFeature));
1280     theCreatedFeatures.insert(aConstraintFeature);
1281     aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
1282                                                     getFeatureResult(theSplitFeature),
1283                                                     getFeatureResult(theAfterFeature));
1284     theCreatedFeatures.insert(aConstraintFeature);
1285   }
1286 #endif
1287   return anNewFeature;
1288 }
1289
1290 FeaturePtr SketchPlugin_Split::splitCircle(FeaturePtr& theSplitFeature,
1291                                                FeaturePtr& theBaseFeatureModified,
1292                                                FeaturePtr& theAfterFeature,
1293                                                std::set<AttributePoint2DPtr>& thePoints,
1294                                                std::set<FeaturePtr>& theCreatedFeatures,
1295                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1296 {
1297   FeaturePtr anNewFeature;
1298
1299   std::set<FeaturePtr> aCreatedFeatures;
1300   FeaturePtr aConstraintFeature;
1301   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1302
1303   SketchPlugin_Sketch* aSketch = sketch();
1304   if (!aSketch)
1305     return anNewFeature;
1306
1307   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1308                                                            data()->attribute(SELECTED_OBJECT()));
1309   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1310   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
1311   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1312   std::string aFeatureKind = aBaseFeature->getKind();
1313   if (aFeatureKind != SketchPlugin_Circle::ID())
1314     return anNewFeature;
1315
1316   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1317     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1318   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1319     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1320
1321   /// split feature
1322   theSplitFeature =
1323     createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1324   bool aSplitReversed = std::dynamic_pointer_cast<SketchPlugin_Arc>(theSplitFeature)->isReversed();
1325   theCreatedFeatures.insert(theSplitFeature);
1326
1327   /// base feature is a left part of the circle
1328   theBaseFeatureModified = createArcFeature(aBaseFeature,
1329     aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1330   anNewFeature = theBaseFeatureModified;
1331   std::dynamic_pointer_cast<SketchPlugin_Arc>(
1332     theBaseFeatureModified)->setReversed(!aSplitReversed);
1333   theBaseFeatureModified->execute();
1334
1335   theModifiedAttributes.insert(
1336     std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1337                   theBaseFeatureModified->attribute(SketchPlugin_Arc::CENTER_ID())));
1338
1339   theCreatedFeatures.insert(theBaseFeatureModified);
1340
1341   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1342                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
1343   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1344                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
1345
1346   // additional constraints between split and base features
1347   aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1348                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1349                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()));
1350   theCreatedFeatures.insert(aConstraintFeature);
1351   aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1352                      theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID()),
1353                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
1354   theCreatedFeatures.insert(aConstraintFeature);
1355
1356 #ifdef CREATE_CONSTRAINTS
1357   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
1358                                                        getFeatureResult(theSplitFeature),
1359                                                        getFeatureResult(theBaseFeatureModified));
1360   theCreatedFeatures.insert(aConstraintFeature);
1361 #endif
1362   return anNewFeature;
1363 }
1364
1365 void SketchPlugin_Split::arrangePointsOnLine(
1366     const AttributePoint2DPtr& theStartPointAttr,
1367     const AttributePoint2DPtr& theEndPointAttr,
1368     AttributePoint2DPtr& theFirstPointAttr,
1369     AttributePoint2DPtr& theLastPointAttr) const
1370 {
1371   // if first point is closer to last point, swap first and last values
1372   if (theStartPointAttr->pnt()->distance(theFirstPointAttr->pnt()) >
1373       theStartPointAttr->pnt()->distance(theLastPointAttr->pnt())) {
1374     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1375     theFirstPointAttr = theLastPointAttr;
1376     theLastPointAttr = aTmpPoint;
1377   }
1378 }
1379
1380 void SketchPlugin_Split::arrangePointsOnArc(
1381     const FeaturePtr& theArc,
1382     const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
1383     const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
1384     std::shared_ptr<GeomDataAPI_Point2D>& theFirstPointAttr,
1385     std::shared_ptr<GeomDataAPI_Point2D>& theSecondPointAttr) const
1386 {
1387   static const double anAngleTol = 1.e-12;
1388
1389   std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1390       theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1391   bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1392
1393   // collect directions to each point
1394   std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1395       new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1396   std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1397       new GeomAPI_Dir2d(theFirstPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1398   std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1399       new GeomAPI_Dir2d(theSecondPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1400
1401   // sort points by their angular values
1402   double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1403   double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1404   double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1405   if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1406     aFirstPtAngle += aPeriod;
1407   if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1408     aSecondPtAngle += aPeriod;
1409
1410   if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1411     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1412     theFirstPointAttr = theSecondPointAttr;
1413     theSecondPointAttr = aTmpPoint;
1414   }
1415 }
1416
1417 void SketchPlugin_Split::fillAttribute(const AttributePtr& theModifiedAttribute,
1418                                                  const AttributePtr& theSourceAttribute)
1419 {
1420   std::string anAttributeType = theModifiedAttribute->attributeType();
1421   if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1422     AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1423                                               theModifiedAttribute);
1424     AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1425                                               theSourceAttribute);
1426
1427     if (aModifiedAttribute.get() && aSourceAttribute.get())
1428       aModifiedAttribute->setValue(aSourceAttribute->pnt());
1429   }
1430   else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1431     AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1432                                               theModifiedAttribute);
1433     AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1434                                               theSourceAttribute);
1435
1436     if (aModifiedAttribute.get() && aSourceAttribute.get())
1437       aModifiedAttribute->setValue(aSourceAttribute->value());
1438   }
1439 }
1440
1441 FeaturePtr SketchPlugin_Split::createLineFeature(const FeaturePtr& theBaseFeature,
1442                                                            const AttributePtr& theFirstPointAttr,
1443                                                            const AttributePtr& theSecondPointAttr)
1444 {
1445   FeaturePtr aFeature;
1446   SketchPlugin_Sketch* aSketch = sketch();
1447   if (!aSketch || !theBaseFeature.get())
1448     return aFeature;
1449
1450   aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1451
1452   fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPointAttr);
1453   fillAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPointAttr);
1454
1455   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1456                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1457
1458   aFeature->execute(); // to obtain result
1459
1460   return aFeature;
1461 }
1462
1463 FeaturePtr SketchPlugin_Split::createArcFeature(const FeaturePtr& theBaseFeature,
1464                                                           const AttributePtr& theFirstPointAttr,
1465                                                           const AttributePtr& theSecondPointAttr)
1466 {
1467   FeaturePtr aFeature;
1468   SketchPlugin_Sketch* aSketch = sketch();
1469   if (!aSketch || !theBaseFeature.get())
1470     return aFeature;
1471
1472   std::string aCenterAttributeId;
1473   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1474     aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1475   else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1476     aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1477
1478   if (aCenterAttributeId.empty())
1479     return aFeature;
1480
1481   aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1482   // update fillet arc: make the arc correct for sure, so, it is not needed to process
1483   // the "attribute updated"
1484   // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1485   bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1486
1487   fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1488                 theBaseFeature->attribute(aCenterAttributeId));
1489   fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPointAttr);
1490   fillAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPointAttr);
1491
1492   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1493                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1494
1495   /// fill referersed state of created arc as it is on the base arc
1496   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1497     bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1498     aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1499   }
1500   aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1501   aFeature->execute(); // to obtain result
1502
1503   return aFeature;
1504 }
1505
1506 FeaturePtr SketchPlugin_Split::createConstraint(const std::string& theConstraintId,
1507                                                     const AttributePtr& theFirstAttribute,
1508                                                     const AttributePtr& theSecondAttribute)
1509 {
1510   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1511   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1512                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1513   aRefAttr->setAttr(theFirstAttribute);
1514
1515   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1516                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1517   aRefAttr->setAttr(theSecondAttribute);
1518
1519   return aConstraint;
1520 }
1521
1522 FeaturePtr SketchPlugin_Split::createConstraintForObjects(
1523                                                     const std::string& theConstraintId,
1524                                                     const ObjectPtr& theFirstObject,
1525                                                     const ObjectPtr& theSecondObject)
1526 {
1527   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1528   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1529                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1530   aRefAttr->setObject(theFirstObject);
1531
1532   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1533                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1534   aRefAttr->setObject(theSecondObject);
1535
1536   return aConstraint;
1537 }
1538
1539 void SketchPlugin_Split::updateFeaturesAfterSplit(
1540                                                    const std::set<FeaturePtr>& theFeaturesToUpdate)
1541 {
1542   std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
1543                                        aLast = theFeaturesToUpdate.end();
1544   for (; anIt != aLast; anIt++) {
1545     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1546     std::string aRefFeatureKind = aRefFeature->getKind();
1547     if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
1548       std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
1549                               std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
1550       if (aLenghtFeature.get()) {
1551         std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
1552             ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
1553         double aValue;
1554         if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
1555           aValueAttr->setValue(aValue);
1556       }
1557     }
1558   }
1559 }
1560
1561 std::shared_ptr<ModelAPI_Result> SketchPlugin_Split::getFeatureResult(
1562                                     const std::shared_ptr<ModelAPI_Feature>& theFeature)
1563 {
1564   std::shared_ptr<ModelAPI_Result> aResult;
1565
1566   std::string aFeatureKind = theFeature->getKind();
1567   if (aFeatureKind == SketchPlugin_Line::ID())
1568     aResult = theFeature->firstResult();
1569   else if (aFeatureKind == SketchPlugin_Arc::ID())
1570     aResult = theFeature->lastResult();
1571   else if (aFeatureKind == SketchPlugin_Circle::ID())
1572     aResult = theFeature->lastResult();
1573
1574   return aResult;
1575 }
1576
1577 std::set<std::shared_ptr<ModelAPI_Attribute> > SketchPlugin_Split::getEdgeAttributes(
1578                                            const std::shared_ptr<ModelAPI_Feature>& theFeature)
1579 {
1580   std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes;
1581
1582   std::string aFeatureKind = theFeature->getKind();
1583   if (aFeatureKind == SketchPlugin_Line::ID()) {
1584     anAttributes.insert(theFeature->attribute(SketchPlugin_Line::START_ID()));
1585     anAttributes.insert(theFeature->attribute(SketchPlugin_Line::END_ID()));
1586   }
1587   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
1588     anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::START_ID()));
1589     anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::END_ID()));
1590   }
1591   else if (aFeatureKind == SketchPlugin_Circle::ID()) {
1592   }
1593
1594   return anAttributes;
1595 }
1596
1597 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_Split::getPointAttribute
1598                                                               (const bool isFirstAttribute)
1599 {
1600   std::shared_ptr<GeomDataAPI_Point2D> anAttribute;
1601
1602   GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
1603   if (!aSelectedShape.get())
1604     return anAttribute;
1605
1606   if (aSelectedShape->shapeType() != GeomAPI_Shape::EDGE)
1607     return anAttribute;
1608
1609   AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1610                                                        data()->attribute(SELECTED_OBJECT()));
1611   ObjectPtr aBaseObject = anObjectAttr->value();
1612   if (!aBaseObject.get())
1613     return anAttribute;
1614
1615   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aSelectedShape));
1616
1617   std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
1618   std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
1619
1620   std::shared_ptr<GeomDataAPI_Point2D> aFirstPointAttr, aLastPointAttr;
1621   /// find the points in feature attributes
1622   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
1623   std::list<AttributePtr> a2DPointAttributes = aBaseFeature->data()->attributes(
1624                                                     GeomDataAPI_Point2D::typeId());
1625   std::list<AttributePtr>::const_iterator anIt = a2DPointAttributes.begin(),
1626                                           aLast = a2DPointAttributes.end();
1627   for (; anIt != aLast; anIt++) {
1628     std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
1629                                   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
1630     std::shared_ptr<GeomAPI_Pnt2d> aPoint2D = anAttributePoint->pnt();
1631     std::shared_ptr<GeomAPI_Pnt> aPoint3D = sketch()->to3D(aPoint2D->x(), aPoint2D->y());
1632     if (aFirstPnt->isEqual(aPoint3D))
1633       aFirstPointAttr = anAttributePoint;
1634     else if (aLastPnt->isEqual(aPoint3D))
1635       aLastPointAttr = anAttributePoint;
1636   }
1637
1638   /// find the points in coincident features
1639   PntToAttributesMap aRefAttributes = myCashedReferences[aBaseObject];
1640   PntToAttributesMap::const_iterator
1641     aRIt = aRefAttributes.begin(), aRLast = aRefAttributes.end();
1642   for (; aRIt != aRLast; aRIt++) {
1643     std::shared_ptr<GeomDataAPI_Point2D> anAttribute = aRIt->first;
1644     std::shared_ptr<GeomAPI_Pnt> aPoint = aRIt->second;
1645     if (!aFirstPointAttr.get() && aFirstPnt->isEqual(aPoint))
1646       aFirstPointAttr = anAttribute;
1647     if (!aLastPointAttr.get() && aLastPnt->isEqual(aPoint))
1648       aLastPointAttr = anAttribute;
1649     if (aFirstPointAttr.get() && aLastPointAttr.get())
1650       break;
1651   }
1652   if (!aFirstPointAttr.get() || !aLastPointAttr)
1653     return anAttribute;
1654
1655   return isFirstAttribute ? aFirstPointAttr : aLastPointAttr;
1656 }
1657
1658 #ifdef _DEBUG
1659 std::string SketchPlugin_Split::getFeatureInfo(const std::shared_ptr<ModelAPI_Feature>& theFeature,
1660                                                const bool isUseAttributesInfo)
1661 {
1662   std::string anInfo;
1663   if (!theFeature.get()) {
1664     return "none";
1665   }
1666
1667   if (theFeature->data()->isValid())
1668     anInfo.append(theFeature->data()->name().c_str());
1669
1670   if (isUseAttributesInfo) {
1671     std::string aPointsInfo = ModelGeomAlgo_Point2D::getPontAttributesInfo(theFeature,
1672                                                              getEdgeAttributes(theFeature));
1673     /// processing of feature with point 2d attributes, like line, arc, circle
1674     if (!aPointsInfo.empty()) {
1675       anInfo += ": ";
1676       anInfo += "\n";
1677       anInfo += aPointsInfo;
1678     }
1679     else { /// process constraint coincidence, find points in ref attr attributes
1680       std::list<AttributePtr> anAttrs = theFeature->data()->attributes(
1681                                                        ModelAPI_AttributeRefAttr::typeId());
1682       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
1683       std::string anAttributesInfo;
1684       for(; anIt != aLast; anIt++) {
1685         if (!anAttributesInfo.empty()) {
1686           anAttributesInfo.append(", ");
1687           anAttributesInfo += "\n";
1688         }
1689         AttributePtr anAttr = *anIt;
1690         std::string aValue = "not defined";
1691         std::string aType = anAttr->attributeType();
1692         if (aType == ModelAPI_AttributeRefAttr::typeId()) {
1693           std::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr =
1694                              std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
1695           if (aRefAttr.get()) {
1696             if (aRefAttr->isObject()) {
1697               FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1698               aValue = "<object:>" + getFeatureInfo(aFeature, false);
1699             }
1700             else {
1701               AttributePtr anAttribute = aRefAttr->attr();
1702               if (anAttribute.get()) {
1703                 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
1704                 aValue = "<attr:>" + ModelGeomAlgo_Point2D::getPointAttributeInfo(anAttribute) +
1705                          " [" + getFeatureInfo(aFeature, false) + "]";
1706               }
1707             }
1708           }
1709         }
1710         anAttributesInfo.append("    " + anAttr->id() + ": " + aValue);
1711       }
1712       if (!anAttributesInfo.empty())
1713         anInfo = anInfo + "\n" + anAttributesInfo;
1714     }
1715   }
1716   return anInfo;
1717 }
1718 #endif