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 anAttr_Value =
37 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
38 aData->attribute(SketchPlugin_Constraint::VALUE()));
40 if (!anAttr_Value->isInitialized()) {
41 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A =
42 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
43 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B =
44 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
46 if (aPoint_A && aPoint_B) { // both points
47 anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
49 if (!aPoint_A && aPoint_B) { //Line and point
50 boost::shared_ptr<SketchPlugin_Line> aLine =
51 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
53 anAttr_Value->setValue(aLine->distanceToPoint(aPoint_B->pnt()));
54 } else if (aPoint_A && !aPoint_B) { // Point and line
55 boost::shared_ptr<SketchPlugin_Line> aLine =
56 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
58 anAttr_Value->setValue(aLine->distanceToPoint(aPoint_A->pnt()));
64 //*************************************************************************************
65 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintDistance::getAISObject(
66 boost::shared_ptr<GeomAPI_AISObject> thePrevious)
71 boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
73 DataPtr aData = data();
74 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
75 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
77 boost::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
78 boost::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
80 if (aPoint_A && aPoint_B) {
81 aPnt_A = aPoint_A->pnt();
82 aPnt_B = aPoint_B->pnt();
83 } else if (!aPoint_A && aPoint_B) {
84 boost::shared_ptr<SketchPlugin_Line> aLine =
85 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
87 aPnt_B = aPoint_B->pnt();
88 aPnt_A = getProjectionPoint(aLine, aPnt_B);
90 } else if (aPoint_A && !aPoint_B) {
91 boost::shared_ptr<SketchPlugin_Line> aLine =
92 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
94 aPnt_A = aPoint_A->pnt();
95 aPnt_B = getProjectionPoint(aLine, aPnt_A);
98 if (!aPnt_A || !aPnt_B)
101 boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr =
102 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
104 boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
105 boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
106 boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = aFlyOutAttr->isInitialized() ?
107 sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()) :
108 boost::shared_ptr<GeomAPI_Pnt>();
111 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
112 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
113 double aValue = aValueAttr->value();
115 boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
117 anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
118 anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
120 // Set color from preferences
121 std::string aColor = Config_PropManager::value("Sketcher", "Visualization",
122 "distance_color", DISTANCE_COLOR);
123 std::vector<int> aRGB = stringToRGB(aColor);
124 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
128 //*************************************************************************************
129 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
131 boost::shared_ptr<ModelAPI_Data> aData = data();
132 if (!aData->isValid())
135 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
136 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
137 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
140 //*************************************************************************************
141 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
142 const std::string& theAttribute)
144 boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
150 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
151 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
153 aFeature = ModelAPI_Feature::feature(anAttr->object());
155 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
156 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
157 (aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
158 else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
159 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
160 (aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
161 else if (anAttr->attr()) {
162 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
167 //*************************************************************************************
168 boost::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData, const std::string& theAttribute)
170 boost::shared_ptr<SketchPlugin_Line> aLine;
174 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
175 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
177 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
178 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
179 aLine = boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
185 //*************************************************************************************
186 boost::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const boost::shared_ptr<SketchPlugin_Line>& theLine,
187 const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
189 boost::shared_ptr<ModelAPI_Data> aData = theLine->data();
190 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
191 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192 aData->attribute(SketchPlugin_Line::START_ID()));
193 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
194 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
195 aData->attribute(SketchPlugin_Line::END_ID()));
197 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
198 return aLin2d.project(thePoint);