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