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 // Check the fillet shapes is not initialized yet
92 AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
93 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
94 bool needNewObjects = aRefListOfFillet->size() == 0;
96 AttributeRefListPtr aRefListOfBaseLines = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
97 aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
99 // Obtain base features
100 FeaturePtr anOldFeatureA, anOldFeatureB;
101 std::list<ObjectPtr> aNewFeatList = aRefListOfBaseLines->list();
102 std::list<ObjectPtr>::iterator aFeatIt = aNewFeatList.begin();
103 anOldFeatureA = ModelAPI_Feature::feature(*aFeatIt++);
104 anOldFeatureB = ModelAPI_Feature::feature(*aFeatIt);
107 if(!anOldFeatureA.get() || !anOldFeatureB.get()) {
108 setError("One of the edges is empty");
112 FeaturePtr aNewFeatureA, aNewFeatureB, aNewArc;
113 if (needNewObjects) {
114 // Create list of objects composing a fillet
116 aNewFeatureA = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(anOldFeatureA, sketch());
118 aNewFeatureB = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(anOldFeatureB, sketch());
119 // create filleting arc (it will be attached to the list later)
120 aNewArc = sketch()->addFeature(SketchPlugin_Arc::ID());
122 // Obtain features from the list
123 std::list<ObjectPtr> aNewFeatList = aRefListOfFillet->list();
124 std::list<ObjectPtr>::iterator aFeatIt = aNewFeatList.begin();
125 aNewFeatureA = ModelAPI_Feature::feature(*aFeatIt++);
126 aNewFeatureB = ModelAPI_Feature::feature(*aFeatIt++);
127 aNewArc = ModelAPI_Feature::feature(*aFeatIt);
130 // Calculate arc attributes
131 static const int aNbFeatures = 2;
132 FeaturePtr aFeature[aNbFeatures] = {anOldFeatureA, anOldFeatureB};
133 FeaturePtr aNewFeature[aNbFeatures] = {aNewFeatureA, aNewFeatureB};
134 std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures]; // tangent directions of the features in coincident point
135 bool isStart[aNbFeatures]; // indicates which point the features share
136 std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2]; // first pair of points relate to first feature, second pair - to second
137 std::string aFeatAttributes[aNbFeatures * 2]; // attributes of features
138 for (int i = 0; i < aNbFeatures; i++) {
139 std::string aStartAttr, aEndAttr;
140 if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
141 aStartAttr = SketchPlugin_Line::START_ID();
142 aEndAttr = SketchPlugin_Line::END_ID();
143 } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
144 aStartAttr = SketchPlugin_Arc::START_ID();
145 aEndAttr = SketchPlugin_Arc::END_ID();
146 } else { // wrong argument
147 aRefListOfFillet->remove(aNewFeatureA);
148 aRefListOfFillet->remove(aNewFeatureB);
149 aRefListOfFillet->remove(aNewArc);
150 aRefListOfBaseLines->clear();
153 aFeatAttributes[2*i] = aStartAttr;
154 aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
155 aFeature[i]->attribute(aStartAttr))->pnt();
156 aFeatAttributes[2*i+1] = aEndAttr;
157 aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
158 aFeature[i]->attribute(aEndAttr))->pnt();
160 for (int i = 0; i < aNbFeatures; i++) {
162 for (; j < 2 * aNbFeatures; j++)
163 if (aStartEndPnt[i]->distance(aStartEndPnt[j]) < 1.e-10) {
165 isStart[1] = j==aNbFeatures;
168 if (j < 2 * aNbFeatures)
171 // tangent directions of the features
172 for (int i = 0; i < aNbFeatures; i++) {
173 std::shared_ptr<GeomAPI_XY> aDir;
174 if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
175 aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
177 aDir = aDir->multiplied(-1.0);
178 } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
179 std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint =
180 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
181 aNewFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
182 aDir = isStart[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
183 aDir = aDir->decreased(aCenterPoint->xy());
185 double x = aDir->x();
186 double y = aDir->y();
190 aDir = aDir->multiplied(-1.0);
192 aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
195 // Wait all constraints being created, then send update events
196 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
197 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
199 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
201 // By default, the start point of fillet arc is connected to FeatureA,
202 // and the end point - to FeatureB. But when the angle between TangentDirA and
203 // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
204 double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A), where A and B - angles between corresponding tanget direction and the X axis
205 bool isReversed = cosBA > 0.0;
207 // Calculate fillet arc parameters
208 std::shared_ptr<GeomAPI_XY> aCenter, aTangentPntA, aTangentPntB;
209 calculateFilletCenter(anOldFeatureA, anOldFeatureB, aFilletRadius, isStart, aCenter, aTangentPntA, aTangentPntB);
211 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
212 aNewFeatureA->attribute(aFeatAttributes[isStart[0] ? 0 : 1]))->setValue(
213 aTangentPntA->x(), aTangentPntA->y());
214 aNewFeatureA->execute();
215 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
216 aNewFeatureB->attribute(aFeatAttributes[2 + (isStart[1] ? 0 : 1)]))->setValue(
217 aTangentPntB->x(), aTangentPntB->y());
218 aNewFeatureB->execute();
220 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
221 aNewArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(
222 aCenter->x(), aCenter->y());
224 std::shared_ptr<GeomAPI_XY> aTmp = aTangentPntA;
225 aTangentPntA = aTangentPntB;
228 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
229 aNewArc->attribute(SketchPlugin_Arc::START_ID()))->setValue(aTangentPntA->x(), aTangentPntA->y());
230 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
231 aNewArc->attribute(SketchPlugin_Arc::END_ID()))->setValue(aTangentPntB->x(), aTangentPntB->y());
234 if (needNewObjects) {
235 // attach new arc to the list
236 aRefListOfFillet->append(aNewFeatureA->lastResult());
237 aRefListOfFillet->append(aNewFeatureB->lastResult());
238 aRefListOfFillet->append(aNewArc->lastResult());
240 // attach base lines to the list
241 aRefListOfBaseLines->append(anOldFeatureA);
242 aRefListOfBaseLines->append(anOldFeatureB);
244 myProducedFeatures.push_back(aNewFeatureA);
245 myProducedFeatures.push_back(aNewFeatureB);
246 myProducedFeatures.push_back(aNewArc);
248 // Create list of additional constraints:
249 // 1. Coincidence of boundary points of features (copied lines/arcs) and fillet arc
251 FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
252 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
253 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
254 aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::START_ID()));
255 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
256 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
257 int aFeatInd = isReversed ? 1 : 0;
258 int anAttrInd = (isReversed ? 2 : 0) + (isStart[isReversed ? 1 : 0] ? 0 : 1);
259 aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
260 recalculateAttributes(aNewArc, SketchPlugin_Arc::START_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
261 aConstraint->execute();
262 myProducedFeatures.push_back(aConstraint);
263 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
265 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
266 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
267 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
268 aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::END_ID()));
269 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
270 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
271 aFeatInd = isReversed ? 0 : 1;
272 anAttrInd = (isReversed ? 0 : 2) + (isStart[isReversed ? 0 : 1] ? 0 : 1);
273 aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
274 recalculateAttributes(aNewArc, SketchPlugin_Arc::END_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
275 aConstraint->execute();
276 myProducedFeatures.push_back(aConstraint);
277 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
278 // 2. Fillet arc radius
279 //aConstraint = sketch()->addFeature(SketchPlugin_ConstraintRadius::ID());
280 //aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
281 // aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
282 //aRefAttr->setObject(aNewArc->lastResult());
283 //std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
284 // aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aFilletRadius);
285 //std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
286 // aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->setValue(
287 // isStart[0] ? aStartEndPnt[0] : aStartEndPnt[1]);
288 //aConstraint->execute();
289 //myProducedFeatures.push_back(aConstraint);
290 //ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
291 // 3. Tangency of fillet arc and features
292 for (int i = 0; i < aNbFeatures; i++) {
293 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
294 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
295 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
296 aRefAttr->setObject(aNewArc->lastResult());
297 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
298 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
299 bool isArc = aNewFeature[i]->getKind() == SketchPlugin_Arc::ID();
300 aRefAttr->setObject(isArc ? aNewFeature[i]->lastResult() : aNewFeature[i]->firstResult());
301 aConstraint->execute();
302 myProducedFeatures.push_back(aConstraint);
303 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
305 // 4. Coincidence of free boundaries of base and copied features
306 for (int i = 0; i < aNbFeatures; i++) {
307 anAttrInd = 2*i + (isStart[i] ? 1 : 0);
308 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
309 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
310 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
311 aRefAttr->setAttr(aFeature[i]->attribute(aFeatAttributes[anAttrInd]));
312 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
313 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
314 aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
315 myProducedFeatures.push_back(aConstraint);
317 // 5. Tangent points should be placed on the base features
318 for (int i = 0; i < aNbFeatures; i++) {
319 anAttrInd = 2*i + (isStart[i] ? 0 : 1);
320 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
321 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
322 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
323 aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
324 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
325 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
326 aRefAttr->setObject(aFeature[i]->lastResult());
327 myProducedFeatures.push_back(aConstraint);
329 // make base features auxiliary
330 anOldFeatureA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
331 anOldFeatureB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
332 myBaseObjects.clear();
333 myBaseObjects.push_back(anOldFeatureA);
334 myBaseObjects.push_back(anOldFeatureB);
335 // exchange the naming IDs of newly created and old line that become auxiliary
336 sketch()->exchangeIDs(anOldFeatureA, aNewFeatureA);
337 sketch()->exchangeIDs(anOldFeatureB, aNewFeatureB);
339 // Update radius value
340 int aNbSubs = sketch()->numberOfSubs();
341 FeaturePtr aSubFeature;
342 for (int aSub = 0; aSub < aNbSubs; aSub++) {
343 aSubFeature = sketch()->subFeature(aSub);
344 if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
346 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
347 aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
348 if (!aRefAttr || !aRefAttr->isObject())
350 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
351 if (aFeature == aNewArc) {
352 AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
353 aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
354 aRadius->setValue(aFilletRadius);
360 // send events to update the sub-features by the solver
362 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
364 // the viewer update should be unblocked in order after the fillet features
365 // are processed by the solver
366 //aMsg = std::shared_ptr<Events_Message>(
367 // new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
368 //Events_Loop::loop()->send(aMsg);
371 void SketchPlugin_ConstraintFillet::attributeChanged(const std::string& theID)
373 if (theID == SketchPlugin_Constraint::ENTITY_A()) {
374 // clear the list of fillet entities
375 AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
376 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
377 aRefListOfFillet->clear();
379 // clear the list of base features
380 AttributeRefListPtr aRefListOfBaseLines = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
381 data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
382 aRefListOfBaseLines->clear();
384 // remove all produced objects and constraints
385 DocumentPtr aDoc = sketch()->document();
386 std::list<FeaturePtr>::iterator aCIt = myProducedFeatures.begin();
387 for (; aCIt != myProducedFeatures.end(); ++aCIt)
388 aDoc->removeFeature(*aCIt);
389 myProducedFeatures.clear();
391 // clear auxiliary flag on initial objects
392 for (aCIt = myBaseObjects.begin(); aCIt != myBaseObjects.end(); ++aCIt)
393 (*aCIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
394 myBaseObjects.clear();
396 AttributeRefAttrPtr aBaseA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
397 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
398 if(!aBaseA->isInitialized() || aBaseA->isObject()) {
402 AttributePtr anAttrBase = aBaseA->attr();
403 const std::set<AttributePtr>& aRefsList = anAttrBase->owner()->data()->refsToMe();
404 std::set<AttributePtr>::const_iterator aIt;
405 FeaturePtr aCoincident;
406 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
407 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
408 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
409 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
410 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
411 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
412 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
413 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
414 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
415 AttributePtr anAttrA = anAttrRefA->attr();
416 if(anAttrBase == anAttrA) {
417 aCoincident = aConstrFeature;
421 if(anAttrRefA.get() && !anAttrRefB->isObject()) {
422 AttributePtr anAttrB = anAttrRefB->attr();
423 if(anAttrBase == anAttrB) {
424 aCoincident = aConstrFeature;
431 if(!aCoincident.get()) {
432 setError("No coincident edges at selected vertex");
436 std::set<FeaturePtr> aCoinsideLines;
437 SketchPlugin_Tools::findCoincidences(aCoincident,
438 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
440 SketchPlugin_Tools::findCoincidences(aCoincident,
441 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
444 // Remove auxilary lines
445 if(aCoinsideLines.size() > 2) {
446 std::set<FeaturePtr> aNewLines;
447 for(std::set<FeaturePtr>::iterator anIt = aCoinsideLines.begin(); anIt != aCoinsideLines.end(); ++anIt) {
448 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
449 aNewLines.insert(*anIt);
452 aCoinsideLines = aNewLines;
456 if(aCoinsideLines.size() != 2) {
457 setError("At selected vertex should be two coincident lines");
461 FeaturePtr anOldFeatureA, anOldFeatureB;
462 std::set<FeaturePtr>::iterator aLinesIt = aCoinsideLines.begin();
463 anOldFeatureA = *aLinesIt++;
464 anOldFeatureB = *aLinesIt;
465 aRefListOfBaseLines->append(anOldFeatureA);
466 aRefListOfBaseLines->append(anOldFeatureB);
469 // Set default value equal to 1/3 of the smallest line sharing the point.
470 static const int aNbFeatures = 2;
471 FeaturePtr aFeature[aNbFeatures] = {anOldFeatureA, anOldFeatureB};
474 double aLengths[aNbFeatures];
475 for (int i = 0; i < aNbFeatures; i++) {
476 std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature[i]->attribute(
477 aFeature[i]->getKind() == SketchPlugin_Line::ID() ? SketchPlugin_Line::START_ID() : SketchPlugin_Arc::START_ID()))->pnt();
478 std::shared_ptr<GeomAPI_Pnt2d> anEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature[i]->attribute(
479 aFeature[i]->getKind() == SketchPlugin_Line::ID() ? SketchPlugin_Line::END_ID() : SketchPlugin_Arc::END_ID()))->pnt();
480 if(aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
481 aLengths[i] = aStartPnt->distance(anEndPnt);
483 std::shared_ptr<GeomAPI_Pnt2d> anArcCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature[i]->attribute(
484 SketchPlugin_Arc::CENTER_ID()))->pnt();
485 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(aStartPnt->xy()->decreased(anArcCenter->xy())));
486 std::shared_ptr<GeomAPI_Dir2d> anEndDir(new GeomAPI_Dir2d(anEndPnt->xy()->decreased(anArcCenter->xy())));
487 double aRadius = aStartPnt->distance(anArcCenter);
488 double anAngle = aStartDir->angle(anEndDir);
489 aLengths[i] = aRadius * abs(anAngle);
492 aLength = aLengths[0];
493 for(int i = 1; i < aNbFeatures; i++) {
494 if(aLengths[i] < aLength) aLength = aLengths[i];
496 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aLength / 3.0);
500 AISObjectPtr SketchPlugin_ConstraintFillet::getAISObject(AISObjectPtr thePrevious)
505 AISObjectPtr anAIS = thePrevious;
506 /// TODO: Equal constraint presentation should be put here
510 bool SketchPlugin_ConstraintFillet::isMacro() const
516 // ========= Auxiliary functions =================
517 void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
518 FeaturePtr theFeature, const std::string& theFeatureAttribute)
520 std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
521 theNewArc->attribute(theNewArcAttribute))->pnt();
522 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
523 theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
526 /// \brief Find intersections of lines shifted along normal direction
527 void possibleFilletCenterLineLine(
528 std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
529 std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
530 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
532 std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
533 std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
534 std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
535 double aDet = theDirA->cross(theDirB);
536 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
537 aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
538 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
539 aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
540 double aVX = aDirAT->xy()->dot(aPntA);
541 double aVY = aDirBT->xy()->dot(aPntB);
542 std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
543 (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
544 (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
545 theCenters.push_back(aPoint);
550 /// \brief Find intersections of line shifted along normal direction in both sides
551 /// and a circle with extended radius
552 void possibleFilletCenterLineArc(
553 std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
554 std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
555 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
557 std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
558 std::shared_ptr<GeomAPI_XY> aPnt;
559 double aDirNorm2 = theDirLine->dot(theDirLine);
561 double aDirX = theDirLine->x();
562 double aDirX2 = theDirLine->x() * theDirLine->x();
563 double aDirY2 = theDirLine->y() * theDirLine->y();
564 double aDirXY = theDirLine->x() * theDirLine->y();
565 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
566 aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
567 double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
568 double aCoeff2 = aCoeff * aCoeff;
569 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
570 aRad = theRadiusArc + aStepB * theRadius;
571 double aD = aRad * aRad * aDirNorm2 - aCoeff2;
574 double aDs = sqrt(aD);
575 double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
576 double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
577 double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
578 aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
579 double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
580 aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
582 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
583 theCenters.push_back(aPoint1);
584 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
585 theCenters.push_back(aPoint2);
590 /// \brief Find intersections of two circles with extended radii
591 void possibleFilletCenterArcArc(
592 std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
593 std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
594 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
596 std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
597 double aCenterDist2 = aCenterDir->dot(aCenterDir);
598 double aCenterDist = sqrt(aCenterDist2);
601 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
602 aRadA = theRadiusA + aStepA * theRadius;
603 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
604 aRadB = theRadiusB + aStepB * theRadius;
605 if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
606 continue; // there is no intersections
608 double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
609 double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
611 double x1 = theCenterA->x() + (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
612 double y1 = theCenterA->y() + (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
614 double x2 = theCenterA->x() + (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
615 double y2 = theCenterA->y() + (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
617 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
618 theCenters.push_back(aPoint1);
619 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
620 theCenters.push_back(aPoint2);
625 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
626 double theRadius, bool theNotInversed[2],
627 std::shared_ptr<GeomAPI_XY>& theCenter,
628 std::shared_ptr<GeomAPI_XY>& theTangentA,
629 std::shared_ptr<GeomAPI_XY>& theTangentB)
631 static const int aNbFeatures = 2;
632 FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
633 std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
634 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
636 for (int i = 0; i < aNbFeatures; i++) {
637 if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
638 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
639 aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
640 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
641 aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
642 } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
643 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
644 aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
645 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
646 aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
647 aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
648 aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
651 aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
652 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
653 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
654 aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
655 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
656 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
659 if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
660 theFeatureB->getKind() == SketchPlugin_Line::ID()) {
661 std::shared_ptr<GeomAPI_Dir2d> aDir[2];
662 std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
663 for (int i = 0; i < aNbFeatures; i++) {
664 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
665 aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
668 // get and filter possible centers
669 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
670 possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1], theRadius, aSuspectCenters);
672 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
673 for (; anIt != aSuspectCenters.end(); anIt++) {
674 aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
675 theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
676 if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
677 continue; // incorrect position
678 aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
679 theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
680 if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
681 continue; // incorrect position
682 // the center is found, stop searching
686 } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
687 theFeatureB->getKind() == SketchPlugin_Line::ID()) ||
688 (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
689 theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
690 int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
691 double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
692 std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
693 new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
694 std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
695 new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
697 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
698 new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
699 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
700 new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
701 double anArcAngle = aEndArcDir->angle(aStartArcDir);
703 // get and filter possible centers
704 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
705 possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd], anArcRadius, theRadius, aSuspectCenters);
707 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
708 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
709 for (; anIt != aSuspectCenters.end(); anIt++) {
710 aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
711 aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
712 if (aLineTgPoint->decreased(aStart[aLineInd])->dot(aDirLine->xy()) < 0.0)
713 continue; // incorrect position
714 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
715 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
716 double aCurAngle = aCurDir->angle(aStartArcDir);
717 if (anArcAngle < 0.0) aCurAngle *= -1.0;
718 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
719 continue; // incorrect position
720 // the center is found, stop searching
722 anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
723 if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
724 theTangentA = aLineTgPoint;
725 theTangentB = anArcTgPoint;
727 theTangentA = anArcTgPoint;
728 theTangentB = aLineTgPoint;
732 } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
733 theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
734 double anArcRadius[aNbFeatures];
735 double anArcAngle[aNbFeatures];
736 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
737 for (int i = 0; i < aNbFeatures; i++) {
738 anArcRadius[i] = aStart[i]->distance(aCenter[i]);
739 aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
740 new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
741 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
742 new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
743 anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
746 // get and filter possible centers
747 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
748 possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1], anArcRadius[1], theRadius, aSuspectCenters);
750 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
751 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
752 for (; anIt != aSuspectCenters.end(); anIt++) {
753 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
754 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
755 double aCurAngle = aCurDir->angle(aStartArcDir[0]);
756 if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
757 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
758 continue; // incorrect position
759 theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
761 aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
762 aCurAngle = aCurDir->angle(aStartArcDir[1]);
763 if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
764 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
765 continue; // incorrect position
766 theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
768 // the center is found, stop searching