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;
43 const double PI = 3.141592653589793238463;
45 /// \brief Attract specified point on theNewArc to the attribute of theFeature
46 static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
47 FeaturePtr theFeature, const std::string& theFeatureAttribute);
49 /// \brief Calculates center of fillet arc and coordinates of tangency points
50 static void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
51 double theRadius, bool theNotInversed[2],
52 std::shared_ptr<GeomAPI_XY>& theCenter,
53 std::shared_ptr<GeomAPI_XY>& theTangentA,
54 std::shared_ptr<GeomAPI_XY>& theTangentB);
56 /// Get point on 1/3 length of edge from fillet point
57 static void getPointOnEdge(const FeaturePtr theFeature,
58 const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
59 std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
61 /// Get distance from point to feature
62 static double getProjectionDistance(const FeaturePtr theFeature,
63 const std::shared_ptr<GeomAPI_Pnt2d> thePoint);
65 /// Get coincide edges for fillet
66 static std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence);
68 static std::set<FeaturePtr> findFeaturesToRemove(const FeaturePtr theFeature,
69 const AttributePtr theAttribute);
71 SketchPlugin_Fillet::SketchPlugin_Fillet()
72 : myFilletCreated(false)
76 void SketchPlugin_Fillet::initAttributes()
78 data()->addAttribute(FILLET_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
81 void SketchPlugin_Fillet::execute()
83 // Wait all constraints being created, then send update events
84 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
85 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
87 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
89 // Calculate Fillet parameters if does not yet
90 if (!myBaseFeatures[0] || !myBaseFeatures[1])
91 calculateFilletParameters();
93 // Create arc feature.
94 FeaturePtr aFilletArc = sketch()->addFeature(SketchPlugin_Arc::ID());
96 // Set arc attributes.
97 bool aWasBlocked = aFilletArc->data()->blockSendAttributeUpdated(true);
98 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
99 aFilletArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenterXY->x(),
101 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
102 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
103 aFilletArc->attribute(SketchPlugin_Arc::START_ID()));
104 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
105 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
106 aFilletArc->attribute(SketchPlugin_Arc::END_ID()));
107 if(aStartPoint->isInitialized() && aEndPoint->isInitialized()
108 && (aStartPoint->pnt()->xy()->distance(myTangentXY1) > tolerance
109 || aEndPoint->pnt()->xy()->distance(myTangentXY2) > tolerance)) {
110 std::dynamic_pointer_cast<SketchPlugin_Arc>(aFilletArc)->setReversed(false);
112 aStartPoint->setValue(myTangentXY1->x(), myTangentXY1->y());
113 aEndPoint->setValue(myTangentXY2->x(), myTangentXY2->y());
114 aFilletArc->data()->blockSendAttributeUpdated(aWasBlocked);
115 aFilletArc->execute();
117 // Delete features with refs to points of edges.
118 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint1;
119 int aFeatInd1 = myIsReversed ? 1 : 0;
120 int anAttrInd1 = (myIsReversed ? 2 : 0) + (myIsNotInversed[aFeatInd1] ? 0 : 1);
121 aStartPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
122 myBaseFeatures[aFeatInd1]->attribute(myFeatAttributes[anAttrInd1]));
123 std::set<FeaturePtr> aFeaturesToBeRemoved1 =
124 findFeaturesToRemove(myBaseFeatures[aFeatInd1], aStartPoint1);
126 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint2;
127 int aFeatInd2 = myIsReversed ? 0 : 1;
128 int anAttrInd2 = (myIsReversed ? 0 : 2) + (myIsNotInversed[aFeatInd2] ? 0 : 1);
129 aStartPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
130 myBaseFeatures[aFeatInd2]->attribute(myFeatAttributes[anAttrInd2]));
131 std::set<FeaturePtr> aFeaturesToBeRemoved2 =
132 findFeaturesToRemove(myBaseFeatures[aFeatInd2], aStartPoint2);
134 aFeaturesToBeRemoved1.insert(aFeaturesToBeRemoved2.begin(), aFeaturesToBeRemoved2.end());
135 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved1);
136 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
138 // Update fillet edges.
139 recalculateAttributes(aFilletArc, SketchPlugin_Arc::START_ID(),
140 myBaseFeatures[aFeatInd1], myFeatAttributes[anAttrInd1]);
141 recalculateAttributes(aFilletArc, SketchPlugin_Arc::END_ID(),
142 myBaseFeatures[aFeatInd2], myFeatAttributes[anAttrInd2]);
144 // Create coincidence features.
145 FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
146 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
147 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
148 aRefAttr->setAttr(aFilletArc->attribute(SketchPlugin_Arc::START_ID()));
149 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
150 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
151 aRefAttr->setAttr(myBaseFeatures[aFeatInd1]->attribute(myFeatAttributes[anAttrInd1]));
152 aConstraint->execute();
153 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
154 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
155 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
156 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
157 aRefAttr->setAttr(aFilletArc->attribute(SketchPlugin_Arc::END_ID()));
158 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
159 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
160 aRefAttr->setAttr(myBaseFeatures[aFeatInd2]->attribute(myFeatAttributes[anAttrInd2]));
161 aConstraint->execute();
162 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
164 // Create tangent features.
165 for (int i = 0; i < 2; i++) {
166 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
167 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
168 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
169 aRefAttr->setObject(aFilletArc->lastResult());
170 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
171 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
172 bool isArc = myBaseFeatures[i]->getKind() == SketchPlugin_Arc::ID();
173 aRefAttr->setObject(isArc ? myBaseFeatures[i]->lastResult() :
174 myBaseFeatures[i]->firstResult());
175 aConstraint->execute();
176 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
179 // Send events to update the sub-features by the solver.
180 if(isUpdateFlushed) {
181 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
184 myFilletCreated = true;
187 AISObjectPtr SketchPlugin_Fillet::getAISObject(AISObjectPtr thePrevious)
189 if(myFilletCreated) {
190 return AISObjectPtr();
193 SketchPlugin_Sketch* aSketch = sketch();
195 return AISObjectPtr();
198 if (!calculateFilletParameters())
199 return AISObjectPtr();
201 // Create arc for presentation.
202 std::shared_ptr<GeomAPI_Pnt> aCenterPnt(aSketch->to3D(myCenterXY->x(), myCenterXY->y()));
203 std::shared_ptr<GeomAPI_Pnt> aTangentPnt1(aSketch->to3D(myTangentXY1->x(),
205 std::shared_ptr<GeomAPI_Pnt> aTangentPnt2(aSketch->to3D(myTangentXY2->x(),
207 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
208 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
209 std::shared_ptr<GeomAPI_Shape> anArcShape =
210 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenterPnt, aTangentPnt1, aTangentPnt2, aNDir->dir());
212 AISObjectPtr anAISObject = thePrevious;
213 if(!anAISObject.get()) {
214 anAISObject = AISObjectPtr(new GeomAPI_AISObject);
216 anAISObject->createShape(anArcShape);
220 bool SketchPlugin_Fillet::calculateFilletParameters()
223 AttributeRefAttrPtr aPointRefAttr = refattr(FILLET_POINT_ID());
224 if (!aPointRefAttr->isInitialized() || aPointRefAttr->isObject())
226 std::shared_ptr<GeomDataAPI_Point2D> aFilletPoint2D =
227 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointRefAttr->attr());
228 if (!aFilletPoint2D.get())
231 if (!findFeaturesContainingFilletPoint(aFilletPoint2D)) {
232 setError("Error: Selected point does not have two suitable edges for fillet.");
236 // Getting points located at 1/3 of edge length from fillet point.
237 std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt2d = aFilletPoint2D->pnt();
238 std::shared_ptr<GeomAPI_Pnt2d> aPnt1, aPnt2;
239 getPointOnEdge(myBaseFeatures[0], aFilletPnt2d, aPnt1);
240 getPointOnEdge(myBaseFeatures[1], aFilletPnt2d, aPnt2);
242 /// Getting distances.
243 double aDistance1 = getProjectionDistance(myBaseFeatures[1], aPnt1);
244 double aDistance2 = getProjectionDistance(myBaseFeatures[0], aPnt2);
246 // Calculate radius value for fillet.
247 double aRadius = aDistance1 < aDistance2 ? aDistance1 / 2.0 : aDistance2 / 2.0;
249 // Calculate arc attributes.
250 static const int aNbFeatures = 2;
251 // First pair of points relate to first feature, second pair - to second.
252 std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2];
253 for (int i = 0; i < aNbFeatures; i++) {
254 std::string aStartAttr, aEndAttr;
255 if (myBaseFeatures[i]->getKind() == SketchPlugin_Line::ID()) {
256 aStartAttr = SketchPlugin_Line::START_ID();
257 aEndAttr = SketchPlugin_Line::END_ID();
258 } else if (myBaseFeatures[i]->getKind() == SketchPlugin_Arc::ID()) {
259 aStartAttr = SketchPlugin_Arc::START_ID();
260 aEndAttr = SketchPlugin_Arc::END_ID();
261 } else { // Wrong argument.
262 setError("Error: One of the points has wrong coincide feature");
265 myFeatAttributes[2*i] = aStartAttr;
266 aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
267 myBaseFeatures[i]->attribute(aStartAttr))->pnt();
268 myFeatAttributes[2*i+1] = aEndAttr;
269 aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
270 myBaseFeatures[i]->attribute(aEndAttr))->pnt();
272 for (int aFeatInd = 0; aFeatInd < aNbFeatures; aFeatInd++) {
273 for (int j = 0; j < 2; j++) // loop on start-end of each feature
274 if (aStartEndPnt[aFeatInd * aNbFeatures + j]->distance(aFilletPnt2d) < 1.e-10) {
275 myIsNotInversed[aFeatInd] = (j==0);
280 calculateFilletCenter(myBaseFeatures[0], myBaseFeatures[1], aRadius,
281 myIsNotInversed, myCenterXY, myTangentXY1, myTangentXY2);
283 // Tangent directions of the features in coincident point.
284 std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures];
285 for (int i = 0; i < aNbFeatures; i++) {
286 std::shared_ptr<GeomAPI_XY> aDir;
287 if (myBaseFeatures[i]->getKind() == SketchPlugin_Line::ID()) {
288 aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
289 if (!myIsNotInversed[i])
290 aDir = aDir->multiplied(-1.0);
291 } else if (myBaseFeatures[i]->getKind() == SketchPlugin_Arc::ID()) {
292 std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint =
293 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
294 myBaseFeatures[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
295 aDir = myIsNotInversed[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
296 aDir = aDir->decreased(aCenterPoint->xy());
298 double x = aDir->x();
299 double y = aDir->y();
302 if (myIsNotInversed[i] ==
303 std::dynamic_pointer_cast<SketchPlugin_Arc>(myBaseFeatures[i])->isReversed())
304 aDir = aDir->multiplied(-1.0);
306 aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
309 // By default, the start point of fillet arc is connected to FeatureA,
310 // and the end point - to FeatureB. But when the angle between TangentDirA and
311 // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
312 double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A),
313 // where A and B - angles between corresponding tanget direction and the X axis
314 myIsReversed = cosBA > 0.0;
317 std::shared_ptr<GeomAPI_XY> aTmp = myTangentXY1;
318 myTangentXY1 = myTangentXY2;
324 bool SketchPlugin_Fillet::findFeaturesContainingFilletPoint(
325 std::shared_ptr<GeomDataAPI_Point2D> theFilletPoint)
327 // Obtain constraint coincidence for the fillet point.
328 FeaturePtr aConstraintCoincidence;
329 const std::set<AttributePtr>& aRefsList = theFilletPoint->owner()->data()->refsToMe();
330 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
331 anIt != aRefsList.cend();
333 std::shared_ptr<ModelAPI_Attribute> anAttr = (*anIt);
334 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
335 if(aFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
336 AttributeRefAttrPtr anAttrRefA =
337 aFeature->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_A());
338 AttributeRefAttrPtr anAttrRefB =
339 aFeature->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_B());
340 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
341 AttributePtr anAttrA = anAttrRefA->attr();
342 if(theFilletPoint == anAttrA) {
343 aConstraintCoincidence = aFeature;
347 if(anAttrRefB.get() && !anAttrRefB->isObject()) {
348 AttributePtr anAttrB = anAttrRefB->attr();
349 if(theFilletPoint == anAttrB) {
350 aConstraintCoincidence = aFeature;
357 if(!aConstraintCoincidence.get())
360 // Get coincide edges.
361 std::set<FeaturePtr> anEdgeFeatures = getCoincides(aConstraintCoincidence);
362 std::set<FeaturePtr>::iterator aLinesIt = anEdgeFeatures.begin();
363 for (int i = 0; aLinesIt != anEdgeFeatures.end() && i < 2; ++aLinesIt, ++i)
364 myBaseFeatures[i] = *aLinesIt;
366 return myBaseFeatures[0] && myBaseFeatures[1];
369 // ========= Auxiliary functions =================
370 void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
371 FeaturePtr theFeature, const std::string& theFeatureAttribute)
373 std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
374 theNewArc->attribute(theNewArcAttribute))->pnt();
375 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
376 theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
379 /// \brief Find intersections of lines shifted along normal direction
380 void possibleFilletCenterLineLine(
381 std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
382 std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
383 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
385 std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
386 std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
387 std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
388 double aDet = theDirA->cross(theDirB);
389 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
390 aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
391 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
392 aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
393 double aVX = aDirAT->xy()->dot(aPntA);
394 double aVY = aDirBT->xy()->dot(aPntB);
395 std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
396 (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
397 (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
398 theCenters.push_back(aPoint);
403 /// \brief Find intersections of line shifted along normal direction in both sides
404 /// and a circle with extended radius
405 void possibleFilletCenterLineArc(
406 std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
407 std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
408 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
410 std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
411 std::shared_ptr<GeomAPI_XY> aPnt;
412 double aDirNorm2 = theDirLine->dot(theDirLine);
414 double aDirX = theDirLine->x();
415 double aDirX2 = theDirLine->x() * theDirLine->x();
416 double aDirY2 = theDirLine->y() * theDirLine->y();
417 double aDirXY = theDirLine->x() * theDirLine->y();
418 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
419 aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
420 double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
421 double aCoeff2 = aCoeff * aCoeff;
422 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
423 aRad = theRadiusArc + aStepB * theRadius;
424 double aD = aRad * aRad * aDirNorm2 - aCoeff2;
427 double aDs = sqrt(aD);
428 double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
429 double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
430 double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
431 aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
432 double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
433 aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
435 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
436 theCenters.push_back(aPoint1);
437 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
438 theCenters.push_back(aPoint2);
443 /// \brief Find intersections of two circles with extended radii
444 void possibleFilletCenterArcArc(
445 std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
446 std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
447 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
449 std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
450 double aCenterDist2 = aCenterDir->dot(aCenterDir);
451 double aCenterDist = sqrt(aCenterDist2);
454 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
455 aRadA = theRadiusA + aStepA * theRadius;
456 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
457 aRadB = theRadiusB + aStepB * theRadius;
458 if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
459 continue; // there is no intersections
461 double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
462 double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
464 double x1 = theCenterA->x() +
465 (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
466 double y1 = theCenterA->y() +
467 (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
469 double x2 = theCenterA->x() +
470 (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
471 double y2 = theCenterA->y() +
472 (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
474 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
475 theCenters.push_back(aPoint1);
476 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
477 theCenters.push_back(aPoint2);
482 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
483 double theRadius, bool theNotInversed[2],
484 std::shared_ptr<GeomAPI_XY>& theCenter,
485 std::shared_ptr<GeomAPI_XY>& theTangentA,
486 std::shared_ptr<GeomAPI_XY>& theTangentB)
488 static const int aNbFeatures = 2;
489 FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
490 std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
491 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
493 for (int i = 0; i < aNbFeatures; i++) {
494 if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
495 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
496 aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
497 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
498 aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
499 } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
500 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
501 aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
502 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
503 aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
504 aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
505 aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
508 aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
509 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
510 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
511 aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
512 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
513 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
516 if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
517 theFeatureB->getKind() == SketchPlugin_Line::ID()) {
518 std::shared_ptr<GeomAPI_Dir2d> aDir[2];
519 std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
520 for (int i = 0; i < aNbFeatures; i++) {
521 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
522 aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
525 // get and filter possible centers
526 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
527 possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1],
528 theRadius, aSuspectCenters);
530 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
531 for (; anIt != aSuspectCenters.end(); anIt++) {
532 aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
533 theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
534 if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
535 continue; // incorrect position
536 aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
537 theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
538 if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
539 continue; // incorrect position
540 // the center is found, stop searching
544 } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
545 theFeatureB->getKind() == SketchPlugin_Line::ID()) ||
546 (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
547 theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
548 int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
549 double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
550 std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
551 new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
552 std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
553 new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
555 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
556 new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
557 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
558 new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
559 double anArcAngle = aStartArcDir->angle(aEndArcDir);
560 if (anArcAngle < 0.0)
561 anArcAngle += 2.0 * PI;
563 // get possible centers and filter them
564 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
565 possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd],
566 anArcRadius, theRadius, aSuspectCenters);
568 // the line is forward into the arc
569 double innerArc = aCenter[1-aLineInd]->decreased(aStart[aLineInd])->dot(aDirLine->xy());
570 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
571 // The possible centers are ranged by their positions.
572 // If the point is not satisfy one of criteria, the weight is decreased with penalty.
574 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
575 for (; anIt != aSuspectCenters.end(); anIt++) {
577 aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
578 aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
579 // Check the point is placed on the correct arc (penalty if false)
580 if (aCenter[1-aLineInd]->distance(*anIt) * innerArc > anArcRadius * innerArc)
582 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
583 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
584 double aCurAngle = aStartArcDir->angle(aCurDir);
586 aCurAngle += 2.0 * PI;
587 if (aCurAngle < 0.0 || aCurAngle > anArcAngle)
589 if (aWeight > aBestWeight)
590 aBestWeight = aWeight;
591 else if (aWeight < aBestWeight ||
592 aStart[aLineInd]->distance(*anIt) >
593 aStart[aLineInd]->distance(theCenter)) // <-- take closer point
595 // the center is found, stop searching
597 anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
598 if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
599 theTangentA = aLineTgPoint;
600 theTangentB = anArcTgPoint;
602 theTangentA = anArcTgPoint;
603 theTangentB = aLineTgPoint;
607 } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
608 theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
609 double anArcRadius[aNbFeatures];
610 double anArcAngle[aNbFeatures];
611 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
612 for (int i = 0; i < aNbFeatures; i++) {
613 anArcRadius[i] = aStart[i]->distance(aCenter[i]);
614 aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
615 new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
616 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
617 new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
618 anArcAngle[i] = aStartArcDir[i]->angle(aEndArcDir);
619 if (anArcAngle[i] < 0.0)
620 anArcAngle[i] += 2.0 * PI;
623 // get and filter possible centers
624 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
625 possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1],
626 anArcRadius[1], theRadius, aSuspectCenters);
628 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
629 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
630 for (; anIt != aSuspectCenters.end(); anIt++) {
631 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
632 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
633 double aCurAngle = aStartArcDir[0]->angle(aCurDir);
635 aCurAngle += 2.0 * PI;
636 if (aCurAngle < 0.0 || aCurAngle > anArcAngle[0])
637 continue; // incorrect position
638 theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
640 aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
641 aCurAngle = aStartArcDir[1]->angle(aCurDir);
643 aCurAngle += 2.0 * PI;
644 if (aCurAngle < 0.0 || aCurAngle > anArcAngle[1])
645 continue; // incorrect position
646 theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
648 // the center is found, stop searching
655 void getPointOnEdge(const FeaturePtr theFeature,
656 const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
657 std::shared_ptr<GeomAPI_Pnt2d>& thePoint) {
658 if(theFeature->getKind() == SketchPlugin_Line::ID()) {
659 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
660 theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
661 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
662 theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
663 if(aPntStart->distance(theFilletPoint) > 1.e-7) {
664 aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
665 theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
666 aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
667 theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
670 new GeomAPI_Pnt2d(aPntStart->xy()->added(
671 aPntEnd->xy()->decreased( aPntStart->xy() )->multiplied(1.0 / 3.0) ) ) );
673 std::shared_ptr<GeomAPI_Pnt2d> aPntTemp;
674 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
675 theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
676 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
677 theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
678 if(theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value()) {
679 aPntTemp = aPntStart;
683 std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
684 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
685 std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
686 double aStartParameter(0), anEndParameter(0);
687 aCirc->parameter(aPntStart, paramTolerance, aStartParameter);
688 aCirc->parameter(aPntEnd, paramTolerance, anEndParameter);
689 if(aPntStart->distance(theFilletPoint) > tolerance) {
690 double aTmpParameter = aStartParameter;
691 aStartParameter = anEndParameter;
692 anEndParameter = aTmpParameter;
694 double aPntParameter = aStartParameter + (anEndParameter - aStartParameter) / 3.0;
695 aCirc->D0(aPntParameter, thePoint);
699 double getProjectionDistance(const FeaturePtr theFeature,
700 const std::shared_ptr<GeomAPI_Pnt2d> thePoint)
702 std::shared_ptr<GeomAPI_Pnt2d> aProjectPnt;
703 if(theFeature->getKind() == SketchPlugin_Line::ID()) {
704 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
705 theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
706 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
707 theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
708 std::shared_ptr<GeomAPI_Lin2d> aLin(new GeomAPI_Lin2d(aPntStart, aPntEnd));
709 aProjectPnt = aLin->project(thePoint);
711 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
712 theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
713 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
714 theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
715 std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
716 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
717 std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
718 aProjectPnt = aCirc->project(thePoint);
720 if(aProjectPnt.get()) {
721 return aProjectPnt->distance(thePoint);
726 std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence)
728 std::set<FeaturePtr> aCoincides;
730 std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt =
731 SketchPlugin_Tools::getCoincidencePoint(theConstraintCoincidence);
733 SketchPlugin_Tools::findCoincidences(theConstraintCoincidence,
734 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
736 SketchPlugin_Tools::findCoincidences(theConstraintCoincidence,
737 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
740 // Remove points from set of coincides.
741 std::set<FeaturePtr> aNewSetOfCoincides;
742 for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
743 std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
744 std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
745 if(aSketchEntity.get() && aSketchEntity->isCopy()) {
748 if((*anIt)->getKind() == SketchPlugin_Line::ID()) {
749 aNewSetOfCoincides.insert(*anIt);
750 } else if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
751 AttributePtr anAttrCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
752 std::shared_ptr<GeomDataAPI_Point2D> aPointCenter2D =
753 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrCenter);
754 if(aPointCenter2D.get() && aFilletPnt->isEqual(aPointCenter2D->pnt())) {
757 aNewSetOfCoincides.insert(*anIt);
760 aCoincides = aNewSetOfCoincides;
762 // If we still have more than two coincides remove auxilary entities from set of coincides.
763 if(aCoincides.size() > 2) {
764 aNewSetOfCoincides.clear();
765 for(std::set<FeaturePtr>::iterator
766 anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
767 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
768 aNewSetOfCoincides.insert(*anIt);
771 aCoincides = aNewSetOfCoincides;
777 std::set<FeaturePtr> findFeaturesToRemove(const FeaturePtr theFeature,
778 const AttributePtr theAttribute) {
779 std::set<FeaturePtr> aFeaturesToBeRemoved;
780 std::set<AttributePtr> aRefs = theFeature->data()->refsToMe();
781 std::list<ResultPtr> aResults = theFeature->results();
782 for(std::list<ResultPtr>::const_iterator aResultsIt = aResults.cbegin();
783 aResultsIt != aResults.cend();
785 ResultPtr aResult = *aResultsIt;
786 std::set<AttributePtr> aResultRefs = aResult->data()->refsToMe();
787 aRefs.insert(aResultRefs.begin(), aResultRefs.end());
789 for(std::set<AttributePtr>::const_iterator anIt = aRefs.cbegin();
790 anIt != aRefs.cend();
792 std::shared_ptr<ModelAPI_Attribute> anAttr = (*anIt);
793 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
794 if(aFeature->getKind() == SketchPlugin_Fillet::ID()) {
797 if(aFeature->getKind() == SketchPlugin_ConstraintLength::ID()
798 || aFeature->getKind() == SketchPlugin_ConstraintEqual::ID()) {
799 aFeaturesToBeRemoved.insert(aFeature);
801 std::list<AttributePtr> anAttrs =
802 aFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
803 for(std::list<AttributePtr>::const_iterator aRefAttrsIt = anAttrs.cbegin();
804 aRefAttrsIt != anAttrs.cend();
806 AttributeRefAttrPtr anAttrRefAttr =
807 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefAttrsIt);
808 if(anAttrRefAttr.get() && anAttrRefAttr->attr() == theAttribute) {
809 aFeaturesToBeRemoved.insert(aFeature);
814 return aFeaturesToBeRemoved;