1 // File: SketchPlugin_ConstraintDistance.cpp
2 // Created: 23 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchPlugin_ConstraintDistance.h"
6 #include <SketchPlugin_Point.h>
7 #include <SketchPlugin_Circle.h>
8 #include <SketchPlugin_Line.h>
10 #include <GeomAPI_Lin2d.h>
11 #include <GeomAPI_Pnt2d.h>
12 #include <GeomDataAPI_Point2D.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_Data.h>
17 #include <Config_PropManager.h>
19 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
23 //*************************************************************************************
24 void SketchPlugin_ConstraintDistance::initAttributes()
26 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
27 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
28 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
29 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
32 //*************************************************************************************
33 void SketchPlugin_ConstraintDistance::execute()
35 std::shared_ptr<ModelAPI_Data> aData = data();
36 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
37 aData->attribute(SketchPlugin_Constraint::VALUE()));
39 if(anAttrValue->isInitialized())
42 double aDistance = calculateCurrentDistance();
44 anAttrValue->setValue(aDistance);
48 //*************************************************************************************
49 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
54 std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
56 DataPtr aData = data();
57 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
58 aData, SketchPlugin_Constraint::ENTITY_A());
59 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
60 aData, SketchPlugin_Constraint::ENTITY_B());
62 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
63 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
65 if (aPoint_A && aPoint_B) {
66 aPnt_A = aPoint_A->pnt();
67 aPnt_B = aPoint_B->pnt();
68 } else if (!aPoint_A && aPoint_B) {
69 std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
70 aData, SketchPlugin_Constraint::ENTITY_A());
72 aPnt_B = aPoint_B->pnt();
73 aPnt_A = getProjectionPoint(aLine, aPnt_B);
75 } else if (aPoint_A && !aPoint_B) {
76 std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
77 aData, SketchPlugin_Constraint::ENTITY_B());
79 aPnt_A = aPoint_A->pnt();
80 aPnt_B = getProjectionPoint(aLine, aPnt_A);
83 if (!aPnt_A || !aPnt_B)
84 return AISObjectPtr();
86 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
87 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
89 std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
90 std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
91 std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = std::shared_ptr<GeomAPI_Pnt>();
92 if(aFlyOutAttr->isInitialized()) {
93 aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
95 std::shared_ptr<GeomAPI_Lin2d> aLine = std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPnt_A, aPnt_B));
96 double aDist = aPoint1->distance(aPoint2)/5.;
97 std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
98 aFlyOutAttr->setValue(aFPnt);
99 aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y());
102 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
103 ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
104 // TODO: has to be calculated on definition of reference object
106 // Issue #196: checking the positivity of the distance constraint
107 // there is a validator for a distance constraint, that the value should be positive
108 // in case if an invalid value is set, the current distance value is shown
109 if (aValueAttr->isInitialized())
110 aValue = aValueAttr->value();
112 aValue = calculateCurrentDistance();
113 aValueAttr->setValue(aValue);
117 AISObjectPtr anAIS = thePrevious;
119 anAIS = AISObjectPtr(new GeomAPI_AISObject);
120 anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
122 // Set color from preferences
123 std::vector<int> aRGB = Config_PropManager::color("Visualization", "distance_color",
125 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
129 //*************************************************************************************
130 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
132 std::shared_ptr<ModelAPI_Data> aData = data();
133 if (!aData->isValid())
136 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
137 aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
138 aPoint->move(theDeltaX, theDeltaY);
141 double SketchPlugin_ConstraintDistance::calculateCurrentDistance() const
143 double aDistance = -1.;
145 std::shared_ptr<ModelAPI_Data> aData = data();
146 std::shared_ptr<GeomDataAPI_Point2D> aPointA =
147 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
148 std::shared_ptr<GeomDataAPI_Point2D> aPointB =
149 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
151 if (aPointA && aPointB) { // both points
152 aDistance = aPointA->pnt()->distance(aPointB->pnt());
154 if (!aPointA && aPointB) { //Line and point
155 std::shared_ptr<SketchPlugin_Line> aLine =
156 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
158 aDistance = aLine->distanceToPoint(aPointB->pnt());
160 } else if (aPointA && !aPointB) { // Point and line
161 std::shared_ptr<SketchPlugin_Line> aLine =
162 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
164 aDistance = aLine->distanceToPoint(aPointA->pnt());
172 //*************************************************************************************
173 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
174 const std::string& theAttribute)
176 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
182 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
183 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
185 aFeature = ModelAPI_Feature::feature(anAttr->object());
187 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
188 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
189 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
190 else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
191 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
193 else if (anAttr->attr()) {
194 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
199 //*************************************************************************************
200 std::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData,
201 const std::string& theAttribute)
203 std::shared_ptr<SketchPlugin_Line> aLine;
207 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
208 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
210 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
211 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
212 aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
218 //*************************************************************************************
219 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(
220 const std::shared_ptr<SketchPlugin_Line>& theLine,
221 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
223 std::shared_ptr<ModelAPI_Data> aData = theLine->data();
224 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
225 aData->attribute(SketchPlugin_Line::START_ID()));
226 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
227 aData->attribute(SketchPlugin_Line::END_ID()));
229 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
230 return aLin2d.project(thePoint);