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