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<ModelAPI_Data> aData = theSketch->data();
116 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
117 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
118 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
119 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
120 gp_Pln aPlane(aNormal->x(), aNormal->y(), aNormal->z(), 0);
122 aData = theFeature->data();
123 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
124 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
127 FeaturePtr aFeature = anAttr->feature();
128 if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
131 boost::shared_ptr<ModelAPI_AttributeDouble> aFlyoutAttr =
132 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
133 double aFlyout = aFlyoutAttr->value();
135 aData = aFeature->data();
136 if (!aData->isValid())
139 boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
140 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
141 boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
142 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
144 gp_Pnt aPoint1, aPoint2;
145 PartSet_Tools::convertTo3D(aPointStart->x(), aPointStart->y(), theSketch, aPoint1);
146 PartSet_Tools::convertTo3D(aPointEnd->x(), aPointEnd->y(), theSketch, aPoint2);
148 //Build dimension here
149 gp_Pnt aP1 = aPoint1;
150 gp_Pnt aP2 = aPoint2;
156 Handle(AIS_InteractiveObject) anAIS = thePreviuos;
159 Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension (aP1, aP2, aPlane);
161 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
162 anAspect->MakeArrows3d (Standard_False);
163 anAspect->MakeText3d(false);
164 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
165 anAspect->MakeTextShaded(false);
166 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
167 //if (isUnitsDisplayed)
169 // aDimAIS->SetDisplayUnits (aDimDlg->GetUnits ());
171 aDimAIS->SetDimensionAspect (anAspect);
172 aDimAIS->SetFlyout(aFlyout);
173 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
178 // update presentation
179 Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
180 if (!aDimAIS.IsNull()) {
181 aDimAIS->SetMeasuredGeometry(aPoint1, aPoint2, aPlane);
182 aDimAIS->SetFlyout(aFlyout);
184 aDimAIS->Redisplay(Standard_True);
191 std::string PartSet_ConstraintDistancePrs::getAttribute(const PartSet_SelectionMode& theMode) const
196 PartSet_SelectionMode PartSet_ConstraintDistancePrs::getNextMode(const std::string& theAttribute) const
198 return SM_FirstPoint;
201 void PartSet_ConstraintDistancePrs::move(double theDeltaX, double theDeltaY)
205 double PartSet_ConstraintDistancePrs::distanceToPoint(FeaturePtr theFeature,
206 double theX, double theY)
211 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::findPoint(FeaturePtr theFeature,
212 double theX, double theY)
214 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
218 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::featurePoint
219 (const PartSet_SelectionMode& theMode)
221 return boost::shared_ptr<GeomDataAPI_Point2D>();