-// Copyright (C) 2014-2019 CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023 CEA, EDF
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
{
+ // It is necessary to keep and restore the current feature in the document,
+ // if the sketch is updated from Python API. Because in that case, the current feature
+ // may be a non-sketch feature, so it is required to set it back, after adding a sketch feature,
+ // to keep the sequence of non-sketch features within the document.
+ FeaturePtr aCurFeature = document()->currentFeature(false);
+ std::shared_ptr<SketchPlugin_Feature> aCurSketchFeature =
+ std::dynamic_pointer_cast<SketchPlugin_Feature>(aCurFeature);
+ std::shared_ptr<SketchPlugin_Sketch> aCurSketch =
+ std::dynamic_pointer_cast<SketchPlugin_Sketch>(aCurFeature);
+ if ((aCurSketch && aCurSketch.get() == this) ||
+ (aCurSketchFeature && aCurSketchFeature->sketch() == this))
+ aCurFeature = FeaturePtr(); // no need to restore feature if it is from the current sketch
+
// 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
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);
+
+ // set as current also after it becomes sub to set correctly enabled for other sketch subs
+ // or restore the previous current feature
+ document()->setCurrentFeature(aCurFeature ? aCurFeature : aNew, false);
return aNew;
}
std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
+ bool aWasBlocked = data()->blockSendAttributeUpdated(true);
// update position of the sketch
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()));
aDirX->setValue(aXDir);
- std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
+ data()->blockSendAttributeUpdated(aWasBlocked, true);
}
}
}
theFeature->setResult(aResult, theIndex);
}
+void SketchPlugin_Sketch::createLine2DResult(ModelAPI_Feature* theFeature,
+ SketchPlugin_Sketch* theSketch,
+ const std::string& theStartAttrID,
+ const std::string& theEndAttrID,
+ const int theIndex)
+{
+ std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->attribute(theStartAttrID));
+ std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->attribute(theEndAttrID));
+
+ if (!aStartAttr || !aStartAttr->isInitialized() ||
+ !anEndAttr || !anEndAttr->isInitialized())
+ return;
+
+ std::shared_ptr<GeomAPI_Pnt> aStart(theSketch->to3D(aStartAttr->x(), aStartAttr->y()));
+ std::shared_ptr<GeomAPI_Pnt> anEnd(theSketch->to3D(anEndAttr->x(), anEndAttr->y()));
+ //std::cout<<"Execute line "<<aStart->x()<<" "<<aStart->y()<<" "<<aStart->z()<<" - "
+ // <<anEnd->x()<<" "<<anEnd->y()<<" "<<anEnd->z()<<std::endl;
+ // make linear edge
+ std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
+ // store the result
+ std::shared_ptr<ModelAPI_ResultConstruction> aResult =
+ theFeature->document()->createConstruction(theFeature->data(), theIndex);
+ aResult->setShape(anEdge);
+ aResult->setIsInHistory(false);
+ theFeature->setResult(aResult, theIndex);
+}
+
FeaturePtr SketchPlugin_Sketch::addUniqueNamedCopiedFeature(FeaturePtr theFeature,
SketchPlugin_Sketch* theSketch,
const bool theIsCopy)
{
FeaturePtr aNewFeature = theSketch->addFeature(theFeature->getKind());
// addFeature generates a unique name for the feature, it caches the name
- std::string aUniqueFeatureName = aNewFeature->data()->name();
+ std::wstring aUniqueFeatureName = aNewFeature->data()->name();
// all attribute values are copied\pasted to the new feature, name is not an exception
theFeature->data()->copyTo(aNewFeature->data());
// external state should not be copied as a new object is an object of the current sketch
std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
aData->attribute(SketchPlugin_Sketch::NORM_ID()));
- return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(anOrigin->pnt(), aDirX->dir(), aNorm->dir()));
+ if (aNorm.get() && aNorm->isInitialized() && anOrigin.get() && anOrigin->isInitialized())
+ return std::shared_ptr<GeomAPI_Ax3>(
+ new GeomAPI_Ax3(anOrigin->pnt(), aDirX->dir(), aNorm->dir()));
+
+ return std::shared_ptr<GeomAPI_Ax3>();
}
bool SketchPlugin_Sketch::customAction(const std::string& theActionId)
return;
DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
- ObjectPtr anOX = aRootDoc->objectByName(ModelAPI_ResultConstruction::group(), "OX");
- ObjectPtr anOY = aRootDoc->objectByName(ModelAPI_ResultConstruction::group(), "OY");
- ObjectPtr anOZ = aRootDoc->objectByName(ModelAPI_ResultConstruction::group(), "OZ");
+ ObjectPtr anOX = aRootDoc->objectByName(ModelAPI_ResultConstruction::group(), L"OX");
+ ObjectPtr anOY = aRootDoc->objectByName(ModelAPI_ResultConstruction::group(), L"OY");
+ ObjectPtr anOZ = aRootDoc->objectByName(ModelAPI_ResultConstruction::group(), L"OZ");
AttributeSelectionPtr anExtFeature;
if (aFeature->getKind() == SketchPlugin_Projection::ID())