]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp
Salome HOME
0e8d30b6accfa5057a0347a7a0282ca73090d3dd
[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<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_A);
42   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_B);
43   if (aPoint_A && aPoint_B) {
44     AttributeDoublePtr anAttr_Value =
45       boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
46
47     if (!anAttr_Value->isInitialized()) {
48       anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
49     }
50   }
51 }
52
53 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintDistance::getAISShape(
54   Handle_AIS_InteractiveObject thePrevious)
55 {
56   if (!sketch())
57     return thePrevious;
58
59   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
60
61   DataPtr aData = data();
62   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_A);
63   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_B);
64   if (!aPoint_A || !aPoint_B)
65     return thePrevious;
66
67   // fly out calculation
68   boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = 
69     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
70   double aFlyout = 0;
71   if (aFlyOutAttr->isInitialized()) {
72     boost::shared_ptr<GeomAPI_Pnt2d> aFlyOutPnt = aFlyOutAttr->pnt();
73
74     boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = 
75       boost::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoint_A->x(), aPoint_A->y(),
76                                                          aPoint_B->x(), aPoint_B->y()));
77     boost::shared_ptr<GeomAPI_Pnt2d> aProjectedPoint = aFeatureLin->project(aFlyOutPnt);
78     double aDistance = aFlyOutPnt->distance(aProjectedPoint);
79     if (!aFeatureLin->isRight(aFlyOutPnt))
80       aDistance = -aDistance;
81     aFlyout = aDistance;
82   }
83
84   //Build dimension here
85   boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPoint_A->x(), aPoint_A->y());
86   boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPoint_B->x(), aPoint_B->y());
87
88   // value calculation
89   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
90     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
91   double aValue = aValueAttr->value();
92
93   Handle(AIS_InteractiveObject) anAIS = thePrevious;
94   if (anAIS.IsNull())
95   {
96     Handle(AIS_LengthDimension) aDimAIS = 
97       new AIS_LengthDimension(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
98     aDimAIS->SetCustomValue(aValue);
99
100     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
101     anAspect->MakeArrows3d (Standard_False);
102     anAspect->MakeText3d(false);
103     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
104     anAspect->MakeTextShaded(false);
105     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
106     aDimAIS->SetDimensionAspect (anAspect);
107     aDimAIS->SetFlyout(aFlyout);
108     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
109
110     anAIS = aDimAIS;
111   }
112   else {
113     // update presentation
114     Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
115     if (!aDimAIS.IsNull()) {
116       aDimAIS->SetMeasuredGeometry(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
117       aDimAIS->SetCustomValue(aValue);
118       aDimAIS->SetFlyout(aFlyout);
119
120       aDimAIS->Redisplay(Standard_True);
121     }
122   }
123   return anAIS;
124 }
125
126 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
127 {
128   boost::shared_ptr<ModelAPI_Data> aData = data();
129   if (!aData->isValid())
130     return;
131
132   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
133         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
134   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
135 }
136
137 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
138                                                        const std::string& theAttribute)
139 {
140   boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
141
142   if (!theData)
143     return aPointAttr;
144
145   FeaturePtr aFeature;
146   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
147     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
148   if (anAttr)
149     aFeature = anAttr->feature();
150
151   if (aFeature && aFeature->getKind() == SKETCH_POINT_KIND)
152     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
153                                            (aFeature->data()->attribute(POINT_ATTR_COORD));
154   else {
155     if (anAttr->attr())
156       aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
157   }
158   return aPointAttr;
159 }