Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintDistance.cpp
1 // File:    SketchPlugin_ConstraintDistance.cpp
2 // Created: 23 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchPlugin_ConstraintDistance.h"
6 #include <SketchPlugin_Point.h>
7
8 #include <GeomAPI_Lin2D.h>
9 #include <GeomAPI_Pnt2D.h>
10 #include <GeomDataAPI_Point2D.h>
11
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_Data.h>
14
15 #include <AIS_LengthDimension.hxx>
16 #include <gp_Pnt.hxx>
17 #include <gp_Pln.hxx>
18
19 /// Obtain the point object from specified constraint parameter
20 static boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(
21             DataPtr             theData,
22             const std::string&  theAttribute);
23
24
25 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
26 {
27 }
28
29 void SketchPlugin_ConstraintDistance::initAttributes()
30 {
31   data()->addAttribute(CONSTRAINT_ATTR_VALUE,    ModelAPI_AttributeDouble::type());
32   data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
33   data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
34   data()->addAttribute(CONSTRAINT_ATTR_ENTITY_B, ModelAPI_AttributeRefAttr::type());
35 }
36
37 void SketchPlugin_ConstraintDistance::execute()
38 {
39   boost::shared_ptr<ModelAPI_Data> aData = data();
40
41   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr_A = 
42           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
43   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr_B = 
44           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
45
46   AttributeDoublePtr anAttr_Value =
47       boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
48   if (anAttr_A->isInitialized() && anAttr_B->isInitialized() && !anAttr_Value->isInitialized())
49   {
50     FeaturePtr aFeature_A = anAttr_A->feature();
51     FeaturePtr aFeature_B = anAttr_B->feature();
52     if (aFeature_A && aFeature_A) {
53       // calculate the distance
54       boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A =
55         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
56                                              (aFeature_A->data()->attribute(POINT_ATTR_COORD));
57       boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B =
58         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
59                                              (aFeature_B->data()->attribute(POINT_ATTR_COORD));
60       if (aPoint_A && aPoint_B) {
61         anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
62       }
63     }
64   }
65 }
66
67 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintDistance::getAISShape(
68   Handle_AIS_InteractiveObject thePrevious)
69 {
70   if (!sketch())
71     return thePrevious;
72
73   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
74
75   DataPtr aData = data();
76   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_A);
77   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_B);
78   if (!aPoint_A || !aPoint_B)
79     return thePrevious;
80
81   // fly out calculation
82   boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = 
83     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
84   double aFlyout = 0;
85   if (aFlyOutAttr->isInitialized()) {
86     boost::shared_ptr<GeomAPI_Pnt2d> aFlyOutPnt = aFlyOutAttr->pnt();
87
88     boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = 
89       boost::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoint_A->x(), aPoint_A->y(),
90                                                          aPoint_B->x(), aPoint_B->y()));
91     boost::shared_ptr<GeomAPI_Pnt2d> aProjectedPoint = aFeatureLin->project(aFlyOutPnt);
92     double aDistance = aFlyOutPnt->distance(aProjectedPoint);
93     if (!aFeatureLin->isRight(aFlyOutPnt))
94       aDistance = -aDistance;
95     aFlyout = aDistance;
96   }
97
98   //Build dimension here
99   boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPoint_A->x(), aPoint_A->y());
100   boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPoint_B->x(), aPoint_B->y());
101
102   // value calculation
103   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
104     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
105   double aValue = aValueAttr->value();
106
107   Handle(AIS_InteractiveObject) anAIS = thePrevious;
108   if (anAIS.IsNull())
109   {
110     Handle(AIS_LengthDimension) aDimAIS = 
111       new AIS_LengthDimension(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
112     aDimAIS->SetCustomValue(aValue);
113
114     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
115     anAspect->MakeArrows3d (Standard_False);
116     anAspect->MakeText3d(false);
117     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
118     anAspect->MakeTextShaded(false);
119     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
120     aDimAIS->SetDimensionAspect (anAspect);
121     aDimAIS->SetFlyout(aFlyout);
122     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
123
124     anAIS = aDimAIS;
125   }
126   else {
127     // update presentation
128     Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
129     if (!aDimAIS.IsNull()) {
130       aDimAIS->SetMeasuredGeometry(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
131       aDimAIS->SetCustomValue(aValue);
132       aDimAIS->SetFlyout(aFlyout);
133
134       aDimAIS->Redisplay(Standard_True);
135     }
136   }
137   return anAIS;
138 }
139
140 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
141 {
142   boost::shared_ptr<ModelAPI_Data> aData = data();
143   if (!aData->isValid())
144     return;
145
146   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
147         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
148   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
149 }
150
151 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
152                                                        const std::string& theAttribute)
153 {
154   boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
155
156   if (!theData)
157     return aPointAttr;
158
159   FeaturePtr aFeature;
160   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
161     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
162   if (anAttr)
163     aFeature = anAttr->feature();
164
165   if (aFeature && aFeature->getKind() == SKETCH_POINT_KIND)
166     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
167                                            (aFeature->data()->attribute(POINT_ATTR_COORD));
168   return aPointAttr;
169 }
170