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 boost::shared_ptr<ModelAPI_Data> aData = data();
36 AttributeDoublePtr anAttrValue = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
37 aData->attribute(SketchPlugin_Constraint::VALUE()));
39 if(anAttrValue->isInitialized())
41 boost::shared_ptr<GeomDataAPI_Point2D> aPointA =
42 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
43 boost::shared_ptr<GeomDataAPI_Point2D> aPointB =
44 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
46 double aDistance = -1.;
47 if (aPointA && aPointB) { // both points
48 aDistance = aPointA->pnt()->distance(aPointB->pnt());
50 if (!aPointA && aPointB) { //Line and point
51 boost::shared_ptr<SketchPlugin_Line> aLine =
52 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
54 aDistance = aLine->distanceToPoint(aPointB->pnt());
56 } else if (aPointA && !aPointB) { // Point and line
57 boost::shared_ptr<SketchPlugin_Line> aLine =
58 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
60 aDistance = aLine->distanceToPoint(aPointA->pnt());
65 anAttrValue->setValue(aDistance);
69 //*************************************************************************************
70 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
75 boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
77 DataPtr aData = data();
78 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
79 aData, SketchPlugin_Constraint::ENTITY_A());
80 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
81 aData, SketchPlugin_Constraint::ENTITY_B());
83 boost::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
84 boost::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
86 if (aPoint_A && aPoint_B) {
87 aPnt_A = aPoint_A->pnt();
88 aPnt_B = aPoint_B->pnt();
89 } else if (!aPoint_A && aPoint_B) {
90 boost::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
91 aData, SketchPlugin_Constraint::ENTITY_A());
93 aPnt_B = aPoint_B->pnt();
94 aPnt_A = getProjectionPoint(aLine, aPnt_B);
96 } else if (aPoint_A && !aPoint_B) {
97 boost::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
98 aData, SketchPlugin_Constraint::ENTITY_B());
100 aPnt_A = aPoint_A->pnt();
101 aPnt_B = getProjectionPoint(aLine, aPnt_A);
104 if (!aPnt_A || !aPnt_B)
107 boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = boost::dynamic_pointer_cast<
108 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
110 boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
111 boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
112 boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt =
113 aFlyOutAttr->isInitialized() ?
114 sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()) : boost::shared_ptr<GeomAPI_Pnt>();
117 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
118 ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
119 double aValue = aValueAttr->value();
121 AISObjectPtr anAIS = thePrevious;
123 anAIS = AISObjectPtr(new GeomAPI_AISObject);
124 anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
126 // Set color from preferences
127 std::vector<int> aRGB = Config_PropManager::color("Visualization", "distance_color",
129 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
133 //*************************************************************************************
134 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
136 boost::shared_ptr<ModelAPI_Data> aData = data();
137 if (!aData->isValid())
140 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
141 aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
142 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
145 //*************************************************************************************
146 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
147 const std::string& theAttribute)
149 boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
155 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
156 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
158 aFeature = ModelAPI_Feature::feature(anAttr->object());
160 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
161 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
162 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
163 else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
164 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
165 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
166 else if (anAttr->attr()) {
167 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
172 //*************************************************************************************
173 boost::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData,
174 const std::string& theAttribute)
176 boost::shared_ptr<SketchPlugin_Line> aLine;
180 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
181 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
183 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
184 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
185 aLine = boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
191 //*************************************************************************************
192 boost::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(
193 const boost::shared_ptr<SketchPlugin_Line>& theLine,
194 const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
196 boost::shared_ptr<ModelAPI_Data> aData = theLine->data();
197 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
198 aData->attribute(SketchPlugin_Line::START_ID()));
199 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
200 aData->attribute(SketchPlugin_Line::END_ID()));
202 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
203 return aLin2d.project(thePoint);