1 // Copyright (C) 2014-2019 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_Tools.h"
22 #include <SketchPlugin_Constraint.h>
23 #include <SketchPlugin_Point.h>
24 #include <SketchPlugin_Circle.h>
25 #include <SketchPlugin_Line.h>
26 #include <SketchPlugin_Arc.h>
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_AttributeRefAttr.h>
30 #include <ModelAPI_AttributeRefList.h>
31 #include <ModelAPI_AttributeDouble.h>
32 #include <ModelAPI_Events.h>
34 #include <ModelGeomAlgo_Point2D.h>
36 #include <Events_InfoMessage.h>
38 #include <GeomDataAPI_Point2D.h>
39 #include <GeomAPI_Lin2d.h>
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS_Vertex.hxx>
45 #include <Prs3d_DimensionAspect.hxx>
47 #include <BRep_Tool.hxx>
48 #include <Precision.hxx>
50 #include <AIS_Dimension.hxx>
52 namespace SketcherPrs_Tools {
54 static ParameterStyle MyStyle = ParameterValue;
56 void setParameterStyle(ParameterStyle theStyle)
61 ParameterStyle parameterStyle()
66 AttributePtr getAttribute(ModelAPI_Feature* theFeature, const std::string& theAttrName)
68 AttributePtr anAttribute;
70 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
71 if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
72 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
73 if (!anAttr->isObject())
74 anAttribute = anAttr->attr();
80 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
84 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
85 if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
86 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
88 anObject = anAttr->object();
95 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
97 ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
98 if (aRes.get() != NULL && aRes->data()->isValid()) {
99 /// essential check as it is called in openGl thread
100 return aRes->shape();
102 return std::shared_ptr<GeomAPI_Shape>();
106 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
107 const std::string& theAttribute)
109 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
110 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->attribute(theAttribute));
111 if (!aPointAttr.get() || !aPointAttr->isInitialized()) {
112 aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
113 theFeature, theAttribute, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
115 if (aPointAttr.get() != NULL)
116 return aPointAttr->pnt();
117 return std::shared_ptr<GeomAPI_Pnt2d>();
120 //*************************************************************************************
121 /// Find an attribute of the given object which corresponds to a vetrex
122 /// defined by a shape
123 /// \param theObject an object
124 /// \param theShape a vertex
125 /// \param thePlane a projection plane (sketcher plane)
126 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject,
127 const TopoDS_Shape& theShape,
128 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
130 std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
132 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
133 if (anObjectFeature) {
134 if (theShape.ShapeType() == TopAbs_VERTEX) {
135 const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
136 if (!aShapeVertex.IsNull()) {
137 gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
138 std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
139 new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
141 // find the given point in the feature attributes
142 std::list<AttributePtr> anObjectAttiributes =
143 anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
144 std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(),
145 aLast = anObjectAttiributes.end();
146 for (; anIt != aLast && !aGeomPoint; anIt++) {
147 std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
148 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
150 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
151 anAttributePoint->y());
152 if (anAttributePnt.get() &&
153 anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
154 aGeomPoint = anAttributePoint;
164 //*************************************************************************************
165 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
166 const std::string& theAttribute,
167 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
169 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
171 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread
175 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
176 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
178 if (anAttr->isObject()) {
179 ObjectPtr anObject = anAttr->object();
180 aFeature = ModelAPI_Feature::feature(anObject);
181 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
182 // Attribute refers to a point
183 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
184 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
187 // if the attribute refers on another object
188 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
190 GeomShapePtr aShape = aRes->shape();
192 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
193 aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
198 else if (anAttr->attr()) {
199 // If attribute is a point
200 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
206 //*************************************************************************************
207 std::list<ResultPtr> getFreePoints(const CompositeFeaturePtr& theSketch)
209 std::list<ResultPtr> aFreePoints;
213 AttributeRefListPtr aFeatures = theSketch->reflist(SketchPlugin_Sketch::FEATURES_ID());
216 std::list<ObjectPtr> anObjects = aFeatures->list();
217 for (std::list<ObjectPtr>::iterator anObjIt = anObjects.begin();
218 anObjIt != anObjects.end(); ++anObjIt) {
219 FeaturePtr aCurrent = ModelAPI_Feature::feature(*anObjIt);
220 if (aCurrent && aCurrent->getKind() == SketchPlugin_Point::ID()) {
221 // check point is not referred by any constraints: the feature and result of point
223 for(int aKind = 0; aIsFree && aKind < 2; aKind++) { // 0 for feature, 1 for result
224 ObjectPtr aReferenced = aCurrent;
226 if (!aCurrent->results().empty())
227 aReferenced = aCurrent->firstResult();
230 const std::set<AttributePtr>& aRefs = aReferenced->data()->refsToMe();
231 std::set<AttributePtr>::iterator aRIt = aRefs.begin();
232 for (; aRIt != aRefs.end(); ++aRIt) {
233 FeaturePtr aRefFeat = ModelAPI_Feature::feature((*aRIt)->owner());
234 std::shared_ptr<SketchPlugin_Constraint> aRefConstr =
235 std::dynamic_pointer_cast<SketchPlugin_Constraint>(aRefFeat);
243 aFreePoints.push_back(aCurrent->lastResult());
249 //*************************************************************************************
250 FeaturePtr getFeatureLine(DataPtr theData,
251 const std::string& theAttribute)
254 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
257 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
258 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
260 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
261 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
268 //*************************************************************************************
269 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
270 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
272 DataPtr aData = theLine->data();
273 if (!aData.get() || !aData->isValid())
274 return std::shared_ptr<GeomAPI_Pnt2d>();
276 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
277 aData->attribute(SketchPlugin_Line::START_ID()));
278 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
279 aData->attribute(SketchPlugin_Line::END_ID()));
281 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
282 return aLin2d.project(thePoint);
285 static int MyPixelRatio = 1;
287 void setPixelRatio(int theRatio)
289 MyPixelRatio = theRatio;
297 static double MyArrowSize = 20;
299 double getArrowSize()
304 void setArrowSize(double theSize)
306 MyArrowSize = theSize;
309 int getDefaultArrowSize()
314 int getConfigArrowSize()
316 return Config_PropManager::integer("Visualization", "dimension_arrow_size");
319 static double MyTextHeight = 16;
320 double getTextHeight()
322 return MyTextHeight * MyPixelRatio;
325 void setTextHeight(double theHeight)
327 MyTextHeight = theHeight;
330 double getDefaultTextHeight()
332 return 16 * MyPixelRatio;
335 double getConfigTextHeight()
337 return Config_PropManager::integer("Visualization", "dimension_value_size") * MyPixelRatio;
340 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
342 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
343 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
344 const_cast<ModelAPI_Feature*>(
345 theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
346 // for not initialized values return zero distance to avoid Presentation crash
347 if (!aFlyoutPoint->isInitialized())
349 return aFlyoutPoint->y();
352 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
353 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
355 ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
356 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
357 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
358 if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
359 return std::shared_ptr<GeomAPI_Pnt>();
361 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
362 std::shared_ptr<GeomAPI_Pnt2d> aCenter;
363 if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
364 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
365 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
366 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
367 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
368 aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
370 return std::shared_ptr<GeomAPI_Pnt>();
372 std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
373 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
374 aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
375 double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
376 aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
377 double aLen = aFlyoutPnt->distance(anOrigin);
378 aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
379 aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
380 aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
381 return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
384 void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError)
386 Events_InfoMessage("SketcherPrs_Tools",
387 "An empty AIS presentation: SketcherPrs_LengthDimension").send();
388 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
389 std::shared_ptr<ModelAPI_Object> aConstraintPtr(theFeature);
390 ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent);