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_ConstraintDistanceHorizontal.h>
15 #include <SketchPlugin_ConstraintDistanceVertical.h>
16 #include <SketchPlugin_Line.h>
17 #include <SketchPlugin_Point.h>
18 #include <SketchPlugin_Circle.h>
20 #include <GeomDataAPI_Point2D.h>
21 #include <GeomAPI_Pnt.h>
22 #include <GeomAPI_XYZ.h>
23 #include <GeomAPI_Pnt2d.h>
24 #include <GeomAPI_Lin2d.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_AttributeDouble.h>
29 #include <AIS_DisplaySpecialSymbol.hxx>
32 /// Creates an aspect to be shown in length/radius dimension presentations
33 /// \return an instance of aspect
34 Handle(Prs3d_DimensionAspect) createDimensionAspect()
36 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
37 anAspect->MakeArrows3d(false);
38 anAspect->MakeText3d(false);
39 anAspect->MakeTextShaded(false);
40 anAspect->MakeUnitsDisplayed(false);
41 anAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
42 anAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
47 /// Update variable aspect parameters (depending on viewer scale)
48 /// \param theDimAspect an aspect to be changed
49 /// \param theDimValue an arrow value
50 /// \param theTextSize an arrow value
51 void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect,
52 double theDimValue, double theTextSize)
54 double anArrowLength = theDimAspect->ArrowAspect()->Length();
55 // This is not realy correct way to get viewer scale.
56 double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
58 if(theTextSize > ((theDimValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
59 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
60 theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
61 theDimAspect->SetExtensionSize(
62 (theTextSize / aViewerScale + SketcherPrs_Tools::getArrowSize()) / 2.0);
64 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
65 theDimAspect->SetArrowOrientation(Prs3d_DAO_Internal);
67 theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
68 // The value of vertical aligment is sometimes changed
69 theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
73 static const gp_Pnt MyDefStart(0,0,0);
74 static const gp_Pnt MyDefEnd(1,0,0);
75 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
77 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
79 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
80 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
81 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
82 myConstraint(theConstraint),
83 mySketcherPlane(thePlane),
84 myFirstPoint(MyDefStart),
85 mySecondPoint(MyDefEnd),
88 myValue(0., false, "")
90 SetDimensionAspect(createDimensionAspect());
91 myStyleListener = new SketcherPrs_DimensionStyleListener();
94 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
96 delete myStyleListener;
99 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
100 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
103 return readyToDisplay(theConstraint, thePlane, aPnt1, aPnt2);
106 void SketcherPrs_LengthDimension::Compute(
107 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
108 const Handle(Prs3d_Presentation)& thePresentation,
109 const Standard_Integer theMode)
112 bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aPnt1, aPnt2);
113 if (aReadyToDisplay) {
114 myFirstPoint = aPnt1;
115 mySecondPoint = aPnt2;
117 myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
118 myPlane = gp_Pln(mySketcherPlane->impl<gp_Ax3>());
120 DataPtr aData = myConstraint->data();
121 AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
122 myValue.init(anAttributeValue);
125 // compute flyout distance
126 SetFlyout(myDistance);
127 SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
129 // Update variable aspect parameters (depending on viewer scale)
130 double aTextSize = 0.0;
131 GetValueString(aTextSize);
133 updateArrows(DimensionAspect(), GetValue(), aTextSize);
135 // Update text visualization: parameter value or parameter text
136 myStyleListener->updateDimensions(this, myValue);
138 AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
140 if (!aReadyToDisplay)
141 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
142 "An empty AIS presentation: SketcherPrs_LengthDimension");
145 bool SketcherPrs_LengthDimension::readyToDisplay(ModelAPI_Feature* theConstraint,
146 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
147 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
149 DataPtr aData = theConstraint->data();
150 if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
151 // The constraint is length
152 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
153 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
154 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
158 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
159 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
162 // Get geometry of the object
163 DataPtr aLineData = aFeature->data();
164 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
165 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
166 (aLineData->attribute(SketchPlugin_Line::START_ID()));
167 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
168 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
169 (aLineData->attribute(SketchPlugin_Line::END_ID()));
170 thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
171 thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
174 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID() ||
175 theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
176 theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID()) {
177 // The constraint is distance
178 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
179 aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
180 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
181 aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
183 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
184 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
185 if (aPoint_A && aPoint_B) {
186 // Both objects are points
187 aPnt_A = aPoint_A->pnt();
188 aPnt_B = aPoint_B->pnt();
189 } else if (!aPoint_A && aPoint_B) {
190 // First object is line
191 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
192 aData, SketchPlugin_Constraint::ENTITY_A());
194 aPnt_B = aPoint_B->pnt();
195 aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
197 } else if (aPoint_A && !aPoint_B) {
198 // Second object is line
199 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
200 aData, SketchPlugin_Constraint::ENTITY_B());
202 aPnt_A = aPoint_A->pnt();
203 aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
206 if (!aPnt_A || !aPnt_B) // Objects not found
209 // Get points from these object
210 std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
211 std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
212 thePnt1 = aPoint1->impl<gp_Pnt>();
213 thePnt2 = aPoint2->impl<gp_Pnt>();
221 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
222 const Standard_Integer theMode)
224 // Map the application selection modes to standard ones
225 Standard_Integer aMode;
227 case 0: // we should use selection of all objects
230 case SketcherPrs_Tools::Sel_Dimension_All:
233 case SketcherPrs_Tools::Sel_Dimension_Line:
236 case SketcherPrs_Tools::Sel_Dimension_Text:
240 // there are own selection modes, so the others should be ignored
241 // otherwise, the text selection appears in the viewer
245 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
246 AIS_LengthDimension::ComputeSelection(aSelection, aMode);