1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <PartSet_Tools.h>
22 #include <PartSet_Module.h>
23 #include <PartSet_SketcherMgr.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_ResultConstruction.h>
31 #include <ModelAPI_Events.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_Tools.h>
35 #include <ModuleBase_IViewWindow.h>
37 #include <ModelGeomAlgo_Point2D.h>
39 #include <Events_Loop.h>
41 #include <SketcherPrs_Tools.h>
43 #include <XGUI_ModuleConnector.h>
44 #include <XGUI_Displayer.h>
45 #include <XGUI_Workshop.h>
46 #include <XGUI_SelectionMgr.h>
47 #include <XGUI_Selection.h>
49 #include <GeomDataAPI_Point.h>
50 #include <GeomDataAPI_Dir.h>
51 #include <GeomDataAPI_Point2D.h>
52 #include <GeomAPI_Pln.h>
53 #include <GeomAPI_Pnt2d.h>
54 #include <GeomAPI_Pnt.h>
55 #include <GeomAPI_Edge.h>
56 #include <GeomAPI_Vertex.h>
58 #include <GeomAPI_Dir.h>
59 #include <GeomAPI_XYZ.h>
61 #include <SketchPlugin_Feature.h>
62 #include <SketchPlugin_Sketch.h>
63 #include <SketchPlugin_ConstraintCoincidence.h>
64 #include <SketchPlugin_ConstraintDistance.h>
65 #include <SketchPlugin_ConstraintLength.h>
66 #include <SketchPlugin_ConstraintRadius.h>
67 #include <SketchPlugin_ConstraintRigid.h>
68 #include <SketchPlugin_Constraint.h>
69 #include <SketchPlugin_Circle.h>
70 #include <SketchPlugin_Arc.h>
71 #include <SketchPlugin_Line.h>
72 #include <SketchPlugin_Point.h>
73 #include <SketchPlugin_Projection.h>
74 #include <SketchPlugin_IntersectionPoint.h>
76 #include <ModuleBase_IWorkshop.h>
77 #include <ModuleBase_ViewerPrs.h>
78 #include <ModuleBase_Tools.h>
80 #include <V3d_View.hxx>
82 #include <gp_Circ.hxx>
83 #include <ProjLib.hxx>
85 #include <Geom_Line.hxx>
86 #include <GeomAPI_ProjectPointOnCurve.hxx>
87 #include <BRep_Tool.hxx>
89 #include <TopoDS_Edge.hxx>
90 #include <TopoDS_Vertex.hxx>
91 #include <AIS_InteractiveObject.hxx>
92 #include <StdSelect_BRepOwner.hxx>
93 #include <SelectMgr_IndexedMapOfOwner.hxx>
94 #include <V3d_Coordinate.hxx>
96 #include <QMouseEvent>
102 const double PRECISION_TOLERANCE = 0.000001;
103 const int AIS_DEFAULT_WIDTH = 2;
104 const bool SKETCH_PROJECTION_INCLUDE_INTO_RESULT = false; // by default, it is not presented
106 int PartSet_Tools::getAISDefaultWidth()
108 return AIS_DEFAULT_WIDTH;
111 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
113 if (theView.IsNull())
116 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
117 theView->Eye(XEye, YEye, ZEye);
119 theView->At(XAt, YAt, ZAt);
120 gp_Pnt EyePoint(XEye, YEye, ZEye);
121 gp_Pnt AtPoint(XAt, YAt, ZAt);
123 gp_Vec EyeVector(EyePoint, AtPoint);
124 gp_Dir EyeDir(EyeVector);
126 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
127 Standard_Real X, Y, Z;
128 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
129 gp_Pnt ConvertedPoint(X, Y, Z);
131 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
132 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
137 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
138 Handle(V3d_View) theView,
139 double& theX, double& theY)
144 AttributeDoublePtr anAttr;
145 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
147 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
148 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
150 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
151 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
152 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
153 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
154 std::shared_ptr<GeomAPI_XYZ> anY = aNorm->xyz()->cross(aX->xyz());
156 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
157 gp_Vec aVec(anOriginPnt, thePoint);
159 if (!theView.IsNull()) {
160 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
161 theView->Eye(XEye, YEye, ZEye);
163 theView->At(XAt, YAt, ZAt);
164 gp_Pnt EyePoint(XEye, YEye, ZEye);
165 gp_Pnt AtPoint(XAt, YAt, ZAt);
167 gp_Vec anEyeVec(EyePoint, AtPoint);
168 anEyeVec.Normalize();
170 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
171 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
172 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
174 double aDen = anEyeVec * aNormalVec;
175 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
177 gp_Vec aDeltaVec = anEyeVec * aLVec;
178 aVec = aVec - aDeltaVec;
180 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
181 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
184 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::convertTo2D(FeaturePtr theSketch,
185 const std::shared_ptr<GeomAPI_Pnt>& thePnt)
187 std::shared_ptr<GeomAPI_Pnt2d> aRes;
188 if (theSketch->getKind() != SketchPlugin_Sketch::ID())
190 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
191 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
192 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
193 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
194 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
195 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
196 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
197 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
201 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY,
202 FeaturePtr theSketch)
204 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
206 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
207 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
208 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
209 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
210 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
211 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
212 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
214 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
215 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
217 return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
220 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
222 return ModelAPI_Session::get()->moduleDocument();
225 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
226 const std::string& theAttribute)
230 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
231 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
232 aData->attribute(theAttribute));
234 anAttribute->setValue(theValue);
237 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
243 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
244 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
245 aData->attribute(theAttribute));
247 aValue = anAttribute->value();
254 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
255 const std::string& theKind)
261 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
262 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
263 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
265 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
266 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
267 aFeature = FeaturePtr();
273 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
274 std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
275 std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
279 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
281 std::shared_ptr<ModelAPI_Document> aDoc = document();
282 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
285 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
287 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
288 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
289 aRef1->setAttr(thePoint1);
291 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
292 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
293 aRef2->setAttr(thePoint2);
295 // we need to flush created signal in order to coincidence is processed by solver
296 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
299 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
301 std::shared_ptr<GeomAPI_Pln> aPlane;
303 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
304 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
305 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
306 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
308 if (aNormal.get() && aNormal->isInitialized() &&
309 anOrigin.get() && anOrigin->isInitialized())
310 aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNormal->dir()));
315 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
316 CompositeFeaturePtr theSketch)
318 std::shared_ptr<GeomAPI_Pnt> aPoint;
319 if (!theSketch || !thePoint2D)
322 DataPtr aData = theSketch->data();
323 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
324 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
325 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
326 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
327 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
328 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
329 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
331 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
334 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
335 const ObjectPtr& theObject,
336 CompositeFeaturePtr theSketch)
338 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
342 for (int i = 0, aNbSubs = theSketch->numberOfSubs(); i < aNbSubs; i++) {
343 FeaturePtr aFeature = theSketch->subFeature(i);
344 if (aFeature->getKind() != SketchPlugin_Projection::PROJECTED_FEATURE_ID())
346 if (aFeature->lastResult() == aResult)
352 ResultPtr PartSet_Tools::createFixedObjectByExternal(
353 const std::shared_ptr<GeomAPI_Shape>& theShape,
354 const ObjectPtr& theObject,
355 CompositeFeaturePtr theSketch,
356 const bool theTemporary,
357 FeaturePtr& theCreatedFeature)
359 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
363 FeaturePtr aProjectionFeature = theSketch->addFeature(SketchPlugin_Projection::ID());
364 theCreatedFeature = aProjectionFeature;
365 AttributeSelectionPtr anExternalAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
366 aProjectionFeature->attribute(SketchPlugin_Projection::EXTERNAL_FEATURE_ID()));
367 anExternalAttr->setValue(aResult, theShape);
369 AttributeBooleanPtr anIntoResult = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>
370 (aProjectionFeature->data()->attribute(SketchPlugin_Projection::INCLUDE_INTO_RESULT()));
371 anIntoResult->setValue(SKETCH_PROJECTION_INCLUDE_INTO_RESULT);
372 aProjectionFeature->execute();
374 // if projection feature has not been created, exit
375 AttributeRefAttrPtr aRefAttr = aProjectionFeature->data()->refattr(
376 SketchPlugin_Projection::PROJECTED_FEATURE_ID());
377 if (!aRefAttr || !aRefAttr->isInitialized())
379 // remove external feature if the attribute is not filled
380 std::set<FeaturePtr> aFeatures;
381 aFeatures.insert(aProjectionFeature);
382 ModelAPI_Tools::removeFeaturesAndReferences(aFeatures);
386 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
387 if (aProjection.get() && aProjection->lastResult().get())
388 return aProjection->lastResult();
393 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrsPtr>& theSelected,
394 const ModuleBase_ViewerPrsPtr& thePrs)
396 foreach (ModuleBase_ViewerPrsPtr aPrs, theSelected) {
397 if (aPrs->object() == thePrs->object())
403 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
404 ModuleBase_IWorkshop* theWorkshop)
407 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
408 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
410 // 2. find visualized vertices of the attribute and if the attribute of the vertex is
411 // the same, return it
412 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
413 // 2.1 get visualized results of the feature
414 const std::list<ResultPtr>& aResList = anAttributeFeature->results();
415 std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
416 for (; anIt != aLast; anIt++) {
417 AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
418 if (aAISObj.get() != NULL) {
419 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
420 // 2.2 find selected owners of a visualizedd object
421 SelectMgr_IndexedMapOfOwner aSelectedOwners;
422 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
423 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
424 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
425 if (!anOwner.IsNull()) {
426 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
427 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
428 const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
429 if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
430 // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
431 // attribute, returns the shape
432 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
433 PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
434 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
435 aBRepShape, aSketchMgr->activeSketch());
436 if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
437 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
438 aShape->setImpl(new TopoDS_Shape(aBRepShape));
450 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(
451 std::shared_ptr<ModelAPI_Feature>& theFeature,
452 const std::string& theAttribute)
454 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
455 theFeature.get(), theAttribute, SketchPlugin_Point::ID(),
456 SketchPlugin_Point::COORD_ID());
457 if (aPointAttr.get() != NULL)
458 return aPointAttr->pnt();
459 return std::shared_ptr<GeomAPI_Pnt2d>();
462 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPnt2d(QMouseEvent* theEvent,
463 ModuleBase_IViewWindow* theWindow,
464 const FeaturePtr& theSketch)
466 gp_Pnt aPnt = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
468 Handle(V3d_View) aView = theWindow->v3dView();
469 PartSet_Tools::convertTo2D(aPnt, theSketch, aView, aX, anY);
471 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY));
474 FeaturePtr findFirstCoincidenceByData(const DataPtr& theData,
475 std::shared_ptr<GeomAPI_Pnt2d> thePoint)
477 FeaturePtr aCoincident;
479 const std::set<AttributePtr>& aRefsList = theData->refsToMe();
480 std::set<AttributePtr>::const_iterator aIt;
481 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
482 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
483 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
484 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
485 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
486 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
487 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
488 aCoincident = aConstrFeature;
491 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
492 SketchPlugin_ConstraintCoincidence::ENTITY_B());
493 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
494 aCoincident = aConstrFeature;
503 FeaturePtr PartSet_Tools::findFirstCoincidence(const FeaturePtr& theFeature,
504 std::shared_ptr<GeomAPI_Pnt2d> thePoint)
506 FeaturePtr aCoincident;
507 if (theFeature.get() == NULL)
510 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
511 std::set<AttributePtr>::const_iterator aIt;
512 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
513 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
514 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
515 if (aConstrFeature && aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
516 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
517 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
518 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
519 aCoincident = aConstrFeature;
522 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
523 SketchPlugin_ConstraintCoincidence::ENTITY_B());
524 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
525 aCoincident = aConstrFeature;
532 if (!aCoincident.get()) {
533 std::list<ResultPtr> aResults = theFeature->results();
534 std::list<ResultPtr>::const_iterator aIt;
535 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
536 ResultPtr aResult = *aIt;
537 aCoincident = findFirstCoincidenceByData(aResult->data(), thePoint);
538 if (aCoincident.get())
545 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
546 QList<FeaturePtr>& theCoincidencies,
547 std::string theAttr, QList<bool>& theIsAttributes)
549 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
550 if (aOrig.get() == NULL)
553 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
556 ObjectPtr aObj = aPnt->object();
557 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
558 if (aFeature.get()) {
559 if (!theList.contains(aFeature)) {
560 theList.append(aFeature);
561 theCoincidencies.append(theStartCoin);
562 theIsAttributes.append(true); // point attribute on a feature
563 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
564 std::set<AttributePtr>::const_iterator aIt;
565 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
566 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
567 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
568 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
569 if (!theCoincidencies.contains(aConstrFeature)) {
570 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
571 if (aPnt.get() && aOrig->isEqual(aPnt)) {
572 findCoincidences(aConstrFeature, theList, theCoincidencies,
573 SketchPlugin_ConstraintCoincidence::ENTITY_A(), theIsAttributes);
574 findCoincidences(aConstrFeature, theList, theCoincidencies,
575 SketchPlugin_ConstraintCoincidence::ENTITY_B(), theIsAttributes);
583 ResultConstructionPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
585 FeaturePtr aFeature = ModelAPI_Feature::feature(aPnt->object());
586 if (!theList.contains(aFeature))
587 theList.append(aFeature);
588 theCoincidencies.append(theStartCoin);
589 theIsAttributes.append(false); // point attribute on a feature
591 const std::set<AttributePtr>& aRefsList = aResult->data()->refsToMe();
592 std::set<AttributePtr>::const_iterator aIt;
593 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
594 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
595 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
596 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
597 if (!theCoincidencies.contains(aConstrFeature)) {
598 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
599 if (aPnt.get() && aOrig->isEqual(aPnt)) {
600 findCoincidences(aConstrFeature, theList, theCoincidencies,
601 SketchPlugin_ConstraintCoincidence::ENTITY_A(), theIsAttributes);
602 findCoincidences(aConstrFeature, theList, theCoincidencies,
603 SketchPlugin_ConstraintCoincidence::ENTITY_B(), theIsAttributes);
612 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
614 std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
615 SketchPlugin_Constraint::ENTITY_A());
616 if (aPnt.get() == NULL)
617 aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
621 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
622 const TopoDS_Shape theShape,
623 FeaturePtr theSketch)
626 AttributePtr anAttribute;
627 FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
629 if (theShape.ShapeType() == TopAbs_VERTEX) {
630 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
631 if (!aVertex.IsNull()) {
632 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
633 std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
634 new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
636 // find the given point in the feature attributes
637 std::list<AttributePtr> anAttiributes =
638 aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
639 std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
640 aLast = anAttiributes.end();
641 for (; anIt != aLast && !anAttribute; anIt++) {
642 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
643 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
644 if (!aCurPoint->isInitialized())
647 std::shared_ptr<GeomAPI_Pnt> aPnt =
648 convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
649 if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
650 anAttribute = aCurPoint;
660 void PartSet_Tools::sendSubFeaturesEvent(const CompositeFeaturePtr& theComposite,
661 const Events_ID theEventId)
663 if (!theComposite.get())
666 static Events_Loop* aLoop = Events_Loop::loop();
667 for (int i = 0; i < theComposite->numberOfSubs(); i++) {
668 FeaturePtr aSubFeature = theComposite->subFeature(i);
669 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
670 aECreator->sendUpdated(aSubFeature, theEventId);
672 Events_Loop::loop()->flush(theEventId);
675 bool PartSet_Tools::isAuxiliarySketchEntity(const ObjectPtr& theObject)
677 bool isAuxiliaryFeature = false;
679 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
680 std::string anAuxiliaryAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
681 AttributeBooleanPtr anAuxiliaryAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
682 anObjectFeature->data()->attribute(anAuxiliaryAttribute));
683 if (anAuxiliaryAttr.get())
684 isAuxiliaryFeature = anAuxiliaryAttr->value();
687 return isAuxiliaryFeature;
690 bool PartSet_Tools::isIncludeIntoSketchResult(const ObjectPtr& theObject)
692 // check the feature is neither Projection nor IntersectionPoint feature
693 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
694 if (aFeature->getKind() == SketchPlugin_Projection::ID() ||
695 aFeature->getKind() == SketchPlugin_IntersectionPoint::ID())
698 // go through the references to the feature to check
699 // if it was created by Projection or Intersection
700 const std::set<AttributePtr>& aRefs = theObject->data()->refsToMe();
701 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
702 aRefIt != aRefs.end(); ++aRefIt) {
703 AttributePtr anAttr = *aRefIt;
704 std::string anIncludeToResultAttrName;
705 if (anAttr->id() == SketchPlugin_Projection::PROJECTED_FEATURE_ID())
706 anIncludeToResultAttrName = SketchPlugin_Projection::INCLUDE_INTO_RESULT();
707 else if (anAttr->id() == SketchPlugin_IntersectionPoint::INTERSECTION_POINTS_ID())
708 anIncludeToResultAttrName = SketchPlugin_IntersectionPoint::INCLUDE_INTO_RESULT();
710 if (!anIncludeToResultAttrName.empty()) {
711 // check "include into result" flag
712 FeaturePtr aParent = ModelAPI_Feature::feature(anAttr->owner());
713 return aParent->boolean(anIncludeToResultAttrName)->value();
720 ResultPtr PartSet_Tools::createFixedByExternalCenter(
721 const ObjectPtr& theObject,
722 const std::shared_ptr<GeomAPI_Edge>& theEdge,
723 ModelAPI_AttributeSelection::CenterType theType,
724 const CompositeFeaturePtr& theSketch,
726 FeaturePtr& theCreatedFeature)
728 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
732 FeaturePtr aProjectionFeature = theSketch->addFeature(SketchPlugin_Projection::ID());
733 theCreatedFeature = aProjectionFeature;
734 AttributeSelectionPtr anExternalAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
735 aProjectionFeature->attribute(SketchPlugin_Projection::EXTERNAL_FEATURE_ID()));
736 anExternalAttr->setValueCenter(aResult, theEdge, theType, theTemporary);
738 AttributeBooleanPtr anIntoResult = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>
739 (aProjectionFeature->data()->attribute(SketchPlugin_Projection::INCLUDE_INTO_RESULT()));
740 anIntoResult->setValue(SKETCH_PROJECTION_INCLUDE_INTO_RESULT);
741 aProjectionFeature->execute();
743 // if projection feature has not been created, exit
744 AttributeRefAttrPtr aRefAttr = aProjectionFeature->data()->refattr(
745 SketchPlugin_Projection::PROJECTED_FEATURE_ID());
746 if (!aRefAttr || !aRefAttr->isInitialized())
749 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
750 if (aProjection.get() && aProjection->lastResult().get())
751 return aProjection->lastResult();