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 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
25 //*************************************************************************************
26 void SketchPlugin_ConstraintDistance::initAttributes()
28 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
29 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
30 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
31 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
34 //*************************************************************************************
35 void SketchPlugin_ConstraintDistance::execute()
37 std::shared_ptr<ModelAPI_Data> aData = data();
38 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
39 aData->attribute(SketchPlugin_Constraint::VALUE()));
41 if(anAttrValue->isInitialized())
44 double aDistance = calculateCurrentDistance();
46 anAttrValue->setValue(aDistance);
49 // the value should to be computed here, not in the getAISObject in order to change the model value
50 // inside the object transaction. This is important for creating a constraint by preselection.
51 // The display of the presentation in this case happens after the transaction commit
52 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
53 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
54 if(!aFlyOutAttr->isInitialized())
55 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
58 bool SketchPlugin_ConstraintDistance::compute(const std::string& theAttributeId)
60 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
66 DataPtr aData = data();
67 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
68 aData, SketchPlugin_Constraint::ENTITY_A());
69 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
70 aData, SketchPlugin_Constraint::ENTITY_B());
72 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
73 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
75 if (aPoint_A && aPoint_B) {
76 aPnt_A = aPoint_A->pnt();
77 aPnt_B = aPoint_B->pnt();
78 } else if (!aPoint_A && aPoint_B) {
79 std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
80 aData, SketchPlugin_Constraint::ENTITY_A());
82 aPnt_B = aPoint_B->pnt();
83 aPnt_A = getProjectionPoint(aLine, aPnt_B);
85 } else if (aPoint_A && !aPoint_B) {
86 std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
87 aData, SketchPlugin_Constraint::ENTITY_B());
89 aPnt_A = aPoint_A->pnt();
90 aPnt_B = getProjectionPoint(aLine, aPnt_A);
93 if (!aPnt_A || !aPnt_B)
96 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
97 GeomDataAPI_Point2D>(aData->attribute(theAttributeId));
99 std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
100 std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
101 std::shared_ptr<GeomAPI_Lin2d> aLine = std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPnt_A, aPnt_B));
102 double aDist = aPoint1->distance(aPoint2)/5.;
103 std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
104 aFlyOutAttr->setValue(aFPnt);
109 //*************************************************************************************
110 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
115 std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
117 DataPtr aData = data();
118 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
119 aData, SketchPlugin_Constraint::ENTITY_A());
120 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
121 aData, SketchPlugin_Constraint::ENTITY_B());
123 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
124 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
126 if (aPoint_A && aPoint_B) {
127 aPnt_A = aPoint_A->pnt();
128 aPnt_B = aPoint_B->pnt();
129 } else if (!aPoint_A && aPoint_B) {
130 std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
131 aData, SketchPlugin_Constraint::ENTITY_A());
133 aPnt_B = aPoint_B->pnt();
134 aPnt_A = getProjectionPoint(aLine, aPnt_B);
136 } else if (aPoint_A && !aPoint_B) {
137 std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
138 aData, SketchPlugin_Constraint::ENTITY_B());
140 aPnt_A = aPoint_A->pnt();
141 aPnt_B = getProjectionPoint(aLine, aPnt_A);
144 if (!aPnt_A || !aPnt_B)
145 return AISObjectPtr();
147 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
148 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
150 std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
151 std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
152 if(!aFlyOutAttr->isInitialized())
153 return AISObjectPtr();
154 std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()/*aFPnt->x(), aFPnt->y()*/);
157 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
158 ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
160 // Issue #196: checking the positivity of the distance constraint
161 // there is a validator for a distance constraint, that the value should be positive
162 // in case if an invalid value is set, the current distance value is shown
163 if (aValueAttr->isInitialized())
164 aValue = aValueAttr->value();
166 AISObjectPtr anAIS = thePrevious;
168 anAIS = AISObjectPtr(new GeomAPI_AISObject);
169 anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
171 // Set color from preferences
172 std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
173 SKETCH_DIMENSION_COLOR);
174 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
178 //*************************************************************************************
179 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
181 std::shared_ptr<ModelAPI_Data> aData = data();
182 if (!aData->isValid())
185 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
186 aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
187 aPoint->move(theDeltaX, theDeltaY);
190 double SketchPlugin_ConstraintDistance::calculateCurrentDistance() const
192 double aDistance = -1.;
194 std::shared_ptr<ModelAPI_Data> aData = data();
195 std::shared_ptr<GeomDataAPI_Point2D> aPointA =
196 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
197 std::shared_ptr<GeomDataAPI_Point2D> aPointB =
198 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
200 if (aPointA && aPointB) { // both points
201 aDistance = aPointA->pnt()->distance(aPointB->pnt());
203 if (!aPointA && aPointB) { //Line and point
204 std::shared_ptr<SketchPlugin_Line> aLine =
205 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
207 aDistance = aLine->distanceToPoint(aPointB->pnt());
209 } else if (aPointA && !aPointB) { // Point and line
210 std::shared_ptr<SketchPlugin_Line> aLine =
211 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
213 aDistance = aLine->distanceToPoint(aPointA->pnt());
220 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID) {
221 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
222 theID == SketchPlugin_Constraint::ENTITY_B())
224 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
225 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
226 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
227 double aDistance = calculateCurrentDistance();
228 if (aDistance > 0) { // set as value the length of updated references
229 aValueAttr->setValue(aDistance);
235 //*************************************************************************************
236 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
237 const std::string& theAttribute)
239 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
245 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
246 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
248 aFeature = ModelAPI_Feature::feature(anAttr->object());
250 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
251 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
252 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
253 else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
254 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
255 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
256 else if (anAttr->attr()) {
257 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
262 //*************************************************************************************
263 std::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData,
264 const std::string& theAttribute)
266 std::shared_ptr<SketchPlugin_Line> aLine;
270 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
271 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
273 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
274 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
275 aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
281 //*************************************************************************************
282 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(
283 const std::shared_ptr<SketchPlugin_Line>& theLine,
284 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
286 std::shared_ptr<ModelAPI_Data> aData = theLine->data();
287 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
288 aData->attribute(SketchPlugin_Line::START_ID()));
289 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
290 aData->attribute(SketchPlugin_Line::END_ID()));
292 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
293 return aLin2d.project(thePoint);