Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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 <GeomAPI_Lin2d.h>
16 #include <GeomAPI_Pnt2d.h>
17
18 #include <AIS_LengthDimension.hxx>
19 #include <gp_Pnt.hxx>
20 #include <gp_Pln.hxx>
21
22 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
23 {
24 }
25
26 void SketchPlugin_ConstraintLength::initAttributes()
27 {
28   data()->addAttribute(CONSTRAINT_ATTR_VALUE,    ModelAPI_AttributeDouble::type());
29   //data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE, ModelAPI_AttributeDouble::type());
30   data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
31   data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
32 }
33
34 void SketchPlugin_ConstraintLength::execute()
35 {
36   if (data()->attribute(CONSTRAINT_ATTR_ENTITY_A)->isInitialized() &&
37       !data()->attribute(CONSTRAINT_ATTR_VALUE)->isInitialized()) {
38
39     boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
40       boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(CONSTRAINT_ATTR_ENTITY_A));
41     FeaturePtr aFeature = aRef->feature();
42     if (aFeature) {
43       // set length value
44       boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
45         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(LINE_ATTR_START));
46       boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
47         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(LINE_ATTR_END));
48
49       double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
50
51       boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
52         boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_VALUE));
53       aValueAttr->setValue(aLenght);
54     }
55   }
56 }
57
58 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintLength::getAISShape(
59   Handle_AIS_InteractiveObject thePrevious)
60 {
61   if (!sketch())
62     return thePrevious;
63
64   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
65
66   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
67     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(CONSTRAINT_ATTR_ENTITY_A));
68   if (!anAttr)
69     return thePrevious;
70   FeaturePtr aFeature = anAttr->feature();
71   if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
72     return thePrevious;
73
74   //boost::shared_ptr<ModelAPI_AttributeDouble> aFlyoutAttr = 
75   //  boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
76   //double aFlyout = aFlyoutAttr->value();
77   // fly out calculation
78   boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = 
79     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
80   boost::shared_ptr<GeomAPI_Pnt2d> aFlyOutPnt = aFlyOutAttr->pnt();
81
82   boost::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
83     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(LINE_ATTR_START));
84   boost::shared_ptr<GeomDataAPI_Point2D> anEndPoint =
85      boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(LINE_ATTR_END));
86
87   boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = boost::shared_ptr<GeomAPI_Lin2d>
88                                              (new GeomAPI_Lin2d(aStartPoint->x(), aStartPoint->y(),
89                                                                 anEndPoint->x(), anEndPoint->y()));
90   boost::shared_ptr<GeomAPI_Pnt2d> aProjectedPoint = aFeatureLin->project(aFlyOutPnt);
91   double aDistance = aFlyOutPnt->distance(aProjectedPoint);
92   if (!aFeatureLin->isRight(aFlyOutPnt))
93     aDistance = -aDistance;
94   double aFlyout = aDistance;
95
96   // value
97   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
98     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_VALUE));
99   double aValue = aValueAttr->value();
100
101   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
102   if (!aData->isValid())
103     return thePrevious;
104
105   boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
106         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
107   boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
108         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
109
110   //Build dimension here
111   boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPointStart->x(), aPointStart->y());
112   boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPointEnd->x(),   aPointEnd->y());
113
114   Handle(AIS_InteractiveObject) anAIS = thePrevious;
115   if (anAIS.IsNull())
116   {
117     Handle(AIS_LengthDimension) aDimAIS = 
118       new AIS_LengthDimension(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
119     aDimAIS->SetCustomValue(aValue);
120
121     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
122     anAspect->MakeArrows3d (Standard_False);
123     anAspect->MakeText3d(false/*is text 3d*/);
124     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
125     anAspect->MakeTextShaded(false/*is test shaded*/);
126     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false/*is units displayed*/);
127     /*if (isUnitsDisplayed)
128     {
129       aDimAIS->SetDisplayUnits (aDimDlg->GetUnits ());
130     }*/
131     aDimAIS->SetDimensionAspect (anAspect);
132     aDimAIS->SetFlyout(aFlyout);
133     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
134
135     anAIS = aDimAIS;
136   }
137   else
138   {
139     // update presentation
140     Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
141     if (!aDimAIS.IsNull()) 
142     {
143       aDimAIS->SetMeasuredGeometry(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
144       aDimAIS->SetCustomValue(aValue);
145       aDimAIS->SetFlyout(aFlyout);
146
147       aDimAIS->Redisplay(Standard_True);
148     }
149   }
150   return anAIS;
151 }
152
153 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
154 {
155   boost::shared_ptr<ModelAPI_Data> aData = data();
156   if (!aData->isValid())
157     return;
158
159   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
160         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
161   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
162 }
163