Salome HOME
Issue #2120 - Crash when creating an arc passing through the arc connected to both...
[modules/shaper.git] / src / PartSet / PartSet_ExternalObjectsMgr.cpp
index df63169bcca030b776758ccd2dfbd51b25876f9a..bc0412047a433984eea80cc684ffd4624fc4a983 100755 (executable)
 #include <XGUI_Workshop.h>
 #include <XGUI_ModuleConnector.h>
 
+#include <ModuleBase_ViewerPrs.h>
+#include <ModuleBase_ISelection.h>
+
 #include <SketchPlugin_Feature.h>
 
 #include <QString>
 
-PartSet_ExternalObjectsMgr::PartSet_ExternalObjectsMgr(const std::string& theExternal, const bool theDefaultValue)
-: myUseExternal(theDefaultValue)
+PartSet_ExternalObjectsMgr::PartSet_ExternalObjectsMgr(const std::string& theExternal,
+                                                       const std::string& theCanCreateExternal,
+                                                       const bool theDefaultValue)
+: myUseExternal(theDefaultValue), myCanCreateExternal(true)
 {
   QString aIsExternal(theExternal.c_str());
   if (!aIsExternal.isEmpty()) {
     QString aStr = aIsExternal.toUpper();
-    myUseExternal = (aStr == "TRUE") || (aStr == "YES"); 
+    myUseExternal = (aStr == "TRUE") || (aStr == "YES");
+  }
+
+  QString aCanCreateExternal(theCanCreateExternal.c_str());
+  if (!aCanCreateExternal.isEmpty()) {
+    QString aStr = aCanCreateExternal.toUpper();
+    myCanCreateExternal = (aStr == "TRUE") || (aStr == "YES");
   }
 }
 
@@ -29,7 +40,7 @@ bool PartSet_ExternalObjectsMgr::isValidObject(const ObjectPtr& theObject)
   bool aValid = true;
   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
   // Do check using of external feature
-  std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
+  std::shared_ptr<SketchPlugin_Feature> aSPFeature =
           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
 
   // Do check that we can use external feature
@@ -47,37 +58,65 @@ ObjectPtr PartSet_ExternalObjectsMgr::externalObject(const ObjectPtr& theSelecte
 {
   ObjectPtr aSelectedObject = PartSet_Tools::findFixedObjectByExternal(
                                   theShape->impl<TopoDS_Shape>(), theSelectedObject, theSketch);
+  FeaturePtr aFeature = ModelAPI_Feature::feature(aSelectedObject);
+  if (aFeature.get()) {
+    std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+                            std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
+    /// some sketch entities should be never shown, e.g. projection feature
+    /// such external features should not be used in selection
+    if (aSketchFeature.get() && !aSketchFeature->canBeDisplayed())
+      return ObjectPtr();
+  }
+
   if (!aSelectedObject.get()) {
     // Processing of external (non-sketch) object
     aSelectedObject = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
                                                     theSelectedObject, theSketch, theTemporary);
-    if (aSelectedObject.get()) {
-      if (theTemporary)
+    if (aSelectedObject.get() && theTemporary)
         myExternalObjectValidated = aSelectedObject;
-      else
-        myExternalObjects.append(aSelectedObject);
-    }
   }
   return aSelectedObject;
 }
 
