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_ConstraintEqual.h"
28 #include "SketchPlugin_ConstraintCoincidence.h"
29 #include "SketchPlugin_ConstraintLength.h"
30 #include "SketchPlugin_ConstraintMiddle.h"
31 #include "SketchPlugin_ConstraintTangent.h"
32 #include "SketchPlugin_ConstraintRadius.h"
33 #include "SketchPlugin_Tools.h"
35 #include <ModelAPI_AttributeRefAttr.h>
36 #include <ModelAPI_Data.h>
37 #include <ModelAPI_Events.h>
38 #include <ModelAPI_Session.h>
39 #include <ModelAPI_Tools.h>
40 #include <ModelAPI_Validator.h>
42 #include <GeomAlgoAPI_Circ2dBuilder.h>
43 #include <GeomAlgoAPI_EdgeBuilder.h>
45 #include <GeomAPI_Circ2d.h>
46 #include <GeomAPI_Dir2d.h>
47 #include <GeomAPI_Lin2d.h>
48 #include <GeomAPI_Pnt2d.h>
49 #include <GeomAPI_XY.h>
51 #include <GeomDataAPI_Point2D.h>
53 #include <Events_Loop.h>
57 const double tolerance = 1.e-7;
58 const double paramTolerance = 1.e-4;
59 const double PI = 3.141592653589793238463;
61 /// \brief Attract specified point on theNewArc to the attribute of theFeature
62 static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
63 FeaturePtr theFeature, const std::string& theFeatureAttribute);
66 /// \brief Calculate radius of a fillet.
67 /// It should not be greater than 1/3 of shortest edge length.
68 static double calculateFilletRadius(FeaturePtr theFilletFeatures[2]);
70 /// \brief Calculates center of fillet arc and coordinates of tangency points
71 static void calculateFilletCenter(FeaturePtr theFilletFeatures[2],
72 double theFilletRadius,
73 const std::shared_ptr<GeomAPI_Ax3>& theSketchPlane,
74 std::shared_ptr<GeomAPI_XY>& theCenter,
75 std::shared_ptr<GeomAPI_XY>& theTangentA,
76 std::shared_ptr<GeomAPI_XY>& theTangentB);
78 static std::set<FeaturePtr> findFeaturesToRemove(const FeaturePtr theFeature,
79 const AttributePtr theAttribute);
81 SketchPlugin_Fillet::SketchPlugin_Fillet()
82 : myFilletCreated(false)
86 void SketchPlugin_Fillet::initAttributes()
88 data()->addAttribute(FILLET_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
91 void SketchPlugin_Fillet::execute()
93 // Wait all constraints being created, then send update events
94 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
95 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
97 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
99 // set flag here to avoid building Fillet presentation if "Redisplay" event appears
100 myFilletCreated = true;
102 // create feature for fillet arc
103 FeaturePtr aFilletArc = createFilletArc();
105 // Delete features with refs to points of edges.
106 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint1;
107 int aFeatInd1 = myIsReversed ? 1 : 0;
108 int anAttrInd1 = (myIsReversed ? 2 : 0) + (myIsNotInversed[aFeatInd1] ? 0 : 1);
109 aStartPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
110 myBaseFeatures[aFeatInd1]->attribute(myFeatAttributes[anAttrInd1]));
111 std::set<FeaturePtr> aFeaturesToBeRemoved1 =
112 findFeaturesToRemove(myBaseFeatures[aFeatInd1], aStartPoint1);
114 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint2;
115 int aFeatInd2 = myIsReversed ? 0 : 1;
116 int anAttrInd2 = (myIsReversed ? 0 : 2) + (myIsNotInversed[aFeatInd2] ? 0 : 1);
117 aStartPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
118 myBaseFeatures[aFeatInd2]->attribute(myFeatAttributes[anAttrInd2]));
119 std::set<FeaturePtr> aFeaturesToBeRemoved2 =
120 findFeaturesToRemove(myBaseFeatures[aFeatInd2], aStartPoint2);
122 aFeaturesToBeRemoved1.insert(aFeaturesToBeRemoved2.begin(), aFeaturesToBeRemoved2.end());
123 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved1);
124 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
126 // Update fillet edges.
127 recalculateAttributes(aFilletArc, SketchPlugin_Arc::START_ID(),
128 myBaseFeatures[aFeatInd1], myFeatAttributes[anAttrInd1]);
129 recalculateAttributes(aFilletArc, SketchPlugin_Arc::END_ID(),
130 myBaseFeatures[aFeatInd2], myFeatAttributes[anAttrInd2]);
132 FeaturePtr aConstraint;
134 // Create coincidence features.
135 aConstraint = SketchPlugin_Tools::createConstraint(sketch(),
136 SketchPlugin_ConstraintCoincidence::ID(),
137 aFilletArc->attribute(SketchPlugin_Arc::START_ID()),
138 myBaseFeatures[aFeatInd1]->attribute(myFeatAttributes[anAttrInd1]));
139 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
140 aConstraint = SketchPlugin_Tools::createConstraint(sketch(),
141 SketchPlugin_ConstraintCoincidence::ID(),
142 aFilletArc->attribute(SketchPlugin_Arc::END_ID()),
143 myBaseFeatures[aFeatInd2]->attribute(myFeatAttributes[anAttrInd2]));
144 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
146 // Create tangent features.
147 for (int i = 0; i < 2; i++) {
148 aConstraint = SketchPlugin_Tools::createConstraint(sketch(),
149 SketchPlugin_ConstraintTangent::ID(),
150 aFilletArc->lastResult(),
151 myBaseFeatures[i]->lastResult());
152 aConstraint->execute();
153 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
156 // Send events to update the sub-features by the solver.
157 if(isUpdateFlushed) {
158 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 // ========= Auxiliary functions =================
330 void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
331 FeaturePtr theFeature, const std::string& theFeatureAttribute)
333 std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
334 theNewArc->attribute(theNewArcAttribute))->pnt();
335 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
336 theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
339 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const AttributePtr& theAttribute)
341 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
342 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
344 aPoint = aPointAttr->pnt();
348 static std::shared_ptr<GeomAPI_Lin2d> toLine(const FeaturePtr& theFeature)
350 std::shared_ptr<GeomAPI_Lin2d> aLine;
351 if (theFeature->getKind() == SketchPlugin_Line::ID()) {
352 std::shared_ptr<GeomAPI_Pnt2d> aStart =
353 toPoint( theFeature->attribute(SketchPlugin_Line::START_ID()) );
354 std::shared_ptr<GeomAPI_Pnt2d> aEnd =
355 toPoint( theFeature->attribute(SketchPlugin_Line::END_ID()) );
356 aLine = std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStart, aEnd));
361 static std::shared_ptr<GeomAPI_Circ2d> toCircle(const FeaturePtr& theFeature)
363 std::shared_ptr<GeomAPI_Circ2d> aCircle;
364 if (theFeature->getKind() == SketchPlugin_Arc::ID()) {
365 std::shared_ptr<GeomAPI_Pnt2d> aCenter =
366 toPoint( theFeature->attribute(SketchPlugin_Arc::CENTER_ID()) );
367 std::shared_ptr<GeomAPI_Pnt2d> aStart =
368 toPoint( theFeature->attribute(SketchPlugin_Arc::START_ID()) );
369 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(aCenter, aStart));
375 void calculateFilletCenter(FeaturePtr theFilletFeatures[2],
376 double theFilletRadius,
377 const std::shared_ptr<GeomAPI_Ax3>& theSketchPlane,
378 std::shared_ptr<GeomAPI_XY>& theCenter,
379 std::shared_ptr<GeomAPI_XY>& theTangentA,
380 std::shared_ptr<GeomAPI_XY>& theTangentB)
382 GeomShapePtr aShapeA = theFilletFeatures[0]->lastResult()->shape();
383 GeomShapePtr aShapeB = theFilletFeatures[1]->lastResult()->shape();
385 GeomAlgoAPI_Circ2dBuilder aCircBuilder(theSketchPlane);
386 aCircBuilder.addTangentCurve(aShapeA);
387 aCircBuilder.addTangentCurve(aShapeB);
388 aCircBuilder.setRadius(theFilletRadius);
390 std::shared_ptr<GeomAPI_Circ2d> aFilletCircle = aCircBuilder.circle();
394 theCenter = aFilletCircle->center()->xy();
396 std::shared_ptr<GeomAPI_Pnt2d> aTgPoints[2];
397 for (int i = 0; i < 2; ++i) {
398 std::shared_ptr<GeomAPI_Circ2d> aCircle = toCircle(theFilletFeatures[i]);
400 aTgPoints[i] = aCircle->project(aFilletCircle->center());
402 std::shared_ptr<GeomAPI_Lin2d> aLine = toLine(theFilletFeatures[i]);
404 aTgPoints[i] = aLine->project(aFilletCircle->center());
407 theTangentA = aTgPoints[0]->xy();
408 theTangentB = aTgPoints[1]->xy();
411 double calculateFilletRadius(FeaturePtr theFilletFeatures[2])
413 double aLengths[2] = { 0, 0 };
414 for (int i = 0; i < 2; ++i) {
415 GeomShapePtr aShape = theFilletFeatures[i]->lastResult()->shape();
416 std::shared_ptr<GeomAPI_Edge> anEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(aShape);
418 aLengths[i] = anEdge->length();
420 return std::min(aLengths[0], aLengths[1]) / 6.0;
423 std::set<FeaturePtr> findFeaturesToRemove(const FeaturePtr theFeature,
424 const AttributePtr theAttribute) {
425 std::set<FeaturePtr> aFeaturesToBeRemoved;
426 std::set<AttributePtr> aRefs = theFeature->data()->refsToMe();
427 std::list<ResultPtr> aResults = theFeature->results();
428 for(std::list<ResultPtr>::const_iterator aResultsIt = aResults.cbegin();
429 aResultsIt != aResults.cend();
431 ResultPtr aResult = *aResultsIt;
432 std::set<AttributePtr> aResultRefs = aResult->data()->refsToMe();
433 aRefs.insert(aResultRefs.begin(), aResultRefs.end());
435 for(std::set<AttributePtr>::const_iterator anIt = aRefs.cbegin();
436 anIt != aRefs.cend();
438 std::shared_ptr<ModelAPI_Attribute> anAttr = (*anIt);
439 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
440 if(aFeature->getKind() == SketchPlugin_Fillet::ID()) {
443 if(aFeature->getKind() == SketchPlugin_ConstraintLength::ID()
444 || aFeature->getKind() == SketchPlugin_ConstraintEqual::ID()
445 || aFeature->getKind() == SketchPlugin_ConstraintMiddle::ID()) {
446 aFeaturesToBeRemoved.insert(aFeature);
448 std::list<AttributePtr> anAttrs =
449 aFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
450 for(std::list<AttributePtr>::const_iterator aRefAttrsIt = anAttrs.cbegin();
451 aRefAttrsIt != anAttrs.cend();
453 AttributeRefAttrPtr anAttrRefAttr =
454 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefAttrsIt);
455 if(anAttrRefAttr.get() && anAttrRefAttr->attr() == theAttribute) {
456 aFeaturesToBeRemoved.insert(aFeature);
461 return aFeaturesToBeRemoved;