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>
17 #include <ModelAPI_Events.h>
18 #include <ModelAPI_Validator.h>
20 #include <ModelGeomAlgo_Point2D.h>
22 #include <Events_Loop.h>
24 #include <SketcherPrs_Tools.h>
26 #include <XGUI_ModuleConnector.h>
27 #include <XGUI_Displayer.h>
28 #include <XGUI_Workshop.h>
29 #include <XGUI_SelectionMgr.h>
30 #include <XGUI_Selection.h>
32 #include <GeomDataAPI_Point.h>
33 #include <GeomDataAPI_Dir.h>
34 #include <GeomDataAPI_Point2D.h>
35 #include <GeomAPI_Pln.h>
36 #include <GeomAPI_Pnt2d.h>
37 #include <GeomAPI_Pnt.h>
38 #include <GeomAPI_Edge.h>
39 #include <GeomAPI_Vertex.h>
41 #include <GeomAPI_Dir.h>
42 #include <GeomAPI_XYZ.h>
44 #include <SketchPlugin_Feature.h>
45 #include <SketchPlugin_Sketch.h>
46 #include <SketchPlugin_ConstraintCoincidence.h>
47 #include <SketchPlugin_ConstraintDistance.h>
48 #include <SketchPlugin_ConstraintLength.h>
49 #include <SketchPlugin_ConstraintRadius.h>
50 #include <SketchPlugin_ConstraintRigid.h>
51 #include <SketchPlugin_Constraint.h>
52 #include <SketchPlugin_Circle.h>
53 #include <SketchPlugin_Arc.h>
54 #include <SketchPlugin_Line.h>
55 #include <SketchPlugin_Point.h>
57 #include <ModuleBase_IWorkshop.h>
58 #include <ModuleBase_ViewerPrs.h>
59 #include <ModuleBase_Tools.h>
61 #include <V3d_View.hxx>
63 #include <gp_Circ.hxx>
64 #include <ProjLib.hxx>
66 #include <Geom_Line.hxx>
67 #include <GeomAPI_ProjectPointOnCurve.hxx>
68 #include <BRep_Tool.hxx>
70 #include <TopoDS_Edge.hxx>
71 #include <TopoDS_Vertex.hxx>
72 #include <AIS_InteractiveObject.hxx>
73 #include <StdSelect_BRepOwner.hxx>
74 #include <SelectMgr_IndexedMapOfOwner.hxx>
80 const double PRECISION_TOLERANCE = 0.000001;
81 const int AIS_DEFAULT_WIDTH = 2;
83 int PartSet_Tools::getAISDefaultWidth()
85 return AIS_DEFAULT_WIDTH;
88 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
93 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
94 theView->Eye(XEye, YEye, ZEye);
96 theView->At(XAt, YAt, ZAt);
97 gp_Pnt EyePoint(XEye, YEye, ZEye);
98 gp_Pnt AtPoint(XAt, YAt, ZAt);
100 gp_Vec EyeVector(EyePoint, AtPoint);
101 gp_Dir EyeDir(EyeVector);
103 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
104 Standard_Real X, Y, Z;
105 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
106 gp_Pnt ConvertedPoint(X, Y, Z);
108 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
109 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
114 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
115 Handle(V3d_View) theView,
116 double& theX, double& theY)
121 AttributeDoublePtr anAttr;
122 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
124 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
125 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
127 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
128 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
129 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
130 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
131 std::shared_ptr<GeomAPI_XYZ> anY = aNorm->xyz()->cross(aX->xyz());
133 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
134 gp_Vec aVec(anOriginPnt, thePoint);
136 if (!theView.IsNull()) {
137 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
138 theView->Eye(XEye, YEye, ZEye);
140 theView->At(XAt, YAt, ZAt);
141 gp_Pnt EyePoint(XEye, YEye, ZEye);
142 gp_Pnt AtPoint(XAt, YAt, ZAt);
144 gp_Vec anEyeVec(EyePoint, AtPoint);
145 anEyeVec.Normalize();
147 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
148 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
149 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
151 double aDen = anEyeVec * aNormalVec;
152 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
154 gp_Vec aDeltaVec = anEyeVec * aLVec;
155 aVec = aVec - aDeltaVec;
157 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
158 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
161 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::convertTo2D(FeaturePtr theSketch,
162 const std::shared_ptr<GeomAPI_Pnt>& thePnt)
164 std::shared_ptr<GeomAPI_Pnt2d> aRes;
165 if (theSketch->getKind() != SketchPlugin_Sketch::ID())
167 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
168 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
169 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
170 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
171 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
172 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
173 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
174 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
178 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY,
179 FeaturePtr theSketch)
181 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
183 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
184 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
185 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
186 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
187 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
188 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
189 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
191 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
192 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
194 return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
197 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
199 return ModelAPI_Session::get()->moduleDocument();
202 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
203 const std::string& theAttribute)
207 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
208 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
209 aData->attribute(theAttribute));
211 anAttribute->setValue(theValue);
214 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
220 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
221 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
222 aData->attribute(theAttribute));
224 aValue = anAttribute->value();
231 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
232 const std::string& theKind)
238 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
239 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
240 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
242 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
243 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
244 aFeature = FeaturePtr();
250 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
251 std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
252 std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
256 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
258 std::shared_ptr<ModelAPI_Document> aDoc = document();
259 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
262 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
264 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
265 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
266 aRef1->setAttr(thePoint1);
268 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
269 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
270 aRef2->setAttr(thePoint2);
272 // we need to flush created signal in order to coincidence is processed by solver
273 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
276 /*std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::
277 findAttributePoint(CompositeFeaturePtr theSketch, double theX, double theY,
278 double theTolerance, const QList<FeaturePtr>& theIgnore)
280 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
281 new GeomAPI_Pnt2d(theX, theY));
283 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
284 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
285 FeaturePtr aFeature = theSketch->subFeature(i);
286 if (!theIgnore.contains(aFeature)) {
287 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
289 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt;
290 for (anIt = anAttiributes.cbegin(); anIt != anAttiributes.cend(); ++anIt) {
291 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
292 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
293 double x = aCurPoint->x();
294 double y = aCurPoint->y();
295 if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < theTolerance)) {
301 return std::shared_ptr<GeomDataAPI_Point2D>();
305 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPointInArgumentFeatures(
306 const FeaturePtr& theFeature, const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
308 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
310 // may be feature is not updated yet, execute is not performed and references features
311 // are not created. Case: rectangle macro feature
312 ModuleBase_Tools::flushUpdated(theFeature);
314 std::list<AttributePtr> anAttributes = theFeature->data()->attributes(
315 ModelAPI_AttributeRefList::typeId());
316 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
317 for (; anIt != aLast && !aFeaturePoint.get(); anIt++) {
318 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
319 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
320 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFeaturePoint.get(); i++) {
321 ObjectPtr anObject = aCurSelList->object(i);
322 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
324 aFeaturePoint = PartSet_Tools::findFirstEqualPoint(aFeature, thePoint);
327 return aFeaturePoint;
330 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPoint(
331 const FeaturePtr& theFeature,
332 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
334 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
336 // find the given point in the feature attributes
337 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
338 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
339 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
340 aLast = anAttiributes.end();
341 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
343 for (; anIt != aLast && !aFPoint; anIt++) {
344 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
345 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
346 if (aCurPoint && aCurPoint->isInitialized() &&
347 aValidators->isCase(theFeature, aCurPoint->id()) &&
348 (aCurPoint->pnt()->distance(thePoint) < Precision::Confusion())) {
356 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPointInSketch(
357 const CompositeFeaturePtr& theSketch,
358 const std::shared_ptr<GeomDataAPI_Point2D>& theFeaturePoint,
359 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
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;
371 FeaturePtr aFeatureOfPoint = ModelAPI_Feature::feature(theFeaturePoint->owner());
372 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
373 for (; anIt != aLast; anIt++) {
374 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
375 if (!aFeature.get() || aFeatureOfPoint == aFeature)
377 aFPoint = PartSet_Tools::findFirstEqualPoint(aFeature, thePoint);
384 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
386 std::shared_ptr<GeomAPI_Pln> aPlane;
388 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
389 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
390 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
391 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
393 if (aNormal.get() && aNormal->isInitialized() &&
394 anOrigin.get() && anOrigin->isInitialized())
395 aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNormal->dir()));
400 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
401 CompositeFeaturePtr theSketch)
403 std::shared_ptr<GeomAPI_Pnt> aPoint;
404 if (!theSketch || !thePoint2D)
407 DataPtr aData = theSketch->data();
408 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
409 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
410 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
411 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
412 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
413 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
414 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
416 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
419 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
420 const ObjectPtr& theObject,
421 CompositeFeaturePtr theSketch)
424 if (theShape.ShapeType() == TopAbs_EDGE) {
425 // Check that we already have such external edge
426 std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
427 aInEdge->setImpl(new TopoDS_Shape(theShape));
428 aResult = findExternalEdge(theSketch, aInEdge);
430 if (theShape.ShapeType() == TopAbs_VERTEX) {
431 std::shared_ptr<GeomAPI_Vertex> aInVert = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex());
432 aInVert->setImpl(new TopoDS_Shape(theShape));
433 aResult = findExternalVertex(theSketch, aInVert);
438 ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShape,
439 const ObjectPtr& theObject,
440 CompositeFeaturePtr theSketch,
441 const bool theTemporary)
443 if (theShape.ShapeType() == TopAbs_EDGE) {
444 Standard_Real aStart, aEnd;
445 Handle(V3d_View) aNullView;
446 FeaturePtr aMyFeature;
448 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
449 GeomAdaptor_Curve aAdaptor(aCurve);
450 std::shared_ptr<GeomAPI_Edge> anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
451 anEdge->setImpl(new TopoDS_Shape(theShape));
452 if (aAdaptor.GetType() == GeomAbs_Line) {
454 aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
455 if (!theObject.get()) {
456 // There is no selected result
457 std::shared_ptr<GeomAPI_Pnt> aPnt1 = anEdge->firstPoint();
458 std::shared_ptr<GeomAPI_Pnt> aPnt2 = anEdge->lastPoint();
459 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = convertTo2D(theSketch, aPnt1);
460 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d2 = convertTo2D(theSketch, aPnt2);
462 std::shared_ptr<ModelAPI_Data> aData = aMyFeature->data();
463 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
464 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
465 aData->attribute(SketchPlugin_Line::START_ID()));
466 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
467 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
468 aData->attribute(SketchPlugin_Line::END_ID()));
470 aPoint1->setValue(aPnt2d1);
471 aPoint2->setValue(aPnt2d2);
473 // If this is an axis then its name has to be changed correspondently
474 std::string aSuffix = "";
475 bool aXdir = fabs(aPnt1->x() - aPnt2->x()) > Precision::Confusion();
476 bool aYdir = fabs(aPnt1->y() - aPnt2->y()) > Precision::Confusion();
477 bool aZdir = fabs(aPnt1->z() - aPnt2->z()) > Precision::Confusion();
478 if (aXdir && (!aYdir) && (!aZdir))
480 else if ((!aXdir) && aYdir && (!aZdir))
482 else if ((!aXdir) && (!aYdir) && aZdir)
484 if (aSuffix.length() > 0)
485 aData->setName("Axis_" + aSuffix);
486 aMyFeature->execute();
489 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
490 if (anEdge->isArc()) {
492 aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
493 if (theShape.Orientation() == TopAbs_REVERSED)
494 aMyFeature->boolean(SketchPlugin_Arc::INVERSED_ID())->setValue(true);
498 aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
502 DataPtr aData = aMyFeature->data();
503 AttributeSelectionPtr anAttr =
504 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
505 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
507 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
508 // selection shape has no result owner => the trihedron axis
509 // TODO: make reference to the real axes when
510 // they are implemented in the initialization plugin
512 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()->objectByName(
513 ModelAPI_ResultConstruction::group(), "Origin");
514 if (aPointObj.get()) { // if initialization plugin performed well
515 aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
519 aRes = aMyFeature->firstResult();
521 if (anAttr.get() && aRes.get()) {
522 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
523 anEdge->setImpl(new TopoDS_Shape(theShape));
525 anAttr->setValue(aRes, anEdge);
526 //if (!theTemporary) {
527 aMyFeature->execute();
530 // FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
531 // aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
532 // setObject(aMyFeature->lastResult());
533 // // we need to flush created signal in order to fixed constraint is processed by solver
534 // Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
536 return aMyFeature->lastResult();
540 if (theShape.ShapeType() == TopAbs_VERTEX) {
541 FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
544 DataPtr aData = aMyFeature->data();
545 AttributeSelectionPtr anAttr =
546 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
547 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
549 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
550 // if there is no object,
551 // it means that this is the origin point: search it in the module document
553 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()->objectByName(
554 ModelAPI_ResultConstruction::group(), "Origin");
555 if (aPointObj.get()) { // if initialization plugin performed well
556 aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
559 // reference to itself with name "Origin" (but this may cause the infinitive cycling)
561 // If the point is selected not from Result object
562 std::shared_ptr<GeomAPI_Shape> aShape =
563 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
564 aShape->setImpl(new TopoDS_Shape(theShape));
566 std::shared_ptr<GeomAPI_Vertex> aVertex =
567 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
568 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
570 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = convertTo2D(theSketch, aPnt);
571 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
572 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
573 aData->attribute(SketchPlugin_Point::COORD_ID()));
574 aPoint->setValue(aPnt2d);
575 if ((aPnt->x() < Precision::Confusion()) &&
576 (aPnt->y() < Precision::Confusion()) &&
577 (aPnt->z() < Precision::Confusion()))
578 aData->setName("Origin");
580 aMyFeature->execute();
581 aRes = aMyFeature->firstResult();
583 if (anAttr.get() && aRes.get()) {
584 std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
585 aVert->setImpl(new TopoDS_Shape(theShape));
587 anAttr->setValue(aRes, aVert);
588 //if (theTemporary) {
589 aMyFeature->execute();
592 // FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
593 // aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
594 // setObject(aMyFeature->lastResult());
595 // // we need to flush created signal in order to fixed constraint is processed by solver
596 // Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
598 return aMyFeature->lastResult();
605 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrsPtr>& theSelected,
606 const ModuleBase_ViewerPrsPtr& thePrs)
608 foreach (ModuleBase_ViewerPrsPtr aPrs, theSelected) {
609 if (aPrs->object() == thePrs->object())
615 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch,
616 std::shared_ptr<GeomAPI_Edge> theEdge)
618 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
619 FeaturePtr aFeature = theSketch->subFeature(i);
620 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
621 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
622 // not displayed result of feature projection should not be returned as external edge
623 if (aSketchFea && aSketchFea->canBeDisplayed()) {
624 if (aSketchFea->isExternal()) {
625 std::list<ResultPtr> aResults = aSketchFea->results();
626 std::list<ResultPtr>::const_iterator aIt;
627 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
628 ResultConstructionPtr aRes =
629 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
631 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
633 if (theEdge->isEqual(aShape))
645 ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch,
646 std::shared_ptr<GeomAPI_Vertex> theVert)
648 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
649 FeaturePtr aFeature = theSketch->subFeature(i);
650 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
651 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
653 if (aSketchFea->isExternal()) {
654 std::list<ResultPtr> aResults = aSketchFea->results();
655 std::list<ResultPtr>::const_iterator aIt;
656 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
657 ResultConstructionPtr aRes =
658 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
660 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
662 if (theVert->isEqual(aShape))
674 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrsPtr& thePrs, FeaturePtr theSketch,
675 Handle(V3d_View) theView, double& theX, double& theY)
677 bool aHasVertex = false;
679 const GeomShapePtr& aShape = thePrs->shape();
680 if (aShape.get() && !aShape->isNull() && aShape->shapeType() == GeomAPI_Shape::VERTEX)
682 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
683 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aTDShape);
684 if (!aVertex.IsNull())
686 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
687 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
695 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
696 ModuleBase_IWorkshop* theWorkshop)
699 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
700 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
702 // 2. find visualized vertices of the attribute and if the attribute of the vertex is
703 // the same, return it
704 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
705 // 2.1 get visualized results of the feature
706 const std::list<ResultPtr>& aResList = anAttributeFeature->results();
707 std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
708 for (; anIt != aLast; anIt++) {
709 AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
710 if (aAISObj.get() != NULL) {
711 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
712 // 2.2 find selected owners of a visualizedd object
713 SelectMgr_IndexedMapOfOwner aSelectedOwners;
714 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
715 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
716 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
717 if (!anOwner.IsNull()) {
718 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
719 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
720 const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
721 if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
722 // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
723 // attribute, returns the shape
724 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
725 PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
726 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
727 aBRepShape, aSketchMgr->activeSketch());
728 if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
729 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
730 aShape->setImpl(new TopoDS_Shape(aBRepShape));
742 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(
743 std::shared_ptr<ModelAPI_Feature>& theFeature,
744 const std::string& theAttribute)
746 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
747 theFeature.get(), theAttribute, SketchPlugin_Point::ID(),
748 SketchPlugin_Point::COORD_ID());
749 if (aPointAttr.get() != NULL)
750 return aPointAttr->pnt();
751 return std::shared_ptr<GeomAPI_Pnt2d>();
754 FeaturePtr findFirstCoincidenceByData(const DataPtr& theData,
755 std::shared_ptr<GeomAPI_Pnt2d> thePoint)
757 FeaturePtr aCoincident;
759 const std::set<AttributePtr>& aRefsList = theData->refsToMe();
760 std::set<AttributePtr>::const_iterator aIt;
761 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
762 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
763 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
764 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
765 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
766 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
767 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
768 aCoincident = aConstrFeature;
771 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
772 SketchPlugin_ConstraintCoincidence::ENTITY_B());
773 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
774 aCoincident = aConstrFeature;
783 FeaturePtr PartSet_Tools::findFirstCoincidence(const FeaturePtr& theFeature,
784 std::shared_ptr<GeomAPI_Pnt2d> thePoint)
786 FeaturePtr aCoincident;
787 if (theFeature.get() == NULL)
790 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
791 std::set<AttributePtr>::const_iterator aIt;
792 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
793 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
794 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
795 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
796 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
797 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
798 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
799 aCoincident = aConstrFeature;
802 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
803 SketchPlugin_ConstraintCoincidence::ENTITY_B());
804 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
805 aCoincident = aConstrFeature;
812 if (!aCoincident.get()) {
813 std::list<ResultPtr> aResults = theFeature->results();
814 std::list<ResultPtr>::const_iterator aIt;
815 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
816 ResultPtr aResult = *aIt;
817 aCoincident = findFirstCoincidenceByData(aResult->data(), thePoint);
818 if (aCoincident.get())
825 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
826 QList<FeaturePtr>& theCoincidencies,
829 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
830 if (aOrig.get() == NULL)
833 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
836 ObjectPtr aObj = aPnt->object();
837 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
838 if (aFeature.get()) {
839 if (!theList.contains(aFeature)) {
840 theList.append(aFeature);
841 theCoincidencies.append(theStartCoin);
842 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
843 std::set<AttributePtr>::const_iterator aIt;
844 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
845 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
846 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
847 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
848 if (!theCoincidencies.contains(aConstrFeature)) {
849 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
850 if (aPnt.get() && aOrig->isEqual(aPnt)) {
851 findCoincidences(aConstrFeature, theList, theCoincidencies,
852 SketchPlugin_ConstraintCoincidence::ENTITY_A());
853 findCoincidences(aConstrFeature, theList, theCoincidencies,
854 SketchPlugin_ConstraintCoincidence::ENTITY_B());
862 ResultConstructionPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
864 FeaturePtr aFeature = ModelAPI_Feature::feature(aPnt->object());
865 if (!theList.contains(aFeature))
866 theList.append(aFeature);
867 theCoincidencies.append(theStartCoin);
869 const std::set<AttributePtr>& aRefsList = aResult->data()->refsToMe();
870 std::set<AttributePtr>::const_iterator aIt;
871 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
872 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
873 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
874 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
875 if (!theCoincidencies.contains(aConstrFeature)) {
876 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
877 if (aPnt.get() && aOrig->isEqual(aPnt)) {
878 findCoincidences(aConstrFeature, theList, theCoincidencies,
879 SketchPlugin_ConstraintCoincidence::ENTITY_A());
880 findCoincidences(aConstrFeature, theList, theCoincidencies,
881 SketchPlugin_ConstraintCoincidence::ENTITY_B());
890 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
892 std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
893 SketchPlugin_Constraint::ENTITY_A());
894 if (aPnt.get() == NULL)
895 aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
899 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
900 const TopoDS_Shape theShape,
901 FeaturePtr theSketch)
904 AttributePtr anAttribute;
905 FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
907 if (theShape.ShapeType() == TopAbs_VERTEX) {
908 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
909 if (!aVertex.IsNull()) {
910 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
911 std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
912 new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
914 // find the given point in the feature attributes
915 std::list<AttributePtr> anAttiributes =
916 aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
917 std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
918 aLast = anAttiributes.end();
919 for (; anIt != aLast && !anAttribute; anIt++) {
920 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
921 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
922 if (!aCurPoint->isInitialized())
925 std::shared_ptr<GeomAPI_Pnt> aPnt =
926 convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
927 if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
928 anAttribute = aCurPoint;
938 void PartSet_Tools::sendSubFeaturesEvent(const CompositeFeaturePtr& theComposite,
939 const Events_ID theEventId)
941 if (!theComposite.get())
944 static Events_Loop* aLoop = Events_Loop::loop();
945 for (int i = 0; i < theComposite->numberOfSubs(); i++) {
946 FeaturePtr aSubFeature = theComposite->subFeature(i);
947 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
948 aECreator->sendUpdated(aSubFeature, theEventId);
950 Events_Loop::loop()->flush(theEventId);
953 bool PartSet_Tools::isAuxiliarySketchEntity(const ObjectPtr& theObject)
955 bool isAuxiliaryFeature = false;
957 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
958 std::string anAuxiliaryAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
959 AttributeBooleanPtr anAuxiliaryAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
960 anObjectFeature->data()->attribute(anAuxiliaryAttribute));
961 if (anAuxiliaryAttr.get())
962 isAuxiliaryFeature = anAuxiliaryAttr->value();
965 return isAuxiliaryFeature;