1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_ConstraintFillet.cpp
4 // Created: 19 Mar 2015
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_ConstraintFillet.h"
9 #include <GeomAPI_Circ2d.h>
10 #include <GeomAPI_Dir2d.h>
11 #include <GeomAPI_Lin2d.h>
12 #include <GeomAPI_Pnt2d.h>
13 #include <GeomAPI_XY.h>
14 #include <GeomDataAPI_Point2D.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeRefList.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Events.h>
20 #include <ModelAPI_ResultConstruction.h>
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_Validator.h>
24 #include <SketchPlugin_Arc.h>
25 #include <SketchPlugin_Line.h>
26 #include <SketchPlugin_Sketch.h>
27 #include <SketchPlugin_ConstraintCoincidence.h>
28 #include <SketchPlugin_ConstraintTangent.h>
29 #include <SketchPlugin_ConstraintRadius.h>
30 #include <SketchPlugin_Point.h>
31 #include <SketchPlugin_Tools.h>
33 #include <SketcherPrs_Factory.h>
34 #include <SketcherPrs_Tools.h>
36 #include <Config_PropManager.h>
37 #include <Events_Loop.h>
39 #define _USE_MATH_DEFINES
42 static const std::string PREVIOUS_VALUE("FilletPreviousRadius");
44 const double tolerance = 1.e-7;
45 const double paramTolerance = 1.e-4;
47 /// \brief Attract specified point on theNewArc to the attribute of theFeature
48 static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
49 FeaturePtr theFeature, const std::string& theFeatureAttribute);
51 /// \brief Calculates center of fillet arc and coordinates of tangency points
52 static void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
53 double theRadius, bool theNotInversed[2],
54 std::shared_ptr<GeomAPI_XY>& theCenter,
55 std::shared_ptr<GeomAPI_XY>& theTangentA,
56 std::shared_ptr<GeomAPI_XY>& theTangentB);
58 /// Get point on 1/3 length of edge from fillet point
59 static void getPointOnEdge(const FeaturePtr theFeature,
60 const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
61 std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
63 /// Get distance from point to feature
64 static double getProjectionDistance(const FeaturePtr theFeature,
65 const std::shared_ptr<GeomAPI_Pnt2d> thePoint);
67 SketchPlugin_ConstraintFillet::SketchPlugin_ConstraintFillet()
71 void SketchPlugin_ConstraintFillet::initAttributes()
73 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
74 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
75 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
76 // This attribute used to store base edges
77 data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::typeId());
78 data()->addAttribute(PREVIOUS_VALUE, ModelAPI_AttributeDouble::typeId());
79 // initialize attribute not applicable for user
80 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
81 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_C());
82 data()->attribute(PREVIOUS_VALUE)->setInitialized();
83 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(PREVIOUS_VALUE))->setValue(0.0);
86 void SketchPlugin_ConstraintFillet::execute()
88 static const double aTol = 1.e-7;
90 // the viewer update should be blocked in order to avoid the temporaty fillet sub-features visualization
91 // before they are processed by the solver
92 //std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
93 // new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
94 //Events_Loop::loop()->send(aMsg);
96 std::shared_ptr<ModelAPI_Data> aData = data();
97 ResultConstructionPtr aRC;
98 // Check the base objects are initialized
99 double aFilletRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
100 aData->attribute(SketchPlugin_Constraint::VALUE()))->value();
101 AttributeRefAttrPtr aBaseA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
102 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
103 if (!aBaseA->isInitialized() || aBaseA->isObject()) {
104 setError("Bad vertex selected");
108 // Obtain fillet point
109 std::shared_ptr<GeomDataAPI_Point2D> aBasePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseA->attr());
111 setError("Bad vertex selected");
114 std::shared_ptr<GeomAPI_Pnt2d> aFilletPoint = aBasePoint->pnt();
116 // Check the fillet shapes is not initialized yet
117 AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
118 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
119 bool needNewObjects = aRefListOfFillet->size() == 0;
121 AttributeRefListPtr aRefListOfBaseLines = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
122 aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
124 // Obtain base features
125 FeaturePtr anOldFeatureA, anOldFeatureB;
126 std::list<ObjectPtr> anOldFeatList = aRefListOfBaseLines->list();
127 std::list<ObjectPtr>::iterator aFeatIt = anOldFeatList.begin();
128 anOldFeatureA = ModelAPI_Feature::feature(*aFeatIt++);
129 anOldFeatureB = ModelAPI_Feature::feature(*aFeatIt);
131 if(!anOldFeatureA.get() || !anOldFeatureB.get()) {
132 setError("One of the edges is empty");
136 FeaturePtr aNewFeatureA, aNewFeatureB, aNewArc;
137 if (needNewObjects) {
138 // Create list of objects composing a fillet
140 aNewFeatureA = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(anOldFeatureA, sketch());
142 aNewFeatureB = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(anOldFeatureB, sketch());
143 // create filleting arc (it will be attached to the list later)
144 aNewArc = sketch()->addFeature(SketchPlugin_Arc::ID());
146 // Obtain features from the list
147 std::list<ObjectPtr> aNewFeatList = aRefListOfFillet->list();
148 std::list<ObjectPtr>::iterator aFeatIt = aNewFeatList.begin();
149 aNewFeatureA = ModelAPI_Feature::feature(*aFeatIt++);
150 aNewFeatureB = ModelAPI_Feature::feature(*aFeatIt++);
151 aNewArc = ModelAPI_Feature::feature(*aFeatIt);
154 // Calculate arc attributes
155 static const int aNbFeatures = 2;
156 FeaturePtr aFeature[aNbFeatures] = {anOldFeatureA, anOldFeatureB};
157 FeaturePtr aNewFeature[aNbFeatures] = {aNewFeatureA, aNewFeatureB};
158 std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures]; // tangent directions of the features in coincident point
159 bool isStart[aNbFeatures]; // indicates which point the features share
160 std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2]; // first pair of points relate to first feature, second pair - to second
161 std::string aFeatAttributes[aNbFeatures * 2]; // attributes of features
162 for (int i = 0; i < aNbFeatures; i++) {
163 std::string aStartAttr, aEndAttr;
164 if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
165 aStartAttr = SketchPlugin_Line::START_ID();
166 aEndAttr = SketchPlugin_Line::END_ID();
167 } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
168 aStartAttr = SketchPlugin_Arc::START_ID();
169 aEndAttr = SketchPlugin_Arc::END_ID();
170 } else { // wrong argument
171 aRefListOfFillet->remove(aNewFeatureA);
172 aRefListOfFillet->remove(aNewFeatureB);
173 aRefListOfFillet->remove(aNewArc);
174 aRefListOfBaseLines->clear();
177 aFeatAttributes[2*i] = aStartAttr;
178 aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
179 aFeature[i]->attribute(aStartAttr))->pnt();
180 aFeatAttributes[2*i+1] = aEndAttr;
181 aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
182 aFeature[i]->attribute(aEndAttr))->pnt();
184 for (int aFeatInd = 0; aFeatInd < aNbFeatures; aFeatInd++) {
185 for (int j = 0; j < 2; j++) // loop on start-end of each feature
186 if (aStartEndPnt[aFeatInd * aNbFeatures + j]->distance(aFilletPoint) < 1.e-10) {
187 isStart[aFeatInd] = (j==0);
191 // tangent directions of the features
192 for (int i = 0; i < aNbFeatures; i++) {
193 std::shared_ptr<GeomAPI_XY> aDir;
194 if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
195 aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
197 aDir = aDir->multiplied(-1.0);
198 } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
199 std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint =
200 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
201 aNewFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
202 aDir = isStart[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
203 aDir = aDir->decreased(aCenterPoint->xy());
205 double x = aDir->x();
206 double y = aDir->y();
209 if (isStart[i] == std::dynamic_pointer_cast<SketchPlugin_Arc>(aFeature[i])->isReversed())
210 aDir = aDir->multiplied(-1.0);
212 aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
215 // Wait all constraints being created, then send update events
216 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
217 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
219 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
221 // By default, the start point of fillet arc is connected to FeatureA,
222 // and the end point - to FeatureB. But when the angle between TangentDirA and
223 // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
224 double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A), where A and B - angles between corresponding tanget direction and the X axis
225 bool isReversed = cosBA > 0.0;
227 // Calculate fillet arc parameters
228 std::shared_ptr<GeomAPI_XY> aCenter, aTangentPntA, aTangentPntB;
229 calculateFilletCenter(anOldFeatureA, anOldFeatureB, aFilletRadius, isStart, aCenter, aTangentPntA, aTangentPntB);
230 if(!aCenter.get() || !aTangentPntA.get() || !aTangentPntB.get()) {
231 setError("Can not create fillet with the specified parameters.");
235 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
236 aNewFeatureA->attribute(aFeatAttributes[isStart[0] ? 0 : 1]))->setValue(
237 aTangentPntA->x(), aTangentPntA->y());
238 aNewFeatureA->execute();
239 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
240 aNewFeatureB->attribute(aFeatAttributes[2 + (isStart[1] ? 0 : 1)]))->setValue(
241 aTangentPntB->x(), aTangentPntB->y());
242 aNewFeatureB->execute();
243 // update fillet arc: make the arc correct for sure, so, it is not needed to process the "attribute updated"
244 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
245 aNewArc->data()->blockSendAttributeUpdated(true);
246 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
247 aNewArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(
248 aCenter->x(), aCenter->y());
250 std::shared_ptr<GeomAPI_XY> aTmp = aTangentPntA;
251 aTangentPntA = aTangentPntB;
254 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
255 aNewArc->attribute(SketchPlugin_Arc::START_ID()));
256 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
257 aNewArc->attribute(SketchPlugin_Arc::END_ID()));
258 if (aStartPoint->isInitialized() && aEndPoint->isInitialized() &&
259 (aStartPoint->pnt()->xy()->distance(aTangentPntA) > aTol ||
260 aEndPoint->pnt()->xy()->distance(aTangentPntB) > aTol))
261 std::dynamic_pointer_cast<SketchPlugin_Arc>(aNewArc)->setReversed(false);
262 aStartPoint->setValue(aTangentPntA->x(), aTangentPntA->y());
263 aEndPoint->setValue(aTangentPntB->x(), aTangentPntB->y());
264 aNewArc->data()->blockSendAttributeUpdated(false);
267 if (needNewObjects) {
268 // attach new arc to the list
269 aRefListOfFillet->append(aNewFeatureA->lastResult());
270 aRefListOfFillet->append(aNewFeatureB->lastResult());
271 aRefListOfFillet->append(aNewArc->lastResult());
273 myProducedFeatures.push_back(aNewFeatureA);
274 myProducedFeatures.push_back(aNewFeatureB);
275 myProducedFeatures.push_back(aNewArc);
277 // Create list of additional constraints:
278 // 1. Coincidence of boundary points of features (copied lines/arcs) and fillet arc
280 FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
281 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
282 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
283 aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::START_ID()));
284 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
285 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
286 int aFeatInd = isReversed ? 1 : 0;
287 int anAttrInd = (isReversed ? 2 : 0) + (isStart[isReversed ? 1 : 0] ? 0 : 1);
288 aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
289 recalculateAttributes(aNewArc, SketchPlugin_Arc::START_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
290 aConstraint->execute();
291 myProducedFeatures.push_back(aConstraint);
292 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
294 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
295 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
296 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
297 aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::END_ID()));
298 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
299 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
300 aFeatInd = isReversed ? 0 : 1;
301 anAttrInd = (isReversed ? 0 : 2) + (isStart[isReversed ? 0 : 1] ? 0 : 1);
302 aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
303 recalculateAttributes(aNewArc, SketchPlugin_Arc::END_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
304 aConstraint->execute();
305 myProducedFeatures.push_back(aConstraint);
306 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
307 // 2. Fillet arc radius
308 //aConstraint = sketch()->addFeature(SketchPlugin_ConstraintRadius::ID());
309 //aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
310 // aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
311 //aRefAttr->setObject(aNewArc->lastResult());
312 //std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
313 // aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aFilletRadius);
314 //std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
315 // aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->setValue(
316 // isStart[0] ? aStartEndPnt[0] : aStartEndPnt[1]);
317 //aConstraint->execute();
318 //myProducedFeatures.push_back(aConstraint);
319 //ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
320 // 3. Tangency of fillet arc and features
321 for (int i = 0; i < aNbFeatures; i++) {
322 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
323 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
324 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
325 aRefAttr->setObject(aNewArc->lastResult());
326 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
327 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
328 bool isArc = aNewFeature[i]->getKind() == SketchPlugin_Arc::ID();
329 aRefAttr->setObject(isArc ? aNewFeature[i]->lastResult() : aNewFeature[i]->firstResult());
330 aConstraint->execute();
331 myProducedFeatures.push_back(aConstraint);
332 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
334 // 4. Coincidence of free boundaries of base and copied features
335 for (int i = 0; i < aNbFeatures; i++) {
336 anAttrInd = 2*i + (isStart[i] ? 1 : 0);
337 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
338 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
339 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
340 aRefAttr->setAttr(aFeature[i]->attribute(aFeatAttributes[anAttrInd]));
341 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
342 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
343 aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
344 myProducedFeatures.push_back(aConstraint);
346 // 4.1. Additional tangency constraints when the fillet is based on arcs.
347 // It is used to verify the created arc will be placed on a source.
348 for (int i = 0; i < aNbFeatures; ++i) {
349 if (aNewFeature[i]->getKind() != SketchPlugin_Arc::ID())
351 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
352 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
353 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
354 aRefAttr->setObject(aFeature[i]->lastResult());
355 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
356 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
357 aRefAttr->setObject(aNewFeature[i]->lastResult());
358 aConstraint->execute();
359 myProducedFeatures.push_back(aConstraint);
360 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
362 // 5. Tangent points should be placed on the base features
363 for (int i = 0; i < aNbFeatures; i++) {
364 anAttrInd = 2*i + (isStart[i] ? 0 : 1);
365 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
366 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
367 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
368 aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
369 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
370 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
371 aRefAttr->setObject(aFeature[i]->lastResult());
372 myProducedFeatures.push_back(aConstraint);
374 // make base features auxiliary
375 anOldFeatureA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
376 anOldFeatureB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
377 myBaseObjects.clear();
378 myBaseObjects.push_back(anOldFeatureA);
379 myBaseObjects.push_back(anOldFeatureB);
380 // exchange the naming IDs of newly created and old line that become auxiliary
381 sketch()->exchangeIDs(anOldFeatureA, aNewFeatureA);
382 sketch()->exchangeIDs(anOldFeatureB, aNewFeatureB);
384 // Update radius value
385 int aNbSubs = sketch()->numberOfSubs();
386 FeaturePtr aSubFeature;
387 for (int aSub = 0; aSub < aNbSubs; aSub++) {
388 aSubFeature = sketch()->subFeature(aSub);
389 if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
391 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
392 aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
393 if (!aRefAttr || !aRefAttr->isObject())
395 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
396 if (aFeature == aNewArc) {
397 AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
398 aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
399 aRadius->setValue(aFilletRadius);
405 // send events to update the sub-features by the solver
407 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
409 // the viewer update should be unblocked in order after the fillet features
410 // are processed by the solver
411 //aMsg = std::shared_ptr<Events_Message>(
412 // new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
413 //Events_Loop::loop()->send(aMsg);
416 void SketchPlugin_ConstraintFillet::attributeChanged(const std::string& theID)
418 if (theID == SketchPlugin_Constraint::ENTITY_A()) {
419 // clear the list of fillet entities
420 AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
421 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
422 aRefListOfFillet->clear();
424 // clear the list of base features
425 AttributeRefListPtr aRefListOfBaseLines = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
426 data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
427 aRefListOfBaseLines->clear();
429 // remove all produced objects and constraints
430 DocumentPtr aDoc = sketch()->document();
431 std::list<FeaturePtr>::iterator aCIt = myProducedFeatures.begin();
432 for (; aCIt != myProducedFeatures.end(); ++aCIt)
433 aDoc->removeFeature(*aCIt);
434 myProducedFeatures.clear();
436 // clear auxiliary flag on initial objects
437 for (aCIt = myBaseObjects.begin(); aCIt != myBaseObjects.end(); ++aCIt)
438 (*aCIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
439 myBaseObjects.clear();
441 // Obtain fillet point
442 AttributeRefAttrPtr aBaseA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
443 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
444 if(!aBaseA->isInitialized() || aBaseA->isObject()) {
447 AttributePtr anAttrBaseA = aBaseA->attr();
448 std::shared_ptr<GeomDataAPI_Point2D> aBasePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrBaseA);
452 std::shared_ptr<GeomAPI_Pnt2d> aFilletPoint = aBasePoint->pnt();
454 // Obtain conicident edges
455 const std::set<AttributePtr>& aRefsList = anAttrBaseA->owner()->data()->refsToMe();
456 std::set<AttributePtr>::const_iterator aIt;
457 FeaturePtr aCoincident;
458 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
459 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
460 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
461 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
462 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
463 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
464 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
465 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
466 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
467 AttributePtr anAttrA = anAttrRefA->attr();
468 if(anAttrBaseA == anAttrA) {
469 aCoincident = aConstrFeature;
473 if(anAttrRefA.get() && !anAttrRefB->isObject()) {
474 AttributePtr anAttrB = anAttrRefB->attr();
475 if(anAttrBaseA == anAttrB) {
476 aCoincident = aConstrFeature;
483 if(!aCoincident.get()) {
484 setError("No coincident edges at selected vertex");
488 std::set<FeaturePtr> aCoinsides;
489 SketchPlugin_Tools::findCoincidences(aCoincident,
490 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
492 SketchPlugin_Tools::findCoincidences(aCoincident,
493 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
497 std::set<FeaturePtr> aNewLines;
498 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
499 if((*anIt)->getKind() != SketchPlugin_Point::ID()) {
500 aNewLines.insert(*anIt);
503 aCoinsides = aNewLines;
505 // Remove auxilary lines
506 if(aCoinsides.size() > 2) {
508 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
509 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
510 aNewLines.insert(*anIt);
513 aCoinsides = aNewLines;
516 if(aCoinsides.size() != 2) {
517 setError("At selected vertex should be two coincident lines");
522 FeaturePtr anOldFeatureA, anOldFeatureB;
523 std::set<FeaturePtr>::iterator aLinesIt = aCoinsides.begin();
524 anOldFeatureA = *aLinesIt++;
525 anOldFeatureB = *aLinesIt;
526 aRefListOfBaseLines->append(anOldFeatureA);
527 aRefListOfBaseLines->append(anOldFeatureB);
529 // Getting points located at 1/3 of edge length from fillet point
530 std::shared_ptr<GeomAPI_Pnt2d> aPntA, aPntB;
531 getPointOnEdge(anOldFeatureA, aFilletPoint, aPntA);
532 getPointOnEdge(anOldFeatureB, aFilletPoint, aPntB);
534 /// Getting distances
536 double aDistanceA = getProjectionDistance(anOldFeatureB, aPntA);
537 double aDistanceB = getProjectionDistance(anOldFeatureA, aPntB);
538 aRadius = aDistanceA < aDistanceB ? aDistanceA / 2.0 : aDistanceB / 2.0;
540 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aRadius);
544 AISObjectPtr SketchPlugin_ConstraintFillet::getAISObject(AISObjectPtr thePrevious)
549 AISObjectPtr anAIS = thePrevious;
550 /// TODO: Equal constraint presentation should be put here
554 bool SketchPlugin_ConstraintFillet::isMacro() const
560 // ========= Auxiliary functions =================
561 void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
562 FeaturePtr theFeature, const std::string& theFeatureAttribute)
564 std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
565 theNewArc->attribute(theNewArcAttribute))->pnt();
566 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
567 theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
570 /// \brief Find intersections of lines shifted along normal direction
571 void possibleFilletCenterLineLine(
572 std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
573 std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
574 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
576 std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
577 std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
578 std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
579 double aDet = theDirA->cross(theDirB);
580 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
581 aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
582 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
583 aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
584 double aVX = aDirAT->xy()->dot(aPntA);
585 double aVY = aDirBT->xy()->dot(aPntB);
586 std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
587 (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
588 (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
589 theCenters.push_back(aPoint);
594 /// \brief Find intersections of line shifted along normal direction in both sides
595 /// and a circle with extended radius
596 void possibleFilletCenterLineArc(
597 std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
598 std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
599 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
601 std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
602 std::shared_ptr<GeomAPI_XY> aPnt;
603 double aDirNorm2 = theDirLine->dot(theDirLine);
605 double aDirX = theDirLine->x();
606 double aDirX2 = theDirLine->x() * theDirLine->x();
607 double aDirY2 = theDirLine->y() * theDirLine->y();
608 double aDirXY = theDirLine->x() * theDirLine->y();
609 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
610 aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
611 double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
612 double aCoeff2 = aCoeff * aCoeff;
613 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
614 aRad = theRadiusArc + aStepB * theRadius;
615 double aD = aRad * aRad * aDirNorm2 - aCoeff2;
618 double aDs = sqrt(aD);
619 double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
620 double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
621 double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
622 aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
623 double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
624 aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
626 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
627 theCenters.push_back(aPoint1);
628 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
629 theCenters.push_back(aPoint2);
634 /// \brief Find intersections of two circles with extended radii
635 void possibleFilletCenterArcArc(
636 std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
637 std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
638 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
640 std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
641 double aCenterDist2 = aCenterDir->dot(aCenterDir);
642 double aCenterDist = sqrt(aCenterDist2);
645 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
646 aRadA = theRadiusA + aStepA * theRadius;
647 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
648 aRadB = theRadiusB + aStepB * theRadius;
649 if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
650 continue; // there is no intersections
652 double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
653 double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
655 double x1 = theCenterA->x() + (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
656 double y1 = theCenterA->y() + (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
658 double x2 = theCenterA->x() + (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
659 double y2 = theCenterA->y() + (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
661 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
662 theCenters.push_back(aPoint1);
663 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
664 theCenters.push_back(aPoint2);
669 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
670 double theRadius, bool theNotInversed[2],
671 std::shared_ptr<GeomAPI_XY>& theCenter,
672 std::shared_ptr<GeomAPI_XY>& theTangentA,
673 std::shared_ptr<GeomAPI_XY>& theTangentB)
675 static const int aNbFeatures = 2;
676 FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
677 std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
678 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
680 for (int i = 0; i < aNbFeatures; i++) {
681 if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
682 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
683 aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
684 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
685 aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
686 } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
687 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
688 aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
689 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
690 aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
691 aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
692 aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
695 aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
696 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
697 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
698 aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
699 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
700 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
703 if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
704 theFeatureB->getKind() == SketchPlugin_Line::ID()) {
705 std::shared_ptr<GeomAPI_Dir2d> aDir[2];
706 std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
707 for (int i = 0; i < aNbFeatures; i++) {
708 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
709 aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
712 // get and filter possible centers
713 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
714 possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1], theRadius, aSuspectCenters);
716 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
717 for (; anIt != aSuspectCenters.end(); anIt++) {
718 aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
719 theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
720 if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
721 continue; // incorrect position
722 aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
723 theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
724 if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
725 continue; // incorrect position
726 // the center is found, stop searching
730 } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
731 theFeatureB->getKind() == SketchPlugin_Line::ID()) ||
732 (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
733 theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
734 int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
735 double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
736 std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
737 new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
738 std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
739 new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
741 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
742 new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
743 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
744 new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
745 double anArcAngle = aEndArcDir->angle(aStartArcDir);
747 // get possible centers and filter them
748 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
749 possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd], anArcRadius, theRadius, aSuspectCenters);
751 // the line is forward into the arc
752 double innerArc = aCenter[1-aLineInd]->decreased(aStart[aLineInd])->dot(aDirLine->xy());
753 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
754 // The possible centers are ranged by their positions.
755 // If the point is not satisfy one of criteria, the weight is decreased with penalty.
757 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
758 for (; anIt != aSuspectCenters.end(); anIt++) {
760 aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
761 aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
762 // Check the point is placed on the correct arc (penalty if false)
763 if (aCenter[1-aLineInd]->distance(*anIt) * innerArc > anArcRadius * innerArc)
765 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
766 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
767 double aCurAngle = aCurDir->angle(aStartArcDir);
768 if (anArcAngle < 0.0) aCurAngle *= -1.0;
769 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
771 if (aWeight > aBestWeight)
772 aBestWeight = aWeight;
773 else if (aWeight < aBestWeight ||
774 aStart[aLineInd]->distance(*anIt) >
775 aStart[aLineInd]->distance(theCenter)) // <-- take closer point
777 // the center is found, stop searching
779 anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
780 if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
781 theTangentA = aLineTgPoint;
782 theTangentB = anArcTgPoint;
784 theTangentA = anArcTgPoint;
785 theTangentB = aLineTgPoint;
789 } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
790 theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
791 double anArcRadius[aNbFeatures];
792 double anArcAngle[aNbFeatures];
793 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
794 for (int i = 0; i < aNbFeatures; i++) {
795 anArcRadius[i] = aStart[i]->distance(aCenter[i]);
796 aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
797 new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
798 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
799 new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
800 anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
803 // get and filter possible centers
804 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
805 possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1], anArcRadius[1], theRadius, aSuspectCenters);
807 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
808 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
809 for (; anIt != aSuspectCenters.end(); anIt++) {
810 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
811 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
812 double aCurAngle = aCurDir->angle(aStartArcDir[0]);
813 if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
814 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
815 continue; // incorrect position
816 theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
818 aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
819 aCurAngle = aCurDir->angle(aStartArcDir[1]);
820 if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
821 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
822 continue; // incorrect position
823 theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
825 // the center is found, stop searching
832 void getPointOnEdge(const FeaturePtr theFeature,
833 const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
834 std::shared_ptr<GeomAPI_Pnt2d>& thePoint) {
835 if(theFeature->getKind() == SketchPlugin_Line::ID()) {
836 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
837 theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
838 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
839 theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
840 if(aPntStart->distance(theFilletPoint) > 1.e-7) {
841 aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
842 theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
843 aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
844 theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
846 thePoint.reset( new GeomAPI_Pnt2d(aPntStart->xy()->added( aPntEnd->xy()->decreased( aPntStart->xy() )->multiplied(1.0 / 3.0) ) ) );
848 std::shared_ptr<GeomAPI_Pnt2d> aPntTemp;
849 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
850 theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
851 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
852 theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
853 if(theFeature->attribute(SketchPlugin_Arc::INVERSED_ID())) {
854 aPntTemp = aPntStart;
858 std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
859 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
860 std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
861 double aStartParameter(0), anEndParameter(0);
862 aCirc->parameter(aPntStart, paramTolerance, aStartParameter);
863 aCirc->parameter(aPntEnd, paramTolerance, anEndParameter);
864 if(aPntStart->distance(theFilletPoint) > tolerance) {
865 double aTmpParameter = aStartParameter;
866 aStartParameter = anEndParameter;
867 anEndParameter = aTmpParameter;
869 double aPntParameter = aStartParameter + (anEndParameter - aStartParameter) / 3.0;
870 aCirc->D0(aPntParameter, thePoint);
874 double getProjectionDistance(const FeaturePtr theFeature,
875 const std::shared_ptr<GeomAPI_Pnt2d> thePoint)
877 std::shared_ptr<GeomAPI_Pnt2d> aProjectPnt;
878 if(theFeature->getKind() == SketchPlugin_Line::ID()) {
879 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
880 theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
881 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
882 theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
883 std::shared_ptr<GeomAPI_Lin2d> aLin(new GeomAPI_Lin2d(aPntStart, aPntEnd));
884 aProjectPnt = aLin->project(thePoint);
886 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
887 theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
888 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
889 theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
890 std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
891 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
892 std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
893 aProjectPnt = aCirc->project(thePoint);
895 if(aProjectPnt.get()) {
896 return aProjectPnt->distance(thePoint);