1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketcherPrs_LengthDimension.cpp
4 // Created: 27 March 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "SketcherPrs_LengthDimension.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_DimensionStyleListener.h"
11 #include <SketchPlugin_Constraint.h>
12 #include <SketchPlugin_ConstraintLength.h>
13 #include <SketchPlugin_ConstraintDistance.h>
14 #include <SketchPlugin_Line.h>
15 #include <SketchPlugin_Point.h>
16 #include <SketchPlugin_Circle.h>
18 #include <GeomDataAPI_Point2D.h>
19 #include <GeomAPI_Pnt.h>
20 #include <GeomAPI_XYZ.h>
21 #include <GeomAPI_Pnt2d.h>
22 #include <GeomAPI_Lin2d.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_AttributeDouble.h>
27 #include <AIS_DisplaySpecialSymbol.hxx>
30 /// Creates an aspect to be shown in length/radius dimension presentations
31 /// \return an instance of aspect
32 Handle(Prs3d_DimensionAspect) createDimensionAspect()
34 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
35 anAspect->MakeArrows3d(false);
36 anAspect->MakeText3d(false);
37 anAspect->MakeTextShaded(false);
38 anAspect->MakeUnitsDisplayed(false);
39 anAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
40 anAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
45 /// Update variable aspect parameters (depending on viewer scale)
46 /// \param theDimAspect an aspect to be changed
47 /// \param theDimValue an arrow value
48 /// \param theTextSize an arrow value
49 void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect,
50 double theDimValue, double theTextSize)
52 double anArrowLength = theDimAspect->ArrowAspect()->Length();
53 // This is not realy correct way to get viewer scale.
54 double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
56 if(theTextSize > ((theDimValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
57 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
58 theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
59 theDimAspect->SetExtensionSize(
60 (theTextSize / aViewerScale + SketcherPrs_Tools::getArrowSize()) / 2.0);
62 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
63 theDimAspect->SetArrowOrientation(Prs3d_DAO_Internal);
65 theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
66 // The value of vertical aligment is sometimes changed
67 theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
71 static const gp_Pnt MyDefStart(0,0,0);
72 static const gp_Pnt MyDefEnd(1,0,0);
73 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
75 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
77 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
78 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
79 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
80 myConstraint(theConstraint),
81 mySketcherPlane(thePlane),
82 myFirstPoint(MyDefStart),
83 mySecondPoint(MyDefEnd),
86 myValue(0., false, "")
88 SetDimensionAspect(createDimensionAspect());
89 myStyleListener = new SketcherPrs_DimensionStyleListener();
92 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
94 delete myStyleListener;
97 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
98 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
101 return readyToDisplay(theConstraint, thePlane, aPnt1, aPnt2);
104 void SketcherPrs_LengthDimension::Compute(
105 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
106 const Handle(Prs3d_Presentation)& thePresentation,
107 const Standard_Integer theMode)
110 bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aPnt1, aPnt2);
111 if (aReadyToDisplay) {
112 myFirstPoint = aPnt1;
113 mySecondPoint = aPnt2;
115 myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
116 myPlane = gp_Pln(mySketcherPlane->impl<gp_Ax3>());
118 DataPtr aData = myConstraint->data();
119 AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
120 myValue.init(anAttributeValue);
123 // compute flyout distance
124 SetFlyout(myDistance);
125 SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
127 // Update variable aspect parameters (depending on viewer scale)
128 double aTextSize = 0.0;
129 GetValueString(aTextSize);
131 updateArrows(DimensionAspect(), GetValue(), aTextSize);
133 // Update text visualization: parameter value or parameter text
134 myStyleListener->updateDimensions(this, myValue);
136 AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
138 if (!aReadyToDisplay)
139 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
140 "An empty AIS presentation: SketcherPrs_LengthDimension");
143 bool SketcherPrs_LengthDimension::readyToDisplay(ModelAPI_Feature* theConstraint,
144 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
145 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
147 DataPtr aData = theConstraint->data();
148 if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
149 // The constraint is length
150 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
151 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
152 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
156 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
157 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
160 // Get geometry of the object
161 DataPtr aLineData = aFeature->data();
162 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
163 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
164 (aLineData->attribute(SketchPlugin_Line::START_ID()));
165 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
166 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
167 (aLineData->attribute(SketchPlugin_Line::END_ID()));
168 thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
169 thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
172 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
173 // The constraint is distance
174 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
175 aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
176 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
177 aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
179 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
180 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
181 if (aPoint_A && aPoint_B) {
182 // Both objects are points
183 aPnt_A = aPoint_A->pnt();
184 aPnt_B = aPoint_B->pnt();
185 } else if (!aPoint_A && aPoint_B) {
186 // First object is line
187 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
188 aData, SketchPlugin_Constraint::ENTITY_A());
190 aPnt_B = aPoint_B->pnt();
191 aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
193 } else if (aPoint_A && !aPoint_B) {
194 // Second object is line
195 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
196 aData, SketchPlugin_Constraint::ENTITY_B());
198 aPnt_A = aPoint_A->pnt();
199 aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
202 if (!aPnt_A || !aPnt_B) // Objects not found
205 // Get points from these object
206 std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
207 std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
208 thePnt1 = aPoint1->impl<gp_Pnt>();
209 thePnt2 = aPoint2->impl<gp_Pnt>();
217 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
218 const Standard_Integer theMode)
220 // Map the application selection modes to standard ones
221 Standard_Integer aMode;
223 case 0: // we should use selection of all objects
226 case SketcherPrs_Tools::Sel_Dimension_All:
229 case SketcherPrs_Tools::Sel_Dimension_Line:
232 case SketcherPrs_Tools::Sel_Dimension_Text:
236 // there are own selection modes, so the others should be ignored
237 // otherwise, the text selection appears in the viewer
241 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
242 AIS_LengthDimension::ComputeSelection(aSelection, aMode);