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_Line.h>
15 #include <SketchPlugin_Point.h>
16 #include <SketchPlugin_Circle.h>
18 #include <GeomDataAPI_Point2D.h>
19 #include <GeomAPI_Pnt.h>
20 #include <GeomAPI_XYZ.h>
21 #include <GeomAPI_Pnt2d.h>
22 #include <GeomAPI_Lin2d.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_AttributeDouble.h>
27 #include <AIS_DisplaySpecialSymbol.hxx>
30 static const gp_Pnt MyDefStart(0,0,0);
31 static const gp_Pnt MyDefEnd(1,0,0);
32 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
34 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_LengthDimension, AIS_LengthDimension);
35 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
37 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
38 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
39 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
40 myConstraint(theConstraint),
41 mySketcherPlane(thePlane),
42 myFirstPoint(MyDefStart),
43 mySecondPoint(MyDefEnd),
46 myValue(0., false, "")
48 SetDimensionAspect(SketcherPrs_Tools::createDimensionAspect());
49 myStyleListener = new SketcherPrs_DimensionStyleListener();
52 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
54 delete myStyleListener;
57 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
58 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
61 return readyToDisplay(theConstraint, thePlane, aPnt1, aPnt2);
64 void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
65 const Handle(Prs3d_Presentation)& thePresentation,
66 const Standard_Integer theMode)
69 bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aPnt1, aPnt2);
70 if (aReadyToDisplay) {
72 mySecondPoint = aPnt2;
74 myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
75 myPlane = gp_Pln(mySketcherPlane->impl<gp_Ax3>());
77 DataPtr aData = myConstraint->data();
78 AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
79 myValue.init(anAttributeValue);
82 // compute flyout distance
83 SetFlyout(myDistance);
84 SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
86 // Update variable aspect parameters (depending on viewer scale)
87 double aTextSize = 0.0;
88 GetValueString(aTextSize);
89 SketcherPrs_Tools::updateArrows(DimensionAspect(), GetValue(), aTextSize);
91 // Update text visualization: parameter value or parameter text
92 myStyleListener->updateDimensions(this, myValue);
94 AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
97 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
98 "An empty AIS presentation: SketcherPrs_LengthDimension");
101 bool SketcherPrs_LengthDimension::readyToDisplay(ModelAPI_Feature* theConstraint,
102 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
103 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
105 DataPtr aData = theConstraint->data();
106 if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
107 // The constraint is length
108 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
109 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
110 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
114 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
115 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
118 // Get geometry of the object
119 DataPtr aLineData = aFeature->data();
120 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
121 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
122 (aLineData->attribute(SketchPlugin_Line::START_ID()));
123 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
124 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
125 (aLineData->attribute(SketchPlugin_Line::END_ID()));
126 thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
127 thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
130 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
131 // The constraint is distance
132 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
133 aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
134 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
135 aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
137 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
138 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
139 if (aPoint_A && aPoint_B) {
140 // Both objects are points
141 aPnt_A = aPoint_A->pnt();
142 aPnt_B = aPoint_B->pnt();
143 } else if (!aPoint_A && aPoint_B) {
144 // First object is line
145 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
146 aData, SketchPlugin_Constraint::ENTITY_A());
148 aPnt_B = aPoint_B->pnt();
149 aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
151 } else if (aPoint_A && !aPoint_B) {
152 // Second object is line
153 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
154 aData, SketchPlugin_Constraint::ENTITY_B());
156 aPnt_A = aPoint_A->pnt();
157 aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
160 if (!aPnt_A || !aPnt_B) // Objects not found
163 // Get points from these object
164 std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
165 std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
166 thePnt1 = aPoint1->impl<gp_Pnt>();
167 thePnt2 = aPoint2->impl<gp_Pnt>();
175 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
176 const Standard_Integer theMode)
178 // Map the application selection modes to standard ones
179 Standard_Integer aMode;
181 case 0: // we should use selection of all objects
184 case SketcherPrs_Tools::Sel_Dimension_All:
187 case SketcherPrs_Tools::Sel_Dimension_Line:
190 case SketcherPrs_Tools::Sel_Dimension_Text:
194 // there are own selection modes, so the others should be ignored
195 // otherwise, the text selection appears in the viewer
199 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
200 AIS_LengthDimension::ComputeSelection(aSelection, aMode);