Salome HOME
1b6cc4d095d270e16afd86d21c85ec0a690d04b0
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintPerpendicular.cpp
1 // File:    SketchPlugin_ConstraintPerpendicular.cpp
2 // Created: 26 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchPlugin_ConstraintPerpendicular.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_PerpendicularRelation.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_ConstraintPerpendicular::SketchPlugin_ConstraintPerpendicular()
22 {
23 }
24
25 void SketchPlugin_ConstraintPerpendicular::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_ConstraintPerpendicular::execute()
33 {
34 }
35
36 const boost::shared_ptr<GeomAPI_Shape>&  SketchPlugin_ConstraintPerpendicular::preview()
37 {
38   /// \todo Preview for perpendicular constraint
39   return getPreview();
40 }
41
42 Handle_AIS_InteractiveObject SketchPlugin_ConstraintPerpendicular::getAISShape(Handle_AIS_InteractiveObject thePrevious)
43 {
44   Handle(AIS_InteractiveObject) anAIS = thePrevious;
45   if (!sketch())
46     return anAIS;
47
48   boost::shared_ptr<ModelAPI_Data> aData = data();
49   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr1 = 
50     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
51   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr2 = 
52     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
53   if (!anAttr1 || !anAttr1->isFeature() || 
54       !anAttr2 || !anAttr2->isFeature())
55     return anAIS;
56   boost::shared_ptr<SketchPlugin_Line> aLine1Feature = 
57     boost::dynamic_pointer_cast<SketchPlugin_Line>(anAttr1->feature());
58   boost::shared_ptr<SketchPlugin_Line> aLine2Feature = 
59     boost::dynamic_pointer_cast<SketchPlugin_Line>(anAttr2->feature());
60   if (!aLine1Feature || !aLine2Feature)
61     return anAIS;
62
63   boost::shared_ptr<GeomAPI_Shape> aLine1 = aLine1Feature->preview();
64   boost::shared_ptr<GeomAPI_Shape> aLine2 = aLine2Feature->preview();
65   Handle(Geom_Plane) aPlane = new Geom_Plane(sketch()->plane()->impl<gp_Pln>());
66
67   boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
68     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
69   boost::shared_ptr<GeomAPI_Pnt2d> aFOPnt2d = aFlyoutAttr->pnt();
70   boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFOPnt2d->x(), aFOPnt2d->y());
71
72   if (anAIS.IsNull())
73   {
74     Handle(AIS_PerpendicularRelation) aPerpendicular = 
75       new AIS_PerpendicularRelation(aLine1->impl<TopoDS_Shape>(), aLine2->impl<TopoDS_Shape>(), aPlane);
76     aPerpendicular->SetPosition(aFlyoutPnt->impl<gp_Pnt>());
77     anAIS = aPerpendicular;
78   }
79   else
80   {
81     Handle(AIS_PerpendicularRelation) aPerpendicular = Handle(AIS_PerpendicularRelation)::DownCast(anAIS);
82     if (!aPerpendicular.IsNull())
83     {
84       aPerpendicular->SetFirstShape(aLine1->impl<TopoDS_Shape>());
85       aPerpendicular->SetSecondShape(aLine2->impl<TopoDS_Shape>());
86       aPerpendicular->SetPlane(aPlane);
87       aPerpendicular->SetPosition(aFlyoutPnt->impl<gp_Pnt>());
88       aPerpendicular->Redisplay(Standard_True);
89     }
90   }
91   return anAIS;
92 }
93
94 void SketchPlugin_ConstraintPerpendicular::move(double theDeltaX, double theDeltaY)
95 {
96   boost::shared_ptr<ModelAPI_Data> aData = data();
97   if (!aData->isValid())
98     return;
99
100   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
101         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
102   aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
103 }
104