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 = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
37 aData->attribute(SketchPlugin_Constraint::VALUE()));
39 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
40 aData, SketchPlugin_Constraint::ENTITY_A());
41 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
42 aData, SketchPlugin_Constraint::ENTITY_B());
44 if (aPoint_A && aPoint_B) { // both points
45 anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
47 if (!aPoint_A && aPoint_B) { //Line and point
48 boost::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
49 aData, SketchPlugin_Constraint::ENTITY_A());
51 anAttr_Value->setValue(aLine->distanceToPoint(aPoint_B->pnt()));
52 } else if (aPoint_A && !aPoint_B) { // Point and line
53 boost::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
54 aData, SketchPlugin_Constraint::ENTITY_B());
56 anAttr_Value->setValue(aLine->distanceToPoint(aPoint_A->pnt()));
61 //*************************************************************************************
62 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
67 boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
69 DataPtr aData = data();
70 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
71 aData, SketchPlugin_Constraint::ENTITY_A());
72 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
73 aData, SketchPlugin_Constraint::ENTITY_B());
75 boost::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
76 boost::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
78 if (aPoint_A && aPoint_B) {
79 aPnt_A = aPoint_A->pnt();
80 aPnt_B = aPoint_B->pnt();
81 } else if (!aPoint_A && aPoint_B) {
82 boost::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
83 aData, SketchPlugin_Constraint::ENTITY_A());
85 aPnt_B = aPoint_B->pnt();
86 aPnt_A = getProjectionPoint(aLine, aPnt_B);
88 } else if (aPoint_A && !aPoint_B) {
89 boost::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
90 aData, SketchPlugin_Constraint::ENTITY_B());
92 aPnt_A = aPoint_A->pnt();
93 aPnt_B = getProjectionPoint(aLine, aPnt_A);
96 if (!aPnt_A || !aPnt_B)
99 boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = boost::dynamic_pointer_cast<
100 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
102 boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
103 boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
104 boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt =
105 aFlyOutAttr->isInitialized() ?
106 sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()) : boost::shared_ptr<GeomAPI_Pnt>();
109 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
110 ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
111 double aValue = aValueAttr->value();
113 AISObjectPtr anAIS = thePrevious;
115 anAIS = AISObjectPtr(new GeomAPI_AISObject);
116 anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
118 // Set color from preferences
119 std::vector<int> aRGB = Config_PropManager::color("Visualization", "distance_color",
121 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
125 //*************************************************************************************
126 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
128 boost::shared_ptr<ModelAPI_Data> aData = data();
129 if (!aData->isValid())
132 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
133 aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
134 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
137 //*************************************************************************************
138 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
139 const std::string& theAttribute)
141 boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
147 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
148 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
150 aFeature = ModelAPI_Feature::feature(anAttr->object());
152 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
153 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
154 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
155 else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
156 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
157 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
158 else if (anAttr->attr()) {
159 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
164 //*************************************************************************************
165 boost::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData,
166 const std::string& theAttribute)
168 boost::shared_ptr<SketchPlugin_Line> aLine;
172 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
173 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
175 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
176 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
177 aLine = boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
183 //*************************************************************************************
184 boost::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(
185 const boost::shared_ptr<SketchPlugin_Line>& theLine,
186 const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
188 boost::shared_ptr<ModelAPI_Data> aData = theLine->data();
189 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190 aData->attribute(SketchPlugin_Line::START_ID()));
191 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192 aData->attribute(SketchPlugin_Line::END_ID()));
194 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
195 return aLin2d.project(thePoint);