1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_ConstraintSplit.cpp
4 // Created: 17 Jul 2016
5 // Author: Natalia ERMOLAEVA
7 #include "SketchPlugin_ConstraintSplit.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_AttributeReference.h>
17 #include <ModelAPI_AttributeString.h>
18 //#include <ModelAPI_AttributeRefList.h>
19 #include <ModelAPI_AttributeRefAttr.h>
21 #include <SketchPlugin_Line.h>
22 #include <SketchPlugin_Arc.h>
24 //#include <ModelAPI_Data.h>
25 //#include <ModelAPI_Events.h>
26 //#include <ModelAPI_Session.h>
27 //#include <ModelAPI_Validator.h>
29 //#include <SketchPlugin_Arc.h>
30 //#include <SketchPlugin_Line.h>
31 //#include <SketchPlugin_Point.h>
32 //#include <SketchPlugin_Sketch.h>
33 //#include <SketchPlugin_ConstraintCoincidence.h>
34 //#include <SketchPlugin_ConstraintTangent.h>
35 //#include <SketchPlugin_ConstraintRadius.h>
36 //#include <SketchPlugin_Tools.h>
38 //#include <Events_Loop.h>
42 //const double tolerance = 1.e-7;
43 //const double paramTolerance = 1.e-4;
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 SketchPlugin_ConstraintSplit::SketchPlugin_ConstraintSplit()
69 //: myListOfPointsChangedInCode(false),
70 // myRadiusChangedByUser(false),
71 // myRadiusChangedInCode(false),
72 // myRadiusInitialized(false)
76 void SketchPlugin_ConstraintSplit::initAttributes()
78 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeReference::typeId());
79 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
80 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
82 //data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
83 //data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttrList::typeId());
86 void SketchPlugin_ConstraintSplit::execute()
88 std::shared_ptr<ModelAPI_Data> aData = data();
90 // Check the base objects are initialized.
91 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
92 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
93 if(!aBaseObjectAttr->isInitialized()) {
94 setError("Error: Base object is not initialized.");
97 AttributePoint2DPtr aFirstPointAttr = getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
98 AttributePoint2DPtr aLastPointAttr = getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
99 if (!aFirstPointAttr.get() || !aFirstPointAttr->isInitialized() ||
100 !aLastPointAttr.get() || !aLastPointAttr->isInitialized()) {
101 setError("Error: Sub-shape is not initialized.");
105 AttributePoint2DPtr aStartPointAttr = getFeaturePoint(true);
106 AttributePoint2DPtr anEndPointAttr = getFeaturePoint(false);
107 if (!aStartPointAttr.get() && !anEndPointAttr.get()) {
108 setError("Error: Circle is not processed."); /// TODO
112 /// if first point is closer to last point, wrap first and last values
113 if (aStartPointAttr->pnt()->distance(aFirstPointAttr->pnt()) >
114 anEndPointAttr->pnt()->distance(aLastPointAttr->pnt())) {
115 AttributePoint2DPtr aTmpPoint = aFirstPointAttr;
116 aFirstPointAttr = aLastPointAttr;
117 aLastPointAttr = aTmpPoint;
119 FeaturePtr aSplitFeature = createFeature(aFirstPointAttr, aLastPointAttr);
121 std::set<FeaturePtr> aLeftFeatures;
122 if (!aStartPointAttr->pnt()->isEqual(aFirstPointAttr->pnt()))
123 aLeftFeatures.insert(createFeature(aStartPointAttr, aFirstPointAttr));
124 if (!aLastPointAttr->pnt()->isEqual(anEndPointAttr->pnt()))
125 aLeftFeatures.insert(createFeature(aLastPointAttr, anEndPointAttr));
128 // Get fillet radius.
129 double aFilletRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
130 aData->attribute(SketchPlugin_Constraint::VALUE()))->value();
132 // Wait all constraints being created, then send update events
133 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
134 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
136 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
138 for(std::set<AttributePtr>::iterator aPointsIter = myNewPoints.begin();
139 aPointsIter != myNewPoints.end();
141 AttributePtr aPointAttr = *aPointsIter;
142 std::shared_ptr<GeomDataAPI_Point2D> aFilletPoint2d = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttr);
143 if(!aFilletPoint2d.get()) {
144 setError("Error: One of the selected points is empty.");
147 std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt2d = aFilletPoint2d->pnt();
149 // Obtain base lines for fillet.
150 bool anIsNeedNewObjects = true;
151 FilletFeatures aFilletFeatures;
152 std::map<AttributePtr, FilletFeatures>::iterator aPrevPointsIter = myPointFeaturesMap.find(aPointAttr);
153 if(aPrevPointsIter != myPointFeaturesMap.end()) {
154 anIsNeedNewObjects = false;
155 aFilletFeatures = aPrevPointsIter->second;
157 FeaturePtr aBaseEdgeA, aBaseEdgeB;
158 if(!anIsNeedNewObjects) {
159 aBaseEdgeA = aFilletFeatures.baseEdgesState.front().first;
160 aBaseEdgeB = aFilletFeatures.baseEdgesState.back().first;
162 // Obtain constraint coincidence for the fillet point.
163 FeaturePtr aConstraintCoincidence;
164 const std::set<AttributePtr>& aRefsList = aFilletPoint2d->owner()->data()->refsToMe();
165 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin(); anIt != aRefsList.cend(); ++anIt) {
166 std::shared_ptr<ModelAPI_Attribute> anAttr = (*anIt);
167 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
168 if(aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
169 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
170 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
171 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
172 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
173 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
174 AttributePtr anAttrA = anAttrRefA->attr();
175 if(aFilletPoint2d == anAttrA) {
176 aConstraintCoincidence = aConstrFeature;
180 if(anAttrRefB.get() && !anAttrRefB->isObject()) {
181 AttributePtr anAttrB = anAttrRefB->attr();
182 if(aFilletPoint2d == anAttrB) {
183 aConstraintCoincidence = aConstrFeature;
190 if(!aConstraintCoincidence.get()) {
191 setError("Error: No coincident edges at one of the selected points.");
195 // Get coincide edges.
196 std::set<FeaturePtr> aCoincides = getCoincides(aConstraintCoincidence);
197 if(aCoincides.size() != 2) {
198 setError("Error: One of the selected points does not have two suitable edges for fillet.");
202 std::set<FeaturePtr>::iterator aLinesIt = aCoincides.begin();
203 aBaseEdgeA = *aLinesIt++;
204 aBaseEdgeB = *aLinesIt;
206 std::pair<FeaturePtr, bool> aBasePairA = std::make_pair(aBaseEdgeA, aBaseEdgeA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value());
207 std::pair<FeaturePtr, bool> aBasePairB = std::make_pair(aBaseEdgeB, aBaseEdgeB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value());
208 aFilletFeatures.baseEdgesState.push_back(aBasePairA);
209 aFilletFeatures.baseEdgesState.push_back(aBasePairB);
212 if(!aBaseEdgeA.get() || !aBaseEdgeB.get()) {
213 setError("Error: One of the base edges is empty.");
217 // Create new edges and arc if needed.
218 FeaturePtr aResultEdgeA, aResultEdgeB, aResultArc;
219 if(!anIsNeedNewObjects) {
220 // Obtain features from the list.
221 std::list<FeaturePtr>::iterator aResultEdgesIt = aFilletFeatures.resultEdges.begin();
222 aResultEdgeA = *aResultEdgesIt++;
223 aResultEdgeB = *aResultEdgesIt++;
224 aResultArc = *aResultEdgesIt;
226 // Copy edges and create arc.
227 aResultEdgeA = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aBaseEdgeA, sketch());
228 aResultEdgeA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
229 aResultEdgeB = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aBaseEdgeB, sketch());
230 aResultEdgeB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
231 aResultArc = sketch()->addFeature(SketchPlugin_Arc::ID());
233 aFilletFeatures.resultEdges.push_back(aResultEdgeA);
234 aFilletFeatures.resultEdges.push_back(aResultEdgeB);
235 aFilletFeatures.resultEdges.push_back(aResultArc);
238 // Calculate arc attributes
239 static const int aNbFeatures = 2;
240 FeaturePtr aBaseFeatures[aNbFeatures] = {aBaseEdgeA, aBaseEdgeB};
241 FeaturePtr aResultFeatures[aNbFeatures] = {aResultEdgeA, aResultEdgeB};
242 std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures]; // tangent directions of the features in coincident point
243 bool isStart[aNbFeatures]; // indicates which point the features share
244 std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2]; // first pair of points relate to first feature, second pair - to second
245 std::string aFeatAttributes[aNbFeatures * 2]; // attributes of features
246 for (int i = 0; i < aNbFeatures; i++) {
247 std::string aStartAttr, aEndAttr;
248 if (aResultFeatures[i]->getKind() == SketchPlugin_Line::ID()) {
249 aStartAttr = SketchPlugin_Line::START_ID();
250 aEndAttr = SketchPlugin_Line::END_ID();
251 } else if (aResultFeatures[i]->getKind() == SketchPlugin_Arc::ID()) {
252 aStartAttr = SketchPlugin_Arc::START_ID();
253 aEndAttr = SketchPlugin_Arc::END_ID();
254 } else { // wrong argument
255 setError("Error: One of the points has wrong coincide feature");
258 aFeatAttributes[2*i] = aStartAttr;
259 aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
260 aBaseFeatures[i]->attribute(aStartAttr))->pnt();
261 aFeatAttributes[2*i+1] = aEndAttr;
262 aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
263 aBaseFeatures[i]->attribute(aEndAttr))->pnt();
265 for (int aFeatInd = 0; aFeatInd < aNbFeatures; aFeatInd++) {
266 for (int j = 0; j < 2; j++) // loop on start-end of each feature
267 if (aStartEndPnt[aFeatInd * aNbFeatures + j]->distance(aFilletPnt2d) < 1.e-10) {
268 isStart[aFeatInd] = (j==0);
272 // tangent directions of the features
273 for (int i = 0; i < aNbFeatures; i++) {
274 std::shared_ptr<GeomAPI_XY> aDir;
275 if (aResultFeatures[i]->getKind() == SketchPlugin_Line::ID()) {
276 aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
278 aDir = aDir->multiplied(-1.0);
279 } else if (aResultFeatures[i]->getKind() == SketchPlugin_Arc::ID()) {
280 std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
281 aResultFeatures[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
282 aDir = isStart[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
283 aDir = aDir->decreased(aCenterPoint->xy());
285 double x = aDir->x();
286 double y = aDir->y();
289 if (isStart[i] == std::dynamic_pointer_cast<SketchPlugin_Arc>(aBaseFeatures[i])->isReversed())
290 aDir = aDir->multiplied(-1.0);
292 aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
295 // By default, the start point of fillet arc is connected to FeatureA,
296 // and the end point - to FeatureB. But when the angle between TangentDirA and
297 // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
298 double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A), where A and B - angles between corresponding tanget direction and the X axis
299 bool isReversed = cosBA > 0.0;
301 // Calculate fillet arc parameters
302 std::shared_ptr<GeomAPI_XY> aCenter, aTangentPntA, aTangentPntB;
303 calculateFilletCenter(aBaseEdgeA, aBaseEdgeB, aFilletRadius, isStart, aCenter, aTangentPntA, aTangentPntB);
304 if(!aCenter.get() || !aTangentPntA.get() || !aTangentPntB.get()) {
305 setError("Can not create fillet with the specified parameters.");
309 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
310 aResultEdgeA->attribute(aFeatAttributes[isStart[0] ? 0 : 1]))->setValue(aTangentPntA->x(), aTangentPntA->y());
311 aResultEdgeA->execute();
312 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
313 aResultEdgeB->attribute(aFeatAttributes[2 + (isStart[1] ? 0 : 1)]))->setValue(aTangentPntB->x(), aTangentPntB->y());
314 aResultEdgeB->execute();
315 // update fillet arc: make the arc correct for sure, so, it is not needed to process the "attribute updated"
316 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
317 aResultArc->data()->blockSendAttributeUpdated(true);
318 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
319 aResultArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(aCenter->x(), aCenter->y());
321 std::shared_ptr<GeomAPI_XY> aTmp = aTangentPntA;
322 aTangentPntA = aTangentPntB;
325 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
326 aResultArc->attribute(SketchPlugin_Arc::START_ID()));
327 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
328 aResultArc->attribute(SketchPlugin_Arc::END_ID()));
329 if(aStartPoint->isInitialized() && aEndPoint->isInitialized() &&
330 (aStartPoint->pnt()->xy()->distance(aTangentPntA) > tolerance ||
331 aEndPoint->pnt()->xy()->distance(aTangentPntB) > tolerance)) {
332 std::dynamic_pointer_cast<SketchPlugin_Arc>(aResultArc)->setReversed(false);
334 aStartPoint->setValue(aTangentPntA->x(), aTangentPntA->y());
335 aEndPoint->setValue(aTangentPntB->x(), aTangentPntB->y());
336 aResultArc->data()->blockSendAttributeUpdated(false);
337 aResultArc->execute();
339 if(anIsNeedNewObjects) {
340 // Create list of additional constraints:
341 // 1. Coincidence of boundary points of features (copied lines/arcs) and fillet arc
343 FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
344 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
345 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
346 aRefAttr->setAttr(aResultArc->attribute(SketchPlugin_Arc::START_ID()));
347 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
348 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
349 int aFeatInd = isReversed ? 1 : 0;
350 int anAttrInd = (isReversed ? 2 : 0) + (isStart[isReversed ? 1 : 0] ? 0 : 1);
351 aRefAttr->setAttr(aResultFeatures[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
352 recalculateAttributes(aResultArc, SketchPlugin_Arc::START_ID(), aResultFeatures[aFeatInd], aFeatAttributes[anAttrInd]);
353 aConstraint->execute();
354 aFilletFeatures.resultConstraints.push_back(aConstraint);
355 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
357 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
358 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
359 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
360 aRefAttr->setAttr(aResultArc->attribute(SketchPlugin_Arc::END_ID()));
361 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
362 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
363 aFeatInd = isReversed ? 0 : 1;
364 anAttrInd = (isReversed ? 0 : 2) + (isStart[isReversed ? 0 : 1] ? 0 : 1);
365 aRefAttr->setAttr(aResultFeatures[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
366 recalculateAttributes(aResultArc, SketchPlugin_Arc::END_ID(), aResultFeatures[aFeatInd], aFeatAttributes[anAttrInd]);
367 aConstraint->execute();
368 aFilletFeatures.resultConstraints.push_back(aConstraint);
369 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
370 // 2. Fillet arc radius
371 //aConstraint = sketch()->addFeature(SketchPlugin_ConstraintRadius::ID());
372 //aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
373 // aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
374 //aRefAttr->setObject(aNewArc->lastResult());
375 //std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
376 // aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aFilletRadius);
377 //std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
378 // aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->setValue(
379 // isStart[0] ? aStartEndPnt[0] : aStartEndPnt[1]);
380 //aConstraint->execute();
381 //myProducedFeatures.push_back(aConstraint);
382 //ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
383 // 3. Tangency of fillet arc and features
384 for (int i = 0; i < aNbFeatures; i++) {
385 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
386 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
387 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
388 aRefAttr->setObject(aResultArc->lastResult());
389 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
390 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
391 bool isArc = aResultFeatures[i]->getKind() == SketchPlugin_Arc::ID();
392 aRefAttr->setObject(isArc ? aResultFeatures[i]->lastResult() : aResultFeatures[i]->firstResult());
393 aConstraint->execute();
394 aFilletFeatures.resultConstraints.push_back(aConstraint);
395 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
397 // 4. Coincidence of free boundaries of base and copied features
398 for (int i = 0; i < aNbFeatures; i++) {
399 anAttrInd = 2*i + (isStart[i] ? 1 : 0);
400 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
401 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
402 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
403 aRefAttr->setAttr(aBaseFeatures[i]->attribute(aFeatAttributes[anAttrInd]));
404 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
405 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
406 aRefAttr->setAttr(aResultFeatures[i]->attribute(aFeatAttributes[anAttrInd]));
407 aFilletFeatures.resultConstraints.push_back(aConstraint);
409 // 4.1. Additional tangency constraints when the fillet is based on arcs.
410 // It is used to verify the created arc will be placed on a source.
411 for (int i = 0; i < aNbFeatures; ++i) {
412 if (aResultFeatures[i]->getKind() != SketchPlugin_Arc::ID())
414 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
415 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
416 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
417 aRefAttr->setObject(aBaseFeatures[i]->lastResult());
418 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
419 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
420 aRefAttr->setObject(aResultFeatures[i]->lastResult());
421 aConstraint->execute();
422 aFilletFeatures.resultConstraints.push_back(aConstraint);
423 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
425 // 5. Tangent points should be placed on the base features
426 for (int i = 0; i < aNbFeatures; i++) {
427 anAttrInd = 2*i + (isStart[i] ? 0 : 1);
428 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
429 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
430 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
431 aRefAttr->setAttr(aResultFeatures[i]->attribute(aFeatAttributes[anAttrInd]));
432 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
433 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
434 aRefAttr->setObject(aBaseFeatures[i]->lastResult());
435 aFilletFeatures.resultConstraints.push_back(aConstraint);
437 // make base features auxiliary
438 aBaseEdgeA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
439 aBaseEdgeB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
441 // exchange the naming IDs of newly created and old line that become auxiliary
442 sketch()->exchangeIDs(aBaseEdgeA, aResultEdgeA);
443 sketch()->exchangeIDs(aBaseEdgeB, aResultEdgeB);
445 // store point and features in the map.
446 myPointFeaturesMap[aPointAttr] = aFilletFeatures;
448 // Update radius value
449 int aNbSubs = sketch()->numberOfSubs();
450 FeaturePtr aSubFeature;
451 for (int aSub = 0; aSub < aNbSubs; aSub++) {
452 aSubFeature = sketch()->subFeature(aSub);
453 if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
455 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
456 aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
457 if (!aRefAttr || !aRefAttr->isObject())
459 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
460 if (aFeature == aResultArc) {
461 AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
462 aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
463 aRadius->setValue(aFilletRadius);
470 // Send events to update the sub-features by the solver.
471 if(isUpdateFlushed) {
472 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
477 void SketchPlugin_ConstraintSplit::attributeChanged(const std::string& theID)
479 /* if(theID == SketchPlugin_Constraint::ENTITY_A()) {
480 if(myListOfPointsChangedInCode) {
487 // Clear list of new points.
490 // Get list of points for fillets and current radius.
491 AttributeRefAttrListPtr aRefListOfFilletPoints = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(
492 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
493 AttributeDoublePtr aRadiusAttribute = real(VALUE());
494 int aListSize = aRefListOfFilletPoints->size();
495 if(aListSize == 0 && !myRadiusChangedByUser) {
496 // If list is empty reset radius to zero (if it was not changed by user).
497 myRadiusChangedInCode = true;
498 aRadiusAttribute->setValue(0);
499 myRadiusChangedInCode = false;
503 // Iterate over points to get base lines an calculate radius for fillets.
504 double aMinimumRadius = 0;
505 std::list<std::pair<ObjectPtr, AttributePtr>> aSelectedPointsList = aRefListOfFilletPoints->list();
506 std::list<std::pair<ObjectPtr, AttributePtr>>::iterator anIter = aSelectedPointsList.begin();
507 std::set<AttributePtr> aPointsToSkeep;
508 for(int anIndex = 0; anIndex < aListSize; anIndex++, anIter++) {
509 AttributePtr aFilletPointAttr = (*anIter).second;
510 std::shared_ptr<GeomDataAPI_Point2D> aFilletPoint2D =
511 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFilletPointAttr);
512 if(!aFilletPoint2D.get()) {
514 setError("Error: One of the selected points is invalid.");
518 // If point or coincident point is already in list remove it from attribute.
519 if(aPointsToSkeep.find(aFilletPointAttr) != aPointsToSkeep.end()) {
520 myListOfPointsChangedInCode = true;
521 aRefListOfFilletPoints->remove(aFilletPointAttr);
522 myListOfPointsChangedInCode = false;
526 // Obtain constraint coincidence for the fillet point.
527 FeaturePtr aConstraintCoincidence;
528 const std::set<AttributePtr>& aRefsList = aFilletPointAttr->owner()->data()->refsToMe();
529 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin(); anIt != aRefsList.cend(); ++anIt) {
530 std::shared_ptr<ModelAPI_Attribute> anAttr = (*anIt);
531 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
532 if(aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
533 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
534 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
535 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
536 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
537 if(anAttrRefA.get()) {
538 AttributePtr anAttrA = anAttrRefA->attr();
539 if(aFilletPointAttr == anAttrA) {
540 aConstraintCoincidence = aConstrFeature;
544 if(anAttrRefB.get()) {
545 AttributePtr anAttrB = anAttrRefB->attr();
546 if(aFilletPointAttr == anAttrB) {
547 aConstraintCoincidence = aConstrFeature;
554 if(!aConstraintCoincidence.get()) {
556 setError("Error: No coincident edges at one of the selected points.");
560 // Get coincides from constraint.
561 std::set<FeaturePtr> aCoincides;
564 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
565 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
567 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
568 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
571 // Remove points from set of coincides. Also get all attributes which is equal to this point to exclude it.
572 std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt2d = aFilletPoint2D->pnt();
573 std::set<FeaturePtr> aNewSetOfCoincides;
574 for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
575 std::string aFeatureKind = (*anIt)->getKind();
576 if(aFeatureKind == SketchPlugin_Point::ID()) {
577 AttributePtr anAttr = (*anIt)->attribute(SketchPlugin_Point::COORD_ID());
578 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
579 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
580 if(aPoint2D.get() && aFilletPnt2d->isEqual(aPoint2D->pnt())) {
581 aPointsToSkeep.insert(anAttr);
583 } else if(aFeatureKind == SketchPlugin_Line::ID()) {
584 AttributePtr anAttrStart = (*anIt)->attribute(SketchPlugin_Line::START_ID());
585 std::shared_ptr<GeomDataAPI_Point2D> aPointStart2D =
586 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrStart);
587 if(aPointStart2D.get() && aFilletPnt2d->isEqual(aPointStart2D->pnt())) {
588 aPointsToSkeep.insert(anAttrStart);
590 AttributePtr anAttrEnd = (*anIt)->attribute(SketchPlugin_Line::END_ID());
591 std::shared_ptr<GeomDataAPI_Point2D> aPointEnd2D =
592 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrEnd);
593 if(aPointEnd2D.get() && aFilletPnt2d->isEqual(aPointEnd2D->pnt())) {
594 aPointsToSkeep.insert(anAttrEnd);
596 aNewSetOfCoincides.insert(*anIt);
597 } else if(aFeatureKind == SketchPlugin_Arc::ID() ) {
598 AttributePtr anAttrCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
599 std::shared_ptr<GeomDataAPI_Point2D> aPointCenter2D =
600 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrCenter);
601 if(aPointCenter2D.get() && aFilletPnt2d->isEqual(aPointCenter2D->pnt())) {
602 aPointsToSkeep.insert(anAttrCenter);
605 AttributePtr anAttrStart = (*anIt)->attribute(SketchPlugin_Arc::START_ID());
606 std::shared_ptr<GeomDataAPI_Point2D> aPointStart2D =
607 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrStart);
608 if(aPointStart2D.get() && aFilletPnt2d->isEqual(aPointStart2D->pnt())) {
609 aPointsToSkeep.insert(anAttrStart);
611 AttributePtr anAttrEnd = (*anIt)->attribute(SketchPlugin_Arc::END_ID());
612 std::shared_ptr<GeomDataAPI_Point2D> aPointEnd2D =
613 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrEnd);
614 if(aPointEnd2D.get() && aFilletPnt2d->isEqual(aPointEnd2D->pnt())) {
615 aPointsToSkeep.insert(anAttrEnd);
617 aNewSetOfCoincides.insert(*anIt);
620 aCoincides = aNewSetOfCoincides;
622 // If we still have more than two coincides remove auxilary entities from set of coincides.
623 if(aCoincides.size() > 2) {
624 aNewSetOfCoincides.clear();
625 for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
626 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
627 aNewSetOfCoincides.insert(*anIt);
630 aCoincides = aNewSetOfCoincides;
633 if(aCoincides.size() != 2) {
635 setError("Error: One of the selected points does not have two suitable edges for fillet.");
639 // Store base point for fillet.
640 aPointsToSkeep.insert(aFilletPointAttr);
641 myNewPoints.insert(aFilletPointAttr);
643 // Get base lines for fillet.
644 FeaturePtr anOldFeatureA, anOldFeatureB;
645 std::set<FeaturePtr>::iterator aLinesIt = aCoincides.begin();
646 anOldFeatureA = *aLinesIt++;
647 anOldFeatureB = *aLinesIt;
649 // Getting radius value if it was not changed by user.
650 if(!myRadiusChangedByUser) {
651 // Getting points located at 1/3 of edge length from fillet point.
652 std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt2d = aFilletPoint2D->pnt();
653 std::shared_ptr<GeomAPI_Pnt2d> aPntA, aPntB;
654 getPointOnEdge(anOldFeatureA, aFilletPnt2d, aPntA);
655 getPointOnEdge(anOldFeatureB, aFilletPnt2d, aPntB);
657 /// Getting distances.
658 double aDistanceA = getProjectionDistance(anOldFeatureB, aPntA);
659 double aDistanceB = getProjectionDistance(anOldFeatureA, aPntB);
660 double aRadius = aDistanceA < aDistanceB ? aDistanceA / 2.0 : aDistanceB / 2.0;
661 aMinimumRadius = aMinimumRadius == 0 ? aRadius : aRadius < aMinimumRadius ? aRadius : aMinimumRadius;
665 // Set new default radius if it was not changed by user.
666 if(!myRadiusChangedByUser) {
667 myRadiusChangedInCode = true;
668 aRadiusAttribute->setValue(aMinimumRadius);
669 myRadiusChangedInCode = false;
672 } else if(theID == SketchPlugin_Constraint::VALUE()) {
673 if(myRadiusInitialized && !myRadiusChangedInCode) {
674 myRadiusChangedByUser = true;
676 if(!myRadiusInitialized) {
677 myRadiusInitialized = true;
683 //AISObjectPtr SketchPlugin_ConstraintSplit::getAISObject(AISObjectPtr thePrevious)
686 // return thePrevious;
688 // AISObjectPtr anAIS = thePrevious;
689 // /// TODO: Equal constraint presentation should be put here
693 bool SketchPlugin_ConstraintSplit::isMacro() const
698 //void SketchPlugin_ConstraintSplit::clearResults()
700 ///* // Clear auxiliary flag on initial objects.
701 // for(std::map<AttributePtr, FilletFeatures>::iterator aPointsIter = myPointFeaturesMap.begin();
702 // aPointsIter != myPointFeaturesMap.end();) {
703 // const FilletFeatures& aFilletFeatures = aPointsIter->second;
704 // std::list<std::pair<FeaturePtr, bool>>::const_iterator aFeatureIt;
705 // for(aFeatureIt = aFilletFeatures.baseEdgesState.cbegin();
706 // aFeatureIt != aFilletFeatures.baseEdgesState.cend();
708 // aFeatureIt->first->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(aFeatureIt->second);
713 // // And remove all produced features.
714 // DocumentPtr aDoc = sketch()->document();
715 // for(std::map<AttributePtr, FilletFeatures>::iterator aPointsIter = myPointFeaturesMap.begin();
716 // aPointsIter != myPointFeaturesMap.end();) {
717 // // Remove all produced constraints.
718 // const FilletFeatures& aFilletFeatures = aPointsIter->second;
719 // std::list<FeaturePtr>::const_iterator aFeatureIt;
720 // for(aFeatureIt = aFilletFeatures.resultConstraints.cbegin();
721 // aFeatureIt != aFilletFeatures.resultConstraints.cend();
723 // aDoc->removeFeature(*aFeatureIt);
726 // // Remove all result edges.
727 // for(aFeatureIt = aFilletFeatures.resultEdges.cbegin();
728 // aFeatureIt != aFilletFeatures.resultEdges.cend();
730 // aDoc->removeFeature(*aFeatureIt);
733 // // Remove point from map.
734 // myPointFeaturesMap.erase(aPointsIter++);
740 //// ========= Auxiliary functions =================
741 //void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
742 // FeaturePtr theFeature, const std::string& theFeatureAttribute)
744 // std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
745 // theNewArc->attribute(theNewArcAttribute))->pnt();
746 // std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
747 // theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
750 /*/// \brief Find intersections of lines shifted along normal direction
751 void possibleFilletCenterLineLine(
752 std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
753 std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
754 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
756 std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
757 std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
758 std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
759 double aDet = theDirA->cross(theDirB);
760 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
761 aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
762 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
763 aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
764 double aVX = aDirAT->xy()->dot(aPntA);
765 double aVY = aDirBT->xy()->dot(aPntB);
766 std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
767 (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
768 (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
769 theCenters.push_back(aPoint);
774 /// \brief Find intersections of line shifted along normal direction in both sides
775 /// and a circle with extended radius
776 void possibleFilletCenterLineArc(
777 std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
778 std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
779 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
781 std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
782 std::shared_ptr<GeomAPI_XY> aPnt;
783 double aDirNorm2 = theDirLine->dot(theDirLine);
785 double aDirX = theDirLine->x();
786 double aDirX2 = theDirLine->x() * theDirLine->x();
787 double aDirY2 = theDirLine->y() * theDirLine->y();
788 double aDirXY = theDirLine->x() * theDirLine->y();
789 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
790 aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
791 double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
792 double aCoeff2 = aCoeff * aCoeff;
793 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
794 aRad = theRadiusArc + aStepB * theRadius;
795 double aD = aRad * aRad * aDirNorm2 - aCoeff2;
798 double aDs = sqrt(aD);
799 double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
800 double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
801 double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
802 aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
803 double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
804 aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
806 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
807 theCenters.push_back(aPoint1);
808 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
809 theCenters.push_back(aPoint2);
814 /// \brief Find intersections of two circles with extended radii
815 void possibleFilletCenterArcArc(
816 std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
817 std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
818 double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
820 std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
821 double aCenterDist2 = aCenterDir->dot(aCenterDir);
822 double aCenterDist = sqrt(aCenterDist2);
825 for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
826 aRadA = theRadiusA + aStepA * theRadius;
827 for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
828 aRadB = theRadiusB + aStepB * theRadius;
829 if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
830 continue; // there is no intersections
832 double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
833 double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
835 double x1 = theCenterA->x() + (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
836 double y1 = theCenterA->y() + (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
838 double x2 = theCenterA->x() + (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
839 double y2 = theCenterA->y() + (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
841 std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
842 theCenters.push_back(aPoint1);
843 std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
844 theCenters.push_back(aPoint2);
849 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
850 double theRadius, bool theNotInversed[2],
851 std::shared_ptr<GeomAPI_XY>& theCenter,
852 std::shared_ptr<GeomAPI_XY>& theTangentA,
853 std::shared_ptr<GeomAPI_XY>& theTangentB)
855 static const int aNbFeatures = 2;
856 FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
857 std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
858 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
860 for (int i = 0; i < aNbFeatures; i++) {
861 if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
862 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
863 aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
864 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
865 aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
866 } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
867 aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
868 aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
869 aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
870 aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
871 aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
872 aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
875 aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
876 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
877 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
878 aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
879 new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
880 new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
883 if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
884 theFeatureB->getKind() == SketchPlugin_Line::ID()) {
885 std::shared_ptr<GeomAPI_Dir2d> aDir[2];
886 std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
887 for (int i = 0; i < aNbFeatures; i++) {
888 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
889 aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
892 // get and filter possible centers
893 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
894 possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1], theRadius, aSuspectCenters);
896 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
897 for (; anIt != aSuspectCenters.end(); anIt++) {
898 aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
899 theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
900 if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
901 continue; // incorrect position
902 aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
903 theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
904 if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
905 continue; // incorrect position
906 // the center is found, stop searching
910 } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
911 theFeatureB->getKind() == SketchPlugin_Line::ID()) ||
912 (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
913 theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
914 int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
915 double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
916 std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
917 new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
918 std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
919 new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
921 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
922 new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
923 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
924 new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
925 double anArcAngle = aEndArcDir->angle(aStartArcDir);
927 // get possible centers and filter them
928 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
929 possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd], anArcRadius, theRadius, aSuspectCenters);
931 // the line is forward into the arc
932 double innerArc = aCenter[1-aLineInd]->decreased(aStart[aLineInd])->dot(aDirLine->xy());
933 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
934 // The possible centers are ranged by their positions.
935 // If the point is not satisfy one of criteria, the weight is decreased with penalty.
937 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
938 for (; anIt != aSuspectCenters.end(); anIt++) {
940 aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
941 aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
942 // Check the point is placed on the correct arc (penalty if false)
943 if (aCenter[1-aLineInd]->distance(*anIt) * innerArc > anArcRadius * innerArc)
945 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
946 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
947 double aCurAngle = aCurDir->angle(aStartArcDir);
948 if (anArcAngle < 0.0) aCurAngle *= -1.0;
949 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
951 if (aWeight > aBestWeight)
952 aBestWeight = aWeight;
953 else if (aWeight < aBestWeight ||
954 aStart[aLineInd]->distance(*anIt) >
955 aStart[aLineInd]->distance(theCenter)) // <-- take closer point
957 // the center is found, stop searching
959 anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
960 if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
961 theTangentA = aLineTgPoint;
962 theTangentB = anArcTgPoint;
964 theTangentA = anArcTgPoint;
965 theTangentB = aLineTgPoint;
969 } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
970 theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
971 double anArcRadius[aNbFeatures];
972 double anArcAngle[aNbFeatures];
973 std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
974 for (int i = 0; i < aNbFeatures; i++) {
975 anArcRadius[i] = aStart[i]->distance(aCenter[i]);
976 aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
977 new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
978 std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
979 new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
980 anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
983 // get and filter possible centers
984 std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
985 possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1], anArcRadius[1], theRadius, aSuspectCenters);
987 std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
988 std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
989 for (; anIt != aSuspectCenters.end(); anIt++) {
990 std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
991 new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
992 double aCurAngle = aCurDir->angle(aStartArcDir[0]);
993 if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
994 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
995 continue; // incorrect position
996 theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
998 aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
999 aCurAngle = aCurDir->angle(aStartArcDir[1]);
1000 if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
1001 if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
1002 continue; // incorrect position
1003 theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
1005 // the center is found, stop searching
1012 void getPointOnEdge(const FeaturePtr theFeature,
1013 const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
1014 std::shared_ptr<GeomAPI_Pnt2d>& thePoint) {
1015 if(theFeature->getKind() == SketchPlugin_Line::ID()) {
1016 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1017 theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
1018 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1019 theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
1020 if(aPntStart->distance(theFilletPoint) > 1.e-7) {
1021 aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1022 theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
1023 aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1024 theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
1026 thePoint.reset( new GeomAPI_Pnt2d(aPntStart->xy()->added( aPntEnd->xy()->decreased( aPntStart->xy() )->multiplied(1.0 / 3.0) ) ) );
1028 std::shared_ptr<GeomAPI_Pnt2d> aPntTemp;
1029 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1030 theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
1031 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1032 theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
1033 if(theFeature->attribute(SketchPlugin_Arc::INVERSED_ID())) {
1034 aPntTemp = aPntStart;
1035 aPntStart = aPntEnd;
1038 std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1039 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1040 std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
1041 double aStartParameter(0), anEndParameter(0);
1042 aCirc->parameter(aPntStart, paramTolerance, aStartParameter);
1043 aCirc->parameter(aPntEnd, paramTolerance, anEndParameter);
1044 if(aPntStart->distance(theFilletPoint) > tolerance) {
1045 double aTmpParameter = aStartParameter;
1046 aStartParameter = anEndParameter;
1047 anEndParameter = aTmpParameter;
1049 double aPntParameter = aStartParameter + (anEndParameter - aStartParameter) / 3.0;
1050 aCirc->D0(aPntParameter, thePoint);
1054 double getProjectionDistance(const FeaturePtr theFeature,
1055 const std::shared_ptr<GeomAPI_Pnt2d> thePoint)
1057 std::shared_ptr<GeomAPI_Pnt2d> aProjectPnt;
1058 if(theFeature->getKind() == SketchPlugin_Line::ID()) {
1059 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1060 theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
1061 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1062 theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
1063 std::shared_ptr<GeomAPI_Lin2d> aLin(new GeomAPI_Lin2d(aPntStart, aPntEnd));
1064 aProjectPnt = aLin->project(thePoint);
1066 std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1067 theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
1068 std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1069 theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
1070 std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1071 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1072 std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
1073 aProjectPnt = aCirc->project(thePoint);
1075 if(aProjectPnt.get()) {
1076 return aProjectPnt->distance(thePoint);
1081 std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence)
1083 std::set<FeaturePtr> aCoincides;
1085 std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt = SketchPlugin_Tools::getCoincidencePoint(theConstraintCoincidence);
1087 SketchPlugin_Tools::findCoincidences(theConstraintCoincidence,
1088 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
1090 SketchPlugin_Tools::findCoincidences(theConstraintCoincidence,
1091 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
1094 // Remove points from set of coincides.
1095 std::set<FeaturePtr> aNewSetOfCoincides;
1096 for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
1097 if((*anIt)->getKind() == SketchPlugin_Line::ID()) {
1098 aNewSetOfCoincides.insert(*anIt);
1099 } else if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
1100 AttributePtr anAttrCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
1101 std::shared_ptr<GeomDataAPI_Point2D> aPointCenter2D =
1102 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrCenter);
1103 if(aPointCenter2D.get() && aFilletPnt->isEqual(aPointCenter2D->pnt())) {
1106 aNewSetOfCoincides.insert(*anIt);
1109 aCoincides = aNewSetOfCoincides;
1111 // If we still have more than two coincides remove auxilary entities from set of coincides.
1112 if(aCoincides.size() > 2) {
1113 aNewSetOfCoincides.clear();
1114 for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
1115 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
1116 aNewSetOfCoincides.insert(*anIt);
1119 aCoincides = aNewSetOfCoincides;
1126 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_ConstraintSplit::getPointOfRefAttr(
1127 const AttributePtr& theAttribute)
1129 AttributePoint2DPtr aPointAttribute;
1131 if (theAttribute->id() == ModelAPI_AttributeRefAttr::typeId()) {
1132 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1133 if (aRefAttr.get() && aRefAttr->isInitialized()) {
1134 AttributePtr anAttribute = aRefAttr->attr();
1135 if (anAttribute.get() && anAttribute->id() == GeomDataAPI_Point2D::typeId())
1136 aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
1139 return aPointAttribute;
1142 AttributePoint2DPtr SketchPlugin_ConstraintSplit::getFeaturePoint(const bool& theStartPoint)
1144 AttributePoint2DPtr aPointAttribute;
1146 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1147 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1148 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1150 std::string aFeatureKind = aBaseFeature->getKind();
1151 std::string anAttributeName;
1152 if (aFeatureKind == SketchPlugin_Line::ID())
1153 anAttributeName = theStartPoint ? SketchPlugin_Line::START_ID()
1154 : SketchPlugin_Line::END_ID();
1155 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
1156 anAttributeName = theStartPoint ? SketchPlugin_Arc::START_ID()
1157 : SketchPlugin_Arc::END_ID();
1159 if (!anAttributeName.empty())
1160 aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1161 aBaseFeature->attribute(anAttributeName));
1162 return aPointAttribute;
1165 FeaturePtr SketchPlugin_ConstraintSplit::createFeature(const AttributePoint2DPtr& theStartPointAttr,
1166 const AttributePoint2DPtr& theEndPointAttr)
1168 FeaturePtr aFeature;
1170 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1171 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1172 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1174 std::string aFeatureKind = aBaseFeature->getKind();
1175 aFeature = sketch()->addFeature(aFeatureKind);
1177 if (aFeatureKind == SketchPlugin_Line::ID()) {
1178 AttributePoint2DPtr aStartAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1179 aFeature->attribute(SketchPlugin_Line::START_ID()));
1180 aStartAttribute->setValue(theStartPointAttr->pnt());
1182 AttributePoint2DPtr anEndAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1183 aFeature->attribute(SketchPlugin_Line::END_ID()));
1184 anEndAttribute->setValue(theEndPointAttr->pnt());
1186 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
1187 AttributeStringPtr anArcType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
1188 data()->addAttribute(SketchPlugin_Arc::ARC_TYPE(), ModelAPI_AttributeString::typeId()));
1190 AttributePoint2DPtr aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1191 aFeature->attribute(SketchPlugin_Arc::START_ID()));
1192 aPointAttribute->setValue(theStartPointAttr->pnt());
1194 AttributePoint2DPtr anEndAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1195 aFeature->attribute(SketchPlugin_Arc::END_ID()));
1196 anEndAttribute->setValue(theEndPointAttr->pnt());