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"
10 #include <SketchPlugin_Constraint.h>
11 #include <SketchPlugin_ConstraintLength.h>
12 #include <SketchPlugin_ConstraintDistance.h>
13 #include <SketchPlugin_Line.h>
14 #include <SketchPlugin_Point.h>
15 #include <SketchPlugin_Circle.h>
17 #include <SketcherPrs_Tools.h>
18 #include <SketcherPrs_DimensionStyleListener.h>
20 #include <Events_Error.h>
21 #include <Events_Loop.h>
23 #include <GeomDataAPI_Point2D.h>
24 #include <GeomAPI_Pnt.h>
25 #include <GeomAPI_XYZ.h>
26 #include <GeomAPI_Pnt2d.h>
27 #include <GeomAPI_Lin2d.h>
29 #include <ModelAPI_Data.h>
30 #include <ModelAPI_AttributeDouble.h>
32 #include <AIS_DisplaySpecialSymbol.hxx>
35 static const gp_Pnt MyDefStart(0,0,0);
36 static const gp_Pnt MyDefEnd(1,0,0);
37 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
39 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_LengthDimension, AIS_LengthDimension);
40 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
42 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
43 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
44 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
45 myConstraint(theConstraint),
47 myAspect(new Prs3d_DimensionAspect())
49 myAspect->MakeArrows3d(false);
50 myAspect->MakeText3d(false);
51 myAspect->MakeTextShaded(false);
52 myAspect->MakeUnitsDisplayed(false);
53 myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
54 myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
56 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
57 SetDimensionAspect(myAspect);
59 myStyleListener = new SketcherPrs_DimensionStyleListener();
62 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
64 delete myStyleListener;
67 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
68 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
70 bool aReadyToDisplay = false;
73 aReadyToDisplay = getPoints(theConstraint, thePlane, aPnt1, aPnt2);
75 return aReadyToDisplay;
78 void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
79 const Handle(Prs3d_Presentation)& thePresentation,
80 const Standard_Integer theMode)
82 if (!SketcherPrs_LengthDimension::IsReadyToDisplay(myConstraint, myPlane)) {
83 Events_Error::throwException("An empty AIS presentation: SketcherPrs_LengthDimension");
88 getPoints(myConstraint, myPlane, aPnt1, aPnt2);
90 // compute flyout distance
91 SetFlyout(SketcherPrs_Tools::getFlyoutDistance(myConstraint));
92 SetMeasuredGeometry(aPnt1, aPnt2, myPlane->impl<gp_Ax3>());
94 // Update variable aspect parameters (depending on viewer scale)
95 double anArrowLength = myAspect->ArrowAspect()->Length();
96 // This is not realy correct way to get viewer scale.
97 double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
98 double aDimensionValue = GetValue();
99 double aTextSize = 0.0;
100 GetValueString(aTextSize);
102 if(aTextSize > ((aDimensionValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
103 myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
104 myAspect->SetArrowOrientation(Prs3d_DAO_External);
105 myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
107 myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
108 myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
110 myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
112 // The value of vertical aligment is sometimes changed
113 myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
115 AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
117 myStyleListener->updateDimensions(this, aValue);
119 AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
122 bool SketcherPrs_LengthDimension::getPoints(ModelAPI_Feature* theConstraint,
123 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
124 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
126 DataPtr aData = theConstraint->data();
127 if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
128 // The constraint is length
129 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
130 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
131 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
135 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
136 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
139 // Get geometry of the object
140 DataPtr aLineData = aFeature->data();
141 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
142 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
143 (aLineData->attribute(SketchPlugin_Line::START_ID()));
144 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
145 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
146 (aLineData->attribute(SketchPlugin_Line::END_ID()));
147 thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
148 thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
151 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
152 // The constraint is distance
153 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
154 aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
155 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
156 aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
158 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
159 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
160 if (aPoint_A && aPoint_B) {
161 // Both objects are points
162 aPnt_A = aPoint_A->pnt();
163 aPnt_B = aPoint_B->pnt();
164 } else if (!aPoint_A && aPoint_B) {
165 // First object is line
166 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
167 aData, SketchPlugin_Constraint::ENTITY_A());
169 aPnt_B = aPoint_B->pnt();
170 aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
172 } else if (aPoint_A && !aPoint_B) {
173 // Second object is line
174 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
175 aData, SketchPlugin_Constraint::ENTITY_B());
177 aPnt_A = aPoint_A->pnt();
178 aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
181 if (!aPnt_A || !aPnt_B) // Objects not found
184 // Get points from these object
185 std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
186 std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
187 thePnt1 = aPoint1->impl<gp_Pnt>();
188 thePnt2 = aPoint2->impl<gp_Pnt>();
196 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
197 const Standard_Integer theMode)
199 // Map the application selection modes to standard ones
200 Standard_Integer aMode;
202 case 0: // we should use selection of all objects
205 case SketcherPrs_Tools::Sel_Dimension_All:
208 case SketcherPrs_Tools::Sel_Dimension_Line:
211 case SketcherPrs_Tools::Sel_Dimension_Text:
215 // there are own selection modes, so the others should be ignored
216 // otherwise, the text selection appears in the viewer
220 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
221 AIS_LengthDimension::ComputeSelection(aSelection, aMode);