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 <Events_Error.h>
20 #include <GeomDataAPI_Point2D.h>
21 #include <GeomAPI_Pnt.h>
22 #include <GeomAPI_XYZ.h>
23 #include <GeomAPI_Pnt2d.h>
24 #include <GeomAPI_Lin2d.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_AttributeDouble.h>
29 #include <AIS_DisplaySpecialSymbol.hxx>
32 static const gp_Pnt MyDefStart(0,0,0);
33 static const gp_Pnt MyDefEnd(1,0,0);
34 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
36 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_LengthDimension, AIS_LengthDimension);
37 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
39 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
40 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
41 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
42 myConstraint(theConstraint),
43 mySketcherPlane(thePlane),
44 myFirstPoint(MyDefStart),
45 mySecondPoint(MyDefEnd),
47 myHasParameters(false),
51 SetDimensionAspect(SketcherPrs_Tools::createDimensionAspect());
52 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
54 myStyleListener = new SketcherPrs_DimensionStyleListener();
57 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
59 delete myStyleListener;
62 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
63 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
66 return readyToDisplay(theConstraint, thePlane, aPnt1, aPnt2);
69 void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
70 const Handle(Prs3d_Presentation)& thePresentation,
71 const Standard_Integer theMode)
74 bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aPnt1, aPnt2);
75 if (aReadyToDisplay) {
77 mySecondPoint = aPnt2;
79 myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
80 myPlane = gp_Pln(mySketcherPlane->impl<gp_Ax3>());
82 AttributeDoublePtr anAttributeValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
84 myHasParameters = anAttributeValue->usedParameters().size() > 0;
85 myValue = anAttributeValue->text();
88 // compute flyout distance
89 SetFlyout(myDistance);
90 SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
92 // Update variable aspect parameters (depending on viewer scale)
93 double aTextSize = 0.0;
94 GetValueString(aTextSize);
95 SketcherPrs_Tools::updateArrows(DimensionAspect(), GetValue(), aTextSize);
97 // Update text visualization: parameter value or parameter text
98 myStyleListener->updateDimensions(this, myHasParameters, myValue);
100 AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
102 if (!aReadyToDisplay)
103 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
104 "An empty AIS presentation: SketcherPrs_LengthDimension");
107 bool SketcherPrs_LengthDimension::readyToDisplay(ModelAPI_Feature* theConstraint,
108 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
109 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
111 DataPtr aData = theConstraint->data();
112 if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
113 // The constraint is length
114 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
115 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
116 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
120 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
121 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
124 // Get geometry of the object
125 DataPtr aLineData = aFeature->data();
126 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
127 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
128 (aLineData->attribute(SketchPlugin_Line::START_ID()));
129 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
130 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
131 (aLineData->attribute(SketchPlugin_Line::END_ID()));
132 thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
133 thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
136 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
137 // The constraint is distance
138 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
139 aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
140 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
141 aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
143 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
144 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
145 if (aPoint_A && aPoint_B) {
146 // Both objects are points
147 aPnt_A = aPoint_A->pnt();
148 aPnt_B = aPoint_B->pnt();
149 } else if (!aPoint_A && aPoint_B) {
150 // First object is line
151 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
152 aData, SketchPlugin_Constraint::ENTITY_A());
154 aPnt_B = aPoint_B->pnt();
155 aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
157 } else if (aPoint_A && !aPoint_B) {
158 // Second object is line
159 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
160 aData, SketchPlugin_Constraint::ENTITY_B());
162 aPnt_A = aPoint_A->pnt();
163 aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
166 if (!aPnt_A || !aPnt_B) // Objects not found
169 // Get points from these object
170 std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
171 std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
172 thePnt1 = aPoint1->impl<gp_Pnt>();
173 thePnt2 = aPoint2->impl<gp_Pnt>();
181 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
182 const Standard_Integer theMode)
184 // Map the application selection modes to standard ones
185 Standard_Integer aMode;
187 case 0: // we should use selection of all objects
190 case SketcherPrs_Tools::Sel_Dimension_All:
193 case SketcherPrs_Tools::Sel_Dimension_Line:
196 case SketcherPrs_Tools::Sel_Dimension_Text:
200 // there are own selection modes, so the others should be ignored
201 // otherwise, the text selection appears in the viewer
205 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
206 AIS_LengthDimension::ComputeSelection(aSelection, aMode);