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;
77 const int AIS_DEFAULT_WIDTH = 2;
79 int PartSet_Tools::getAISDefaultWidth()
81 return AIS_DEFAULT_WIDTH;
84 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
89 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
90 theView->Eye(XEye, YEye, ZEye);
92 theView->At(XAt, YAt, ZAt);
93 gp_Pnt EyePoint(XEye, YEye, ZEye);
94 gp_Pnt AtPoint(XAt, YAt, ZAt);
96 gp_Vec EyeVector(EyePoint, AtPoint);
97 gp_Dir EyeDir(EyeVector);
99 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
100 Standard_Real X, Y, Z;
101 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
102 gp_Pnt ConvertedPoint(X, Y, Z);
104 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
105 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
110 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
111 Handle(V3d_View) theView,
112 double& theX, double& theY)
117 AttributeDoublePtr anAttr;
118 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
120 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
121 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
123 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
124 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
125 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
126 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
127 std::shared_ptr<GeomAPI_XYZ> anY = aNorm->xyz()->cross(aX->xyz());
129 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
130 gp_Vec aVec(anOriginPnt, thePoint);
132 if (!theView.IsNull()) {
133 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
134 theView->Eye(XEye, YEye, ZEye);
136 theView->At(XAt, YAt, ZAt);
137 gp_Pnt EyePoint(XEye, YEye, ZEye);
138 gp_Pnt AtPoint(XAt, YAt, ZAt);
140 gp_Vec anEyeVec(EyePoint, AtPoint);
141 anEyeVec.Normalize();
143 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
144 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
145 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
147 double aDen = anEyeVec * aNormalVec;
148 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
150 gp_Vec aDeltaVec = anEyeVec * aLVec;
151 aVec = aVec - aDeltaVec;
153 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
154 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
157 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::convertTo2D(FeaturePtr theSketch,
158 const std::shared_ptr<GeomAPI_Pnt>& thePnt)
160 std::shared_ptr<GeomAPI_Pnt2d> aRes;
161 if (theSketch->getKind() != SketchPlugin_Sketch::ID())
163 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
164 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
165 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
166 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
167 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
168 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
169 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
170 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
174 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch)
176 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
178 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
179 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
180 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
181 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
182 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
183 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
184 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
186 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
187 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
189 return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
192 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
194 return ModelAPI_Session::get()->moduleDocument();
197 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
198 const std::string& theAttribute)
202 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
203 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
204 aData->attribute(theAttribute));
206 anAttribute->setValue(theValue);
209 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
215 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
216 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
217 aData->attribute(theAttribute));
219 aValue = anAttribute->value();
226 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
227 const std::string& theKind)
233 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
234 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
235 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
237 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
238 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
239 aFeature = FeaturePtr();
245 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
246 std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
247 std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
251 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
253 std::shared_ptr<ModelAPI_Document> aDoc = document();
254 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
257 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
259 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
260 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
261 aRef1->setAttr(thePoint1);
263 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
264 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
265 aRef2->setAttr(thePoint2);
267 // we need to flush created signal in order to coincidence is processed by solver
268 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
271 /*std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::
272 findAttributePoint(CompositeFeaturePtr theSketch, double theX, double theY,
273 double theTolerance, const QList<FeaturePtr>& theIgnore)
275 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
276 new GeomAPI_Pnt2d(theX, theY));
278 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
279 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
280 FeaturePtr aFeature = theSketch->subFeature(i);
281 if (!theIgnore.contains(aFeature)) {
282 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
284 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt;
285 for (anIt = anAttiributes.cbegin(); anIt != anAttiributes.cend(); ++anIt) {
286 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
287 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
288 double x = aCurPoint->x();
289 double y = aCurPoint->y();
290 if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < theTolerance)) {
296 return std::shared_ptr<GeomDataAPI_Point2D>();
300 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPointInArgumentFeatures(
301 const FeaturePtr& theFeature, const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
303 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
305 // may be feature is not updated yet, execute is not performed and references features
306 // are not created. Case: rectangle macro feature
307 ModuleBase_ModelWidget::updateObject(theFeature);
309 std::list<AttributePtr> anAttributes = theFeature->data()->attributes(
310 ModelAPI_AttributeRefList::typeId());
311 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
312 for (; anIt != aLast && !aFeaturePoint.get(); anIt++) {
313 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
314 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
315 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFeaturePoint.get(); i++) {
316 ObjectPtr anObject = aCurSelList->object(i);
317 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
319 aFeaturePoint = PartSet_Tools::findFirstEqualPoint(aFeature, thePoint);
322 return aFeaturePoint;
325 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPoint(const FeaturePtr& theFeature,
326 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
328 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
330 // find the given point in the feature attributes
331 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
332 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
333 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
334 aLast = anAttiributes.end();
335 for (; anIt != aLast && !aFPoint; anIt++) {
336 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
337 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
338 if (aCurPoint && (aCurPoint->pnt()->distance(thePoint) < Precision::Confusion())) {
346 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
347 const std::string& theAttribute, double theClickedX,
350 if (!theFeature.get())
353 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
354 new GeomAPI_Pnt2d(theClickedX, theClickedY));
356 // find a feature point by the selection mode
357 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
358 if (theFeature->isMacro()) {
359 // the macro feature will be removed after the operation is stopped, so we need to build
360 // coicidence to possible sub-features
361 aFeaturePoint = PartSet_Tools::findFirstEqualPointInArgumentFeatures(theFeature, aClickedPoint);
364 aFeaturePoint = std::dynamic_pointer_cast<
365 GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
370 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
371 // the constraint is created between the feature point and the found sketch point
372 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
373 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
374 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
376 std::list<ObjectPtr> aFeatures = aRefList->list();
377 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
378 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
379 for (; anIt != aLast; anIt++) {
380 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
381 if (!aFeature.get() || theFeature == aFeature)
383 std::shared_ptr<GeomDataAPI_Point2D> aFPoint = PartSet_Tools::findFirstEqualPoint(aFeature,
386 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
390 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
392 std::shared_ptr<GeomAPI_Pln> aPlane;
394 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
396 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
397 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
398 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
399 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
400 if (aNormal && anOrigin) {
401 double adX = aNormal->x();
402 double adY = aNormal->y();
403 double adZ = aNormal->z();
405 if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
406 double aX = anOrigin->x();
407 double aY = anOrigin->y();
408 double aZ = anOrigin->z();
409 gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
410 double aA, aB, aC, aD;
411 aPln.Coefficients(aA, aB, aC, aD);
412 aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
419 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
420 CompositeFeaturePtr theSketch)
422 std::shared_ptr<GeomAPI_Pnt> aPoint;
423 if (!theSketch || !thePoint2D)
426 DataPtr aData = theSketch->data();
427 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
428 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
429 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
430 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
431 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
432 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
433 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
435 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
438 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
439 const ObjectPtr& theObject,
440 CompositeFeaturePtr theSketch)
443 if (theShape.ShapeType() == TopAbs_EDGE) {
444 // Check that we already have such external edge
445 std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
446 aInEdge->setImpl(new TopoDS_Shape(theShape));
447 aResult = findExternalEdge(theSketch, aInEdge);
449 if (theShape.ShapeType() == TopAbs_VERTEX) {
450 std::shared_ptr<GeomAPI_Vertex> aInVert = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex());
451 aInVert->setImpl(new TopoDS_Shape(theShape));
452 aResult = findExternalVertex(theSketch, aInVert);
457 ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShape,
458 const ObjectPtr& theObject,
459 CompositeFeaturePtr theSketch,
460 const bool theTemporary)
462 if (theShape.ShapeType() == TopAbs_EDGE) {
463 Standard_Real aStart, aEnd;
464 Handle(V3d_View) aNullView;
465 FeaturePtr aMyFeature;
467 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
468 GeomAdaptor_Curve aAdaptor(aCurve);
469 std::shared_ptr<GeomAPI_Edge> anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
470 anEdge->setImpl(new TopoDS_Shape(theShape));
471 if (aAdaptor.GetType() == GeomAbs_Line) {
473 aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
474 if (!theObject.get()) {
475 // There is no selected result
476 std::shared_ptr<GeomAPI_Pnt> aPnt1 = anEdge->firstPoint();
477 std::shared_ptr<GeomAPI_Pnt> aPnt2 = anEdge->lastPoint();
478 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = convertTo2D(theSketch, aPnt1);
479 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d2 = convertTo2D(theSketch, aPnt2);
481 std::shared_ptr<ModelAPI_Data> aData = aMyFeature->data();
482 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
483 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
484 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
485 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
487 aPoint1->setValue(aPnt2d1);
488 aPoint2->setValue(aPnt2d2);
490 // If this is an axis then its name has to be changed correspondently
491 std::string aSuffix = "";
492 bool aXdir = fabs(aPnt1->x() - aPnt2->x()) > Precision::Confusion();
493 bool aYdir = fabs(aPnt1->y() - aPnt2->y()) > Precision::Confusion();
494 bool aZdir = fabs(aPnt1->z() - aPnt2->z()) > Precision::Confusion();
495 if (aXdir && (!aYdir) && (!aZdir))
497 else if ((!aXdir) && aYdir && (!aZdir))
499 else if ((!aXdir) && (!aYdir) && aZdir)
501 if (aSuffix.length() > 0)
502 aData->setName("Axis_" + aSuffix);
503 aMyFeature->execute();
506 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
507 if (anEdge->isArc()) {
509 aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
513 aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
517 DataPtr aData = aMyFeature->data();
518 AttributeSelectionPtr anAttr =
519 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
520 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
522 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
524 aRes = aMyFeature->firstResult();
526 if (anAttr.get() && aRes.get()) {
527 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
528 anEdge->setImpl(new TopoDS_Shape(theShape));
530 anAttr->setValue(aRes, anEdge);
531 //if (!theTemporary) {
532 aMyFeature->execute();
535 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
536 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
537 setObject(aMyFeature->lastResult());
539 return aMyFeature->lastResult();
543 if (theShape.ShapeType() == TopAbs_VERTEX) {
544 FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
547 DataPtr aData = aMyFeature->data();
548 AttributeSelectionPtr anAttr =
549 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
550 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
552 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
554 // If the point is selected not from Result object
555 std::shared_ptr<GeomAPI_Shape> aShape =
556 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
557 aShape->setImpl(new TopoDS_Shape(theShape));
559 std::shared_ptr<GeomAPI_Vertex> aVertex =
560 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
561 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
563 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = convertTo2D(theSketch, aPnt);
564 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
565 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Point::COORD_ID()));
566 aPoint->setValue(aPnt2d);
567 if ((aPnt->x() < Precision::Confusion()) &&
568 (aPnt->y() < Precision::Confusion()) &&
569 (aPnt->z() < Precision::Confusion()))
570 aData->setName("Origin");
572 aMyFeature->execute();
573 aRes = aMyFeature->firstResult();
575 if (anAttr.get() && aRes.get()) {
576 std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
577 aVert->setImpl(new TopoDS_Shape(theShape));
579 anAttr->setValue(aRes, aVert);
580 //if (theTemporary) {
581 aMyFeature->execute();
584 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
585 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
586 setObject(aMyFeature->lastResult());
588 return aMyFeature->lastResult();
595 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
596 const ModuleBase_ViewerPrs& thePrs)
598 foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
599 if (aPrs.object() == thePrs.object())
605 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge)
607 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
608 FeaturePtr aFeature = theSketch->subFeature(i);
609 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
610 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
612 if (aSketchFea->isExternal()) {
613 std::list<ResultPtr> aResults = aSketchFea->results();
614 std::list<ResultPtr>::const_iterator aIt;
615 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
616 ResultConstructionPtr aRes =
617 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
619 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
621 if (theEdge->isEqual(aShape))
633 ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
635 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
636 FeaturePtr aFeature = theSketch->subFeature(i);
637 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
638 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
640 if (aSketchFea->isExternal()) {
641 std::list<ResultPtr> aResults = aSketchFea->results();
642 std::list<ResultPtr>::const_iterator aIt;
643 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
644 ResultConstructionPtr aRes =
645 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
647 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
649 if (theVert->isEqual(aShape))
661 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch,
662 Handle_V3d_View theView, double& theX, double& theY)
664 bool aHasVertex = false;
666 const GeomShapePtr& aShape = thePrs.shape();
667 if (aShape.get() && !aShape->isNull() && aShape->shapeType() == GeomAPI_Shape::VERTEX)
669 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
670 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aTDShape);
671 if (!aVertex.IsNull())
673 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
674 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
682 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
683 ModuleBase_IWorkshop* theWorkshop)
686 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
687 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
689 // 2. find visualized vertices of the attribute and if the attribute of the vertex is
690 // the same, return it
691 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
692 // 2.1 get visualized results of the feature
693 const std::list<ResultPtr>& aResList = anAttributeFeature->results();
694 std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
695 for (; anIt != aLast; anIt++) {
696 AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
697 if (aAISObj.get() != NULL) {
698 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
699 // 2.2 find selected owners of a visualizedd object
700 SelectMgr_IndexedMapOfOwner aSelectedOwners;
701 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
702 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
703 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
704 if (!anOwner.IsNull()) {
705 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
706 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
707 const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
708 if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
709 // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
710 // attribute, returns the shape
711 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
712 PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
713 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
714 aBRepShape, aSketchMgr->activeSketch());
715 if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
716 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
717 aShape->setImpl(new TopoDS_Shape(aBRepShape));
729 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
730 const std::string& theAttribute)
732 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
734 if (!theFeature->data())
735 return std::shared_ptr<GeomAPI_Pnt2d>();
738 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
739 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
741 return std::shared_ptr<GeomAPI_Pnt2d>();
743 aFeature = ModelAPI_Feature::feature(anAttr->object());
745 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
746 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
747 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
749 else if (anAttr->attr()) {
750 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
752 if (aPointAttr.get() != NULL)
753 return aPointAttr->pnt();
754 return std::shared_ptr<GeomAPI_Pnt2d>();
757 FeaturePtr findFirstCoincidenceByData(const DataPtr& theData, std::shared_ptr<GeomAPI_Pnt2d> thePoint)
759 FeaturePtr aCoincident;
761 const std::set<AttributePtr>& aRefsList = theData->refsToMe();
762 std::set<AttributePtr>::const_iterator aIt;
763 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
764 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
765 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
766 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
767 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
768 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
769 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
770 aCoincident = aConstrFeature;
773 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
774 SketchPlugin_ConstraintCoincidence::ENTITY_B());
775 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
776 aCoincident = aConstrFeature;
785 FeaturePtr PartSet_Tools::findFirstCoincidence(const FeaturePtr& theFeature,
786 std::shared_ptr<GeomAPI_Pnt2d> thePoint)
788 FeaturePtr aCoincident;
789 if (theFeature.get() == NULL)
792 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
793 std::set<AttributePtr>::const_iterator aIt;
794 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
795 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
796 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
797 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
798 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
799 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
800 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
801 aCoincident = aConstrFeature;
804 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
805 SketchPlugin_ConstraintCoincidence::ENTITY_B());
806 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
807 aCoincident = aConstrFeature;
814 if (!aCoincident.get()) {
815 std::list<ResultPtr> aResults = theFeature->results();
816 std::list<ResultPtr>::const_iterator aIt;
817 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
818 ResultPtr aResult = *aIt;
819 aCoincident = findFirstCoincidenceByData(aResult->data(), thePoint);
820 if (aCoincident.get())
827 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
828 QList<FeaturePtr>& theCoincidencies,
831 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
832 if (aOrig.get() == NULL)
835 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
838 ObjectPtr aObj = aPnt->object();
839 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
840 if (aFeature.get()) {
841 if (!theList.contains(aFeature)) {
842 theList.append(aFeature);
843 theCoincidencies.append(theStartCoin);
844 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
845 std::set<AttributePtr>::const_iterator aIt;
846 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
847 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
848 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
849 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
850 if (!theCoincidencies.contains(aConstrFeature)) {
851 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
852 if (aPnt.get() && aOrig->isEqual(aPnt)) {
853 findCoincidences(aConstrFeature, theList, theCoincidencies,
854 SketchPlugin_ConstraintCoincidence::ENTITY_A());
855 findCoincidences(aConstrFeature, theList, theCoincidencies,
856 SketchPlugin_ConstraintCoincidence::ENTITY_B());
864 ResultConstructionPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
866 FeaturePtr aFeature = ModelAPI_Feature::feature(aPnt->object());
867 if (!theList.contains(aFeature))
868 theList.append(aFeature);
869 theCoincidencies.append(theStartCoin);
871 const std::set<AttributePtr>& aRefsList = aResult->data()->refsToMe();
872 std::set<AttributePtr>::const_iterator aIt;
873 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
874 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
875 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
876 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
877 if (!theCoincidencies.contains(aConstrFeature)) {
878 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
879 if (aPnt.get() && aOrig->isEqual(aPnt)) {
880 findCoincidences(aConstrFeature, theList, theCoincidencies,
881 SketchPlugin_ConstraintCoincidence::ENTITY_A());
882 findCoincidences(aConstrFeature, theList, theCoincidencies,
883 SketchPlugin_ConstraintCoincidence::ENTITY_B());
892 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
894 std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
895 SketchPlugin_Constraint::ENTITY_A());
896 if (aPnt.get() == NULL)
897 aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
901 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
902 const TopoDS_Shape theShape,
903 FeaturePtr theSketch)
906 AttributePtr anAttribute;
907 FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
909 if (theShape.ShapeType() == TopAbs_VERTEX) {
910 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
911 if (!aVertex.IsNull()) {
912 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
913 std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
914 new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
916 // find the given point in the feature attributes
917 std::list<AttributePtr> anAttiributes =
918 aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
919 std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
920 aLast = anAttiributes.end();
921 for (; anIt != aLast && !anAttribute; anIt++) {
922 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
923 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
925 std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
926 if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
927 anAttribute = aCurPoint;
937 void PartSet_Tools::sendSubFeaturesEvent(const CompositeFeaturePtr& theComposite,
938 const Events_ID theEventId)
940 if (!theComposite.get())
943 static Events_Loop* aLoop = Events_Loop::loop();
944 for (int i = 0; i < theComposite->numberOfSubs(); i++) {
945 FeaturePtr aSubFeature = theComposite->subFeature(i);
946 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
947 aECreator->sendUpdated(aSubFeature, theEventId);
949 Events_Loop::loop()->flush(theEventId);
952 bool PartSet_Tools::isAuxiliarySketchEntity(const ObjectPtr& theObject)
954 bool isAuxiliaryFeature = false;
956 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
957 std::string anAuxiliaryAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
958 AttributeBooleanPtr anAuxiliaryAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
959 anObjectFeature->data()->attribute(anAuxiliaryAttribute));
960 if (anAuxiliaryAttr.get())
961 isAuxiliaryFeature = anAuxiliaryAttr->value();
964 return isAuxiliaryFeature;