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 <SketcherPrs_Tools.h>
20 #include <XGUI_ModuleConnector.h>
21 #include <XGUI_Displayer.h>
22 #include <XGUI_Workshop.h>
23 #include <XGUI_SelectionMgr.h>
24 #include <XGUI_Selection.h>
26 #include <GeomDataAPI_Point.h>
27 #include <GeomDataAPI_Dir.h>
28 #include <GeomDataAPI_Point2D.h>
29 #include <GeomAPI_Pln.h>
30 #include <GeomAPI_Pnt2d.h>
31 #include <GeomAPI_Pnt.h>
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_Vertex.h>
35 #include <GeomAPI_Dir.h>
36 #include <GeomAPI_XYZ.h>
38 #include <SketchPlugin_Feature.h>
39 #include <SketchPlugin_Sketch.h>
40 #include <SketchPlugin_ConstraintCoincidence.h>
41 #include <SketchPlugin_ConstraintDistance.h>
42 #include <SketchPlugin_ConstraintLength.h>
43 #include <SketchPlugin_ConstraintRadius.h>
44 #include <SketchPlugin_ConstraintRigid.h>
45 #include <SketchPlugin_Constraint.h>
46 #include <SketchPlugin_Circle.h>
47 #include <SketchPlugin_Arc.h>
48 #include <SketchPlugin_Line.h>
49 #include <SketchPlugin_Point.h>
51 #include <ModuleBase_IWorkshop.h>
52 #include <ModuleBase_ViewerPrs.h>
54 #include <V3d_View.hxx>
56 #include <gp_Circ.hxx>
57 #include <ProjLib.hxx>
59 #include <Geom_Line.hxx>
60 #include <GeomAPI_ProjectPointOnCurve.hxx>
61 #include <BRep_Tool.hxx>
63 #include <TopoDS_Edge.hxx>
64 #include <TopoDS_Vertex.hxx>
65 #include <AIS_InteractiveObject.hxx>
66 #include <StdSelect_BRepOwner.hxx>
67 #include <SelectMgr_IndexedMapOfOwner.hxx>
73 const double PRECISION_TOLERANCE = 0.000001;
75 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
80 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
81 theView->Eye(XEye, YEye, ZEye);
83 theView->At(XAt, YAt, ZAt);
84 gp_Pnt EyePoint(XEye, YEye, ZEye);
85 gp_Pnt AtPoint(XAt, YAt, ZAt);
87 gp_Vec EyeVector(EyePoint, AtPoint);
88 gp_Dir EyeDir(EyeVector);
90 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
91 Standard_Real X, Y, Z;
92 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
93 gp_Pnt ConvertedPoint(X, Y, Z);
95 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
96 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
101 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
102 Handle(V3d_View) theView,
103 double& theX, double& theY)
108 AttributeDoublePtr anAttr;
109 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
111 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
112 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
114 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
115 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
116 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
117 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
118 std::shared_ptr<GeomAPI_XYZ> anY = aNorm->xyz()->cross(aX->xyz());
120 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
121 gp_Vec aVec(anOriginPnt, thePoint);
123 if (!theView.IsNull()) {
124 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
125 theView->Eye(XEye, YEye, ZEye);
127 theView->At(XAt, YAt, ZAt);
128 gp_Pnt EyePoint(XEye, YEye, ZEye);
129 gp_Pnt AtPoint(XAt, YAt, ZAt);
131 gp_Vec anEyeVec(EyePoint, AtPoint);
132 anEyeVec.Normalize();
134 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
135 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
136 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
138 double aDen = anEyeVec * aNormalVec;
139 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
141 gp_Vec aDeltaVec = anEyeVec * aLVec;
142 aVec = aVec - aDeltaVec;
144 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
145 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
148 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::convertTo2D(FeaturePtr theSketch,
149 const std::shared_ptr<GeomAPI_Pnt>& thePnt)
151 std::shared_ptr<GeomAPI_Pnt2d> aRes;
152 if (theSketch->getKind() != SketchPlugin_Sketch::ID())
154 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
155 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
156 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
157 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
158 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
159 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
160 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
161 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
165 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch)
167 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
169 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
170 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
171 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
172 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
173 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
174 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
175 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
177 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
178 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
180 return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
183 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
185 return ModelAPI_Session::get()->moduleDocument();
188 /*std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::getFeaturePoint(FeaturePtr theFeature,
189 double theX, double theY)
191 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
192 new GeomAPI_Pnt2d(theX, theY));
193 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
194 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
195 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
196 aLast = anAttiributes.end();
197 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
198 for (; anIt != aLast && !aFPoint; anIt++) {
199 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = std::dynamic_pointer_cast<
200 GeomDataAPI_Point2D>(*anIt);
201 if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
208 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
209 const std::string& theAttribute)
213 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
214 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
215 aData->attribute(theAttribute));
217 anAttribute->setValue(theValue);
220 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
226 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
227 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
228 aData->attribute(theAttribute));
230 aValue = anAttribute->value();
237 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
238 const std::string& theKind)
244 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
245 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
246 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
248 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
249 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
250 aFeature = FeaturePtr();
256 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
257 std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
258 std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
262 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
264 std::shared_ptr<ModelAPI_Document> aDoc = document();
265 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
268 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
270 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
271 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
272 aRef1->setAttr(thePoint1);
274 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
275 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
276 aRef2->setAttr(thePoint2);
278 if (aFeature) // TODO: generate an error if feature was not created
282 /*std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::
283 findAttributePoint(CompositeFeaturePtr theSketch, double theX, double theY,
284 double theTolerance, const QList<FeaturePtr>& theIgnore)
286 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
287 new GeomAPI_Pnt2d(theX, theY));
289 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
290 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
291 FeaturePtr aFeature = theSketch->subFeature(i);
292 if (!theIgnore.contains(aFeature)) {
293 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
295 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt;
296 for (anIt = anAttiributes.cbegin(); anIt != anAttiributes.cend(); ++anIt) {
297 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
298 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
299 double x = aCurPoint->x();
300 double y = aCurPoint->y();
301 if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < theTolerance)) {
307 return std::shared_ptr<GeomDataAPI_Point2D>();
311 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
312 const std::string& theAttribute, double theClickedX,
315 // find a feature point by the selection mode
316 //std::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
317 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
318 GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
322 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
323 // the constraint is created between the feature point and the found sketch point
324 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
325 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
326 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
328 std::list<ObjectPtr> aFeatures = aRefList->list();
329 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
330 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
331 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
332 new GeomAPI_Pnt2d(theClickedX, theClickedY));
333 for (; anIt != aLast; anIt++) {
334 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
335 if (!aFeature.get() || theFeature == aFeature)
337 // find the given point in the feature attributes
338 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
339 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
340 aLast = anAttiributes.end();
341 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
342 for (; anIt != aLast && !aFPoint; anIt++) {
343 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
344 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
345 if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())) {
351 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
355 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
357 std::shared_ptr<GeomAPI_Pln> aPlane;
359 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
361 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
362 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
363 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
364 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
365 if (aNormal && anOrigin) {
366 double adX = aNormal->x();
367 double adY = aNormal->y();
368 double adZ = aNormal->z();
370 if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
371 double aX = anOrigin->x();
372 double aY = anOrigin->y();
373 double aZ = anOrigin->z();
374 gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
375 double aA, aB, aC, aD;
376 aPln.Coefficients(aA, aB, aC, aD);
377 aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
384 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
385 CompositeFeaturePtr theSketch)
387 std::shared_ptr<GeomAPI_Pnt> aPoint;
388 if (!theSketch || !thePoint2D)
391 DataPtr aData = theSketch->data();
392 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
393 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
394 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
395 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
396 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
397 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
398 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
400 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
403 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
404 const ObjectPtr& theObject,
405 CompositeFeaturePtr theSketch)
408 if (theShape.ShapeType() == TopAbs_EDGE) {
409 // Check that we already have such external edge
410 std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
411 aInEdge->setImpl(new TopoDS_Shape(theShape));
412 aResult = findExternalEdge(theSketch, aInEdge);
414 if (theShape.ShapeType() == TopAbs_VERTEX) {
415 std::shared_ptr<GeomAPI_Vertex> aInVert = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex());
416 aInVert->setImpl(new TopoDS_Shape(theShape));
417 aResult = findExternalVertex(theSketch, aInVert);
422 ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShape,
423 const ObjectPtr& theObject,
424 CompositeFeaturePtr theSketch,
425 const bool theTemporary)
427 if (theShape.ShapeType() == TopAbs_EDGE) {
428 Standard_Real aStart, aEnd;
429 Handle(V3d_View) aNullView;
430 FeaturePtr aMyFeature;
432 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
433 GeomAdaptor_Curve aAdaptor(aCurve);
434 if (aAdaptor.GetType() == GeomAbs_Line) {
436 aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
437 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
438 std::shared_ptr<GeomAPI_Edge> anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
439 anEdge->setImpl(new TopoDS_Shape(theShape));
440 if (anEdge->isArc()) {
442 aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
446 aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
450 DataPtr aData = aMyFeature->data();
451 AttributeSelectionPtr anAttr =
452 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
453 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
455 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
456 if (anAttr && aRes) {
457 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
458 anEdge->setImpl(new TopoDS_Shape(theShape));
460 anAttr->setValue(aRes, anEdge);
461 //if (!theTemporary) {
462 aMyFeature->execute();
465 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
466 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
467 setObject(aMyFeature->lastResult());
469 return aMyFeature->lastResult();
473 if (theShape.ShapeType() == TopAbs_VERTEX) {
474 FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
477 DataPtr aData = aMyFeature->data();
478 AttributeSelectionPtr anAttr =
479 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
480 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
482 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
483 if (anAttr && aRes) {
484 std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
485 aVert->setImpl(new TopoDS_Shape(theShape));
487 anAttr->setValue(aRes, aVert);
488 //if (theTemporary) {
489 aMyFeature->execute();
492 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
493 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
494 setObject(aMyFeature->lastResult());
496 return aMyFeature->lastResult();
503 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
504 const ModuleBase_ViewerPrs& thePrs)
506 foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
507 if (aPrs.object() == thePrs.object())
513 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge)
515 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
516 FeaturePtr aFeature = theSketch->subFeature(i);
517 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
518 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
520 if (aSketchFea->isExternal()) {
521 std::list<ResultPtr> aResults = aSketchFea->results();
522 std::list<ResultPtr>::const_iterator aIt;
523 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
524 ResultConstructionPtr aRes =
525 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
527 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
529 if (theEdge->isEqual(aShape))
541 ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
543 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
544 FeaturePtr aFeature = theSketch->subFeature(i);
545 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
546 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
548 if (aSketchFea->isExternal()) {
549 std::list<ResultPtr> aResults = aSketchFea->results();
550 std::list<ResultPtr>::const_iterator aIt;
551 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
552 ResultConstructionPtr aRes =
553 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
555 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
557 if (theVert->isEqual(aShape))
569 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch,
570 Handle_V3d_View theView, double& theX, double& theY)
572 bool aHasVertex = false;
574 const TopoDS_Shape& aShape = thePrs.shape();
575 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX)
577 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
578 if (!aVertex.IsNull())
580 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
581 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
589 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
590 ModuleBase_IWorkshop* theWorkshop)
592 // 1. find an attribute value in attribute reference attribute value
594 AttributeRefAttrPtr aRefAttr =
595 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
597 if (!aRefAttr->isObject()) {
598 AttributePtr theAttribute = aRefAttr->attr();
599 if (theAttribute.get()) {
600 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
601 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
603 // 2. find visualized vertices of the attribute and if the attribute of the vertex is
604 // the same, return it
605 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
606 // 2.1 get visualized results of the feature
607 const std::list<ResultPtr>& aResList = anAttributeFeature->results();
608 std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
609 for (; anIt != aLast; anIt++) {
610 AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
611 if (aAISObj.get() != NULL) {
612 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
613 // 2.2 find selected owners of a visualizedd object
614 SelectMgr_IndexedMapOfOwner aSelectedOwners;
615 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
616 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
617 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
618 if (!anOwner.IsNull()) {
619 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
620 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
621 const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
622 if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
623 // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
624 // attribute, returns the shape
625 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
626 PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
627 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
628 aBRepShape, aSketchMgr->activeSketch());
629 if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
630 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
631 aShape->setImpl(new TopoDS_Shape(aBRepShape));
646 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
647 const std::string& theAttribute)
649 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
651 if (!theFeature->data())
652 return std::shared_ptr<GeomAPI_Pnt2d>();
655 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
656 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
658 return std::shared_ptr<GeomAPI_Pnt2d>();
660 aFeature = ModelAPI_Feature::feature(anAttr->object());
662 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
663 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
664 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
666 else if (anAttr->attr()) {
667 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
669 if (aPointAttr.get() != NULL)
670 return aPointAttr->pnt();
671 return std::shared_ptr<GeomAPI_Pnt2d>();
674 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
677 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
679 FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
680 if (!theList.contains(aObj)) {
681 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
682 if (aOrig.get() == NULL)
684 theList.append(aObj);
685 const std::set<AttributePtr>& aRefsList = aObj->data()->refsToMe();
686 std::set<AttributePtr>::const_iterator aIt;
687 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
688 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
689 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
690 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
691 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
692 if (aPnt.get() && aOrig->isEqual(aPnt)) {
693 findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_A());
694 findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_B());
701 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
703 std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
704 SketchPlugin_Constraint::ENTITY_A());
705 if (aPnt.get() == NULL)
706 aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
710 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
711 const TopoDS_Shape theShape,
712 FeaturePtr theSketch)
715 AttributePtr anAttribute;
716 FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
718 if (theShape.ShapeType() == TopAbs_VERTEX) {
719 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
720 if (!aVertex.IsNull()) {
721 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
722 std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
723 new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
725 // find the given point in the feature attributes
726 std::list<AttributePtr> anAttiributes =
727 aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
728 std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
729 aLast = anAttiributes.end();
730 for (; anIt != aLast && !anAttribute; anIt++) {
731 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
732 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
734 std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
735 if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
736 anAttribute = aCurPoint;