1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_ConstraintDistance.cpp
4 // Created: 23 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_ConstraintDistance.h"
8 #include <SketchPlugin_Point.h>
9 #include <SketchPlugin_Circle.h>
10 #include <SketchPlugin_Line.h>
12 #include <GeomAPI_Lin2d.h>
13 #include <GeomAPI_Pnt2d.h>
14 #include <GeomDataAPI_Point2D.h>
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_Data.h>
19 #include <Config_PropManager.h>
21 const double tolerance = 1e-7;
23 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
27 //*************************************************************************************
28 void SketchPlugin_ConstraintDistance::initAttributes()
30 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
31 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
32 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
33 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
36 //*************************************************************************************
37 void SketchPlugin_ConstraintDistance::execute()
39 std::shared_ptr<ModelAPI_Data> aData = data();
40 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
41 aData->attribute(SketchPlugin_Constraint::VALUE()));
43 if(anAttrValue->isInitialized())
46 double aDistance = calculateCurrentDistance();
48 anAttrValue->setValue(aDistance);
51 // the value should to be computed here, not in the getAISObject in order to change the model value
52 // inside the object transaction. This is important for creating a constraint by preselection.
53 // The display of the presentation in this case happens after the transaction commit
54 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
55 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
56 if(!aFlyOutAttr->isInitialized())
57 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
60 bool SketchPlugin_ConstraintDistance::compute(const std::string& theAttributeId)
62 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
68 DataPtr aData = data();
69 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
70 aData, SketchPlugin_Constraint::ENTITY_A());
71 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
72 aData, SketchPlugin_Constraint::ENTITY_B());
74 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
75 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
77 if (aPoint_A && aPoint_B) {
78 aPnt_A = aPoint_A->pnt();
79 aPnt_B = aPoint_B->pnt();
80 } else if (!aPoint_A && aPoint_B) {
81 std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
82 aData, SketchPlugin_Constraint::ENTITY_A());
84 aPnt_B = aPoint_B->pnt();
85 aPnt_A = getProjectionPoint(aLine, aPnt_B);
87 } else if (aPoint_A && !aPoint_B) {
88 std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
89 aData, SketchPlugin_Constraint::ENTITY_B());
91 aPnt_A = aPoint_A->pnt();
92 aPnt_B = getProjectionPoint(aLine, aPnt_A);
95 if (!aPnt_A || !aPnt_B)
98 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
99 GeomDataAPI_Point2D>(aData->attribute(theAttributeId));
101 std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
102 std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
103 // it is not possible to create lin2d on the points with equal position
104 if (aPoint1->distance(aPoint1) < tolerance)
107 std::shared_ptr<GeomAPI_Lin2d> aLine = std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPnt_A, aPnt_B));
108 double aDist = aPoint1->distance(aPoint2)/5.;
109 std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
110 aFlyOutAttr->setValue(aFPnt);
115 //*************************************************************************************
116 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
121 std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
123 DataPtr aData = data();
124 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
125 aData, SketchPlugin_Constraint::ENTITY_A());
126 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
127 aData, SketchPlugin_Constraint::ENTITY_B());
129 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
130 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
132 if (aPoint_A && aPoint_B) {
133 aPnt_A = aPoint_A->pnt();
134 aPnt_B = aPoint_B->pnt();
135 } else if (!aPoint_A && aPoint_B) {
136 std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
137 aData, SketchPlugin_Constraint::ENTITY_A());
139 aPnt_B = aPoint_B->pnt();
140 aPnt_A = getProjectionPoint(aLine, aPnt_B);
142 } else if (aPoint_A && !aPoint_B) {
143 std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
144 aData, SketchPlugin_Constraint::ENTITY_B());
146 aPnt_A = aPoint_A->pnt();
147 aPnt_B = getProjectionPoint(aLine, aPnt_A);
150 if (!aPnt_A || !aPnt_B)
151 return AISObjectPtr();
153 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
154 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
156 std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
157 std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
158 if(!aFlyOutAttr->isInitialized())
159 return AISObjectPtr();
160 std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()/*aFPnt->x(), aFPnt->y()*/);
163 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
164 ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
166 // Issue #196: checking the positivity of the distance constraint
167 // there is a validator for a distance constraint, that the value should be positive
168 // in case if an invalid value is set, the current distance value is shown
169 if (aValueAttr->isInitialized())
170 aValue = aValueAttr->value();
172 AISObjectPtr anAIS = thePrevious;
174 anAIS = AISObjectPtr(new GeomAPI_AISObject);
175 anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
177 // Set color from preferences
178 std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
179 SKETCH_DIMENSION_COLOR);
180 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
184 //*************************************************************************************
185 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
187 std::shared_ptr<ModelAPI_Data> aData = data();
188 if (!aData->isValid())
191 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192 aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
193 aPoint->move(theDeltaX, theDeltaY);
196 double SketchPlugin_ConstraintDistance::calculateCurrentDistance() const
198 double aDistance = -1.;
200 std::shared_ptr<ModelAPI_Data> aData = data();
201 std::shared_ptr<GeomDataAPI_Point2D> aPointA =
202 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
203 std::shared_ptr<GeomDataAPI_Point2D> aPointB =
204 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
206 if (aPointA && aPointB) { // both points
207 aDistance = aPointA->pnt()->distance(aPointB->pnt());
209 if (!aPointA && aPointB) { //Line and point
210 std::shared_ptr<SketchPlugin_Line> aLine =
211 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
213 aDistance = aLine->distanceToPoint(aPointB->pnt());
215 } else if (aPointA && !aPointB) { // Point and line
216 std::shared_ptr<SketchPlugin_Line> aLine =
217 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
219 aDistance = aLine->distanceToPoint(aPointA->pnt());
226 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID) {
227 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
228 theID == SketchPlugin_Constraint::ENTITY_B())
230 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
231 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
232 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
233 double aDistance = calculateCurrentDistance();
234 if (aDistance > 0) { // set as value the length of updated references
235 aValueAttr->setValue(aDistance);
241 //*************************************************************************************
242 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
243 const std::string& theAttribute)
245 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
251 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
252 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
254 aFeature = ModelAPI_Feature::feature(anAttr->object());
256 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
257 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
258 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
259 else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
260 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
261 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
262 else if (anAttr->attr()) {
263 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
268 //*************************************************************************************
269 std::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData,
270 const std::string& theAttribute)
272 std::shared_ptr<SketchPlugin_Line> aLine;
276 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
277 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
279 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
280 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
281 aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
287 //*************************************************************************************
288 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(
289 const std::shared_ptr<SketchPlugin_Line>& theLine,
290 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
292 std::shared_ptr<ModelAPI_Data> aData = theLine->data();
293 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
294 aData->attribute(SketchPlugin_Line::START_ID()));
295 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
296 aData->attribute(SketchPlugin_Line::END_ID()));
298 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
299 return aLin2d.project(thePoint);