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_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);
99 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
101 aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFeature->lastResult());
103 if (aRes.get() != NULL && aRes->data()->isValid()) {
104 /// essential check as it is called in openGl thread
105 return aRes->shape();
107 return std::shared_ptr<GeomAPI_Shape>();
111 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
112 const std::string& theAttribute)
114 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
115 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->attribute(theAttribute));
116 if (!aPointAttr.get() || !aPointAttr->isInitialized()) {
117 aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
118 theFeature, theAttribute, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
120 if (aPointAttr.get() != NULL)
121 return aPointAttr->pnt();
122 return std::shared_ptr<GeomAPI_Pnt2d>();
125 //*************************************************************************************
126 /// Find an attribute of the given object which corresponds to a vetrex
127 /// defined by a shape
128 /// \param theObject an object
129 /// \param theShape a vertex
130 /// \param thePlane a projection plane (sketcher plane)
131 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject,
132 const TopoDS_Shape& theShape,
133 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
135 std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
137 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
138 if (anObjectFeature) {
139 if (theShape.ShapeType() == TopAbs_VERTEX) {
140 const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
141 if (!aShapeVertex.IsNull()) {
142 gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
143 std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
144 new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
146 // find the given point in the feature attributes
147 std::list<AttributePtr> anObjectAttiributes =
148 anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
149 std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(),
150 aLast = anObjectAttiributes.end();
151 for (; anIt != aLast && !aGeomPoint; anIt++) {
152 std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
153 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
155 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
156 anAttributePoint->y());
157 if (anAttributePnt.get() &&
158 anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
159 aGeomPoint = anAttributePoint;
169 //*************************************************************************************
170 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
171 const std::string& theAttribute,
172 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
174 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
176 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread
180 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
181 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
183 if (anAttr->isObject()) {
184 ObjectPtr anObject = anAttr->object();
185 aFeature = ModelAPI_Feature::feature(anObject);
186 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
187 // Attribute refers to a point
188 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
189 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
192 // if the attribute refers on another object
193 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
195 GeomShapePtr aShape = aRes->shape();
197 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
198 aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
203 else if (anAttr->attr()) {
204 // If attribute is a point
205 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
211 //*************************************************************************************
212 std::list<ResultPtr> getFreePoints(const CompositeFeaturePtr& theSketch)
214 std::list<ResultPtr> aFreePoints;
218 AttributeRefListPtr aFeatures = theSketch->reflist(SketchPlugin_Sketch::FEATURES_ID());
221 std::list<ObjectPtr> anObjects = aFeatures->list();
222 for (std::list<ObjectPtr>::iterator anObjIt = anObjects.begin();
223 anObjIt != anObjects.end(); ++anObjIt) {
224 FeaturePtr aCurrent = ModelAPI_Feature::feature(*anObjIt);
225 if (aCurrent && aCurrent->getKind() == SketchPlugin_Point::ID()) {
226 // check point is not referred by any constraints: the feature and result of point
228 for(int aKind = 0; aIsFree && aKind < 2; aKind++) { // 0 for feature, 1 for result
229 ObjectPtr aReferenced = aCurrent;
231 if (!aCurrent->results().empty())
232 aReferenced = aCurrent->firstResult();
236 const std::set<AttributePtr>& aRefs = aReferenced->data()->refsToMe();
237 std::set<AttributePtr>::iterator aRIt = aRefs.begin();
238 for (; aRIt != aRefs.end(); ++aRIt) {
239 FeaturePtr aRefFeat = ModelAPI_Feature::feature((*aRIt)->owner());
240 std::shared_ptr<SketchPlugin_Constraint> aRefConstr =
241 std::dynamic_pointer_cast<SketchPlugin_Constraint>(aRefFeat);
249 aFreePoints.push_back(aCurrent->lastResult());
255 //*************************************************************************************
256 FeaturePtr getFeatureLine(DataPtr theData,
257 const std::string& theAttribute)
260 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
263 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
264 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
266 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
267 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
274 //*************************************************************************************
275 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
276 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
278 DataPtr aData = theLine->data();
279 if (!aData.get() || !aData->isValid())
280 return std::shared_ptr<GeomAPI_Pnt2d>();
282 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
283 aData->attribute(SketchPlugin_Line::START_ID()));
284 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
285 aData->attribute(SketchPlugin_Line::END_ID()));
287 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
288 return aLin2d.project(thePoint);
291 static int MyPixelRatio = 1;
293 void setPixelRatio(int theRatio)
295 MyPixelRatio = theRatio;
303 static double MyArrowSize = 20;
305 double getArrowSize()
310 void setArrowSize(double theSize)
312 MyArrowSize = theSize;
315 int getDefaultArrowSize()
320 int getConfigArrowSize()
322 return Config_PropManager::integer("Visualization", "dimension_arrow_size");
325 static double MyTextHeight = 16;
326 double getTextHeight()
328 return MyTextHeight * MyPixelRatio;
331 void setTextHeight(double theHeight)
333 MyTextHeight = theHeight;
336 double getDefaultTextHeight()
338 return 16 * MyPixelRatio;
341 double getConfigTextHeight()
343 return Config_PropManager::integer("Visualization", "dimension_value_size") * MyPixelRatio;
346 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
348 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
349 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
350 const_cast<ModelAPI_Feature*>(
351 theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
352 // for not initialized values return zero distance to avoid Presentation crash
353 if (!aFlyoutPoint->isInitialized())
355 return aFlyoutPoint->y();
358 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
359 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
361 ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
362 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
363 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
364 if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
365 return std::shared_ptr<GeomAPI_Pnt>();
367 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
368 std::shared_ptr<GeomAPI_Pnt2d> aCenter;
369 if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
370 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
371 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
372 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
373 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
374 aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
376 return std::shared_ptr<GeomAPI_Pnt>();
378 std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
379 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
380 aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
381 double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
382 aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
383 double aLen = aFlyoutPnt->distance(anOrigin);
384 aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
385 aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
386 aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
387 return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
390 void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError)
392 Events_InfoMessage("SketcherPrs_Tools",
393 "An empty AIS presentation: SketcherPrs_LengthDimension").send();
394 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
395 std::shared_ptr<ModelAPI_Object> aConstraintPtr(theFeature);
396 ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent);