1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_Tools.h
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 ObjectPtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
182 FeaturePtr theSketch,
183 const QList<ModuleBase_ViewerPrs>& theSelected,
184 const QList<ModuleBase_ViewerPrs>& theHighlighted)
186 // firstly it finds the feature in the list of highlight
187 ObjectPtr aDeltaObject = nearestFeature(thePoint, theView, theSketch, theHighlighted);
189 // secondly it finds the feature in the list of selected objects
190 aDeltaObject = nearestFeature(thePoint, theView, theSketch, theSelected);
195 ObjectPtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
196 FeaturePtr theSketch,
197 const QList<ModuleBase_ViewerPrs>& thePresentations)
199 ObjectPtr aDeltaObject;
201 CompositeFeaturePtr aSketch =
202 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
203 // 1. find it in the selected list by the selected point
206 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
207 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
210 double aMinDelta = -1;
211 ModuleBase_ViewerPrs aPrs;
212 foreach (ModuleBase_ViewerPrs aPrs, thePresentations) {
215 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
216 std::shared_ptr<SketchPlugin_Feature> aSketchFeature = std::dynamic_pointer_cast<
217 SketchPlugin_Feature>(aFeature);
218 if (!aSketchFeature || !aSketch->isSub(aSketchFeature))
221 double aDelta = aSketchFeature->distanceToPoint(
222 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
223 if (aMinDelta < 0 || aMinDelta > aDelta) {
225 // TODO aDeltaObject = aPrs.result();
229 // 2. if the object is not found, returns the first selected sketch feature
230 if (!aDeltaObject && thePresentations.size() > 0) {
231 // there can be some highlighted objects, e.g. a result of boolean operation and a sketch point
232 foreach (ModuleBase_ViewerPrs aPrs, thePresentations) {
235 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
236 if (aFeature && aSketch->isSub(aFeature))
237 aDeltaObject = aPrs.object();
243 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
245 return ModelAPI_Session::get()->moduleDocument();
248 /*std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::getFeaturePoint(FeaturePtr theFeature,
249 double theX, double theY)
251 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
252 new GeomAPI_Pnt2d(theX, theY));
253 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
254 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
255 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
256 aLast = anAttiributes.end();
257 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
258 for (; anIt != aLast && !aFPoint; anIt++) {
259 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = std::dynamic_pointer_cast<
260 GeomDataAPI_Point2D>(*anIt);
261 if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
268 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
269 const std::string& theAttribute)
273 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
274 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
275 aData->attribute(theAttribute));
277 anAttribute->setValue(theValue);
280 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
286 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
287 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
288 aData->attribute(theAttribute));
290 aValue = anAttribute->value();
297 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
298 const std::string& theKind)
304 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
305 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
306 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
308 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
309 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
310 aFeature = FeaturePtr();
316 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
317 std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
318 std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
322 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
324 std::shared_ptr<ModelAPI_Document> aDoc = document();
325 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
328 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
330 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
331 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
332 aRef1->setAttr(thePoint1);
334 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
335 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
336 aRef2->setAttr(thePoint2);
338 if (aFeature) // TODO: generate an error if feature was not created
342 /*std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::
343 findAttributePoint(CompositeFeaturePtr theSketch, double theX, double theY,
344 double theTolerance, const QList<FeaturePtr>& theIgnore)
346 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
347 new GeomAPI_Pnt2d(theX, theY));
349 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
350 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
351 FeaturePtr aFeature = theSketch->subFeature(i);
352 if (!theIgnore.contains(aFeature)) {
353 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
355 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt;
356 for (anIt = anAttiributes.cbegin(); anIt != anAttiributes.cend(); ++anIt) {
357 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
358 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
359 double x = aCurPoint->x();
360 double y = aCurPoint->y();
361 if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < theTolerance)) {
367 return std::shared_ptr<GeomDataAPI_Point2D>();
371 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
372 const std::string& theAttribute, double theClickedX,
375 // find a feature point by the selection mode
376 //std::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
377 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
378 GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
382 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
383 // the constraint is created between the feature point and the found sketch point
384 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
385 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
386 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
388 std::list<ObjectPtr> aFeatures = aRefList->list();
389 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
390 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
391 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
392 new GeomAPI_Pnt2d(theClickedX, theClickedY));
393 for (; anIt != aLast; anIt++) {
394 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
395 if (theFeature == aFeature)
397 // find the given point in the feature attributes
398 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
399 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
400 aLast = anAttiributes.end();
401 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
402 for (; anIt != aLast && !aFPoint; anIt++) {
403 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
404 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
405 if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())) {
411 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
415 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
417 std::shared_ptr<GeomAPI_Pln> aPlane;
419 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
421 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
422 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
423 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
424 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
425 if (aNormal && anOrigin) {
426 double adX = aNormal->x();
427 double adY = aNormal->y();
428 double adZ = aNormal->z();
430 if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
431 double aX = anOrigin->x();
432 double aY = anOrigin->y();
433 double aZ = anOrigin->z();
434 gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
435 double aA, aB, aC, aD;
436 aPln.Coefficients(aA, aB, aC, aD);
437 aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
444 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
445 CompositeFeaturePtr theSketch)
447 std::shared_ptr<GeomAPI_Pnt> aPoint;
448 if (!theSketch || !thePoint2D)
451 DataPtr aData = theSketch->data();
452 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
453 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
454 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
455 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
456 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
457 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
458 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
460 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
463 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
464 const ObjectPtr& theObject,
465 CompositeFeaturePtr theSketch)
468 if (theShape.ShapeType() == TopAbs_EDGE) {
469 // Check that we already have such external edge
470 std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
471 aInEdge->setImpl(new TopoDS_Shape(theShape));
472 aResult = findExternalEdge(theSketch, aInEdge);
474 if (theShape.ShapeType() == TopAbs_VERTEX) {
475 std::shared_ptr<GeomAPI_Vertex> aInVert = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex());
476 aInVert->setImpl(new TopoDS_Shape(theShape));
477 aResult = findExternalVertex(theSketch, aInVert);
482 ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShape,
483 const ObjectPtr& theObject,
484 CompositeFeaturePtr theSketch)
486 if (theShape.ShapeType() == TopAbs_EDGE) {
487 Standard_Real aStart, aEnd;
488 Handle(V3d_View) aNullView;
489 FeaturePtr aMyFeature;
491 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
492 GeomAdaptor_Curve aAdaptor(aCurve);
493 if (aAdaptor.GetType() == GeomAbs_Line) {
495 aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
496 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
497 std::shared_ptr<GeomAPI_Edge> anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
498 anEdge->setImpl(new TopoDS_Shape(theShape));
499 if (anEdge->isArc()) {
501 aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
505 aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
509 DataPtr aData = aMyFeature->data();
510 AttributeSelectionPtr anAttr =
511 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
512 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
514 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
515 if (anAttr && aRes) {
516 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
517 anEdge->setImpl(new TopoDS_Shape(theShape));
519 anAttr->setValue(aRes, anEdge);
521 aMyFeature->execute();
524 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
525 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
526 setObject(aMyFeature->lastResult());
528 return aMyFeature->lastResult();
532 if (theShape.ShapeType() == TopAbs_VERTEX) {
533 FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
536 DataPtr aData = aMyFeature->data();
537 AttributeSelectionPtr anAttr =
538 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
539 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
541 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
542 if (anAttr && aRes) {
543 std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
544 aVert->setImpl(new TopoDS_Shape(theShape));
546 anAttr->setValue(aRes, aVert);
547 aMyFeature->execute();
550 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
551 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
552 setObject(aMyFeature->lastResult());
554 return aMyFeature->lastResult();
561 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
562 const ModuleBase_ViewerPrs& thePrs)
564 foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
565 if (aPrs.object() == thePrs.object())
571 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge)
573 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
574 FeaturePtr aFeature = theSketch->subFeature(i);
575 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
576 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
578 if (aSketchFea->isExternal()) {
579 std::list<ResultPtr> aResults = aSketchFea->results();
580 std::list<ResultPtr>::const_iterator aIt;
581 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
582 ResultConstructionPtr aRes =
583 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
585 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
587 if (theEdge->isEqual(aShape))
599 ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
601 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
602 FeaturePtr aFeature = theSketch->subFeature(i);
603 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
604 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
606 if (aSketchFea->isExternal()) {
607 std::list<ResultPtr> aResults = aSketchFea->results();
608 std::list<ResultPtr>::const_iterator aIt;
609 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
610 ResultConstructionPtr aRes =
611 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
613 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
615 if (theVert->isEqual(aShape))
627 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch,
628 Handle_V3d_View theView, double& theX, double& theY)
630 bool aHasVertex = false;
632 const TopoDS_Shape& aShape = thePrs.shape();
633 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX)
635 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
636 if (!aVertex.IsNull())
638 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
639 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
647 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
648 ModuleBase_IWorkshop* theWorkshop)
650 // 1. find an attribute value in attribute reference attribute value
652 AttributeRefAttrPtr aRefAttr =
653 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
655 if (!aRefAttr->isObject()) {
656 AttributePtr theAttribute = aRefAttr->attr();
657 if (theAttribute.get()) {
658 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
659 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
661 // 2. find visualized vertices of the attribute and if the attribute of the vertex is
662 // the same, return it
663 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
664 // 2.1 get visualized results of the feature
665 const std::list<ResultPtr>& aResList = anAttributeFeature->results();
666 std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
667 for (; anIt != aLast; anIt++) {
668 AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
669 if (aAISObj.get() != NULL) {
670 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
671 // 2.2 find selected owners of a visualizedd object
672 SelectMgr_IndexedMapOfOwner aSelectedOwners;
673 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
674 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
675 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
676 if (!anOwner.IsNull()) {
677 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
678 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
679 const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
680 if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
681 // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
682 // attribute, returns the shape
683 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
684 PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
685 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
686 aBRepShape, aSketchMgr->activeSketch());
687 if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
688 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
689 aShape->setImpl(new TopoDS_Shape(aBRepShape));
704 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
705 const std::string& theAttribute)
707 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
709 if (!theFeature->data())
710 return std::shared_ptr<GeomAPI_Pnt2d>();
713 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
714 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
716 aFeature = ModelAPI_Feature::feature(anAttr->object());
718 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
719 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
720 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
722 else if (anAttr->attr()) {
723 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
725 if (aPointAttr.get() != NULL)
726 return aPointAttr->pnt();
727 return std::shared_ptr<GeomAPI_Pnt2d>();
730 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
733 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
734 FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
735 if (!theList.contains(aObj)) {
736 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getPoint(theStartCoin, theAttr);
737 if (aOrig.get() == NULL)
739 theList.append(aObj);
740 const std::set<AttributePtr>& aRefsList = aObj->data()->refsToMe();
741 std::set<AttributePtr>::const_iterator aIt;
742 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
743 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
744 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
745 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
746 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getPoint(aConstrFeature, theAttr);
747 if (aPnt.get() && aOrig->isEqual(aPnt)) {
748 findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_A());
749 findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_B());
756 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
757 const TopoDS_Shape theShape,
758 FeaturePtr theSketch)
761 AttributePtr anAttribute;
762 FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
764 if (theShape.ShapeType() == TopAbs_VERTEX) {
765 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
766 if (!aVertex.IsNull()) {
767 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
768 std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
769 new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
771 // find the given point in the feature attributes
772 std::list<AttributePtr> anAttiributes =
773 aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
774 std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
775 aLast = anAttiributes.end();
776 for (; anIt != aLast && !anAttribute; anIt++) {
777 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
778 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
780 std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
781 if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
782 anAttribute = aCurPoint;