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 std::shared_ptr<GeomAPI_Edge> anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
435 anEdge->setImpl(new TopoDS_Shape(theShape));
436 if (aAdaptor.GetType() == GeomAbs_Line) {
438 aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
439 if (!theObject.get()) {
440 // There is no selected result
441 std::shared_ptr<GeomAPI_Pnt> aPnt1 = anEdge->firstPoint();
442 std::shared_ptr<GeomAPI_Pnt> aPnt2 = anEdge->lastPoint();
443 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = convertTo2D(theSketch, aPnt1);
444 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d2 = convertTo2D(theSketch, aPnt2);
446 std::shared_ptr<ModelAPI_Data> aData = aMyFeature->data();
447 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
448 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
449 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
450 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
452 aPoint1->setValue(aPnt2d1);
453 aPoint2->setValue(aPnt2d2);
455 // If this is an axis then its name has to be changed correspondently
456 std::string aSuffix = "";
457 bool aXdir = fabs(aPnt1->x() - aPnt2->x()) > Precision::Confusion();
458 bool aYdir = fabs(aPnt1->y() - aPnt2->y()) > Precision::Confusion();
459 bool aZdir = fabs(aPnt1->z() - aPnt2->z()) > Precision::Confusion();
460 if (aXdir && (!aYdir) && (!aZdir))
462 else if ((!aXdir) && aYdir && (!aZdir))
464 else if ((!aXdir) && (!aYdir) && aZdir)
466 if (aSuffix.length() > 0)
467 aData->setName("Axis_" + aSuffix);
468 aMyFeature->execute();
471 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
472 if (anEdge->isArc()) {
474 aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
478 aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
482 DataPtr aData = aMyFeature->data();
483 AttributeSelectionPtr anAttr =
484 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
485 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
487 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
489 aRes = aMyFeature->firstResult();
491 if (anAttr.get() && aRes.get()) {
492 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
493 anEdge->setImpl(new TopoDS_Shape(theShape));
495 anAttr->setValue(aRes, anEdge);
496 //if (!theTemporary) {
497 aMyFeature->execute();
500 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
501 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
502 setObject(aMyFeature->lastResult());
504 return aMyFeature->lastResult();
508 if (theShape.ShapeType() == TopAbs_VERTEX) {
509 FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
512 DataPtr aData = aMyFeature->data();
513 AttributeSelectionPtr anAttr =
514 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
515 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
517 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
519 // If the point is selected not from Result object
520 std::shared_ptr<GeomAPI_Shape> aShape =
521 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
522 aShape->setImpl(new TopoDS_Shape(theShape));
524 std::shared_ptr<GeomAPI_Vertex> aVertex =
525 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
526 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
528 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = convertTo2D(theSketch, aPnt);
529 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
530 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Point::COORD_ID()));
531 aPoint->setValue(aPnt2d);
532 if ((aPnt->x() < Precision::Confusion()) &&
533 (aPnt->y() < Precision::Confusion()) &&
534 (aPnt->z() < Precision::Confusion()))
535 aData->setName("Origin");
537 aMyFeature->execute();
538 aRes = aMyFeature->firstResult();
540 if (anAttr.get() && aRes.get()) {
541 std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
542 aVert->setImpl(new TopoDS_Shape(theShape));
544 anAttr->setValue(aRes, aVert);
545 //if (theTemporary) {
546 aMyFeature->execute();
549 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
550 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
551 setObject(aMyFeature->lastResult());
553 return aMyFeature->lastResult();
560 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
561 const ModuleBase_ViewerPrs& thePrs)
563 foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
564 if (aPrs.object() == thePrs.object())
570 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge)
572 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
573 FeaturePtr aFeature = theSketch->subFeature(i);
574 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
575 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
577 if (aSketchFea->isExternal()) {
578 std::list<ResultPtr> aResults = aSketchFea->results();
579 std::list<ResultPtr>::const_iterator aIt;
580 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
581 ResultConstructionPtr aRes =
582 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
584 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
586 if (theEdge->isEqual(aShape))
598 ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
600 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
601 FeaturePtr aFeature = theSketch->subFeature(i);
602 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
603 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
605 if (aSketchFea->isExternal()) {
606 std::list<ResultPtr> aResults = aSketchFea->results();
607 std::list<ResultPtr>::const_iterator aIt;
608 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
609 ResultConstructionPtr aRes =
610 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
612 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
614 if (theVert->isEqual(aShape))
626 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch,
627 Handle_V3d_View theView, double& theX, double& theY)
629 bool aHasVertex = false;
631 const TopoDS_Shape& aShape = thePrs.shape();
632 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX)
634 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
635 if (!aVertex.IsNull())
637 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
638 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
646 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
647 ModuleBase_IWorkshop* theWorkshop)
649 // 1. find an attribute value in attribute reference attribute value
651 AttributeRefAttrPtr aRefAttr =
652 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
654 if (!aRefAttr->isObject()) {
655 AttributePtr theAttribute = aRefAttr->attr();
656 if (theAttribute.get()) {
657 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
658 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
660 // 2. find visualized vertices of the attribute and if the attribute of the vertex is
661 // the same, return it
662 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
663 // 2.1 get visualized results of the feature
664 const std::list<ResultPtr>& aResList = anAttributeFeature->results();
665 std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
666 for (; anIt != aLast; anIt++) {
667 AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
668 if (aAISObj.get() != NULL) {
669 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
670 // 2.2 find selected owners of a visualizedd object
671 SelectMgr_IndexedMapOfOwner aSelectedOwners;
672 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
673 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
674 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
675 if (!anOwner.IsNull()) {
676 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
677 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
678 const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
679 if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
680 // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
681 // attribute, returns the shape
682 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
683 PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
684 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
685 aBRepShape, aSketchMgr->activeSketch());
686 if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
687 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
688 aShape->setImpl(new TopoDS_Shape(aBRepShape));
703 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
704 const std::string& theAttribute)
706 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
708 if (!theFeature->data())
709 return std::shared_ptr<GeomAPI_Pnt2d>();
712 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
713 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
715 return std::shared_ptr<GeomAPI_Pnt2d>();
717 aFeature = ModelAPI_Feature::feature(anAttr->object());
719 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
720 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
721 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
723 else if (anAttr->attr()) {
724 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
726 if (aPointAttr.get() != NULL)
727 return aPointAttr->pnt();
728 return std::shared_ptr<GeomAPI_Pnt2d>();
731 FeaturePtr findFirstCoincidenceByData(const DataPtr& theData, std::shared_ptr<GeomAPI_Pnt2d> thePoint)
733 FeaturePtr aCoincident;
735 const std::set<AttributePtr>& aRefsList = theData->refsToMe();
736 std::set<AttributePtr>::const_iterator aIt;
737 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
738 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
739 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
740 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
741 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
742 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
743 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
744 aCoincident = aConstrFeature;
747 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
748 SketchPlugin_ConstraintCoincidence::ENTITY_B());
749 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
750 aCoincident = aConstrFeature;
759 FeaturePtr PartSet_Tools::findFirstCoincidence(const FeaturePtr& theFeature,
760 std::shared_ptr<GeomAPI_Pnt2d> thePoint,
761 const bool theSearchInResults)
763 FeaturePtr aCoincident;
764 if (theFeature.get() == NULL)
767 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
768 std::set<AttributePtr>::const_iterator aIt;
769 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
770 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
771 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
772 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
773 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
774 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
775 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
776 aCoincident = aConstrFeature;
779 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
780 SketchPlugin_ConstraintCoincidence::ENTITY_B());
781 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
782 aCoincident = aConstrFeature;
788 if (theSearchInResults) {
789 if (!aCoincident.get()) {
790 std::list<ResultPtr> aResults = theFeature->results();
791 std::list<ResultPtr>::const_iterator aIt;
792 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
793 ResultPtr aResult = *aIt;
794 aCoincident = findFirstCoincidenceByData(aResult->data(), thePoint);
795 if (aCoincident.get())
803 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
806 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
808 FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
809 if (!theList.contains(aObj)) {
810 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
811 if (aOrig.get() == NULL)
813 theList.append(aObj);
814 const std::set<AttributePtr>& aRefsList = aObj->data()->refsToMe();
815 std::set<AttributePtr>::const_iterator aIt;
816 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
817 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
818 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
819 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
820 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
821 if (aPnt.get() && aOrig->isEqual(aPnt)) {
822 findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_A());
823 findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_B());
830 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
832 std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
833 SketchPlugin_Constraint::ENTITY_A());
834 if (aPnt.get() == NULL)
835 aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
839 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
840 const TopoDS_Shape theShape,
841 FeaturePtr theSketch)
844 AttributePtr anAttribute;
845 FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
847 if (theShape.ShapeType() == TopAbs_VERTEX) {
848 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
849 if (!aVertex.IsNull()) {
850 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
851 std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
852 new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
854 // find the given point in the feature attributes
855 std::list<AttributePtr> anAttiributes =
856 aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
857 std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
858 aLast = anAttiributes.end();
859 for (; anIt != aLast && !anAttribute; anIt++) {
860 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
861 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
863 std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
864 if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
865 anAttribute = aCurPoint;