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 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
189 const std::string& theAttribute)
193 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
194 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
195 aData->attribute(theAttribute));
197 anAttribute->setValue(theValue);
200 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
206 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
207 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
208 aData->attribute(theAttribute));
210 aValue = anAttribute->value();
217 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
218 const std::string& theKind)
224 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
225 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
226 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
228 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
229 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
230 aFeature = FeaturePtr();
236 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
237 std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
238 std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
242 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
244 std::shared_ptr<ModelAPI_Document> aDoc = document();
245 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
248 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
250 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
251 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
252 aRef1->setAttr(thePoint1);
254 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
255 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
256 aRef2->setAttr(thePoint2);
258 if (aFeature) // TODO: generate an error if feature was not created
262 /*std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::
263 findAttributePoint(CompositeFeaturePtr theSketch, double theX, double theY,
264 double theTolerance, const QList<FeaturePtr>& theIgnore)
266 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
267 new GeomAPI_Pnt2d(theX, theY));
269 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
270 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
271 FeaturePtr aFeature = theSketch->subFeature(i);
272 if (!theIgnore.contains(aFeature)) {
273 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
275 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt;
276 for (anIt = anAttiributes.cbegin(); anIt != anAttiributes.cend(); ++anIt) {
277 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
278 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
279 double x = aCurPoint->x();
280 double y = aCurPoint->y();
281 if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < theTolerance)) {
287 return std::shared_ptr<GeomDataAPI_Point2D>();
291 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPointInArgumentFeatures(
292 const FeaturePtr& theFeature, const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
294 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
296 // may be feature is not updated yet, execute is not performed and references features
297 // are not created. Case: rectangle macro feature
298 ModuleBase_ModelWidget::updateObject(theFeature);
300 std::list<AttributePtr> anAttributes = theFeature->data()->attributes(
301 ModelAPI_AttributeRefList::typeId());
302 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
303 for (; anIt != aLast && !aFeaturePoint.get(); anIt++) {
304 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
305 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
306 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFeaturePoint.get(); i++) {
307 ObjectPtr anObject = aCurSelList->object(i);
308 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
310 aFeaturePoint = PartSet_Tools::findFirstEqualPoint(aFeature, thePoint);
313 return aFeaturePoint;
316 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPoint(const FeaturePtr& theFeature,
317 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
319 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
321 // find the given point in the feature attributes
322 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
323 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
324 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
325 aLast = anAttiributes.end();
326 for (; anIt != aLast && !aFPoint; anIt++) {
327 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
328 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
329 if (aCurPoint && (aCurPoint->pnt()->distance(thePoint) < Precision::Confusion())) {
337 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
338 const std::string& theAttribute, double theClickedX,
341 if (!theFeature.get())
344 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
345 new GeomAPI_Pnt2d(theClickedX, theClickedY));
347 // find a feature point by the selection mode
348 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
349 if (theFeature->isMacro()) {
350 // the macro feature will be removed after the operation is stopped, so we need to build
351 // coicidence to possible sub-features
352 aFeaturePoint = PartSet_Tools::findFirstEqualPointInArgumentFeatures(theFeature, aClickedPoint);
355 aFeaturePoint = std::dynamic_pointer_cast<
356 GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
361 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
362 // the constraint is created between the feature point and the found sketch point
363 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
364 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
365 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
367 std::list<ObjectPtr> aFeatures = aRefList->list();
368 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
369 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
370 for (; anIt != aLast; anIt++) {
371 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
372 if (!aFeature.get() || theFeature == aFeature)
374 std::shared_ptr<GeomDataAPI_Point2D> aFPoint = PartSet_Tools::findFirstEqualPoint(aFeature,
377 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
381 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
383 std::shared_ptr<GeomAPI_Pln> aPlane;
385 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
387 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
388 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
389 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
390 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
391 if (aNormal && anOrigin) {
392 double adX = aNormal->x();
393 double adY = aNormal->y();
394 double adZ = aNormal->z();
396 if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
397 double aX = anOrigin->x();
398 double aY = anOrigin->y();
399 double aZ = anOrigin->z();
400 gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
401 double aA, aB, aC, aD;
402 aPln.Coefficients(aA, aB, aC, aD);
403 aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
410 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
411 CompositeFeaturePtr theSketch)
413 std::shared_ptr<GeomAPI_Pnt> aPoint;
414 if (!theSketch || !thePoint2D)
417 DataPtr aData = theSketch->data();
418 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
419 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
420 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
421 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
422 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
423 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
424 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
426 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
429 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
430 const ObjectPtr& theObject,
431 CompositeFeaturePtr theSketch)
434 if (theShape.ShapeType() == TopAbs_EDGE) {
435 // Check that we already have such external edge
436 std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
437 aInEdge->setImpl(new TopoDS_Shape(theShape));
438 aResult = findExternalEdge(theSketch, aInEdge);
440 if (theShape.ShapeType() == TopAbs_VERTEX) {
441 std::shared_ptr<GeomAPI_Vertex> aInVert = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex());
442 aInVert->setImpl(new TopoDS_Shape(theShape));
443 aResult = findExternalVertex(theSketch, aInVert);
448 ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShape,
449 const ObjectPtr& theObject,
450 CompositeFeaturePtr theSketch,
451 const bool theTemporary)
453 if (theShape.ShapeType() == TopAbs_EDGE) {
454 Standard_Real aStart, aEnd;
455 Handle(V3d_View) aNullView;
456 FeaturePtr aMyFeature;
458 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
459 GeomAdaptor_Curve aAdaptor(aCurve);
460 std::shared_ptr<GeomAPI_Edge> anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
461 anEdge->setImpl(new TopoDS_Shape(theShape));
462 if (aAdaptor.GetType() == GeomAbs_Line) {
464 aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
465 if (!theObject.get()) {
466 // There is no selected result
467 std::shared_ptr<GeomAPI_Pnt> aPnt1 = anEdge->firstPoint();
468 std::shared_ptr<GeomAPI_Pnt> aPnt2 = anEdge->lastPoint();
469 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = convertTo2D(theSketch, aPnt1);
470 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d2 = convertTo2D(theSketch, aPnt2);
472 std::shared_ptr<ModelAPI_Data> aData = aMyFeature->data();
473 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
474 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
475 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
476 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
478 aPoint1->setValue(aPnt2d1);
479 aPoint2->setValue(aPnt2d2);
481 // If this is an axis then its name has to be changed correspondently
482 std::string aSuffix = "";
483 bool aXdir = fabs(aPnt1->x() - aPnt2->x()) > Precision::Confusion();
484 bool aYdir = fabs(aPnt1->y() - aPnt2->y()) > Precision::Confusion();
485 bool aZdir = fabs(aPnt1->z() - aPnt2->z()) > Precision::Confusion();
486 if (aXdir && (!aYdir) && (!aZdir))
488 else if ((!aXdir) && aYdir && (!aZdir))
490 else if ((!aXdir) && (!aYdir) && aZdir)
492 if (aSuffix.length() > 0)
493 aData->setName("Axis_" + aSuffix);
494 aMyFeature->execute();
497 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
498 if (anEdge->isArc()) {
500 aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
504 aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
508 DataPtr aData = aMyFeature->data();
509 AttributeSelectionPtr anAttr =
510 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
511 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
513 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
515 aRes = aMyFeature->firstResult();
517 if (anAttr.get() && aRes.get()) {
518 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
519 anEdge->setImpl(new TopoDS_Shape(theShape));
521 anAttr->setValue(aRes, anEdge);
522 //if (!theTemporary) {
523 aMyFeature->execute();
526 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
527 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
528 setObject(aMyFeature->lastResult());
530 return aMyFeature->lastResult();
534 if (theShape.ShapeType() == TopAbs_VERTEX) {
535 FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
538 DataPtr aData = aMyFeature->data();
539 AttributeSelectionPtr anAttr =
540 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
541 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
543 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
545 // If the point is selected not from Result object
546 std::shared_ptr<GeomAPI_Shape> aShape =
547 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
548 aShape->setImpl(new TopoDS_Shape(theShape));
550 std::shared_ptr<GeomAPI_Vertex> aVertex =
551 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
552 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
554 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = convertTo2D(theSketch, aPnt);
555 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
556 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Point::COORD_ID()));
557 aPoint->setValue(aPnt2d);
558 if ((aPnt->x() < Precision::Confusion()) &&
559 (aPnt->y() < Precision::Confusion()) &&
560 (aPnt->z() < Precision::Confusion()))
561 aData->setName("Origin");
563 aMyFeature->execute();
564 aRes = aMyFeature->firstResult();
566 if (anAttr.get() && aRes.get()) {
567 std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
568 aVert->setImpl(new TopoDS_Shape(theShape));
570 anAttr->setValue(aRes, aVert);
571 //if (theTemporary) {
572 aMyFeature->execute();
575 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
576 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
577 setObject(aMyFeature->lastResult());
579 return aMyFeature->lastResult();
586 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
587 const ModuleBase_ViewerPrs& thePrs)
589 foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
590 if (aPrs.object() == thePrs.object())
596 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge)
598 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
599 FeaturePtr aFeature = theSketch->subFeature(i);
600 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
601 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
603 if (aSketchFea->isExternal()) {
604 std::list<ResultPtr> aResults = aSketchFea->results();
605 std::list<ResultPtr>::const_iterator aIt;
606 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
607 ResultConstructionPtr aRes =
608 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
610 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
612 if (theEdge->isEqual(aShape))
624 ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
626 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
627 FeaturePtr aFeature = theSketch->subFeature(i);
628 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
629 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
631 if (aSketchFea->isExternal()) {
632 std::list<ResultPtr> aResults = aSketchFea->results();
633 std::list<ResultPtr>::const_iterator aIt;
634 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
635 ResultConstructionPtr aRes =
636 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
638 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
640 if (theVert->isEqual(aShape))
652 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch,
653 Handle_V3d_View theView, double& theX, double& theY)
655 bool aHasVertex = false;
657 const TopoDS_Shape& aShape = thePrs.shape();
658 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX)
660 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
661 if (!aVertex.IsNull())
663 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
664 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
672 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
673 ModuleBase_IWorkshop* theWorkshop)
676 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
677 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
679 // 2. find visualized vertices of the attribute and if the attribute of the vertex is
680 // the same, return it
681 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
682 // 2.1 get visualized results of the feature
683 const std::list<ResultPtr>& aResList = anAttributeFeature->results();
684 std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
685 for (; anIt != aLast; anIt++) {
686 AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
687 if (aAISObj.get() != NULL) {
688 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
689 // 2.2 find selected owners of a visualizedd object
690 SelectMgr_IndexedMapOfOwner aSelectedOwners;
691 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
692 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
693 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
694 if (!anOwner.IsNull()) {
695 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
696 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
697 const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
698 if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
699 // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
700 // attribute, returns the shape
701 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
702 PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
703 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
704 aBRepShape, aSketchMgr->activeSketch());
705 if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
706 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
707 aShape->setImpl(new TopoDS_Shape(aBRepShape));
719 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
720 const std::string& theAttribute)
722 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
724 if (!theFeature->data())
725 return std::shared_ptr<GeomAPI_Pnt2d>();
728 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
729 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
731 return std::shared_ptr<GeomAPI_Pnt2d>();
733 aFeature = ModelAPI_Feature::feature(anAttr->object());
735 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
736 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
737 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
739 else if (anAttr->attr()) {
740 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
742 if (aPointAttr.get() != NULL)
743 return aPointAttr->pnt();
744 return std::shared_ptr<GeomAPI_Pnt2d>();
747 FeaturePtr findFirstCoincidenceByData(const DataPtr& theData, std::shared_ptr<GeomAPI_Pnt2d> thePoint)
749 FeaturePtr aCoincident;
751 const std::set<AttributePtr>& aRefsList = theData->refsToMe();
752 std::set<AttributePtr>::const_iterator aIt;
753 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
754 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
755 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
756 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
757 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
758 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
759 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
760 aCoincident = aConstrFeature;
763 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
764 SketchPlugin_ConstraintCoincidence::ENTITY_B());
765 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
766 aCoincident = aConstrFeature;
775 FeaturePtr PartSet_Tools::findFirstCoincidence(const FeaturePtr& theFeature,
776 std::shared_ptr<GeomAPI_Pnt2d> thePoint,
777 const bool theSearchInResults)
779 FeaturePtr aCoincident;
780 if (theFeature.get() == NULL)
783 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
784 std::set<AttributePtr>::const_iterator aIt;
785 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
786 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
787 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
788 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
789 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
790 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
791 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
792 aCoincident = aConstrFeature;
795 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
796 SketchPlugin_ConstraintCoincidence::ENTITY_B());
797 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
798 aCoincident = aConstrFeature;
804 if (theSearchInResults) {
805 if (!aCoincident.get()) {
806 std::list<ResultPtr> aResults = theFeature->results();
807 std::list<ResultPtr>::const_iterator aIt;
808 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
809 ResultPtr aResult = *aIt;
810 aCoincident = findFirstCoincidenceByData(aResult->data(), thePoint);
811 if (aCoincident.get())
819 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
822 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
824 FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
825 if (!theList.contains(aObj)) {
826 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
827 if (aOrig.get() == NULL)
829 theList.append(aObj);
830 const std::set<AttributePtr>& aRefsList = aObj->data()->refsToMe();
831 std::set<AttributePtr>::const_iterator aIt;
832 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
833 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
834 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
835 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
836 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
837 if (aPnt.get() && aOrig->isEqual(aPnt)) {
838 findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_A());
839 findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_B());
846 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
848 std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
849 SketchPlugin_Constraint::ENTITY_A());
850 if (aPnt.get() == NULL)
851 aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
855 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
856 const TopoDS_Shape theShape,
857 FeaturePtr theSketch)
860 AttributePtr anAttribute;
861 FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
863 if (theShape.ShapeType() == TopAbs_VERTEX) {
864 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
865 if (!aVertex.IsNull()) {
866 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
867 std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
868 new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
870 // find the given point in the feature attributes
871 std::list<AttributePtr> anAttiributes =
872 aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
873 std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
874 aLast = anAttiributes.end();
875 for (; anIt != aLast && !anAttribute; anIt++) {
876 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
877 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
879 std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
880 if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
881 anAttribute = aCurPoint;