Salome HOME
Preferences improvement
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintRadius.cpp
index b6a32df9476e9849ff205ecbabf33bf76d2f7e77..31b253d542b8010d97f6f19e47fe16f2cc34fef0 100644 (file)
 
 #include <GeomAPI_Pnt2d.h>
 #include <GeomAPI_Circ.h>
+#include <GeomAPI_Circ2d.h>
+#include <GeomAPI_Dir.h>
+#include <GeomAPI_XYZ.h>
 #include <GeomDataAPI_Point2D.h>
 #include <GeomDataAPI_Dir.h>
 
-#include <AIS_InteractiveObject.hxx>
-#include <AIS_RadiusDimension.hxx>
+#include <Config_PropManager.h>
 
 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
 {
@@ -25,135 +27,141 @@ SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
 
 void SketchPlugin_ConstraintRadius::initAttributes()
 {
-  data()->addAttribute(CONSTRAINT_ATTR_VALUE,    ModelAPI_AttributeDouble::type());
-  data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
-  data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
+  data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::type());
+  data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
+  data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
 }
 
 void SketchPlugin_ConstraintRadius::execute()
 {
-  if (data()->attribute(CONSTRAINT_ATTR_ENTITY_A)->isInitialized() &&
-      !data()->attribute(CONSTRAINT_ATTR_VALUE)->isInitialized()) {
+  if (data()->attribute(SketchPlugin_Constraint::ENTITY_A())->isInitialized() &&
+      !data()->attribute(SketchPlugin_Constraint::VALUE())->isInitialized()) {
 
     boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
-      boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(CONSTRAINT_ATTR_ENTITY_A));
-    FeaturePtr aFeature = aRef->feature();
+      boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
+    FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
     if (aFeature) {
       double aRadius = 0;
-      if (aFeature->getKind() == SKETCH_CIRCLE_KIND) {
+      boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
+      if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
         AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>
-                                            (aFeature->data()->attribute(CIRCLE_ATTR_RADIUS));
+                                            (aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
         if (anAttribute)
           aRadius = anAttribute->value();
       }
-      else if (aFeature->getKind() == SKETCH_ARC_KIND) {
-        //aLength = PartSet_FeatureArcPrs::radius(theFeature);
+      else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
+        boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
+          boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
+        boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
+          boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
+        if (aCenterAttr && aStartAttr)
+          aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
       }
       boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
-        boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_VALUE));
+        boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
       aValueAttr->setValue(aRadius);
     }
   }
 }
 
-Handle(AIS_InteractiveObject) SketchPlugin_ConstraintRadius::getAISShape(
-  Handle_AIS_InteractiveObject thePrevious)
+boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintRadius::getAISObject(
+                    boost::shared_ptr<GeomAPI_AISObject> thePrevious)
 {
-  Handle(AIS_InteractiveObject) anAIS = thePrevious;
   if (!sketch())
-    return anAIS;
+    return thePrevious;
 
   boost::shared_ptr<ModelAPI_Data> aData = data();
   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
-    boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
+    boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
   if (!anAttr)
-    return anAIS;
-  FeaturePtr aFeature = anAttr->feature();
+    return thePrevious;
+  FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
   std::string aKind = aFeature ? aFeature->getKind() : "";
-  if (aKind != SKETCH_CIRCLE_KIND && aKind != SKETCH_ARC_KIND)
-    return anAIS;
+  if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
+    return thePrevious;
 
-  boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
-          boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
-  double aValue = aValueAttr->value();
-
-  // an anchor point
-  boost::shared_ptr<GeomDataAPI_Point2D> aAnchorAttr = 
-    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
-  if (!aAnchorAttr->isInitialized())
-    return anAIS;
-  boost::shared_ptr<GeomAPI_Pnt2d> anAnchor2D = aAnchorAttr->pnt();
-  boost::shared_ptr<GeomAPI_Pnt> anAnchor = sketch()->to3D(anAnchor2D->x(), anAnchor2D->y());
+  // Flyout point
+  boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
+    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+  if (!aFlyoutAttr->isInitialized())
+    return thePrevious;
+  boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
 
+  // Prepare a circle
   aData = aFeature->data();
   boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
   double aRadius;
-  if (aKind == SKETCH_CIRCLE_KIND) {
-    aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
+  if (aKind == SketchPlugin_Circle::ID()) {
+    aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Circle::CENTER_ID()));
     AttributeDoublePtr aCircRadius = 
-      boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CIRCLE_ATTR_RADIUS));
+      boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
     aRadius = aCircRadius->value();
   }
