]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp
Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintDistance.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintDistance.cpp
4 // Created: 23 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintDistance.h"
8 #include <SketchPlugin_Point.h>
9 #include <SketchPlugin_Circle.h>
10 #include <SketchPlugin_Line.h>
11
12 #include <SketcherPrs_Tools.h>
13 #include <SketcherPrs_Factory.h>
14
15 #include <GeomAPI_Lin2d.h>
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomDataAPI_Point2D.h>
18
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_Data.h>
21
22 #include <Config_PropManager.h>
23
24 const double tolerance = 1e-7;
25
26
27 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
28 {
29 }
30
31 //*************************************************************************************
32 void SketchPlugin_ConstraintDistance::initAttributes()
33 {
34   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
35   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
36   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
37   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
38 }
39
40 //*************************************************************************************
41 void SketchPlugin_ConstraintDistance::execute()
42 {
43   std::shared_ptr<ModelAPI_Data> aData = data();
44   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
45       aData->attribute(SketchPlugin_Constraint::VALUE()));
46
47   if(anAttrValue->isInitialized())
48     return;
49
50   double aDistance = calculateCurrentDistance();
51   if(aDistance >= 0) {
52     anAttrValue->setValue(aDistance);
53   }
54
55   // the value should to be computed here, not in the getAISObject in order to change the model value
56   // inside the object transaction. This is important for creating a constraint by preselection.
57   // The display of the presentation in this case happens after the transaction commit
58   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
59       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
60   if(!aFlyOutAttr->isInitialized())
61     compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
62 }
63
64 bool SketchPlugin_ConstraintDistance::compute(const std::string& theAttributeId)
65 {
66   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
67     return false;
68
69   if (!sketch())
70     return false;
71
72   DataPtr aData = data();
73   std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
74       aData, SketchPlugin_Constraint::ENTITY_A());
75   std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
76       aData, SketchPlugin_Constraint::ENTITY_B());
77
78   std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
79   std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
80
81   if (aPoint_A && aPoint_B) {
82     aPnt_A = aPoint_A->pnt();
83     aPnt_B = aPoint_B->pnt();
84   } else if (!aPoint_A && aPoint_B) {
85     //std::shared_ptr<SketchPlugin_Line> aLine = SketcherPrs_Tools::getFeatureLine(
86     //    aData, SketchPlugin_Constraint::ENTITY_A());
87     //if (aLine) {
88     //  aPnt_B = aPoint_B->pnt();
89     //  aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
90     //}
91   } else if (aPoint_A && !aPoint_B) {
92     //std::shared_ptr<SketchPlugin_Line> aLine = SketcherPrs_Tools::getFeatureLine(
93     //    aData, SketchPlugin_Constraint::ENTITY_B());
94     //if (aLine) {
95     //  aPnt_A = aPoint_A->pnt();
96     //  aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
97     //}
98   }
99   if (!aPnt_A || !aPnt_B)
100     return false;
101
102   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
103                            GeomDataAPI_Point2D>(aData->attribute(theAttributeId));
104
105   std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
106   std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
107   // it is not possible to create lin2d on the points with equal position
108   if (aPoint1->distance(aPoint1) < tolerance)
109     return false;
110
111   std::shared_ptr<GeomAPI_Lin2d> aLine = std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPnt_A, aPnt_B));
112   double aDist = aPoint1->distance(aPoint2)/5.;
113   std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
114   aFlyOutAttr->setValue(aFPnt);
115
116   return true;
117 }
118
119 //*************************************************************************************
120 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
121 {
122   if (!sketch())
123     return thePrevious;
124
125   AISObjectPtr anAIS = thePrevious;
126   if (!anAIS) {
127     anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this, sketch()->coordinatePlane());
128   }
129
130   // Set color from preferences
131   std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
132                                                     SKETCH_DIMENSION_COLOR);
133   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
134   return anAIS;
135 }
136
137 //*************************************************************************************
138 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
139 {
140   std::shared_ptr<ModelAPI_Data> aData = data();
141   if (!aData->isValid())
142     return;
143
144   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
145       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
146   aPoint->move(theDeltaX, theDeltaY);
147 }
148
149 double SketchPlugin_ConstraintDistance::calculateCurrentDistance() const
150 {
151   double aDistance = -1.;
152
153   std::shared_ptr<ModelAPI_Data> aData = data();
154   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
155     SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
156   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
157       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
158
159   if (aPointA && aPointB) {  // both points
160     aDistance = aPointA->pnt()->distance(aPointB->pnt());
161   } else {
162 //    if (!aPointA && aPointB) {  //Line and point
163 //      std::shared_ptr<SketchPlugin_Line> aLine =
164 //          SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
165 //      if (aLine) {
166 //        aDistance = aLine->distanceToPoint(aPointB->pnt());
167 //      }
168 //    } else if (aPointA && !aPointB) {  // Point and line
169 //      std::shared_ptr<SketchPlugin_Line> aLine =
170 //          SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
171 //      if (aLine) {
172 //        aDistance = aLine->distanceToPoint(aPointA->pnt());
173 //      }
174 //    }
175   }
176   return aDistance;
177 }
178
179 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID) {
180   if (theID == SketchPlugin_Constraint::ENTITY_A() || 
181       theID == SketchPlugin_Constraint::ENTITY_B()) 
182   {
183     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
184         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
185     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
186       double aDistance = calculateCurrentDistance();
187       if (aDistance > 0) { // set as value the length of updated references
188         aValueAttr->setValue(aDistance);
189       }
190     }
191   }
192 }
193