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_Events.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),
46 mySketcherPlane(thePlane)
48 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
50 anAspect->MakeArrows3d(false);
51 anAspect->MakeText3d(false);
52 anAspect->MakeTextShaded(false);
53 anAspect->MakeUnitsDisplayed(false);
54 anAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
55 anAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
57 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
58 SetDimensionAspect(anAspect);
60 myStyleListener = new SketcherPrs_DimensionStyleListener();
63 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
65 delete myStyleListener;
68 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
69 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
71 bool aReadyToDisplay = false;
74 aReadyToDisplay = getPoints(theConstraint, thePlane, aPnt1, aPnt2);
76 return aReadyToDisplay;
79 void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
80 const Handle(Prs3d_Presentation)& thePresentation,
81 const Standard_Integer theMode)
83 bool aReadyToDisplay = SketcherPrs_LengthDimension::IsReadyToDisplay(myConstraint, mySketcherPlane);
84 if (aReadyToDisplay) {
85 myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
86 getPoints(myConstraint, mySketcherPlane, myFirstPoint, mySecondPoint);
87 myPlane = gp_Pln(mySketcherPlane->impl<gp_Ax3>());
89 AttributeDoublePtr anAttributeValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
91 myHasParameters = anAttributeValue->usedParameters().size() > 0;
92 myValue = anAttributeValue->text();
95 // compute flyout distance
96 SetFlyout(myDistance);
97 SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
99 // Update variable aspect parameters (depending on viewer scale)
102 myStyleListener->updateDimensions(this, myHasParameters, myValue);
104 AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
106 if (!aReadyToDisplay) {
107 Events_Error::throwException("An empty AIS presentation: SketcherPrs_LengthDimension");
108 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
109 std::shared_ptr<ModelAPI_Object> aConstraintPtr(myConstraint);
110 ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent);
114 bool SketcherPrs_LengthDimension::getPoints(ModelAPI_Feature* theConstraint,
115 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
116 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
118 DataPtr aData = theConstraint->data();
119 if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
120 // The constraint is length
121 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
122 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
123 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
127 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
128 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
131 // Get geometry of the object
132 DataPtr aLineData = aFeature->data();
133 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
134 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
135 (aLineData->attribute(SketchPlugin_Line::START_ID()));
136 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
137 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
138 (aLineData->attribute(SketchPlugin_Line::END_ID()));
139 thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
140 thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
143 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
144 // The constraint is distance
145 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
146 aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
147 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
148 aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
150 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
151 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
152 if (aPoint_A && aPoint_B) {
153 // Both objects are points
154 aPnt_A = aPoint_A->pnt();
155 aPnt_B = aPoint_B->pnt();
156 } else if (!aPoint_A && aPoint_B) {
157 // First object is line
158 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
159 aData, SketchPlugin_Constraint::ENTITY_A());
161 aPnt_B = aPoint_B->pnt();
162 aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
164 } else if (aPoint_A && !aPoint_B) {
165 // Second object is line
166 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
167 aData, SketchPlugin_Constraint::ENTITY_B());
169 aPnt_A = aPoint_A->pnt();
170 aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
173 if (!aPnt_A || !aPnt_B) // Objects not found
176 // Get points from these object
177 std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
178 std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
179 thePnt1 = aPoint1->impl<gp_Pnt>();
180 thePnt2 = aPoint2->impl<gp_Pnt>();
188 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
189 const Standard_Integer theMode)
191 // Map the application selection modes to standard ones
192 Standard_Integer aMode;
194 case 0: // we should use selection of all objects
197 case SketcherPrs_Tools::Sel_Dimension_All:
200 case SketcherPrs_Tools::Sel_Dimension_Line:
203 case SketcherPrs_Tools::Sel_Dimension_Text:
207 // there are own selection modes, so the others should be ignored
208 // otherwise, the text selection appears in the viewer
212 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
213 AIS_LengthDimension::ComputeSelection(aSelection, aMode);
216 void SketcherPrs_LengthDimension::updateArrows()
218 Handle(Prs3d_DimensionAspect) anAspect = DimensionAspect();
220 double anArrowLength = anAspect->ArrowAspect()->Length();
221 // This is not realy correct way to get viewer scale.
222 double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
223 double aDimensionValue = GetValue();
224 double aTextSize = 0.0;
225 GetValueString(aTextSize);
227 if(aTextSize > ((aDimensionValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
228 anAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
229 anAspect->SetArrowOrientation(Prs3d_DAO_External);
230 anAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
232 anAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
233 anAspect->SetArrowOrientation(Prs3d_DAO_Internal);
235 anAspect->SetArrowTailSize(anAspect->ArrowAspect()->Length());
236 // The value of vertical aligment is sometimes changed
237 anAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);