1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Fillet.cpp
4 // Created: 19 Mar 2015
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_Fillet.h"
9 #include "SketchPlugin_Arc.h"
10 #include "SketchPlugin_Line.h"
11 #include "SketchPlugin_Point.h"
12 #include "SketchPlugin_Sketch.h"
13 #include "SketchPlugin_ConstraintEqual.h"
14 #include "SketchPlugin_ConstraintCoincidence.h"
15 #include "SketchPlugin_ConstraintLength.h"
16 #include "SketchPlugin_ConstraintTangent.h"
17 #include "SketchPlugin_ConstraintRadius.h"
18 #include "SketchPlugin_Tools.h"
20 #include <ModelAPI_AttributeRefAttr.h>
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Events.h>
23 #include <ModelAPI_Session.h>
24 #include <ModelAPI_Tools.h>
25 #include <ModelAPI_Validator.h>
27 #include <GeomAlgoAPI_EdgeBuilder.h>
29 #include <GeomAPI_Circ2d.h>
30 #include <GeomAPI_Dir2d.h>
31 #include <GeomAPI_Lin2d.h>
32 #include <GeomAPI_Pnt2d.h>
33 #include <GeomAPI_XY.h>
35 #include <GeomDataAPI_Point2D.h>
37 #include <Events_Loop.h>
41 const double tolerance = 1.e-7;
42 const double paramTolerance = 1.e-4;
44 /// \brief Attract specified point on theNewArc to the attribute of theFeature
45 static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
46 FeaturePtr theFeature, const std::string& theFeatureAttribute);
48 /// \brief Calculates center of fillet arc and coordinates of tangency points
49 static void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
50 double theRadius, bool theNotInversed[2],
51 std::shared_ptr<GeomAPI_XY>& theCenter,
52 std::shared_ptr<GeomAPI_XY>& theTangentA,
53 std::shared_ptr<GeomAPI_XY>& theTangentB);
55 /// Get point on 1/3 length of edge from fillet point
56 static void getPointOnEdge(const FeaturePtr theFeature,
57 const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
58 std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
60 /// Get distance from point to feature
61 static double getProjectionDistance(const FeaturePtr theFeature,
62 const std::shared_ptr<GeomAPI_Pnt2d> thePoint);
64 /// Get coincide edges for fillet
65 static std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence);
67 static std::set<FeaturePtr> findFeaturesToRemove(const FeaturePtr theFeature,
68 const AttributePtr theAttribute);
70 SketchPlugin_Fillet::SketchPlugin_Fillet()
71 : myFilletCreated(false)
75 void SketchPlugin_Fillet::initAttributes()
77 data()->addAttribute(FILLET_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
80 void SketchPlugin_Fillet::execute()
82 // Wait all constraints being created, then send update events
83 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
84 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
86 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
88 // Calculate Fillet parameters if does not yet
89 if (!myBaseFeatures[0] || !myBaseFeatures[1])
90 calculateFilletParameters();
92 // Create arc feature.
93 FeaturePtr aFilletArc = sketch()->addFeature(SketchPlugin_Arc::ID());
95 // Set arc attributes.
96 bool aWasBlocked = aFilletArc->data()->blockSendAttributeUpdated(true);
97 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
98 aFilletArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenterXY->x(),
100 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
101 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
102 aFilletArc->attribute(SketchPlugin_Arc::START_ID()));
103 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
104 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
105 aFilletArc->attribute(SketchPlugin_Arc::END_ID()));
106 if(aStartPoint->isInitialized() && aEndPoint->isInitialized()
107 && (aStartPoint->pnt()->xy()->distance(myTangentXY1) > tolerance
108 || aEndPoint->pnt()->xy()->distance(myTangentXY2) > tolerance)) {
109 std::dynamic_pointer_cast<SketchPlugin_Arc>(aFilletArc)->setReversed(false);
111 aStartPoint->setValue(myTangentXY1->x(), myTangentXY1->y());
112 aEndPoint->setValue(myTangentXY2->x(), myTangentXY2->y());
113 aFilletArc->data()->blockSendAttributeUpdated(aWasBlocked);
114 aFilletArc->execute();
116 // Delete features with refs to points of edges.
117 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint1;
118 int aFeatInd1 = myIsReversed ? 1 : 0;
119 int anAttrInd1 = (myIsReversed ? 2 : 0) + (myIsNotInversed[aFeatInd1] ? 0 : 1);
120 aStartPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
121 myBaseFeatures[aFeatInd1]->attribute(myFeatAttributes[anAttrInd1]));
122 std::set<FeaturePtr> aFeaturesToBeRemoved1 =
123 findFeaturesToRemove(myBaseFeatures[aFeatInd1], aStartPoint1);
125 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint2;
126 int aFeatInd2 = myIsReversed ? 0 : 1;
127 int anAttrInd2 = (myIsReversed ? 0 : 2) + (myIsNotInversed[aFeatInd2] ? 0 : 1);
128 aStartPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
129 myBaseFeatures[aFeatInd2]->attribute(myFeatAttributes[anAttrInd2]));
130 std::set<FeaturePtr> aFeaturesToBeRemoved2 =
131 findFeaturesToRemove(myBaseFeatures[aFeatInd2], aStartPoint2);
133 aFeaturesToBeRemoved1.insert(aFeaturesToBeRemoved2.begin(), aFeaturesToBeRemoved2.end());
134 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved1);
135 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
137 // Update fillet edges.
138 recalculateAttributes(aFilletArc, SketchPlugin_Arc::START_ID(),
139 myBaseFeatures[aFeatInd1], myFeatAttributes[anAttrInd1]);
140 recalculateAttributes(aFilletArc, SketchPlugin_Arc::END_ID(),
141 myBaseFeatures[aFeatInd2], myFeatAttributes[anAttrInd2]);
143 // Create coincidence features.
144 FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
145 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
146 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
147 aRefAttr->setAttr(aFilletArc->attribute(SketchPlugin_Arc::START_ID()));
148 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
149 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
150 aRefAttr->setAttr(myBaseFeatures[aFeatInd1]->attribute(myFeatAttributes[anAttrInd1]));
151 aConstraint->execute();
152 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
153 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
154 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
155 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
156 aRefAttr->setAttr(aFilletArc->attribute(SketchPlugin_Arc::END_ID()));
157 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
158 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
159 aRefAttr->setAttr(myBaseFeatures[aFeatInd2]->attribute(myFeatAttributes[anAttrInd2]));
160 aConstraint->execute();
161 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
163 // Create tangent features.
164 for (int i = 0; i < 2; i++) {
165 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
166 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
167 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
168 aRefAttr->setObject(aFilletArc->lastResult());
169 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
170 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
171 bool isArc = myBaseFeatures[i]->getKind() == SketchPlugin_Arc::ID();
172 aRefAttr->setObject(isArc ? myBaseFeatures[i]->lastResult() :
173 myBaseFeatures[i]->firstResult());
174 aConstraint->execute();
175 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
178 // Send events to update the sub-features by the solver.
179 if(isUpdateFlushed) {
180 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
183 myFilletCreated = true;
186 AISObjectPtr SketchPlugin_Fillet::getAISObject(AISObjectPtr thePrevious)
188 if(myFilletCreated) {
189 return AISObjectPtr();
192 SketchPlugin_Sketch* aSketch = sketch();
194 return AISObjectPtr();
197 if (!calculateFilletParameters())
198 return AISObjectPtr();
200 // Create arc for presentation.
201 std::shared_ptr<GeomAPI_Pnt> aCenterPnt(aSketch->to3D(myCenterXY->x(), myCenterXY->y()));
202 std::shared_ptr<GeomAPI_Pnt> aTangentPnt1(aSketch->to3D(myTangentXY1->x(),
204 std::shared_ptr<GeomAPI_Pnt> aTangentPnt2(aSketch->to3D(myTangentXY2->x(),
206 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
207 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
208 std::shared_ptr<GeomAPI_Shape> anArcShape =
209 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenterPnt, aTangentPnt1, aTangentPnt2, aNDir->dir());
211 AISObjectPtr anAISObject = thePrevious;
212 if(!anAISObject.get()) {
213 anAISObject = AISObjectPtr(new GeomAPI_AISObject);
215 anAISObject->createShape(anArcShape);
219 bool SketchPlugin_Fillet::calculateFilletParameters()
222 AttributeRefAttrPtr aPointRefAttr = refattr(FILLET_POINT_ID());
223 if (!aPointRefAttr->isInitialized() || aPointRefAttr->isObject())
225 std::shared_ptr<GeomDataAPI_Point2D> aFilletPoint2D =
226 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointRefAttr->attr());
227 if (!aFilletPoint2D.get())
230 if (!findFeaturesContainingFilletPoint(aFilletPoint2D)) {
231 setError("Error: Selected point does not have two suitable edges for fillet.");
235 // Getting points located at 1/3 of edge length from fillet point.
236 std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt2d = aFilletPoint2D->pnt();
237 std::shared_ptr<GeomAPI_Pnt2d> aPnt1, aPnt2;
238 getPointOnEdge(myBaseFeatures[0], aFilletPnt2d, aPnt1);
239 getPointOnEdge(myBaseFeatures[1], aFilletPnt2d, aPnt2);
241 /// Getting distances.
242 double aDistance1 = getProjectionDistance(myBaseFeatures[1], aPnt1);
243 double aDistance2 = getProjectionDistance(myBaseFeatures[0], aPnt2);
245 // Calculate radius value for fillet.
246 double aRadius = aDistance1 < aDistance2 ? aDistance1 / 2.0 : aDistance2 / 2.0;
248 // Calculate arc attributes.
249 static const int aNbFeatures = 2;
250 // First pair of points relate to first feature, second pair - to second.
251 std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2];
252 for (int i = 0; i < aNbFeatures; i++) {
253 std::string aStartAttr, aEndAttr;
254 if (myBaseFeatures[i]->getKind() == SketchPlugin_Line::ID()) {
255 aStartAttr = SketchPlugin_Line::START_ID();
256 aEndAttr = SketchPlugin_Line::END_ID();
257 } else if (myBaseFeatures[i]->getKind() == SketchPlugin_Arc::ID()) {
258 aStartAttr = SketchPlugin_Arc::START_ID();
259 aEndAttr = SketchPlugin_Arc::END_ID();
260 } else { // Wrong argument.
261 setError("Error: One of the points has wrong coincide feature");
264 myFeatAttributes[2*i] = aStartAttr;
265 aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
266 myBaseFeatures[i]->attribute(aStartAttr))->pnt();
267 myFeatAttributes[2*i+1] = aEndAttr;
268 aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
269 myBaseFeatures[i]->attribute(aEndAttr))->pnt();
271 for (int aFeatInd = 0; aFeatInd < aNbFeatures; aFeatInd++) {
272 for (int j = 0; j < 2; j++) // loop on start-end of each feature
273 if (aStartEndPnt[aFeatInd * aNbFeatures + j]->distance(aFilletPnt2d) < 1.e-10) {
274 myIsNotInversed[aFeatInd] = (j==0);
279 calculateFilletCenter(myBaseFeatures[0], myBaseFeatures[1], aRadius,
280 myIsNotInversed, myCenterXY, myTangentXY1, myTangentXY2);
282 // Tangent directions of the features in coincident point.
283 std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures];
284 for (int i = 0; i < aNbFeatures; i++) {
285 std::shared_ptr<GeomAPI_XY> aDir;
286 if (myBaseFeatures[i]->getKind() == SketchPlugin_Line::ID()) {
287 aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
288 if (!myIsNotInversed[i])
289 aDir = aDir->multiplied(-1.0);
290 } else if (myBaseFeatures[i]->getKind() == SketchPlugin_Arc::ID()) {
291 std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint =
292 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
293 myBaseFeatures[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
294 aDir = myIsNotInversed[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
295 aDir = aDir->decreased(aCenterPoint->xy());
297 double x = aDir->x();
298 double y = aDir->y();
301 if (myIsNotInversed[i] ==
302 std::dynamic_pointer_cast<SketchPlugin_Arc>(myBaseFeatures[i])->isReversed())
303 aDir = aDir->multiplied(-1.0);
305 aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
308 // By default, the start point of fillet arc is connected to FeatureA,
309 // and the end point - to FeatureB. But when the angle between TangentDirA and
310 // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
311 double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A),
312 // where A and B - angles between corresponding tanget direction and the X axis
313 myIsReversed = cosBA > 0.0;
316 std::shared_ptr<GeomAPI_XY> aTmp = myTangentXY1;
317 myTangentXY1 = myTangentXY2;
323 bool SketchPlugin_Fillet::findFeaturesContainingFilletPoint(
324 std::shared_ptr<GeomDataAPI_Point2D> theFilletPoint)
326 // Obtain constraint coincidence for the fillet point.
327 FeaturePtr aConstraintCoincidence;
328 const std::set<AttributePtr>& aRefsList = theFilletPoint->owner()->data()->refsToMe();
329 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
330 anIt != aRefsList.cend();
332 std::shared_ptr<ModelAPI_Attribute> anAttr = (*anIt);
333 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
334 if(aFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
335 AttributeRefAttrPtr anAttrRefA =
336 aFeature->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_A());
337 AttributeRefAttrPtr anAttrRefB =
338 aFeature->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_B());
339 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
340 AttributePtr anAttrA = anAttrRefA->attr();
341 if(theFilletPoint == anAttrA) {
342 aConstraintCoincidence = aFeature;
346 if(anAttrRefB.get() && !anAttrRefB->isObject()) {
347 AttributePtr anAttrB = anAttrRefB->attr();
348 if(theFilletPoint == anAttrB) {
349 aConstraintCoincidence = aFeature;
356 if(!aConstraintCoincidence.get())
359 // Get coincide edges.
360 std::set<FeaturePtr> anEdgeFeatures = getCoincides(aConstraintCoincidence);
361 std::set<FeaturePtr>::iterator aLinesIt = anEdgeFeatures.begin();
362 for (int i = 0; aLinesIt != anEdgeFeatures.end() && i < 2; ++aLinesIt, ++i)
363 myBaseFeatures[i] = *aLinesIt;
365 return myBaseFeatures[0] && myBaseFeatures[1];
368 // ========= Auxiliary functions =================
369 void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
370 FeaturePtr theFeature, const std::string& theFeatureAttribute)
372 std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
373 theNewArc->attribute(theNewArcAttribute))->pnt();
374 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
375 theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
378 /// \brief Find intersections of lines shifted along normal direction
379 void possibleFilletCenterLineLine(
380 std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
381 std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
382 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
384 std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
385 std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
386 std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
387 double aDet = theDirA->cross(theDirB);
388 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
389 aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
390 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
391 aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
392 double aVX = aDirAT->xy()->dot(aPntA);
393 double aVY = aDirBT->xy()->dot(aPntB);
394 std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
395 (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
396 (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
397 theCenters.push_back(aPoint);
402 /// \brief Find intersections of line shifted along normal direction in both sides
403 /// and a circle with extended radius
404 void possibleFilletCenterLineArc(
405 std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
406 std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
407 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
409 std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
410 std::shared_ptr<GeomAPI_XY> aPnt;
411 double aDirNorm2 = theDirLine->dot(theDirLine);
413 double aDirX = theDirLine->x();
414 double aDirX2 = theDirLine->x() * theDirLine->x();
415 double aDirY2 = theDirLine->y() * theDirLine->y();
416 double aDirXY = theDirLine->x() * theDirLine->y();
417 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
418 aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
419 double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
420 double aCoeff2 = aCoeff * aCoeff;
421 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
422 aRad = theRadiusArc + aStepB * theRadius;
423 double aD = aRad * aRad * aDirNorm2 - aCoeff2;
426 double aDs = sqrt(aD);
427 double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
428 double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
429 double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
430 aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
431 double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
432 aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
434 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
435 theCenters.push_back(aPoint1);
436 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
437 theCenters.push_back(aPoint2);
442 /// \brief Find intersections of two circles with extended radii
443 void possibleFilletCenterArcArc(
444 std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
445 std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
446 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
448 std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
449 double aCenterDist2 = aCenterDir->dot(aCenterDir);
450 double aCenterDist = sqrt(aCenterDist2);
453 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
454 aRadA = theRadiusA + aStepA * theRadius;
455 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
456 aRadB = theRadiusB + aStepB * theRadius;
457 if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
458 continue; // there is no intersections
460 double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
461 double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
463 double x1 = theCenterA->x() +
464 (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
465 double y1 = theCenterA->y() +
466 (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
468 double x2 = theCenterA->x() +
469 (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
470 double y2 = theCenterA->y() +
471 (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
473 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
474 theCenters.push_back(aPoint1);
475 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
476 theCenters.push_back(aPoint2);
481 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
482 double theRadius, bool theNotInversed[2],
483 std::shared_ptr<GeomAPI_XY>& theCenter,
484 std::shared_ptr<GeomAPI_XY>& theTangentA,
485 std::shared_ptr<GeomAPI_XY>& theTangentB)
487 static const int aNbFeatures = 2;
488 FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
489 std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
490 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
492 for (int i = 0; i < aNbFeatures; i++) {
493 if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
494 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
495 aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
496 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
497 aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
498 } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
499 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
500 aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
501 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
502 aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
503 aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
504 aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
507 aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
508 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
509 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
510 aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
511 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
512 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
515 if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
516 theFeatureB->getKind() == SketchPlugin_Line::ID()) {
517 std::shared_ptr<GeomAPI_Dir2d> aDir[2];
518 std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
519 for (int i = 0; i < aNbFeatures; i++) {
520 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
521 aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
524 // get and filter possible centers
525 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
526 possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1],
527 theRadius, aSuspectCenters);
529 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
530 for (; anIt != aSuspectCenters.end(); anIt++) {
531 aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
532 theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
533 if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
534 continue; // incorrect position
535 aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
536 theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
537 if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
538 continue; // incorrect position
539 // the center is found, stop searching
543 } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
544 theFeatureB->getKind() == SketchPlugin_Line::ID()) ||
545 (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
546 theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
547 int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
548 double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
549 std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
550 new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
551 std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
552 new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
554 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
555 new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
556 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
557 new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
558 double anArcAngle = aEndArcDir->angle(aStartArcDir);
560 // get possible centers and filter them
561 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
562 possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd],
563 anArcRadius, theRadius, aSuspectCenters);
565 // the line is forward into the arc
566 double innerArc = aCenter[1-aLineInd]->decreased(aStart[aLineInd])->dot(aDirLine->xy());
567 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
568 // The possible centers are ranged by their positions.
569 // If the point is not satisfy one of criteria, the weight is decreased with penalty.
571 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
572 for (; anIt != aSuspectCenters.end(); anIt++) {
574 aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
575 aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
576 // Check the point is placed on the correct arc (penalty if false)
577 if (aCenter[1-aLineInd]->distance(*anIt) * innerArc > anArcRadius * innerArc)
579 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
580 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
581 double aCurAngle = aCurDir->angle(aStartArcDir);
582 if (anArcAngle < 0.0) aCurAngle *= -1.0;
583 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
585 if (aWeight > aBestWeight)
586 aBestWeight = aWeight;
587 else if (aWeight < aBestWeight ||
588 aStart[aLineInd]->distance(*anIt) >
589 aStart[aLineInd]->distance(theCenter)) // <-- take closer point
591 // the center is found, stop searching
593 anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
594 if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
595 theTangentA = aLineTgPoint;
596 theTangentB = anArcTgPoint;
598 theTangentA = anArcTgPoint;
599 theTangentB = aLineTgPoint;
603 } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
604 theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
605 double anArcRadius[aNbFeatures];
606 double anArcAngle[aNbFeatures];
607 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
608 for (int i = 0; i < aNbFeatures; i++) {
609 anArcRadius[i] = aStart[i]->distance(aCenter[i]);
610 aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
611 new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
612 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
613 new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
614 anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
617 // get and filter possible centers
618 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
619 possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1],
620 anArcRadius[1], theRadius, aSuspectCenters);
622 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
623 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
624 for (; anIt != aSuspectCenters.end(); anIt++) {
625 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
626 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
627 double aCurAngle = aCurDir->angle(aStartArcDir[0]);
628 if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
629 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
630 continue; // incorrect position
631 theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
633 aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
634 aCurAngle = aCurDir->angle(aStartArcDir[1]);
635 if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
636 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
637 continue; // incorrect position
638 theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
640 // the center is found, stop searching
647 void getPointOnEdge(const FeaturePtr theFeature,
648 const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
649 std::shared_ptr<GeomAPI_Pnt2d>& thePoint) {
650 if(theFeature->getKind() == SketchPlugin_Line::ID()) {
651 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
652 theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
653 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
654 theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
655 if(aPntStart->distance(theFilletPoint) > 1.e-7) {
656 aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
657 theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
658 aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
659 theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
662 new GeomAPI_Pnt2d(aPntStart->xy()->added(
663 aPntEnd->xy()->decreased( aPntStart->xy() )->multiplied(1.0 / 3.0) ) ) );
665 std::shared_ptr<GeomAPI_Pnt2d> aPntTemp;
666 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
667 theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
668 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
669 theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
670 if(theFeature->attribute(SketchPlugin_Arc::REVERSED_ID())) {
671 aPntTemp = aPntStart;
675 std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
676 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
677 std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
678 double aStartParameter(0), anEndParameter(0);
679 aCirc->parameter(aPntStart, paramTolerance, aStartParameter);
680 aCirc->parameter(aPntEnd, paramTolerance, anEndParameter);
681 if(aPntStart->distance(theFilletPoint) > tolerance) {
682 double aTmpParameter = aStartParameter;
683 aStartParameter = anEndParameter;
684 anEndParameter = aTmpParameter;
686 double aPntParameter = aStartParameter + (anEndParameter - aStartParameter) / 3.0;
687 aCirc->D0(aPntParameter, thePoint);
691 double getProjectionDistance(const FeaturePtr theFeature,
692 const std::shared_ptr<GeomAPI_Pnt2d> thePoint)
694 std::shared_ptr<GeomAPI_Pnt2d> aProjectPnt;
695 if(theFeature->getKind() == SketchPlugin_Line::ID()) {
696 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
697 theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
698 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
699 theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
700 std::shared_ptr<GeomAPI_Lin2d> aLin(new GeomAPI_Lin2d(aPntStart, aPntEnd));
701 aProjectPnt = aLin->project(thePoint);
703 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
704 theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
705 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
706 theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
707 std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
708 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
709 std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
710 aProjectPnt = aCirc->project(thePoint);
712 if(aProjectPnt.get()) {
713 return aProjectPnt->distance(thePoint);
718 std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence)
720 std::set<FeaturePtr> aCoincides;
722 std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt =
723 SketchPlugin_Tools::getCoincidencePoint(theConstraintCoincidence);
725 SketchPlugin_Tools::findCoincidences(theConstraintCoincidence,
726 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
728 SketchPlugin_Tools::findCoincidences(theConstraintCoincidence,
729 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
732 // Remove points from set of coincides.
733 std::set<FeaturePtr> aNewSetOfCoincides;
734 for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
735 std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
736 std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
737 if(aSketchEntity.get() && aSketchEntity->isCopy()) {
740 if((*anIt)->getKind() == SketchPlugin_Line::ID()) {
741 aNewSetOfCoincides.insert(*anIt);
742 } else if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
743 AttributePtr anAttrCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
744 std::shared_ptr<GeomDataAPI_Point2D> aPointCenter2D =
745 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrCenter);
746 if(aPointCenter2D.get() && aFilletPnt->isEqual(aPointCenter2D->pnt())) {
749 aNewSetOfCoincides.insert(*anIt);
752 aCoincides = aNewSetOfCoincides;
754 // If we still have more than two coincides remove auxilary entities from set of coincides.
755 if(aCoincides.size() > 2) {
756 aNewSetOfCoincides.clear();
757 for(std::set<FeaturePtr>::iterator
758 anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
759 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
760 aNewSetOfCoincides.insert(*anIt);
763 aCoincides = aNewSetOfCoincides;
769 std::set<FeaturePtr> findFeaturesToRemove(const FeaturePtr theFeature,
770 const AttributePtr theAttribute) {
771 std::set<FeaturePtr> aFeaturesToBeRemoved;
772 std::set<AttributePtr> aRefs = theFeature->data()->refsToMe();
773 std::list<ResultPtr> aResults = theFeature->results();
774 for(std::list<ResultPtr>::const_iterator aResultsIt = aResults.cbegin();
775 aResultsIt != aResults.cend();
777 ResultPtr aResult = *aResultsIt;
778 std::set<AttributePtr> aResultRefs = aResult->data()->refsToMe();
779 aRefs.insert(aResultRefs.begin(), aResultRefs.end());
781 for(std::set<AttributePtr>::const_iterator anIt = aRefs.cbegin();
782 anIt != aRefs.cend();
784 std::shared_ptr<ModelAPI_Attribute> anAttr = (*anIt);
785 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
786 if(aFeature->getKind() == SketchPlugin_Fillet::ID()) {
789 if(aFeature->getKind() == SketchPlugin_ConstraintLength::ID()
790 || aFeature->getKind() == SketchPlugin_ConstraintEqual::ID()) {
791 aFeaturesToBeRemoved.insert(aFeature);
793 std::list<AttributePtr> anAttrs =
794 aFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
795 for(std::list<AttributePtr>::const_iterator aRefAttrsIt = anAttrs.cbegin();
796 aRefAttrsIt != anAttrs.cend();
798 AttributeRefAttrPtr anAttrRefAttr =
799 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefAttrsIt);
800 if(anAttrRefAttr.get() && anAttrRefAttr->attr() == theAttribute) {
801 aFeaturesToBeRemoved.insert(aFeature);
806 return aFeaturesToBeRemoved;