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>
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 Events_Loop* aLoop = Events_Loop::loop();
59 const Events_ID kDocCreatedEvent =
60 SketcherPrs_ParameterStyleMessage::eventId();
61 aLoop->registerListener(this, kDocCreatedEvent, NULL, false);
64 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
66 Events_Loop* aLoop = Events_Loop::loop();
67 aLoop->removeListener(this);
70 void SketcherPrs_LengthDimension::processEvent(const std::shared_ptr<Events_Message>& theMessage)
72 const Events_ID kParameterStyleEvent = SketcherPrs_ParameterStyleMessage::eventId();
73 if (theMessage->eventID() == kParameterStyleEvent) {
74 std::shared_ptr<SketcherPrs_ParameterStyleMessage> aMessage = std::dynamic_pointer_cast<
75 SketcherPrs_ParameterStyleMessage>(theMessage);
76 myStyle = aMessage->style();
80 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
81 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
83 bool aReadyToDisplay = false;
86 aReadyToDisplay = getPoints(theConstraint, thePlane, aPnt1, aPnt2);
88 return aReadyToDisplay;
91 void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
92 const Handle(Prs3d_Presentation)& thePresentation,
93 const Standard_Integer theMode)
95 if (!SketcherPrs_LengthDimension::IsReadyToDisplay(myConstraint, myPlane)) {
96 Events_Error::throwException("An empty AIS presentation: SketcherPrs_LengthDimension");
101 getPoints(myConstraint, myPlane, aPnt1, aPnt2);
103 // compute flyout distance
104 SetFlyout(SketcherPrs_Tools::getFlyoutDistance(myConstraint));
105 SetMeasuredGeometry(aPnt1, aPnt2, myPlane->impl<gp_Ax3>());
107 // Update variable aspect parameters (depending on viewer scale)
108 double anArrowLength = myAspect->ArrowAspect()->Length();
109 // This is not realy correct way to get viewer scale.
110 double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
111 double aDimensionValue = GetValue();
112 double aTextSize = 0.0;
113 GetValueString(aTextSize);
115 if(aTextSize > ((aDimensionValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
116 myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
117 myAspect->SetArrowOrientation(Prs3d_DAO_External);
118 myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
120 myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
121 myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
123 myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
125 // The value of vertical aligment is sometimes changed
126 myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
128 AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
129 bool aHasParameters = aValue->usedParameters().size() > 0;
130 if (aHasParameters) {
131 bool isParameterValueStyle = myStyle == SketcherPrs_ParameterStyleMessage::ParameterValue;
132 SketcherPrs_Tools::setDisplaySpecialSymbol(this, isParameterValueStyle);
133 SketcherPrs_Tools::setDisplayParameter(this, aValue->text(), !isParameterValueStyle);
136 SketcherPrs_Tools::setDisplaySpecialSymbol(this, false);
137 SketcherPrs_Tools::setDisplayParameter(this, aValue->text(), false);
139 AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
142 bool SketcherPrs_LengthDimension::getPoints(ModelAPI_Feature* theConstraint,
143 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
144 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
146 DataPtr aData = theConstraint->data();
147 if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
148 // The constraint is length
149 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
150 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
151 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
155 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
156 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
159 // Get geometry of the object
160 DataPtr aLineData = aFeature->data();
161 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
162 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
163 (aLineData->attribute(SketchPlugin_Line::START_ID()));
164 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
165 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
166 (aLineData->attribute(SketchPlugin_Line::END_ID()));
167 thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
168 thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
171 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
172 // The constraint is distance
173 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
174 aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
175 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
176 aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
178 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
179 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
180 if (aPoint_A && aPoint_B) {
181 // Both objects are points
182 aPnt_A = aPoint_A->pnt();
183 aPnt_B = aPoint_B->pnt();
184 } else if (!aPoint_A && aPoint_B) {
185 // First object is line
186 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
187 aData, SketchPlugin_Constraint::ENTITY_A());
189 aPnt_B = aPoint_B->pnt();
190 aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
192 } else if (aPoint_A && !aPoint_B) {
193 // Second object is line
194 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
195 aData, SketchPlugin_Constraint::ENTITY_B());
197 aPnt_A = aPoint_A->pnt();
198 aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
201 if (!aPnt_A || !aPnt_B) // Objects not found
204 // Get points from these object
205 std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
206 std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
207 thePnt1 = aPoint1->impl<gp_Pnt>();
208 thePnt2 = aPoint2->impl<gp_Pnt>();
216 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
217 const Standard_Integer theMode)
219 // Map the application selection modes to standard ones
220 Standard_Integer aMode;
222 case 0: // we should use selection of all objects
225 case SketcherPrs_Tools::Sel_Dimension_All:
228 case SketcherPrs_Tools::Sel_Dimension_Line:
231 case SketcherPrs_Tools::Sel_Dimension_Text:
235 // there are own selection modes, so the others should be ignored
236 // otherwise, the text selection appears in the viewer
240 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
241 AIS_LengthDimension::ComputeSelection(aSelection, aMode);