1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "SketcherPrs_LengthDimension.h"
21 #include "SketcherPrs_Tools.h"
22 #include "SketcherPrs_DimensionStyleListener.h"
24 #include <SketchPlugin_Constraint.h>
25 #include <SketchPlugin_ConstraintLength.h>
26 #include <SketchPlugin_ConstraintDistance.h>
27 #include <SketchPlugin_Line.h>
28 #include <SketchPlugin_Point.h>
29 #include <SketchPlugin_Circle.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <GeomAPI_Pnt.h>
33 #include <GeomAPI_XYZ.h>
34 #include <GeomAPI_Pnt2d.h>
35 #include <GeomAPI_Lin2d.h>
37 #include <ModelAPI_Data.h>
38 #include <ModelAPI_AttributeDouble.h>
40 #include <AIS_DisplaySpecialSymbol.hxx>
43 /// Creates an aspect to be shown in length/radius dimension presentations
44 /// \return an instance of aspect
45 Handle(Prs3d_DimensionAspect) createDimensionAspect()
47 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
48 anAspect->MakeArrows3d(false);
49 anAspect->MakeText3d(false);
50 anAspect->MakeTextShaded(false);
51 anAspect->MakeUnitsDisplayed(false);
52 anAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
53 anAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
58 /// Update variable aspect parameters (depending on viewer scale)
59 /// \param theDimAspect an aspect to be changed
60 /// \param theDimValue an arrow value
61 /// \param theTextSize an arrow value
62 void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect,
63 double theDimValue, double theTextSize)
65 double anArrowLength = theDimAspect->ArrowAspect()->Length();
66 // This is not realy correct way to get viewer scale.
67 double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
69 if(theTextSize > ((theDimValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
70 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
71 theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
72 theDimAspect->SetExtensionSize(
73 (theTextSize / aViewerScale + SketcherPrs_Tools::getArrowSize()) / 2.0);
75 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
76 theDimAspect->SetArrowOrientation(Prs3d_DAO_Internal);
78 theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
79 // The value of vertical aligment is sometimes changed
80 theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
84 static const gp_Pnt MyDefStart(0,0,0);
85 static const gp_Pnt MyDefEnd(1,0,0);
86 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
88 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
90 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
91 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
92 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
93 myConstraint(theConstraint),
94 mySketcherPlane(thePlane),
95 myFirstPoint(MyDefStart),
96 mySecondPoint(MyDefEnd),
99 myValue(0., false, "")
101 SetDimensionAspect(createDimensionAspect());
102 myStyleListener = new SketcherPrs_DimensionStyleListener();
105 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
107 delete myStyleListener;
110 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
111 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
114 return readyToDisplay(theConstraint, thePlane, aPnt1, aPnt2);
117 void SketcherPrs_LengthDimension::Compute(
118 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
119 const Handle(Prs3d_Presentation)& thePresentation,
120 const Standard_Integer theMode)
123 bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aPnt1, aPnt2);
124 if (aReadyToDisplay) {
125 myFirstPoint = aPnt1;
126 mySecondPoint = aPnt2;
128 myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
129 myPlane = gp_Pln(mySketcherPlane->impl<gp_Ax3>());
131 DataPtr aData = myConstraint->data();
132 AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
133 myValue.init(anAttributeValue);
136 // compute flyout distance
137 SetFlyout(myDistance);
138 SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
140 // Update variable aspect parameters (depending on viewer scale)
141 double aTextSize = 0.0;
142 GetValueString(aTextSize);
144 updateArrows(DimensionAspect(), GetValue(), aTextSize);
146 // Update text visualization: parameter value or parameter text
147 myStyleListener->updateDimensions(this, myValue);
149 AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
151 if (!aReadyToDisplay)
152 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
153 "An empty AIS presentation: SketcherPrs_LengthDimension");
156 bool SketcherPrs_LengthDimension::readyToDisplay(ModelAPI_Feature* theConstraint,
157 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
158 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
160 DataPtr aData = theConstraint->data();
161 if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
162 // The constraint is length
163 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
164 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
165 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
169 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
170 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
173 // Get geometry of the object
174 DataPtr aLineData = aFeature->data();
175 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
176 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
177 (aLineData->attribute(SketchPlugin_Line::START_ID()));
178 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
179 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
180 (aLineData->attribute(SketchPlugin_Line::END_ID()));
181 thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
182 thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
185 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
186 // The constraint is distance
187 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
188 aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
189 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
190 aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
192 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
193 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
194 if (aPoint_A && aPoint_B) {
195 // Both objects are points
196 aPnt_A = aPoint_A->pnt();
197 aPnt_B = aPoint_B->pnt();
198 } else if (!aPoint_A && aPoint_B) {
199 // First object is line
200 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
201 aData, SketchPlugin_Constraint::ENTITY_A());
203 aPnt_B = aPoint_B->pnt();
204 aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
206 } else if (aPoint_A && !aPoint_B) {
207 // Second object is line
208 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
209 aData, SketchPlugin_Constraint::ENTITY_B());
211 aPnt_A = aPoint_A->pnt();
212 aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
215 if (!aPnt_A || !aPnt_B) // Objects not found
218 // Get points from these object
219 std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
220 std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
221 thePnt1 = aPoint1->impl<gp_Pnt>();
222 thePnt2 = aPoint2->impl<gp_Pnt>();
230 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
231 const Standard_Integer theMode)
233 // Map the application selection modes to standard ones
234 Standard_Integer aMode;
236 case 0: // we should use selection of all objects
239 case SketcherPrs_Tools::Sel_Dimension_All:
242 case SketcherPrs_Tools::Sel_Dimension_Line:
245 case SketcherPrs_Tools::Sel_Dimension_Text:
249 // there are own selection modes, so the others should be ignored
250 // otherwise, the text selection appears in the viewer
254 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
255 AIS_LengthDimension::ComputeSelection(aSelection, aMode);