Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 #include <SketchPlugin_Line.h>
10
11 #include <AIS_InteractiveObject.hxx>
12 #include <AIS_PerpendicularRelation.hxx>
13 #include <Geom_Plane.hxx>
14
15 SketchPlugin_ConstraintPerpendicular::SketchPlugin_ConstraintPerpendicular()
16 {
17 }
18
19 void SketchPlugin_ConstraintPerpendicular::initAttributes()
20 {
21   data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
22   data()->addAttribute(CONSTRAINT_ATTR_ENTITY_B, ModelAPI_AttributeRefAttr::type());
23 }
24
25 void SketchPlugin_ConstraintPerpendicular::execute()
26 {
27 }
28
29 const boost::shared_ptr<GeomAPI_Shape>&  SketchPlugin_ConstraintPerpendicular::preview()
30 {
31   /// \todo Preview for perpendicular constraint
32   return getPreview();
33 }
34
35 Handle_AIS_InteractiveObject SketchPlugin_ConstraintPerpendicular::getAISShape(Handle_AIS_InteractiveObject thePrevious)
36 {
37   Handle(AIS_InteractiveObject) anAIS = thePrevious;
38   if (!sketch())
39     return anAIS;
40
41   boost::shared_ptr<ModelAPI_Data> aData = data();
42   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr1 = 
43     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
44   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr2 = 
45     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
46   if (!anAttr1 || !anAttr1->isFeature() || 
47       !anAttr2 || !anAttr2->isFeature())
48     return anAIS;
49   boost::shared_ptr<SketchPlugin_Line> aLine1Feature = 
50     boost::dynamic_pointer_cast<SketchPlugin_Line>(anAttr1->feature());
51   boost::shared_ptr<SketchPlugin_Line> aLine2Feature = 
52     boost::dynamic_pointer_cast<SketchPlugin_Line>(anAttr2->feature());
53   if (!aLine1Feature || !aLine2Feature)
54     return anAIS;
55
56   boost::shared_ptr<GeomAPI_Shape> aLine1 = aLine1Feature->preview();
57   boost::shared_ptr<GeomAPI_Shape> aLine2 = aLine2Feature->preview();
58   Handle(Geom_Plane) aPlane = new Geom_Plane(sketch()->plane()->impl<gp_Pln>());
59
60   if (anAIS.IsNull())
61   {
62     Handle(AIS_PerpendicularRelation) aPerpendicular = 
63       new AIS_PerpendicularRelation(aLine1->impl<TopoDS_Shape>(), aLine2->impl<TopoDS_Shape>(), aPlane);
64
65     anAIS = aPerpendicular;
66   }
67   else
68   {
69     Handle(AIS_PerpendicularRelation) aPerpendicular = Handle(AIS_PerpendicularRelation)::DownCast(anAIS);
70     if (!aPerpendicular.IsNull())
71     {
72       aPerpendicular->SetFirstShape(aLine1->impl<TopoDS_Shape>());
73       aPerpendicular->SetSecondShape(aLine2->impl<TopoDS_Shape>());
74       aPerpendicular->SetPlane(aPlane);
75       aPerpendicular->Redisplay(Standard_True);
76     }
77   }
78   return anAIS;
79 }
80