+ data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::typeId());
+ data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::typeId());
+ data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::typeId());
+ data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
+ // the selected face, base for the sketcher plane, not obligatory
+ data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(),
+ ModelAPI_AttributeSelection::typeId());
+ ModelAPI_Session::get()->validators()->registerNotObligatory(
+ getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
+ data()->addAttribute(SketchPlugin_Sketch::SOLVER_ERROR(), ModelAPI_AttributeString::typeId());
+ ModelAPI_Session::get()->validators()->registerNotObligatory(
+ getKind(), SketchPlugin_Sketch::SOLVER_ERROR());
+ data()->addAttribute(SketchPlugin_Sketch::SOLVER_DOF(), ModelAPI_AttributeString::typeId());
+ ModelAPI_Session::get()->validators()->registerNotObligatory(
+ getKind(), SketchPlugin_Sketch::SOLVER_DOF());
+}
+
+void SketchPlugin_Sketch::execute()
+{
+ if (!data()->isValid())
+ return;
+ std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
+ ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
+
+ std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
+ data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
+ std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+ data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
+ std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+ data()->attribute(SketchPlugin_Sketch::NORM_ID()));
+
+ std::list<ObjectPtr> aFeatures = aRefList->list();
+ if (aFeatures.empty()) // actually, this must be avoided by the validators
+ return;
+
+ std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
+ std::shared_ptr<SketchPlugin_Feature> aFeature;
+ std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
+ for (; anIt != aLast; anIt++) {
+ aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
+ if (aFeature) {
+ if (!aFeature->sketch()) // on load document the back references are missed
+ aFeature->setSketch(this);
+ // do not include into the result the external edges with disabled flag "Include into result"
+ if (aFeature->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID())) {
+ if (aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->context()) {
+ const std::string& anAttrName =
+ aFeature->getKind() == SketchPlugin_Projection::ID() ?
+ SketchPlugin_Projection::INCLUDE_INTO_RESULT() :
+ SketchPlugin_IntersectionPoint::INCLUDE_INTO_RESULT();
+ AttributeBooleanPtr aKeepResult = aFeature->boolean(anAttrName);
+ if (!aKeepResult || !aKeepResult->value())
+ continue;
+ }
+ }
+ // do not include the construction entities in the result
+ if (aFeature->data()->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID())) {
+ if (aFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
+ continue;
+ }
+
+ const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
+ std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
+ for (; aResIter != aRes.cend(); aResIter++) {
+ std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
+ ModelAPI_ResultConstruction>(*aResIter);
+ if (aConstr) {
+ std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
+ if (aShape)
+ aFeaturesPreview.push_back(aShape);
+ }
+ }
+ }
+ }
+
+ // Collect all edges as one big wire
+ std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
+ std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
+ for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
+ aBigWire->addEdge(*aShapeIt);
+ }
+ aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
+ std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
+ aConstr->setShape(aBigWire);
+ setResult(aConstr);
+}
+
+std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
+{
+ // Set last feature of the sketch as current feature.
+ // Reason: Changing of parameter through Python API may lead to creation of new features
+ // (e.g. changing number of copies in MultiRotation). If the sketch is not the last
+ // feature in the Object Browser, then new features will be added to the end feature.
+ // Therefore, setting any feature below the sketch as a current feature will disable
+ // these newly created features.
+ std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
+ ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
+ int aSize = aRefList->size(false);
+ ObjectPtr aLastObject = aSize == 0 ? data()->owner() : aRefList->object(aSize - 1, false);
+ FeaturePtr aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastObject);
+ document()->setCurrentFeature(aLastFeature, false);
+
+ // add new feature
+ std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
+ if (aNew) {
+ // the sketch cannot be specified for the macro-features defined in python
+ // like SketchRectangle, so we need to check the type of new feature
+ std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+ std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew);
+ if (aSketchFeature)
+ aSketchFeature->setSketch(this);
+ data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
+ }
+ // set as current also after it becomes sub to set correctly enabled for other sketch subs
+ document()->setCurrentFeature(aNew, false);
+
+ return aNew;
+}
+
+void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
+{
+ if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
+ return;
+ AttributeRefListPtr aList = reflist(SketchPlugin_Sketch::FEATURES_ID());
+ // if the object is last, remove it from the list
+ // (needed to skip empty transaction on edit of sketch feature)
+ if (aList->object(aList->size(true) - 1, true) == theFeature) {
+ aList->remove(theFeature);
+ } else {
+ // to keep the persistent sub-elements indexing, do not remove elements from list,
+ // but substitute by nulls
+ aList->substitute(theFeature, ObjectPtr());
+ }
+}
+
+int SketchPlugin_Sketch::numberOfSubs(bool forTree) const
+{
+ if (forTree)
+ return 0;
+ return data()->reflist(FEATURES_ID())->size(false);
+}
+
+std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(
+ const int theIndex, bool forTree)
+{
+ if (forTree)
+ return FeaturePtr();
+
+ ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex, false);
+ FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
+ return aRes;
+}
+
+int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
+{
+ std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
+ ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
+ std::list<ObjectPtr> aFeatures = aRefList->list();
+ std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
+ int aResultIndex = 1; // number of the counted (created) features, started from 1
+ int aFeatureIndex = -1; // number of the not-empty features in the list
+ for (; anIt != aFeatures.end(); anIt++) {
+ if (anIt->get())
+ aFeatureIndex++;
+ if (aFeatureIndex == theIndex)
+ break;
+ aResultIndex++;
+ }
+ return aResultIndex;
+}
+
+bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
+{
+ // check is this feature of result
+ FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
+ if (!aFeature) {
+ ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+ if (aRes)
+ aFeature = document()->feature(aRes);
+ }
+ if (aFeature) {
+ return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->isInList(aFeature);
+ }
+ return false;
+}
+
+
+static bool isOrigin(const GeomPointPtr& thePoint, const double theTolerance)
+{
+ return fabs(thePoint->x()) < theTolerance &&
+ fabs(thePoint->y()) < theTolerance &&
+ fabs(thePoint->z()) < theTolerance;