Salome HOME
Implemented possibility to update fillet radius
[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
21 #include <SketchPlugin_Arc.h>
22 #include <SketchPlugin_Line.h>
23 #include <SketchPlugin_Sketch.h>
24 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_ConstraintTangent.h>
26 #include <SketchPlugin_ConstraintRadius.h>
27
28 #include <SketcherPrs_Factory.h>
29
30 #include <Config_PropManager.h>
31 #include <Events_Loop.h>
32
33 static const std::string PREVIOUS_VALUE("FilletPreviousRadius");
34
35 /// \brief Attract specified point on theNewArc to the attribute of theFeature
36 static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
37   FeaturePtr theFeature, const std::string& theFeatureAttribute);
38
39
40 SketchPlugin_ConstraintFillet::SketchPlugin_ConstraintFillet()
41 {
42 }
43
44 void SketchPlugin_ConstraintFillet::initAttributes()
45 {
46   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
47   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
48   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
49   data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::typeId());
50   data()->addAttribute(PREVIOUS_VALUE, ModelAPI_AttributeDouble::typeId());
51   // initialize attribute not applicable for user
52   data()->attribute(SketchPlugin_Constraint::ENTITY_C())->setInitialized();
53   data()->attribute(PREVIOUS_VALUE)->setInitialized();
54   std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(PREVIOUS_VALUE))->setValue(0.0);
55 }
56
57 void SketchPlugin_ConstraintFillet::execute()
58 {
59   std::shared_ptr<ModelAPI_Data> aData = data();
60   ResultConstructionPtr aRC;
61   // Check the base objects are initialized
62   double aFilletRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
63       aData->attribute(SketchPlugin_Constraint::VALUE()))->value();
64   AttributeRefAttrPtr aBaseA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
65       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
66   AttributeRefAttrPtr aBaseB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
67       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
68   if (!aBaseA->isInitialized() || !aBaseB->isInitialized() ||
69       !aBaseA->isObject() || !aBaseB->isObject())
70     return;
71   // Check the fillet shapes is not initialized yet
72   AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
73       aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
74   if (aRefListOfFillet->size() > 0) {
75     // update the Radius constraint
76     ObjectPtr aFilletArcObj = aRefListOfFillet->list().back();
77     aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFilletArcObj);
78     FeaturePtr aFilletArcFeature = aRC ? aRC->document()->feature(aRC) : 
79       std::dynamic_pointer_cast<ModelAPI_Feature>(aFilletArcObj);
80
81     int aNbSubs = sketch()->numberOfSubs();
82     FeaturePtr aSubFeature;
83     for (int aSub = 0; aSub < aNbSubs; aSub++) {
84       aSubFeature = sketch()->subFeature(aSub);
85       if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
86         continue;
87       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
88           aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
89       if (!aRefAttr || !aRefAttr->isObject())
90         continue;
91       aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aRefAttr->object());
92       FeaturePtr aFeature = aRC ? aRC->document()->feature(aRC) : 
93         std::dynamic_pointer_cast<ModelAPI_Feature>(aRefAttr->object());
94       if (aFeature == aFilletArcFeature) {
95         // Update radius constraint only if the value is changed in fillet's attribute
96         double aPrevRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
97             aData->attribute(PREVIOUS_VALUE))->value();
98         if (aFilletRadius != aPrevRadius) {
99           AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
100               aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
101           aRadius->setValue(aFilletRadius);
102           std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
103               aData->attribute(PREVIOUS_VALUE))->setValue(aFilletRadius);
104         }
105         break;
106       }
107     }
108     return;
109   }
110   // Obtain features for the base objects
111   FeaturePtr aFeatureA, aFeatureB;
112   aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseA->object());
113   if (aRC) aFeatureA = aRC->document()->feature(aRC);
114   aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseB->object());
115   if (aRC) aFeatureB = aRC->document()->feature(aRC);
116   if (!aFeatureA || !aFeatureB)
117     return;
118
119   // Create list of objects composing a fillet
120   // copy aFeatureA
121   FeaturePtr aNewFeatureA = sketch()->addFeature(aFeatureA->getKind());
122   aFeatureA->data()->copyTo(aNewFeatureA->data());
123   aNewFeatureA->execute();
124   aRefListOfFillet->append(aNewFeatureA);
125   // copy aFeatureB
126   FeaturePtr aNewFeatureB = sketch()->addFeature(aFeatureB->getKind());
127   aFeatureB->data()->copyTo(aNewFeatureB->data());
128   aNewFeatureB->execute();
129   aRefListOfFillet->append(aNewFeatureB);
130   // create filleting arc
131   FeaturePtr aNewArc = sketch()->addFeature(SketchPlugin_Arc::ID());
132   aRefListOfFillet->append(aNewArc);
133   aRefListOfFillet->setInitialized();
134
135   // Wait all constraints being created, then send update events
136   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
137   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
138   if (isUpdateFlushed)
139     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
140
141   // Calculate arc attributes
142   static const int aNbFeatures = 2;
143   FeaturePtr aFeature[aNbFeatures] = {aNewFeatureA, aNewFeatureB};
144   std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures]; // tangent directions of the features in coincident point
145   bool isStart[aNbFeatures]; // indicates which point the features share
146   std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2]; // first pair of points relate to first feature, second pair -  to second
147   std::string aFeatAttributes[aNbFeatures * 2]; // attributes of features
148   for (int i = 0; i < aNbFeatures; i++) {
149     std::string aStartAttr, aEndAttr;
150     if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
151       aStartAttr = SketchPlugin_Line::START_ID();
152       aEndAttr = SketchPlugin_Line::END_ID();
153     } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
154       aStartAttr = SketchPlugin_Arc::START_ID();
155       aEndAttr = SketchPlugin_Arc::END_ID();
156     } else { // wrong argument
157       aRefListOfFillet->remove(aNewFeatureA);
158       aRefListOfFillet->remove(aNewFeatureB);
159       aRefListOfFillet->remove(aNewArc);
160       return;
161     }
162     aFeatAttributes[2*i] = aStartAttr;
163     aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
164         aFeature[i]->attribute(aStartAttr))->pnt();
165     aFeatAttributes[2*i+1] = aEndAttr;
166     aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
167         aFeature[i]->attribute(aEndAttr))->pnt();
168   }
169   for (int i = 0; i < aNbFeatures; i++) {
170     int j = aNbFeatures;
171     for (; j < 2 * aNbFeatures; j++)
172       if (aStartEndPnt[i]->distance(aStartEndPnt[j]) < 1.e-10) {
173         isStart[0] = i==0;
174         isStart[1] = j==aNbFeatures;
175         break;
176       }
177     if (j < 2 * aNbFeatures)
178       break;
179   }
180   // tangent directions of the features
181   for (int i = 0; i < aNbFeatures; i++) {
182     std::shared_ptr<GeomAPI_XY> aDir;
183     if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
184       aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
185       if (!isStart[i])
186         aDir = aDir->multiplied(-1.0);
187     } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
188       std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint =
189           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190           aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
191       aDir = isStart[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
192       aDir = aDir->decreased(aCenterPoint->xy());
193
194       double x = aDir->x();
195       double y = aDir->y();
196       aDir->setX(-y);
197       aDir->setY(x);
198       if (!isStart[i])
199         aDir = aDir->multiplied(-1.0);
200     }
201     aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
202   }
203   // By default, the start point of fillet arc is connected to FeatureA,
204   // and the end point - to FeatureB. But when the angle between TangentDirA and
205   // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
206   double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A), where A and B - angles between corresponding tanget direction and the X axis
207   bool isReversed = cosBA > 0.0;
208
209   std::shared_ptr<GeomAPI_Pnt2d> aSharedPoint = aStartEndPnt[isStart[0] ? 0 : 1];
210   std::shared_ptr<GeomAPI_Dir2d> aBisect(new GeomAPI_Dir2d(
211       aTangentDir[0]->xy()->added(aTangentDir[1]->xy())));
212   std::shared_ptr<GeomAPI_XY> aStep = aBisect->xy()->multiplied(aFilletRadius);
213   std::shared_ptr<GeomAPI_XY> aCenter = aSharedPoint->xy()->added(aStep);
214   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
215       aNewArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(
216       aCenter->x(), aCenter->y());
217   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
218       aNewArc->attribute(SketchPlugin_Arc::START_ID()))->setValue(
219       aCenter->x() - aStep->y(), aCenter->y() + aStep->x());
220   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
221       aNewArc->attribute(SketchPlugin_Arc::END_ID()))->setValue(
222       aCenter->x() + aStep->y(), aCenter->y() - aStep->x());
223   aNewArc->execute();
224
225   // Create list of additional constraints:
226   // 1. Coincidence of boundary points of features and fillet arc
227   // 1.1. coincidence
228   FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
229   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
230       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
231   aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::START_ID()));
232   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
233       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
234   int aFeatInd = isReversed ? 1 : 0;
235   int anAttrInd = (isReversed ? 2 : 0) + (isStart[isReversed ? 1 : 0] ? 0 : 1);
236   aRefAttr->setAttr(aFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
237   recalculateAttributes(aNewArc, SketchPlugin_Arc::START_ID(), aFeature[aFeatInd], aFeatAttributes[anAttrInd]);
238   aConstraint->execute();
239   ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
240   // 1.2. coincidence
241   aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
242   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
243       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
244   aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::END_ID()));
245   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
246       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
247   aFeatInd = isReversed ? 0 : 1;
248   anAttrInd = (isReversed ? 0 : 2) + (isStart[isReversed ? 0 : 1] ? 0 : 1);
249   aRefAttr->setAttr(aFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
250   recalculateAttributes(aNewArc, SketchPlugin_Arc::END_ID(), aFeature[aFeatInd], aFeatAttributes[anAttrInd]);
251   aConstraint->execute();
252   ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
253   // recalculate center of fillet arc
254   std::shared_ptr<GeomAPI_Pnt2d> aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
255       aNewArc->attribute(SketchPlugin_Arc::START_ID()))->pnt();
256   std::shared_ptr<GeomAPI_Pnt2d> aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
257       aNewArc->attribute(SketchPlugin_Arc::END_ID()))->pnt();
258   aCenter = aStartPoint->xy()->added(aEndPoint->xy())->multiplied(0.5);
259   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
260       aNewArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(
261       aCenter->x(), aCenter->y());
262   // 2. Fillet arc radius
263   aConstraint = sketch()->addFeature(SketchPlugin_ConstraintRadius::ID());
264   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
265       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
266   aRefAttr->setObject(aNewArc->lastResult());
267   std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
268       aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aFilletRadius);
269   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
270       aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->setValue(
271       isStart[0] ? aStartEndPnt[0] : aStartEndPnt[1]);
272   aConstraint->execute();
273   ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
274   // 3. Tangency of fillet arc and features
275   for (int i = 0; i < aNbFeatures; i++) {
276     aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
277     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
278         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
279     aRefAttr->setObject(aNewArc->lastResult());
280     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
281         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
282     bool isArc = aFeature[i]->getKind() == SketchPlugin_Arc::ID();
283     aRefAttr->setObject(isArc ? aFeature[i]->lastResult() : aFeature[i]->firstResult());
284     aConstraint->execute();
285     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
286   }
287
288   // make base features auxiliary
289   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
290   aFeatureA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
291   aFeatureB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
292   ModelAPI_EventCreator::get()->sendUpdated(aFeatureA, aRedisplayEvent);
293   ModelAPI_EventCreator::get()->sendUpdated(aFeatureB, aRedisplayEvent);
294 ////  Events_Loop::loop()->flush(aRedisplayEvent);
295
296   // send events
297   if (isUpdateFlushed)
298     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
299   Events_Loop::loop()->flush(anUpdateEvent);
300 }
301
302 AISObjectPtr SketchPlugin_ConstraintFillet::getAISObject(AISObjectPtr thePrevious)
303 {
304   if (!sketch())
305     return thePrevious;
306
307   AISObjectPtr anAIS = thePrevious;
308   /// TODO: Equal constraint presentation should be put here
309   return anAIS;
310 }
311
312
313
314 // =========   Auxiliary functions   =================
315 void recalculateAttributes(FeaturePtr theNewArc,  const std::string& theNewArcAttribute,
316                             FeaturePtr theFeature, const std::string& theFeatureAttribute)
317 {
318   std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
319       theNewArc->attribute(theNewArcAttribute))->pnt();
320   if (theFeature->getKind() == SketchPlugin_Line::ID()) {
321     std::shared_ptr<GeomAPI_Pnt2d> aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
322         theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
323     std::shared_ptr<GeomAPI_Pnt2d> aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
324         theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
325     std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(
326         aEndPoint->xy()->decreased(aStartPoint->xy())));
327     std::shared_ptr<GeomAPI_XY> aVec = anArcPoint->xy()->decreased(aStartPoint->xy());
328     double aDot = aVec->dot(aLineDir->xy());
329     aVec = aStartPoint->xy()->added(aLineDir->xy()->multiplied(aDot));
330     anArcPoint->setX(aVec->x());
331     anArcPoint->setY(aVec->y());
332   } else if (theFeature->getKind() == SketchPlugin_Arc::ID()) {
333     std::shared_ptr<GeomAPI_Pnt2d> aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
334         theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
335     std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
336         theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
337     double aRadius = aStartPoint->distance(aCenterPoint);
338     std::shared_ptr<GeomAPI_Dir2d> aDir(new GeomAPI_Dir2d(
339         anArcPoint->xy()->decreased(aCenterPoint->xy())));
340     std::shared_ptr<GeomAPI_XY> aPoint = aCenterPoint->xy()->added(aDir->xy()->multiplied(aRadius));
341     anArcPoint->setX(aPoint->x());
342     anArcPoint->setY(aPoint->y());
343   } else
344     return;
345
346   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
347       theNewArc->attribute(theNewArcAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
348   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
349       theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
350 }