1 // File: PartSet_FeaturePrs.h
2 // Created: 16 Jun 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_ConstraintLengthPrs.h>
6 #include <PartSet_Tools.h>
7 #include <PartSet_Constants.h>
9 #include <PartSet_FeatureLinePrs.h>
11 #include <SketchPlugin_Feature.h>
12 #include <SketchPlugin_Sketch.h>
13 #include <SketchPlugin_Line.h>
14 #include <SketchPlugin_ConstraintLength.h>
16 #include <GeomDataAPI_Point.h>
17 #include <GeomDataAPI_Point2D.h>
18 #include <GeomDataAPI_Dir.h>
19 #include <GeomAPI_Pnt2d.h>
20 #include <GeomAPI_Lin2d.h>
21 #include <GeomAPI_Pln.h>
23 #include <AIS_InteractiveObject.hxx>
24 #include <AIS_LengthDimension.hxx>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Document.h>
28 #include <ModelAPI_AttributeRefAttr.h>
29 #include <ModelAPI_AttributeRefList.h>
30 #include <ModelAPI_AttributeDouble.h>
32 #include <AIS_InteractiveObject.hxx>
35 #include <Precision.hxx>
39 PartSet_ConstraintLengthPrs::PartSet_ConstraintLengthPrs(FeaturePtr theSketch)
40 : PartSet_FeaturePrs(theSketch)
44 std::string PartSet_ConstraintLengthPrs::getKind()
46 return SKETCH_CONSTRAINT_LENGTH_KIND;
49 PartSet_SelectionMode PartSet_ConstraintLengthPrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode)
51 PartSet_SelectionMode aMode = theMode;
52 if (feature() && theFeature && theFeature->getKind() == SKETCH_LINE_KIND && theMode == SM_FirstPoint)
55 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
56 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
57 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
58 aRef->setFeature(theFeature);
61 aData = theFeature->data();
62 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
63 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
64 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
65 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
67 double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
68 PartSet_Tools::setFeatureValue(feature(), aLenght, CONSTRAINT_ATTR_VALUE);
74 PartSet_SelectionMode PartSet_ConstraintLengthPrs::setPoint(double theX, double theY,
75 const PartSet_SelectionMode& theMode)
77 PartSet_SelectionMode aMode = theMode;
81 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
82 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
83 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
86 aFeature = anAttr->feature();
87 if (aFeature->getKind() != SKETCH_LINE_KIND) {
88 aFeature = FeaturePtr();
91 boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>
92 (new GeomAPI_Pnt2d(theX, theY));
93 boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = PartSet_FeatureLinePrs::createLin2d(aFeature);
94 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
95 double aDistance = aPoint->distance(aResult);
97 double aStartX, aStartY;
98 PartSet_FeatureLinePrs::getLinePoint(aFeature, LINE_ATTR_START, aStartX, aStartY);
100 if (!aFeatureLin->isRight(aPoint))
101 aDistance = -aDistance;
103 AttributeDoublePtr aFlyoutAttr =
104 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
105 aFlyoutAttr->setValue(aDistance);
107 aMode = SM_DonePoint;
116 Handle(AIS_InteractiveObject) PartSet_ConstraintLengthPrs::createPresentation(FeaturePtr theFeature,
117 FeaturePtr theSketch,
118 Handle(AIS_InteractiveObject) thePreviuos)
120 if (!theFeature || !theSketch)
123 boost::shared_ptr<GeomAPI_Pln> aGPlane = PartSet_Tools::sketchPlane(theSketch);
124 gp_Pln aPlane = aGPlane->impl<gp_Pln>();
126 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
127 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
128 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
131 FeaturePtr aFeature = anAttr->feature();
132 if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
135 boost::shared_ptr<ModelAPI_AttributeDouble> aFlyoutAttr =
136 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
137 double aFlyout = aFlyoutAttr->value();
139 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
140 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
141 double aValue = aValueAttr->value();
143 aData = aFeature->data();
144 if (!aData->isValid())
147 boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
148 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
149 boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
150 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
152 gp_Pnt aPoint1, aPoint2;
153 PartSet_Tools::convertTo3D(aPointStart->x(), aPointStart->y(), theSketch, aPoint1);
154 PartSet_Tools::convertTo3D(aPointEnd->x(), aPointEnd->y(), theSketch, aPoint2);
156 //Build dimension here
157 gp_Pnt aP1 = aPoint1;
158 gp_Pnt aP2 = aPoint2;
164 Handle(AIS_InteractiveObject) anAIS = thePreviuos;
167 Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension (aP1, aP2, aPlane);
168 aDimAIS->SetCustomValue(aValue);
170 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
171 anAspect->MakeArrows3d (Standard_False);
172 anAspect->MakeText3d(false/*is text 3d*/);
173 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
174 anAspect->MakeTextShaded(false/*is test shaded*/);
175 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false/*is units displayed*/);
176 /*if (isUnitsDisplayed)
178 aDimAIS->SetDisplayUnits (aDimDlg->GetUnits ());
180 aDimAIS->SetDimensionAspect (anAspect);
181 aDimAIS->SetFlyout(aFlyout);
182 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
187 // update presentation
188 Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
189 if (!aDimAIS.IsNull()) {
190 aDimAIS->SetMeasuredGeometry(aPoint1, aPoint2, aPlane);
191 aDimAIS->SetCustomValue(aValue);
192 aDimAIS->SetFlyout(aFlyout);
194 aDimAIS->Redisplay(Standard_True);
200 std::string PartSet_ConstraintLengthPrs::getAttribute(const PartSet_SelectionMode& theMode) const
205 PartSet_SelectionMode PartSet_ConstraintLengthPrs::getNextMode(const std::string& theAttribute) const
207 return SM_FirstPoint;
210 void PartSet_ConstraintLengthPrs::move(double theDeltaX, double theDeltaY)
214 double PartSet_ConstraintLengthPrs::distanceToPoint(FeaturePtr theFeature,
215 double theX, double theY)
220 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintLengthPrs::findPoint(FeaturePtr theFeature,
221 double theX, double theY)
223 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
227 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintLengthPrs::featurePoint
228 (const PartSet_SelectionMode& theMode)
230 return boost::shared_ptr<GeomDataAPI_Point2D>();