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