]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintLength.cpp
Salome HOME
Issue #394 Undo-ing a Sketch element
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintLength.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintLength.cpp
4 // Created: 30 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintLength.h"
8 #include <SketchPlugin_Line.h>
9
10 #include <GeomDataAPI_Point2D.h>
11
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_Result.h>
15
16 #include <GeomDataAPI_Point2D.h>
17
18 #include <GeomAPI_Lin2d.h>
19 #include <GeomAPI_Pnt2d.h>
20
21 #include <Config_PropManager.h>
22
23 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
24 {
25 }
26
27 void SketchPlugin_ConstraintLength::initAttributes()
28 {
29   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
30   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
31   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
32 }
33
34 void SketchPlugin_ConstraintLength::execute()
35 {
36   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
37       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
38   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
39   if (aFeature) {
40     // set length value
41     std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
42         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
43     std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
44         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
45
46     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
47
48     //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
49     //    ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
50     //if(!aValueAttr->isInitialized()) {
51     //  aValueAttr->setValue(aLenght);
52     //}
53   }
54 }
55
56 bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
57 {
58   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
59     return false;
60
61   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
62   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
63   if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
64     return false;
65
66   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
67       GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
68
69   std::shared_ptr<GeomAPI_Lin2d> aLine = 
70     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
71   double aDist = aPoint1->distance(aPoint2)/5.;
72   std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
73   aFlyOutAttr->setValue(aFPnt);
74
75   return true;
76 }
77
78 bool SketchPlugin_ConstraintLength::getPoints(
79   std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
80   std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
81   std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
82 {
83   if (!sketch())
84     return false;
85   std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
86   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
87       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
88   if (!anAttr)
89     return false;
90   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
91   if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
92     return false;
93   DataPtr aData = aFeature->data();
94   theStartPoint = std::dynamic_pointer_cast<
95       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
96   theEndPoint = std::dynamic_pointer_cast<
97       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
98   thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
99   thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
100   return true;
101 }
102
103 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
104 {
105   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
106   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
107   if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
108     return thePrevious; // not possible to show length because points are not defined
109
110   AttributePtr aFlyOutAttribute = data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
111   if (!aFlyOutAttribute->isInitialized()) {
112     if (!compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))
113       return thePrevious; // not possible to show length because points are not defined
114   }
115   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
116                                                           GeomDataAPI_Point2D>(aFlyOutAttribute);
117   std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
118   // value calculation
119   // TODO: has to be calculated on definition of reference object
120   double aDistance = aPoint1->distance(aPoint2);
121   std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
122       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
123   double aValue = aDistance;
124   if (aValueAttr->isInitialized())
125     aValue = aValueAttr->value();
126   else 
127     aValueAttr->setValue(aValue);
128   // End TODO
129
130   AISObjectPtr anAIS = thePrevious;
131   if (!anAIS)
132     anAIS = AISObjectPtr(new GeomAPI_AISObject);
133   std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
134   anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
135
136   // Set color from preferences
137   std::vector<int> aRGB = 
138     Config_PropManager::color("Visualization", "length_color", LENGTH_COLOR);
139   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
140   return anAIS;
141 }
142
143 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
144 {
145   std::shared_ptr<ModelAPI_Data> aData = data();
146   if (!aData->isValid())
147     return;
148
149   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
150       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
151   aPoint->move(theDeltaX, theDeltaY);
152 }
153
154 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
155   if (theID == SketchPlugin_Constraint::ENTITY_A()) 
156   {
157     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
158       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
159     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
160       std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
161       std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
162       if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
163         double aLength = aPoint1->distance(aPoint2);
164         aValueAttr->setValue(aLength);
165       }
166     }
167   }
168 }