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_Tools.h"
10 #include "SketcherPrs_DimensionStyleListener.h"
12 #include <SketchPlugin_Constraint.h>
13 #include <SketchPlugin_ConstraintLength.h>
14 #include <SketchPlugin_ConstraintDistance.h>
15 #include <SketchPlugin_Line.h>
16 #include <SketchPlugin_Point.h>
17 #include <SketchPlugin_Circle.h>
19 #include <Events_Error.h>
20 #include <Events_Loop.h>
22 #include <GeomDataAPI_Point2D.h>
23 #include <GeomAPI_Pnt.h>
24 #include <GeomAPI_XYZ.h>
25 #include <GeomAPI_Pnt2d.h>
26 #include <GeomAPI_Lin2d.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_AttributeDouble.h>
31 #include <AIS_DisplaySpecialSymbol.hxx>
34 static const gp_Pnt MyDefStart(0,0,0);
35 static const gp_Pnt MyDefEnd(1,0,0);
36 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
38 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_LengthDimension, AIS_LengthDimension);
39 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
41 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
42 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
43 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
44 myConstraint(theConstraint),
46 myAspect(new Prs3d_DimensionAspect())
48 myAspect->MakeArrows3d(false);
49 myAspect->MakeText3d(false);
50 myAspect->MakeTextShaded(false);
51 myAspect->MakeUnitsDisplayed(false);
52 myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
53 myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
55 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
56 SetDimensionAspect(myAspect);
58 myStyleListener = new SketcherPrs_DimensionStyleListener();
61 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
63 delete myStyleListener;
66 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
67 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
69 bool aReadyToDisplay = false;
72 aReadyToDisplay = getPoints(theConstraint, thePlane, aPnt1, aPnt2);
74 return aReadyToDisplay;
77 void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
78 const Handle(Prs3d_Presentation)& thePresentation,
79 const Standard_Integer theMode)
81 if (!SketcherPrs_LengthDimension::IsReadyToDisplay(myConstraint, myPlane)) {
82 Events_Error::throwException("An empty AIS presentation: SketcherPrs_LengthDimension");
87 getPoints(myConstraint, myPlane, aPnt1, aPnt2);
89 // compute flyout distance
90 SetFlyout(SketcherPrs_Tools::getFlyoutDistance(myConstraint));
91 SetMeasuredGeometry(aPnt1, aPnt2, myPlane->impl<gp_Ax3>());
93 // Update variable aspect parameters (depending on viewer scale)
94 double anArrowLength = myAspect->ArrowAspect()->Length();
95 // This is not realy correct way to get viewer scale.
96 double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
97 double aDimensionValue = GetValue();
98 double aTextSize = 0.0;
99 GetValueString(aTextSize);
101 if(aTextSize > ((aDimensionValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
102 myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
103 myAspect->SetArrowOrientation(Prs3d_DAO_External);
104 myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
106 myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
107 myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
109 myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
111 // The value of vertical aligment is sometimes changed
112 myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
114 AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
116 myStyleListener->updateDimensions(this, aValue);
118 AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
121 bool SketcherPrs_LengthDimension::getPoints(ModelAPI_Feature* theConstraint,
122 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
123 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
125 DataPtr aData = theConstraint->data();
126 if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
127 // The constraint is length
128 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
129 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
130 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
134 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
135 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
138 // Get geometry of the object
139 DataPtr aLineData = aFeature->data();
140 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
141 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
142 (aLineData->attribute(SketchPlugin_Line::START_ID()));
143 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
144 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
145 (aLineData->attribute(SketchPlugin_Line::END_ID()));
146 thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
147 thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
150 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
151 // The constraint is distance
152 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
153 aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
154 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
155 aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
157 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
158 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
159 if (aPoint_A && aPoint_B) {
160 // Both objects are points
161 aPnt_A = aPoint_A->pnt();
162 aPnt_B = aPoint_B->pnt();
163 } else if (!aPoint_A && aPoint_B) {
164 // First object is line
165 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
166 aData, SketchPlugin_Constraint::ENTITY_A());
168 aPnt_B = aPoint_B->pnt();
169 aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
171 } else if (aPoint_A && !aPoint_B) {
172 // Second object is line
173 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
174 aData, SketchPlugin_Constraint::ENTITY_B());
176 aPnt_A = aPoint_A->pnt();
177 aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
180 if (!aPnt_A || !aPnt_B) // Objects not found
183 // Get points from these object
184 std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
185 std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
186 thePnt1 = aPoint1->impl<gp_Pnt>();
187 thePnt2 = aPoint2->impl<gp_Pnt>();
195 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
196 const Standard_Integer theMode)
198 // Map the application selection modes to standard ones
199 Standard_Integer aMode;
201 case 0: // we should use selection of all objects
204 case SketcherPrs_Tools::Sel_Dimension_All:
207 case SketcherPrs_Tools::Sel_Dimension_Line:
210 case SketcherPrs_Tools::Sel_Dimension_Text:
214 // there are own selection modes, so the others should be ignored
215 // otherwise, the text selection appears in the viewer
219 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
220 AIS_LengthDimension::ComputeSelection(aSelection, aMode);