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>
28 #include <SketchPlugin_Tools.h>
30 #include <SketcherPrs_Factory.h>
31 #include <SketcherPrs_Tools.h>
33 #include <Config_PropManager.h>
34 #include <Events_Loop.h>
36 #define _USE_MATH_DEFINES
39 static const std::string PREVIOUS_VALUE("FilletPreviousRadius");
41 /// \brief Attract specified point on theNewArc to the attribute of theFeature
42 static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
43 FeaturePtr theFeature, const std::string& theFeatureAttribute);
45 /// \brief Calculates center of fillet arc and coordinates of tangency points
46 static void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
47 double theRadius, bool theNotInversed[2],
48 std::shared_ptr<GeomAPI_XY>& theCenter,
49 std::shared_ptr<GeomAPI_XY>& theTangentA,
50 std::shared_ptr<GeomAPI_XY>& theTangentB);
52 SketchPlugin_ConstraintFillet::SketchPlugin_ConstraintFillet()
56 void SketchPlugin_ConstraintFillet::initAttributes()
58 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
59 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
60 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
61 // This attribute used to store base edges
62 data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::typeId());
63 data()->addAttribute(PREVIOUS_VALUE, ModelAPI_AttributeDouble::typeId());
64 // initialize attribute not applicable for user
65 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
66 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_C());
67 data()->attribute(PREVIOUS_VALUE)->setInitialized();
68 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(PREVIOUS_VALUE))->setValue(0.0);
71 void SketchPlugin_ConstraintFillet::execute()
73 static const double aTol = 1.e-7;
75 // the viewer update should be blocked in order to avoid the temporaty fillet sub-features visualization
76 // before they are processed by the solver
77 //std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
78 // new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
79 //Events_Loop::loop()->send(aMsg);
81 std::shared_ptr<ModelAPI_Data> aData = data();
82 ResultConstructionPtr aRC;
83 // Check the base objects are initialized
84 double aFilletRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
85 aData->attribute(SketchPlugin_Constraint::VALUE()))->value();
86 AttributeRefAttrPtr aBaseA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
87 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
88 if (!aBaseA->isInitialized() || aBaseA->isObject()) {
89 setError("Bad vertex selected");
93 // Obtain fillet point
94 std::shared_ptr<GeomDataAPI_Point2D> aBasePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseA->attr());
96 setError("Bad vertex selected");
99 std::shared_ptr<GeomAPI_Pnt2d> aFilletPoint = aBasePoint->pnt();
101 // Check the fillet shapes is not initialized yet
102 AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
103 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
104 bool needNewObjects = aRefListOfFillet->size() == 0;
106 AttributeRefListPtr aRefListOfBaseLines = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
107 aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
109 // Obtain base features
110 FeaturePtr anOldFeatureA, anOldFeatureB;
111 std::list<ObjectPtr> anOldFeatList = aRefListOfBaseLines->list();
112 std::list<ObjectPtr>::iterator aFeatIt = anOldFeatList.begin();
113 anOldFeatureA = ModelAPI_Feature::feature(*aFeatIt++);
114 anOldFeatureB = ModelAPI_Feature::feature(*aFeatIt);
116 if(!anOldFeatureA.get() || !anOldFeatureB.get()) {
117 setError("One of the edges is empty");
121 FeaturePtr aNewFeatureA, aNewFeatureB, aNewArc;
122 if (needNewObjects) {
123 // Create list of objects composing a fillet
125 aNewFeatureA = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(anOldFeatureA, sketch());
127 aNewFeatureB = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(anOldFeatureB, sketch());
128 // create filleting arc (it will be attached to the list later)
129 aNewArc = sketch()->addFeature(SketchPlugin_Arc::ID());
131 // Obtain features from the list
132 std::list<ObjectPtr> aNewFeatList = aRefListOfFillet->list();
133 std::list<ObjectPtr>::iterator aFeatIt = aNewFeatList.begin();
134 aNewFeatureA = ModelAPI_Feature::feature(*aFeatIt++);
135 aNewFeatureB = ModelAPI_Feature::feature(*aFeatIt++);
136 aNewArc = ModelAPI_Feature::feature(*aFeatIt);
139 // Calculate arc attributes
140 static const int aNbFeatures = 2;
141 FeaturePtr aFeature[aNbFeatures] = {anOldFeatureA, anOldFeatureB};
142 FeaturePtr aNewFeature[aNbFeatures] = {aNewFeatureA, aNewFeatureB};
143 std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures]; // tangent directions of the features in coincident point
144 bool isStart[aNbFeatures]; // indicates which point the features share
145 std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2]; // first pair of points relate to first feature, second pair - to second
146 std::string aFeatAttributes[aNbFeatures * 2]; // attributes of features
147 for (int i = 0; i < aNbFeatures; i++) {
148 std::string aStartAttr, aEndAttr;
149 if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
150 aStartAttr = SketchPlugin_Line::START_ID();
151 aEndAttr = SketchPlugin_Line::END_ID();
152 } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
153 aStartAttr = SketchPlugin_Arc::START_ID();
154 aEndAttr = SketchPlugin_Arc::END_ID();
155 } else { // wrong argument
156 aRefListOfFillet->remove(aNewFeatureA);
157 aRefListOfFillet->remove(aNewFeatureB);
158 aRefListOfFillet->remove(aNewArc);
159 aRefListOfBaseLines->clear();
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();
169 for (int aFeatInd = 0; aFeatInd < aNbFeatures; aFeatInd++) {
170 for (int j = 0; j < 2; j++) // loop on start-end of each feature
171 if (aStartEndPnt[aFeatInd * aNbFeatures + j]->distance(aFilletPoint) < 1.e-10) {
172 isStart[aFeatInd] = (j==0);
176 // tangent directions of the features
177 for (int i = 0; i < aNbFeatures; i++) {
178 std::shared_ptr<GeomAPI_XY> aDir;
179 if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
180 aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
182 aDir = aDir->multiplied(-1.0);
183 } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
184 std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint =
185 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
186 aNewFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
187 aDir = isStart[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
188 aDir = aDir->decreased(aCenterPoint->xy());
190 double x = aDir->x();
191 double y = aDir->y();
195 aDir = aDir->multiplied(-1.0);
197 aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
200 // Wait all constraints being created, then send update events
201 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
202 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
204 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
206 // By default, the start point of fillet arc is connected to FeatureA,
207 // and the end point - to FeatureB. But when the angle between TangentDirA and
208 // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
209 double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A), where A and B - angles between corresponding tanget direction and the X axis
210 bool isReversed = cosBA > 0.0;
212 // Calculate fillet arc parameters
213 std::shared_ptr<GeomAPI_XY> aCenter, aTangentPntA, aTangentPntB;
214 calculateFilletCenter(anOldFeatureA, anOldFeatureB, aFilletRadius, isStart, aCenter, aTangentPntA, aTangentPntB);
216 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
217 aNewFeatureA->attribute(aFeatAttributes[isStart[0] ? 0 : 1]))->setValue(
218 aTangentPntA->x(), aTangentPntA->y());
219 aNewFeatureA->execute();
220 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
221 aNewFeatureB->attribute(aFeatAttributes[2 + (isStart[1] ? 0 : 1)]))->setValue(
222 aTangentPntB->x(), aTangentPntB->y());
223 aNewFeatureB->execute();
224 // update fillet arc: make the arc correct for sure, so, it is not needed to process the "attribute updated"
225 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
226 aNewArc->data()->blockSendAttributeUpdated(true);
227 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
228 aNewArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(
229 aCenter->x(), aCenter->y());
231 std::shared_ptr<GeomAPI_XY> aTmp = aTangentPntA;
232 aTangentPntA = aTangentPntB;
235 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
236 aNewArc->attribute(SketchPlugin_Arc::START_ID()));
237 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
238 aNewArc->attribute(SketchPlugin_Arc::END_ID()));
239 if (aStartPoint->isInitialized() && aEndPoint->isInitialized() &&
240 (aStartPoint->pnt()->xy()->distance(aTangentPntA) > aTol ||
241 aEndPoint->pnt()->xy()->distance(aTangentPntB) > aTol))
242 std::dynamic_pointer_cast<SketchPlugin_Arc>(aNewArc)->setReversed(false);
243 aStartPoint->setValue(aTangentPntA->x(), aTangentPntA->y());
244 aEndPoint->setValue(aTangentPntB->x(), aTangentPntB->y());
245 aNewArc->data()->blockSendAttributeUpdated(false);
248 if (needNewObjects) {
249 // attach new arc to the list
250 aRefListOfFillet->append(aNewFeatureA->lastResult());
251 aRefListOfFillet->append(aNewFeatureB->lastResult());
252 aRefListOfFillet->append(aNewArc->lastResult());
254 myProducedFeatures.push_back(aNewFeatureA);
255 myProducedFeatures.push_back(aNewFeatureB);
256 myProducedFeatures.push_back(aNewArc);
258 // Create list of additional constraints:
259 // 1. Coincidence of boundary points of features (copied lines/arcs) and fillet arc
261 FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
262 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
263 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
264 aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::START_ID()));
265 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
266 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
267 int aFeatInd = isReversed ? 1 : 0;
268 int anAttrInd = (isReversed ? 2 : 0) + (isStart[isReversed ? 1 : 0] ? 0 : 1);
269 aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
270 recalculateAttributes(aNewArc, SketchPlugin_Arc::START_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
271 aConstraint->execute();
272 myProducedFeatures.push_back(aConstraint);
273 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
275 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
276 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
277 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
278 aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::END_ID()));
279 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
280 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
281 aFeatInd = isReversed ? 0 : 1;
282 anAttrInd = (isReversed ? 0 : 2) + (isStart[isReversed ? 0 : 1] ? 0 : 1);
283 aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
284 recalculateAttributes(aNewArc, SketchPlugin_Arc::END_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
285 aConstraint->execute();
286 myProducedFeatures.push_back(aConstraint);
287 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
288 // 2. Fillet arc radius
289 //aConstraint = sketch()->addFeature(SketchPlugin_ConstraintRadius::ID());
290 //aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
291 // aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
292 //aRefAttr->setObject(aNewArc->lastResult());
293 //std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
294 // aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aFilletRadius);
295 //std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
296 // aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->setValue(
297 // isStart[0] ? aStartEndPnt[0] : aStartEndPnt[1]);
298 //aConstraint->execute();
299 //myProducedFeatures.push_back(aConstraint);
300 //ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
301 // 3. Tangency of fillet arc and features
302 for (int i = 0; i < aNbFeatures; i++) {
303 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
304 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
305 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
306 aRefAttr->setObject(aNewArc->lastResult());
307 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
308 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
309 bool isArc = aNewFeature[i]->getKind() == SketchPlugin_Arc::ID();
310 aRefAttr->setObject(isArc ? aNewFeature[i]->lastResult() : aNewFeature[i]->firstResult());
311 aConstraint->execute();
312 myProducedFeatures.push_back(aConstraint);
313 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
315 // 4. Coincidence of free boundaries of base and copied features
316 for (int i = 0; i < aNbFeatures; i++) {
317 anAttrInd = 2*i + (isStart[i] ? 1 : 0);
318 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
319 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
320 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
321 aRefAttr->setAttr(aFeature[i]->attribute(aFeatAttributes[anAttrInd]));
322 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
323 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
324 aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
325 myProducedFeatures.push_back(aConstraint);
327 // 5. Tangent points should be placed on the base features
328 for (int i = 0; i < aNbFeatures; i++) {
329 anAttrInd = 2*i + (isStart[i] ? 0 : 1);
330 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
331 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
332 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
333 aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
334 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
335 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
336 aRefAttr->setObject(aFeature[i]->lastResult());
337 myProducedFeatures.push_back(aConstraint);
339 // make base features auxiliary
340 anOldFeatureA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
341 anOldFeatureB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
342 myBaseObjects.clear();
343 myBaseObjects.push_back(anOldFeatureA);
344 myBaseObjects.push_back(anOldFeatureB);
345 // exchange the naming IDs of newly created and old line that become auxiliary
346 sketch()->exchangeIDs(anOldFeatureA, aNewFeatureA);
347 sketch()->exchangeIDs(anOldFeatureB, aNewFeatureB);
349 // Update radius value
350 int aNbSubs = sketch()->numberOfSubs();
351 FeaturePtr aSubFeature;
352 for (int aSub = 0; aSub < aNbSubs; aSub++) {
353 aSubFeature = sketch()->subFeature(aSub);
354 if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
356 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
357 aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
358 if (!aRefAttr || !aRefAttr->isObject())
360 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
361 if (aFeature == aNewArc) {
362 AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
363 aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
364 aRadius->setValue(aFilletRadius);
370 // send events to update the sub-features by the solver
372 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
374 // the viewer update should be unblocked in order after the fillet features
375 // are processed by the solver
376 //aMsg = std::shared_ptr<Events_Message>(
377 // new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
378 //Events_Loop::loop()->send(aMsg);
381 void SketchPlugin_ConstraintFillet::attributeChanged(const std::string& theID)
383 if (theID == SketchPlugin_Constraint::ENTITY_A()) {
384 // clear the list of fillet entities
385 AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
386 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
387 aRefListOfFillet->clear();
389 // clear the list of base features
390 AttributeRefListPtr aRefListOfBaseLines = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
391 data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
392 aRefListOfBaseLines->clear();
394 // remove all produced objects and constraints
395 DocumentPtr aDoc = sketch()->document();
396 std::list<FeaturePtr>::iterator aCIt = myProducedFeatures.begin();
397 for (; aCIt != myProducedFeatures.end(); ++aCIt)
398 aDoc->removeFeature(*aCIt);
399 myProducedFeatures.clear();
401 // clear auxiliary flag on initial objects
402 for (aCIt = myBaseObjects.begin(); aCIt != myBaseObjects.end(); ++aCIt)
403 (*aCIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
404 myBaseObjects.clear();
406 AttributeRefAttrPtr aBaseA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
407 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
408 if(!aBaseA->isInitialized() || aBaseA->isObject()) {
412 AttributePtr anAttrBase = aBaseA->attr();
413 const std::set<AttributePtr>& aRefsList = anAttrBase->owner()->data()->refsToMe();
414 std::set<AttributePtr>::const_iterator aIt;
415 FeaturePtr aCoincident;
416 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
417 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
418 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
419 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
420 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
421 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
422 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
423 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
424 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
425 AttributePtr anAttrA = anAttrRefA->attr();
426 if(anAttrBase == anAttrA) {
427 aCoincident = aConstrFeature;
431 if(anAttrRefA.get() && !anAttrRefB->isObject()) {
432 AttributePtr anAttrB = anAttrRefB->attr();
433 if(anAttrBase == anAttrB) {
434 aCoincident = aConstrFeature;
441 if(!aCoincident.get()) {
442 setError("No coincident edges at selected vertex");
446 std::set<FeaturePtr> aCoinsideLines;
447 SketchPlugin_Tools::findCoincidences(aCoincident,
448 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
450 SketchPlugin_Tools::findCoincidences(aCoincident,
451 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
454 // Remove auxilary lines
455 if(aCoinsideLines.size() > 2) {
456 std::set<FeaturePtr> aNewLines;
457 for(std::set<FeaturePtr>::iterator anIt = aCoinsideLines.begin(); anIt != aCoinsideLines.end(); ++anIt) {
458 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
459 aNewLines.insert(*anIt);
462 aCoinsideLines = aNewLines;
466 if(aCoinsideLines.size() != 2) {
467 setError("At selected vertex should be two coincident lines");
471 FeaturePtr anOldFeatureA, anOldFeatureB;
472 std::set<FeaturePtr>::iterator aLinesIt = aCoinsideLines.begin();
473 anOldFeatureA = *aLinesIt++;
474 anOldFeatureB = *aLinesIt;
475 aRefListOfBaseLines->append(anOldFeatureA);
476 aRefListOfBaseLines->append(anOldFeatureB);
479 // Set default value equal to 1/3 of the smallest line sharing the point.
480 static const int aNbFeatures = 2;
481 FeaturePtr aFeature[aNbFeatures] = {anOldFeatureA, anOldFeatureB};
484 double aLengths[aNbFeatures];
485 for (int i = 0; i < aNbFeatures; i++) {
486 std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature[i]->attribute(
487 aFeature[i]->getKind() == SketchPlugin_Line::ID() ? SketchPlugin_Line::START_ID() : SketchPlugin_Arc::START_ID()))->pnt();
488 std::shared_ptr<GeomAPI_Pnt2d> anEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature[i]->attribute(
489 aFeature[i]->getKind() == SketchPlugin_Line::ID() ? SketchPlugin_Line::END_ID() : SketchPlugin_Arc::END_ID()))->pnt();
490 if(aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
491 aLengths[i] = aStartPnt->distance(anEndPnt);
493 std::shared_ptr<GeomAPI_Pnt2d> anArcCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature[i]->attribute(
494 SketchPlugin_Arc::CENTER_ID()))->pnt();
495 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(aStartPnt->xy()->decreased(anArcCenter->xy())));
496 std::shared_ptr<GeomAPI_Dir2d> anEndDir(new GeomAPI_Dir2d(anEndPnt->xy()->decreased(anArcCenter->xy())));
497 double aRadius = aStartPnt->distance(anArcCenter);
498 double anAngle = aStartDir->angle(anEndDir);
499 aLengths[i] = aRadius * abs(anAngle);
502 aLength = aLengths[0];
503 for(int i = 1; i < aNbFeatures; i++) {
504 if(aLengths[i] < aLength) aLength = aLengths[i];
506 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aLength / 3.0);
510 AISObjectPtr SketchPlugin_ConstraintFillet::getAISObject(AISObjectPtr thePrevious)
515 AISObjectPtr anAIS = thePrevious;
516 /// TODO: Equal constraint presentation should be put here
520 bool SketchPlugin_ConstraintFillet::isMacro() const
526 // ========= Auxiliary functions =================
527 void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
528 FeaturePtr theFeature, const std::string& theFeatureAttribute)
530 std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
531 theNewArc->attribute(theNewArcAttribute))->pnt();
532 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
533 theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
536 /// \brief Find intersections of lines shifted along normal direction
537 void possibleFilletCenterLineLine(
538 std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
539 std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
540 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
542 std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
543 std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
544 std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
545 double aDet = theDirA->cross(theDirB);
546 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
547 aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
548 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
549 aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
550 double aVX = aDirAT->xy()->dot(aPntA);
551 double aVY = aDirBT->xy()->dot(aPntB);
552 std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
553 (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
554 (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
555 theCenters.push_back(aPoint);
560 /// \brief Find intersections of line shifted along normal direction in both sides
561 /// and a circle with extended radius
562 void possibleFilletCenterLineArc(
563 std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
564 std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
565 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
567 std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
568 std::shared_ptr<GeomAPI_XY> aPnt;
569 double aDirNorm2 = theDirLine->dot(theDirLine);
571 double aDirX = theDirLine->x();
572 double aDirX2 = theDirLine->x() * theDirLine->x();
573 double aDirY2 = theDirLine->y() * theDirLine->y();
574 double aDirXY = theDirLine->x() * theDirLine->y();
575 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
576 aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
577 double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
578 double aCoeff2 = aCoeff * aCoeff;
579 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
580 aRad = theRadiusArc + aStepB * theRadius;
581 double aD = aRad * aRad * aDirNorm2 - aCoeff2;
584 double aDs = sqrt(aD);
585 double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
586 double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
587 double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
588 aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
589 double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
590 aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
592 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
593 theCenters.push_back(aPoint1);
594 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
595 theCenters.push_back(aPoint2);
600 /// \brief Find intersections of two circles with extended radii
601 void possibleFilletCenterArcArc(
602 std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
603 std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
604 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
606 std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
607 double aCenterDist2 = aCenterDir->dot(aCenterDir);
608 double aCenterDist = sqrt(aCenterDist2);
611 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
612 aRadA = theRadiusA + aStepA * theRadius;
613 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
614 aRadB = theRadiusB + aStepB * theRadius;
615 if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
616 continue; // there is no intersections
618 double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
619 double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
621 double x1 = theCenterA->x() + (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
622 double y1 = theCenterA->y() + (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
624 double x2 = theCenterA->x() + (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
625 double y2 = theCenterA->y() + (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
627 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
628 theCenters.push_back(aPoint1);
629 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
630 theCenters.push_back(aPoint2);
635 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
636 double theRadius, bool theNotInversed[2],
637 std::shared_ptr<GeomAPI_XY>& theCenter,
638 std::shared_ptr<GeomAPI_XY>& theTangentA,
639 std::shared_ptr<GeomAPI_XY>& theTangentB)
641 static const int aNbFeatures = 2;
642 FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
643 std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
644 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
646 for (int i = 0; i < aNbFeatures; i++) {
647 if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
648 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
649 aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
650 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
651 aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
652 } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
653 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
654 aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
655 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
656 aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
657 aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
658 aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
661 aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
662 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
663 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
664 aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
665 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
666 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
669 if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
670 theFeatureB->getKind() == SketchPlugin_Line::ID()) {
671 std::shared_ptr<GeomAPI_Dir2d> aDir[2];
672 std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
673 for (int i = 0; i < aNbFeatures; i++) {
674 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
675 aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
678 // get and filter possible centers
679 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
680 possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1], theRadius, aSuspectCenters);
682 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
683 for (; anIt != aSuspectCenters.end(); anIt++) {
684 aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
685 theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
686 if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
687 continue; // incorrect position
688 aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
689 theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
690 if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
691 continue; // incorrect position
692 // the center is found, stop searching
696 } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
697 theFeatureB->getKind() == SketchPlugin_Line::ID()) ||
698 (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
699 theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
700 int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
701 double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
702 std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
703 new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
704 std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
705 new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
707 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
708 new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
709 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
710 new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
711 double anArcAngle = aEndArcDir->angle(aStartArcDir);
713 // get possible centers and filter them
714 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
715 possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd], anArcRadius, theRadius, aSuspectCenters);
717 // the line is forward into the arc
718 double innerArc = aCenter[1-aLineInd]->decreased(aStart[aLineInd])->dot(aDirLine->xy());
719 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
720 // The possible centers are ranged by their positions.
721 // If the point is not satisfy one of criteria, the weight is decreased with penalty.
723 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
724 for (; anIt != aSuspectCenters.end(); anIt++) {
726 aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
727 aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
728 // Check the point is placed on the correct arc (penalty if false)
729 if (aCenter[1-aLineInd]->distance(*anIt) * innerArc > anArcRadius * innerArc)
731 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
732 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
733 double aCurAngle = aCurDir->angle(aStartArcDir);
734 if (anArcAngle < 0.0) aCurAngle *= -1.0;
735 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
737 if (aWeight > aBestWeight)
738 aBestWeight = aWeight;
739 else if (aWeight < aBestWeight ||
740 aStart[aLineInd]->distance(*anIt) >
741 aStart[aLineInd]->distance(theCenter)) // <-- take closer point
743 // the center is found, stop searching
745 anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
746 if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
747 theTangentA = aLineTgPoint;
748 theTangentB = anArcTgPoint;
750 theTangentA = anArcTgPoint;
751 theTangentB = aLineTgPoint;
755 } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
756 theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
757 double anArcRadius[aNbFeatures];
758 double anArcAngle[aNbFeatures];
759 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
760 for (int i = 0; i < aNbFeatures; i++) {
761 anArcRadius[i] = aStart[i]->distance(aCenter[i]);
762 aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
763 new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
764 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
765 new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
766 anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
769 // get and filter possible centers
770 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
771 possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1], anArcRadius[1], theRadius, aSuspectCenters);
773 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
774 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
775 for (; anIt != aSuspectCenters.end(); anIt++) {
776 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
777 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
778 double aCurAngle = aCurDir->angle(aStartArcDir[0]);
779 if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
780 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
781 continue; // incorrect position
782 theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
784 aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
785 aCurAngle = aCurDir->angle(aStartArcDir[1]);
786 if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
787 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
788 continue; // incorrect position
789 theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
791 // the center is found, stop searching