1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchPlugin_Fillet.h"
23 #include "SketchPlugin_Arc.h"
24 #include "SketchPlugin_Line.h"
25 #include "SketchPlugin_Point.h"
26 #include "SketchPlugin_Sketch.h"
27 #include "SketchPlugin_ConstraintDistance.h"
28 #include "SketchPlugin_ConstraintEqual.h"
29 #include "SketchPlugin_ConstraintCoincidence.h"
30 #include "SketchPlugin_ConstraintLength.h"
31 #include "SketchPlugin_ConstraintMiddle.h"
32 #include "SketchPlugin_ConstraintTangent.h"
33 #include "SketchPlugin_ConstraintRadius.h"
34 #include "SketchPlugin_Tools.h"
36 #include <ModelAPI_AttributeDouble.h>
37 #include <ModelAPI_AttributeInteger.h>
38 #include <ModelAPI_AttributeRefAttr.h>
39 #include <ModelAPI_Data.h>
40 #include <ModelAPI_Events.h>
41 #include <ModelAPI_Session.h>
42 #include <ModelAPI_Tools.h>
43 #include <ModelAPI_Validator.h>
45 #include <GeomAlgoAPI_Circ2dBuilder.h>
46 #include <GeomAlgoAPI_EdgeBuilder.h>
48 #include <GeomAPI_Circ2d.h>
49 #include <GeomAPI_Dir2d.h>
50 #include <GeomAPI_Lin2d.h>
51 #include <GeomAPI_Pnt2d.h>
52 #include <GeomAPI_XY.h>
54 #include <GeomDataAPI_Point2D.h>
56 #include <Events_Loop.h>
60 const double tolerance = 1.e-7;
61 const double paramTolerance = 1.e-4;
62 const double PI = 3.141592653589793238463;
64 /// \brief Attract specified point on theNewArc to the attribute of theFeature
65 static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
66 FeaturePtr theFeature, const std::string& theFeatureAttribute);
69 /// \brief Calculate radius of a fillet.
70 /// It should not be greater than 1/3 of shortest edge length.
71 static double calculateFilletRadius(FeaturePtr theFilletFeatures[2]);
73 /// \brief Calculates center of fillet arc and coordinates of tangency points
74 static void calculateFilletCenter(FeaturePtr theFilletFeatures[2],
75 double theFilletRadius,
76 const std::shared_ptr<GeomAPI_Ax3>& theSketchPlane,
77 std::shared_ptr<GeomAPI_XY>& theCenter,
78 std::shared_ptr<GeomAPI_XY>& theTangentA,
79 std::shared_ptr<GeomAPI_XY>& theTangentB);
81 static std::set<FeaturePtr> findFeaturesToRemove(const FeaturePtr theFeature,
82 const AttributePtr theAttribute);
84 SketchPlugin_Fillet::SketchPlugin_Fillet()
85 : myFilletCreated(false)
89 void SketchPlugin_Fillet::initAttributes()
91 data()->addAttribute(FILLET_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
94 void SketchPlugin_Fillet::execute()
96 // Wait all constraints being created, then send update events
97 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
98 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
100 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
102 // set flag here to avoid building Fillet presentation if "Redisplay" event appears
103 myFilletCreated = true;
105 // create feature for fillet arc
106 FeaturePtr aFilletArc = createFilletArc();
108 // collect features referred to the edges participating in fillet
109 AttributePoint2DPtr aFilletPoints[2];
112 std::set<FeaturePtr> aFeaturesToBeRemoved;
113 for (int i = 0; i < 2; ++i) {
114 bool isFirstIndex = (i == 0);
115 aFeatInd[i] = myIsReversed == isFirstIndex ? 1 : 0;
116 anAttrInd[i] = (myIsReversed == isFirstIndex ? 2 : 0) + (myIsNotInversed[aFeatInd[i]] ? 0 : 1);
117 aFilletPoints[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
118 myBaseFeatures[aFeatInd[i]]->attribute(myFeatAttributes[anAttrInd[i]]));
119 std::set<FeaturePtr> aRemove =
120 findFeaturesToRemove(myBaseFeatures[aFeatInd[i]], aFilletPoints[i]);
121 aFeaturesToBeRemoved.insert(aRemove.begin(), aRemove.end());
124 // keep "distance" constraints and remove all other references
125 removeReferencesButKeepDistances(aFeaturesToBeRemoved, aFilletPoints);
127 // Update fillet edges.
128 recalculateAttributes(aFilletArc, SketchPlugin_Arc::START_ID(),
129 myBaseFeatures[aFeatInd[0]], myFeatAttributes[anAttrInd[0]]);
130 recalculateAttributes(aFilletArc, SketchPlugin_Arc::END_ID(),
131 myBaseFeatures[aFeatInd[1]], myFeatAttributes[anAttrInd[1]]);
133 FeaturePtr aConstraint;
135 // Create coincidence features.
136 aConstraint = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
137 SketchPlugin_ConstraintCoincidence::ID(),
138 aFilletArc->attribute(SketchPlugin_Arc::START_ID()),
139 myBaseFeatures[aFeatInd[0]]->attribute(myFeatAttributes[anAttrInd[0]]));
140 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
141 aConstraint = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
142 SketchPlugin_ConstraintCoincidence::ID(),
143 aFilletArc->attribute(SketchPlugin_Arc::END_ID()),
144 myBaseFeatures[aFeatInd[1]]->attribute(myFeatAttributes[anAttrInd[1]]));
145 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
147 // Create tangent features.
148 for (int i = 0; i < 2; i++) {
149 aConstraint = SketchPlugin_Tools::createConstraintObjectObject(sketch(),
150 SketchPlugin_ConstraintTangent::ID(),
151 aFilletArc->lastResult(),
152 myBaseFeatures[i]->lastResult());
153 aConstraint->execute();
154 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
157 // Send events to update the sub-features by the solver.
159 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
162 AISObjectPtr SketchPlugin_Fillet::getAISObject(AISObjectPtr thePrevious)
164 if(myFilletCreated) {
165 return AISObjectPtr();
168 SketchPlugin_Sketch* aSketch = sketch();
170 return AISObjectPtr();
173 if (!calculateFilletParameters())
174 return AISObjectPtr();
176 // Create arc for presentation.
177 std::shared_ptr<GeomAPI_Pnt> aCenterPnt(aSketch->to3D(myCenterXY->x(), myCenterXY->y()));
178 std::shared_ptr<GeomAPI_Pnt> aTangentPnt1(aSketch->to3D(myTangentXY1->x(),
180 std::shared_ptr<GeomAPI_Pnt> aTangentPnt2(aSketch->to3D(myTangentXY2->x(),
182 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
183 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
184 std::shared_ptr<GeomAPI_Shape> anArcShape =
185 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenterPnt, aTangentPnt1, aTangentPnt2, aNDir->dir());
187 AISObjectPtr anAISObject = thePrevious;
188 if(!anAISObject.get()) {
189 anAISObject = AISObjectPtr(new GeomAPI_AISObject);
191 anAISObject->createShape(anArcShape);
195 bool SketchPlugin_Fillet::calculateFilletParameters()
198 AttributeRefAttrPtr aPointRefAttr = refattr(FILLET_POINT_ID());
199 if (!aPointRefAttr->isInitialized() || aPointRefAttr->isObject())
201 std::shared_ptr<GeomDataAPI_Point2D> aFilletPoint2D =
202 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointRefAttr->attr());
203 if (!aFilletPoint2D.get())
206 std::set<FeaturePtr> aFilletFeatures =
207 SketchPlugin_Tools::findFeaturesCoincidentToPoint(aFilletPoint2D);
208 if (aFilletFeatures.size() != 2) {
209 setError("Error: Selected point does not have two suitable edges for fillet.");
213 std::set<FeaturePtr>::iterator aFIt = aFilletFeatures.begin();
214 myBaseFeatures[0] = *aFIt;
215 myBaseFeatures[1] = *(++aFIt);
217 std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt2d = aFilletPoint2D->pnt();
218 double aRadius = calculateFilletRadius(myBaseFeatures);
220 // Calculate arc attributes.
221 static const int aNbFeatures = 2;
222 // First pair of points relate to first feature, second pair - to second.
223 std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2];
224 for (int i = 0; i < aNbFeatures; i++) {
225 std::string aStartAttr, aEndAttr;
226 if (myBaseFeatures[i]->getKind() == SketchPlugin_Line::ID()) {
227 aStartAttr = SketchPlugin_Line::START_ID();
228 aEndAttr = SketchPlugin_Line::END_ID();
229 } else if (myBaseFeatures[i]->getKind() == SketchPlugin_Arc::ID()) {
230 aStartAttr = SketchPlugin_Arc::START_ID();
231 aEndAttr = SketchPlugin_Arc::END_ID();
232 } else { // Wrong argument.
233 setError("Error: One of the points has wrong coincide feature");
236 myFeatAttributes[2*i] = aStartAttr;
237 aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
238 myBaseFeatures[i]->attribute(aStartAttr))->pnt();
239 myFeatAttributes[2*i+1] = aEndAttr;
240 aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
241 myBaseFeatures[i]->attribute(aEndAttr))->pnt();
243 for (int aFeatInd = 0; aFeatInd < aNbFeatures; aFeatInd++) {
244 for (int j = 0; j < 2; j++) // loop on start-end of each feature
245 if (aStartEndPnt[aFeatInd * aNbFeatures + j]->distance(aFilletPnt2d) < 1.e-10) {
246 myIsNotInversed[aFeatInd] = (j==0);
251 std::shared_ptr<GeomAPI_Ax3> aSketchPlane = SketchPlugin_Sketch::plane(sketch());
252 calculateFilletCenter(myBaseFeatures, aRadius, aSketchPlane,
253 myCenterXY, myTangentXY1, myTangentXY2);
255 // Tangent directions of the features in coincident point.
256 std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures];
257 for (int i = 0; i < aNbFeatures; i++) {
258 std::shared_ptr<GeomAPI_XY> aDir;
259 if (myBaseFeatures[i]->getKind() == SketchPlugin_Line::ID()) {
260 aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
261 if (!myIsNotInversed[i])
262 aDir = aDir->multiplied(-1.0);
263 } else if (myBaseFeatures[i]->getKind() == SketchPlugin_Arc::ID()) {
264 std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint =
265 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
266 myBaseFeatures[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
267 aDir = myIsNotInversed[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
268 aDir = aDir->decreased(aCenterPoint->xy());
270 double x = aDir->x();
271 double y = aDir->y();
274 if (myIsNotInversed[i] ==
275 std::dynamic_pointer_cast<SketchPlugin_Arc>(myBaseFeatures[i])->isReversed())
276 aDir = aDir->multiplied(-1.0);
278 aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
281 // By default, the start point of fillet arc is connected to FeatureA,
282 // and the end point - to FeatureB. But when the angle between TangentDirA and
283 // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
284 double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A),
285 // where A and B - angles between corresponding tanget direction and the X axis
286 myIsReversed = cosBA > 0.0;
289 std::shared_ptr<GeomAPI_XY> aTmp = myTangentXY1;
290 myTangentXY1 = myTangentXY2;
296 FeaturePtr SketchPlugin_Fillet::createFilletArc()
298 // Calculate Fillet parameters if does not yet
299 if (!myBaseFeatures[0] || !myBaseFeatures[1])
300 calculateFilletParameters();
302 // Create arc feature.
303 FeaturePtr aFilletArc = sketch()->addFeature(SketchPlugin_Arc::ID());
305 // Set arc attributes.
306 bool aWasBlocked = aFilletArc->data()->blockSendAttributeUpdated(true);
307 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
308 aFilletArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenterXY->x(),
310 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
311 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
312 aFilletArc->attribute(SketchPlugin_Arc::START_ID()));
313 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
314 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
315 aFilletArc->attribute(SketchPlugin_Arc::END_ID()));
316 if(aStartPoint->isInitialized() && aEndPoint->isInitialized()
317 && (aStartPoint->pnt()->xy()->distance(myTangentXY1) > tolerance
318 || aEndPoint->pnt()->xy()->distance(myTangentXY2) > tolerance)) {
319 std::dynamic_pointer_cast<SketchPlugin_Arc>(aFilletArc)->setReversed(false);
321 aStartPoint->setValue(myTangentXY1->x(), myTangentXY1->y());
322 aEndPoint->setValue(myTangentXY2->x(), myTangentXY2->y());
323 aFilletArc->data()->blockSendAttributeUpdated(aWasBlocked);
324 aFilletArc->execute();
329 FeaturePtr SketchPlugin_Fillet::createFilletApex(const GeomPnt2dPtr& theCoordinates)
331 FeaturePtr anApex = sketch()->addFeature(SketchPlugin_Point::ID());
332 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
333 anApex->attribute(SketchPlugin_Point::COORD_ID()));
334 aCoord->setValue(theCoordinates);
335 anApex->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
337 // additional coincidence constraints
338 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
339 FeaturePtr aConstraint;
340 for (int i = 0; i < 2; i++) {
341 aConstraint = SketchPlugin_Tools::createConstraintAttrObject(sketch(),
342 SketchPlugin_ConstraintCoincidence::ID(),
344 myBaseFeatures[i]->lastResult());
345 aConstraint->execute();
346 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
353 AttributePtr myPoints[2];
354 std::string myValueText;
355 double myValueDouble;
356 GeomPnt2dPtr myFlyoutPoint;
360 void SketchPlugin_Fillet::removeReferencesButKeepDistances(
361 std::set<FeaturePtr>& theFeaturesToRemove,
362 const AttributePoint2DPtr theFilletPoints[2])
364 FeaturePtr aFilletApex;
365 std::list<Length> aLengthToDistance;
367 std::set<FeaturePtr>::iterator aFeat = theFeaturesToRemove.begin();
368 while (aFeat != theFeaturesToRemove.end()) {
369 std::shared_ptr<SketchPlugin_ConstraintDistance> aDistance =
370 std::dynamic_pointer_cast<SketchPlugin_ConstraintDistance>(*aFeat);
373 aFilletApex = createFilletApex(theFilletPoints[0]->pnt());
374 // update attributes of distance constraints
375 bool isUpdated = false;
376 for (int attrInd = 0; attrInd < CONSTRAINT_ATTR_SIZE && !isUpdated; ++attrInd) {
377 AttributeRefAttrPtr aRefAttr =
378 aDistance->refattr(SketchPlugin_Constraint::ATTRIBUTE(attrInd));
379 if (aRefAttr && !aRefAttr->isObject() &&
380 (aRefAttr->attr() == theFilletPoints[0] || aRefAttr->attr() == theFilletPoints[1])) {
381 aRefAttr->setAttr(aFilletApex->attribute(SketchPlugin_Point::COORD_ID()));
385 // avoid distance from removing if it is updated
386 std::set<FeaturePtr>::iterator aKeepIt = aFeat++;
388 theFeaturesToRemove.erase(aKeepIt);
391 std::shared_ptr<SketchPlugin_ConstraintLength> aLength =
392 std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*aFeat);
395 aFilletApex = createFilletApex(theFilletPoints[0]->pnt());
396 // remove Length, but create new distance constraint
397 AttributeRefAttrPtr aRefAttr =
398 aLength->refattr(SketchPlugin_Constraint::ENTITY_A());
399 FeaturePtr aLine = ModelAPI_Feature::feature(aRefAttr->object());
401 aLengthToDistance.push_back(Length());
402 Length& aNewLength = aLengthToDistance.back();
404 for (int i = 0; i < 2; ++i) {
405 AttributePtr anAttr = aLine->attribute(
406 i == 0 ? SketchPlugin_Line::START_ID() : SketchPlugin_Line::END_ID());
407 if (anAttr == theFilletPoints[0] || anAttr == theFilletPoints[1])
408 aNewLength.myPoints[i] = aFilletApex->attribute(SketchPlugin_Point::COORD_ID());
410 aNewLength.myPoints[i] = anAttr;
413 AttributeDoublePtr aValue = aLength->real(SketchPlugin_Constraint::VALUE());
414 aNewLength.myValueDouble = aValue->value();
415 aNewLength.myValueText = aValue->text();
416 // auxiliary attributes
417 AttributePoint2DPtr aFlyoutAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
418 aLength->attribute(SketchPlugin_ConstraintLength::FLYOUT_VALUE_PNT()));
419 if (aFlyoutAttr && aFlyoutAttr->isInitialized())
420 aNewLength.myFlyoutPoint = SketchPlugin_Tools::flyoutPointCoordinates(aLength);
421 AttributeIntegerPtr aLocationAttr =
422 aLength->integer(SketchPlugin_ConstraintLength::LOCATION_TYPE_ID());
423 if (aLocationAttr && aLocationAttr->isInitialized())
424 aNewLength.myLocationType = aLocationAttr->value();
426 aNewLength.myLocationType = -1;
435 ModelAPI_Tools::removeFeaturesAndReferences(theFeaturesToRemove);
436 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
438 // restore Length constraints as point-point distances
439 FeaturePtr aConstraint;
440 std::list<Length>::iterator anIt = aLengthToDistance.begin();
441 for (; anIt != aLengthToDistance.end(); ++anIt) {
442 aConstraint = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
443 SketchPlugin_ConstraintDistance::ID(), anIt->myPoints[0], anIt->myPoints[1]);
445 AttributeDoublePtr aValue = aConstraint->real(SketchPlugin_Constraint::VALUE());
446 if (anIt->myValueText.empty())
447 aValue->setValue(anIt->myValueDouble);
449 aValue->setText(anIt->myValueText);
450 // set flyout point if exists
451 if (anIt->myFlyoutPoint) {
452 AttributePoint2DPtr aFlyoutAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
453 aConstraint->attribute(SketchPlugin_ConstraintDistance::FLYOUT_VALUE_PNT()));
454 aFlyoutAttr->setValue(anIt->myFlyoutPoint);
456 // set location type if initialized
457 if (anIt->myLocationType >= 0) {
458 AttributeIntegerPtr aLocationType =
459 aConstraint->integer(SketchPlugin_ConstraintDistance::LOCATION_TYPE_ID());
460 aLocationType->setValue(anIt->myLocationType);
462 aConstraint->execute();
463 ModelAPI_EventCreator::get()->sendUpdated(aConstraint,
464 Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
468 // ========= Auxiliary functions =================
469 void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
470 FeaturePtr theFeature, const std::string& theFeatureAttribute)
472 std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
473 theNewArc->attribute(theNewArcAttribute))->pnt();
474 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
475 theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
478 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const AttributePtr& theAttribute)
480 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
481 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
483 aPoint = aPointAttr->pnt();
487 static std::shared_ptr<GeomAPI_Lin2d> toLine(const FeaturePtr& theFeature)
489 std::shared_ptr<GeomAPI_Lin2d> aLine;
490 if (theFeature->getKind() == SketchPlugin_Line::ID()) {
491 std::shared_ptr<GeomAPI_Pnt2d> aStart =
492 toPoint( theFeature->attribute(SketchPlugin_Line::START_ID()) );
493 std::shared_ptr<GeomAPI_Pnt2d> aEnd =
494 toPoint( theFeature->attribute(SketchPlugin_Line::END_ID()) );
495 aLine = std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStart, aEnd));
500 static std::shared_ptr<GeomAPI_Circ2d> toCircle(const FeaturePtr& theFeature)
502 std::shared_ptr<GeomAPI_Circ2d> aCircle;
503 if (theFeature->getKind() == SketchPlugin_Arc::ID()) {
504 std::shared_ptr<GeomAPI_Pnt2d> aCenter =
505 toPoint( theFeature->attribute(SketchPlugin_Arc::CENTER_ID()) );
506 std::shared_ptr<GeomAPI_Pnt2d> aStart =
507 toPoint( theFeature->attribute(SketchPlugin_Arc::START_ID()) );
508 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(aCenter, aStart));
514 void calculateFilletCenter(FeaturePtr theFilletFeatures[2],
515 double theFilletRadius,
516 const std::shared_ptr<GeomAPI_Ax3>& theSketchPlane,
517 std::shared_ptr<GeomAPI_XY>& theCenter,
518 std::shared_ptr<GeomAPI_XY>& theTangentA,
519 std::shared_ptr<GeomAPI_XY>& theTangentB)
521 GeomShapePtr aShapeA = theFilletFeatures[0]->lastResult()->shape();
522 GeomShapePtr aShapeB = theFilletFeatures[1]->lastResult()->shape();
524 GeomAlgoAPI_Circ2dBuilder aCircBuilder(theSketchPlane);
525 aCircBuilder.addTangentCurve(aShapeA);
526 aCircBuilder.addTangentCurve(aShapeB);
527 aCircBuilder.setRadius(theFilletRadius);
529 std::shared_ptr<GeomAPI_Circ2d> aFilletCircle = aCircBuilder.circle();
533 theCenter = aFilletCircle->center()->xy();
535 std::shared_ptr<GeomAPI_Pnt2d> aTgPoints[2];
536 for (int i = 0; i < 2; ++i) {
537 std::shared_ptr<GeomAPI_Circ2d> aCircle = toCircle(theFilletFeatures[i]);
539 aTgPoints[i] = aCircle->project(aFilletCircle->center());
541 std::shared_ptr<GeomAPI_Lin2d> aLine = toLine(theFilletFeatures[i]);
543 aTgPoints[i] = aLine->project(aFilletCircle->center());
546 theTangentA = aTgPoints[0]->xy();
547 theTangentB = aTgPoints[1]->xy();
550 double calculateFilletRadius(FeaturePtr theFilletFeatures[2])
552 double aLengths[2] = { 0, 0 };
553 for (int i = 0; i < 2; ++i) {
554 GeomShapePtr aShape = theFilletFeatures[i]->lastResult()->shape();
555 std::shared_ptr<GeomAPI_Edge> anEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(aShape);
557 aLengths[i] = anEdge->length();
559 return std::min(aLengths[0], aLengths[1]) / 6.0;
562 std::set<FeaturePtr> findFeaturesToRemove(const FeaturePtr theFeature,
563 const AttributePtr theAttribute) {
564 std::set<FeaturePtr> aFeaturesToBeRemoved;
565 std::set<AttributePtr> aRefs = theFeature->data()->refsToMe();
566 std::list<ResultPtr> aResults = theFeature->results();
567 for(std::list<ResultPtr>::const_iterator aResultsIt = aResults.cbegin();
568 aResultsIt != aResults.cend();
570 ResultPtr aResult = *aResultsIt;
571 std::set<AttributePtr> aResultRefs = aResult->data()->refsToMe();
572 aRefs.insert(aResultRefs.begin(), aResultRefs.end());
574 for(std::set<AttributePtr>::const_iterator anIt = aRefs.cbegin();
575 anIt != aRefs.cend();
577 std::shared_ptr<ModelAPI_Attribute> anAttr = (*anIt);
578 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
579 if(aFeature->getKind() == SketchPlugin_Fillet::ID()) {
582 if(aFeature->getKind() == SketchPlugin_ConstraintLength::ID()
583 || aFeature->getKind() == SketchPlugin_ConstraintEqual::ID()
584 || aFeature->getKind() == SketchPlugin_ConstraintMiddle::ID()) {
585 aFeaturesToBeRemoved.insert(aFeature);
587 std::list<AttributePtr> anAttrs =
588 aFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
589 for(std::list<AttributePtr>::const_iterator aRefAttrsIt = anAttrs.cbegin();
590 aRefAttrsIt != anAttrs.cend();
592 AttributeRefAttrPtr anAttrRefAttr =
593 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefAttrsIt);
594 if(anAttrRefAttr.get() && anAttrRefAttr->attr() == theAttribute) {
595 aFeaturesToBeRemoved.insert(aFeature);
600 return aFeaturesToBeRemoved;