1 // Copyright (C) 2014-2020 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
20 #include "SketcherPrs_LengthDimension.h"
21 #include "SketcherPrs_Tools.h"
22 #include "SketcherPrs_DimensionStyle.h"
24 #include <SketchPlugin_Constraint.h>
25 #include <SketchPlugin_ConstraintLength.h>
26 #include <SketchPlugin_ConstraintDistance.h>
27 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
28 #include <SketchPlugin_ConstraintDistanceVertical.h>
29 #include <SketchPlugin_Line.h>
30 #include <SketchPlugin_Point.h>
31 #include <SketchPlugin_Circle.h>
33 #include <GeomDataAPI_Point2D.h>
34 #include <GeomAPI_Pnt.h>
35 #include <GeomAPI_XYZ.h>
36 #include <GeomAPI_Pnt2d.h>
37 #include <GeomAPI_Lin2d.h>
39 #include <ModelAPI_Data.h>
40 #include <ModelAPI_AttributeDouble.h>
41 #include <ModelAPI_AttributeInteger.h>
43 #include <AIS_DisplaySpecialSymbol.hxx>
45 #define 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::getConfigTextHeight());
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, SketcherPrs_Tools::LocationType theLocationType)
69 if (theLocationType == SketcherPrs_Tools::LOCATION_AUTOMATIC) {
70 double anArrowLength = theDimAspect->ArrowAspect()->Length();
71 // This is not realy correct way to get viewer scale.
72 double aViewerScale = (double) SketcherPrs_Tools::getConfigArrowSize() / anArrowLength;
74 if(theTextSize > ((theDimValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
75 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
76 theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
77 theDimAspect->SetExtensionSize(
78 (theTextSize / aViewerScale + SketcherPrs_Tools::getArrowSize()) / 2.0);
80 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
81 theDimAspect->SetArrowOrientation(Prs3d_DAO_Internal);
84 else if (theLocationType == SketcherPrs_Tools::LOCATION_RIGHT ||
85 theLocationType == SketcherPrs_Tools::LOCATION_LEFT) {
86 theDimAspect->SetTextHorizontalPosition(
87 theLocationType == SketcherPrs_Tools::LOCATION_LEFT ? Prs3d_DTHP_Left : Prs3d_DTHP_Right);
88 theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
90 double anArrowLength = theDimAspect->ArrowAspect()->Length();
91 // This is not realy correct way to get viewer scale.
92 double aViewerScale = (double) SketcherPrs_Tools::getConfigArrowSize() / anArrowLength;
93 theDimAspect->SetExtensionSize(
94 (theTextSize / aViewerScale + SketcherPrs_Tools::getArrowSize()) / 2.0);
96 theDimAspect->TextAspect()->SetFont(Config_PropManager::string("Visualization",
97 "dimension_font").c_str());
99 theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
100 // The value of vertical aligment is sometimes changed
101 theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
105 static const gp_Pnt MyDefStart(0,0,0);
106 static const gp_Pnt MyDefEnd(1,0,0);
107 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
109 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
111 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
112 SketchPlugin_Sketch* theSketcher)
113 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
114 myConstraint(theConstraint),
115 mySketcher(theSketcher),
116 myFirstPoint(MyDefStart),
117 mySecondPoint(MyDefEnd),
120 myValue(0., false, "")
122 SetDimensionAspect(createDimensionAspect());
123 myStyleListener = new SketcherPrs_DimensionStyle();
125 setDirection(theConstraint, plane());
128 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
130 delete myStyleListener;
133 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
134 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
136 gp_Pnt2d aPnt1, aPnt2;
137 return readyToDisplay(theConstraint, thePlane, aPnt1, aPnt2);
140 static bool isEqualPoints(ModelAPI_Feature* theConstraint,
141 const gp_Pnt2d& theP1, const gp_Pnt2d& theP2)
143 bool isEqual = false;
144 if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID())
145 isEqual = Abs(theP1.X() - theP2.X()) < Precision::Confusion();
146 else if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
147 isEqual = Abs(theP1.Y() - theP2.Y()) < Precision::Confusion();
149 isEqual = theP1.SquareDistance(theP2) < Precision::SquareConfusion();
154 void SketcherPrs_LengthDimension::Compute(
155 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
156 const Handle(Prs3d_Presentation)& thePresentation,
157 const Standard_Integer theMode)
162 bool aReadyToDisplay = readyToDisplay(myConstraint, plane(), aP1, aP2);
163 if (aReadyToDisplay) {
164 if (isEqualPoints(myConstraint, aP1, aP2)) {
165 // adjust points to draw the dimension presentation
166 std::shared_ptr<GeomDataAPI_Dir> aDirAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
167 myConstraint->attribute(SketchPlugin_ConstraintDistance::DIRECTION_ID()));
168 double x = 0.0, y = 0.0;
169 if (aDirAttr && aDirAttr->isInitialized()) {
172 if (x == 0.0 && y == 0.0)
175 else if (myConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
181 aP1.ChangeCoord().Add(aDir * (-Precision::Confusion()));
182 aP2.ChangeCoord().Add(aDir * Precision::Confusion());
185 myFirstPoint = plane()->to3D(aP1.X(), aP1.Y())->impl<gp_Pnt>();
186 mySecondPoint = plane()->to3D(aP2.X(), aP2.Y())->impl<gp_Pnt>();
188 myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
189 myPlane = gp_Pln(plane()->impl<gp_Ax3>());
191 DataPtr aData = myConstraint->data();
192 AttributeDoublePtr anAttributeValue;
193 if (myConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
194 myConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
195 anAttributeValue = aData->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID());
197 anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
198 myValue.init(anAttributeValue);
200 setDirection(myConstraint, plane());
203 // compute flyout distance
204 SetFlyout(myDistance);
205 SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
207 // Update variable aspect parameters (depending on viewer scale)
208 double aTextSize = 0.0;
209 GetValueString(aTextSize);
211 std::string aLocationAttribute;
212 std::string aConstraintKind = myConstraint->getKind();
213 if (aConstraintKind == SketchPlugin_ConstraintLength::ID())
214 aLocationAttribute = SketchPlugin_ConstraintLength::LOCATION_TYPE_ID();
215 else if (aConstraintKind == SketchPlugin_ConstraintDistance::ID())
216 aLocationAttribute = SketchPlugin_ConstraintDistance::LOCATION_TYPE_ID();
217 else if (aConstraintKind == SketchPlugin_ConstraintDistanceHorizontal::ID())
218 aLocationAttribute = SketchPlugin_ConstraintDistanceHorizontal::LOCATION_TYPE_ID();
219 else if (aConstraintKind == SketchPlugin_ConstraintDistanceVertical::ID())
220 aLocationAttribute = SketchPlugin_ConstraintDistanceVertical::LOCATION_TYPE_ID();
222 AttributeIntegerPtr aLocAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>
223 (myConstraint->data()->attribute(aLocationAttribute));
224 SketcherPrs_Tools::LocationType aLocationType = aLocAttr->isInitialized() ?
225 (SketcherPrs_Tools::LocationType)(aLocAttr->value()) : SketcherPrs_Tools::LOCATION_AUTOMATIC;
226 updateArrows(DimensionAspect(), GetValue(), aTextSize, aLocationType);
228 // Update text visualization: parameter value or parameter text
229 myStyleListener->updateDimensions(this, myValue);
231 AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
233 if (!aReadyToDisplay)
234 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
235 "An empty AIS presentation: SketcherPrs_LengthDimension");
238 bool SketcherPrs_LengthDimension::readyToDisplay(ModelAPI_Feature* theConstraint,
239 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
240 gp_Pnt2d& thePnt1, gp_Pnt2d& thePnt2)
245 DataPtr aData = theConstraint->data();
246 if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
247 // The constraint is length
248 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
249 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
250 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
254 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
255 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
258 // Get geometry of the object
259 DataPtr aLineData = aFeature->data();
260 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
261 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
262 (aLineData->attribute(SketchPlugin_Line::START_ID()));
263 std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
264 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
265 (aLineData->attribute(SketchPlugin_Line::END_ID()));
266 thePnt1 = gp_Pnt2d(aStartPoint->x(), aStartPoint->y());
267 thePnt2 = gp_Pnt2d(aEndPoint->x(), aEndPoint->y());
270 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID() ||
271 theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
272 theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID()) {
273 // The constraint is distance
274 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
275 aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
276 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
277 aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
279 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
280 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
281 if (aPoint_A && aPoint_B) {
282 // Both objects are points
283 aPnt_A = aPoint_A->pnt();
284 aPnt_B = aPoint_B->pnt();
285 } else if (!aPoint_A && aPoint_B) {
286 // First object is line
287 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
288 aData, SketchPlugin_Constraint::ENTITY_A());
290 aPnt_B = aPoint_B->pnt();
291 aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
293 } else if (aPoint_A && !aPoint_B) {
294 // Second object is line
295 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
296 aData, SketchPlugin_Constraint::ENTITY_B());
298 aPnt_A = aPoint_A->pnt();
299 aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
302 if (!aPnt_A || !aPnt_B) // Objects not found
305 // Get points from these object
306 thePnt1 = gp_Pnt2d(aPnt_A->x(), aPnt_A->y());
307 thePnt2 = gp_Pnt2d(aPnt_B->x(), aPnt_B->y());
315 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
316 const Standard_Integer theMode)
318 // Map the application selection modes to standard ones
319 Standard_Integer aMode;
321 case 0: // we should use selection of all objects
324 case SketcherPrs_Tools::Sel_Dimension_All:
327 case SketcherPrs_Tools::Sel_Dimension_Line:
330 case SketcherPrs_Tools::Sel_Dimension_Text:
334 // there are own selection modes, so the others should be ignored
335 // otherwise, the text selection appears in the viewer
339 SetSelToleranceForText2d(SketcherPrs_Tools::getArrowSize()/5.);
340 AIS_LengthDimension::ComputeSelection(aSelection, aMode);
343 void SketcherPrs_LengthDimension::setDirection(ModelAPI_Feature* theConstraint,
344 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
346 #ifdef OCCT_28850_FIXED
347 if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID())
348 SetDirection(thePlane->dirX()->impl<gp_Dir>(), true);
349 else if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
350 SetDirection(thePlane->dirY()->impl<gp_Dir>(), true);