+ FeaturePtr aNewFeatureA, aNewFeatureB, aNewArc;
+ if (needNewObjects) {
+ // Create list of objects composing a fillet
+ // copy aFeatureA
+ aNewFeatureA = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeatureA, sketch());
+ // copy aFeatureB
+ aNewFeatureB = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeatureB, sketch());
+ // create filleting arc (it will be attached to the list later)
+ aNewArc = sketch()->addFeature(SketchPlugin_Arc::ID());
+ } else {
+ // Obtain features from the list
+ std::list<ObjectPtr> aNewFeatList = aRefListOfFillet->list();
+ std::list<ObjectPtr>::iterator aFeatIt = aNewFeatList.begin();
+ aNewFeatureA = ModelAPI_Feature::feature(*aFeatIt++);
+ aNewFeatureB = ModelAPI_Feature::feature(*aFeatIt++);
+ aNewArc = ModelAPI_Feature::feature(*aFeatIt);
+ }
+
+ // Calculate arc attributes
+ static const int aNbFeatures = 2;
+ FeaturePtr aFeature[aNbFeatures] = {aFeatureA, aFeatureB};
+ FeaturePtr aNewFeature[aNbFeatures] = {aNewFeatureA, aNewFeatureB};
+ std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures]; // tangent directions of the features in coincident point
+ bool isStart[aNbFeatures]; // indicates which point the features share
+ std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2]; // first pair of points relate to first feature, second pair - to second
+ std::string aFeatAttributes[aNbFeatures * 2]; // attributes of features
+ for (int i = 0; i < aNbFeatures; i++) {
+ std::string aStartAttr, aEndAttr;
+ if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
+ aStartAttr = SketchPlugin_Line::START_ID();
+ aEndAttr = SketchPlugin_Line::END_ID();
+ } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
+ aStartAttr = SketchPlugin_Arc::START_ID();
+ aEndAttr = SketchPlugin_Arc::END_ID();
+ } else { // wrong argument
+ aRefListOfFillet->remove(aNewFeatureA);
+ aRefListOfFillet->remove(aNewFeatureB);
+ aRefListOfFillet->remove(aNewArc);
+ return;
+ }
+ aFeatAttributes[2*i] = aStartAttr;
+ aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aFeature[i]->attribute(aStartAttr))->pnt();
+ aFeatAttributes[2*i+1] = aEndAttr;
+ aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aFeature[i]->attribute(aEndAttr))->pnt();
+ }
+ for (int i = 0; i < aNbFeatures; i++) {
+ int j = aNbFeatures;
+ for (; j < 2 * aNbFeatures; j++)
+ if (aStartEndPnt[i]->distance(aStartEndPnt[j]) < 1.e-10) {
+ isStart[0] = i==0;
+ isStart[1] = j==aNbFeatures;
+ break;
+ }
+ if (j < 2 * aNbFeatures)
+ break;
+ }
+ // tangent directions of the features
+ for (int i = 0; i < aNbFeatures; i++) {
+ std::shared_ptr<GeomAPI_XY> aDir;
+ if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
+ aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
+ if (!isStart[i])
+ aDir = aDir->multiplied(-1.0);
+ } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
+ std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aNewFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
+ aDir = isStart[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
+ aDir = aDir->decreased(aCenterPoint->xy());
+
+ double x = aDir->x();
+ double y = aDir->y();
+ aDir->setX(-y);
+ aDir->setY(x);
+ if (!isStart[i])
+ aDir = aDir->multiplied(-1.0);
+ }
+ aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
+ }
+
+ // Wait all constraints being created, then send update events
+ static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
+ bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
+ if (isUpdateFlushed)
+ Events_Loop::loop()->setFlushed(anUpdateEvent, false);
+
+ // By default, the start point of fillet arc is connected to FeatureA,
+ // and the end point - to FeatureB. But when the angle between TangentDirA and
+ // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
+ double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A), where A and B - angles between corresponding tanget direction and the X axis
+ bool isReversed = cosBA > 0.0;
+
+ // Calculate fillet arc parameters
+ std::shared_ptr<GeomAPI_XY> aCenter, aTangentPntA, aTangentPntB;
+ calculateFilletCenter(aFeatureA, aFeatureB, aFilletRadius, isStart, aCenter, aTangentPntA, aTangentPntB);
+ // update features
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aNewFeatureA->attribute(aFeatAttributes[isStart[0] ? 0 : 1]))->setValue(
+ aTangentPntA->x(), aTangentPntA->y());
+ aNewFeatureA->execute();
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aNewFeatureB->attribute(aFeatAttributes[2 + (isStart[1] ? 0 : 1)]))->setValue(
+ aTangentPntB->x(), aTangentPntB->y());
+ aNewFeatureB->execute();
+ // update fillet arc
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aNewArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(
+ aCenter->x(), aCenter->y());
+ if (isReversed) {
+ std::shared_ptr<GeomAPI_XY> aTmp = aTangentPntA;
+ aTangentPntA = aTangentPntB;
+ aTangentPntB = aTmp;
+ }
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aNewArc->attribute(SketchPlugin_Arc::START_ID()))->setValue(aTangentPntA->x(), aTangentPntA->y());
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aNewArc->attribute(SketchPlugin_Arc::END_ID()))->setValue(aTangentPntB->x(), aTangentPntB->y());
+ aNewArc->execute();
+
+ if (needNewObjects) {
+ // attach new arc to the list
+ aRefListOfFillet->append(aNewFeatureA->lastResult());
+ aRefListOfFillet->append(aNewFeatureB->lastResult());
+ aRefListOfFillet->append(aNewArc->lastResult());
+
+ myProducedFeatures.push_back(aNewFeatureA);
+ myProducedFeatures.push_back(aNewFeatureB);
+ myProducedFeatures.push_back(aNewArc);
+
+ // Create list of additional constraints:
+ // 1. Coincidence of boundary points of features (copied lines/arcs) and fillet arc
+ // 1.1. coincidence
+ FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
+ AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
+ aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::START_ID()));
+ aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
+ int aFeatInd = isReversed ? 1 : 0;
+ int anAttrInd = (isReversed ? 2 : 0) + (isStart[isReversed ? 1 : 0] ? 0 : 1);
+ aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
+ recalculateAttributes(aNewArc, SketchPlugin_Arc::START_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
+ aConstraint->execute();
+ myProducedFeatures.push_back(aConstraint);
+ ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
+ // 1.2. coincidence
+ aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
+ aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
+ aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::END_ID()));
+ aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
+ aFeatInd = isReversed ? 0 : 1;
+ anAttrInd = (isReversed ? 0 : 2) + (isStart[isReversed ? 0 : 1] ? 0 : 1);
+ aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
+ recalculateAttributes(aNewArc, SketchPlugin_Arc::END_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
+ aConstraint->execute();
+ myProducedFeatures.push_back(aConstraint);
+ ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
+ // 2. Fillet arc radius
+ aConstraint = sketch()->addFeature(SketchPlugin_ConstraintRadius::ID());
+ aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
+ aRefAttr->setObject(aNewArc->lastResult());
+ std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+ aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aFilletRadius);
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->setValue(
+ isStart[0] ? aStartEndPnt[0] : aStartEndPnt[1]);
+ aConstraint->execute();
+ myProducedFeatures.push_back(aConstraint);
+ ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
+ // 3. Tangency of fillet arc and features
+ for (int i = 0; i < aNbFeatures; i++) {
+ aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
+ aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
+ aRefAttr->setObject(aNewArc->lastResult());
+ aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
+ bool isArc = aNewFeature[i]->getKind() == SketchPlugin_Arc::ID();
+ aRefAttr->setObject(isArc ? aNewFeature[i]->lastResult() : aNewFeature[i]->firstResult());
+ aConstraint->execute();
+ myProducedFeatures.push_back(aConstraint);
+ ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
+ }
+ // 4. Coincidence of free boundaries of base and copied features
+ for (int i = 0; i < aNbFeatures; i++) {
+ anAttrInd = 2*i + (isStart[i] ? 1 : 0);
+ aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
+ aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
+ aRefAttr->setAttr(aFeature[i]->attribute(aFeatAttributes[anAttrInd]));
+ aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
+ aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
+ myProducedFeatures.push_back(aConstraint);
+ }
+ // 5. Tangent points should be placed on the base features
+ for (int i = 0; i < aNbFeatures; i++) {
+ anAttrInd = 2*i + (isStart[i] ? 0 : 1);
+ aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
+ aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
+ aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
+ aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
+ aRefAttr->setObject(aFeature[i]->lastResult());
+ myProducedFeatures.push_back(aConstraint);
+ }
+ // make base features auxiliary
+ aFeatureA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
+ aFeatureB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
+ myBaseObjects.clear();
+ myBaseObjects.push_back(aFeatureA);
+ myBaseObjects.push_back(aFeatureB);
+ } else {
+ // Update radius value
+ int aNbSubs = sketch()->numberOfSubs();
+ FeaturePtr aSubFeature;
+ for (int aSub = 0; aSub < aNbSubs; aSub++) {
+ aSubFeature = sketch()->subFeature(aSub);
+ if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
+ continue;
+ AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
+ if (!aRefAttr || !aRefAttr->isObject())
+ continue;
+ FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
+ if (aFeature == aNewArc) {
+ AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+ aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
+ aRadius->setValue(aFilletRadius);
+ break;
+ }
+ }
+ }
+
+ // send events to update the sub-features by the solver
+ if (isUpdateFlushed)
+ Events_Loop::loop()->setFlushed(anUpdateEvent, true);
+
+ // the viewer update should be unblocked in order after the fillet features
+ // are processed by the solver
+ //aMsg = std::shared_ptr<Events_Message>(
+ // new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
+ //Events_Loop::loop()->send(aMsg);
+}
+
+void SketchPlugin_ConstraintFillet::attributeChanged(const std::string& theID)
+{
+ if (theID == SketchPlugin_Constraint::ENTITY_A() ||
+ theID == SketchPlugin_Constraint::ENTITY_B()) {
+ // clear the list of fillet entities
+ AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
+ data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
+ aRefListOfFillet->clear();
+
+ // remove all produced objects and constraints
+ DocumentPtr aDoc = sketch()->document();
+ std::list<FeaturePtr>::iterator aCIt = myProducedFeatures.begin();
+ for (; aCIt != myProducedFeatures.end(); ++aCIt)
+ aDoc->removeFeature(*aCIt);
+ myProducedFeatures.clear();
+
+ // clear auxiliary flag on initial objects
+ for (aCIt = myBaseObjects.begin(); aCIt != myBaseObjects.end(); ++aCIt)
+ (*aCIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
+ myBaseObjects.clear();
+ }