-//********************************************************************
-/*ObjectPtr PartSet_ExternalObjectsMgr::externalObjectValidated(const ObjectPtr& theSelectedObject,
-                                                     const GeomShapePtr& theShape,
-                                                     const CompositeFeaturePtr& theSketch)
+void PartSet_ExternalObjectsMgr::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
+                                                   ObjectPtr& theObject,
+                                                   GeomShapePtr& theShape,
+                                                   ModuleBase_IWorkshop* theWorkshop,
+                                                   const CompositeFeaturePtr& theSketch,
+                                                   const bool isInValidate)
 {
-  // TODO(nds): unite with externalObject()
-  ObjectPtr aSelectedObject = PartSet_Tools::findFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
-                                                             theSelectedObject, theSketch);
-  if (!aSelectedObject.get()) {
-    // Processing of external (non-sketch) object
-    aSelectedObject = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
-                                                                 theSelectedObject, theSketch);
-    if (aSelectedObject.get())
-      myExternalObjectValidated = aSelectedObject;
+  FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
+  std::shared_ptr<SketchPlugin_Feature> aSPFeature =
+          std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
+  // there is no a sketch feature is selected, but the shape exists,
+  // try to create an exernal object
+  // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
+  if (aSPFeature.get() == NULL) {
+    ObjectPtr anExternalObject = ObjectPtr();
+    GeomShapePtr anExternalShape = GeomShapePtr();
+    if (useExternal()) {
+      if (canCreateExternal()) {
+        GeomShapePtr aShape = theShape;
+        if (!aShape.get()) {
+          ResultPtr aResult = theWorkshop->selection()->getResult(thePrs);
+          if (aResult.get())
+            aShape = aResult->shape();
+        }
+        if (aShape.get() != NULL && !aShape->isNull())
+          anExternalObject =
+            externalObject(theObject, aShape, theSketch, isInValidate);
+      }
+      else { /// use objects of found selection
+        anExternalObject = theObject;
+        anExternalShape = theShape;
+      }
+    }
+    /// the object is null if the selected feature is "external"(not sketch entity feature of the
+    /// current sketch) and it is not created by object manager
+    theObject = anExternalObject;
+    theShape = anExternalShape;
   }
-  return aSelectedObject;
-}*/
+}
 
 //********************************************************************
 void PartSet_ExternalObjectsMgr::removeExternal(const CompositeFeaturePtr& theSketch,
@@ -87,50 +126,10 @@ void PartSet_ExternalObjectsMgr::removeExternal(const CompositeFeaturePtr& theSk
 {
   if (theTemporary)
     removeExternalObject(myExternalObjectValidated, theSketch, theFeature, theWorkshop);
-  else{
-    QObjectPtrList::const_iterator anIt = myExternalObjects.begin(), aLast = myExternalObjects.end();
-    for (; anIt != aLast; anIt++) {
-      ObjectPtr anObject = *anIt;
-      removeExternalObject(anObject, theSketch, theFeature, theWorkshop);
-    }
-    myExternalObjects.clear();
-  }
 }
 
-//********************************************************************
-void PartSet_ExternalObjectsMgr::removeUnusedExternalObjects(const QObjectPtrList& theIgnoreObjects,
-                                                             const CompositeFeaturePtr& theSketch,
-                                                             const FeaturePtr& theFeature)
-{
-  /*
-  // TODO(nds): unite with removeExternal(), remove parameters
-  QObjectPtrList aUsedExternalObjects;
-
-  QObjectPtrList::const_iterator anIt = myExternalObjects.begin(), aLast = myExternalObjects.end();
-  for (; anIt != aLast; anIt++) {
-    ObjectPtr anObject = *anIt;
-    if (theIgnoreObjects.contains(anObject))
-      aUsedExternalObjects.append(anObject);
-    else
-      removeExternalObject(anObject, theSketch, theFeature);
-  }*/
-  myExternalObjects.clear();
-  //if (!aUsedExternalObjects.empty())
-  //  myExternalObjects = aUsedExternalObjects;
-}
-
-//********************************************************************
-/*void PartSet_ExternalObjectsMgr::removeExternalValidated(const CompositeFeaturePtr& theSketch,
-                                                         const FeaturePtr& theFeature,
-                                                         ModuleBase_IWorkshop* theWorkshop)
-{
-  // TODO(nds): unite with removeExternal(), remove parameters
-  removeExternalObject(myExternalObjectValidated, theSketch, theFeature, theWorkshop);
-  myExternalObjectValidated = ObjectPtr();
-}*/
-
 void PartSet_ExternalObjectsMgr::removeExternalObject(const ObjectPtr& theObject,
-                                                      const CompositeFeaturePtr& theSketch,
+                                                      const CompositeFeaturePtr& /*theSketch*/,
                                                       const FeaturePtr& theFeature,
                                                       ModuleBase_IWorkshop* theWorkshop)
 {
@@ -140,14 +139,9 @@ void PartSet_ExternalObjectsMgr::removeExternalObject(const ObjectPtr& theObject
     if (aFeature.get() != NULL) {
       QObjectPtrList anObjects;
       anObjects.append(aFeature);
-      // the external feature should be removed with all references, sketch feature should be ignored
-      std::set<FeaturePtr> anIgnoredFeatures;
-      // the current feature should be ignored, because it can use the external feature in the
-      // attributes and, therefore have a references to it. So, the delete functionality tries
-      // to delete this feature. Test case is creation of a constraint on external point,
-      // use in this control after an external point, the point of the sketch.
-      anIgnoredFeatures.insert(theFeature);
-      workshop(theWorkshop)->deleteFeatures(anObjects, anIgnoredFeatures);
+      // the external feature should be removed with all references,
+      // composite sketch feature will be ignored
+      workshop(theWorkshop)->deleteFeatures(anObjects);
     }
   }
 }