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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketcherPrs_LengthDimension.h"
22 #include "SketcherPrs_Tools.h"
23 #include "SketcherPrs_DimensionStyleListener.h"
25 #include <SketchPlugin_Constraint.h>
26 #include <SketchPlugin_ConstraintLength.h>
27 #include <SketchPlugin_ConstraintDistance.h>
28 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
29 #include <SketchPlugin_ConstraintDistanceVertical.h>
30 #include <SketchPlugin_Line.h>
31 #include <SketchPlugin_Point.h>
32 #include <SketchPlugin_Circle.h>
34 #include <GeomDataAPI_Point2D.h>
35 #include <GeomAPI_Pnt.h>
36 #include <GeomAPI_XYZ.h>
37 #include <GeomAPI_Pnt2d.h>
38 #include <GeomAPI_Lin2d.h>
40 #include <ModelAPI_Data.h>
41 #include <ModelAPI_AttributeDouble.h>
43 #include <AIS_DisplaySpecialSymbol.hxx>
45 //#ifdef OCCT_28850_FIXED
47 /// Creates an aspect to be shown in length/radius dimension presentations
48 /// \return an instance of aspect
49 Handle(Prs3d_DimensionAspect) createDimensionAspect()
51 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
52 anAspect->MakeArrows3d(false);
53 anAspect->MakeText3d(false);
54 anAspect->MakeTextShaded(false);
55 anAspect->MakeUnitsDisplayed(false);
56 anAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
57 anAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
62 /// Update variable aspect parameters (depending on viewer scale)
63 /// \param theDimAspect an aspect to be changed
64 /// \param theDimValue an arrow value
65 /// \param theTextSize an arrow value
66 void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect,
67 double theDimValue, double theTextSize)
69 double anArrowLength = theDimAspect->ArrowAspect()->Length();
70 // This is not realy correct way to get viewer scale.
71 double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
73 if(theTextSize > ((theDimValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
74 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
75 theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
76 theDimAspect->SetExtensionSize(
77 (theTextSize / aViewerScale + SketcherPrs_Tools::getArrowSize()) / 2.0);
79 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
80 theDimAspect->SetArrowOrientation(Prs3d_DAO_Internal);
82 theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
83 // The value of vertical aligment is sometimes changed
84 theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
88 static const gp_Pnt MyDefStart(0,0,0);
89 static const gp_Pnt MyDefEnd(1,0,0);
90 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
92 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
94 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
95 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
96 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
97 myConstraint(theConstraint),
98 mySketcherPlane(thePlane),
99 myFirstPoint(MyDefStart),
100 mySecondPoint(MyDefEnd),
103 myValue(0., false, "")
105 SetDimensionAspect(createDimensionAspect());
106 myStyleListener = new SketcherPrs_DimensionStyleListener();
108 #ifdef OCCT_28850_FIXED
109 if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID())
110 SetDirection(true, mySketcherPlane->dirX()->impl<gp_Dir>());
111 else if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
112 SetDirection(true, mySketcherPlane->dirY()->impl<gp_Dir>());
116 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
118 delete myStyleListener;
121 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
122 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
125 return readyToDisplay(theConstraint, thePlane, aPnt1, aPnt2);
128 void SketcherPrs_LengthDimension::Compute(
129 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
130 const Handle(Prs3d_Presentation)& thePresentation,
131 const Standard_Integer theMode)
134 bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aPnt1, aPnt2);
135 if (aReadyToDisplay) {
136 myFirstPoint = aPnt1;
137 mySecondPoint = aPnt2;
139 myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
140 myPlane = gp_Pln(mySketcherPlane->impl<gp_Ax3>());
142 DataPtr aData = myConstraint->data();
143 AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
144 myValue.init(anAttributeValue);
147 // compute flyout distance
148 SetFlyout(myDistance);
149 SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
151 // Update variable aspect parameters (depending on viewer scale)
152 double aTextSize = 0.0;
153 GetValueString(aTextSize);
155 updateArrows(DimensionAspect(), GetValue(), aTextSize);
157 // Update text visualization: parameter value or parameter text
158 myStyleListener->updateDimensions(this, myValue);
160 AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
162 if (!aReadyToDisplay)
163 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
164 "An empty AIS presentation: SketcherPrs_LengthDimension");
167 bool SketcherPrs_LengthDimension::readyToDisplay(ModelAPI_Feature* theConstraint,
168 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
169 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
171 DataPtr aData = theConstraint->data();
172 if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
173 // The constraint is length
174 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
175 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
176 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
180 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
181 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
184 // Get geometry of the object
185 DataPtr aLineData = aFeature->data();
186 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
187 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
188 (aLineData->attribute(SketchPlugin_Line::START_ID()));
189 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
190 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
191 (aLineData->attribute(SketchPlugin_Line::END_ID()));
192 thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
193 thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
196 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID() ||
197 theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
198 theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID()) {
199 // The constraint is distance
200 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
201 aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
202 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
203 aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
205 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
206 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
207 if (aPoint_A && aPoint_B) {
208 // Both objects are points
209 aPnt_A = aPoint_A->pnt();
210 aPnt_B = aPoint_B->pnt();
211 } else if (!aPoint_A && aPoint_B) {
212 // First object is line
213 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
214 aData, SketchPlugin_Constraint::ENTITY_A());
216 aPnt_B = aPoint_B->pnt();
217 aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
219 } else if (aPoint_A && !aPoint_B) {
220 // Second object is line
221 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
222 aData, SketchPlugin_Constraint::ENTITY_B());
224 aPnt_A = aPoint_A->pnt();
225 aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
228 if (!aPnt_A || !aPnt_B) // Objects not found
231 // Get points from these object
232 std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
233 std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
234 thePnt1 = aPoint1->impl<gp_Pnt>();
235 thePnt2 = aPoint2->impl<gp_Pnt>();
243 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
244 const Standard_Integer theMode)
246 // Map the application selection modes to standard ones
247 Standard_Integer aMode;
249 case 0: // we should use selection of all objects
252 case SketcherPrs_Tools::Sel_Dimension_All:
255 case SketcherPrs_Tools::Sel_Dimension_Line:
258 case SketcherPrs_Tools::Sel_Dimension_Text:
262 // there are own selection modes, so the others should be ignored
263 // otherwise, the text selection appears in the viewer
267 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
268 AIS_LengthDimension::ComputeSelection(aSelection, aMode);