1 // File: PartSet_FeaturePrs.h
2 // Created: 16 Jun 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_FeatureLengthPrs.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>
22 #include <AIS_InteractiveObject.hxx>
23 #include <AIS_LengthDimension.hxx>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Document.h>
27 #include <ModelAPI_AttributeRefAttr.h>
28 #include <ModelAPI_AttributeRefList.h>
29 #include <ModelAPI_AttributeDouble.h>
33 #include <Precision.hxx>
37 PartSet_FeatureLengthPrs::PartSet_FeatureLengthPrs(FeaturePtr theSketch)
38 : PartSet_FeaturePrs(theSketch)
42 std::string PartSet_FeatureLengthPrs::getKind()
44 return SKETCH_CONSTRAINT_LENGTH_KIND;
47 bool PartSet_FeatureLengthPrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode)
50 if (feature() && theFeature && theFeature->getKind() == SKETCH_LINE_KIND && theMode == SM_FirstPoint)
53 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
54 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
55 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
56 aRef->setFeature(theFeature);
59 aData = theFeature->data();
60 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
61 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
62 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
63 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
65 double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
66 PartSet_Tools::setFeatureValue(feature(), aLenght, CONSTRAINT_ATTR_VALUE);
72 PartSet_SelectionMode PartSet_FeatureLengthPrs::setPoint(double theX, double theY,
73 const PartSet_SelectionMode& theMode)
75 PartSet_SelectionMode aMode = theMode;
78 case SM_SecondPoint: {
79 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
80 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
81 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
84 aFeature = anAttr->feature();
85 if (aFeature->getKind() != SKETCH_LINE_KIND) {
86 aFeature = FeaturePtr();
89 boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>
90 (new GeomAPI_Pnt2d(theX, theY));
91 boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = PartSet_FeatureLinePrs::createLin2d(aFeature);
92 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
93 double aDistance = aPoint->distance(aResult);
95 double aStartX, aStartY;
96 PartSet_FeatureLinePrs::getLinePoint(aFeature, LINE_ATTR_START, aStartX, aStartY);
98 if (!aFeatureLin->isRight(aPoint))
99 aDistance = -aDistance;
101 AttributeDoublePtr aFlyoutAttr =
102 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
103 aFlyoutAttr->setValue(aDistance);
105 aMode = SM_DonePoint;
114 Handle(AIS_InteractiveObject) PartSet_FeatureLengthPrs::createPresentation(FeaturePtr theFeature,
115 FeaturePtr theSketch,
116 Handle(AIS_InteractiveObject) thePreviuos)
118 if (!theFeature || !theSketch)
121 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
122 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
123 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
124 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
125 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
126 gp_Pln aPlane(aNormal->x(), aNormal->y(), aNormal->z(), 0/*D*/);
128 aData = theFeature->data();
129 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
130 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
133 FeaturePtr aFeature = anAttr->feature();
134 if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
137 boost::shared_ptr<ModelAPI_AttributeDouble> aFlyoutAttr =
138 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
139 double aFlyout = aFlyoutAttr->value();
141 aData = aFeature->data();
142 if (!aData->isValid())
145 boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
146 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
147 boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
148 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
150 gp_Pnt aPoint1, aPoint2;
151 PartSet_Tools::convertTo3D(aPointStart->x(), aPointStart->y(), theSketch, aPoint1);
152 PartSet_Tools::convertTo3D(aPointEnd->x(), aPointEnd->y(), theSketch, aPoint2);
154 //Build dimension here
155 gp_Pnt aP1 = aPoint1;
156 gp_Pnt aP2 = aPoint2;
162 Handle(AIS_InteractiveObject) anAIS = thePreviuos;
165 Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension (aP1, aP2, aPlane);
167 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
168 anAspect->MakeArrows3d (Standard_False);
169 anAspect->MakeText3d(false/*is text 3d*/);
170 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
171 anAspect->MakeTextShaded(false/*is test shaded*/);
172 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false/*is units displayed*/);
173 /*if (isUnitsDisplayed)
175 aDimAIS->SetDisplayUnits (aDimDlg->GetUnits ());
177 aDimAIS->SetDimensionAspect (anAspect);
178 aDimAIS->SetFlyout(aFlyout);
179 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
184 // update presentation
185 Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
186 if (!aDimAIS.IsNull()) {
187 aDimAIS->SetMeasuredGeometry(aPoint1, aPoint2, aPlane);
188 aDimAIS->SetFlyout(aFlyout);
190 aDimAIS->Redisplay(Standard_True);
196 std::string PartSet_FeatureLengthPrs::getAttribute(const PartSet_SelectionMode& theMode) const
201 PartSet_SelectionMode PartSet_FeatureLengthPrs::getNextMode(const std::string& theAttribute) const
203 return SM_FirstPoint;
206 void PartSet_FeatureLengthPrs::move(double theDeltaX, double theDeltaY)
210 double PartSet_FeatureLengthPrs::distanceToPoint(FeaturePtr theFeature,
211 double theX, double theY)
216 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLengthPrs::findPoint(FeaturePtr theFeature,
217 double theX, double theY)
219 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
223 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLengthPrs::featurePoint
224 (const PartSet_SelectionMode& theMode)
226 return boost::shared_ptr<GeomDataAPI_Point2D>();