Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[modules/shaper.git] / src / GeomAPI / GeomAPI_AISObject.cpp
index 7f068f0f9cd15267c9f23be3e9026b86c3b1b22b..6be05290b1d06ac86d6ad5cc2c7f8c252ea6cc94 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 // File:        GeomAPI_AISObject.cpp
 // Created:     25 Jun 2014
 // Author:      Artem ZHIDKOV
 #include <Geom_Plane.hxx>
 #include <TopoDS_Shape.hxx>
 #include <Quantity_NameOfColor.hxx>
+#include <BRepBndLib.hxx>
 
 #include <AIS_InteractiveObject.hxx>
+#include <AIS_InteractiveContext.hxx>
 #include <AIS_LengthDimension.hxx>
 #include <AIS_ParallelRelation.hxx>
 #include <AIS_PerpendicularRelation.hxx>
 #include <AIS_RadiusDimension.hxx>
 #include <AIS_Shape.hxx>
+#include <AIS_FixRelation.hxx>
+#include <Prs3d_PointAspect.hxx>
+
+const double tolerance = 1e-7;
 
 const int CONSTRAINT_TEXT_HEIGHT = 28;  /// the text height of the constraint
 const int CONSTRAINT_TEXT_SELECTION_TOLERANCE = 20;  /// the text selection tolerance
 
-// Initialization of color constants
-int Colors::COLOR_BROWN = Quantity_NOC_BROWN;
-int Colors::COLOR_RED = Quantity_NOC_RED;
-int Colors::COLOR_GREEN = Quantity_NOC_GREEN;
-int Colors::COLOR_BLUE = Quantity_NOC_BLUE1;
-
 GeomAPI_AISObject::GeomAPI_AISObject()
     : GeomAPI_Interface(new Handle(AIS_InteractiveObject)())
 {
 }
 
-void GeomAPI_AISObject::createShape(boost::shared_ptr<GeomAPI_Shape> theShape)
+void GeomAPI_AISObject::createShape(std::shared_ptr<GeomAPI_Shape> theShape)
 {
   const TopoDS_Shape& aTDS =
       (theShape && theShape->implPtr<TopoDS_Shape>()) ?
@@ -60,21 +62,26 @@ void GeomAPI_AISObject::createShape(boost::shared_ptr<GeomAPI_Shape> theShape)
     setImpl(new Handle(AIS_InteractiveObject)(new AIS_Shape(aTDS)));
 }
 
