Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintParallel.cpp
1 // File:    SketchPlugin_ConstraintParallel.cpp
2 // Created: 26 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchPlugin_ConstraintParallel.h"
6
7 #include <ModelAPI_AttributeDouble.h>
8 #include <ModelAPI_Data.h>
9
10 #include <SketchPlugin_Line.h>
11 #include <SketchPlugin_Sketch.h>
12
13 #include <AIS_InteractiveObject.hxx>
14 #include <AIS_ParallelRelation.hxx>
15 #include <Geom_Plane.hxx>
16
17 #include <GeomDataAPI_Point2D.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_Pnt.h>
20
21 SketchPlugin_ConstraintParallel::SketchPlugin_ConstraintParallel()
22 {
23 }
24
25 void SketchPlugin_ConstraintParallel::initAttributes()
26 {
27   data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
28   data()->addAttribute(CONSTRAINT_ATTR_ENTITY_B, ModelAPI_AttributeRefAttr::type());
29   data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
30 }
31
32 void SketchPlugin_ConstraintParallel::execute()
33 {
34 }
35
36 const boost::shared_ptr<GeomAPI_Shape>&  SketchPlugin_ConstraintParallel::preview()
37 {
38   /// \todo Preview for parallel constraint
39   return getPreview();
40 }
41
42
43 Handle_AIS_InteractiveObject SketchPlugin_ConstraintParallel::getAISShape(Handle_AIS_InteractiveObject thePrevious)
44 {
45   Handle(AIS_InteractiveObject) anAIS = thePrevious;
46   if (!sketch())
47     return anAIS;
48
49   boost::shared_ptr<ModelAPI_Data> aData = data();
50   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr1 = 
51     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
52   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr2 = 
53     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
54   if (!anAttr1 || !anAttr1->isFeature() || 
55       !anAttr2 || !anAttr2->isFeature())
56     return anAIS;
57   boost::shared_ptr<SketchPlugin_Line> aLine1Feature = 
58     boost::dynamic_pointer_cast<SketchPlugin_Line>(anAttr1->feature());
59   boost::shared_ptr<SketchPlugin_Line> aLine2Feature = 
60     boost::dynamic_pointer_cast<SketchPlugin_Line>(anAttr2->feature());
61   if (!aLine1Feature || !aLine2Feature)
62     return anAIS;
63
64   boost::shared_ptr<GeomAPI_Shape> aLine1 = aLine1Feature->preview();
65   boost::shared_ptr<GeomAPI_Shape> aLine2 = aLine2Feature->preview();
66   Handle(Geom_Plane) aPlane = new Geom_Plane(sketch()->plane()->impl<gp_Pln>());
67
68   boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
69     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
70   boost::shared_ptr<GeomAPI_Pnt2d> aFOPnt2d = aFlyoutAttr->pnt();
71   boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFOPnt2d->x(), aFOPnt2d->y());
72
73   if (anAIS.IsNull())
74   {
75     Handle(AIS_ParallelRelation) aParallel = 
76       new AIS_ParallelRelation(aLine1->impl<TopoDS_Shape>(), aLine2->impl<TopoDS_Shape>(), aPlane);
77     aParallel->SetPosition(aFlyoutPnt->impl<gp_Pnt>());
78     anAIS = aParallel;
79   }
80   else
81   {
82     Handle(AIS_ParallelRelation) aParallel = Handle(AIS_ParallelRelation)::DownCast(anAIS);
83     if (!aParallel.IsNull())
84     {
85       aParallel->SetFirstShape(aLine1->impl<TopoDS_Shape>());
86       aParallel->SetSecondShape(aLine2->impl<TopoDS_Shape>());
87       aParallel->SetPlane(aPlane);
88       aParallel->SetPosition(aFlyoutPnt->impl<gp_Pnt>());
89       aParallel->Redisplay(Standard_True);
90     }
91   }
92   return anAIS;
93 }
94
95 void SketchPlugin_ConstraintParallel::move(double theDeltaX, double theDeltaY)
96 {
97   boost::shared_ptr<ModelAPI_Data> aData = data();
98   if (!aData->isValid())
99     return;
100
101   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
102         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
103   aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
104 }
105