-  else if (aKind == SKETCH_ARC_KIND) {
-    aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
+  else if (aKind == SketchPlugin_Arc::ID()) {
+    aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
-      boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
+      boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
   }
 
-  // a circle
   boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
-
   boost::shared_ptr<GeomDataAPI_Dir> aNDir = 
-    boost::dynamic_pointer_cast<GeomDataAPI_Dir>(sketch()->data()->attribute(SKETCH_ATTR_NORM));
+    boost::dynamic_pointer_cast<GeomDataAPI_Dir>(sketch()->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
   boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
-
   boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
 
-  anAnchor = aCircle->project(anAnchor);
-  // TODO: a bug in AIS_RadiusDimension:
-  // The anchor point can't be myCirc.Location() - an exception is raised.
-  // But we need exactly this case...
-  // 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.
-  gp_Pnt anAPnt = anAnchor->impl<gp_Pnt>();
-  double aDelta = 1/1000.0;
-  if (anAnchor->x() != 0)
-    anAnchor->setX(anAnchor->x() + aDelta);
-  if (anAnchor->y() != 0)
-    anAnchor->setY(anAnchor->y() + aDelta);
-  if (anAnchor->z() != 0)
-    anAnchor->setZ(anAnchor->z() + aDelta);
-
-  if (anAIS.IsNull())
-  {
-    Handle(AIS_RadiusDimension) aDimAIS = 
-      new AIS_RadiusDimension(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
-    aDimAIS->SetCustomValue(aValue);
-
-    Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
-    anAspect->MakeArrows3d (Standard_False);
-    anAspect->MakeText3d(false);
-    anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
-    anAspect->MakeTextShaded(false);
-    aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
-    aDimAIS->SetDimensionAspect (anAspect);
-    aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
-
-    anAIS = aDimAIS;
-  }
-  else
-  {
-    // update presentation
-    Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS);
-    if (!aDimAIS.IsNull())
-    {
-      aDimAIS->SetMeasuredGeometry(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
-      aDimAIS->SetCustomValue(aValue);
-      aDimAIS->Redisplay(Standard_True);
-    }
-  }
+  // Value
+  boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
+    boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
+  double aValue = aRadius;
+  if (aValueAttr && aValueAttr->isInitialized())
+   aValue = aValueAttr->value();
+
+  boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
+  if (!anAIS)
+    anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
+  anAIS->createRadius(aCircle, aFlyoutPnt, aValue);
+
+  // Set color from preferences
+  std::vector<int> aRGB = Config_PropManager::color("Visualization", "radius_color", RADIUS_COLOR);
+  anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
   return anAIS;
 }
 
+void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
+{
+  boost::shared_ptr<ModelAPI_Data> aData = data();
+  if (!aData->isValid())
+    return;
+
+  boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
+    boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
+  if (!aFeature)
+    return;
+  std::string aCenterAttrName;
+  if (aFeature->getKind() == SketchPlugin_Circle::ID())
+    aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
+  else if (aFeature->getKind() == SketchPlugin_Arc::ID())
+    aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
+  boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
+    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
+  boost::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
+
+  // The specified delta applied on the circle curve, 
+  // so it will be scaled due to distance between flyout and center points
+  boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
+    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+  boost::shared_ptr<GeomAPI_Pnt2d> aFlyout = aFlyoutAttr->pnt();
+
+  boost::shared_ptr<ModelAPI_AttributeDouble> aRadius =
+    boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
+  double aScale = aFlyout->distance(aCenter) / aRadius->value();
+
+  boost::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aFlyout));
+  aFlyout->setX(aFlyout->x() + aScale * theDeltaX);
+  aFlyout->setY(aFlyout->y() + aScale * theDeltaY);
+  aFlyout = aCircle->project(aFlyout);
+
+  aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());
+}