-void GeomAPI_AISObject::createDistance(boost::shared_ptr<GeomAPI_Pnt> theStartPoint,
-                                       boost::shared_ptr<GeomAPI_Pnt> theEndPoint,
-                                       boost::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
-                                       boost::shared_ptr<GeomAPI_Pln> thePlane, double theDistance)
+void GeomAPI_AISObject::createDistance(std::shared_ptr<GeomAPI_Pnt> theStartPoint,
+                                       std::shared_ptr<GeomAPI_Pnt> theEndPoint,
+                                       std::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
+                                       std::shared_ptr<GeomAPI_Pln> thePlane, double theDistance)
 {
   double aFlyout = 0;
   if (theFlyoutPoint) {
-    boost::shared_ptr<GeomAPI_Lin> aLine = boost::shared_ptr<GeomAPI_Lin>(
-        new GeomAPI_Lin(theStartPoint, theEndPoint));
-    double aDist = aLine->distance(theFlyoutPoint);
+    double aDist = 0.0;
+    if (theStartPoint->distance(theEndPoint) < tolerance)
+      aDist = theStartPoint->distance(theFlyoutPoint);
+    else {
+      std::shared_ptr<GeomAPI_Lin> aLine = std::shared_ptr<GeomAPI_Lin>(
+          new GeomAPI_Lin(theStartPoint, theEndPoint));
+      aDist = aLine->distance(theFlyoutPoint);
+    }
 
-    boost::shared_ptr<GeomAPI_XYZ> aLineDir = theEndPoint->xyz()->decreased(theStartPoint->xyz());
-    boost::shared_ptr<GeomAPI_XYZ> aFOutDir = theFlyoutPoint->xyz()->decreased(
+    std::shared_ptr<GeomAPI_XYZ> aLineDir = theEndPoint->xyz()->decreased(theStartPoint->xyz());
+    std::shared_ptr<GeomAPI_XYZ> aFOutDir = theFlyoutPoint->xyz()->decreased(
         theStartPoint->xyz());
-    boost::shared_ptr<GeomAPI_XYZ> aNorm = thePlane->direction()->xyz();
+    std::shared_ptr<GeomAPI_XYZ> aNorm = thePlane->direction()->xyz();
     if (aLineDir->cross(aFOutDir)->dot(aNorm) < 0)
       aDist = -aDist;
     aFlyout = aDist;
@@ -113,11 +120,11 @@ void GeomAPI_AISObject::createDistance(boost::shared_ptr<GeomAPI_Pnt> theStartPo
   }
 }
 
-void GeomAPI_AISObject::createRadius(boost::shared_ptr<GeomAPI_Circ> theCircle,
-                                     boost::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
+void GeomAPI_AISObject::createRadius(std::shared_ptr<GeomAPI_Circ> theCircle,
+                                     std::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
                                      double theRadius)
 {
-  boost::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
+  std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
 
   // TODO: a bug in AIS_RadiusDimension:
   // The anchor point can't be myCirc.Location() - an exception is raised.
@@ -125,10 +132,10 @@ void GeomAPI_AISObject::createRadius(boost::shared_ptr<GeomAPI_Circ> theCircle,
   // We want to show a radius dimension starting from the circle centre and 
   // ending at the user-defined point.
   // Also, if anchor point coincides with myP2, the radius dimension is not displayed at all.
-  boost::shared_ptr<GeomAPI_Pnt> anAnchor = theCircle->project(theFlyoutPoint);
-  boost::shared_ptr<GeomAPI_XYZ> anAnchorXYZ = anAnchor->xyz();
+  std::shared_ptr<GeomAPI_Pnt> anAnchor = theCircle->project(theFlyoutPoint);
+  std::shared_ptr<GeomAPI_XYZ> anAnchorXYZ = anAnchor->xyz();
   anAnchorXYZ = anAnchorXYZ->decreased(aCenter->xyz());
-  boost::shared_ptr<GeomAPI_Dir> aDeltaDir(new GeomAPI_Dir(anAnchorXYZ));
+  std::shared_ptr<GeomAPI_Dir> aDeltaDir(new GeomAPI_Dir(anAnchorXYZ));
   const double aDelta = 1e-3;
   anAnchor->setX(anAnchor->x() + aDelta * aDeltaDir->x());
   anAnchor->setY(anAnchor->y() + aDelta * aDeltaDir->y());
@@ -162,10 +169,10 @@ void GeomAPI_AISObject::createRadius(boost::shared_ptr<GeomAPI_Circ> theCircle,
   }
 }
 
-void GeomAPI_AISObject::createParallel(boost::shared_ptr<GeomAPI_Shape> theLine1,
-                                       boost::shared_ptr<GeomAPI_Shape> theLine2,
-                                       boost::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
-                                       boost::shared_ptr<GeomAPI_Pln> thePlane)
+void GeomAPI_AISObject::createParallel(std::shared_ptr<GeomAPI_Shape> theLine1,
+                                       std::shared_ptr<GeomAPI_Shape> theLine2,
+                                       std::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
+                                       std::shared_ptr<GeomAPI_Pln> thePlane)
 {
   Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
   Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
@@ -189,9 +196,9 @@ void GeomAPI_AISObject::createParallel(boost::shared_ptr<GeomAPI_Shape> theLine1
   }
 }
 
-void GeomAPI_AISObject::createPerpendicular(boost::shared_ptr<GeomAPI_Shape> theLine1,
-                                            boost::shared_ptr<GeomAPI_Shape> theLine2,
-                                            boost::shared_ptr<GeomAPI_Pln> thePlane)
+void GeomAPI_AISObject::createPerpendicular(std::shared_ptr<GeomAPI_Shape> theLine1,
+                                            std::shared_ptr<GeomAPI_Shape> theLine2,
+                                            std::shared_ptr<GeomAPI_Pln> thePlane)
 {
   Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
   Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
@@ -212,6 +219,42 @@ void GeomAPI_AISObject::createPerpendicular(boost::shared_ptr<GeomAPI_Shape> the
   }
 }
 
+
+void GeomAPI_AISObject::createFixed(std::shared_ptr<GeomAPI_Shape> theShape,
+                                    std::shared_ptr<GeomAPI_Pln> thePlane)
+{
+  Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
+  Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
+  TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
+  Handle(AIS_FixRelation) aFixPrs;
+  if (anAIS.IsNull()) {
+    aFixPrs = new AIS_FixRelation(aShape, aPlane);
+
+    setImpl(new Handle(AIS_InteractiveObject)(aFixPrs));
+  } else {
+    aFixPrs = Handle(AIS_FixRelation)::DownCast(anAIS);
+    if (!aFixPrs.IsNull()) {
+      aFixPrs->SetFirstShape(aShape);
+      aFixPrs->SetPlane(aPlane);
+      aFixPrs->Redisplay(Standard_True);
+    }
+  }
+  if (!aFixPrs.IsNull()) {
+    Bnd_Box aBox;
+    BRepBndLib::Add(aShape, aBox);
+    double aXmin, aXmax, aYmin, aYmax, aZmin, aZmax;
+    aBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
+    gp_Pnt aXYZ1(aXmin, aXmax, aYmin);
+    gp_Pnt aXYZ2(aXmax, aYmax, aZmax);
+    double aDist = aXYZ1.Distance(aXYZ2);
+    if (aDist > Precision::Confusion()) {
+      aFixPrs->SetArrowSize(aDist/8.);
+    }
+  }
+}
+
+
+
 void GeomAPI_AISObject::setColor(const int& theColor)
 {
   Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
@@ -231,6 +274,7 @@ void GeomAPI_AISObject::setWidth(const double& theWidth)
   if (anAIS.IsNull())
     return;
   anAIS->SetWidth(theWidth);
+  anAIS->Redisplay();
 }
 
 void GeomAPI_AISObject::setColor(int theR, int theG, int theB)
@@ -255,3 +299,55 @@ bool GeomAPI_AISObject::empty() const
   return false;
 }
 
+int GeomAPI_AISObject::getShapeType() const
+{
+  Handle(AIS_InteractiveObject) anAIS = const_cast<GeomAPI_AISObject*>(this)
+      ->impl<Handle(AIS_InteractiveObject)>();
+  if (!anAIS.IsNull()) {
+    Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(anAIS);
+    if (!aAISShape.IsNull()) {
+      return aAISShape->Shape().ShapeType();
+    }
+  }
+  return -1;
+}
+
+void GeomAPI_AISObject::setPointMarker(int theType, double theScale)
+{
+  Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
+  if (!anAIS.IsNull()) {
+    Handle(AIS_Drawer) aDrawer = anAIS->Attributes();
+    if (aDrawer->HasPointAspect()) {
+      Handle(Prs3d_PointAspect) aPA = aDrawer->PointAspect();
+      aPA->SetTypeOfMarker((Aspect_TypeOfMarker)theType);
+      aPA->SetScale(theScale);
+    } else {
+      Quantity_NameOfColor aCol = Quantity_NOC_YELLOW;
+      aDrawer->SetPointAspect(new Prs3d_PointAspect((Aspect_TypeOfMarker)theType, aCol, theScale));
+    }
+  }
+}
+
+
+void GeomAPI_AISObject::setLineStyle(int theStyle)
+{
+  Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
+  if (!anAIS.IsNull()) {
+    Handle(AIS_Drawer) aDrawer = anAIS->Attributes();
+    if (aDrawer->HasLineAspect())
+      aDrawer->LineAspect()->SetTypeOfLine((Aspect_TypeOfLine)theStyle);
+    if (aDrawer->HasWireAspect())
+      aDrawer->WireAspect()->SetTypeOfLine((Aspect_TypeOfLine)theStyle);
+  }
+}
+
+
+void GeomAPI_AISObject::setTransparensy(double theVal)
+{
+  Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
+  if (!anAIS.IsNull()) {
+    Handle(AIS_InteractiveContext) aContext = anAIS->GetContext();
+    if (!aContext.IsNull())
+      aContext->SetTransparency(anAIS, theVal, false);
+  }
+}