From: mpv Date: Mon, 15 Aug 2016 12:25:00 +0000 (+0300) Subject: Issue #1648: Dump Python in the High Level Parameterized Geometry API. Debug of unit... X-Git-Tag: V_2.5.0~137^2~55 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=8bb6d0093e663565a2144ca82f082effa766b12b;p=modules%2Fshaper.git Issue #1648: Dump Python in the High Level Parameterized Geometry API. Debug of unit tests with dumper procedure. --- diff --git a/src/ConstructionPlugin/CMakeLists.txt b/src/ConstructionPlugin/CMakeLists.txt index 00263fbf7..43ac4fdad 100644 --- a/src/ConstructionPlugin/CMakeLists.txt +++ b/src/ConstructionPlugin/CMakeLists.txt @@ -59,6 +59,6 @@ INCLUDE_DIRECTORIES( ADD_UNIT_TESTS(TestAxisCreation.py UnitTestAxis.py - TestPointName.py TestPoint.py + TestPointName.py TestPlane.py) diff --git a/src/Model/Model_AttributeBoolean.cpp b/src/Model/Model_AttributeBoolean.cpp index 3ebcaf953..f603def2d 100644 --- a/src/Model/Model_AttributeBoolean.cpp +++ b/src/Model/Model_AttributeBoolean.cpp @@ -14,6 +14,8 @@ void Model_AttributeBoolean::setValue(bool theValue) { Standard_Boolean aValue = theValue ? Standard_True : Standard_False; if (!myIsInitialized || myBool->Get() != aValue) { + if (myBool.IsNull()) + myBool = TDataStd_Integer::Set(myLab, 0); myBool->Set(aValue); owner()->data()->sendAttributeUpdated(this); } @@ -21,15 +23,12 @@ void Model_AttributeBoolean::setValue(bool theValue) bool Model_AttributeBoolean::value() { - return myBool->Get() == Standard_True ; + return myIsInitialized && myBool->Get() == Standard_True ; } Model_AttributeBoolean::Model_AttributeBoolean(TDF_Label& theLabel) { + myLab = theLabel; // check the attribute could be already presented in this doc (after load document) myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), myBool) == Standard_True; - if (!myIsInitialized) { - // create attribute: not initialized by value yet, just zero - myBool = TDataStd_Integer::Set(theLabel, 0); - } } diff --git a/src/Model/Model_AttributeBoolean.h b/src/Model/Model_AttributeBoolean.h index c3217b6ca..1ad1f487e 100644 --- a/src/Model/Model_AttributeBoolean.h +++ b/src/Model/Model_AttributeBoolean.h @@ -20,6 +20,7 @@ class Model_AttributeBoolean : public ModelAPI_AttributeBoolean { Handle_TDataStd_Integer myBool; ///< double is Real attribute + TDF_Label myLab; ///< if attribute is not initialized, store label here public: /// Defines the double value MODEL_EXPORT virtual void setValue(bool theValue); diff --git a/src/Model/Model_AttributeString.cpp b/src/Model/Model_AttributeString.cpp index d2bd6191a..773a8bef9 100644 --- a/src/Model/Model_AttributeString.cpp +++ b/src/Model/Model_AttributeString.cpp @@ -20,6 +20,8 @@ void Model_AttributeString::setValue(const std::string& theValue) { TCollection_ExtendedString aValue(theValue.c_str()); if (!myIsInitialized || myString->Get() != aValue) { + if (myString.IsNull()) + myString = TDataStd_Name::Set(myLab, TCollection_ExtendedString()); myString->Set(aValue); owner()->data()->sendAttributeUpdated(this); } @@ -27,15 +29,14 @@ void Model_AttributeString::setValue(const std::string& theValue) std::string Model_AttributeString::value() { + if (myString.IsNull()) + return ""; // not initialized return TCollection_AsciiString(myString->Get()).ToCString(); } Model_AttributeString::Model_AttributeString(TDF_Label& theLabel) { + myLab = theLabel; // check the attribute could be already presented in this doc (after load document) myIsInitialized = theLabel.FindAttribute(TDataStd_Name::GetID(), myString) == Standard_True; - if (!myIsInitialized) { - // create attribute: not initialized by value yet, just empty string - myString = TDataStd_Name::Set(theLabel, TCollection_ExtendedString()); - } } diff --git a/src/Model/Model_AttributeString.h b/src/Model/Model_AttributeString.h index 2d1ba634d..ac020f051 100644 --- a/src/Model/Model_AttributeString.h +++ b/src/Model/Model_AttributeString.h @@ -22,7 +22,8 @@ class Model_AttributeString : public ModelAPI_AttributeString { - Handle_TDataStd_Name myString; + Handle_TDataStd_Name myString; ///< container of the string value + TDF_Label myLab; ///< if attribute is not initialized, store label here public: /// Defines the std::string value MODEL_EXPORT virtual void setValue(const std::string& theValue); diff --git a/src/Model/Model_Expression.cpp b/src/Model/Model_Expression.cpp index c7ad95c81..1cd650101 100644 --- a/src/Model/Model_Expression.cpp +++ b/src/Model/Model_Expression.cpp @@ -76,11 +76,11 @@ Model_ExpressionDouble::Model_ExpressionDouble(TDF_Label& theLabel) : Model_Expression(theLabel) { if (!theLabel.FindAttribute(TDataStd_Real::GetID(), myReal)) { - myReal = TDataStd_Real::Set(theLabel, 0.); myIsInitialized = false; // MPV: temporarily to support the previously saved files (to check and resolve bugs), to be removed Handle(TDataStd_RealArray) anOldArray; if (theLabel.Father().FindAttribute(TDataStd_RealArray::GetID(), anOldArray) == Standard_True) { + myReal = TDataStd_Real::Set(theLabel, 0.); myReal->Set(anOldArray->Value(theLabel.Tag() - 1)); myIsInitialized = true; Handle(TDataStd_ExtStringArray) anOldExp; @@ -91,6 +91,7 @@ Model_ExpressionDouble::Model_ExpressionDouble(TDF_Label& theLabel) Handle(TDataStd_Real) anOldReal; if (theLabel.Father().FindAttribute(TDataStd_Real::GetID(), anOldReal)) { myIsInitialized = true; + myReal = TDataStd_Real::Set(theLabel, 0.); myReal->Set(anOldReal->Get()); Handle(TDataStd_Name) aText; if (theLabel.Father().FindAttribute(TDataStd_Name::GetID(), aText)) { @@ -104,27 +105,33 @@ Model_ExpressionDouble::Model_ExpressionDouble(TDF_Label& theLabel) void Model_ExpressionDouble::setValue(const double theValue) { - if (value() != theValue) + if (!myIsInitialized) { + myReal = TDataStd_Real::Set(myText->Label(), theValue); + myIsInitialized = true; + } else if (value() != theValue) { myReal->Set(theValue); + } } double Model_ExpressionDouble::value() { - return myReal->Get(); + if (myIsInitialized) + return myReal->Get(); + return -1.; // error } void Model_ExpressionDouble::setInvalid(const bool theFlag) { if (theFlag) { - TDataStd_UAttribute::Set(myReal->Label(), kInvalidGUID); + TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID); } else { - myReal->Label().ForgetAttribute(kInvalidGUID); + myText->Label().ForgetAttribute(kInvalidGUID); } } bool Model_ExpressionDouble::isInvalid() { - return myReal->Label().IsAttribute(kInvalidGUID) == Standard_True; + return myText->Label().IsAttribute(kInvalidGUID) == Standard_True; } @@ -132,7 +139,6 @@ Model_ExpressionInteger::Model_ExpressionInteger(TDF_Label& theLabel) : Model_Expression(theLabel) { if (!theLabel.FindAttribute(TDataStd_Integer::GetID(), myInteger)) { - myInteger = TDataStd_Integer::Set(theLabel, 0); myIsInitialized = false; } else myIsInitialized = true; @@ -140,25 +146,31 @@ Model_ExpressionInteger::Model_ExpressionInteger(TDF_Label& theLabel) void Model_ExpressionInteger::setValue(const int theValue) { - if (value() != theValue) + if (!myIsInitialized) { + myInteger = TDataStd_Integer::Set(myText->Label(), theValue); + myIsInitialized = true; + } else if (value() != theValue) { myInteger->Set(theValue); + } } int Model_ExpressionInteger::value() { - return myInteger->Get(); + if (myIsInitialized) + return myInteger->Get(); + return -1; // error } void Model_ExpressionInteger::setInvalid(const bool theFlag) { if (theFlag) { - TDataStd_UAttribute::Set(myInteger->Label(), kInvalidGUID); + TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID); } else { - myInteger->Label().ForgetAttribute(kInvalidGUID); + myText->Label().ForgetAttribute(kInvalidGUID); } } bool Model_ExpressionInteger::isInvalid() { - return myInteger->Label().IsAttribute(kInvalidGUID) == Standard_True; + return myText->Label().IsAttribute(kInvalidGUID) == Standard_True; } diff --git a/src/ModelAPI/Test/TestDoubleArray.py b/src/ModelAPI/Test/TestDoubleArray.py index f546ea4a9..88a1099c2 100644 --- a/src/ModelAPI/Test/TestDoubleArray.py +++ b/src/ModelAPI/Test/TestDoubleArray.py @@ -28,6 +28,3 @@ assert(math.fabs(aFeatureData.realArray("double_array").value(1) - 1.5) < 10 ** #========================================================================= # End of test #========================================================================= - -import model -assert(model.checkPythonDump()) diff --git a/src/ModelAPI/Test/TestIntArray.py b/src/ModelAPI/Test/TestIntArray.py index 66a1a0a21..879e6ef60 100755 --- a/src/ModelAPI/Test/TestIntArray.py +++ b/src/ModelAPI/Test/TestIntArray.py @@ -21,14 +21,15 @@ aSession = ModelAPI_Session.get() aPartSet = aSession.moduleDocument() aSession.startOperation() aSketchFeature = featureToCompositeFeature(aPartSet.addFeature("Sketch")) +aXOYPlane = objectToResult(aPartSet.objectByName("Construction", "XOY")) +aSketchFeature.selection("External").setValue(aXOYPlane, None) aFeatureData = aSketchFeature.data() anArray = aFeatureData.addAttribute("IntArray_1", "IntArray") aFeatureData.intArray("IntArray_1").setSize(5) +aSession.finishOperation() + assert(aFeatureData.intArray("IntArray_1").size() == 5) #========================================================================= # End of test #========================================================================= - -import model -assert(model.checkPythonDump()) diff --git a/src/ModelHighAPI/ModelHighAPI_Services.cpp b/src/ModelHighAPI/ModelHighAPI_Services.cpp index 80f466e5e..c1c6e64b3 100644 --- a/src/ModelHighAPI/ModelHighAPI_Services.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Services.cpp @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include @@ -70,6 +72,13 @@ std::string defaultPlane(const std::shared_ptr& theOrigin, return std::string(); } +std::shared_ptr standardPlane(const std::string & theName){ + DocumentPtr aPartSet = ModelAPI_Session::get()->moduleDocument(); + // searching for the construction element + return std::dynamic_pointer_cast( + aPartSet->objectByName(ModelAPI_ResultConstruction::group(), theName)); +} + //-------------------------------------------------------------------------------------- void begin() { diff --git a/src/ModelHighAPI/ModelHighAPI_Services.h b/src/ModelHighAPI/ModelHighAPI_Services.h index fa500e5de..4e719429d 100644 --- a/src/ModelHighAPI/ModelHighAPI_Services.h +++ b/src/ModelHighAPI/ModelHighAPI_Services.h @@ -17,6 +17,7 @@ class GeomAPI_Ax3; class GeomAPI_Dir; class GeomAPI_Pnt; class ModelAPI_Document; +class ModelAPI_Result; //-------------------------------------------------------------------------------------- /// Return the main document (the Partset) created or open from the Modeler. MODELHIGHAPI_EXPORT @@ -43,6 +44,13 @@ std::string defaultPlane(const std::shared_ptr& theOrigin, const std::shared_ptr& theNormal, const std::shared_ptr& theDirX); +/** Return one of the three standard results defined in PartSet document. + * + * These planes are respectively referred to by name "XOY" (Z=0), "XOZ" (Y=0) or "YOZ" (X=0). + */ +MODELHIGHAPI_EXPORT +std::shared_ptr standardPlane(const std::string & theName); + /** Start a data structure transaction. * * Make a control point for being able to discard or undo diff --git a/src/ModelHighAPI/ModelHighAPI_Tools.cpp b/src/ModelHighAPI/ModelHighAPI_Tools.cpp index 8923fd6e1..ca56b3712 100644 --- a/src/ModelHighAPI/ModelHighAPI_Tools.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Tools.cpp @@ -43,6 +43,9 @@ #include +// Have to be included before std headers +#include + #include #include @@ -351,7 +354,10 @@ bool checkPythonDump() // close all before importation of the script aSession->closeAll(); // execute the dumped - Config_ModuleReader::loadScript("check_dump"); + PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */ + PyObject* PyFileObject = PyFile_FromString("./check_dump.py", "r"); + PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "./check_dump.py", 1); + PyGILState_Release(gstate); /* release python thread */ // compare with the stored data anError = storeFeatures( diff --git a/src/PythonAPI/model/services/__init__.py b/src/PythonAPI/model/services/__init__.py index 480f8e7e5..cf9636106 100644 --- a/src/PythonAPI/model/services/__init__.py +++ b/src/PythonAPI/model/services/__init__.py @@ -2,7 +2,7 @@ """ from ModelHighAPI import moduleDocument, activeDocument -from ModelHighAPI import defaultPlane +from ModelHighAPI import defaultPlane, standardPlane from ModelHighAPI import begin, end from ModelHighAPI import apply as do from ModelHighAPI import undo, redo diff --git a/src/SketchAPI/SketchAPI_Sketch.cpp b/src/SketchAPI/SketchAPI_Sketch.cpp index 137671cde..9d82de1ae 100644 --- a/src/SketchAPI/SketchAPI_Sketch.cpp +++ b/src/SketchAPI/SketchAPI_Sketch.cpp @@ -72,6 +72,16 @@ SketchAPI_Sketch::SketchAPI_Sketch( } } +SketchAPI_Sketch::SketchAPI_Sketch( + const std::shared_ptr & theFeature, + std::shared_ptr thePlaneObject) +: ModelHighAPI_Interface(theFeature) +{ + if (initialize()) { + setExternal(thePlaneObject); + } +} + SketchAPI_Sketch::~SketchAPI_Sketch() { @@ -100,6 +110,13 @@ void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal) execute(); } +void SketchAPI_Sketch::setExternal(std::shared_ptr thePlaneObject) +{ + ResultPtr aRes = std::dynamic_pointer_cast(thePlaneObject); + ModelHighAPI_Selection aSel(aRes); + setExternal(aSel); +} + //-------------------------------------------------------------------------------------- void SketchAPI_Sketch::setValue( const std::shared_ptr & theConstraint, @@ -157,6 +174,14 @@ SketchPtr addSketch(const std::shared_ptr & thePart, return SketchPtr(new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName))); } +SketchPtr addSketch(const std::shared_ptr & thePart, + std::shared_ptr thePlaneObject) +{ + std::shared_ptr aFeature = thePart->addFeature(SketchAPI_Sketch::ID()); + return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject)); +} + + //-------------------------------------------------------------------------------------- std::shared_ptr SketchAPI_Sketch::addPoint( double theX, double theY) @@ -637,24 +662,34 @@ void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const // Check the plane is coordinate plane std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX); - if (aPlaneName.empty()) { - // needs import additional module - theDumper.importModule("GeomAPI"); - // dump plane parameters - const std::string& aSketchName = theDumper.name(aBase); - std::string anOriginName = aSketchName + "_origin"; - std::string aNormalName = aSketchName + "_norm"; - std::string aDirXName = aSketchName + "_dirx"; - theDumper << anOriginName << " = " << anOrigin << std::endl - << aNormalName << " = " << aNormal << std::endl - << aDirXName << " = " << aDirX << std::endl; - // dump sketch based on arbitrary plane - theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3(" - << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl; + if (anExternal->context()) { // checking for selected planes + if (!aPlaneName.empty()) { + // dump sketch based on coordinate plane + theDumper << aBase << " = model.addSketch(" << aDocName + << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl; + } else { // some other plane + theDumper << aBase << " = model.addSketch(" << aDocName << ", " << anExternal<< ")" << std::endl; + } } else { - // dump sketch based on coordinate plane - theDumper << aBase << " = model.addSketch(" << aDocName - << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl; + if (aPlaneName.empty()) { + // needs import additional module + theDumper.importModule("GeomAPI"); + // dump plane parameters + const std::string& aSketchName = theDumper.name(aBase); + std::string anOriginName = aSketchName + "_origin"; + std::string aNormalName = aSketchName + "_norm"; + std::string aDirXName = aSketchName + "_dirx"; + theDumper << anOriginName << " = " << anOrigin << std::endl + << aNormalName << " = " << aNormal << std::endl + << aDirXName << " = " << aDirX << std::endl; + // dump sketch based on arbitrary plane + theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3(" + << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl; + } else { + // dump sketch based on coordinate plane + theDumper << aBase << " = model.addSketch(" << aDocName + << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl; + } } } diff --git a/src/SketchAPI/SketchAPI_Sketch.h b/src/SketchAPI/SketchAPI_Sketch.h index dcf06446d..545095521 100644 --- a/src/SketchAPI/SketchAPI_Sketch.h +++ b/src/SketchAPI/SketchAPI_Sketch.h @@ -53,6 +53,10 @@ public: SKETCHAPI_EXPORT SketchAPI_Sketch(const std::shared_ptr & theFeature, const ModelHighAPI_Selection & theExternal); + /// Constructor with values + SKETCHAPI_EXPORT + SketchAPI_Sketch(const std::shared_ptr & theFeature, + std::shared_ptr thePlaneObject); /// Destructor SKETCHAPI_EXPORT virtual ~SketchAPI_Sketch(); @@ -75,6 +79,10 @@ public: SKETCHAPI_EXPORT void setExternal(const ModelHighAPI_Selection & theExternal); + /// Set external + SKETCHAPI_EXPORT + void setExternal(std::shared_ptr thePlaneObject); + /// Add point SKETCHAPI_EXPORT std::shared_ptr addPoint( @@ -384,6 +392,13 @@ SKETCHAPI_EXPORT SketchPtr addSketch(const std::shared_ptr & thePart, const std::string & theExternalName); +/**\ingroup CPPHighAPI + * \brief Create Sketch feature + */ +SKETCHAPI_EXPORT +SketchPtr addSketch(const std::shared_ptr & thePart, + std::shared_ptr thePlaneObject); + //-------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------- #endif /* SRC_SKETCHAPI_SKETCHAPI_SKETCH_H_ */