Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintLength.cpp
1 // File:    SketchPlugin_ConstraintLength.cpp
2 // Created: 30 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchPlugin_ConstraintLength.h"
6 #include <SketchPlugin_Line.h>
7
8 #include <GeomDataAPI_Point2D.h>
9
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_Data.h>
12
13 #include <GeomDataAPI_Point2D.h>
14
15 #include <AIS_LengthDimension.hxx>
16 #include <gp_Pnt.hxx>
17 #include <gp_Pln.hxx>
18
19 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
20 {
21 }
22
23 void SketchPlugin_ConstraintLength::initAttributes()
24 {
25   data()->addAttribute(CONSTRAINT_ATTR_VALUE,    ModelAPI_AttributeDouble::type());
26   data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE, ModelAPI_AttributeDouble::type());
27   data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
28   data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
29 }
30
31 void SketchPlugin_ConstraintLength::execute()
32 {
33 }
34
35 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintLength::getAISShape(
36   Handle_AIS_InteractiveObject thePrevious)
37 {
38   if (!sketch())
39     return thePrevious;
40
41   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
42
43   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
44     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(CONSTRAINT_ATTR_ENTITY_A));
45   if (!anAttr)
46     return thePrevious;
47   FeaturePtr aFeature = anAttr->feature();
48   if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
49     return thePrevious;
50
51   boost::shared_ptr<ModelAPI_AttributeDouble> aFlyoutAttr = 
52     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
53   double aFlyout = aFlyoutAttr->value();
54
55   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
56     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_VALUE));
57   double aValue = aValueAttr->value();
58
59   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
60   if (!aData->isValid())
61     return thePrevious;
62
63   boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
64         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
65   boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
66         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
67
68   //Build dimension here
69   boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPointStart->x(), aPointStart->y());
70   boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPointEnd->x(),   aPointEnd->y());
71   if (aFlyout < 0)
72     aPoint1.swap(aPoint2);
73
74   Handle(AIS_InteractiveObject) anAIS = thePrevious;
75   if (anAIS.IsNull())
76   {
77     Handle(AIS_LengthDimension) aDimAIS = 
78       new AIS_LengthDimension(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
79     aDimAIS->SetCustomValue(aValue);
80
81     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
82     anAspect->MakeArrows3d (Standard_False);
83     anAspect->MakeText3d(false/*is text 3d*/);
84     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
85     anAspect->MakeTextShaded(false/*is test shaded*/);
86     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false/*is units displayed*/);
87     /*if (isUnitsDisplayed)
88     {
89       aDimAIS->SetDisplayUnits (aDimDlg->GetUnits ());
90     }*/
91     aDimAIS->SetDimensionAspect (anAspect);
92     aDimAIS->SetFlyout(aFlyout);
93     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
94
95     anAIS = aDimAIS;
96   }
97   else
98   {
99     // update presentation
100     Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
101     if (!aDimAIS.IsNull()) 
102     {
103       aDimAIS->SetMeasuredGeometry(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
104       aDimAIS->SetCustomValue(aValue);
105       aDimAIS->SetFlyout(aFlyout);
106
107       aDimAIS->Redisplay(Standard_True);
108     }
109   }
110   return anAIS;
111 }
112
113 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
114 {
115   boost::shared_ptr<ModelAPI_Data> aData = data();
116   if (!aData->isValid())
117     return;
118
119   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
120         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
121   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
122 }
123