1 // File: SketchPlugin_ConstraintLength.cpp
2 // Created: 30 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchPlugin_ConstraintLength.h"
6 #include <SketchPlugin_Line.h>
8 #include <GeomDataAPI_Point2D.h>
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Result.h>
14 #include <GeomDataAPI_Point2D.h>
16 #include <GeomAPI_Lin2d.h>
17 #include <GeomAPI_Pnt2d.h>
19 #include <Config_PropManager.h>
21 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
25 void SketchPlugin_ConstraintLength::initAttributes()
27 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
28 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
29 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
32 void SketchPlugin_ConstraintLength::execute()
34 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
35 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
36 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
39 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<
40 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
41 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<
42 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
44 double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
46 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
47 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
48 if(!aValueAttr->isInitialized()) {
49 aValueAttr->setValue(aLenght);
54 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
59 boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
61 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
62 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
65 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
66 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
69 boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = boost::dynamic_pointer_cast<
70 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
72 DataPtr aData = aFeature->data();
73 boost::shared_ptr<GeomDataAPI_Point2D> aStartPoint = boost::dynamic_pointer_cast<
74 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
75 boost::shared_ptr<GeomDataAPI_Point2D> anEndPoint = boost::dynamic_pointer_cast<
76 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
78 boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aStartPoint->x(), aStartPoint->y());
79 boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(anEndPoint->x(), anEndPoint->y());
80 boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = boost::shared_ptr<GeomAPI_Pnt>();
81 if (aFlyOutAttr->isInitialized()) {
82 aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
84 boost::shared_ptr<GeomAPI_Lin2d> aLine =
85 boost::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
86 double aDist = aPoint1->distance(aPoint2)/5.;
87 boost::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
88 aFlyOutAttr->setValue(aFPnt);
89 aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y());
92 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
93 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
94 double aValue = aValueAttr->value();
96 AISObjectPtr anAIS = thePrevious;
98 anAIS = AISObjectPtr(new GeomAPI_AISObject);
99 anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
101 // Set color from preferences
102 std::vector<int> aRGB = Config_PropManager::color("Visualization", "length_color", LENGTH_COLOR);
103 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
107 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
109 boost::shared_ptr<ModelAPI_Data> aData = data();
110 if (!aData->isValid())
113 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
114 aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
115 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);