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