Salome HOME
Fix sketcher bugs
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintParallel.cpp
index f77c95d27ad041f871daaaabb84a9542b5e882e5..0d73cba025f4cacedbc4ba15403962679b7532d7 100644 (file)
@@ -6,14 +6,11 @@
 
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_Data.h>
+#include <ModelAPI_ResultConstruction.h>
 
 #include <SketchPlugin_Line.h>
 #include <SketchPlugin_Sketch.h>
 
-#include <AIS_InteractiveObject.hxx>
-#include <AIS_ParallelRelation.hxx>
-#include <Geom_Plane.hxx>
-
 #include <GeomDataAPI_Point2D.h>
 #include <GeomAPI_Pnt2d.h>
 #include <GeomAPI_Pnt.h>
@@ -24,71 +21,62 @@ SketchPlugin_ConstraintParallel::SketchPlugin_ConstraintParallel()
 
 void SketchPlugin_ConstraintParallel::initAttributes()
 {
-  data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
-  data()->addAttribute(CONSTRAINT_ATTR_ENTITY_B, ModelAPI_AttributeRefAttr::type());
-  data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
+  data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
+  data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
+  data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
 }
 
 void SketchPlugin_ConstraintParallel::execute()
 {
 }
 
-const boost::shared_ptr<GeomAPI_Shape>&  SketchPlugin_ConstraintParallel::preview()
-{
-  /// \todo Preview for parallel constraint
-  return getPreview();
-}
-
-
-Handle_AIS_InteractiveObject SketchPlugin_ConstraintParallel::getAISShape(Handle_AIS_InteractiveObject thePrevious)
+boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintParallel::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> anAttr1 = 
-    boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
+    boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr2 = 
-    boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
-  if (!anAttr1 || !anAttr1->isFeature() || 
-      !anAttr2 || !anAttr2->isFeature())
-    return anAIS;
+    boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
+  if (!anAttr1 || !anAttr1->isObject() || 
+      !anAttr2 || !anAttr2->isObject())
+    return thePrevious;
+
+  FeaturePtr aFeature = SketchPlugin_Sketch::getFeature(anAttr1->object());
+  if (!aFeature)
+    return thePrevious;
   boost::shared_ptr<SketchPlugin_Line> aLine1Feature = 
-    boost::dynamic_pointer_cast<SketchPlugin_Line>(anAttr1->feature());
+    boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
+
+  aFeature = SketchPlugin_Sketch::getFeature(anAttr2->object());
+  if (!aFeature)
+    return thePrevious;
   boost::shared_ptr<SketchPlugin_Line> aLine2Feature = 
-    boost::dynamic_pointer_cast<SketchPlugin_Line>(anAttr2->feature());
+    boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
+
   if (!aLine1Feature || !aLine2Feature)
-    return anAIS;
+    return thePrevious;
 
-  boost::shared_ptr<GeomAPI_Shape> aLine1 = aLine1Feature->preview();
-  boost::shared_ptr<GeomAPI_Shape> aLine2 = aLine2Feature->preview();
-  Handle(Geom_Plane) aPlane = new Geom_Plane(sketch()->plane()->impl<gp_Pln>());
+  boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
+  boost::shared_ptr<GeomAPI_Shape> aLine1, aLine2;
+  boost::shared_ptr<ModelAPI_ResultConstruction> aConst1 = 
+    boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aLine1Feature->firstResult());
+  if (aConst1) aLine1 = aConst1->shape();
+  boost::shared_ptr<ModelAPI_ResultConstruction> aConst2 = 
+    boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aLine1Feature->firstResult());
+  if (aConst2) aLine2 = aConst2->shape();
 
   boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
-    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
-  boost::shared_ptr<GeomAPI_Pnt2d> aFOPnt2d = aFlyoutAttr->pnt();
-  boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFOPnt2d->x(), aFOPnt2d->y());
-
-  if (anAIS.IsNull())
-  {
-    Handle(AIS_ParallelRelation) aParallel = 
-      new AIS_ParallelRelation(aLine1->impl<TopoDS_Shape>(), aLine2->impl<TopoDS_Shape>(), aPlane);
-    aParallel->SetPosition(aFlyoutPnt->impl<gp_Pnt>());
-    anAIS = aParallel;
-  }
-  else
-  {
-    Handle(AIS_ParallelRelation) aParallel = Handle(AIS_ParallelRelation)::DownCast(anAIS);
-    if (!aParallel.IsNull())
-    {
-      aParallel->SetFirstShape(aLine1->impl<TopoDS_Shape>());
-      aParallel->SetSecondShape(aLine2->impl<TopoDS_Shape>());
-      aParallel->SetPlane(aPlane);
-      aParallel->SetPosition(aFlyoutPnt->impl<gp_Pnt>());
-      aParallel->Redisplay(Standard_True);
-    }
-  }
+    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+  boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
+
+  boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
+  if (!anAIS)
+    anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
+  anAIS->createParallel(aLine1, aLine2, aFlyoutPnt, aPlane);
   return anAIS;
 }
 
@@ -99,7 +87,7 @@ void SketchPlugin_ConstraintParallel::move(double theDeltaX, double theDeltaY)
     return;
 
   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
-        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
+        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
   aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
 }