Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintSplit.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintSplit.cpp
4 // Created: 25 Aug 2016
5 // Author:  Natalia ERMOLAEVA
6
7 #include "SketchPlugin_ConstraintSplit.h"
8
9 #include <GeomAPI_Dir2d.h>
10 #include <GeomAPI_Pnt2d.h>
11 #include <GeomAPI_XY.h>
12 #include <GeomDataAPI_Point2D.h>
13 #include <GeomAlgoAPI_ShapeTools.h>
14
15 #include <ModelAPI_AttributeReference.h>
16 #include <ModelAPI_AttributeString.h>
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_Tools.h>
19 #include <ModelAPI_AttributeBoolean.h>
20
21 #include <ModelAPI_Validator.h>
22 #include <ModelAPI_Session.h>
23 #include <ModelAPI_AttributeDouble.h>
24
25 #include <SketchPlugin_Line.h>
26 #include <SketchPlugin_Arc.h>
27 #include <SketchPlugin_Circle.h>
28 #include <SketchPlugin_ConstraintCoincidence.h>
29 #include <SketchPlugin_ConstraintEqual.h>
30 #include <SketchPlugin_ConstraintParallel.h>
31 #include <SketchPlugin_ConstraintTangent.h>
32 #include <SketchPlugin_ConstraintLength.h>
33 #include <SketchPlugin_ConstraintMirror.h>
34 #include <SketchPlugin_MultiRotation.h>
35 #include <SketchPlugin_MultiTranslation.h>
36 #include <SketchPlugin_ConstraintMiddle.h>
37
38 #include <ModelAPI_Events.h>
39 #include <SketchPlugin_Line.h>
40 #include <SketchPlugin_Arc.h>
41 #include <SketchPlugin_Circle.h>
42
43 #include <ModelGeomAlgo_Point2D.h>
44 #include <Events_Loop.h>
45
46 #include <cmath>
47
48 //#define CREATE_CONSTRAINTS
49
50 //#define DEBUG_SPLIT
51 #ifdef DEBUG_SPLIT
52 #include <iostream>
53 #endif
54
55 static const double PI = 3.141592653589793238463;
56
57 SketchPlugin_ConstraintSplit::SketchPlugin_ConstraintSplit()
58 {
59 }
60
61 void SketchPlugin_ConstraintSplit::initAttributes()
62 {
63   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeReference::typeId());
64   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
65   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
66 }
67
68 void SketchPlugin_ConstraintSplit::execute()
69 {
70   std::shared_ptr<ModelAPI_Data> aData = data();
71
72   // Check the base objects are initialized.
73   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
74                                             aData->attribute(SketchPlugin_Constraint::VALUE()));
75   if(!aBaseObjectAttr->isInitialized()) {
76     setError("Error: Base object is not initialized.");
77     return;
78   }
79   AttributePoint2DPtr aFirstPointAttrOfSplit =
80     getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
81   AttributePoint2DPtr aSecondPointAttrOfSplit =
82     getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
83   if (!aFirstPointAttrOfSplit.get() || !aFirstPointAttrOfSplit->isInitialized() ||
84       !aSecondPointAttrOfSplit.get() || !aSecondPointAttrOfSplit->isInitialized()) {
85     setError("Error: Sub-shape is not initialized.");
86     return;
87   }
88
89   // Wait all constraints being created, then send update events
90   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
91   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
92   if (isUpdateFlushed)
93     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
94
95
96   // Find feature constraints
97   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
98   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
99   std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
100
101   //std::map<FeaturePtr, IdToPointPair> aTangentFeatures;
102   std::map<FeaturePtr, IdToPointPair> aCoincidenceToFeature;
103   getConstraints(aFeaturesToDelete, aFeaturesToUpdate, /*aTangentFeatures, */
104                  aCoincidenceToFeature);
105
106   std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
107   std::list<AttributePtr> aRefsToFeature;
108   getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
109
110   std::map<AttributePtr, AttributePtr> aBasePointModifiedAttributes;
111
112 #ifdef DEBUG_SPLIT
113   std::cout << std::endl;
114   std::cout << "SketchPlugin_ConstraintSplit::execute()" << std::endl;
115   std::cout << std::endl;
116
117   SketchPlugin_Sketch* aSketch = sketch();
118   std::cout << "SKETCH FEATURES (before split) [" << aSketch->numberOfSubs() << "]:" << std::endl;
119   for (int i = 0, aNbSubs = aSketch->numberOfSubs(); i < aNbSubs; i++) {
120     std::cout << getFeatureInfo(aSketch->subFeature(i), false) << std::endl;
121   }
122
123   std::cout << std::endl;
124   std::cout << "---- IN PARAMETERS ----" << std::endl;
125   std::cout << "Base feature:" << getFeatureInfo(aBaseFeature) << std::endl;
126   std::cout << std::endl;
127
128   if (!aCoincidenceToFeature.empty()) {
129     std::cout << "Coincidences to base feature[" <<
130       aCoincidenceToFeature.size() << "]: " << std::endl;
131     std::map<FeaturePtr, IdToPointPair>::const_iterator anIt = aCoincidenceToFeature.begin(),
132                                                         aLast = aCoincidenceToFeature.end();
133     for (int i = 1; anIt != aLast; anIt++, i++) {
134       FeaturePtr aFeature = (*anIt).first;
135       std::string anAttributeId = (*anIt).second.first;
136       std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = (*anIt).second.second;
137
138       std::cout << i << "-" << getFeatureInfo(aFeature) << std::endl;
139       std::cout <<     " -Attribute to correct:" << anAttributeId << std::endl;
140       std::cout <<     " -Point attribute:" <<
141         ModelGeomAlgo_Point2D::getPointAttributeInfo(aPointAttr) << std::endl;
142     }
143   }
144
145   std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
146     aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
147   std::cout << std::endl << "References to attributes of base feature [" <<
148     aBaseRefAttributes.size() << "]" << std::endl;
149   for (; aRefIt != aRefLast; aRefIt++) {
150     AttributePtr aBaseAttr = aRefIt->first;
151     std::list<AttributePtr> aRefAttributes = aRefIt->second;
152     std::string aRefsInfo;
153     std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
154                                             aRefAttrLast = aRefAttributes.end();
155     for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
156       if (!aRefsInfo.empty())
157         aRefsInfo.append(",");
158       AttributePtr aRAttr = *aRefAttrIt;
159       aRefsInfo.append(aRAttr->id());
160       FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
161       aRefsInfo.append("(" + aRFeature->name() + ") ");
162     }
163     std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
164       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
165     std::cout << aPointAttr->id().c_str() <<
166       ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
167   }
168   std::cout << std::endl;
169   std::cout << std::endl << "References to base feature [" <<
170     aRefsToFeature.size() << "]" << std::endl;
171   std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
172                                           aRefAttrLast = aRefsToFeature.end();
173   std::string aRefsInfo;
174   for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
175     if (!aRefsInfo.empty())
176       aRefsInfo.append(",");
177     AttributePtr aRAttr = *aRefAttrIt;
178     aRefsInfo.append(aRAttr->id());
179     FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
180     aRefsInfo.append("(" + aRFeature->name() + ") ");
181   }
182   std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
183
184
185   std::cout << std::endl;
186   std::cout << "---- SPLIT ----" << std::endl;
187   std::cout << std::endl;
188 #endif
189
190   std::string aFeatureKind = aBaseFeature->getKind();
191   FeaturePtr aSplitFeature, anAfterFeature;
192   std::set<AttributePoint2DPtr> aFurtherCoincidences;
193   std::set<FeaturePtr> aCreatedFeatures;
194   std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
195   if (aFeatureKind == SketchPlugin_Line::ID())
196     splitLine(aSplitFeature, aBaseFeature, anAfterFeature, aFurtherCoincidences, aCreatedFeatures,
197               aModifiedAttributes);
198   else if (aFeatureKind == SketchPlugin_Arc::ID())
199     splitArc(aSplitFeature, aBaseFeature, anAfterFeature, aFurtherCoincidences, aCreatedFeatures,
200              aModifiedAttributes);
201   if (aFeatureKind == SketchPlugin_Circle::ID()) {
202     FeaturePtr aCircleFeature = aBaseFeature;
203     splitCircle(aSplitFeature, aBaseFeature, anAfterFeature, aFurtherCoincidences,
204       aCreatedFeatures, aModifiedAttributes);
205
206     updateRefFeatureConstraints(getFeatureResult(aBaseFeature), aRefsToFeature);
207
208     AttributePtr aCenterAttr = aCircleFeature->attribute(SketchPlugin_Circle::CENTER_ID());
209     aFeaturesToDelete.insert(aCircleFeature);
210     // as circle is removed, temporary fill this attribute*/
211     aBaseObjectAttr->setObject(ResultPtr());
212   }
213
214 #ifdef DEBUG_SPLIT
215   std::cout << "---- OUT PARAMETERS ----" << std::endl;
216   std::cout << "Base modified feature:" << getFeatureInfo(aBaseFeature) << std::endl;
217   std::cout << "Split feature:" << getFeatureInfo(aSplitFeature) << std::endl;
218   std::cout << "After feature:" << getFeatureInfo(anAfterFeature) << std::endl;
219   std::cout << std::endl;
220
221   std::cout << "Created features by split:[" << aCreatedFeatures.size() << "]" << std::endl;
222   std::set<FeaturePtr>::const_iterator aFIt = aCreatedFeatures.begin(),
223                                        aFLast = aCreatedFeatures.end();
224   for (; aFIt != aFLast; aFIt++) {
225     std::cout << getFeatureInfo(*aFIt) << std::endl;
226   }
227   std::cout << std::endl;
228
229   std::cout << "Attributes for further Coincidences:" << std::endl;
230   std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
231                                                 aLast = aFurtherCoincidences.end();
232   for (; anIt != aLast; anIt++) {
233     AttributePtr anAttribute = *anIt;
234     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
235     std::cout << ModelGeomAlgo_Point2D::getPointAttributeInfo(anAttribute)
236               << " [" << getFeatureInfo(aFeature, false) << "]" << std::endl;
237   }
238
239   std::cout << "Modifed attributes (constraints to attributes are moved here):" << std::endl;
240   std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
241     aPIt = aModifiedAttributes.begin(), aPLast = aModifiedAttributes.end();
242   std::string aResInfo;
243   for (; aPIt != aPLast; aPIt++) {
244     if (!aResInfo.empty())
245       aResInfo += "\n";
246
247     std::pair<AttributePtr, AttributePtr> aPair = *aPIt;
248
249     AttributePtr anAttr = aPair.first;
250     aResInfo.append(anAttr->id());
251     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->owner());
252     aResInfo.append("(" + aFeature->name() + ") ");
253
254     aResInfo.append("  - is modified to -  ");
255
256     anAttr = aPair.second;
257     aResInfo.append(anAttr->id());
258     aFeature = ModelAPI_Feature::feature(anAttr->owner());
259     aResInfo.append("(" + aFeature->name() + ") ");
260   }
261   std::cout << aResInfo << std::endl;
262 #endif
263
264   std::set<ResultPtr> aFeatureResults;
265   aFeatureResults.insert(getFeatureResult(aBaseFeature));
266   if (anAfterFeature.get() && anAfterFeature != aBaseFeature)
267     aFeatureResults.insert(getFeatureResult(anAfterFeature));
268
269   // coincidence to feature
270   updateCoincidenceConstraintsToFeature(aCoincidenceToFeature, aFurtherCoincidences,
271                                         aFeatureResults, aSplitFeature);
272
273   updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes);
274
275   // delete constraints
276 #ifdef DEBUG_SPLIT
277   std::cout << "remove features and references:" << std::endl;
278   std::set<FeaturePtr>::const_iterator aDIt = aFeaturesToDelete.begin(),
279                                        aDLast = aFeaturesToDelete.end();
280   for (; aDIt != aDLast; aDIt++) {
281     std::cout << getFeatureInfo(*aDIt, false) << std::endl;
282     std::cout << std::endl;
283   }
284 #endif
285   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
286   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
287
288 #ifdef DEBUG_SPLIT
289   std::cout << "update features after split:" << std::endl;
290   std::set<FeaturePtr>::const_iterator anUIt = aFeaturesToUpdate.begin(),
291                                        anULast = aFeaturesToUpdate.end();
292   for (; anUIt != anULast; anUIt++) {
293     std::cout << getFeatureInfo(*anUIt, false) << std::endl;
294     std::cout << std::endl;
295   }
296 #endif
297   updateFeaturesAfterSplit(aFeaturesToUpdate);
298
299   // Send events to update the sub-features by the solver.
300   if(isUpdateFlushed) {
301     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
302   }
303
304 #ifdef DEBUG_SPLIT
305   std::cout << "SKETCH FEATURES (after split) [" << aSketch->numberOfSubs() << "]:" << std::endl;
306   for (int i = 0, aNbSubs = aSketch->numberOfSubs(); i < aNbSubs; i++) {
307     std::cout << getFeatureInfo(aSketch->subFeature(i), false) << std::endl;
308   }
309 #endif
310 }
311
312 bool SketchPlugin_ConstraintSplit::isMacro() const
313 {
314   return true;
315 }
316
317 AISObjectPtr SketchPlugin_ConstraintSplit::getAISObject(AISObjectPtr thePrevious)
318 {
319   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
320                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
321   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
322
323   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointOfRefAttr(
324                                         data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
325   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointOfRefAttr(
326                                         data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
327
328   if (aBaseObjectAttr->isInitialized() && aBaseFeature.get() &&
329       aFirstPointAttrOfSplit->isInitialized() &&
330       aSecondPointAttrOfSplit->isInitialized()) {
331
332     ResultPtr aResult = getFeatureResult(aBaseFeature);
333     GeomShapePtr aBaseShape = aResult->shape();
334     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
335
336     std::shared_ptr<GeomAPI_Pnt2d> aStartPnt2d = aFirstPointAttrOfSplit->pnt();
337     std::shared_ptr<GeomAPI_Pnt> aStartPoint = sketch()->to3D(aStartPnt2d->x(), aStartPnt2d->y());
338     aPoints.push_back(aStartPoint);
339
340     std::shared_ptr<GeomAPI_Pnt2d> aSecondPnt2d = aSecondPointAttrOfSplit->pnt();
341     std::shared_ptr<GeomAPI_Pnt> aSecondPoint =
342       sketch()->to3D(aSecondPnt2d->x(), aSecondPnt2d->y());
343     aPoints.push_back(aSecondPoint);
344
345     std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
346
347     GeomAlgoAPI_ShapeTools::splitShape_p(aBaseShape, aPoints, aSplitShapes);
348     std::shared_ptr<GeomAPI_Shape> aShape =
349       GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
350
351     AISObjectPtr anAIS = thePrevious;
352     if (aShape) {
353       if (!anAIS)
354         anAIS = AISObjectPtr(new GeomAPI_AISObject);
355       anAIS->createShape(aShape);
356       std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
357              aBaseFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID());
358
359       bool isConstruction = anAuxiliaryAttr.get() != NULL && anAuxiliaryAttr->value();
360
361       std::vector<int> aColor;
362       double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
363       int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
364       if (isConstruction) {
365         aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color");
366         aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH_AUXILIARY();
367         aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY();
368       }
369       else {
370         aColor = Config_PropManager::color("Visualization", "sketch_entity_color");
371       }
372       anAIS->setColor(aColor[0], aColor[1], aColor[2]);
373       anAIS->setWidth(aWidth + 1);
374       anAIS->setLineStyle(aLineStyle);
375     }
376     return anAIS;
377   }
378   return AISObjectPtr();
379 }
380
381 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_ConstraintSplit::getPointOfRefAttr(
382                                                       const AttributePtr& theAttribute)
383 {
384   AttributePoint2DPtr aPointAttribute;
385
386   if (theAttribute->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
387     AttributeRefAttrPtr aRefAttr =
388       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
389     if (aRefAttr.get() && aRefAttr->isInitialized()) {
390       AttributePtr anAttribute = aRefAttr->attr();
391       if (anAttribute.get() && anAttribute->attributeType() == GeomDataAPI_Point2D::typeId())
392         aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
393     }
394   }
395   return aPointAttribute;
396 }
397
398 void SketchPlugin_ConstraintSplit::getFeaturePoints(const FeaturePtr& theFeature,
399                                                     AttributePoint2DPtr& theStartPointAttr,
400                                                     AttributePoint2DPtr& theEndPointAttr)
401 {
402   std::string aFeatureKind = theFeature->getKind();
403   std::string aStartAttributeName, anEndAttributeName;
404   if (aFeatureKind == SketchPlugin_Line::ID()) {
405     aStartAttributeName = SketchPlugin_Line::START_ID();
406     anEndAttributeName = SketchPlugin_Line::END_ID();
407   }
408   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
409     aStartAttributeName = SketchPlugin_Arc::START_ID();
410     anEndAttributeName = SketchPlugin_Arc::END_ID();
411   }
412   if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
413     theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
414                                          theFeature->attribute(aStartAttributeName));
415     theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
416                                          theFeature->attribute(anEndAttributeName));
417   }
418 }
419
420 void SketchPlugin_ConstraintSplit::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
421                                       std::set<FeaturePtr>& theFeaturesToUpdate,
422                                       std::map<FeaturePtr, IdToPointPair>& theCoincidenceToFeature)
423 {
424   std::shared_ptr<ModelAPI_Data> aData = data();
425
426   // Check the base objects are initialized.
427   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
428                                             aData->attribute(SketchPlugin_Constraint::VALUE()));
429   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
430   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
431
432   std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
433   std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
434   aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
435
436   std::set<AttributePtr>::const_iterator aIt;
437   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
438     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
439     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
440     std::string aRefFeatureKind = aRefFeature->getKind();
441     if (aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() ||
442         aRefFeatureKind == SketchPlugin_MultiRotation::ID() ||
443         aRefFeatureKind == SketchPlugin_MultiTranslation::ID() ||
444         aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
445       theFeaturesToDelete.insert(aRefFeature);
446     else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
447       theFeaturesToUpdate.insert(aRefFeature);
448     else if (aRefFeatureKind == SketchPlugin_ConstraintCoincidence::ID()) {
449       std::string anAttributeToBeModified;
450       AttributePoint2DPtr aCoincidentPoint;
451       AttributeRefAttrPtr anAttrA = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_A());
452       AttributeRefAttrPtr anAttrB = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_B());
453       bool isToFeature = false;
454       if (anAttrA->isObject() || anAttrB->isObject()) { /// coincidence to base feature
455         FeaturePtr aFeature = anAttrA->isObject() ? ModelAPI_Feature::feature(anAttrA->object())
456                                                   : FeaturePtr();
457         isToFeature = aFeature.get() && aFeature == aBaseFeature;
458         anAttributeToBeModified = anAttrA->id();
459         if (!isToFeature) {
460           aFeature = anAttrB->isObject() ? ModelAPI_Feature::feature(anAttrB->object())
461                                          : FeaturePtr();
462           isToFeature = aFeature.get() && aFeature == aBaseFeature;
463           anAttributeToBeModified = anAttrB->id();
464         }
465         if (isToFeature)
466           aCoincidentPoint = SketchPlugin_ConstraintCoincidence::getPoint(aRefFeature);
467       }
468       if (!isToFeature) { /// coincidence to point on base feature
469         AttributePtr anAttribute;
470
471         if (!anAttrA->isObject()) {
472           AttributePtr aCurAttribute = anAttrA->attr();
473           if (aCurAttribute.get()) {
474             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
475             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
476               anAttribute = anAttrB->attr();
477               anAttributeToBeModified = anAttrA->id();
478             }
479           }
480         }
481         if (!anAttribute.get() && !anAttrB->isObject()) {
482           AttributePtr aCurAttribute = anAttrB->attr();
483           if (aCurAttribute.get()) {
484             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
485             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
486               anAttribute = anAttrA->attr();
487               anAttributeToBeModified = anAttrB->id();
488             }
489           }
490         }
491         if (anAttribute.get())
492           aCoincidentPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
493       }
494       if (aCoincidentPoint.get() && isToFeature)
495         theCoincidenceToFeature[aRefFeature] = std::make_pair(anAttributeToBeModified,
496                                                               aCoincidentPoint);
497     }
498   }
499 }
500
501 void SketchPlugin_ConstraintSplit::getRefAttributes(const FeaturePtr& theFeature,
502                                     std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
503                                     std::list<AttributePtr>& theRefsToFeature)
504 {
505   theRefs.clear();
506
507   std::list<AttributePtr> aPointAttributes =
508     theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
509   std::set<AttributePtr> aPointAttributesSet;
510
511   std::list<AttributePtr>::const_iterator aPIt =
512     aPointAttributes.begin(), aPLast = aPointAttributes.end();
513   for (; aPIt != aPLast; aPIt++)
514     aPointAttributesSet.insert(*aPIt);
515
516   std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
517   std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
518   aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
519
520   std::set<AttributePtr>::const_iterator aIt;
521   for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
522     AttributePtr anAttr = (*aIt);
523     FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
524     if (anAttrFeature.get() != this &&
525         anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
526       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
527       if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
528         AttributePtr anAttrInRef = aRefAttr->attr();
529         if (anAttrInRef.get() &&
530             aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
531           if (theRefs.find(anAttrInRef) != theRefs.end())
532             theRefs[anAttrInRef].push_back(aRefAttr);
533           else {
534             std::list<AttributePtr> anAttrList;
535             anAttrList.push_back(aRefAttr);
536             theRefs[anAttrInRef] = anAttrList;
537           }
538         }
539       }
540       else { /// find attributes referenced to feature itself
541         theRefsToFeature.push_back(anAttr);
542       }
543     }
544   }
545 }
546
547 void SketchPlugin_ConstraintSplit::updateCoincidenceConstraintsToFeature(
548       const std::map<std::shared_ptr<ModelAPI_Feature>, IdToPointPair>& theCoincidenceToFeature,
549       const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences,
550       const std::set<ResultPtr>& theFeatureResults,
551       const FeaturePtr& theSplitFeature)
552 {
553   if (theCoincidenceToFeature.empty())
554     return;
555
556   // we should build coincidence constraints to end of the split feature
557   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aNewCoincidencesToSplitFeature;
558   AttributePoint2DPtr aStartPointAttr, anEndPointAttr;
559   getFeaturePoints(theSplitFeature, aStartPointAttr, anEndPointAttr);
560   if (theFurtherCoincidences.find(aStartPointAttr) == theFurtherCoincidences.end())
561     aNewCoincidencesToSplitFeature.insert(aStartPointAttr);
562   if (theFurtherCoincidences.find(anEndPointAttr) == theFurtherCoincidences.end())
563     aNewCoincidencesToSplitFeature.insert(anEndPointAttr);
564
565   std::map<FeaturePtr, IdToPointPair>::const_iterator aCIt = theCoincidenceToFeature.begin(),
566                                                             aCLast = theCoincidenceToFeature.end();
567 #ifdef DEBUG_SPLIT
568   std::cout << std::endl;
569   std::cout << "Coincidences to feature(modified):"<< std::endl;
570 #endif
571   for (; aCIt != aCLast; aCIt++) {
572     FeaturePtr aCoincFeature = aCIt->first;
573     std::string anAttributeId = aCIt->second.first;
574     AttributePoint2DPtr aCoincPoint = aCIt->second.second;
575     std::set<AttributePoint2DPtr>::const_iterator aFCIt = theFurtherCoincidences.begin(),
576                                                   aFCLast = theFurtherCoincidences.end();
577     std::shared_ptr<GeomAPI_Pnt2d> aCoincPnt = aCoincPoint->pnt();
578     AttributePoint2DPtr aFeaturePointAttribute;
579     for (; aFCIt != aFCLast && !aFeaturePointAttribute.get(); aFCIt++) {
580       AttributePoint2DPtr aFCAttribute = *aFCIt;
581       if (aCoincPnt->isEqual(aFCAttribute->pnt()))
582         aFeaturePointAttribute = aFCAttribute;
583     }
584     if (aFeaturePointAttribute.get()) {
585       aCoincFeature->refattr(anAttributeId)->setObject(ResultPtr());
586       aCoincFeature->refattr(anAttributeId)->setAttr(aFeaturePointAttribute);
587       // create new coincidences to split feature points
588       std::set<AttributePoint2DPtr>::const_iterator aSFIt = aNewCoincidencesToSplitFeature.begin(),
589                                                     aSFLast = aNewCoincidencesToSplitFeature.end();
590       for (; aSFIt != aSFLast; aSFIt++) {
591         AttributePoint2DPtr aSFAttribute = *aSFIt;
592         if (aCoincPnt->isEqual(aSFAttribute->pnt())) {
593           std::string aSecondAttribute = SketchPlugin_Constraint::ENTITY_A();
594           if (anAttributeId == SketchPlugin_Constraint::ENTITY_A())
595             aSecondAttribute = SketchPlugin_Constraint::ENTITY_B();
596           createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
597                            aSFAttribute, aCoincFeature->refattr(aSecondAttribute)->attr());
598         }
599       }
600     }
601     else {
602       /// find feature by shape intersected the point
603       ResultPtr aResultForCoincidence = *(theFeatureResults.begin());
604
605       if (theFeatureResults.size() > 1) { // try to find point on additional feature
606         ResultPtr anAddtionalResult = *(theFeatureResults.begin()++);
607         GeomShapePtr aShape = anAddtionalResult->shape();
608
609         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCoincPoint->pnt();
610         std::shared_ptr<GeomAPI_Pnt> aPoint = sketch()->to3D(aPnt2d->x(), aPnt2d->y());
611
612         std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
613         if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPoint, aProjectedPoint))
614           aResultForCoincidence = anAddtionalResult;
615       }
616       aCoincFeature->refattr(anAttributeId)->setObject(aResultForCoincidence);
617     }
618 #ifdef DEBUG_SPLIT
619   std::cout << " -" << getFeatureInfo(aCoincFeature) << std::endl;
620 #endif
621   }
622 }
623
624 void SketchPlugin_ConstraintSplit::updateRefFeatureConstraints(
625                                                   const ResultPtr& theFeatureBaseResult,
626                                                   const std::list<AttributePtr>& theRefsToFeature)
627 {
628   std::list<AttributePtr>::const_iterator anIt = theRefsToFeature.begin(),
629                                           aLast = theRefsToFeature.end();
630   for (; anIt != aLast; anIt++) {
631     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIt);
632     if (aRefAttr.get())
633       aRefAttr->setObject(theFeatureBaseResult);
634   }
635 }
636
637 void SketchPlugin_ConstraintSplit::updateRefAttConstraints(
638                     const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
639                     const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes)
640 {
641 #ifdef DEBUG_SPLIT
642   std::cout << "SketchPlugin_ConstraintSplit::updateRefAttConstraints" << std::endl;
643 #endif
644
645   std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
646     anIt = theModifiedAttributes.begin(),  aLast = theModifiedAttributes.end();
647   for (; anIt != aLast; anIt++) {
648     AttributePtr anAttribute = anIt->first;
649
650     /// not found in references
651     if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
652       continue;
653     std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
654     std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
655                                             aRLast = aRefAttributes.end();
656
657     AttributePtr aNewAttribute = anIt->second;
658     for (; aRefIt != aRLast; aRefIt++) {
659       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
660       if (aRefAttr.get()) {
661         aRefAttr->setAttr(aNewAttribute);
662 #ifdef DEBUG_SPLIT
663         FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->owner());
664         std::cout << " -" << getFeatureInfo(aFeature) << std::endl;
665 #endif
666       }
667     }
668   }
669 }
670
671 void SketchPlugin_ConstraintSplit::splitLine(FeaturePtr& theSplitFeature,
672                                              FeaturePtr& theBaseFeatureModified,
673                                              FeaturePtr& theAfterFeature,
674                                              std::set<AttributePoint2DPtr>& thePoints,
675                                              std::set<FeaturePtr>& theCreatedFeatures,
676                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
677 {
678   std::set<FeaturePtr> aCreatedFeatures;
679   FeaturePtr aConstraintFeature;
680   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
681
682   SketchPlugin_Sketch* aSketch = sketch();
683   if (!aSketch)
684     return;
685
686   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
687                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
688   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
689   std::string aFeatureKind = aBaseFeature->getKind();
690   if (aFeatureKind != SketchPlugin_Line::ID())
691     return;
692
693   AttributePoint2DPtr aFirstPointAttrOfSplit =
694     getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
695   AttributePoint2DPtr aSecondPointAttrOfSplit =
696     getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
697   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
698
699   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
700   if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
701     setError("Error: Feature has no start and end points.");
702     return;
703   }
704
705   arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
706                       aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
707
708 #ifdef DEBUG_SPLIT
709   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
710   std::cout << "Start point: " <<
711     ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
712   std::cout << "1st point:   " <<
713     ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
714   std::cout << "2nd point:   " <<
715     ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
716   std::cout << "End point:   " <<
717     ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
718 #endif
719
720   /// create a split feature
721   theSplitFeature =
722     createLineFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
723   theCreatedFeatures.insert(theSplitFeature);
724
725   // before split feature
726   if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
727     theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
728                                         theSplitFeature->attribute(SketchPlugin_Line::START_ID())));
729   }
730   else {
731     theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
732     /// move end arc point to start of split
733   }
734
735   // after split feature
736   if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
737     FeaturePtr aFeature;
738     if (!theBaseFeatureModified.get()) {
739       aFeature = aBaseFeature; ///< use base feature to store all constraints here
740       fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), aSecondPointAttrOfSplit);
741       aFeature->execute(); // to update result
742     }
743     else {
744       aFeature = createLineFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
745       theCreatedFeatures.insert(aFeature);
746       theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
747                                              aFeature->attribute(SketchPlugin_Line::END_ID())));
748     }
749     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
750                      theSplitFeature->attribute(SketchPlugin_Line::END_ID()),
751                      aFeature->attribute(SketchPlugin_Line::START_ID()));
752     theCreatedFeatures.insert(aConstraintFeature);
753
754     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
755                                 (aFeature->attribute(SketchPlugin_Line::START_ID())));
756     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
757                                 (aFeature->attribute(SketchPlugin_Line::END_ID())));
758
759     if (!theBaseFeatureModified.get())
760       theBaseFeatureModified = aFeature;
761     else
762       theAfterFeature = aFeature;
763   }
764   else {
765     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
766                                   (theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
767     theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
768                                    theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
769   }
770   // base split, that is defined before split feature should be changed at end
771   // (after the after feature creation). Otherwise modified value will be used in after feature
772   // before split feature
773   if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
774     /// move end arc point to start of split
775     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
776                                                     aFirstPointAttrOfSplit);
777     theBaseFeatureModified->execute(); // to update result
778     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
779                      theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
780                      theSplitFeature->attribute(SketchPlugin_Line::START_ID()));
781     theCreatedFeatures.insert(aConstraintFeature);
782
783     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
784                              (theBaseFeatureModified->attribute(SketchPlugin_Line::START_ID())));
785     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
786                                (theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID())));
787   }
788   else
789     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
790                                        (theSplitFeature->attribute(SketchPlugin_Line::START_ID())));
791
792 #ifdef CREATE_CONSTRAINTS
793   // additional constraints between split and base features
794   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintParallel::ID(),
795                                                        getFeatureResult(aBaseFeature),
796                                                        getFeatureResult(theSplitFeature));
797   theCreatedFeatures.insert(aConstraintFeature);
798   if (theAfterFeature.get()) {
799     aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintParallel::ID(),
800                                                     getFeatureResult(aBaseFeature),
801                                                     getFeatureResult(theAfterFeature));
802     theCreatedFeatures.insert(aConstraintFeature);
803   }
804 #endif
805 }
806
807 void SketchPlugin_ConstraintSplit::splitArc(FeaturePtr& theSplitFeature,
808                                             FeaturePtr& theBaseFeatureModified,
809                                             FeaturePtr& theAfterFeature,
810                                             std::set<AttributePoint2DPtr>& thePoints,
811                                             std::set<FeaturePtr>& theCreatedFeatures,
812                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
813 {
814   std::set<FeaturePtr> aCreatedFeatures;
815   FeaturePtr aConstraintFeature;
816   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
817
818   SketchPlugin_Sketch* aSketch = sketch();
819   if (!aSketch)
820     return;
821
822   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
823                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
824   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
825   std::string aFeatureKind = aBaseFeature->getKind();
826   if (aFeatureKind != SketchPlugin_Arc::ID())
827     return;
828
829   AttributePoint2DPtr aFirstPointAttrOfSplit =
830     getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
831   AttributePoint2DPtr aSecondPointAttrOfSplit =
832     getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
833   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
834   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
835   if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
836     setError("Error: Feature has no start and end points.");
837     return;
838   }
839
840   // manually change type of arc to avoid incorrect self-constrainting of the tangent arc
841   aBaseFeature->string(SketchPlugin_Arc::ARC_TYPE())->setValue(
842       SketchPlugin_Arc::ARC_TYPE_CENTER_START_END());
843
844   arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
845                      aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
846 #ifdef DEBUG_SPLIT
847   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
848   std::cout << "Start point: " <<
849     ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
850   std::cout << "1st point:   " <<
851     ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
852   std::cout << "2nd point:   " <<
853     ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
854   std::cout << "End point:   " <<
855     ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
856 #endif
857
858   /// split feature
859   theSplitFeature = createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
860   theCreatedFeatures.insert(theSplitFeature);
861
862   // before split feature
863   if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
864     theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
865                                   theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
866   }
867   else {
868     theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
869     /// move end arc point to start of split
870   }
871
872   // after split feature
873   if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
874     FeaturePtr aFeature;
875     if (!theBaseFeatureModified.get()) {
876       aFeature = aBaseFeature; ///< use base feature to store all constraints here
877       fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), aSecondPointAttrOfSplit);
878       aFeature->execute(); // to update result
879     }
880     else {
881       aFeature = createArcFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
882       theCreatedFeatures.insert(aFeature);
883       theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
884                                                   aFeature->attribute(SketchPlugin_Arc::END_ID())));
885     }
886     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
887                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()),
888                      aFeature->attribute(SketchPlugin_Arc::START_ID()));
889     theCreatedFeatures.insert(aConstraintFeature);
890
891     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
892                                 (aFeature->attribute(SketchPlugin_Arc::START_ID())));
893     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
894                                 (aFeature->attribute(SketchPlugin_Arc::END_ID())));
895
896     if (!theBaseFeatureModified.get())
897       theBaseFeatureModified = aFeature;
898     else
899       theAfterFeature = aFeature;
900   }
901   else {
902     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
903                                   (theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
904     theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
905                                    theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
906   }
907   // base split, that is defined before split feature should be changed at end
908   // (after the after feature creation). Otherwise modified value will be used in after feature
909   // before split feature
910   if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
911     /// move end arc point to start of split
912     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
913                                                     aFirstPointAttrOfSplit);
914     theBaseFeatureModified->execute(); // to update result
915     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
916                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
917                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
918     theCreatedFeatures.insert(aConstraintFeature);
919
920     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
921                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
922     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
923                                (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
924   }
925   else
926     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
927                                        (theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
928
929   // additional constraints between split and base features
930 #ifdef CREATE_CONSTRAINTS
931   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
932                                                        getFeatureResult(aBaseFeature),
933                                                        getFeatureResult(theSplitFeature));
934   theCreatedFeatures.insert(aConstraintFeature);
935   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
936                                                        getFeatureResult(theSplitFeature),
937                                                        getFeatureResult(aBaseFeature));
938   theCreatedFeatures.insert(aConstraintFeature);
939   if (theAfterFeature.get()) {
940     aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
941                                                     getFeatureResult(aBaseFeature),
942                                                     getFeatureResult(theAfterFeature));
943     theCreatedFeatures.insert(aConstraintFeature);
944     aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
945                                                     getFeatureResult(theSplitFeature),
946                                                     getFeatureResult(theAfterFeature));
947     theCreatedFeatures.insert(aConstraintFeature);
948   }
949 #endif
950 }
951
952 void SketchPlugin_ConstraintSplit::splitCircle(FeaturePtr& theSplitFeature,
953                                                FeaturePtr& theBaseFeatureModified,
954                                                FeaturePtr& theAfterFeature,
955                                                std::set<AttributePoint2DPtr>& thePoints,
956                                                std::set<FeaturePtr>& theCreatedFeatures,
957                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
958 {
959   std::set<FeaturePtr> aCreatedFeatures;
960   FeaturePtr aConstraintFeature;
961   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
962
963   SketchPlugin_Sketch* aSketch = sketch();
964   if (!aSketch)
965     return;
966
967   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
968                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
969   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
970   std::string aFeatureKind = aBaseFeature->getKind();
971   if (aFeatureKind != SketchPlugin_Circle::ID())
972     return;
973
974   AttributePoint2DPtr aFirstPointAttrOfSplit =
975     getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
976   AttributePoint2DPtr aSecondPointAttrOfSplit =
977     getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
978
979   /// split feature
980   theSplitFeature =
981     createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
982   bool aSplitReversed = std::dynamic_pointer_cast<SketchPlugin_Arc>(theSplitFeature)->isReversed();
983   theCreatedFeatures.insert(theSplitFeature);
984
985   /// base feature is a left part of the circle
986   theBaseFeatureModified = createArcFeature(aBaseFeature,
987     aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
988   std::dynamic_pointer_cast<SketchPlugin_Arc>(
989     theBaseFeatureModified)->setReversed(!aSplitReversed);
990   theBaseFeatureModified->execute();
991
992   theModifiedAttributes.insert(
993     std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
994                   theBaseFeatureModified->attribute(SketchPlugin_Arc::CENTER_ID())));
995
996   theCreatedFeatures.insert(theBaseFeatureModified);
997
998   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
999                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
1000   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1001                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
1002
1003   // additional constraints between split and base features
1004   aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1005                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1006                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()));
1007   theCreatedFeatures.insert(aConstraintFeature);
1008   aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1009                      theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID()),
1010                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
1011   theCreatedFeatures.insert(aConstraintFeature);
1012
1013 #ifdef CREATE_CONSTRAINTS
1014   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
1015                                                        getFeatureResult(theSplitFeature),
1016                                                        getFeatureResult(theBaseFeatureModified));
1017   theCreatedFeatures.insert(aConstraintFeature);
1018 #endif
1019 }
1020
1021 void SketchPlugin_ConstraintSplit::arrangePointsOnLine(
1022     const AttributePoint2DPtr& theStartPointAttr,
1023     const AttributePoint2DPtr& theEndPointAttr,
1024     AttributePoint2DPtr& theFirstPointAttr,
1025     AttributePoint2DPtr& theLastPointAttr) const
1026 {
1027   // if first point is closer to last point, swap first and last values
1028   if (theStartPointAttr->pnt()->distance(theFirstPointAttr->pnt()) >
1029       theStartPointAttr->pnt()->distance(theLastPointAttr->pnt())) {
1030     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1031     theFirstPointAttr = theLastPointAttr;
1032     theLastPointAttr = aTmpPoint;
1033   }
1034 }
1035
1036 void SketchPlugin_ConstraintSplit::arrangePointsOnArc(
1037     const FeaturePtr& theArc,
1038     const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
1039     const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
1040     std::shared_ptr<GeomDataAPI_Point2D>& theFirstPointAttr,
1041     std::shared_ptr<GeomDataAPI_Point2D>& theSecondPointAttr) const
1042 {
1043   static const double anAngleTol = 1.e-12;
1044
1045   std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1046       theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1047   bool isReversed = theArc->boolean(SketchPlugin_Arc::INVERSED_ID())->value();
1048
1049   // collect directions to each point
1050   std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1051       new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1052   std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1053       new GeomAPI_Dir2d(theFirstPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1054   std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1055       new GeomAPI_Dir2d(theSecondPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1056
1057   // sort points by their angular values
1058   double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1059   double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1060   double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1061   if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1062     aFirstPtAngle += aPeriod;
1063   if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1064     aSecondPtAngle += aPeriod;
1065
1066   if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1067     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1068     theFirstPointAttr = theSecondPointAttr;
1069     theSecondPointAttr = aTmpPoint;
1070   }
1071 }
1072
1073 void SketchPlugin_ConstraintSplit::fillAttribute(const AttributePtr& theModifiedAttribute,
1074                                                  const AttributePtr& theSourceAttribute)
1075 {
1076   std::string anAttributeType = theModifiedAttribute->attributeType();
1077   if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1078     AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1079                                               theModifiedAttribute);
1080     AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1081                                               theSourceAttribute);
1082
1083     if (aModifiedAttribute.get() && aSourceAttribute.get())
1084       aModifiedAttribute->setValue(aSourceAttribute->pnt());
1085   }
1086   else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1087     AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1088                                               theModifiedAttribute);
1089     AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1090                                               theSourceAttribute);
1091
1092     if (aModifiedAttribute.get() && aSourceAttribute.get())
1093       aModifiedAttribute->setValue(aSourceAttribute->value());
1094   }
1095 }
1096
1097 FeaturePtr SketchPlugin_ConstraintSplit::createLineFeature(const FeaturePtr& theBaseFeature,
1098                                                            const AttributePtr& theFirstPointAttr,
1099                                                            const AttributePtr& theSecondPointAttr)
1100 {
1101   FeaturePtr aFeature;
1102   SketchPlugin_Sketch* aSketch = sketch();
1103   if (!aSketch || !theBaseFeature.get())
1104     return aFeature;
1105
1106   aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1107
1108   fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPointAttr);
1109   fillAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPointAttr);
1110
1111   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1112                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1113
1114   aFeature->execute(); // to obtain result
1115
1116   return aFeature;
1117 }
1118
1119 FeaturePtr SketchPlugin_ConstraintSplit::createArcFeature(const FeaturePtr& theBaseFeature,
1120                                                           const AttributePtr& theFirstPointAttr,
1121                                                           const AttributePtr& theSecondPointAttr)
1122 {
1123   FeaturePtr aFeature;
1124   SketchPlugin_Sketch* aSketch = sketch();
1125   if (!aSketch || !theBaseFeature.get())
1126     return aFeature;
1127
1128   std::string aCenterAttributeId;
1129   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1130     aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1131   else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1132     aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1133
1134   if (aCenterAttributeId.empty())
1135     return aFeature;
1136
1137   aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1138   // update fillet arc: make the arc correct for sure, so, it is not needed to process
1139   // the "attribute updated"
1140   // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1141   bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1142
1143   aFeature->string(SketchPlugin_Arc::ARC_TYPE())->setValue(
1144                 SketchPlugin_Arc::ARC_TYPE_CENTER_START_END());
1145
1146   fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1147                 theBaseFeature->attribute(aCenterAttributeId));
1148   fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPointAttr);
1149   fillAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPointAttr);
1150
1151   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1152                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1153
1154   /// fill referersed state of created arc as it is on the base arc
1155   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1156     bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::INVERSED_ID())->value();
1157     aFeature->boolean(SketchPlugin_Arc::INVERSED_ID())->setValue(aReversed);
1158   }
1159   aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1160   aFeature->execute(); // to obtain result
1161
1162   return aFeature;
1163 }
1164
1165 FeaturePtr SketchPlugin_ConstraintSplit::createConstraint(const std::string& theConstraintId,
1166                                                     const AttributePtr& theFirstAttribute,
1167                                                     const AttributePtr& theSecondAttribute)
1168 {
1169   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1170   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1171                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1172   aRefAttr->setAttr(theFirstAttribute);
1173
1174   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1175                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1176   aRefAttr->setAttr(theSecondAttribute);
1177
1178   return aConstraint;
1179 }
1180
1181 FeaturePtr SketchPlugin_ConstraintSplit::createConstraintForObjects(
1182                                                     const std::string& theConstraintId,
1183                                                     const ObjectPtr& theFirstObject,
1184                                                     const ObjectPtr& theSecondObject)
1185 {
1186   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1187   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1188                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1189   aRefAttr->setObject(theFirstObject);
1190
1191   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1192                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1193   aRefAttr->setObject(theSecondObject);
1194
1195   return aConstraint;
1196 }
1197
1198 void SketchPlugin_ConstraintSplit::updateFeaturesAfterSplit(
1199                                                    const std::set<FeaturePtr>& theFeaturesToUpdate)
1200 {
1201   std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
1202                                        aLast = theFeaturesToUpdate.end();
1203   for (; anIt != aLast; anIt++) {
1204     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1205     std::string aRefFeatureKind = aRefFeature->getKind();
1206     if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
1207       std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
1208                               std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
1209       if (aLenghtFeature.get()) {
1210         std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
1211             ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
1212         double aValue;
1213         if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
1214           aValueAttr->setValue(aValue);
1215       }
1216     }
1217   }
1218 }
1219
1220 std::shared_ptr<ModelAPI_Result> SketchPlugin_ConstraintSplit::getFeatureResult(
1221                                     const std::shared_ptr<ModelAPI_Feature>& theFeature)
1222 {
1223   std::shared_ptr<ModelAPI_Result> aResult;
1224
1225   std::string aFeatureKind = theFeature->getKind();
1226   if (aFeatureKind == SketchPlugin_Line::ID())
1227     aResult = theFeature->firstResult();
1228   else if (aFeatureKind == SketchPlugin_Arc::ID())
1229     aResult = theFeature->lastResult();
1230   else if (aFeatureKind == SketchPlugin_Circle::ID())
1231     aResult = theFeature->lastResult();
1232
1233   return aResult;
1234 }
1235
1236 std::set<std::shared_ptr<ModelAPI_Attribute> > SketchPlugin_ConstraintSplit::getEdgeAttributes(
1237                                            const std::shared_ptr<ModelAPI_Feature>& theFeature)
1238 {
1239   std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes;
1240
1241   std::string aFeatureKind = theFeature->getKind();
1242   if (aFeatureKind == SketchPlugin_Line::ID()) {
1243     anAttributes.insert(theFeature->attribute(SketchPlugin_Line::START_ID()));
1244     anAttributes.insert(theFeature->attribute(SketchPlugin_Line::END_ID()));
1245   }
1246   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
1247     anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::START_ID()));
1248     anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::END_ID()));
1249   }
1250   else if (aFeatureKind == SketchPlugin_Circle::ID()) {
1251   }
1252
1253   return anAttributes;
1254 }
1255
1256 #ifdef _DEBUG
1257 std::string SketchPlugin_ConstraintSplit::getFeatureInfo(
1258                                                const std::shared_ptr<ModelAPI_Feature>& theFeature,
1259                                                const bool isUseAttributesInfo)
1260 {
1261   std::string anInfo;
1262   if (!theFeature.get()) {
1263     return "none";
1264   }
1265
1266   if (theFeature->data()->isValid())
1267     anInfo.append(theFeature->data()->name().c_str());
1268
1269   if (isUseAttributesInfo) {
1270     std::string aPointsInfo = ModelGeomAlgo_Point2D::getPontAttributesInfo(theFeature,
1271                                                              getEdgeAttributes(theFeature));
1272     /// processing of feature with point 2d attributes, like line, arc, circle
1273     if (!aPointsInfo.empty()) {
1274       anInfo += ": ";
1275       anInfo += "\n";
1276       anInfo += aPointsInfo;
1277     }
1278     else { /// process constraint coincidence, find points in ref attr attributes
1279       std::list<AttributePtr> anAttrs = theFeature->data()->attributes(
1280                                                        ModelAPI_AttributeRefAttr::typeId());
1281       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
1282       std::string anAttributesInfo;
1283       for(; anIt != aLast; anIt++) {
1284         if (!anAttributesInfo.empty()) {
1285           anAttributesInfo.append(", ");
1286           anAttributesInfo += "\n";
1287         }
1288         AttributePtr anAttr = *anIt;
1289         std::string aValue = "not defined";
1290         std::string aType = anAttr->attributeType();
1291         if (aType == ModelAPI_AttributeRefAttr::typeId()) {
1292           std::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr =
1293                              std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
1294           if (aRefAttr.get()) {
1295             if (aRefAttr->isObject()) {
1296               FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1297               aValue = "<object:>" + getFeatureInfo(aFeature, false);
1298             }
1299             else {
1300               AttributePtr anAttribute = aRefAttr->attr();
1301               if (anAttribute.get()) {
1302                 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
1303                 aValue = "<attr:>" + ModelGeomAlgo_Point2D::getPointAttributeInfo(anAttribute) +
1304                          " [" + getFeatureInfo(aFeature, false) + "]";
1305               }
1306             }
1307           }
1308         }
1309         anAttributesInfo.append("    " + anAttr->id() + ": " + aValue);
1310       }
1311       if (!anAttributesInfo.empty())
1312         anInfo = anInfo + "\n" + anAttributesInfo;
1313     }
1314   }
1315   return anInfo;
1316 }
1317 #endif