1 // File: PartSet_FeaturePrs.h
2 // Created: 16 Jun 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_ConstraintDistancePrs.h>
6 #include <PartSet_Tools.h>
8 #include <PartSet_FeatureLinePrs.h>
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Sketch.h>
12 #include <SketchPlugin_Line.h>
13 #include <SketchPlugin_ConstraintDistance.h>
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomAPI_Lin2d.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeRefList.h>
23 #include <ModelAPI_AttributeDouble.h>
25 #include <AIS_InteractiveObject.hxx>
26 #include <Precision.hxx>
30 PartSet_ConstraintDistancePrs::PartSet_ConstraintDistancePrs(FeaturePtr theSketch)
31 : PartSet_FeaturePrs(theSketch)
35 std::string PartSet_ConstraintDistancePrs::getKind()
37 return SKETCH_CONSTRAINT_DISTANCE_KIND;
40 bool PartSet_ConstraintDistancePrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode)
43 if (feature() && theFeature && theFeature->getKind() == SKETCH_LINE_KIND && theMode == SM_FirstPoint)
46 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
47 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
48 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
49 aRef->setFeature(theFeature);
52 aData = theFeature->data();
53 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
54 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
55 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
56 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
58 double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
59 PartSet_Tools::setFeatureValue(feature(), aLenght, CONSTRAINT_ATTR_VALUE);
65 PartSet_SelectionMode PartSet_ConstraintDistancePrs::setPoint(double theX, double theY,
66 const PartSet_SelectionMode& theMode)
68 PartSet_SelectionMode aMode = theMode;
71 case SM_SecondPoint: {
72 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
73 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
74 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
77 aFeature = anAttr->feature();
78 if (aFeature->getKind() != SKETCH_LINE_KIND) {
79 aFeature = FeaturePtr();
82 boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>
83 (new GeomAPI_Pnt2d(theX, theY));
84 boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = PartSet_FeatureLinePrs::createLin2d(aFeature);
85 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
86 double aDistance = aPoint->distance(aResult);
88 double aStartX, aStartY;
89 PartSet_FeatureLinePrs::getLinePoint(aFeature, LINE_ATTR_START, aStartX, aStartY);
91 if (!aFeatureLin->isRight(aPoint))
92 aDistance = -aDistance;
94 AttributeDoublePtr aFlyoutAttr =
95 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
96 aFlyoutAttr->setValue(aDistance);
107 Handle(AIS_InteractiveObject) PartSet_ConstraintDistancePrs::createPresentation(FeaturePtr theFeature,
108 FeaturePtr theSketch,
109 Handle(AIS_InteractiveObject) thePreviuos)
111 Handle(AIS_InteractiveObject) anAIS = thePreviuos;
112 if (!theFeature || !theSketch)
115 boost::shared_ptr<GeomAPI_Pln> aGPlane = PartSet_Tools::sketchPlane(theSketch);
116 gp_Pln aPlane = aGPlane->impl<gp_Pln>();
118 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
119 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
120 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
123 FeaturePtr aFeature = anAttr->feature();
124 if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
127 boost::shared_ptr<ModelAPI_AttributeDouble> aFlyoutAttr =
128 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
129 double aFlyout = aFlyoutAttr->value();
131 aData = aFeature->data();
132 if (!aData->isValid())
135 boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
136 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
137 boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
138 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
140 gp_Pnt aPoint1, aPoint2;
141 PartSet_Tools::convertTo3D(aPointStart->x(), aPointStart->y(), theSketch, aPoint1);
142 PartSet_Tools::convertTo3D(aPointEnd->x(), aPointEnd->y(), theSketch, aPoint2);
144 //Build dimension here
145 gp_Pnt aP1 = aPoint1;
146 gp_Pnt aP2 = aPoint2;
152 Handle(AIS_InteractiveObject) anAIS = thePreviuos;
155 Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension (aP1, aP2, aPlane);
157 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
158 anAspect->MakeArrows3d (Standard_False);
159 anAspect->MakeText3d(false);
160 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
161 anAspect->MakeTextShaded(false);
162 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
163 //if (isUnitsDisplayed)
165 // aDimAIS->SetDisplayUnits (aDimDlg->GetUnits ());
167 aDimAIS->SetDimensionAspect (anAspect);
168 aDimAIS->SetFlyout(aFlyout);
169 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
174 // update presentation
175 Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
176 if (!aDimAIS.IsNull()) {
177 aDimAIS->SetMeasuredGeometry(aPoint1, aPoint2, aPlane);
178 aDimAIS->SetFlyout(aFlyout);
180 aDimAIS->Redisplay(Standard_True);
187 std::string PartSet_ConstraintDistancePrs::getAttribute(const PartSet_SelectionMode& theMode) const
192 PartSet_SelectionMode PartSet_ConstraintDistancePrs::getNextMode(const std::string& theAttribute) const
194 return SM_FirstPoint;
197 void PartSet_ConstraintDistancePrs::move(double theDeltaX, double theDeltaY)
201 double PartSet_ConstraintDistancePrs::distanceToPoint(FeaturePtr theFeature,
202 double theX, double theY)
207 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::findPoint(FeaturePtr theFeature,
208 double theX, double theY)
210 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
214 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::featurePoint
215 (const PartSet_SelectionMode& theMode)
217 return boost::shared_ptr<GeomDataAPI_Point2D>();