Salome HOME
Fix errors and adjust unit tests
[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: 17 Jul 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_Data.h>
31 #include <ModelAPI_Events.h>
32 //#include <ModelAPI_Session.h>
33 //#include <ModelAPI_Validator.h>
34 //
35 #include <SketchPlugin_Line.h>
36 #include <SketchPlugin_Arc.h>
37 #include <SketchPlugin_Circle.h>
38 //#include <SketchPlugin_Point.h>
39 //#include <SketchPlugin_Sketch.h>
40 //#include <SketchPlugin_Tools.h>
41 //
42
43 #include <ModelGeomAlgo_Point2D.h>
44 #include <Events_Loop.h>
45 //
46 //#include <math.h>
47 //
48 //const double tolerance = 1.e-7;
49 //const double paramTolerance = 1.e-4;
50
51 ///// \brief Attract specified point on theNewArc to the attribute of theFeature
52 //static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
53 //  FeaturePtr theFeature, const std::string& theFeatureAttribute);
54 //
55 ///// \brief Calculates center of fillet arc and coordinates of tangency points
56 //static void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
57 //                                  double theRadius, bool theNotInversed[2],
58 //                                  std::shared_ptr<GeomAPI_XY>& theCenter,
59 //                                  std::shared_ptr<GeomAPI_XY>& theTangentA,
60 //                                  std::shared_ptr<GeomAPI_XY>& theTangentB);
61 //
62 ///// Get point on 1/3 length of edge from fillet point
63 //static void getPointOnEdge(const FeaturePtr theFeature,
64 //                           const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
65 //                           std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
66 //
67 ///// Get distance from point to feature
68 //static double getProjectionDistance(const FeaturePtr theFeature,
69 //                             const std::shared_ptr<GeomAPI_Pnt2d> thePoint);
70 //
71 ///// Get coincide edges for fillet
72 //static std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence);
73
74 SketchPlugin_ConstraintSplit::SketchPlugin_ConstraintSplit()
75 //: myListOfPointsChangedInCode(false),
76 //  myRadiusChangedByUser(false),
77 //  myRadiusChangedInCode(false),
78 //  myRadiusInitialized(false)
79 {
80 }
81
82 void SketchPlugin_ConstraintSplit::initAttributes()
83 {
84   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeReference::typeId());
85   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
86   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
87
88   //data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
89   //data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttrList::typeId());
90 }
91
92 void SketchPlugin_ConstraintSplit::execute()
93 {
94   std::shared_ptr<ModelAPI_Data> aData = data();
95
96   // Check the base objects are initialized.
97   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
98                                             aData->attribute(SketchPlugin_Constraint::VALUE()));
99   if(!aBaseObjectAttr->isInitialized()) {
100     setError("Error: Base object is not initialized.");
101     return;
102   }
103   AttributePoint2DPtr aFirstPointAttr = getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
104   AttributePoint2DPtr aSecondPointAttr = getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
105   if (!aFirstPointAttr.get() || !aFirstPointAttr->isInitialized() ||
106       !aSecondPointAttr.get() || !aSecondPointAttr->isInitialized()) {
107     setError("Error: Sub-shape is not initialized.");
108     return;
109   }
110
111   // Wait all constraints being created, then send update events
112   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
113   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
114   if (isUpdateFlushed)
115     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
116
117
118   // Find feature constraints
119   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
120   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
121
122   std::set<FeaturePtr> aFeaturesToDelete;
123   std::map<FeaturePtr, IdToPointPair> aTangentFeatures;
124   std::map<FeaturePtr, IdToPointPair> aCoincidenceToFeature;
125   std::map<FeaturePtr, IdToPointPair> aCoincidenceToPoint;
126   getConstraints(aFeaturesToDelete, aTangentFeatures, aCoincidenceToFeature, aCoincidenceToPoint);
127
128
129   std::string aFeatureKind = aBaseFeature->getKind();
130   FeaturePtr aSplitFeature, anAfterFeature;
131   std::set<AttributePoint2DPtr> aFurtherCoincidences;
132   /*if (aFeatureKind == SketchPlugin_Line::ID())
133     splitLine(aSplitFeature, anOtherFeatures);
134   else*/ if (aFeatureKind == SketchPlugin_Arc::ID())
135     splitArc(aSplitFeature, aBaseFeature, anAfterFeature, aFurtherCoincidences);
136   /*if (aFeatureKind == SketchPlugin_Circle::ID())
137     splitCircle(aSplitFeature, anOtherFeatures);
138   FeaturePtr aSplitFeature;
139   std::set<FeaturePtr> anOtherFeatures;*/
140
141   //setConstraints(aSplitFeature, aBaseFeature, anAfterFeature, aFeaturesToDelete, aTangentFeatures,
142   //               aCoincidenceToFeature, aCoincidenceToPoint);
143   if (true) {
144   std::set<ResultPtr> aFeatureResults;
145   aFeatureResults.insert(getFeatureResult(aBaseFeature));
146   if (anAfterFeature.get() && anAfterFeature != aBaseFeature)
147     aFeatureResults.insert(getFeatureResult(anAfterFeature));
148
149   // coincidence to feature
150   std::map<FeaturePtr, IdToPointPair>::const_iterator aCIt = aCoincidenceToFeature.begin(),
151                                                             aCLast = aCoincidenceToFeature.end();
152   for (; aCIt != aCLast; aCIt++) {
153     FeaturePtr aCoincFeature = aCIt->first;
154     std::string anAttributeId = aCIt->second.first;
155     AttributePoint2DPtr aCoincPoint = aCIt->second.second;
156     std::set<AttributePoint2DPtr>::const_iterator aFCIt = aFurtherCoincidences.begin(),
157                                                   aFCLast = aFurtherCoincidences.end();
158     std::shared_ptr<GeomAPI_Pnt2d> aCoincPnt = aCoincPoint->pnt();
159     AttributePoint2DPtr aFeaturePointAttribute;
160     for (; aFCIt != aFCLast && !aFeaturePointAttribute.get(); aFCIt++) {
161       AttributePoint2DPtr aFCAttribute = *aFCIt;
162       if (aCoincPnt->isEqual(aFCAttribute->pnt()))
163         aFeaturePointAttribute = aFCAttribute;
164     }
165     if (aFeaturePointAttribute.get()) {
166       aCoincFeature->refattr(anAttributeId)->setAttr(aFeaturePointAttribute);
167     }
168     else {
169       /// find feature by shape intersected the point
170       ResultPtr aResultForCoincidence = *(aFeatureResults.begin());
171
172       if (aFeatureResults.size() > 1) { // try to find point on additional feature
173         ResultPtr anAddtionalResult = *(aFeatureResults.begin()++);
174         GeomShapePtr aShape = anAddtionalResult->shape();
175
176         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCoincPoint->pnt();
177         std::shared_ptr<GeomAPI_Pnt> aPoint = sketch()->to3D(aPnt2d->x(), aPnt2d->y());
178
179         std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
180         if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPoint, aProjectedPoint))
181           aResultForCoincidence = anAddtionalResult;
182       }
183       aCoincFeature->refattr(anAttributeId)->setObject(aResultForCoincidence);
184     }
185   }
186   // coincidence to points
187   // TODO
188   // tangency
189   // TODO
190   }
191   // delete constraints
192   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
193
194   // Send events to update the sub-features by the solver.
195   if(isUpdateFlushed) {
196     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
197   }
198 }
199
200 bool SketchPlugin_ConstraintSplit::isMacro() const
201 {
202   return true;
203 }
204
205 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_ConstraintSplit::getPointOfRefAttr(
206                                                                   const AttributePtr& theAttribute)
207 {
208   AttributePoint2DPtr aPointAttribute;
209
210   if (theAttribute->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
211     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
212     if (aRefAttr.get() && aRefAttr->isInitialized()) {
213       AttributePtr anAttribute = aRefAttr->attr();
214       if (anAttribute.get() && anAttribute->attributeType() == GeomDataAPI_Point2D::typeId())
215         aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
216     }
217   }
218   return aPointAttribute;
219 }
220
221 void SketchPlugin_ConstraintSplit::getFeaturePoints(AttributePoint2DPtr& theStartPointAttr,
222                                                     AttributePoint2DPtr& theEndPointAttr)
223 {
224   AttributePoint2DPtr aPointAttribute;
225
226   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
227                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
228   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
229
230   std::string aFeatureKind = aBaseFeature->getKind();
231   std::string aStartAttributeName, anEndAttributeName;
232   if (aFeatureKind == SketchPlugin_Line::ID()) {
233     aStartAttributeName = SketchPlugin_Line::START_ID();
234     anEndAttributeName = SketchPlugin_Line::END_ID();
235   }
236   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
237     aStartAttributeName = SketchPlugin_Arc::START_ID();
238     anEndAttributeName = SketchPlugin_Arc::END_ID();
239   }
240   if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
241     theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
242                                                          aBaseFeature->attribute(aStartAttributeName));
243     theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
244                                                          aBaseFeature->attribute(anEndAttributeName));
245   }
246 }
247
248 void SketchPlugin_ConstraintSplit::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
249                                       std::map<FeaturePtr, IdToPointPair>& theTangentFeatures,
250                                       std::map<FeaturePtr, IdToPointPair>& theCoincidenceToFeature,
251                                       std::map<FeaturePtr, IdToPointPair>& theCoincidenceToPoint)
252 {
253   std::shared_ptr<ModelAPI_Data> aData = data();
254
255   // Check the base objects are initialized.
256   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
257                                             aData->attribute(SketchPlugin_Constraint::VALUE()));
258   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
259   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
260
261   const std::set<AttributePtr>& aRefsList = aBaseFeatureResult->data()->refsToMe();
262   std::set<AttributePtr>::const_iterator aIt;
263   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
264     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
265     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
266     std::string aRefFeatureKind = aRefFeature->getKind();
267     if (aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() ||
268         aRefFeatureKind == SketchPlugin_MultiRotation::ID() ||
269         aRefFeatureKind == SketchPlugin_MultiTranslation::ID())
270       theFeaturesToDelete.insert(aRefFeature);
271     else if (aRefFeatureKind == SketchPlugin_ConstraintTangent::ID()) {
272       if (aBaseFeature->getKind() == SketchPlugin_Circle::ID()) /// TEMPORARY limitaion
273         theFeaturesToDelete.insert(aRefFeature); /// until tangency between arc and line is implemented
274       else {
275         std::string anAttributeToBeModified;
276         AttributePoint2DPtr aTangentPoint;
277         ObjectPtr aResult1 = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_A())->object();
278         ObjectPtr aResult2 = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_B())->object();
279         if (aResult1.get() && aResult2.get()) {
280           FeaturePtr aCoincidenceFeature = SketchPlugin_ConstraintCoincidence::findCoincidenceFeature
281                                                                     (ModelAPI_Feature::feature(aResult1),
282                                                                      ModelAPI_Feature::feature(aResult2));
283           aTangentPoint = SketchPlugin_ConstraintCoincidence::getPoint(aCoincidenceFeature);
284         }
285         if (aTangentPoint.get()) {
286           FeaturePtr aFeature1 = ModelAPI_Feature::feature(aResult1);
287           std::string anAttributeToBeModified = aFeature1 == aBaseFeature
288                        ? SketchPlugin_Constraint::ENTITY_B() : SketchPlugin_Constraint::ENTITY_A();
289           theTangentFeatures[aRefFeature] = std::make_pair(anAttributeToBeModified, aTangentPoint);
290         }
291         else
292           theFeaturesToDelete.insert(aRefFeature); /// there is not coincident point between tangent constraint
293       }
294     }
295     else if (aRefFeatureKind == SketchPlugin_ConstraintCoincidence::ID()) {
296       std::string anAttributeToBeModified;
297       AttributePoint2DPtr aCoincidentPoint;
298       AttributeRefAttrPtr anAttrA = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_A());
299       AttributeRefAttrPtr anAttrB = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_B());
300       bool isToFeature = false;
301       if (anAttrA->isObject() || anAttrB->isObject()) { /// coincidence to base feature
302         FeaturePtr aFeature = anAttrA->isObject() ? ModelAPI_Feature::feature(anAttrA->object())
303                                                   : FeaturePtr();
304         isToFeature = aFeature.get() && aFeature == aBaseFeature;
305         anAttributeToBeModified = SketchPlugin_Constraint::ENTITY_B();
306         if (!isToFeature) {
307           aFeature = anAttrB->isObject() ? ModelAPI_Feature::feature(anAttrB->object())
308                                          : FeaturePtr();
309           isToFeature = aFeature.get() && aFeature == aBaseFeature;
310           anAttributeToBeModified = SketchPlugin_Constraint::ENTITY_A();
311         }
312         if (isToFeature)
313           aCoincidentPoint = SketchPlugin_ConstraintCoincidence::getPoint(aRefFeature);
314       }
315       if (!isToFeature) { /// coincidence to point on base feature
316         AttributePtr anAttribute;
317         if (!anAttrA->isObject()) {
318           AttributePtr aCurAttribute = anAttrA->attr();
319           if (aCurAttribute.get()) {
320             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
321             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
322               anAttribute = anAttrA->attr();
323               anAttributeToBeModified = SketchPlugin_Constraint::ENTITY_A();
324             }
325           }
326         }
327         if (!anAttribute.get() && !anAttrB->isObject()) {
328           AttributePtr aCurAttribute = anAttrB->attr();
329           if (aCurAttribute.get()) {
330             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
331             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
332               anAttribute = anAttrB->attr();
333               anAttributeToBeModified = SketchPlugin_Constraint::ENTITY_B();
334             }
335           }
336         }
337         if (anAttribute.get())
338           aCoincidentPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
339       }
340       if (aCoincidentPoint.get()) {
341         if (isToFeature)
342           theCoincidenceToFeature[aRefFeature] = std::make_pair(anAttributeToBeModified,
343                                                                 aCoincidentPoint);
344         else
345           theCoincidenceToPoint[aRefFeature] = std::make_pair(anAttributeToBeModified,
346                                                               aCoincidentPoint);
347       }
348       else
349         theFeaturesToDelete.insert(aRefFeature); /// this case should not happen
350     }
351   }
352 }
353
354 /*void SketchPlugin_ConstraintSplit::setConstraints(const FeaturePtr& theSplitFeature,
355                                                   const FeaturePtr& theBaseFeature,
356                                                   const FeaturePtr& theAfterFeature,
357     const std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete,
358     const std::map<std::shared_ptr<ModelAPI_Feature>, std::shared_ptr<GeomDataAPI_Point2D> >& theTangentFeatures,
359     const std::map<std::shared_ptr<ModelAPI_Feature>, std::shared_ptr<GeomDataAPI_Point2D> >& theCoincidenceToFeature,
360     const std::map<std::shared_ptr<ModelAPI_Feature>, std::shared_ptr<GeomDataAPI_Point2D> >& theCoincidenceToPoint)
361 {
362   // coincidence to feature
363
364
365   // coincidence to points
366
367   // tangency
368
369   // delete constraints
370
371 }*/
372
373 void SketchPlugin_ConstraintSplit::splitArc(FeaturePtr& theSplitFeature,
374                                             FeaturePtr& theBaseFeature,
375                                             FeaturePtr& theAfterFeature,
376                                             std::set<AttributePoint2DPtr>& thePoints)
377 {
378   SketchPlugin_Sketch* aSketch = sketch();
379   if (!aSketch)
380     return;
381
382   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
383                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
384   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
385   std::string aFeatureKind = aBaseFeature->getKind();
386   if (aFeatureKind != SketchPlugin_Arc::ID())
387     return;
388
389   AttributePoint2DPtr aFirstPointAttr = getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
390   AttributePoint2DPtr aSecondPointAttr = getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
391   AttributePoint2DPtr aStartPointAttr, anEndPointAttr;
392   getFeaturePoints(aStartPointAttr, anEndPointAttr);
393   if (!aStartPointAttr.get() && !anEndPointAttr.get()) {
394     setError("Error: Feature has no start and end points.");
395     return;
396   }
397
398   arrangePoints(aStartPointAttr, anEndPointAttr, aFirstPointAttr, aSecondPointAttr);
399
400   /// split feature
401   theSplitFeature = createArcFeature(aBaseFeature, aFirstPointAttr, aSecondPointAttr);
402
403   // before split feature
404   if (!aStartPointAttr->pnt()->isEqual(aFirstPointAttr->pnt())) {
405     theBaseFeature = aBaseFeature; ///< use base feature to store all constraints here
406     /// move end arc point to start of split
407   }
408
409   // after split feature
410   if (!aSecondPointAttr->pnt()->isEqual(anEndPointAttr->pnt())) {
411     FeaturePtr aFeature;
412     if (!theBaseFeature.get()) {
413       aFeature = aBaseFeature; ///< use base feature to store all constraints here
414       fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), aSecondPointAttr);
415       aFeature->execute(); // to update result
416     }
417     else
418       aFeature = createArcFeature(aBaseFeature, aSecondPointAttr, anEndPointAttr);
419
420     createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
421                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()),
422                      aFeature->attribute(SketchPlugin_Arc::START_ID()));
423     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
424                                 (theBaseFeature->attribute(SketchPlugin_Arc::START_ID())));
425     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
426                                 (theBaseFeature->attribute(SketchPlugin_Arc::END_ID())));
427
428     if (!theBaseFeature.get())
429       theBaseFeature = aFeature;
430     else
431       theAfterFeature = aFeature;
432   }
433   else
434     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
435                                   (theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
436
437   // base split, that is defined before split feature should be changed at end
438   // (after the after feature creation). Otherwise modified value will be used in after feature
439   // before split feature
440   if (!aStartPointAttr->pnt()->isEqual(aFirstPointAttr->pnt())) {
441     /// move end arc point to start of split
442     fillAttribute(theBaseFeature->attribute(SketchPlugin_Arc::END_ID()), aFirstPointAttr);
443     theBaseFeature->execute(); // to update result
444     createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
445                      theBaseFeature->attribute(SketchPlugin_Arc::END_ID()),
446                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
447     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
448                                         (theBaseFeature->attribute(SketchPlugin_Arc::START_ID())));
449     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
450                                         (theBaseFeature->attribute(SketchPlugin_Arc::END_ID())));
451   }
452   else
453     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
454                                        (theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
455
456   // additional constraints between split and base features
457   createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(), getFeatureResult(aBaseFeature),
458                                                        getFeatureResult(theSplitFeature));
459   createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(), getFeatureResult(theSplitFeature),
460                                                          getFeatureResult(aBaseFeature));
461   if (theAfterFeature.get()) {
462     createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(), getFeatureResult(aBaseFeature),
463                                                          getFeatureResult(theAfterFeature));
464     createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(), getFeatureResult(theSplitFeature),
465                                                          getFeatureResult(theAfterFeature));
466   }
467 }
468
469 void SketchPlugin_ConstraintSplit::arrangePoints(const AttributePoint2DPtr& theStartPointAttr,
470                                                  const AttributePoint2DPtr& theEndPointAttr,
471                                                  AttributePoint2DPtr& theFirstPointAttr,
472                                                  AttributePoint2DPtr& theLastPointAttr)
473 {
474   /// if first point is closer to last point, wrap first and last values
475   if (theStartPointAttr->pnt()->distance(theFirstPointAttr->pnt()) >
476       theStartPointAttr->pnt()->distance(theLastPointAttr->pnt())) {
477     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
478     theFirstPointAttr = theLastPointAttr;
479     theLastPointAttr = aTmpPoint;
480   }
481 }
482
483 void SketchPlugin_ConstraintSplit::fillAttribute(const AttributePtr& theModifiedAttribute,
484                                                  const AttributePtr& theSourceAttribute)
485 {
486   AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
487                                             theModifiedAttribute);
488   AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
489                                             theSourceAttribute);
490
491   if (aModifiedAttribute.get() && aSourceAttribute.get())
492     aModifiedAttribute->setValue(aSourceAttribute->pnt());
493 }
494
495 FeaturePtr SketchPlugin_ConstraintSplit::createArcFeature(const FeaturePtr& theBaseFeature,
496                                                           const AttributePtr& theFirstPointAttr,
497                                                           const AttributePtr& theSecondPointAttr)
498 {
499   FeaturePtr aFeature;
500   SketchPlugin_Sketch* aSketch = sketch();
501   if (!aSketch || !theBaseFeature.get())
502     return aFeature;
503
504   aFeature = aSketch->addFeature(theBaseFeature->getKind());
505   // update fillet arc: make the arc correct for sure, so, it is not needed to process the "attribute updated"
506   // by arc; moreover, it may cause cyclicity in hte mechanism of updater
507   aFeature->data()->blockSendAttributeUpdated(true);
508
509   aFeature->string(SketchPlugin_Arc::ARC_TYPE())->setValue(
510                 SketchPlugin_Arc::ARC_TYPE_CENTER_START_END());
511   fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
512                 theBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
513   fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPointAttr);
514   fillAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPointAttr);
515   aFeature->data()->blockSendAttributeUpdated(false);
516   aFeature->execute(); // to obtain result
517
518   return aFeature;
519 }
520
521 void SketchPlugin_ConstraintSplit::createConstraint(const std::string& theConstraintId,
522                                                     const AttributePtr& theFirstAttribute,
523                                                     const AttributePtr& theSecondAttribute)
524 {
525   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
526   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
527                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
528   aRefAttr->setAttr(theFirstAttribute);
529
530   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
531                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
532   aRefAttr->setAttr(theSecondAttribute);
533 }
534
535 void SketchPlugin_ConstraintSplit::createConstraintForObjects(const std::string& theConstraintId,
536                                                     const ObjectPtr& theFirstObject,
537                                                     const ObjectPtr& theSecondObject)
538 {
539   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
540   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
541                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
542   aRefAttr->setObject(theFirstObject);
543
544   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
545                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
546   aRefAttr->setObject(theSecondObject);
547 }
548
549 std::shared_ptr<ModelAPI_Result> SketchPlugin_ConstraintSplit::getFeatureResult(
550                                     const std::shared_ptr<ModelAPI_Feature>& theFeature)
551 {
552   std::shared_ptr<ModelAPI_Result> aResult;
553
554   std::string aFeatureKind = theFeature->getKind();
555   if (aFeatureKind == SketchPlugin_Line::ID())
556     aResult = theFeature->firstResult();
557   else if (aFeatureKind == SketchPlugin_Arc::ID())
558     aResult = theFeature->lastResult();
559   else if (aFeatureKind == SketchPlugin_Circle::ID())
560     aResult = theFeature->lastResult();
561
562   return aResult;
563 }
564