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>
42 #include <ModelAPI_AttributeInteger.h>
44 #include <AIS_DisplaySpecialSymbol.hxx>
46 #define OCCT_28850_FIXED
48 /// Creates an aspect to be shown in length/radius dimension presentations
49 /// \return an instance of aspect
50 Handle(Prs3d_DimensionAspect) createDimensionAspect()
52 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
53 anAspect->MakeArrows3d(false);
54 anAspect->MakeText3d(false);
55 anAspect->MakeTextShaded(false);
56 anAspect->MakeUnitsDisplayed(false);
57 anAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getConfigTextHeight());
58 anAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
63 /// Update variable aspect parameters (depending on viewer scale)
64 /// \param theDimAspect an aspect to be changed
65 /// \param theDimValue an arrow value
66 /// \param theTextSize an arrow value
67 void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect,
68 double theDimValue, double theTextSize, SketcherPrs_Tools::LocationType theLocationType)
70 if (theLocationType == SketcherPrs_Tools::LOCATION_AUTOMATIC) {
71 double anArrowLength = theDimAspect->ArrowAspect()->Length();
72 // This is not realy correct way to get viewer scale.
73 double aViewerScale = (double) SketcherPrs_Tools::getConfigArrowSize() / anArrowLength;
75 if(theTextSize > ((theDimValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
76 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
77 theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
78 theDimAspect->SetExtensionSize(
79 (theTextSize / aViewerScale + SketcherPrs_Tools::getArrowSize()) / 2.0);
81 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
82 theDimAspect->SetArrowOrientation(Prs3d_DAO_Internal);
85 else if (theLocationType == SketcherPrs_Tools::LOCATION_RIGHT ||
86 theLocationType == SketcherPrs_Tools::LOCATION_LEFT) {
87 theDimAspect->SetTextHorizontalPosition(
88 theLocationType == SketcherPrs_Tools::LOCATION_LEFT ? Prs3d_DTHP_Left : Prs3d_DTHP_Right);
89 theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
91 double anArrowLength = theDimAspect->ArrowAspect()->Length();
92 // This is not realy correct way to get viewer scale.
93 double aViewerScale = (double) SketcherPrs_Tools::getConfigArrowSize() / anArrowLength;
94 theDimAspect->SetExtensionSize(
95 (theTextSize / aViewerScale + SketcherPrs_Tools::getArrowSize()) / 2.0);
98 theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
99 // The value of vertical aligment is sometimes changed
100 theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
104 static const gp_Pnt MyDefStart(0,0,0);
105 static const gp_Pnt MyDefEnd(1,0,0);
106 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
108 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
110 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
111 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
112 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
113 myConstraint(theConstraint),
114 mySketcherPlane(thePlane),
115 myFirstPoint(MyDefStart),
116 mySecondPoint(MyDefEnd),
119 myValue(0., false, "")
121 SetDimensionAspect(createDimensionAspect());
122 myStyleListener = new SketcherPrs_DimensionStyleListener();
124 #ifdef OCCT_28850_FIXED
125 if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID())
126 SetDirection(mySketcherPlane->dirX()->impl<gp_Dir>(), true);
127 else if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
128 SetDirection(mySketcherPlane->dirY()->impl<gp_Dir>(), true);
132 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
134 delete myStyleListener;
137 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
138 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
141 return readyToDisplay(theConstraint, thePlane, aPnt1, aPnt2);
144 void SketcherPrs_LengthDimension::Compute(
145 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
146 const Handle(Prs3d_Presentation)& thePresentation,
147 const Standard_Integer theMode)
150 bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aPnt1, aPnt2);
151 if (aReadyToDisplay) {
152 myFirstPoint = aPnt1;
153 mySecondPoint = aPnt2;
155 myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
156 myPlane = gp_Pln(mySketcherPlane->impl<gp_Ax3>());
158 DataPtr aData = myConstraint->data();
159 AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
160 myValue.init(anAttributeValue);
163 // compute flyout distance
164 SetFlyout(myDistance);
165 SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
167 // Update variable aspect parameters (depending on viewer scale)
168 double aTextSize = 0.0;
169 GetValueString(aTextSize);
171 std::string aLocationAttribute;
172 std::string aConstraintKind = myConstraint->getKind();
173 if (aConstraintKind == SketchPlugin_ConstraintLength::ID())
174 aLocationAttribute = SketchPlugin_ConstraintLength::LOCATION_TYPE_ID();
175 else if (aConstraintKind == SketchPlugin_ConstraintDistance::ID())
176 aLocationAttribute = SketchPlugin_ConstraintDistance::LOCATION_TYPE_ID();
177 else if (aConstraintKind == SketchPlugin_ConstraintDistanceHorizontal::ID())
178 aLocationAttribute = SketchPlugin_ConstraintDistanceHorizontal::LOCATION_TYPE_ID();
179 else if (aConstraintKind == SketchPlugin_ConstraintDistanceVertical::ID())
180 aLocationAttribute = SketchPlugin_ConstraintDistanceVertical::LOCATION_TYPE_ID();
182 std::shared_ptr<ModelAPI_AttributeInteger> aLocationTypeAttr = std::dynamic_pointer_cast<
183 ModelAPI_AttributeInteger>(myConstraint->data()->attribute(aLocationAttribute));
184 updateArrows(DimensionAspect(), GetValue(), aTextSize,
185 (SketcherPrs_Tools::LocationType)(aLocationTypeAttr->value()));
187 // Update text visualization: parameter value or parameter text
188 myStyleListener->updateDimensions(this, myValue);
190 AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
192 if (!aReadyToDisplay)
193 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
194 "An empty AIS presentation: SketcherPrs_LengthDimension");
197 bool SketcherPrs_LengthDimension::readyToDisplay(ModelAPI_Feature* theConstraint,
198 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
199 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
201 DataPtr aData = theConstraint->data();
202 if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
203 // The constraint is length
204 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
205 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
206 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
210 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
211 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
214 // Get geometry of the object
215 DataPtr aLineData = aFeature->data();
216 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
217 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
218 (aLineData->attribute(SketchPlugin_Line::START_ID()));
219 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
220 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
221 (aLineData->attribute(SketchPlugin_Line::END_ID()));
222 thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
223 thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
226 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID() ||
227 theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
228 theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID()) {
229 // The constraint is distance
230 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
231 aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
232 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
233 aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
235 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
236 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
237 if (aPoint_A && aPoint_B) {
238 // Both objects are points
239 aPnt_A = aPoint_A->pnt();
240 aPnt_B = aPoint_B->pnt();
241 } else if (!aPoint_A && aPoint_B) {
242 // First object is line
243 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
244 aData, SketchPlugin_Constraint::ENTITY_A());
246 aPnt_B = aPoint_B->pnt();
247 aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
249 } else if (aPoint_A && !aPoint_B) {
250 // Second object is line
251 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
252 aData, SketchPlugin_Constraint::ENTITY_B());
254 aPnt_A = aPoint_A->pnt();
255 aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
258 if (!aPnt_A || !aPnt_B) // Objects not found
261 // Get points from these object
262 std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
263 std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
264 thePnt1 = aPoint1->impl<gp_Pnt>();
265 thePnt2 = aPoint2->impl<gp_Pnt>();
273 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
274 const Standard_Integer theMode)
276 // Map the application selection modes to standard ones
277 Standard_Integer aMode;
279 case 0: // we should use selection of all objects
282 case SketcherPrs_Tools::Sel_Dimension_All:
285 case SketcherPrs_Tools::Sel_Dimension_Line:
288 case SketcherPrs_Tools::Sel_Dimension_Text:
292 // there are own selection modes, so the others should be ignored
293 // otherwise, the text selection appears in the viewer
297 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
298 AIS_LengthDimension::ComputeSelection(aSelection, aMode);