Salome HOME
Issue #1648: Dump Python in the High Level Parameterized Geometry API. Debug of unit...
authormpv <mpv@opencascade.com>
Mon, 15 Aug 2016 12:25:00 +0000 (15:25 +0300)
committermpv <mpv@opencascade.com>
Mon, 15 Aug 2016 12:25:00 +0000 (15:25 +0300)
14 files changed:
src/ConstructionPlugin/CMakeLists.txt
src/Model/Model_AttributeBoolean.cpp
src/Model/Model_AttributeBoolean.h
src/Model/Model_AttributeString.cpp
src/Model/Model_AttributeString.h
src/Model/Model_Expression.cpp
src/ModelAPI/Test/TestDoubleArray.py
src/ModelAPI/Test/TestIntArray.py
src/ModelHighAPI/ModelHighAPI_Services.cpp
src/ModelHighAPI/ModelHighAPI_Services.h
src/ModelHighAPI/ModelHighAPI_Tools.cpp
src/PythonAPI/model/services/__init__.py
src/SketchAPI/SketchAPI_Sketch.cpp
src/SketchAPI/SketchAPI_Sketch.h

index 00263fbf70190cffeba5d3997700e723ed504173..43ac4fdadeb1581698497044086c1471849f68e7 100644 (file)
@@ -59,6 +59,6 @@ INCLUDE_DIRECTORIES(
 
 ADD_UNIT_TESTS(TestAxisCreation.py
                UnitTestAxis.py
-               TestPointName.py
                TestPoint.py
+               TestPointName.py
                TestPlane.py)
index 3ebcaf953db0f6a17d9460084274f5cf116628dd..f603def2d681f8ffbea1fa3b2cf146d286fce82c 100644 (file)
@@ -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);
-  }
 }
index c3217b6ca7f6112b099aa2334a92beb1bd9b90a4..1ad1f487e170854ef9f1bdf06a076294ceb8aeb4 100644 (file)
@@ -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);
index d2bd6191aa87a8aa9dea0864c05792a5c776c577..773a8bef919fbeaf61988b4080f8da2512d0e13b 100644 (file)
@@ -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());
-  }
 }
index 2d1ba634dea54673f8a62b93e388d4b1413c463d..ac020f051cb82c2a88c0e66cb18fd34731e78b90 100644 (file)
@@ -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);
index c7ad95c8188ee116a3fc086cbe8ac490a2d61dbc..1cd6501014edb81131e4bdf32e939248ed2c61af 100644 (file)
@@ -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;
 }
index f546ea4a9e4bb77d0e41d296f5ad9f4057b4684f..88a1099c20e7241515a50d8bf511e82f43c4ec48 100644 (file)
@@ -28,6 +28,3 @@ assert(math.fabs(aFeatureData.realArray("double_array").value(1) - 1.5) < 10 **
 #=========================================================================
 # End of test
 #=========================================================================
-
-import model
-assert(model.checkPythonDump())
index 66a1a0a2198ba803c36b494bc91d050d42beff15..879e6ef6022cca7d75b9aea790e6255fc6f9835d 100755 (executable)
@@ -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())
index 80f466e5e4568f11685293587b84eef39aed9382..c1c6e64b3a1d1f5f4df34ccf2f12bf2f40c0a0ee 100644 (file)
@@ -10,6 +10,8 @@
 #include <GeomAPI_Ax3.h>
 #include <GeomAPI_Pnt.h>
 #include <ModelAPI_Session.h>
+#include <ModelAPI_Document.h>
+#include <ModelAPI_ResultConstruction.h>
 
 #include <cmath>
 
@@ -70,6 +72,13 @@ std::string defaultPlane(const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
   return std::string();
 }
 
