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_Pnt2d.h>
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_AttributeReference.h>
12 #include <ModelAPI_AttributeString.h>
13 #include <ModelAPI_AttributeRefAttr.h>
14 #include <ModelAPI_Tools.h>
16 #include <ModelAPI_Validator.h>
17 #include <ModelAPI_Session.h>
19 #include <SketchPlugin_Line.h>
20 #include <SketchPlugin_Arc.h>
21 #include <SketchPlugin_Circle.h>
22 #include <SketchPlugin_ConstraintCoincidence.h>
23 #include <SketchPlugin_ConstraintEqual.h>
24 #include <SketchPlugin_ConstraintParallel.h>
25 #include <SketchPlugin_ConstraintTangent.h>
26 #include <SketchPlugin_ConstraintMirror.h>
27 #include <SketchPlugin_MultiRotation.h>
28 #include <SketchPlugin_MultiTranslation.h>
30 //#include <ModelAPI_Data.h>
31 #include <ModelAPI_Events.h>
32 //#include <ModelAPI_Session.h>
33 //#include <ModelAPI_Validator.h>
35 #include <SketchPlugin_Line.h>
36 #include <SketchPlugin_Arc.h>
37 #include <SketchPlugin_Circle.h>
38 //#include <SketchPlugin_Point.h>
39 //#include <SketchPlugin_Sketch.h>
40 //#include <SketchPlugin_Tools.h>
43 #include <ModelGeomAlgo_Point2D.h>
44 #include <Events_Loop.h>
48 //const double tolerance = 1.e-7;
49 //const double paramTolerance = 1.e-4;
51 ///// \brief Attract specified point on theNewArc to the attribute of theFeature
52 //static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
53 // FeaturePtr theFeature, const std::string& theFeatureAttribute);
55 ///// \brief Calculates center of fillet arc and coordinates of tangency points
56 //static void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
57 // double theRadius, bool theNotInversed[2],
58 // std::shared_ptr<GeomAPI_XY>& theCenter,
59 // std::shared_ptr<GeomAPI_XY>& theTangentA,
60 // std::shared_ptr<GeomAPI_XY>& theTangentB);
62 ///// Get point on 1/3 length of edge from fillet point
63 //static void getPointOnEdge(const FeaturePtr theFeature,
64 // const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
65 // std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
67 ///// Get distance from point to feature
68 //static double getProjectionDistance(const FeaturePtr theFeature,
69 // const std::shared_ptr<GeomAPI_Pnt2d> thePoint);
71 ///// Get coincide edges for fillet
72 //static std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence);
74 SketchPlugin_ConstraintSplit::SketchPlugin_ConstraintSplit()
75 //: myListOfPointsChangedInCode(false),
76 // myRadiusChangedByUser(false),
77 // myRadiusChangedInCode(false),
78 // myRadiusInitialized(false)
82 void SketchPlugin_ConstraintSplit::initAttributes()
84 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeReference::typeId());
85 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
86 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
88 //data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
89 //data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttrList::typeId());
92 void SketchPlugin_ConstraintSplit::execute()
94 std::shared_ptr<ModelAPI_Data> aData = data();
96 // Check the base objects are initialized.
97 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
98 aData->attribute(SketchPlugin_Constraint::VALUE()));
99 if(!aBaseObjectAttr->isInitialized()) {
100 setError("Error: Base object is not initialized.");
103 AttributePoint2DPtr aFirstPointAttr = getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
104 AttributePoint2DPtr aSecondPointAttr = getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
105 if (!aFirstPointAttr.get() || !aFirstPointAttr->isInitialized() ||
106 !aSecondPointAttr.get() || !aSecondPointAttr->isInitialized()) {
107 setError("Error: Sub-shape is not initialized.");
111 // Wait all constraints being created, then send update events
112 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
113 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
115 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
118 // Find feature constraints
119 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
120 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
122 std::set<FeaturePtr> aFeaturesToDelete;
123 std::map<FeaturePtr, IdToPointPair> aTangentFeatures;
124 std::map<FeaturePtr, IdToPointPair> aCoincidenceToFeature;
125 std::map<FeaturePtr, IdToPointPair> aCoincidenceToPoint;
126 getConstraints(aFeaturesToDelete, aTangentFeatures, aCoincidenceToFeature, aCoincidenceToPoint);
129 std::string aFeatureKind = aBaseFeature->getKind();
130 FeaturePtr aSplitFeature, anAfterFeature;
131 std::set<AttributePoint2DPtr> aFurtherCoincidences;
132 /*if (aFeatureKind == SketchPlugin_Line::ID())
133 splitLine(aSplitFeature, anOtherFeatures);
134 else*/ if (aFeatureKind == SketchPlugin_Arc::ID())
135 splitArc(aSplitFeature, aBaseFeature, anAfterFeature, aFurtherCoincidences);
136 /*if (aFeatureKind == SketchPlugin_Circle::ID())
137 splitCircle(aSplitFeature, anOtherFeatures);
138 FeaturePtr aSplitFeature;
139 std::set<FeaturePtr> anOtherFeatures;*/
141 //setConstraints(aSplitFeature, aBaseFeature, anAfterFeature, aFeaturesToDelete, aTangentFeatures,
142 // aCoincidenceToFeature, aCoincidenceToPoint);
144 std::set<ResultPtr> aFeatureResults;
145 aFeatureResults.insert(getFeatureResult(aBaseFeature));
146 if (anAfterFeature.get() && anAfterFeature != aBaseFeature)
147 aFeatureResults.insert(getFeatureResult(anAfterFeature));
149 // coincidence to feature
150 std::map<FeaturePtr, IdToPointPair>::const_iterator aCIt = aCoincidenceToFeature.begin(),
151 aCLast = aCoincidenceToFeature.end();
152 for (; aCIt != aCLast; aCIt++) {
153 FeaturePtr aCoincFeature = aCIt->first;
154 std::string anAttributeId = aCIt->second.first;
155 AttributePoint2DPtr aCoincPoint = aCIt->second.second;
156 std::set<AttributePoint2DPtr>::const_iterator aFCIt = aFurtherCoincidences.begin(),
157 aFCLast = aFurtherCoincidences.end();
158 std::shared_ptr<GeomAPI_Pnt2d> aCoincPnt = aCoincPoint->pnt();
159 AttributePoint2DPtr aFeaturePointAttribute;
160 for (; aFCIt != aFCLast && !aFeaturePointAttribute.get(); aFCIt++) {
161 AttributePoint2DPtr aFCAttribute = *aFCIt;
162 if (aCoincPnt->isEqual(aFCAttribute->pnt()))
163 aFeaturePointAttribute = aFCAttribute;
165 if (aFeaturePointAttribute.get()) {
166 aCoincFeature->refattr(anAttributeId)->setAttr(aFeaturePointAttribute);
169 /// find feature by shape intersected the point
170 ResultPtr aResultForCoincidence = *(aFeatureResults.begin());
172 if (aFeatureResults.size() > 1) { // try to find point on additional feature
173 ResultPtr anAddtionalResult = *(aFeatureResults.begin()++);
174 GeomShapePtr aShape = anAddtionalResult->shape();
176 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCoincPoint->pnt();
177 std::shared_ptr<GeomAPI_Pnt> aPoint = sketch()->to3D(aPnt2d->x(), aPnt2d->y());
179 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
180 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPoint, aProjectedPoint))
181 aResultForCoincidence = anAddtionalResult;
183 aCoincFeature->refattr(anAttributeId)->setObject(aResultForCoincidence);
186 // coincidence to points
191 // delete constraints
192 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
194 // Send events to update the sub-features by the solver.
195 if(isUpdateFlushed) {
196 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
200 bool SketchPlugin_ConstraintSplit::isMacro() const
205 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_ConstraintSplit::getPointOfRefAttr(
206 const AttributePtr& theAttribute)
208 AttributePoint2DPtr aPointAttribute;
210 if (theAttribute->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
211 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
212 if (aRefAttr.get() && aRefAttr->isInitialized()) {
213 AttributePtr anAttribute = aRefAttr->attr();
214 if (anAttribute.get() && anAttribute->attributeType() == GeomDataAPI_Point2D::typeId())
215 aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
218 return aPointAttribute;
221 void SketchPlugin_ConstraintSplit::getFeaturePoints(AttributePoint2DPtr& theStartPointAttr,
222 AttributePoint2DPtr& theEndPointAttr)
224 AttributePoint2DPtr aPointAttribute;
226 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
227 data()->attribute(SketchPlugin_Constraint::VALUE()));
228 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
230 std::string aFeatureKind = aBaseFeature->getKind();
231 std::string aStartAttributeName, anEndAttributeName;
232 if (aFeatureKind == SketchPlugin_Line::ID()) {
233 aStartAttributeName = SketchPlugin_Line::START_ID();
234 anEndAttributeName = SketchPlugin_Line::END_ID();
236 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
237 aStartAttributeName = SketchPlugin_Arc::START_ID();
238 anEndAttributeName = SketchPlugin_Arc::END_ID();
240 if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
241 theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
242 aBaseFeature->attribute(aStartAttributeName));
243 theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
244 aBaseFeature->attribute(anEndAttributeName));
248 void SketchPlugin_ConstraintSplit::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
249 std::map<FeaturePtr, IdToPointPair>& theTangentFeatures,
250 std::map<FeaturePtr, IdToPointPair>& theCoincidenceToFeature,
251 std::map<FeaturePtr, IdToPointPair>& theCoincidenceToPoint)
253 std::shared_ptr<ModelAPI_Data> aData = data();
255 // Check the base objects are initialized.
256 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
257 aData->attribute(SketchPlugin_Constraint::VALUE()));
258 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
259 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
261 const std::set<AttributePtr>& aRefsList = aBaseFeatureResult->data()->refsToMe();
262 std::set<AttributePtr>::const_iterator aIt;
263 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
264 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
265 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
266 std::string aRefFeatureKind = aRefFeature->getKind();
267 if (aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() ||
268 aRefFeatureKind == SketchPlugin_MultiRotation::ID() ||
269 aRefFeatureKind == SketchPlugin_MultiTranslation::ID())
270 theFeaturesToDelete.insert(aRefFeature);
271 else if (aRefFeatureKind == SketchPlugin_ConstraintTangent::ID()) {
272 if (aBaseFeature->getKind() == SketchPlugin_Circle::ID()) /// TEMPORARY limitaion
273 theFeaturesToDelete.insert(aRefFeature); /// until tangency between arc and line is implemented
275 std::string anAttributeToBeModified;
276 AttributePoint2DPtr aTangentPoint;
277 ObjectPtr aResult1 = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_A())->object();
278 ObjectPtr aResult2 = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_B())->object();
279 if (aResult1.get() && aResult2.get()) {
280 FeaturePtr aCoincidenceFeature = SketchPlugin_ConstraintCoincidence::findCoincidenceFeature
281 (ModelAPI_Feature::feature(aResult1),
282 ModelAPI_Feature::feature(aResult2));
283 aTangentPoint = SketchPlugin_ConstraintCoincidence::getPoint(aCoincidenceFeature);
285 if (aTangentPoint.get()) {
286 FeaturePtr aFeature1 = ModelAPI_Feature::feature(aResult1);
287 std::string anAttributeToBeModified = aFeature1 == aBaseFeature
288 ? SketchPlugin_Constraint::ENTITY_B() : SketchPlugin_Constraint::ENTITY_A();
289 theTangentFeatures[aRefFeature] = std::make_pair(anAttributeToBeModified, aTangentPoint);
292 theFeaturesToDelete.insert(aRefFeature); /// there is not coincident point between tangent constraint
295 else if (aRefFeatureKind == SketchPlugin_ConstraintCoincidence::ID()) {
296 std::string anAttributeToBeModified;
297 AttributePoint2DPtr aCoincidentPoint;
298 AttributeRefAttrPtr anAttrA = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_A());
299 AttributeRefAttrPtr anAttrB = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_B());
300 bool isToFeature = false;
301 if (anAttrA->isObject() || anAttrB->isObject()) { /// coincidence to base feature
302 FeaturePtr aFeature = anAttrA->isObject() ? ModelAPI_Feature::feature(anAttrA->object())
304 isToFeature = aFeature.get() && aFeature == aBaseFeature;
305 anAttributeToBeModified = SketchPlugin_Constraint::ENTITY_B();
307 aFeature = anAttrB->isObject() ? ModelAPI_Feature::feature(anAttrB->object())
309 isToFeature = aFeature.get() && aFeature == aBaseFeature;
310 anAttributeToBeModified = SketchPlugin_Constraint::ENTITY_A();
313 aCoincidentPoint = SketchPlugin_ConstraintCoincidence::getPoint(aRefFeature);
315 if (!isToFeature) { /// coincidence to point on base feature
316 AttributePtr anAttribute;
317 if (!anAttrA->isObject()) {
318 AttributePtr aCurAttribute = anAttrA->attr();
319 if (aCurAttribute.get()) {
320 FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
321 if (aCurFeature.get() && aCurFeature == aBaseFeature) {
322 anAttribute = anAttrA->attr();
323 anAttributeToBeModified = SketchPlugin_Constraint::ENTITY_A();
327 if (!anAttribute.get() && !anAttrB->isObject()) {
328 AttributePtr aCurAttribute = anAttrB->attr();
329 if (aCurAttribute.get()) {
330 FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
331 if (aCurFeature.get() && aCurFeature == aBaseFeature) {
332 anAttribute = anAttrB->attr();
333 anAttributeToBeModified = SketchPlugin_Constraint::ENTITY_B();
337 if (anAttribute.get())
338 aCoincidentPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
340 if (aCoincidentPoint.get()) {
342 theCoincidenceToFeature[aRefFeature] = std::make_pair(anAttributeToBeModified,
345 theCoincidenceToPoint[aRefFeature] = std::make_pair(anAttributeToBeModified,
349 theFeaturesToDelete.insert(aRefFeature); /// this case should not happen
354 /*void SketchPlugin_ConstraintSplit::setConstraints(const FeaturePtr& theSplitFeature,
355 const FeaturePtr& theBaseFeature,
356 const FeaturePtr& theAfterFeature,
357 const std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete,
358 const std::map<std::shared_ptr<ModelAPI_Feature>, std::shared_ptr<GeomDataAPI_Point2D> >& theTangentFeatures,
359 const std::map<std::shared_ptr<ModelAPI_Feature>, std::shared_ptr<GeomDataAPI_Point2D> >& theCoincidenceToFeature,
360 const std::map<std::shared_ptr<ModelAPI_Feature>, std::shared_ptr<GeomDataAPI_Point2D> >& theCoincidenceToPoint)
362 // coincidence to feature
365 // coincidence to points
369 // delete constraints
373 void SketchPlugin_ConstraintSplit::splitArc(FeaturePtr& theSplitFeature,
374 FeaturePtr& theBaseFeature,
375 FeaturePtr& theAfterFeature,
376 std::set<AttributePoint2DPtr>& thePoints)
378 SketchPlugin_Sketch* aSketch = sketch();
382 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
383 data()->attribute(SketchPlugin_Constraint::VALUE()));
384 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
385 std::string aFeatureKind = aBaseFeature->getKind();
386 if (aFeatureKind != SketchPlugin_Arc::ID())
389 AttributePoint2DPtr aFirstPointAttr = getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
390 AttributePoint2DPtr aSecondPointAttr = getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
391 AttributePoint2DPtr aStartPointAttr, anEndPointAttr;
392 getFeaturePoints(aStartPointAttr, anEndPointAttr);
393 if (!aStartPointAttr.get() && !anEndPointAttr.get()) {
394 setError("Error: Feature has no start and end points.");
398 arrangePoints(aStartPointAttr, anEndPointAttr, aFirstPointAttr, aSecondPointAttr);
401 theSplitFeature = createArcFeature(aBaseFeature, aFirstPointAttr, aSecondPointAttr);
403 // before split feature
404 if (!aStartPointAttr->pnt()->isEqual(aFirstPointAttr->pnt())) {
405 theBaseFeature = aBaseFeature; ///< use base feature to store all constraints here
406 /// move end arc point to start of split
409 // after split feature
410 if (!aSecondPointAttr->pnt()->isEqual(anEndPointAttr->pnt())) {
412 if (!theBaseFeature.get()) {
413 aFeature = aBaseFeature; ///< use base feature to store all constraints here
414 fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), aSecondPointAttr);
415 aFeature->execute(); // to update result
418 aFeature = createArcFeature(aBaseFeature, aSecondPointAttr, anEndPointAttr);
420 createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
421 theSplitFeature->attribute(SketchPlugin_Arc::END_ID()),
422 aFeature->attribute(SketchPlugin_Arc::START_ID()));
423 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
424 (theBaseFeature->attribute(SketchPlugin_Arc::START_ID())));
425 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
426 (theBaseFeature->attribute(SketchPlugin_Arc::END_ID())));
428 if (!theBaseFeature.get())
429 theBaseFeature = aFeature;
431 theAfterFeature = aFeature;
434 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
435 (theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
437 // base split, that is defined before split feature should be changed at end
438 // (after the after feature creation). Otherwise modified value will be used in after feature
439 // before split feature
440 if (!aStartPointAttr->pnt()->isEqual(aFirstPointAttr->pnt())) {
441 /// move end arc point to start of split
442 fillAttribute(theBaseFeature->attribute(SketchPlugin_Arc::END_ID()), aFirstPointAttr);
443 theBaseFeature->execute(); // to update result
444 createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
445 theBaseFeature->attribute(SketchPlugin_Arc::END_ID()),
446 theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
447 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
448 (theBaseFeature->attribute(SketchPlugin_Arc::START_ID())));
449 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
450 (theBaseFeature->attribute(SketchPlugin_Arc::END_ID())));
453 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
454 (theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
456 // additional constraints between split and base features
457 createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(), getFeatureResult(aBaseFeature),
458 getFeatureResult(theSplitFeature));
459 createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(), getFeatureResult(theSplitFeature),
460 getFeatureResult(aBaseFeature));
461 if (theAfterFeature.get()) {
462 createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(), getFeatureResult(aBaseFeature),
463 getFeatureResult(theAfterFeature));
464 createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(), getFeatureResult(theSplitFeature),
465 getFeatureResult(theAfterFeature));
469 void SketchPlugin_ConstraintSplit::arrangePoints(const AttributePoint2DPtr& theStartPointAttr,
470 const AttributePoint2DPtr& theEndPointAttr,
471 AttributePoint2DPtr& theFirstPointAttr,
472 AttributePoint2DPtr& theLastPointAttr)
474 /// if first point is closer to last point, wrap first and last values
475 if (theStartPointAttr->pnt()->distance(theFirstPointAttr->pnt()) >
476 theStartPointAttr->pnt()->distance(theLastPointAttr->pnt())) {
477 AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
478 theFirstPointAttr = theLastPointAttr;
479 theLastPointAttr = aTmpPoint;
483 void SketchPlugin_ConstraintSplit::fillAttribute(const AttributePtr& theModifiedAttribute,
484 const AttributePtr& theSourceAttribute)
486 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
487 theModifiedAttribute);
488 AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
491 if (aModifiedAttribute.get() && aSourceAttribute.get())
492 aModifiedAttribute->setValue(aSourceAttribute->pnt());
495 FeaturePtr SketchPlugin_ConstraintSplit::createArcFeature(const FeaturePtr& theBaseFeature,
496 const AttributePtr& theFirstPointAttr,
497 const AttributePtr& theSecondPointAttr)
500 SketchPlugin_Sketch* aSketch = sketch();
501 if (!aSketch || !theBaseFeature.get())
504 aFeature = aSketch->addFeature(theBaseFeature->getKind());
505 // update fillet arc: make the arc correct for sure, so, it is not needed to process the "attribute updated"
506 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
507 aFeature->data()->blockSendAttributeUpdated(true);
509 aFeature->string(SketchPlugin_Arc::ARC_TYPE())->setValue(
510 SketchPlugin_Arc::ARC_TYPE_CENTER_START_END());
511 fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
512 theBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
513 fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPointAttr);
514 fillAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPointAttr);
515 aFeature->data()->blockSendAttributeUpdated(false);
516 aFeature->execute(); // to obtain result
521 void SketchPlugin_ConstraintSplit::createConstraint(const std::string& theConstraintId,
522 const AttributePtr& theFirstAttribute,
523 const AttributePtr& theSecondAttribute)
525 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
526 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
527 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
528 aRefAttr->setAttr(theFirstAttribute);
530 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
531 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
532 aRefAttr->setAttr(theSecondAttribute);
535 void SketchPlugin_ConstraintSplit::createConstraintForObjects(const std::string& theConstraintId,
536 const ObjectPtr& theFirstObject,
537 const ObjectPtr& theSecondObject)
539 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
540 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
541 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
542 aRefAttr->setObject(theFirstObject);
544 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
545 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
546 aRefAttr->setObject(theSecondObject);
549 std::shared_ptr<ModelAPI_Result> SketchPlugin_ConstraintSplit::getFeatureResult(
550 const std::shared_ptr<ModelAPI_Feature>& theFeature)
552 std::shared_ptr<ModelAPI_Result> aResult;
554 std::string aFeatureKind = theFeature->getKind();
555 if (aFeatureKind == SketchPlugin_Line::ID())
556 aResult = theFeature->firstResult();
557 else if (aFeatureKind == SketchPlugin_Arc::ID())
558 aResult = theFeature->lastResult();
559 else if (aFeatureKind == SketchPlugin_Circle::ID())
560 aResult = theFeature->lastResult();