1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_Tools.cpp
4 // Created: 28 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include <PartSet_Tools.h>
8 #include <PartSet_Module.h>
9 #include <PartSet_SketcherMgr.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_AttributeRefList.h>
14 #include <ModelAPI_Document.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_ResultConstruction.h>
18 #include <XGUI_ModuleConnector.h>
19 #include <XGUI_Displayer.h>
20 #include <XGUI_Workshop.h>
21 #include <XGUI_SelectionMgr.h>
22 #include <XGUI_Selection.h>
24 #include <GeomDataAPI_Point.h>
25 #include <GeomDataAPI_Dir.h>
26 #include <GeomDataAPI_Point2D.h>
27 #include <GeomAPI_Pln.h>
28 #include <GeomAPI_Pnt2d.h>
29 #include <GeomAPI_Pnt.h>
30 #include <GeomAPI_Edge.h>
31 #include <GeomAPI_Vertex.h>
33 #include <GeomAPI_Dir.h>
34 #include <GeomAPI_XYZ.h>
36 #include <SketchPlugin_Feature.h>
37 #include <SketchPlugin_Sketch.h>
38 #include <SketchPlugin_ConstraintCoincidence.h>
39 #include <SketchPlugin_ConstraintDistance.h>
40 #include <SketchPlugin_ConstraintLength.h>
41 #include <SketchPlugin_ConstraintRadius.h>
42 #include <SketchPlugin_ConstraintRigid.h>
43 #include <SketchPlugin_Constraint.h>
44 #include <SketchPlugin_Circle.h>
45 #include <SketchPlugin_Arc.h>
46 #include <SketchPlugin_Line.h>
47 #include <SketchPlugin_Point.h>
49 #include <ModuleBase_IWorkshop.h>
50 #include <ModuleBase_ViewerPrs.h>
52 #include <V3d_View.hxx>
54 #include <gp_Circ.hxx>
55 #include <ProjLib.hxx>
57 #include <Geom_Line.hxx>
58 #include <GeomAPI_ProjectPointOnCurve.hxx>
59 #include <BRep_Tool.hxx>
61 #include <TopoDS_Edge.hxx>
62 #include <TopoDS_Vertex.hxx>
63 #include <AIS_InteractiveObject.hxx>
64 #include <StdSelect_BRepOwner.hxx>
65 #include <SelectMgr_IndexedMapOfOwner.hxx>
71 const double PRECISION_TOLERANCE = 0.000001;
73 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
78 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
79 theView->Eye(XEye, YEye, ZEye);
81 theView->At(XAt, YAt, ZAt);
82 gp_Pnt EyePoint(XEye, YEye, ZEye);
83 gp_Pnt AtPoint(XAt, YAt, ZAt);
85 gp_Vec EyeVector(EyePoint, AtPoint);
86 gp_Dir EyeDir(EyeVector);
88 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
89 Standard_Real X, Y, Z;
90 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
91 gp_Pnt ConvertedPoint(X, Y, Z);
93 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
94 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
99 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
100 Handle(V3d_View) theView,
101 double& theX, double& theY)
106 AttributeDoublePtr anAttr;
107 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
109 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
110 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
112 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
113 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
114 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
115 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
116 std::shared_ptr<GeomAPI_XYZ> anY = aNorm->xyz()->cross(aX->xyz());
118 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
119 gp_Vec aVec(anOriginPnt, thePoint);
121 if (!theView.IsNull()) {
122 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
123 theView->Eye(XEye, YEye, ZEye);
125 theView->At(XAt, YAt, ZAt);
126 gp_Pnt EyePoint(XEye, YEye, ZEye);
127 gp_Pnt AtPoint(XAt, YAt, ZAt);
129 gp_Vec anEyeVec(EyePoint, AtPoint);
130 anEyeVec.Normalize();
132 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
133 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
134 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
136 double aDen = anEyeVec * aNormalVec;
137 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
139 gp_Vec aDeltaVec = anEyeVec * aLVec;
140 aVec = aVec - aDeltaVec;
142 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
143 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
146 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::convertTo2D(FeaturePtr theSketch,
147 const std::shared_ptr<GeomAPI_Pnt>& thePnt)
149 std::shared_ptr<GeomAPI_Pnt2d> aRes;
150 if (theSketch->getKind() != SketchPlugin_Sketch::ID())
152 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
153 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
154 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
155 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
156 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
157 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
158 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
159 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
163 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch)
165 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
167 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
168 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
169 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
170 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
171 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
172 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
173 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
175 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
176 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
178 return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
181 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
183 return ModelAPI_Session::get()->moduleDocument();
186 /*std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::getFeaturePoint(FeaturePtr theFeature,
187 double theX, double theY)
189 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
190 new GeomAPI_Pnt2d(theX, theY));
191 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
192 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
193 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
194 aLast = anAttiributes.end();
195 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
196 for (; anIt != aLast && !aFPoint; anIt++) {
197 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = std::dynamic_pointer_cast<
198 GeomDataAPI_Point2D>(*anIt);
199 if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
206 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
207 const std::string& theAttribute)
211 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
212 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
213 aData->attribute(theAttribute));
215 anAttribute->setValue(theValue);
218 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
224 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
225 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
226 aData->attribute(theAttribute));
228 aValue = anAttribute->value();
235 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
236 const std::string& theKind)
242 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
243 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
244 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
246 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
247 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
248 aFeature = FeaturePtr();
254 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
255 std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
256 std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
260 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
262 std::shared_ptr<ModelAPI_Document> aDoc = document();
263 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
266 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
268 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
269 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
270 aRef1->setAttr(thePoint1);
272 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
273 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
274 aRef2->setAttr(thePoint2);
276 if (aFeature) // TODO: generate an error if feature was not created
280 /*std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::
281 findAttributePoint(CompositeFeaturePtr theSketch, double theX, double theY,
282 double theTolerance, const QList<FeaturePtr>& theIgnore)
284 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
285 new GeomAPI_Pnt2d(theX, theY));
287 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
288 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
289 FeaturePtr aFeature = theSketch->subFeature(i);
290 if (!theIgnore.contains(aFeature)) {
291 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
293 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt;
294 for (anIt = anAttiributes.cbegin(); anIt != anAttiributes.cend(); ++anIt) {
295 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
296 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
297 double x = aCurPoint->x();
298 double y = aCurPoint->y();
299 if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < theTolerance)) {
305 return std::shared_ptr<GeomDataAPI_Point2D>();
309 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
310 const std::string& theAttribute, double theClickedX,
313 // find a feature point by the selection mode
314 //std::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
315 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
316 GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
320 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
321 // the constraint is created between the feature point and the found sketch point
322 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
323 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
324 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
326 std::list<ObjectPtr> aFeatures = aRefList->list();
327 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
328 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
329 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
330 new GeomAPI_Pnt2d(theClickedX, theClickedY));
331 for (; anIt != aLast; anIt++) {
332 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
333 if (!aFeature.get() || theFeature == aFeature)
335 // find the given point in the feature attributes
336 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
337 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
338 aLast = anAttiributes.end();
339 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
340 for (; anIt != aLast && !aFPoint; anIt++) {
341 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
342 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
343 if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())) {
349 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
353 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
355 std::shared_ptr<GeomAPI_Pln> aPlane;
357 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
359 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
360 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
361 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
362 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
363 if (aNormal && anOrigin) {
364 double adX = aNormal->x();
365 double adY = aNormal->y();
366 double adZ = aNormal->z();
368 if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
369 double aX = anOrigin->x();
370 double aY = anOrigin->y();
371 double aZ = anOrigin->z();
372 gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
373 double aA, aB, aC, aD;
374 aPln.Coefficients(aA, aB, aC, aD);
375 aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
382 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
383 CompositeFeaturePtr theSketch)
385 std::shared_ptr<GeomAPI_Pnt> aPoint;
386 if (!theSketch || !thePoint2D)
389 DataPtr aData = theSketch->data();
390 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
391 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
392 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
393 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
394 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
395 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
396 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
398 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
401 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
402 const ObjectPtr& theObject,
403 CompositeFeaturePtr theSketch)
406 if (theShape.ShapeType() == TopAbs_EDGE) {
407 // Check that we already have such external edge
408 std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
409 aInEdge->setImpl(new TopoDS_Shape(theShape));
410 aResult = findExternalEdge(theSketch, aInEdge);
412 if (theShape.ShapeType() == TopAbs_VERTEX) {
413 std::shared_ptr<GeomAPI_Vertex> aInVert = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex());
414 aInVert->setImpl(new TopoDS_Shape(theShape));
415 aResult = findExternalVertex(theSketch, aInVert);
420 ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShape,
421 const ObjectPtr& theObject,
422 CompositeFeaturePtr theSketch,
423 const bool theTemporary)
425 if (theShape.ShapeType() == TopAbs_EDGE) {
426 Standard_Real aStart, aEnd;
427 Handle(V3d_View) aNullView;
428 FeaturePtr aMyFeature;
430 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
431 GeomAdaptor_Curve aAdaptor(aCurve);
432 if (aAdaptor.GetType() == GeomAbs_Line) {
434 aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
435 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
436 std::shared_ptr<GeomAPI_Edge> anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
437 anEdge->setImpl(new TopoDS_Shape(theShape));
438 if (anEdge->isArc()) {
440 aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
444 aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
448 DataPtr aData = aMyFeature->data();
449 AttributeSelectionPtr anAttr =
450 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
451 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
453 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
454 if (anAttr && aRes) {
455 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
456 anEdge->setImpl(new TopoDS_Shape(theShape));
458 anAttr->setValue(aRes, anEdge);
459 //if (!theTemporary) {
460 aMyFeature->execute();
463 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
464 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
465 setObject(aMyFeature->lastResult());
467 return aMyFeature->lastResult();
471 if (theShape.ShapeType() == TopAbs_VERTEX) {
472 FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
475 DataPtr aData = aMyFeature->data();
476 AttributeSelectionPtr anAttr =
477 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
478 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
480 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
481 if (anAttr && aRes) {
482 std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
483 aVert->setImpl(new TopoDS_Shape(theShape));
485 anAttr->setValue(aRes, aVert);
486 //if (theTemporary) {
487 aMyFeature->execute();
490 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
491 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
492 setObject(aMyFeature->lastResult());
494 return aMyFeature->lastResult();
501 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
502 const ModuleBase_ViewerPrs& thePrs)
504 foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
505 if (aPrs.object() == thePrs.object())
511 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge)
513 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
514 FeaturePtr aFeature = theSketch->subFeature(i);
515 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
516 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
518 if (aSketchFea->isExternal()) {
519 std::list<ResultPtr> aResults = aSketchFea->results();
520 std::list<ResultPtr>::const_iterator aIt;
521 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
522 ResultConstructionPtr aRes =
523 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
525 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
527 if (theEdge->isEqual(aShape))
539 ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
541 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
542 FeaturePtr aFeature = theSketch->subFeature(i);
543 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
544 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
546 if (aSketchFea->isExternal()) {
547 std::list<ResultPtr> aResults = aSketchFea->results();
548 std::list<ResultPtr>::const_iterator aIt;
549 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
550 ResultConstructionPtr aRes =
551 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
553 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
555 if (theVert->isEqual(aShape))
567 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch,
568 Handle_V3d_View theView, double& theX, double& theY)
570 bool aHasVertex = false;
572 const TopoDS_Shape& aShape = thePrs.shape();
573 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX)
575 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
576 if (!aVertex.IsNull())
578 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
579 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
587 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
588 ModuleBase_IWorkshop* theWorkshop)
590 // 1. find an attribute value in attribute reference attribute value
592 AttributeRefAttrPtr aRefAttr =
593 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
595 if (!aRefAttr->isObject()) {
596 AttributePtr theAttribute = aRefAttr->attr();
597 if (theAttribute.get()) {
598 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
599 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
601 // 2. find visualized vertices of the attribute and if the attribute of the vertex is
602 // the same, return it
603 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
604 // 2.1 get visualized results of the feature
605 const std::list<ResultPtr>& aResList = anAttributeFeature->results();
606 std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
607 for (; anIt != aLast; anIt++) {
608 AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
609 if (aAISObj.get() != NULL) {
610 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
611 // 2.2 find selected owners of a visualizedd object
612 SelectMgr_IndexedMapOfOwner aSelectedOwners;
613 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
614 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
615 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
616 if (!anOwner.IsNull()) {
617 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
618 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
619 const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
620 if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
621 // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
622 // attribute, returns the shape
623 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
624 PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
625 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
626 aBRepShape, aSketchMgr->activeSketch());
627 if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
628 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
629 aShape->setImpl(new TopoDS_Shape(aBRepShape));
644 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
645 const std::string& theAttribute)
647 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
649 if (!theFeature->data())
650 return std::shared_ptr<GeomAPI_Pnt2d>();
653 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
654 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
656 return std::shared_ptr<GeomAPI_Pnt2d>();
658 aFeature = ModelAPI_Feature::feature(anAttr->object());
660 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
661 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
662 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
664 else if (anAttr->attr()) {
665 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
667 if (aPointAttr.get() != NULL)
668 return aPointAttr->pnt();
669 return std::shared_ptr<GeomAPI_Pnt2d>();
672 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
675 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
677 FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
678 if (!theList.contains(aObj)) {
679 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getPoint(theStartCoin, theAttr);
680 if (aOrig.get() == NULL)
682 theList.append(aObj);
683 const std::set<AttributePtr>& aRefsList = aObj->data()->refsToMe();
684 std::set<AttributePtr>::const_iterator aIt;
685 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
686 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
687 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
688 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
689 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getPoint(aConstrFeature, theAttr);
690 if (aPnt.get() && aOrig->isEqual(aPnt)) {
691 findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_A());
692 findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_B());
699 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
700 const TopoDS_Shape theShape,
701 FeaturePtr theSketch)
704 AttributePtr anAttribute;
705 FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
707 if (theShape.ShapeType() == TopAbs_VERTEX) {
708 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
709 if (!aVertex.IsNull()) {
710 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
711 std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
712 new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
714 // find the given point in the feature attributes
715 std::list<AttributePtr> anAttiributes =
716 aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
717 std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
718 aLast = anAttiributes.end();
719 for (; anIt != aLast && !anAttribute; anIt++) {
720 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
721 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
723 std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
724 if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
725 anAttribute = aCurPoint;