+std::shared_ptr<ModelAPI_Result> standardPlane(const std::string & theName){
+  DocumentPtr aPartSet = ModelAPI_Session::get()->moduleDocument();
+  // searching for the construction element
+  return std::dynamic_pointer_cast<ModelAPI_Result>(
+    aPartSet->objectByName(ModelAPI_ResultConstruction::group(), theName));
+}
+
 //--------------------------------------------------------------------------------------
 void begin()
 {
index fa500e5de5bfede57dc724de4a8673c8d82d3e55..4e719429dc984aa4c72ce4c5153062d709a089d1 100644 (file)
@@ -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<GeomAPI_Pnt>& theOrigin,
                          const std::shared_ptr<GeomAPI_Dir>& theNormal,
                          const std::shared_ptr<GeomAPI_Dir>& 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<ModelAPI_Result> standardPlane(const std::string & theName);
+
 /** Start a data structure transaction.
  *
  *  Make a control point for being able to discard or undo
index 8923fd6e13bf01788622a586a564361eccb44709..ca56b3712df2f167df528aa1c69361351e5336e9 100644 (file)
@@ -43,6 +43,9 @@
 
 #include <Events_InfoMessage.h>
 
+// Have to be included before std headers
+#include <Python.h>
+
 #include <algorithm>
 #include <iostream>
 
@@ -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(
index 480f8e7e59ae6d86f337d8ae2fc366d5e7da707f..cf963610652599bb6a658c4c4504fe8f456e2004 100644 (file)
@@ -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
index 137671cde4b89249bfedb563276c0957fe396b9c..9d82de1aeb2d15a85a434de8dad4b19e32ec4da1 100644 (file)
@@ -72,6 +72,16 @@ SketchAPI_Sketch::SketchAPI_Sketch(
   }
 }
 
+SketchAPI_Sketch::SketchAPI_Sketch(
+    const std::shared_ptr<ModelAPI_Feature> & theFeature,
+    std::shared_ptr<ModelAPI_Object> 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<ModelAPI_Object> thePlaneObject)
+{
+  ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
+  ModelHighAPI_Selection aSel(aRes);
+  setExternal(aSel);
+}
+
 //--------------------------------------------------------------------------------------
 void SketchAPI_Sketch::setValue(
     const std::shared_ptr<ModelAPI_Feature> & theConstraint,
@@ -157,6 +174,14 @@ SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
   return SketchPtr(new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
 }
 
+SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
+                    std::shared_ptr<ModelAPI_Object> thePlaneObject)
+{
+  std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
+  return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
+}
+
+
 //--------------------------------------------------------------------------------------
 std::shared_ptr<SketchAPI_Point> 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;
+      }
     }
   }
 
index dcf06446dc2d42b4dd03a17e5775e1102cf7b65a..545095521762a3209bf3e5306123323bbaa9db17 100644 (file)
@@ -53,6 +53,10 @@ public:
   SKETCHAPI_EXPORT
   SketchAPI_Sketch(const std::shared_ptr<ModelAPI_Feature> & theFeature,
                    const ModelHighAPI_Selection & theExternal);
+  /// Constructor with values
+  SKETCHAPI_EXPORT
+  SketchAPI_Sketch(const std::shared_ptr<ModelAPI_Feature> & theFeature,
+                   std::shared_ptr<ModelAPI_Object> 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<ModelAPI_Object> thePlaneObject);
+
   /// Add point
   SKETCHAPI_EXPORT
   std::shared_ptr<SketchAPI_Point> addPoint(
@@ -384,6 +392,13 @@ SKETCHAPI_EXPORT
 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
                     const std::string & theExternalName);
 
+/**\ingroup CPPHighAPI
+ * \brief Create Sketch feature
+ */
+SKETCHAPI_EXPORT
+SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
+                    std::shared_ptr<ModelAPI_Object> thePlaneObject);
+
 //--------------------------------------------------------------------------------------
 //--------------------------------------------------------------------------------------
 #endif /* SRC_SKETCHAPI_SKETCHAPI_SKETCH_H_ */