1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_Tools.cpp
4 // Created: 28 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include <PartSet_Tools.h>
8 #include <PartSet_Module.h>
9 #include <PartSet_SketcherMgr.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_AttributeRefList.h>
14 #include <ModelAPI_Document.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_ResultConstruction.h>
17 #include <ModelAPI_Events.h>
18 #include <ModelAPI_Validator.h>
20 #include <Events_Loop.h>
22 #include <SketcherPrs_Tools.h>
24 #include <XGUI_ModuleConnector.h>
25 #include <XGUI_Displayer.h>
26 #include <XGUI_Workshop.h>
27 #include <XGUI_SelectionMgr.h>
28 #include <XGUI_Selection.h>
30 #include <GeomDataAPI_Point.h>
31 #include <GeomDataAPI_Dir.h>
32 #include <GeomDataAPI_Point2D.h>
33 #include <GeomAPI_Pln.h>
34 #include <GeomAPI_Pnt2d.h>
35 #include <GeomAPI_Pnt.h>
36 #include <GeomAPI_Edge.h>
37 #include <GeomAPI_Vertex.h>
39 #include <GeomAPI_Dir.h>
40 #include <GeomAPI_XYZ.h>
42 #include <SketchPlugin_Feature.h>
43 #include <SketchPlugin_Sketch.h>
44 #include <SketchPlugin_ConstraintCoincidence.h>
45 #include <SketchPlugin_ConstraintDistance.h>
46 #include <SketchPlugin_ConstraintLength.h>
47 #include <SketchPlugin_ConstraintRadius.h>
48 #include <SketchPlugin_ConstraintRigid.h>
49 #include <SketchPlugin_Constraint.h>
50 #include <SketchPlugin_Circle.h>
51 #include <SketchPlugin_Arc.h>
52 #include <SketchPlugin_Line.h>
53 #include <SketchPlugin_Point.h>
55 #include <ModuleBase_IWorkshop.h>
56 #include <ModuleBase_ViewerPrs.h>
57 #include <ModuleBase_Tools.h>
59 #include <V3d_View.hxx>
61 #include <gp_Circ.hxx>
62 #include <ProjLib.hxx>
64 #include <Geom_Line.hxx>
65 #include <GeomAPI_ProjectPointOnCurve.hxx>
66 #include <BRep_Tool.hxx>
68 #include <TopoDS_Edge.hxx>
69 #include <TopoDS_Vertex.hxx>
70 #include <AIS_InteractiveObject.hxx>
71 #include <StdSelect_BRepOwner.hxx>
72 #include <SelectMgr_IndexedMapOfOwner.hxx>
78 const double PRECISION_TOLERANCE = 0.000001;
79 const int AIS_DEFAULT_WIDTH = 2;
81 int PartSet_Tools::getAISDefaultWidth()
83 return AIS_DEFAULT_WIDTH;
86 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
91 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
92 theView->Eye(XEye, YEye, ZEye);
94 theView->At(XAt, YAt, ZAt);
95 gp_Pnt EyePoint(XEye, YEye, ZEye);
96 gp_Pnt AtPoint(XAt, YAt, ZAt);
98 gp_Vec EyeVector(EyePoint, AtPoint);
99 gp_Dir EyeDir(EyeVector);
101 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
102 Standard_Real X, Y, Z;
103 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
104 gp_Pnt ConvertedPoint(X, Y, Z);
106 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
107 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
112 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
113 Handle(V3d_View) theView,
114 double& theX, double& theY)
119 AttributeDoublePtr anAttr;
120 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
122 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
123 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
125 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
126 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
127 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
128 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
129 std::shared_ptr<GeomAPI_XYZ> anY = aNorm->xyz()->cross(aX->xyz());
131 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
132 gp_Vec aVec(anOriginPnt, thePoint);
134 if (!theView.IsNull()) {
135 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
136 theView->Eye(XEye, YEye, ZEye);
138 theView->At(XAt, YAt, ZAt);
139 gp_Pnt EyePoint(XEye, YEye, ZEye);
140 gp_Pnt AtPoint(XAt, YAt, ZAt);
142 gp_Vec anEyeVec(EyePoint, AtPoint);
143 anEyeVec.Normalize();
145 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
146 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
147 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
149 double aDen = anEyeVec * aNormalVec;
150 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
152 gp_Vec aDeltaVec = anEyeVec * aLVec;
153 aVec = aVec - aDeltaVec;
155 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
156 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
159 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::convertTo2D(FeaturePtr theSketch,
160 const std::shared_ptr<GeomAPI_Pnt>& thePnt)
162 std::shared_ptr<GeomAPI_Pnt2d> aRes;
163 if (theSketch->getKind() != SketchPlugin_Sketch::ID())
165 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
166 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
167 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
168 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
169 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
170 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
171 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
172 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
176 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch)
178 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
180 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
181 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
182 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
183 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
184 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
185 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
186 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
188 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
189 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
191 return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
194 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
196 return ModelAPI_Session::get()->moduleDocument();
199 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
200 const std::string& theAttribute)
204 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
205 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
206 aData->attribute(theAttribute));
208 anAttribute->setValue(theValue);
211 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
217 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
218 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
219 aData->attribute(theAttribute));
221 aValue = anAttribute->value();
228 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
229 const std::string& theKind)
235 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
236 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
237 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
239 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
240 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
241 aFeature = FeaturePtr();
247 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
248 std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
249 std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
253 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
255 std::shared_ptr<ModelAPI_Document> aDoc = document();
256 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
259 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
261 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
262 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
263 aRef1->setAttr(thePoint1);
265 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
266 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
267 aRef2->setAttr(thePoint2);
269 // we need to flush created signal in order to coincidence is processed by solver
270 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
273 /*std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::
274 findAttributePoint(CompositeFeaturePtr theSketch, double theX, double theY,
275 double theTolerance, const QList<FeaturePtr>& theIgnore)
277 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
278 new GeomAPI_Pnt2d(theX, theY));
280 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
281 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
282 FeaturePtr aFeature = theSketch->subFeature(i);
283 if (!theIgnore.contains(aFeature)) {
284 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
286 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt;
287 for (anIt = anAttiributes.cbegin(); anIt != anAttiributes.cend(); ++anIt) {
288 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
289 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
290 double x = aCurPoint->x();
291 double y = aCurPoint->y();
292 if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < theTolerance)) {
298 return std::shared_ptr<GeomDataAPI_Point2D>();
302 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPointInArgumentFeatures(
303 const FeaturePtr& theFeature, const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
305 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
307 // may be feature is not updated yet, execute is not performed and references features
308 // are not created. Case: rectangle macro feature
309 ModuleBase_Tools::flushUpdated(theFeature);
311 std::list<AttributePtr> anAttributes = theFeature->data()->attributes(
312 ModelAPI_AttributeRefList::typeId());
313 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
314 for (; anIt != aLast && !aFeaturePoint.get(); anIt++) {
315 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
316 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
317 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFeaturePoint.get(); i++) {
318 ObjectPtr anObject = aCurSelList->object(i);
319 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
321 aFeaturePoint = PartSet_Tools::findFirstEqualPoint(aFeature, thePoint);
324 return aFeaturePoint;
327 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPoint(const FeaturePtr& theFeature,
328 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
330 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
332 // find the given point in the feature attributes
333 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
334 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
335 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
336 aLast = anAttiributes.end();
337 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
339 for (; anIt != aLast && !aFPoint; anIt++) {
340 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
341 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
342 if (aCurPoint && aCurPoint->isInitialized() &&
343 aValidators->isCase(theFeature, aCurPoint->id()) &&
344 (aCurPoint->pnt()->distance(thePoint) < Precision::Confusion())) {
352 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
353 const std::string& theAttribute, double theClickedX,
356 if (!theFeature.get())
359 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
360 new GeomAPI_Pnt2d(theClickedX, theClickedY));
362 // find a feature point by the selection mode
363 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
364 if (theFeature->isMacro()) {
365 // the macro feature will be removed after the operation is stopped, so we need to build
366 // coicidence to possible sub-features
367 aFeaturePoint = PartSet_Tools::findFirstEqualPointInArgumentFeatures(theFeature, aClickedPoint);
370 aFeaturePoint = std::dynamic_pointer_cast<
371 GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
376 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
377 // the constraint is created between the feature point and the found sketch point
378 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
379 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
380 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
382 std::list<ObjectPtr> aFeatures = aRefList->list();
383 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
384 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
385 for (; anIt != aLast; anIt++) {
386 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
387 if (!aFeature.get() || theFeature == aFeature)
389 std::shared_ptr<GeomDataAPI_Point2D> aFPoint = PartSet_Tools::findFirstEqualPoint(aFeature,
392 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
396 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
398 std::shared_ptr<GeomAPI_Pln> aPlane;
400 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
402 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
403 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
404 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
405 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
406 if (aNormal.get() && aNormal->isInitialized() &&
407 anOrigin.get() && anOrigin->isInitialized()) {
408 double adX = aNormal->x();
409 double adY = aNormal->y();
410 double adZ = aNormal->z();
412 if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
413 double aX = anOrigin->x();
414 double aY = anOrigin->y();
415 double aZ = anOrigin->z();
416 gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
417 double aA, aB, aC, aD;
418 aPln.Coefficients(aA, aB, aC, aD);
419 aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
426 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
427 CompositeFeaturePtr theSketch)
429 std::shared_ptr<GeomAPI_Pnt> aPoint;
430 if (!theSketch || !thePoint2D)
433 DataPtr aData = theSketch->data();
434 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
435 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
436 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
437 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
438 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
439 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
440 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
442 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
445 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
446 const ObjectPtr& theObject,
447 CompositeFeaturePtr theSketch)
450 if (theShape.ShapeType() == TopAbs_EDGE) {
451 // Check that we already have such external edge
452 std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
453 aInEdge->setImpl(new TopoDS_Shape(theShape));
454 aResult = findExternalEdge(theSketch, aInEdge);
456 if (theShape.ShapeType() == TopAbs_VERTEX) {
457 std::shared_ptr<GeomAPI_Vertex> aInVert = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex());
458 aInVert->setImpl(new TopoDS_Shape(theShape));
459 aResult = findExternalVertex(theSketch, aInVert);
464 ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShape,
465 const ObjectPtr& theObject,
466 CompositeFeaturePtr theSketch,
467 const bool theTemporary)
469 if (theShape.ShapeType() == TopAbs_EDGE) {
470 Standard_Real aStart, aEnd;
471 Handle(V3d_View) aNullView;
472 FeaturePtr aMyFeature;
474 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
475 GeomAdaptor_Curve aAdaptor(aCurve);
476 std::shared_ptr<GeomAPI_Edge> anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
477 anEdge->setImpl(new TopoDS_Shape(theShape));
478 if (aAdaptor.GetType() == GeomAbs_Line) {
480 aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
481 if (!theObject.get()) {
482 // There is no selected result
483 std::shared_ptr<GeomAPI_Pnt> aPnt1 = anEdge->firstPoint();
484 std::shared_ptr<GeomAPI_Pnt> aPnt2 = anEdge->lastPoint();
485 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = convertTo2D(theSketch, aPnt1);
486 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d2 = convertTo2D(theSketch, aPnt2);
488 std::shared_ptr<ModelAPI_Data> aData = aMyFeature->data();
489 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
490 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
491 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
492 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
494 aPoint1->setValue(aPnt2d1);
495 aPoint2->setValue(aPnt2d2);
497 // If this is an axis then its name has to be changed correspondently
498 std::string aSuffix = "";
499 bool aXdir = fabs(aPnt1->x() - aPnt2->x()) > Precision::Confusion();
500 bool aYdir = fabs(aPnt1->y() - aPnt2->y()) > Precision::Confusion();
501 bool aZdir = fabs(aPnt1->z() - aPnt2->z()) > Precision::Confusion();
502 if (aXdir && (!aYdir) && (!aZdir))
504 else if ((!aXdir) && aYdir && (!aZdir))
506 else if ((!aXdir) && (!aYdir) && aZdir)
508 if (aSuffix.length() > 0)
509 aData->setName("Axis_" + aSuffix);
510 aMyFeature->execute();
513 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
514 if (anEdge->isArc()) {
516 aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
520 aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
524 DataPtr aData = aMyFeature->data();
525 AttributeSelectionPtr anAttr =
526 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
527 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
529 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
530 // selection shape has no result owner => the trihedron axis
531 // TODO: make reference to the real axes when they are implemented in the initialization plugin
533 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()->objectByName(
534 ModelAPI_ResultConstruction::group(), "Origin");
535 if (aPointObj.get()) { // if initialization plugin performed well
536 aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
540 aRes = aMyFeature->firstResult();
542 if (anAttr.get() && aRes.get()) {
543 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
544 anEdge->setImpl(new TopoDS_Shape(theShape));
546 anAttr->setValue(aRes, anEdge);
547 //if (!theTemporary) {
548 aMyFeature->execute();
551 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
552 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
553 setObject(aMyFeature->lastResult());
555 return aMyFeature->lastResult();
559 if (theShape.ShapeType() == TopAbs_VERTEX) {
560 FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
563 DataPtr aData = aMyFeature->data();
564 AttributeSelectionPtr anAttr =
565 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
566 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
568 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
569 // if there is no object, it means that this is the origin point: search it in the module document
571 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()->objectByName(
572 ModelAPI_ResultConstruction::group(), "Origin");
573 if (aPointObj.get()) { // if initialization plugin performed well
574 aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
577 // reference to itself with name "Origin" (but this may cause the infinitive cycling)
579 // If the point is selected not from Result object
580 std::shared_ptr<GeomAPI_Shape> aShape =
581 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
582 aShape->setImpl(new TopoDS_Shape(theShape));
584 std::shared_ptr<GeomAPI_Vertex> aVertex =
585 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
586 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
588 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = convertTo2D(theSketch, aPnt);
589 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
590 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Point::COORD_ID()));
591 aPoint->setValue(aPnt2d);
592 if ((aPnt->x() < Precision::Confusion()) &&
593 (aPnt->y() < Precision::Confusion()) &&
594 (aPnt->z() < Precision::Confusion()))
595 aData->setName("Origin");
597 aMyFeature->execute();
598 aRes = aMyFeature->firstResult();
600 if (anAttr.get() && aRes.get()) {
601 std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
602 aVert->setImpl(new TopoDS_Shape(theShape));
604 anAttr->setValue(aRes, aVert);
605 //if (theTemporary) {
606 aMyFeature->execute();
609 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
610 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
611 setObject(aMyFeature->lastResult());
613 return aMyFeature->lastResult();
620 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrsPtr>& theSelected,
621 const ModuleBase_ViewerPrsPtr& thePrs)
623 foreach (ModuleBase_ViewerPrsPtr aPrs, theSelected) {
624 if (aPrs->object() == thePrs->object())
630 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge)
632 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
633 FeaturePtr aFeature = theSketch->subFeature(i);
634 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
635 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
637 if (aSketchFea->isExternal()) {
638 std::list<ResultPtr> aResults = aSketchFea->results();
639 std::list<ResultPtr>::const_iterator aIt;
640 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
641 ResultConstructionPtr aRes =
642 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
644 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
646 if (theEdge->isEqual(aShape))
658 ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
660 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
661 FeaturePtr aFeature = theSketch->subFeature(i);
662 std::shared_ptr<SketchPlugin_Feature> aSketchFea =
663 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
665 if (aSketchFea->isExternal()) {
666 std::list<ResultPtr> aResults = aSketchFea->results();
667 std::list<ResultPtr>::const_iterator aIt;
668 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
669 ResultConstructionPtr aRes =
670 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
672 std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
674 if (theVert->isEqual(aShape))
686 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrsPtr& thePrs, FeaturePtr theSketch,
687 Handle_V3d_View theView, double& theX, double& theY)
689 bool aHasVertex = false;
691 const GeomShapePtr& aShape = thePrs->shape();
692 if (aShape.get() && !aShape->isNull() && aShape->shapeType() == GeomAPI_Shape::VERTEX)
694 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
695 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aTDShape);
696 if (!aVertex.IsNull())
698 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
699 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
707 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
708 ModuleBase_IWorkshop* theWorkshop)
711 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
712 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
714 // 2. find visualized vertices of the attribute and if the attribute of the vertex is
715 // the same, return it
716 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
717 // 2.1 get visualized results of the feature
718 const std::list<ResultPtr>& aResList = anAttributeFeature->results();
719 std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
720 for (; anIt != aLast; anIt++) {
721 AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
722 if (aAISObj.get() != NULL) {
723 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
724 // 2.2 find selected owners of a visualizedd object
725 SelectMgr_IndexedMapOfOwner aSelectedOwners;
726 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
727 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
728 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
729 if (!anOwner.IsNull()) {
730 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
731 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
732 const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
733 if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
734 // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
735 // attribute, returns the shape
736 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
737 PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
738 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
739 aBRepShape, aSketchMgr->activeSketch());
740 if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
741 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
742 aShape->setImpl(new TopoDS_Shape(aBRepShape));
754 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
755 const std::string& theAttribute)
757 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
759 if (!theFeature->data())
760 return std::shared_ptr<GeomAPI_Pnt2d>();
763 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
764 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
766 return std::shared_ptr<GeomAPI_Pnt2d>();
768 aFeature = ModelAPI_Feature::feature(anAttr->object());
770 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
771 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
772 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
774 else if (anAttr->attr()) {
775 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
777 if (aPointAttr.get() != NULL)
778 return aPointAttr->pnt();
779 return std::shared_ptr<GeomAPI_Pnt2d>();
782 FeaturePtr findFirstCoincidenceByData(const DataPtr& theData, std::shared_ptr<GeomAPI_Pnt2d> thePoint)
784 FeaturePtr aCoincident;
786 const std::set<AttributePtr>& aRefsList = theData->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;
810 FeaturePtr PartSet_Tools::findFirstCoincidence(const FeaturePtr& theFeature,
811 std::shared_ptr<GeomAPI_Pnt2d> thePoint)
813 FeaturePtr aCoincident;
814 if (theFeature.get() == NULL)
817 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
818 std::set<AttributePtr>::const_iterator aIt;
819 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
820 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
821 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
822 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
823 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
824 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
825 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
826 aCoincident = aConstrFeature;
829 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
830 SketchPlugin_ConstraintCoincidence::ENTITY_B());
831 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
832 aCoincident = aConstrFeature;
839 if (!aCoincident.get()) {
840 std::list<ResultPtr> aResults = theFeature->results();
841 std::list<ResultPtr>::const_iterator aIt;
842 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
843 ResultPtr aResult = *aIt;
844 aCoincident = findFirstCoincidenceByData(aResult->data(), thePoint);
845 if (aCoincident.get())
852 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
853 QList<FeaturePtr>& theCoincidencies,
856 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
857 if (aOrig.get() == NULL)
860 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
863 ObjectPtr aObj = aPnt->object();
864 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
865 if (aFeature.get()) {
866 if (!theList.contains(aFeature)) {
867 theList.append(aFeature);
868 theCoincidencies.append(theStartCoin);
869 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
870 std::set<AttributePtr>::const_iterator aIt;
871 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
872 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
873 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
874 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
875 if (!theCoincidencies.contains(aConstrFeature)) {
876 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
877 if (aPnt.get() && aOrig->isEqual(aPnt)) {
878 findCoincidences(aConstrFeature, theList, theCoincidencies,
879 SketchPlugin_ConstraintCoincidence::ENTITY_A());
880 findCoincidences(aConstrFeature, theList, theCoincidencies,
881 SketchPlugin_ConstraintCoincidence::ENTITY_B());
889 ResultConstructionPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
891 FeaturePtr aFeature = ModelAPI_Feature::feature(aPnt->object());
892 if (!theList.contains(aFeature))
893 theList.append(aFeature);
894 theCoincidencies.append(theStartCoin);
896 const std::set<AttributePtr>& aRefsList = aResult->data()->refsToMe();
897 std::set<AttributePtr>::const_iterator aIt;
898 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
899 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
900 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
901 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
902 if (!theCoincidencies.contains(aConstrFeature)) {
903 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
904 if (aPnt.get() && aOrig->isEqual(aPnt)) {
905 findCoincidences(aConstrFeature, theList, theCoincidencies,
906 SketchPlugin_ConstraintCoincidence::ENTITY_A());
907 findCoincidences(aConstrFeature, theList, theCoincidencies,
908 SketchPlugin_ConstraintCoincidence::ENTITY_B());
917 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
919 std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
920 SketchPlugin_Constraint::ENTITY_A());
921 if (aPnt.get() == NULL)
922 aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
926 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
927 const TopoDS_Shape theShape,
928 FeaturePtr theSketch)
931 AttributePtr anAttribute;
932 FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
934 if (theShape.ShapeType() == TopAbs_VERTEX) {
935 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
936 if (!aVertex.IsNull()) {
937 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
938 std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
939 new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
941 // find the given point in the feature attributes
942 std::list<AttributePtr> anAttiributes =
943 aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
944 std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
945 aLast = anAttiributes.end();
946 for (; anIt != aLast && !anAttribute; anIt++) {
947 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
948 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
950 std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
951 if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
952 anAttribute = aCurPoint;
962 void PartSet_Tools::sendSubFeaturesEvent(const CompositeFeaturePtr& theComposite,
963 const Events_ID theEventId)
965 if (!theComposite.get())
968 static Events_Loop* aLoop = Events_Loop::loop();
969 for (int i = 0; i < theComposite->numberOfSubs(); i++) {
970 FeaturePtr aSubFeature = theComposite->subFeature(i);
971 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
972 aECreator->sendUpdated(aSubFeature, theEventId);
974 Events_Loop::loop()->flush(theEventId);
977 bool PartSet_Tools::isAuxiliarySketchEntity(const ObjectPtr& theObject)
979 bool isAuxiliaryFeature = false;
981 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
982 std::string anAuxiliaryAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
983 AttributeBooleanPtr anAuxiliaryAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
984 anObjectFeature->data()->attribute(anAuxiliaryAttribute));
985 if (anAuxiliaryAttr.get())
986 isAuxiliaryFeature = anAuxiliaryAttr->value();
989 return isAuxiliaryFeature;