]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintFillet.cpp
Salome HOME
Merge branch 'Dev_1.5.0' into BR_REENTRANCE_OPERATION
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintFillet.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintFillet.cpp
4 // Created: 19 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintFillet.h"
8
9 #include <GeomAPI_Dir2d.h>
10 #include <GeomAPI_Pnt2d.h>
11 #include <GeomAPI_XY.h>
12 #include <GeomDataAPI_Point2D.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_AttributeRefAttr.h>
15 #include <ModelAPI_AttributeRefList.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Events.h>
18 #include <ModelAPI_ResultConstruction.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
21
22 #include <SketchPlugin_Arc.h>
23 #include <SketchPlugin_Line.h>
24 #include <SketchPlugin_Sketch.h>
25 #include <SketchPlugin_ConstraintCoincidence.h>
26 #include <SketchPlugin_ConstraintTangent.h>
27 #include <SketchPlugin_ConstraintRadius.h>
28 #include <SketchPlugin_Tools.h>
29
30 #include <SketcherPrs_Factory.h>
31 #include <SketcherPrs_Tools.h>
32
33 #include <Config_PropManager.h>
34 #include <Events_Loop.h>
35
36 #include <math.h>
37
38 static const std::string PREVIOUS_VALUE("FilletPreviousRadius");
39
40 /// \brief Attract specified point on theNewArc to the attribute of theFeature
41 static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
42   FeaturePtr theFeature, const std::string& theFeatureAttribute);
43
44 /// \brief Calculates center of fillet arc and coordinates of tangency points
45 static void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
46                                   double theRadius, bool theNotInversed[2],
47                                   std::shared_ptr<GeomAPI_XY>& theCenter,
48                                   std::shared_ptr<GeomAPI_XY>& theTangentA,
49                                   std::shared_ptr<GeomAPI_XY>& theTangentB);
50
51 SketchPlugin_ConstraintFillet::SketchPlugin_ConstraintFillet()
52 {
53 }
54
55 void SketchPlugin_ConstraintFillet::initAttributes()
56 {
57   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
58   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
59   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
60   data()->addAttribute(PREVIOUS_VALUE, ModelAPI_AttributeDouble::typeId());
61   // initialize attribute not applicable for user
62   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
63   data()->attribute(PREVIOUS_VALUE)->setInitialized();
64   std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(PREVIOUS_VALUE))->setValue(0.0);
65 }
66
67 void SketchPlugin_ConstraintFillet::execute()
68 {
69   // the viewer update should be blocked in order to avoid the temporaty fillet sub-features visualization
70   // before they are processed by the solver
71   //std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
72   //    new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
73   //Events_Loop::loop()->send(aMsg);
74
75   std::shared_ptr<ModelAPI_Data> aData = data();
76   ResultConstructionPtr aRC;
77   // Check the base objects are initialized
78   double aFilletRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
79       aData->attribute(SketchPlugin_Constraint::VALUE()))->value();
80   AttributeRefAttrPtr aBaseA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
81       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
82   if (!aBaseA->isInitialized() || aBaseA->isObject()) {
83     setError("Bad vertex selected");
84     return;
85   }
86
87   // Check the fillet shapes is not initialized yet
88   AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
89       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
90   bool needNewObjects = aRefListOfFillet->size() == 0;
91
92   // Obtain base features
93   AttributePtr anAttrBase = aBaseA->attr();
94   const std::set<AttributePtr>& aRefsList = anAttrBase->owner()->data()->refsToMe();
95   std::set<AttributePtr>::const_iterator aIt;
96   FeaturePtr aCoincident;
97   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
98     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
99     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
100     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
101       AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
102         aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
103       AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
104         aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
105       if(anAttrRefA.get() && !anAttrRefA->isObject()) {
106         AttributePtr anAttrA = anAttrRefA->attr();
107         if(anAttrBase == anAttrA) {
108           aCoincident = aConstrFeature;
109           break;
110         }
111       }
112       if(anAttrRefA.get() && !anAttrRefB->isObject()) {
113         AttributePtr anAttrB = anAttrRefB->attr();
114         if(anAttrBase == anAttrB) {
115           aCoincident = aConstrFeature;
116           break;
117         }
118       }
119     }
120   }
121
122   if(!aCoincident.get()) {
123     setError("No coincident edges at selected vertex");
124     return;
125   }
126
127   std::set<FeaturePtr> aCoinsideLines;
128   SketchPlugin_Tools::findCoincidences(aCoincident,
129                                        SketchPlugin_ConstraintCoincidence::ENTITY_A(),
130                                        aCoinsideLines);
131   SketchPlugin_Tools::findCoincidences(aCoincident,
132                                        SketchPlugin_ConstraintCoincidence::ENTITY_B(),
133                                        aCoinsideLines);
134   if(aCoinsideLines.size() != 2) {
135     setError("At selected vertex should be two coincident lines");
136     return;
137   }
138
139   std::set<FeaturePtr>::iterator aLinesIt = aCoinsideLines.begin();
140   FeaturePtr anOldFeatureA = *aLinesIt;
141   if(!anOldFeatureA) {
142     setError("One of the edges is empty");
143     return;
144   }
145   aLinesIt++;
146   FeaturePtr anOldFeatureB = *aLinesIt;
147   if(!anOldFeatureB) {
148     setError("One of the edges is empty");
149     return;
150   }
151
152   FeaturePtr aNewFeatureA, aNewFeatureB, aNewArc;
153   if (needNewObjects) {
154     // Create list of objects composing a fillet
155     // copy aFeatureA
156     aNewFeatureA = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(anOldFeatureB, sketch());
157     // copy aFeatureB
158     aNewFeatureB = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(anOldFeatureB, sketch());
159     // create filleting arc (it will be attached to the list later)
160     aNewArc = sketch()->addFeature(SketchPlugin_Arc::ID());
161   } else {
162     // Obtain features from the list
163     std::list<ObjectPtr> aNewFeatList = aRefListOfFillet->list();
164     std::list<ObjectPtr>::iterator aFeatIt = aNewFeatList.begin();
165     aNewFeatureA = ModelAPI_Feature::feature(*aFeatIt++);
166     aNewFeatureB = ModelAPI_Feature::feature(*aFeatIt++);
167     aNewArc = ModelAPI_Feature::feature(*aFeatIt);
168   }
169
170   // Calculate arc attributes
171   static const int aNbFeatures = 2;
172   FeaturePtr aFeature[aNbFeatures] = {anOldFeatureA, anOldFeatureB};
173   FeaturePtr aNewFeature[aNbFeatures] = {aNewFeatureA, aNewFeatureB};
174   std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures]; // tangent directions of the features in coincident point
175   bool isStart[aNbFeatures]; // indicates which point the features share
176   std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2]; // first pair of points relate to first feature, second pair -  to second
177   std::string aFeatAttributes[aNbFeatures * 2]; // attributes of features
178   for (int i = 0; i < aNbFeatures; i++) {
179     std::string aStartAttr, aEndAttr;
180     if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
181       aStartAttr = SketchPlugin_Line::START_ID();
182       aEndAttr = SketchPlugin_Line::END_ID();
183     } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
184       aStartAttr = SketchPlugin_Arc::START_ID();
185       aEndAttr = SketchPlugin_Arc::END_ID();
186     } else { // wrong argument
187       aRefListOfFillet->remove(aNewFeatureA);
188       aRefListOfFillet->remove(aNewFeatureB);
189       aRefListOfFillet->remove(aNewArc);
190       return;
191     }
192     aFeatAttributes[2*i] = aStartAttr;
193     aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
194         aFeature[i]->attribute(aStartAttr))->pnt();
195     aFeatAttributes[2*i+1] = aEndAttr;
196     aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
197         aFeature[i]->attribute(aEndAttr))->pnt();
198   }
199   for (int i = 0; i < aNbFeatures; i++) {
200     int j = aNbFeatures;
201     for (; j < 2 * aNbFeatures; j++)
202       if (aStartEndPnt[i]->distance(aStartEndPnt[j]) < 1.e-10) {
203         isStart[0] = i==0;
204         isStart[1] = j==aNbFeatures;
205         break;
206       }
207     if (j < 2 * aNbFeatures)
208       break;
209   }
210   // tangent directions of the features
211   for (int i = 0; i < aNbFeatures; i++) {
212     std::shared_ptr<GeomAPI_XY> aDir;
213     if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
214       aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
215       if (!isStart[i])
216         aDir = aDir->multiplied(-1.0);
217     } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
218       std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint =
219           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
220           aNewFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
221       aDir = isStart[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
222       aDir = aDir->decreased(aCenterPoint->xy());
223
224       double x = aDir->x();
225       double y = aDir->y();
226       aDir->setX(-y);
227       aDir->setY(x);
228       if (!isStart[i])
229         aDir = aDir->multiplied(-1.0);
230     }
231     aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
232   }
233
234   // Wait all constraints being created, then send update events
235   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
236   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
237   if (isUpdateFlushed)
238     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
239
240   // By default, the start point of fillet arc is connected to FeatureA,
241   // and the end point - to FeatureB. But when the angle between TangentDirA and
242   // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
243   double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A), where A and B - angles between corresponding tanget direction and the X axis
244   bool isReversed = cosBA > 0.0;
245
246   // Calculate fillet arc parameters
247   std::shared_ptr<GeomAPI_XY> aCenter, aTangentPntA, aTangentPntB;
248   calculateFilletCenter(anOldFeatureA, anOldFeatureB, aFilletRadius, isStart, aCenter, aTangentPntA, aTangentPntB);
249   // update features
250   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
251       aNewFeatureA->attribute(aFeatAttributes[isStart[0] ? 0 : 1]))->setValue(
252       aTangentPntA->x(), aTangentPntA->y());
253   aNewFeatureA->execute();
254   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
255       aNewFeatureB->attribute(aFeatAttributes[2 + (isStart[1] ? 0 : 1)]))->setValue(
256       aTangentPntB->x(), aTangentPntB->y());
257   aNewFeatureB->execute();
258   // update fillet arc
259   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
260       aNewArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(
261       aCenter->x(), aCenter->y());
262   if (isReversed) {
263     std::shared_ptr<GeomAPI_XY> aTmp = aTangentPntA;
264     aTangentPntA = aTangentPntB;
265     aTangentPntB = aTmp;
266   }
267   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
268       aNewArc->attribute(SketchPlugin_Arc::START_ID()))->setValue(aTangentPntA->x(), aTangentPntA->y());
269   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
270       aNewArc->attribute(SketchPlugin_Arc::END_ID()))->setValue(aTangentPntB->x(), aTangentPntB->y());
271   aNewArc->execute();
272
273   if (needNewObjects) {
274     // attach new arc to the list
275     aRefListOfFillet->append(aNewFeatureA->lastResult());
276     aRefListOfFillet->append(aNewFeatureB->lastResult());
277     aRefListOfFillet->append(aNewArc->lastResult());
278
279     myProducedFeatures.push_back(aNewFeatureA);
280     myProducedFeatures.push_back(aNewFeatureB);
281     myProducedFeatures.push_back(aNewArc);
282
283     // Create list of additional constraints:
284     // 1. Coincidence of boundary points of features (copied lines/arcs) and fillet arc
285     // 1.1. coincidence
286     FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
287     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
288         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
289     aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::START_ID()));
290     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
291         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
292     int aFeatInd = isReversed ? 1 : 0;
293     int anAttrInd = (isReversed ? 2 : 0) + (isStart[isReversed ? 1 : 0] ? 0 : 1);
294     aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
295     recalculateAttributes(aNewArc, SketchPlugin_Arc::START_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
296     aConstraint->execute();
297     myProducedFeatures.push_back(aConstraint);
298     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
299     // 1.2. coincidence
300     aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
301     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
302         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
303     aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::END_ID()));
304     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
305         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
306     aFeatInd = isReversed ? 0 : 1;
307     anAttrInd = (isReversed ? 0 : 2) + (isStart[isReversed ? 0 : 1] ? 0 : 1);
308     aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
309     recalculateAttributes(aNewArc, SketchPlugin_Arc::END_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
310     aConstraint->execute();
311     myProducedFeatures.push_back(aConstraint);
312     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
313     // 2. Fillet arc radius
314     //aConstraint = sketch()->addFeature(SketchPlugin_ConstraintRadius::ID());
315     //aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
316     //    aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
317     //aRefAttr->setObject(aNewArc->lastResult());
318     //std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
319     //    aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aFilletRadius);
320     //std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
321     //    aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->setValue(
322     //    isStart[0] ? aStartEndPnt[0] : aStartEndPnt[1]);
323     //aConstraint->execute();
324     //myProducedFeatures.push_back(aConstraint);
325     //ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
326     // 3. Tangency of fillet arc and features
327     for (int i = 0; i < aNbFeatures; i++) {
328       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
329       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
330           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
331       aRefAttr->setObject(aNewArc->lastResult());
332       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
333           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
334       bool isArc = aNewFeature[i]->getKind() == SketchPlugin_Arc::ID();
335       aRefAttr->setObject(isArc ? aNewFeature[i]->lastResult() : aNewFeature[i]->firstResult());
336       aConstraint->execute();
337       myProducedFeatures.push_back(aConstraint);
338       ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
339     }
340     // 4. Coincidence of free boundaries of base and copied features
341     for (int i = 0; i < aNbFeatures; i++) {
342       anAttrInd = 2*i + (isStart[i] ? 1 : 0);
343       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
344       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
345           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
346       aRefAttr->setAttr(aFeature[i]->attribute(aFeatAttributes[anAttrInd]));
347       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
348           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
349       aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
350       myProducedFeatures.push_back(aConstraint);
351     }
352     // 5. Tangent points should be placed on the base features
353     for (int i = 0; i < aNbFeatures; i++) {
354       anAttrInd = 2*i + (isStart[i] ? 0 : 1);
355       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
356       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
357           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
358       aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
359       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
360           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
361       aRefAttr->setObject(aFeature[i]->lastResult());
362       myProducedFeatures.push_back(aConstraint);
363     }
364     // make base features auxiliary
365     anOldFeatureA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
366     anOldFeatureB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
367     myBaseObjects.clear();
368     myBaseObjects.push_back(anOldFeatureA);
369     myBaseObjects.push_back(anOldFeatureB);
370     // exchange the naming IDs of newly created and old line that become auxiliary
371     sketch()->exchangeIDs(anOldFeatureA, aNewFeatureA);
372     sketch()->exchangeIDs(anOldFeatureB, aNewFeatureB);
373   } else {
374     // Update radius value
375     int aNbSubs = sketch()->numberOfSubs();
376     FeaturePtr aSubFeature;
377     for (int aSub = 0; aSub < aNbSubs; aSub++) {
378       aSubFeature = sketch()->subFeature(aSub);
379       if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
380         continue;
381       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
382           aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
383       if (!aRefAttr || !aRefAttr->isObject())
384         continue;
385       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
386       if (aFeature == aNewArc) {
387         AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
388           aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
389         aRadius->setValue(aFilletRadius);
390         break;
391       }
392     }
393   }
394
395   // send events to update the sub-features by the solver
396   if (isUpdateFlushed)
397     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
398
399   // the viewer update should be unblocked in order after the fillet features
400   // are processed by the solver
401   //aMsg = std::shared_ptr<Events_Message>(
402   //              new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
403   //Events_Loop::loop()->send(aMsg);
404 }
405
406 void SketchPlugin_ConstraintFillet::attributeChanged(const std::string& theID)
407 {
408   if (theID == SketchPlugin_Constraint::ENTITY_A()) {
409     // clear the list of fillet entities
410     AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
411         data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
412     aRefListOfFillet->clear();
413
414     // remove all produced objects and constraints
415     DocumentPtr aDoc = sketch()->document();
416     std::list<FeaturePtr>::iterator aCIt = myProducedFeatures.begin();
417     for (; aCIt != myProducedFeatures.end(); ++aCIt)
418       aDoc->removeFeature(*aCIt);
419     myProducedFeatures.clear();
420
421     // clear auxiliary flag on initial objects
422     for (aCIt = myBaseObjects.begin(); aCIt != myBaseObjects.end(); ++aCIt)
423       (*aCIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
424     myBaseObjects.clear();
425   }
426 }
427
428 AISObjectPtr SketchPlugin_ConstraintFillet::getAISObject(AISObjectPtr thePrevious)
429 {
430   if (!sketch())
431     return thePrevious;
432
433   AISObjectPtr anAIS = thePrevious;
434   /// TODO: Equal constraint presentation should be put here
435   return anAIS;
436 }
437
438 bool SketchPlugin_ConstraintFillet::isMacro() const
439 {
440   return true;
441 }
442
443
444 // =========   Auxiliary functions   =================
445 void recalculateAttributes(FeaturePtr theNewArc,  const std::string& theNewArcAttribute,
446                            FeaturePtr theFeature, const std::string& theFeatureAttribute)
447 {
448   std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
449       theNewArc->attribute(theNewArcAttribute))->pnt();
450   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
451       theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
452 }
453
454 /// \brief Find intersections of lines shifted along normal direction
455 void possibleFilletCenterLineLine(
456     std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
457     std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
458     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
459 {
460   std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
461   std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
462   std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
463   double aDet = theDirA->cross(theDirB);
464   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
465     aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
466     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
467       aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
468       double aVX = aDirAT->xy()->dot(aPntA);
469       double aVY = aDirBT->xy()->dot(aPntB);
470       std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
471           (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
472           (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
473       theCenters.push_back(aPoint);
474     }
475   }
476 }
477
478 /// \brief Find intersections of line shifted along normal direction in both sides
479 ///        and a circle with extended radius
480 void possibleFilletCenterLineArc(
481     std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
482     std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
483     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
484 {
485   std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
486   std::shared_ptr<GeomAPI_XY> aPnt;
487   double aDirNorm2 = theDirLine->dot(theDirLine);
488   double aRad = 0.0;
489   double aDirX = theDirLine->x();
490   double aDirX2 = theDirLine->x() * theDirLine->x();
491   double aDirY2 = theDirLine->y() * theDirLine->y();
492   double aDirXY = theDirLine->x() * theDirLine->y();
493   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
494     aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
495     double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
496     double aCoeff2 = aCoeff * aCoeff;
497     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
498       aRad = theRadiusArc + aStepB * theRadius;
499       double aD = aRad * aRad * aDirNorm2 - aCoeff2;
500       if (aD < 0.0)
501         continue;
502       double aDs = sqrt(aD);
503       double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
504       double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
505       double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
506           aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
507       double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
508           aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
509
510       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
511       theCenters.push_back(aPoint1);
512       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
513       theCenters.push_back(aPoint2);
514     }
515   }
516 }
517
518 /// \brief Find intersections of two circles with extended radii
519 void possibleFilletCenterArcArc(
520     std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
521     std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
522     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
523 {
524   std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
525   double aCenterDist2 = aCenterDir->dot(aCenterDir);
526   double aCenterDist = sqrt(aCenterDist2);
527
528   double aRadA, aRadB;
529   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
530     aRadA = theRadiusA + aStepA * theRadius;
531     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
532       aRadB = theRadiusB + aStepB * theRadius;
533       if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
534         continue; // there is no intersections
535
536       double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
537       double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
538
539       double x1 = theCenterA->x() + (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
540       double y1 = theCenterA->y() + (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
541
542       double x2 = theCenterA->x() + (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
543       double y2 = theCenterA->y() + (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
544
545       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
546       theCenters.push_back(aPoint1);
547       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
548       theCenters.push_back(aPoint2);
549     }
550   }
551 }
552
553 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
554                            double theRadius, bool theNotInversed[2],
555                            std::shared_ptr<GeomAPI_XY>& theCenter,
556                            std::shared_ptr<GeomAPI_XY>& theTangentA,
557                            std::shared_ptr<GeomAPI_XY>& theTangentB)
558 {
559   static const int aNbFeatures = 2;
560   FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
561   std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
562   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
563
564   for (int i = 0; i < aNbFeatures; i++) {
565     if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
566       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
567           aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
568       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
569           aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
570     } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
571       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
572           aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
573       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
574           aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
575       aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
576           aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
577     } else
578       return;
579     aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
580         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
581         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
582     aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
583         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
584         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
585   }
586
587   if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
588       theFeatureB->getKind() == SketchPlugin_Line::ID()) {
589     std::shared_ptr<GeomAPI_Dir2d> aDir[2];
590     std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
591     for (int i = 0; i < aNbFeatures; i++) {
592       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
593       aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
594     }
595
596     // get and filter possible centers
597     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
598     possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1], theRadius, aSuspectCenters);
599     double aDot = 0.0;
600     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
601     for (; anIt != aSuspectCenters.end(); anIt++) {
602       aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
603       theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
604       if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
605         continue; // incorrect position
606       aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
607       theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
608       if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
609         continue; // incorrect position
610       // the center is found, stop searching
611       theCenter = *anIt;
612       return;
613     }
614   } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
615       theFeatureB->getKind() == SketchPlugin_Line::ID()) || 
616       (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
617       theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
618     int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
619     double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
620     std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
621         new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
622     std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
623         new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
624
625     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
626         new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
627     std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
628         new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
629     double anArcAngle = aEndArcDir->angle(aStartArcDir);
630
631     // get and filter possible centers
632     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
633     possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd], anArcRadius, theRadius, aSuspectCenters);
634     double aDot = 0.0;
635     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
636     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
637     for (; anIt != aSuspectCenters.end(); anIt++) {
638       aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
639       aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
640       if (aLineTgPoint->decreased(aStart[aLineInd])->dot(aDirLine->xy()) < 0.0)
641         continue; // incorrect position
642       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
643           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
644       double aCurAngle = aCurDir->angle(aStartArcDir);
645       if (anArcAngle < 0.0) aCurAngle *= -1.0;
646       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
647         continue; // incorrect position
648       // the center is found, stop searching
649       theCenter = *anIt;
650       anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
651       if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
652         theTangentA = aLineTgPoint;
653         theTangentB = anArcTgPoint;
654       } else {
655         theTangentA = anArcTgPoint;
656         theTangentB = aLineTgPoint;
657       }
658       return;
659     }
660   } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
661       theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
662     double anArcRadius[aNbFeatures];
663     double anArcAngle[aNbFeatures];
664     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
665     for (int i = 0; i < aNbFeatures; i++) {
666       anArcRadius[i] = aStart[i]->distance(aCenter[i]);
667       aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
668           new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
669       std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
670           new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
671       anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
672     }
673
674     // get and filter possible centers
675     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
676     possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1], anArcRadius[1], theRadius, aSuspectCenters);
677     double aDot = 0.0;
678     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
679     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
680     for (; anIt != aSuspectCenters.end(); anIt++) {
681       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
682           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
683       double aCurAngle = aCurDir->angle(aStartArcDir[0]);
684       if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
685       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
686         continue; // incorrect position
687       theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
688
689       aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
690       aCurAngle = aCurDir->angle(aStartArcDir[1]);
691       if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
692       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
693         continue; // incorrect position
694       theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
695
696       // the center is found, stop searching
697       theCenter = *anIt;
698       return;
699     }
700   }
701 }