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>
19 #include <Events_Loop.h>
21 #include <SketcherPrs_Tools.h>
23 #include <XGUI_ModuleConnector.h>
24 #include <XGUI_Displayer.h>
25 #include <XGUI_Workshop.h>
26 #include <XGUI_SelectionMgr.h>
27 #include <XGUI_Selection.h>
29 #include <GeomDataAPI_Point.h>
30 #include <GeomDataAPI_Dir.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <GeomAPI_Pln.h>
33 #include <GeomAPI_Pnt2d.h>
34 #include <GeomAPI_Pnt.h>
35 #include <GeomAPI_Edge.h>
36 #include <GeomAPI_Vertex.h>
38 #include <GeomAPI_Dir.h>
39 #include <GeomAPI_XYZ.h>
41 #include <SketchPlugin_Feature.h>
42 #include <SketchPlugin_Sketch.h>
43 #include <SketchPlugin_ConstraintCoincidence.h>
44 #include <SketchPlugin_ConstraintDistance.h>
45 #include <SketchPlugin_ConstraintLength.h>
46 #include <SketchPlugin_ConstraintRadius.h>
47 #include <SketchPlugin_ConstraintRigid.h>
48 #include <SketchPlugin_Constraint.h>
49 #include <SketchPlugin_Circle.h>
50 #include <SketchPlugin_Arc.h>
51 #include <SketchPlugin_Line.h>
52 #include <SketchPlugin_Point.h>
54 #include <ModuleBase_IWorkshop.h>
55 #include <ModuleBase_ViewerPrs.h>
57 #include <V3d_View.hxx>
59 #include <gp_Circ.hxx>
60 #include <ProjLib.hxx>
62 #include <Geom_Line.hxx>
63 #include <GeomAPI_ProjectPointOnCurve.hxx>
64 #include <BRep_Tool.hxx>
66 #include <TopoDS_Edge.hxx>
67 #include <TopoDS_Vertex.hxx>
68 #include <AIS_InteractiveObject.hxx>
69 #include <StdSelect_BRepOwner.hxx>
70 #include <SelectMgr_IndexedMapOfOwner.hxx>
76 const double PRECISION_TOLERANCE = 0.000001;
78 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
83 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
84 theView->Eye(XEye, YEye, ZEye);
86 theView->At(XAt, YAt, ZAt);
87 gp_Pnt EyePoint(XEye, YEye, ZEye);
88 gp_Pnt AtPoint(XAt, YAt, ZAt);
90 gp_Vec EyeVector(EyePoint, AtPoint);
91 gp_Dir EyeDir(EyeVector);
93 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
94 Standard_Real X, Y, Z;
95 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
96 gp_Pnt ConvertedPoint(X, Y, Z);
98 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
99 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
104 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
105 Handle(V3d_View) theView,
106 double& theX, double& theY)
111 AttributeDoublePtr anAttr;
112 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
114 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
115 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
117 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
118 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
119 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
120 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
121 std::shared_ptr<GeomAPI_XYZ> anY = aNorm->xyz()->cross(aX->xyz());
123 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
124 gp_Vec aVec(anOriginPnt, thePoint);
126 if (!theView.IsNull()) {
127 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
128 theView->Eye(XEye, YEye, ZEye);
130 theView->At(XAt, YAt, ZAt);
131 gp_Pnt EyePoint(XEye, YEye, ZEye);
132 gp_Pnt AtPoint(XAt, YAt, ZAt);
134 gp_Vec anEyeVec(EyePoint, AtPoint);
135 anEyeVec.Normalize();
137 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
138 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
139 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
141 double aDen = anEyeVec * aNormalVec;
142 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
144 gp_Vec aDeltaVec = anEyeVec * aLVec;
145 aVec = aVec - aDeltaVec;
147 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
148 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
151 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::convertTo2D(FeaturePtr theSketch,
152 const std::shared_ptr<GeomAPI_Pnt>& thePnt)
154 std::shared_ptr<GeomAPI_Pnt2d> aRes;
155 if (theSketch->getKind() != SketchPlugin_Sketch::ID())
157 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
158 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
159 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
160 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
161 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
162 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
163 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
164 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
168 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch)
170 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
172 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
173 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
174 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
175 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
176 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
177 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
178 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
180 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
181 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
183 return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
186 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
188 return ModelAPI_Session::get()->moduleDocument();
191 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
192 const std::string& theAttribute)
196 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
197 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
198 aData->attribute(theAttribute));
200 anAttribute->setValue(theValue);
203 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
209 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
210 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
211 aData->attribute(theAttribute));
213 aValue = anAttribute->value();
220 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
221 const std::string& theKind)
227 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
228 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
229 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
231 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
232 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
233 aFeature = FeaturePtr();
239 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
240 std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
241 std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
245 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
247 std::shared_ptr<ModelAPI_Document> aDoc = document();
248 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
251 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
253 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
254 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
255 aRef1->setAttr(thePoint1);
257 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
258 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
259 aRef2->setAttr(thePoint2);
261 // we need to flush created signal in order to coincidence is processed by solver
262 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
265 /*std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::
266 findAttributePoint(CompositeFeaturePtr theSketch, double theX, double theY,
267 double theTolerance, const QList<FeaturePtr>& theIgnore)
269 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
270 new GeomAPI_Pnt2d(theX, theY));
272 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
273 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
274 FeaturePtr aFeature = theSketch->subFeature(i);
275 if (!theIgnore.contains(aFeature)) {
276 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
278 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt;
279 for (anIt = anAttiributes.cbegin(); anIt != anAttiributes.cend(); ++anIt) {
280 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
281 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
282 double x = aCurPoint->x();
283 double y = aCurPoint->y();
284 if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < theTolerance)) {
290 return std::shared_ptr<GeomDataAPI_Point2D>();
294 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPointInArgumentFeatures(
295 const FeaturePtr& theFeature, const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
297 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
299 // may be feature is not updated yet, execute is not performed and references features
300 // are not created. Case: rectangle macro feature
301 ModuleBase_ModelWidget::updateObject(theFeature);
303 std::list<AttributePtr> anAttributes = theFeature->data()->attributes(
304 ModelAPI_AttributeRefList::typeId());
305 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
306 for (; anIt != aLast && !aFeaturePoint.get(); anIt++) {
307 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
308 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
309 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFeaturePoint.get(); i++) {
310 ObjectPtr anObject = aCurSelList->object(i);
311 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
313 aFeaturePoint = PartSet_Tools::findFirstEqualPoint(aFeature, thePoint);
316 return aFeaturePoint;
319 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPoint(const FeaturePtr& theFeature,
320 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
322 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
324 // find the given point in the feature attributes
325 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
326 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
327 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
328 aLast = anAttiributes.end();
329 for (; anIt != aLast && !aFPoint; anIt++) {
330 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
331 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
332 if (aCurPoint && (aCurPoint->pnt()->distance(thePoint) < Precision::Confusion())) {
340 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
341 const std::string& theAttribute, double theClickedX,
344 if (!theFeature.get())
347 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
348 new GeomAPI_Pnt2d(theClickedX, theClickedY));
350 // find a feature point by the selection mode
351 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
352 if (theFeature->isMacro()) {
353 // the macro feature will be removed after the operation is stopped, so we need to build
354 // coicidence to possible sub-features
355 aFeaturePoint = PartSet_Tools::findFirstEqualPointInArgumentFeatures(theFeature, aClickedPoint);
358 aFeaturePoint = std::dynamic_pointer_cast<
359 GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
364 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
365 // the constraint is created between the feature point and the found sketch point
366 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
367 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
368 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
370 std::list<ObjectPtr> aFeatures = aRefList->list();
371 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
372 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
373 for (; anIt != aLast; anIt++) {
374 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
375 if (!aFeature.get() || theFeature == aFeature)
377 std::shared_ptr<GeomDataAPI_Point2D> aFPoint = PartSet_Tools::findFirstEqualPoint(aFeature,
380 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
384 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
386 std::shared_ptr<GeomAPI_Pln> aPlane;
388 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
390 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
391 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
392 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
393 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
394 if (aNormal && anOrigin) {
395 double adX = aNormal->x();
396 double adY = aNormal->y();
397 double adZ = aNormal->z();
399 if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
400 double aX = anOrigin->x();
401 double aY = anOrigin->y();
402 double aZ = anOrigin->z();
403 gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
404 double aA, aB, aC, aD;
405 aPln.Coefficients(aA, aB, aC, aD);
406 aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
413 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
414 CompositeFeaturePtr theSketch)
416 std::shared_ptr<GeomAPI_Pnt> aPoint;
417 if (!theSketch || !thePoint2D)
420 DataPtr aData = theSketch->data();
421 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
422 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
423 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
424 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
425 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
426 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
427 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
429 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
432 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
433 const ObjectPtr& theObject,
434 CompositeFeaturePtr theSketch)
437 if (theShape.ShapeType() == TopAbs_EDGE) {
438 // Check that we already have such external edge
439 std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
440 aInEdge->setImpl(new TopoDS_Shape(theShape));
441 aResult = findExternalEdge(theSketch, aInEdge);
443 if (theShape.ShapeType() == TopAbs_VERTEX) {
444 std::shared_ptr<GeomAPI_Vertex> aInVert = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex());
445 aInVert->setImpl(new TopoDS_Shape(theShape));
446 aResult = findExternalVertex(theSketch, aInVert);
451 ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShape,
452 const ObjectPtr& theObject,
453 CompositeFeaturePtr theSketch,
454 const bool theTemporary)
456 if (theShape.ShapeType() == TopAbs_EDGE) {
457 Standard_Real aStart, aEnd;
458 Handle(V3d_View) aNullView;
459 FeaturePtr aMyFeature;
461 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
462 GeomAdaptor_Curve aAdaptor(aCurve);
463 std::shared_ptr<GeomAPI_Edge> anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
464 anEdge->setImpl(new TopoDS_Shape(theShape));
465 if (aAdaptor.GetType() == GeomAbs_Line) {
467 aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
468 if (!theObject.get()) {
469 // There is no selected result
470 std::shared_ptr<GeomAPI_Pnt> aPnt1 = anEdge->firstPoint();
471 std::shared_ptr<GeomAPI_Pnt> aPnt2 = anEdge->lastPoint();
472 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = convertTo2D(theSketch, aPnt1);
473 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d2 = convertTo2D(theSketch, aPnt2);
475 std::shared_ptr<ModelAPI_Data> aData = aMyFeature->data();
476 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
477 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
478 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
479 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
481 aPoint1->setValue(aPnt2d1);
482 aPoint2->setValue(aPnt2d2);
484 // If this is an axis then its name has to be changed correspondently
485 std::string aSuffix = "";
486 bool aXdir = fabs(aPnt1->x() - aPnt2->x()) > Precision::Confusion();
487 bool aYdir = fabs(aPnt1->y() - aPnt2->y()) > Precision::Confusion();
488 bool aZdir = fabs(aPnt1->z() - aPnt2->z()) > Precision::Confusion();
489 if (aXdir && (!aYdir) && (!aZdir))
491 else if ((!aXdir) && aYdir && (!aZdir))
493 else if ((!aXdir) && (!aYdir) && aZdir)
495 if (aSuffix.length() > 0)
496 aData->setName("Axis_" + aSuffix);
497 aMyFeature->execute();
500 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
501 if (anEdge->isArc()) {
503 aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
507 aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
511 DataPtr aData = aMyFeature->data();
512 AttributeSelectionPtr anAttr =
513 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
514 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
516 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
518 aRes = aMyFeature->firstResult();
520 if (anAttr.get() && aRes.get()) {
521 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
522 anEdge->setImpl(new TopoDS_Shape(theShape));
524 anAttr->setValue(aRes, anEdge);
525 //if (!theTemporary) {
526 aMyFeature->execute();
529 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
530 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
531 setObject(aMyFeature->lastResult());
533 return aMyFeature->lastResult();
537 if (theShape.ShapeType() == TopAbs_VERTEX) {
538 FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
541 DataPtr aData = aMyFeature->data();
542 AttributeSelectionPtr anAttr =
543 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
544 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
546 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
548 // If the point is selected not from Result object
549 std::shared_ptr<GeomAPI_Shape> aShape =
550 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
551 aShape->setImpl(new TopoDS_Shape(theShape));
553 std::shared_ptr<GeomAPI_Vertex> aVertex =
554 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
555 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
557 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = convertTo2D(theSketch, aPnt);
558 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
559 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Point::COORD_ID()));
560 aPoint->setValue(aPnt2d);
561 if ((aPnt->x() < Precision::Confusion()) &&
562 (aPnt->y() < Precision::Confusion()) &&
563 (aPnt->z() < Precision::Confusion()))
564 aData->setName("Origin");
566 aMyFeature->execute();
567 aRes = aMyFeature->firstResult();
569 if (anAttr.get() && aRes.get()) {
570 std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
571 aVert->setImpl(new TopoDS_Shape(theShape));
573 anAttr->setValue(aRes, aVert);
574 //if (theTemporary) {
575 aMyFeature->execute();
578 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
579 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
580 setObject(aMyFeature->lastResult());
582 return aMyFeature->lastResult();
589 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
590 const ModuleBase_ViewerPrs& thePrs)
592 foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
593 if (aPrs.object() == thePrs.object())
599 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge)
601 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
602 FeaturePtr aFeature = theSketch->subFeature(i);
603 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
604 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
606 if (aSketchFea->isExternal()) {
607 std::list<ResultPtr> aResults = aSketchFea->results();
608 std::list<ResultPtr>::const_iterator aIt;
609 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
610 ResultConstructionPtr aRes =
611 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
613 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
615 if (theEdge->isEqual(aShape))
627 ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
629 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
630 FeaturePtr aFeature = theSketch->subFeature(i);
631 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
632 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
634 if (aSketchFea->isExternal()) {
635 std::list<ResultPtr> aResults = aSketchFea->results();
636 std::list<ResultPtr>::const_iterator aIt;
637 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
638 ResultConstructionPtr aRes =
639 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
641 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
643 if (theVert->isEqual(aShape))
655 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch,
656 Handle_V3d_View theView, double& theX, double& theY)
658 bool aHasVertex = false;
660 const GeomShapePtr& aShape = thePrs.shape();
661 if (aShape.get() && !aShape->isNull() && aShape->shapeType() == GeomAPI_Shape::VERTEX)
663 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
664 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aTDShape);
665 if (!aVertex.IsNull())
667 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
668 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
676 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
677 ModuleBase_IWorkshop* theWorkshop)
680 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
681 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
683 // 2. find visualized vertices of the attribute and if the attribute of the vertex is
684 // the same, return it
685 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
686 // 2.1 get visualized results of the feature
687 const std::list<ResultPtr>& aResList = anAttributeFeature->results();
688 std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
689 for (; anIt != aLast; anIt++) {
690 AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
691 if (aAISObj.get() != NULL) {
692 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
693 // 2.2 find selected owners of a visualizedd object
694 SelectMgr_IndexedMapOfOwner aSelectedOwners;
695 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
696 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
697 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
698 if (!anOwner.IsNull()) {
699 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
700 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
701 const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
702 if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
703 // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
704 // attribute, returns the shape
705 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
706 PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
707 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
708 aBRepShape, aSketchMgr->activeSketch());
709 if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
710 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
711 aShape->setImpl(new TopoDS_Shape(aBRepShape));
723 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
724 const std::string& theAttribute)
726 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
728 if (!theFeature->data())
729 return std::shared_ptr<GeomAPI_Pnt2d>();
732 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
733 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
735 return std::shared_ptr<GeomAPI_Pnt2d>();
737 aFeature = ModelAPI_Feature::feature(anAttr->object());
739 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
740 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
741 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
743 else if (anAttr->attr()) {
744 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
746 if (aPointAttr.get() != NULL)
747 return aPointAttr->pnt();
748 return std::shared_ptr<GeomAPI_Pnt2d>();
751 FeaturePtr findFirstCoincidenceByData(const DataPtr& theData, std::shared_ptr<GeomAPI_Pnt2d> thePoint)
753 FeaturePtr aCoincident;
755 const std::set<AttributePtr>& aRefsList = theData->refsToMe();
756 std::set<AttributePtr>::const_iterator aIt;
757 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
758 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
759 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
760 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
761 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
762 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
763 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
764 aCoincident = aConstrFeature;
767 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
768 SketchPlugin_ConstraintCoincidence::ENTITY_B());
769 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
770 aCoincident = aConstrFeature;
779 FeaturePtr PartSet_Tools::findFirstCoincidence(const FeaturePtr& theFeature,
780 std::shared_ptr<GeomAPI_Pnt2d> thePoint)
782 FeaturePtr aCoincident;
783 if (theFeature.get() == NULL)
786 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
787 std::set<AttributePtr>::const_iterator aIt;
788 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
789 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
790 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
791 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
792 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
793 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
794 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
795 aCoincident = aConstrFeature;
798 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
799 SketchPlugin_ConstraintCoincidence::ENTITY_B());
800 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
801 aCoincident = aConstrFeature;
808 if (!aCoincident.get()) {
809 std::list<ResultPtr> aResults = theFeature->results();
810 std::list<ResultPtr>::const_iterator aIt;
811 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
812 ResultPtr aResult = *aIt;
813 aCoincident = findFirstCoincidenceByData(aResult->data(), thePoint);
814 if (aCoincident.get())
821 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
822 QList<FeaturePtr>& theCoincidencies,
825 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
826 if (aOrig.get() == NULL)
829 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
832 ObjectPtr aObj = aPnt->object();
833 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
834 if (aFeature.get()) {
835 if (!theList.contains(aFeature)) {
836 theList.append(aFeature);
837 theCoincidencies.append(theStartCoin);
838 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
839 std::set<AttributePtr>::const_iterator aIt;
840 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
841 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
842 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
843 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
844 if (!theCoincidencies.contains(aConstrFeature)) {
845 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
846 if (aPnt.get() && aOrig->isEqual(aPnt)) {
847 findCoincidences(aConstrFeature, theList, theCoincidencies,
848 SketchPlugin_ConstraintCoincidence::ENTITY_A());
849 findCoincidences(aConstrFeature, theList, theCoincidencies,
850 SketchPlugin_ConstraintCoincidence::ENTITY_B());
858 ResultConstructionPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
860 FeaturePtr aFeature = ModelAPI_Feature::feature(aPnt->object());
861 if (!theList.contains(aFeature))
862 theList.append(aFeature);
863 theCoincidencies.append(theStartCoin);
865 const std::set<AttributePtr>& aRefsList = aResult->data()->refsToMe();
866 std::set<AttributePtr>::const_iterator aIt;
867 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
868 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
869 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
870 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
871 if (!theCoincidencies.contains(aConstrFeature)) {
872 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
873 if (aPnt.get() && aOrig->isEqual(aPnt)) {
874 findCoincidences(aConstrFeature, theList, theCoincidencies,
875 SketchPlugin_ConstraintCoincidence::ENTITY_A());
876 findCoincidences(aConstrFeature, theList, theCoincidencies,
877 SketchPlugin_ConstraintCoincidence::ENTITY_B());
886 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
888 std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
889 SketchPlugin_Constraint::ENTITY_A());
890 if (aPnt.get() == NULL)
891 aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
895 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
896 const TopoDS_Shape theShape,
897 FeaturePtr theSketch)
900 AttributePtr anAttribute;
901 FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
903 if (theShape.ShapeType() == TopAbs_VERTEX) {
904 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
905 if (!aVertex.IsNull()) {
906 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
907 std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
908 new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
910 // find the given point in the feature attributes
911 std::list<AttributePtr> anAttiributes =
912 aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
913 std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
914 aLast = anAttiributes.end();
915 for (; anIt != aLast && !anAttribute; anIt++) {
916 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
917 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
919 std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
920 if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
921 anAttribute = aCurPoint;
931 void PartSet_Tools::sendSubFeaturesEvent(const CompositeFeaturePtr& theComposite,
932 const Events_ID theEventId)
934 if (!theComposite.get())
937 static Events_Loop* aLoop = Events_Loop::loop();
938 for (int i = 0; i < theComposite->numberOfSubs(); i++) {
939 FeaturePtr aSubFeature = theComposite->subFeature(i);
940 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
941 aECreator->sendUpdated(aSubFeature, theEventId);
943 Events_Loop::loop()->flush(theEventId);
946 bool PartSet_Tools::isAuxiliarySketchEntity(const ObjectPtr& theObject)
948 bool isAuxiliaryFeature = false;
950 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
951 std::string anAuxiliaryAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
952 AttributeBooleanPtr anAuxiliaryAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
953 anObjectFeature->data()->attribute(anAuxiliaryAttribute));
954 if (anAuxiliaryAttr.get())
955 isAuxiliaryFeature = anAuxiliaryAttr->value();
958 return isAuxiliaryFeature;