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.get() && aNormal->isInitialized() &&
401 anOrigin.get() && anOrigin->isInitialized()) {
402 double adX = aNormal->x();
403 double adY = aNormal->y();
404 double adZ = aNormal->z();
406 if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
407 double aX = anOrigin->x();
408 double aY = anOrigin->y();
409 double aZ = anOrigin->z();
410 gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
411 double aA, aB, aC, aD;
412 aPln.Coefficients(aA, aB, aC, aD);
413 aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
420 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
421 CompositeFeaturePtr theSketch)
423 std::shared_ptr<GeomAPI_Pnt> aPoint;
424 if (!theSketch || !thePoint2D)
427 DataPtr aData = theSketch->data();
428 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
429 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
430 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
431 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
432 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
433 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
434 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
436 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
439 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
440 const ObjectPtr& theObject,
441 CompositeFeaturePtr theSketch)
444 if (theShape.ShapeType() == TopAbs_EDGE) {
445 // Check that we already have such external edge
446 std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
447 aInEdge->setImpl(new TopoDS_Shape(theShape));
448 aResult = findExternalEdge(theSketch, aInEdge);
450 if (theShape.ShapeType() == TopAbs_VERTEX) {
451 std::shared_ptr<GeomAPI_Vertex> aInVert = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex());
452 aInVert->setImpl(new TopoDS_Shape(theShape));
453 aResult = findExternalVertex(theSketch, aInVert);
458 ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShape,
459 const ObjectPtr& theObject,
460 CompositeFeaturePtr theSketch,
461 const bool theTemporary)
463 if (theShape.ShapeType() == TopAbs_EDGE) {
464 Standard_Real aStart, aEnd;
465 Handle(V3d_View) aNullView;
466 FeaturePtr aMyFeature;
468 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
469 GeomAdaptor_Curve aAdaptor(aCurve);
470 std::shared_ptr<GeomAPI_Edge> anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
471 anEdge->setImpl(new TopoDS_Shape(theShape));
472 if (aAdaptor.GetType() == GeomAbs_Line) {
474 aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
475 if (!theObject.get()) {
476 // There is no selected result
477 std::shared_ptr<GeomAPI_Pnt> aPnt1 = anEdge->firstPoint();
478 std::shared_ptr<GeomAPI_Pnt> aPnt2 = anEdge->lastPoint();
479 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = convertTo2D(theSketch, aPnt1);
480 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d2 = convertTo2D(theSketch, aPnt2);
482 std::shared_ptr<ModelAPI_Data> aData = aMyFeature->data();
483 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
484 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
485 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
486 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
488 aPoint1->setValue(aPnt2d1);
489 aPoint2->setValue(aPnt2d2);
491 // If this is an axis then its name has to be changed correspondently
492 std::string aSuffix = "";
493 bool aXdir = fabs(aPnt1->x() - aPnt2->x()) > Precision::Confusion();
494 bool aYdir = fabs(aPnt1->y() - aPnt2->y()) > Precision::Confusion();
495 bool aZdir = fabs(aPnt1->z() - aPnt2->z()) > Precision::Confusion();
496 if (aXdir && (!aYdir) && (!aZdir))
498 else if ((!aXdir) && aYdir && (!aZdir))
500 else if ((!aXdir) && (!aYdir) && aZdir)
502 if (aSuffix.length() > 0)
503 aData->setName("Axis_" + aSuffix);
504 aMyFeature->execute();
507 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
508 if (anEdge->isArc()) {
510 aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
514 aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
518 DataPtr aData = aMyFeature->data();
519 AttributeSelectionPtr anAttr =
520 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
521 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
523 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
524 // selection shape has no result owner => the trihedron axis
525 // TODO: make reference to the real axes when they are implemented in the initialization plugin
527 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()->objectByName(
528 ModelAPI_ResultConstruction::group(), "Origin");
529 if (aPointObj.get()) { // if initialization plugin performed well
530 aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
534 aRes = aMyFeature->firstResult();
536 if (anAttr.get() && aRes.get()) {
537 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
538 anEdge->setImpl(new TopoDS_Shape(theShape));
540 anAttr->setValue(aRes, anEdge);
541 //if (!theTemporary) {
542 aMyFeature->execute();
545 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
546 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
547 setObject(aMyFeature->lastResult());
549 return aMyFeature->lastResult();
553 if (theShape.ShapeType() == TopAbs_VERTEX) {
554 FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
557 DataPtr aData = aMyFeature->data();
558 AttributeSelectionPtr anAttr =
559 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
560 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
562 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
563 // if there is no object, it means that this is the origin point: search it in the module document
565 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()->objectByName(
566 ModelAPI_ResultConstruction::group(), "Origin");
567 if (aPointObj.get()) { // if initialization plugin performed well
568 aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
571 // reference to itself with name "Origin" (but this may cause the infinitive cycling)
573 // If the point is selected not from Result object
574 std::shared_ptr<GeomAPI_Shape> aShape =
575 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
576 aShape->setImpl(new TopoDS_Shape(theShape));
578 std::shared_ptr<GeomAPI_Vertex> aVertex =
579 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
580 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
582 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = convertTo2D(theSketch, aPnt);
583 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
584 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Point::COORD_ID()));
585 aPoint->setValue(aPnt2d);
586 if ((aPnt->x() < Precision::Confusion()) &&
587 (aPnt->y() < Precision::Confusion()) &&
588 (aPnt->z() < Precision::Confusion()))
589 aData->setName("Origin");
591 aMyFeature->execute();
592 aRes = aMyFeature->firstResult();
594 if (anAttr.get() && aRes.get()) {
595 std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
596 aVert->setImpl(new TopoDS_Shape(theShape));
598 anAttr->setValue(aRes, aVert);
599 //if (theTemporary) {
600 aMyFeature->execute();
603 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
604 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
605 setObject(aMyFeature->lastResult());
607 return aMyFeature->lastResult();
614 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrsPtr>& theSelected,
615 const ModuleBase_ViewerPrsPtr& thePrs)
617 foreach (ModuleBase_ViewerPrsPtr aPrs, theSelected) {
618 if (aPrs->object() == thePrs->object())
624 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge)
626 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
627 FeaturePtr aFeature = theSketch->subFeature(i);
628 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
629 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
631 if (aSketchFea->isExternal()) {
632 std::list<ResultPtr> aResults = aSketchFea->results();
633 std::list<ResultPtr>::const_iterator aIt;
634 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
635 ResultConstructionPtr aRes =
636 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
638 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
640 if (theEdge->isEqual(aShape))
652 ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
654 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
655 FeaturePtr aFeature = theSketch->subFeature(i);
656 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
657 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
659 if (aSketchFea->isExternal()) {
660 std::list<ResultPtr> aResults = aSketchFea->results();
661 std::list<ResultPtr>::const_iterator aIt;
662 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
663 ResultConstructionPtr aRes =
664 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
666 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
668 if (theVert->isEqual(aShape))
680 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrsPtr& thePrs, FeaturePtr theSketch,
681 Handle_V3d_View theView, double& theX, double& theY)
683 bool aHasVertex = false;
685 const GeomShapePtr& aShape = thePrs->shape();
686 if (aShape.get() && !aShape->isNull() && aShape->shapeType() == GeomAPI_Shape::VERTEX)
688 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
689 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aTDShape);
690 if (!aVertex.IsNull())
692 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
693 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
701 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
702 ModuleBase_IWorkshop* theWorkshop)
705 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
706 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
708 // 2. find visualized vertices of the attribute and if the attribute of the vertex is
709 // the same, return it
710 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
711 // 2.1 get visualized results of the feature
712 const std::list<ResultPtr>& aResList = anAttributeFeature->results();
713 std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
714 for (; anIt != aLast; anIt++) {
715 AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
716 if (aAISObj.get() != NULL) {
717 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
718 // 2.2 find selected owners of a visualizedd object
719 SelectMgr_IndexedMapOfOwner aSelectedOwners;
720 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
721 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
722 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
723 if (!anOwner.IsNull()) {
724 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
725 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
726 const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
727 if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
728 // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
729 // attribute, returns the shape
730 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
731 PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
732 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
733 aBRepShape, aSketchMgr->activeSketch());
734 if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
735 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
736 aShape->setImpl(new TopoDS_Shape(aBRepShape));
748 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
749 const std::string& theAttribute)
751 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
753 if (!theFeature->data())
754 return std::shared_ptr<GeomAPI_Pnt2d>();
757 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
758 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
760 return std::shared_ptr<GeomAPI_Pnt2d>();
762 aFeature = ModelAPI_Feature::feature(anAttr->object());
764 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
765 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
766 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
768 else if (anAttr->attr()) {
769 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
771 if (aPointAttr.get() != NULL)
772 return aPointAttr->pnt();
773 return std::shared_ptr<GeomAPI_Pnt2d>();
776 FeaturePtr findFirstCoincidenceByData(const DataPtr& theData, std::shared_ptr<GeomAPI_Pnt2d> thePoint)
778 FeaturePtr aCoincident;
780 const std::set<AttributePtr>& aRefsList = theData->refsToMe();
781 std::set<AttributePtr>::const_iterator aIt;
782 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
783 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
784 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
785 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
786 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
787 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
788 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
789 aCoincident = aConstrFeature;
792 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
793 SketchPlugin_ConstraintCoincidence::ENTITY_B());
794 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
795 aCoincident = aConstrFeature;
804 FeaturePtr PartSet_Tools::findFirstCoincidence(const FeaturePtr& theFeature,
805 std::shared_ptr<GeomAPI_Pnt2d> thePoint)
807 FeaturePtr aCoincident;
808 if (theFeature.get() == NULL)
811 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
812 std::set<AttributePtr>::const_iterator aIt;
813 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
814 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
815 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
816 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
817 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
818 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
819 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
820 aCoincident = aConstrFeature;
823 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
824 SketchPlugin_ConstraintCoincidence::ENTITY_B());
825 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
826 aCoincident = aConstrFeature;
833 if (!aCoincident.get()) {
834 std::list<ResultPtr> aResults = theFeature->results();
835 std::list<ResultPtr>::const_iterator aIt;
836 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
837 ResultPtr aResult = *aIt;
838 aCoincident = findFirstCoincidenceByData(aResult->data(), thePoint);
839 if (aCoincident.get())
846 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
847 QList<FeaturePtr>& theCoincidencies,
850 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
851 if (aOrig.get() == NULL)
854 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
857 ObjectPtr aObj = aPnt->object();
858 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
859 if (aFeature.get()) {
860 if (!theList.contains(aFeature)) {
861 theList.append(aFeature);
862 theCoincidencies.append(theStartCoin);
863 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
864 std::set<AttributePtr>::const_iterator aIt;
865 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
866 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
867 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
868 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
869 if (!theCoincidencies.contains(aConstrFeature)) {
870 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
871 if (aPnt.get() && aOrig->isEqual(aPnt)) {
872 findCoincidences(aConstrFeature, theList, theCoincidencies,
873 SketchPlugin_ConstraintCoincidence::ENTITY_A());
874 findCoincidences(aConstrFeature, theList, theCoincidencies,
875 SketchPlugin_ConstraintCoincidence::ENTITY_B());
883 ResultConstructionPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
885 FeaturePtr aFeature = ModelAPI_Feature::feature(aPnt->object());
886 if (!theList.contains(aFeature))
887 theList.append(aFeature);
888 theCoincidencies.append(theStartCoin);
890 const std::set<AttributePtr>& aRefsList = aResult->data()->refsToMe();
891 std::set<AttributePtr>::const_iterator aIt;
892 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
893 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
894 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
895 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
896 if (!theCoincidencies.contains(aConstrFeature)) {
897 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
898 if (aPnt.get() && aOrig->isEqual(aPnt)) {
899 findCoincidences(aConstrFeature, theList, theCoincidencies,
900 SketchPlugin_ConstraintCoincidence::ENTITY_A());
901 findCoincidences(aConstrFeature, theList, theCoincidencies,
902 SketchPlugin_ConstraintCoincidence::ENTITY_B());
911 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
913 std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
914 SketchPlugin_Constraint::ENTITY_A());
915 if (aPnt.get() == NULL)
916 aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
920 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
921 const TopoDS_Shape theShape,
922 FeaturePtr theSketch)
925 AttributePtr anAttribute;
926 FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
928 if (theShape.ShapeType() == TopAbs_VERTEX) {
929 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
930 if (!aVertex.IsNull()) {
931 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
932 std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
933 new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
935 // find the given point in the feature attributes
936 std::list<AttributePtr> anAttiributes =
937 aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
938 std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
939 aLast = anAttiributes.end();
940 for (; anIt != aLast && !anAttribute; anIt++) {
941 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
942 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
944 std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
945 if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
946 anAttribute = aCurPoint;
956 void PartSet_Tools::sendSubFeaturesEvent(const CompositeFeaturePtr& theComposite,
957 const Events_ID theEventId)
959 if (!theComposite.get())
962 static Events_Loop* aLoop = Events_Loop::loop();
963 for (int i = 0; i < theComposite->numberOfSubs(); i++) {
964 FeaturePtr aSubFeature = theComposite->subFeature(i);
965 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
966 aECreator->sendUpdated(aSubFeature, theEventId);
968 Events_Loop::loop()->flush(theEventId);
971 bool PartSet_Tools::isAuxiliarySketchEntity(const ObjectPtr& theObject)
973 bool isAuxiliaryFeature = false;
975 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
976 std::string anAuxiliaryAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
977 AttributeBooleanPtr anAuxiliaryAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
978 anObjectFeature->data()->attribute(anAuxiliaryAttribute));
979 if (anAuxiliaryAttr.get())
980 isAuxiliaryFeature = anAuxiliaryAttr->value();
983 return isAuxiliaryFeature;