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