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