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 // the viewer update should be blocked in order to avoid the temporaty fillet sub-features visualization
74 // before they are processed by the solver
75 //std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
76 // new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
77 //Events_Loop::loop()->send(aMsg);
79 std::shared_ptr<ModelAPI_Data> aData = data();
80 ResultConstructionPtr aRC;
81 // Check the base objects are initialized
82 double aFilletRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
83 aData->attribute(SketchPlugin_Constraint::VALUE()))->value();
84 AttributeRefAttrPtr aBaseA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
85 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
86 if (!aBaseA->isInitialized() || aBaseA->isObject()) {
87 setError("Bad vertex selected");
91 // Obtain fillet point
92 std::shared_ptr<GeomDataAPI_Point2D> aBasePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseA->attr());
94 setError("Bad vertex selected");
97 std::shared_ptr<GeomAPI_Pnt2d> aFilletPoint = aBasePoint->pnt();
99 // Check the fillet shapes is not initialized yet
100 AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
101 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
102 bool needNewObjects = aRefListOfFillet->size() == 0;
104 AttributeRefListPtr aRefListOfBaseLines = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
105 aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
107 // Obtain base features
108 FeaturePtr anOldFeatureA, anOldFeatureB;
109 std::list<ObjectPtr> anOldFeatList = aRefListOfBaseLines->list();
110 std::list<ObjectPtr>::iterator aFeatIt = anOldFeatList.begin();
111 anOldFeatureA = ModelAPI_Feature::feature(*aFeatIt++);
112 anOldFeatureB = ModelAPI_Feature::feature(*aFeatIt);
114 if(!anOldFeatureA.get() || !anOldFeatureB.get()) {
115 setError("One of the edges is empty");
119 FeaturePtr aNewFeatureA, aNewFeatureB, aNewArc;
120 if (needNewObjects) {
121 // Create list of objects composing a fillet
123 aNewFeatureA = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(anOldFeatureA, sketch());
125 aNewFeatureB = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(anOldFeatureB, sketch());
126 // create filleting arc (it will be attached to the list later)
127 aNewArc = sketch()->addFeature(SketchPlugin_Arc::ID());
129 // Obtain features from the list
130 std::list<ObjectPtr> aNewFeatList = aRefListOfFillet->list();
131 std::list<ObjectPtr>::iterator aFeatIt = aNewFeatList.begin();
132 aNewFeatureA = ModelAPI_Feature::feature(*aFeatIt++);
133 aNewFeatureB = ModelAPI_Feature::feature(*aFeatIt++);
134 aNewArc = ModelAPI_Feature::feature(*aFeatIt);
137 // Calculate arc attributes
138 static const int aNbFeatures = 2;
139 FeaturePtr aFeature[aNbFeatures] = {anOldFeatureA, anOldFeatureB};
140 FeaturePtr aNewFeature[aNbFeatures] = {aNewFeatureA, aNewFeatureB};
141 std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures]; // tangent directions of the features in coincident point
142 bool isStart[aNbFeatures]; // indicates which point the features share
143 std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2]; // first pair of points relate to first feature, second pair - to second
144 std::string aFeatAttributes[aNbFeatures * 2]; // attributes of features
145 for (int i = 0; i < aNbFeatures; i++) {
146 std::string aStartAttr, aEndAttr;
147 if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
148 aStartAttr = SketchPlugin_Line::START_ID();
149 aEndAttr = SketchPlugin_Line::END_ID();
150 } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
151 aStartAttr = SketchPlugin_Arc::START_ID();
152 aEndAttr = SketchPlugin_Arc::END_ID();
153 } else { // wrong argument
154 aRefListOfFillet->remove(aNewFeatureA);
155 aRefListOfFillet->remove(aNewFeatureB);
156 aRefListOfFillet->remove(aNewArc);
157 aRefListOfBaseLines->clear();
160 aFeatAttributes[2*i] = aStartAttr;
161 aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
162 aFeature[i]->attribute(aStartAttr))->pnt();
163 aFeatAttributes[2*i+1] = aEndAttr;
164 aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
165 aFeature[i]->attribute(aEndAttr))->pnt();
167 for (int aFeatInd = 0; aFeatInd < aNbFeatures; aFeatInd++) {
168 for (int j = 0; j < 2; j++) // loop on start-end of each feature
169 if (aStartEndPnt[aFeatInd * aNbFeatures + j]->distance(aFilletPoint) < 1.e-10) {
170 isStart[aFeatInd] = (j==0);
174 // tangent directions of the features
175 for (int i = 0; i < aNbFeatures; i++) {
176 std::shared_ptr<GeomAPI_XY> aDir;
177 if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
178 aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
180 aDir = aDir->multiplied(-1.0);
181 } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
182 std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint =
183 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
184 aNewFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
185 aDir = isStart[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
186 aDir = aDir->decreased(aCenterPoint->xy());
188 double x = aDir->x();
189 double y = aDir->y();
193 aDir = aDir->multiplied(-1.0);
195 aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
198 // Wait all constraints being created, then send update events
199 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
200 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
202 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
204 // By default, the start point of fillet arc is connected to FeatureA,
205 // and the end point - to FeatureB. But when the angle between TangentDirA and
206 // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
207 double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A), where A and B - angles between corresponding tanget direction and the X axis
208 bool isReversed = cosBA > 0.0;
210 // Calculate fillet arc parameters
211 std::shared_ptr<GeomAPI_XY> aCenter, aTangentPntA, aTangentPntB;
212 calculateFilletCenter(anOldFeatureA, anOldFeatureB, aFilletRadius, isStart, aCenter, aTangentPntA, aTangentPntB);
214 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
215 aNewFeatureA->attribute(aFeatAttributes[isStart[0] ? 0 : 1]))->setValue(
216 aTangentPntA->x(), aTangentPntA->y());
217 aNewFeatureA->execute();
218 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
219 aNewFeatureB->attribute(aFeatAttributes[2 + (isStart[1] ? 0 : 1)]))->setValue(
220 aTangentPntB->x(), aTangentPntB->y());
221 aNewFeatureB->execute();
222 // update fillet arc: make the arc correct for sure, so, it is not needed to process the "attribute updated"
223 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
224 aNewArc->data()->blockSendAttributeUpdated(true);
225 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
226 aNewArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(
227 aCenter->x(), aCenter->y());
229 std::shared_ptr<GeomAPI_XY> aTmp = aTangentPntA;
230 aTangentPntA = aTangentPntB;
233 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
234 aNewArc->attribute(SketchPlugin_Arc::START_ID()))->setValue(aTangentPntA->x(), aTangentPntA->y());
235 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
236 aNewArc->attribute(SketchPlugin_Arc::END_ID()))->setValue(aTangentPntB->x(), aTangentPntB->y());
237 aNewArc->data()->blockSendAttributeUpdated(false);
240 if (needNewObjects) {
241 // attach new arc to the list
242 aRefListOfFillet->append(aNewFeatureA->lastResult());
243 aRefListOfFillet->append(aNewFeatureB->lastResult());
244 aRefListOfFillet->append(aNewArc->lastResult());
246 myProducedFeatures.push_back(aNewFeatureA);
247 myProducedFeatures.push_back(aNewFeatureB);
248 myProducedFeatures.push_back(aNewArc);
250 // Create list of additional constraints:
251 // 1. Coincidence of boundary points of features (copied lines/arcs) and fillet arc
253 FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
254 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
255 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
256 aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::START_ID()));
257 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
258 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
259 int aFeatInd = isReversed ? 1 : 0;
260 int anAttrInd = (isReversed ? 2 : 0) + (isStart[isReversed ? 1 : 0] ? 0 : 1);
261 aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
262 recalculateAttributes(aNewArc, SketchPlugin_Arc::START_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
263 aConstraint->execute();
264 myProducedFeatures.push_back(aConstraint);
265 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
267 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
268 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
269 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
270 aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::END_ID()));
271 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
272 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
273 aFeatInd = isReversed ? 0 : 1;
274 anAttrInd = (isReversed ? 0 : 2) + (isStart[isReversed ? 0 : 1] ? 0 : 1);
275 aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
276 recalculateAttributes(aNewArc, SketchPlugin_Arc::END_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
277 aConstraint->execute();
278 myProducedFeatures.push_back(aConstraint);
279 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
280 // 2. Fillet arc radius
281 //aConstraint = sketch()->addFeature(SketchPlugin_ConstraintRadius::ID());
282 //aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
283 // aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
284 //aRefAttr->setObject(aNewArc->lastResult());
285 //std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
286 // aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aFilletRadius);
287 //std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
288 // aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->setValue(
289 // isStart[0] ? aStartEndPnt[0] : aStartEndPnt[1]);
290 //aConstraint->execute();
291 //myProducedFeatures.push_back(aConstraint);
292 //ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
293 // 3. Tangency of fillet arc and features
294 for (int i = 0; i < aNbFeatures; i++) {
295 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
296 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
297 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
298 aRefAttr->setObject(aNewArc->lastResult());
299 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
300 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
301 bool isArc = aNewFeature[i]->getKind() == SketchPlugin_Arc::ID();
302 aRefAttr->setObject(isArc ? aNewFeature[i]->lastResult() : aNewFeature[i]->firstResult());
303 aConstraint->execute();
304 myProducedFeatures.push_back(aConstraint);
305 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
307 // 4. Coincidence of free boundaries of base and copied features
308 for (int i = 0; i < aNbFeatures; i++) {
309 anAttrInd = 2*i + (isStart[i] ? 1 : 0);
310 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
311 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
312 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
313 aRefAttr->setAttr(aFeature[i]->attribute(aFeatAttributes[anAttrInd]));
314 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
315 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
316 aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
317 myProducedFeatures.push_back(aConstraint);
319 // 5. Tangent points should be placed on the base features
320 for (int i = 0; i < aNbFeatures; i++) {
321 anAttrInd = 2*i + (isStart[i] ? 0 : 1);
322 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
323 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
324 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
325 aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
326 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
327 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
328 aRefAttr->setObject(aFeature[i]->lastResult());
329 myProducedFeatures.push_back(aConstraint);
331 // make base features auxiliary
332 anOldFeatureA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
333 anOldFeatureB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
334 myBaseObjects.clear();
335 myBaseObjects.push_back(anOldFeatureA);
336 myBaseObjects.push_back(anOldFeatureB);
337 // exchange the naming IDs of newly created and old line that become auxiliary
338 sketch()->exchangeIDs(anOldFeatureA, aNewFeatureA);
339 sketch()->exchangeIDs(anOldFeatureB, aNewFeatureB);
341 // Update radius value
342 int aNbSubs = sketch()->numberOfSubs();
343 FeaturePtr aSubFeature;
344 for (int aSub = 0; aSub < aNbSubs; aSub++) {
345 aSubFeature = sketch()->subFeature(aSub);
346 if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
348 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
349 aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
350 if (!aRefAttr || !aRefAttr->isObject())
352 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
353 if (aFeature == aNewArc) {
354 AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
355 aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
356 aRadius->setValue(aFilletRadius);
362 // send events to update the sub-features by the solver
364 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
366 // the viewer update should be unblocked in order after the fillet features
367 // are processed by the solver
368 //aMsg = std::shared_ptr<Events_Message>(
369 // new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
370 //Events_Loop::loop()->send(aMsg);
373 void SketchPlugin_ConstraintFillet::attributeChanged(const std::string& theID)
375 if (theID == SketchPlugin_Constraint::ENTITY_A()) {
376 // clear the list of fillet entities
377 AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
378 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
379 aRefListOfFillet->clear();
381 // clear the list of base features
382 AttributeRefListPtr aRefListOfBaseLines = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
383 data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
384 aRefListOfBaseLines->clear();
386 // remove all produced objects and constraints
387 DocumentPtr aDoc = sketch()->document();
388 std::list<FeaturePtr>::iterator aCIt = myProducedFeatures.begin();
389 for (; aCIt != myProducedFeatures.end(); ++aCIt)
390 aDoc->removeFeature(*aCIt);
391 myProducedFeatures.clear();
393 // clear auxiliary flag on initial objects
394 for (aCIt = myBaseObjects.begin(); aCIt != myBaseObjects.end(); ++aCIt)
395 (*aCIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
396 myBaseObjects.clear();
398 AttributeRefAttrPtr aBaseA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
399 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
400 if(!aBaseA->isInitialized() || aBaseA->isObject()) {
404 AttributePtr anAttrBase = aBaseA->attr();
405 const std::set<AttributePtr>& aRefsList = anAttrBase->owner()->data()->refsToMe();
406 std::set<AttributePtr>::const_iterator aIt;
407 FeaturePtr aCoincident;
408 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
409 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
410 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
411 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
412 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
413 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
414 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
415 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
416 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
417 AttributePtr anAttrA = anAttrRefA->attr();
418 if(anAttrBase == anAttrA) {
419 aCoincident = aConstrFeature;
423 if(anAttrRefA.get() && !anAttrRefB->isObject()) {
424 AttributePtr anAttrB = anAttrRefB->attr();
425 if(anAttrBase == anAttrB) {
426 aCoincident = aConstrFeature;
433 if(!aCoincident.get()) {
434 setError("No coincident edges at selected vertex");
438 std::set<FeaturePtr> aCoinsideLines;
439 SketchPlugin_Tools::findCoincidences(aCoincident,
440 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
442 SketchPlugin_Tools::findCoincidences(aCoincident,
443 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
446 // Remove auxilary lines
447 if(aCoinsideLines.size() > 2) {
448 std::set<FeaturePtr> aNewLines;
449 for(std::set<FeaturePtr>::iterator anIt = aCoinsideLines.begin(); anIt != aCoinsideLines.end(); ++anIt) {
450 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
451 aNewLines.insert(*anIt);
454 aCoinsideLines = aNewLines;
458 if(aCoinsideLines.size() != 2) {
459 setError("At selected vertex should be two coincident lines");
463 FeaturePtr anOldFeatureA, anOldFeatureB;
464 std::set<FeaturePtr>::iterator aLinesIt = aCoinsideLines.begin();
465 anOldFeatureA = *aLinesIt++;
466 anOldFeatureB = *aLinesIt;
467 aRefListOfBaseLines->append(anOldFeatureA);
468 aRefListOfBaseLines->append(anOldFeatureB);
471 // Set default value equal to 1/3 of the smallest line sharing the point.
472 static const int aNbFeatures = 2;
473 FeaturePtr aFeature[aNbFeatures] = {anOldFeatureA, anOldFeatureB};
476 double aLengths[aNbFeatures];
477 for (int i = 0; i < aNbFeatures; i++) {
478 std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature[i]->attribute(
479 aFeature[i]->getKind() == SketchPlugin_Line::ID() ? SketchPlugin_Line::START_ID() : SketchPlugin_Arc::START_ID()))->pnt();
480 std::shared_ptr<GeomAPI_Pnt2d> anEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature[i]->attribute(
481 aFeature[i]->getKind() == SketchPlugin_Line::ID() ? SketchPlugin_Line::END_ID() : SketchPlugin_Arc::END_ID()))->pnt();
482 if(aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
483 aLengths[i] = aStartPnt->distance(anEndPnt);
485 std::shared_ptr<GeomAPI_Pnt2d> anArcCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature[i]->attribute(
486 SketchPlugin_Arc::CENTER_ID()))->pnt();
487 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(aStartPnt->xy()->decreased(anArcCenter->xy())));
488 std::shared_ptr<GeomAPI_Dir2d> anEndDir(new GeomAPI_Dir2d(anEndPnt->xy()->decreased(anArcCenter->xy())));
489 double aRadius = aStartPnt->distance(anArcCenter);
490 double anAngle = aStartDir->angle(anEndDir);
491 aLengths[i] = aRadius * abs(anAngle);
494 aLength = aLengths[0];
495 for(int i = 1; i < aNbFeatures; i++) {
496 if(aLengths[i] < aLength) aLength = aLengths[i];
498 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aLength / 3.0);
502 AISObjectPtr SketchPlugin_ConstraintFillet::getAISObject(AISObjectPtr thePrevious)
507 AISObjectPtr anAIS = thePrevious;
508 /// TODO: Equal constraint presentation should be put here
512 bool SketchPlugin_ConstraintFillet::isMacro() const
518 // ========= Auxiliary functions =================
519 void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
520 FeaturePtr theFeature, const std::string& theFeatureAttribute)
522 std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
523 theNewArc->attribute(theNewArcAttribute))->pnt();
524 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
525 theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
528 /// \brief Find intersections of lines shifted along normal direction
529 void possibleFilletCenterLineLine(
530 std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
531 std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
532 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
534 std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
535 std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
536 std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
537 double aDet = theDirA->cross(theDirB);
538 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
539 aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
540 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
541 aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
542 double aVX = aDirAT->xy()->dot(aPntA);
543 double aVY = aDirBT->xy()->dot(aPntB);
544 std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
545 (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
546 (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
547 theCenters.push_back(aPoint);
552 /// \brief Find intersections of line shifted along normal direction in both sides
553 /// and a circle with extended radius
554 void possibleFilletCenterLineArc(
555 std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
556 std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
557 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
559 std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
560 std::shared_ptr<GeomAPI_XY> aPnt;
561 double aDirNorm2 = theDirLine->dot(theDirLine);
563 double aDirX = theDirLine->x();
564 double aDirX2 = theDirLine->x() * theDirLine->x();
565 double aDirY2 = theDirLine->y() * theDirLine->y();
566 double aDirXY = theDirLine->x() * theDirLine->y();
567 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
568 aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
569 double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
570 double aCoeff2 = aCoeff * aCoeff;
571 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
572 aRad = theRadiusArc + aStepB * theRadius;
573 double aD = aRad * aRad * aDirNorm2 - aCoeff2;
576 double aDs = sqrt(aD);
577 double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
578 double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
579 double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
580 aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
581 double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
582 aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
584 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
585 theCenters.push_back(aPoint1);
586 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
587 theCenters.push_back(aPoint2);
592 /// \brief Find intersections of two circles with extended radii
593 void possibleFilletCenterArcArc(
594 std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
595 std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
596 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
598 std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
599 double aCenterDist2 = aCenterDir->dot(aCenterDir);
600 double aCenterDist = sqrt(aCenterDist2);
603 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
604 aRadA = theRadiusA + aStepA * theRadius;
605 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
606 aRadB = theRadiusB + aStepB * theRadius;
607 if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
608 continue; // there is no intersections
610 double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
611 double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
613 double x1 = theCenterA->x() + (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
614 double y1 = theCenterA->y() + (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
616 double x2 = theCenterA->x() + (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
617 double y2 = theCenterA->y() + (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
619 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
620 theCenters.push_back(aPoint1);
621 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
622 theCenters.push_back(aPoint2);
627 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
628 double theRadius, bool theNotInversed[2],
629 std::shared_ptr<GeomAPI_XY>& theCenter,
630 std::shared_ptr<GeomAPI_XY>& theTangentA,
631 std::shared_ptr<GeomAPI_XY>& theTangentB)
633 static const int aNbFeatures = 2;
634 FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
635 std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
636 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
638 for (int i = 0; i < aNbFeatures; i++) {
639 if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
640 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
641 aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
642 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
643 aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
644 } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
645 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
646 aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
647 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
648 aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
649 aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
650 aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
653 aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
654 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
655 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
656 aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
657 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
658 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
661 if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
662 theFeatureB->getKind() == SketchPlugin_Line::ID()) {
663 std::shared_ptr<GeomAPI_Dir2d> aDir[2];
664 std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
665 for (int i = 0; i < aNbFeatures; i++) {
666 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
667 aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
670 // get and filter possible centers
671 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
672 possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1], theRadius, aSuspectCenters);
674 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
675 for (; anIt != aSuspectCenters.end(); anIt++) {
676 aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
677 theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
678 if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
679 continue; // incorrect position
680 aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
681 theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
682 if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
683 continue; // incorrect position
684 // the center is found, stop searching
688 } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
689 theFeatureB->getKind() == SketchPlugin_Line::ID()) ||
690 (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
691 theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
692 int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
693 double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
694 std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
695 new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
696 std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
697 new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
699 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
700 new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
701 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
702 new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
703 double anArcAngle = aEndArcDir->angle(aStartArcDir);
705 // get possible centers and filter them
706 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
707 possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd], anArcRadius, theRadius, aSuspectCenters);
709 // the line is forward into the arc
710 double innerArc = aCenter[1-aLineInd]->decreased(aStart[aLineInd])->dot(aDirLine->xy());
711 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
712 // The possible centers are ranged by their positions.
713 // If the point is not satisfy one of criteria, the weight is decreased with penalty.
715 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
716 for (; anIt != aSuspectCenters.end(); anIt++) {
718 aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
719 aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
720 // Check the point is placed on the correct arc (penalty if false)
721 if (aCenter[1-aLineInd]->distance(*anIt) * innerArc > anArcRadius * innerArc)
723 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
724 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
725 double aCurAngle = aCurDir->angle(aStartArcDir);
726 if (anArcAngle < 0.0) aCurAngle *= -1.0;
727 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
729 if (aWeight > aBestWeight)
730 aBestWeight = aWeight;
731 else if (aWeight < aBestWeight ||
732 aStart[aLineInd]->distance(*anIt) >
733 aStart[aLineInd]->distance(theCenter)) // <-- take closer point
735 // the center is found, stop searching
737 anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
738 if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
739 theTangentA = aLineTgPoint;
740 theTangentB = anArcTgPoint;
742 theTangentA = anArcTgPoint;
743 theTangentB = aLineTgPoint;
747 } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
748 theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
749 double anArcRadius[aNbFeatures];
750 double anArcAngle[aNbFeatures];
751 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
752 for (int i = 0; i < aNbFeatures; i++) {
753 anArcRadius[i] = aStart[i]->distance(aCenter[i]);
754 aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
755 new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
756 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
757 new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
758 anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
761 // get and filter possible centers
762 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
763 possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1], anArcRadius[1], theRadius, aSuspectCenters);
765 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
766 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
767 for (; anIt != aSuspectCenters.end(); anIt++) {
768 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
769 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
770 double aCurAngle = aCurDir->angle(aStartArcDir[0]);
771 if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
772 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
773 continue; // incorrect position
774 theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
776 aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
777 aCurAngle = aCurDir->angle(aStartArcDir[1]);
778 if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
779 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
780 continue; // incorrect position
781 theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
783 // the center is found, stop searching