Salome HOME
Better management of errors.
[modules/shaper.git] / src / XGUI / XGUI_Selection.cpp
index 6091e71f40f739945710091312cc5aab0a7aaa2a..5af4ea4c3f4739988c0d331433bb58b6507f1b20 100644 (file)
@@ -15,6 +15,8 @@
 
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Tools.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_ResultConstruction.h>
 
 #include <AIS_InteractiveContext.hxx>
 #include <AIS_Axis.hxx>
@@ -78,15 +80,15 @@ Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPt
 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
 {
   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
-  if (!aContext.IsNull() && aContext->HasOpenedContext()) {
+  if (!aContext.IsNull()) {
     QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
       ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
 
-      if (aSelectedIds.contains((long)anOwner.Access()))
+      if (aSelectedIds.contains((long)anOwner.get()))
         continue;
-      aSelectedIds.append((long)anOwner.Access());
+      aSelectedIds.append((long)anOwner.get());
 
       fillPresentation(aPrs, anOwner);
 
@@ -121,8 +123,8 @@ void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
                                       const Handle(SelectMgr_EntityOwner)& theOwner) const
 {
   thePrs->setOwner(theOwner);
-
-  Handle(AIS_InteractiveObject) anIO = 
+  Handle(SelectMgr_SelectableObject) aSelectable = theOwner->Selectable();
+  Handle(AIS_InteractiveObject) anIO =
                            Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
   thePrs->setInteractive(anIO);
 
@@ -130,15 +132,16 @@ void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
   // for one feature
   Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
   if( !aBRO.IsNull() && aBRO->HasShape() ) {
-    // the located method is called in the context to obtain the shape by the SelectedShape() method,
-    // so the shape is located by the same rules
+    // the located method is called in the context to obtain the shape by the SelectedShape()
+    // method, so the shape is located by the same rules
     TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
 #ifndef DEBUG_DELIVERY
     if (aShape.IsNull())
       aShape = findAxisShape(anIO);
 #endif
     if (!aShape.IsNull()) {
-      std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
+      std::shared_ptr<GeomAPI_Shape> aGeomShape =
+        std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
       aGeomShape->setImpl(new TopoDS_Shape(aShape));
       thePrs->setShape(aGeomShape);
     }
@@ -146,38 +149,57 @@ void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
 #ifdef DEBUG_DELIVERY
     // Fill by trihedron shapes
     Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
+    DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
+    int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
+    ObjectPtr aObj;
     if (!aAxis.IsNull()) {
       // an Axis from Trihedron
-      Handle(Geom_Line) aLine = aAxis->Component();
-      Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
-      Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
-
-      BRep_Builder aBuilder;      
-      TopoDS_Edge aEdge;
-      aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
-      if (!aEdge.IsNull()) {
-        std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
-        aGeomShape->setImpl(new TopoDS_Shape(aEdge));
-        thePrs->setShape(aGeomShape);
+      gp_Lin aLine = aAxis->Component()->Lin();
+      gp_Dir aDir = aLine.Direction();
+      std::string aAxName;
+      if (aDir.X() == 1.)
+        aAxName = "OX";
+      else if (aDir.Y() == 1.)
+        aAxName = "OY";
+      else if (aDir.Z() == 1.)
+        aAxName = "OZ";
+      if (aAxName.length() > 0) {
+        ResultPtr aAx;
+        for (int i = 0; i < aSize; i++) {
+          aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
+          if (aObj->data()->name() == aAxName) {
+            aAx = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
+            break;
+          }
+        }
+        if (aAx.get()) {
+          thePrs->setObject(aAx);
+          thePrs->setShape(aAx->shape());
+          return;
+        }
       }
     } else {
       Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
       if (!aPoint.IsNull()) {
-        // A point from trihedron
-        Handle(Geom_Point) aPnt = aPoint->Component();
-        BRep_Builder aBuilder;
-        TopoDS_Vertex aVertex;
-        aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
-        if (!aVertex.IsNull()) {
-          std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
-          aGeomShape->setImpl(new TopoDS_Shape(aVertex));
-          thePrs->setShape(aGeomShape);
+        // An origin point from trihedron
+        ResultPtr aOrigin;
+        for (int i = 0; i < aSize; i++) {
+          aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
+          if (aObj->data()->name() == "Origin") {
+            aOrigin = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
+            break;
+          }
+        }
+        if (aOrigin.get()) {
+          thePrs->setObject(aOrigin);
+          thePrs->setShape(aOrigin->shape());
+          return;
         }
       }
     }
 #endif
   }
-     
+
   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
   ObjectPtr aFeature = aDisplayer->getObject(anIO);
 
@@ -210,34 +232,25 @@ QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
   for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
-    ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
-    Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
-    if (aSelectedIds.contains((long)anIO.Access()))
-      continue;
-    
-    aSelectedIds.append((long)anIO.Access());
-    aPrs->setInteractive(anIO);
-
-    ObjectPtr aResult = aDisplayer->getObject(anIO);
-    // we should not check the appearance of this feature because there can be some selected shapes
-    // for one feature
-    aPrs->setObject(aResult);
-    if (aContext->HasOpenedContext()) {
-      TopoDS_Shape aShape = aContext->DetectedShape();
-      if (!aShape.IsNull()) {
-        std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
-        aGeomShape->setImpl(new TopoDS_Shape(aShape));
-        aPrs->setShape(aGeomShape);
-      }
+    Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
+    if (!anOwner.IsNull()) {
+      if (aSelectedIds.contains((long)anOwner.get()))
+        continue;
+      aSelectedIds.append((long)anOwner.get());
+
+      ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
+      fillPresentation(aPrs, anOwner);
+      aPresentations.push_back(aPrs);
     }
-    aPresentations.push_back(aPrs);
   }
   return aPresentations;
 }
 
 QObjectPtrList XGUI_Selection::selectedObjects() const
 {
-  return myWorkshop->objectBrowser()->selectedObjects();
+  if (myWorkshop->objectBrowser())
+    return myWorkshop->objectBrowser()->selectedObjects();
+  return QObjectPtrList();
 }
 
 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
@@ -286,7 +299,7 @@ ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner
 
   Handle(SelectMgr_EntityOwner) aEO = theOwner;
   if (!aEO.IsNull()) {
-    Handle(AIS_InteractiveObject) anObj = 
+    Handle(AIS_InteractiveObject) anObj =
       Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
     anObject = myWorkshop->displayer()->getObject(anObj);
   }
@@ -346,7 +359,7 @@ TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO)
     Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
     Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
 
-    BRep_Builder aBuilder;      
+    BRep_Builder aBuilder;
     TopoDS_Edge aEdge;
     aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
     if (!aEdge.IsNull())