1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_ConstraintFillet.cpp
4 // Created: 19 Mar 2015
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_ConstraintFillet.h"
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>
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>
29 #include <SketcherPrs_Factory.h>
31 #include <Config_PropManager.h>
32 #include <Events_Loop.h>
34 static const std::string PREVIOUS_VALUE("FilletPreviousRadius");
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);
41 SketchPlugin_ConstraintFillet::SketchPlugin_ConstraintFillet()
45 void SketchPlugin_ConstraintFillet::initAttributes()
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);
58 void SketchPlugin_ConstraintFillet::execute()
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);
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())
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);
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())
94 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
95 aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
96 if (!aRefAttr || !aRefAttr->isObject())
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);
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)
126 // Create list of objects composing a fillet
128 FeaturePtr aNewFeatureA = sketch()->addFeature(aFeatureA->getKind());
129 aFeatureA->data()->copyTo(aNewFeatureA->data());
130 aNewFeatureA->execute();
131 aRefListOfFillet->append(aNewFeatureA->firstResult());
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());
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);
144 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
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);
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();
174 for (int i = 0; i < aNbFeatures; i++) {
176 for (; j < 2 * aNbFeatures; j++)
177 if (aStartEndPnt[i]->distance(aStartEndPnt[j]) < 1.e-10) {
179 isStart[1] = j==aNbFeatures;
182 if (j < 2 * aNbFeatures)
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());
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());
199 double x = aDir->x();
200 double y = aDir->y();
204 aDir = aDir->multiplied(-1.0);
206 aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
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;
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());
229 // attach new arc to the list
230 aRefListOfFillet->append(aNewArc->lastResult());
232 // Create list of additional constraints:
233 // 1. Coincidence of boundary points of features and fillet arc
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);
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);
285 // make base features auxiliary
286 aFeatureA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
287 aFeatureB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
289 // send events to update the sub-features by the solver
291 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
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);
300 AISObjectPtr SketchPlugin_ConstraintFillet::getAISObject(AISObjectPtr thePrevious)
305 AISObjectPtr anAIS = thePrevious;
306 /// TODO: Equal constraint presentation should be put here
312 // ========= Auxiliary functions =================
313 void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
314 FeaturePtr theFeature, const std::string& theFeatureAttribute)
316 std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
317 theNewArc->attribute(theNewArcAttribute))->pnt();
318 if (theFeature->getKind() == SketchPlugin_Line::ID()) {
319 std::shared_ptr<GeomAPI_Pnt2d> aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
320 theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
321 std::shared_ptr<GeomAPI_Pnt2d> aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
322 theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
323 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(
324 aEndPoint->xy()->decreased(aStartPoint->xy())));
325 std::shared_ptr<GeomAPI_XY> aVec = anArcPoint->xy()->decreased(aStartPoint->xy());
326 double aDot = aVec->dot(aLineDir->xy());
327 aVec = aStartPoint->xy()->added(aLineDir->xy()->multiplied(aDot));
328 anArcPoint->setX(aVec->x());
329 anArcPoint->setY(aVec->y());
330 } else if (theFeature->getKind() == SketchPlugin_Arc::ID()) {
331 std::shared_ptr<GeomAPI_Pnt2d> aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
332 theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
333 std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
334 theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
335 double aRadius = aStartPoint->distance(aCenterPoint);
336 std::shared_ptr<GeomAPI_Dir2d> aDir(new GeomAPI_Dir2d(
337 anArcPoint->xy()->decreased(aCenterPoint->xy())));
338 std::shared_ptr<GeomAPI_XY> aPoint = aCenterPoint->xy()->added(aDir->xy()->multiplied(aRadius));
339 anArcPoint->setX(aPoint->x());
340 anArcPoint->setY(aPoint->y());
344 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
345 theNewArc->attribute(theNewArcAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
346 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
347 theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());