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