1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include <PartSet_Tools.h>
21 #include <PartSet_Module.h>
22 #include <PartSet_SketcherMgr.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeRefList.h>
27 #include <ModelAPI_Document.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelAPI_Events.h>
31 #include <ModelAPI_Validator.h>
32 #include <ModelAPI_Tools.h>
34 #include <ModuleBase_IViewWindow.h>
36 #include <ModelGeomAlgo_Point2D.h>
38 #include <Events_Loop.h>
39 #include <Events_InfoMessage.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 <GeomDataAPI_Point2DArray.h>
53 #include <GeomAPI_Pln.h>
54 #include <GeomAPI_Pnt2d.h>
55 #include <GeomAPI_Pnt.h>
56 #include <GeomAPI_Edge.h>
57 #include <GeomAPI_Vertex.h>
58 #include <GeomAPI_ShapeExplorer.h>
60 #include <GeomAPI_Dir.h>
61 #include <GeomAPI_XYZ.h>
63 #include <SketchPlugin_Feature.h>
64 #include <SketchPlugin_Sketch.h>
65 #include <SketchPlugin_ConstraintCoincidence.h>
66 #include <SketchPlugin_ConstraintDistance.h>
67 #include <SketchPlugin_ConstraintLength.h>
68 #include <SketchPlugin_ConstraintRadius.h>
69 #include <SketchPlugin_ConstraintRigid.h>
70 #include <SketchPlugin_Constraint.h>
71 #include <SketchPlugin_Circle.h>
72 #include <SketchPlugin_Arc.h>
73 #include <SketchPlugin_Line.h>
74 #include <SketchPlugin_Point.h>
75 #include <SketchPlugin_Projection.h>
76 #include <SketchPlugin_IntersectionPoint.h>
78 #include <ModuleBase_IWorkshop.h>
79 #include <ModuleBase_ViewerPrs.h>
80 #include <ModuleBase_Tools.h>
82 #include <V3d_View.hxx>
84 #include <gp_Circ.hxx>
85 #include <ProjLib.hxx>
87 #include <Geom_Line.hxx>
88 #include <GeomAPI_ProjectPointOnCurve.hxx>
89 #include <BRep_Tool.hxx>
91 #include <TopoDS_Edge.hxx>
92 #include <TopoDS_Vertex.hxx>
93 #include <AIS_InteractiveObject.hxx>
94 #include <StdSelect_BRepOwner.hxx>
95 #include <SelectMgr_IndexedMapOfOwner.hxx>
96 #include <V3d_Coordinate.hxx>
98 #include <QMouseEvent>
104 const double PRECISION_TOLERANCE = 0.000001;
105 const int AIS_DEFAULT_WIDTH = 2;
106 const bool SKETCH_PROJECTION_INCLUDE_INTO_RESULT = false; // by default, it is not presented
108 int PartSet_Tools::getAISDefaultWidth()
110 return AIS_DEFAULT_WIDTH;
113 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
115 if (theView.IsNull())
118 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
119 theView->Eye(XEye, YEye, ZEye);
121 theView->At(XAt, YAt, ZAt);
122 gp_Pnt EyePoint(XEye, YEye, ZEye);
123 gp_Pnt AtPoint(XAt, YAt, ZAt);
125 gp_Vec EyeVector(EyePoint, AtPoint);
126 gp_Dir EyeDir(EyeVector);
128 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
129 Standard_Real X, Y, Z;
130 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
131 gp_Pnt ConvertedPoint(X, Y, Z);
133 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
134 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
139 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
140 Handle(V3d_View) theView,
141 double& theX, double& theY)
146 AttributeDoublePtr anAttr;
147 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
149 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
150 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
152 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
153 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
154 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
155 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
156 std::shared_ptr<GeomAPI_XYZ> anY = aNorm->xyz()->cross(aX->xyz());
158 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
159 gp_Vec aVec(anOriginPnt, thePoint);
161 if (!theView.IsNull()) {
162 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
163 theView->Eye(XEye, YEye, ZEye);
165 theView->At(XAt, YAt, ZAt);
166 gp_Pnt EyePoint(XEye, YEye, ZEye);
167 gp_Pnt AtPoint(XAt, YAt, ZAt);
169 gp_Vec anEyeVec(EyePoint, AtPoint);
170 anEyeVec.Normalize();
172 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
173 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
174 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
176 double aDen = anEyeVec * aNormalVec;
177 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
179 gp_Vec aDeltaVec = anEyeVec * aLVec;
180 aVec = aVec - aDeltaVec;
182 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
183 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
186 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::convertTo2D(FeaturePtr theSketch,
187 const std::shared_ptr<GeomAPI_Pnt>& thePnt)
189 std::shared_ptr<GeomAPI_Pnt2d> aRes;
190 if (theSketch->getKind() != SketchPlugin_Sketch::ID())
192 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
193 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
194 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
195 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
196 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
197 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
198 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
199 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
203 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY,
204 FeaturePtr theSketch)
206 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
208 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
209 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
210 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
211 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
212 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
213 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
214 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
216 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
217 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
219 return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
222 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
224 return ModelAPI_Session::get()->moduleDocument();
227 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
228 const std::string& theAttribute)
232 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
233 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
234 aData->attribute(theAttribute));
236 anAttribute->setValue(theValue);
239 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
245 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
246 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
247 aData->attribute(theAttribute));
249 aValue = anAttribute->value();
256 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
257 const std::string& theKind)
263 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
264 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
265 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
267 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
268 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
269 aFeature = FeaturePtr();
275 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
276 std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
277 std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
281 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
283 std::shared_ptr<ModelAPI_Document> aDoc = document();
284 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
287 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
289 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
290 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
291 aRef1->setAttr(thePoint1);
293 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
294 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
295 aRef2->setAttr(thePoint2);
297 // we need to flush created signal in order to coincidence is processed by solver
298 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
301 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
303 std::shared_ptr<GeomAPI_Pln> aPlane;
305 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
306 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
307 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
308 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
310 if (aNormal.get() && aNormal->isInitialized() &&
311 anOrigin.get() && anOrigin->isInitialized())
312 aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNormal->dir()));
317 void PartSet_Tools::nullifySketchPlane(CompositeFeaturePtr theSketch)
319 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
320 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
321 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
322 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
328 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
329 CompositeFeaturePtr theSketch)
331 std::shared_ptr<GeomAPI_Pnt> aPoint;
332 if (!theSketch || !thePoint2D)
335 DataPtr aData = theSketch->data();
336 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
337 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
338 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
339 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
340 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
341 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
342 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
344 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
347 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
348 const ObjectPtr& theObject,
349 CompositeFeaturePtr theSketch)
351 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
355 for (int i = 0, aNbSubs = theSketch->numberOfSubs(); i < aNbSubs; i++) {
356 FeaturePtr aFeature = theSketch->subFeature(i);
357 if (aFeature->getKind() != SketchPlugin_Projection::PROJECTED_FEATURE_ID())
359 if (aFeature->lastResult() == aResult)
365 ResultPtr PartSet_Tools::createFixedObjectByExternal(
366 const std::shared_ptr<GeomAPI_Shape>& theShape,
367 const ObjectPtr& theObject,
368 CompositeFeaturePtr theSketch,
369 const bool theTemporary,
370 FeaturePtr& theCreatedFeature)
372 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
376 FeaturePtr aProjectionFeature = theSketch->addFeature(SketchPlugin_Projection::ID());
377 theCreatedFeature = aProjectionFeature;
378 AttributeSelectionPtr anExternalAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
379 aProjectionFeature->attribute(SketchPlugin_Projection::EXTERNAL_FEATURE_ID()));
380 anExternalAttr->setValue(aResult, theShape);
382 AttributeBooleanPtr anIntoResult = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>
383 (aProjectionFeature->data()->attribute(SketchPlugin_Projection::INCLUDE_INTO_RESULT()));
384 anIntoResult->setValue(SKETCH_PROJECTION_INCLUDE_INTO_RESULT);
385 aProjectionFeature->execute();
387 // if projection feature has not been created, exit
388 AttributeRefAttrPtr aRefAttr = aProjectionFeature->data()->refattr(
389 SketchPlugin_Projection::PROJECTED_FEATURE_ID());
390 if (!aRefAttr || !aRefAttr->isInitialized())
392 // remove external feature if the attribute is not filled
393 std::set<FeaturePtr> aFeatures;
394 aFeatures.insert(aProjectionFeature);
395 ModelAPI_Tools::removeFeaturesAndReferences(aFeatures);
399 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
400 if (aProjection.get() && aProjection->lastResult().get())
401 return aProjection->lastResult();
406 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrsPtr>& theSelected,
407 const ModuleBase_ViewerPrsPtr& thePrs)
409 foreach (ModuleBase_ViewerPrsPtr aPrs, theSelected) {
410 if (aPrs->object() == thePrs->object())
416 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
417 ModuleBase_IWorkshop* theWorkshop)
420 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
421 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
423 // 2. find visualized vertices of the attribute and if the attribute of the vertex is
424 // the same, return it
425 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
426 // 2.1 get visualized results of the feature
427 const std::list<ResultPtr>& aResList = anAttributeFeature->results();
428 std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
429 for (; anIt != aLast; anIt++) {
430 AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
431 if (aAISObj.get() != NULL) {
432 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
433 // 2.2 find selected owners of a visualizedd object
434 SelectMgr_IndexedMapOfOwner aSelectedOwners;
435 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
436 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
437 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
438 if (!anOwner.IsNull()) {
439 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
440 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
441 const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
442 if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
443 // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
444 // attribute, returns the shape
445 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
446 PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
447 std::pair<AttributePtr, int> aPntAttrIndex = PartSet_Tools::findAttributeBy2dPoint(
448 anAttributeFeature, aBRepShape, aSketchMgr->activeSketch());
449 if (aPntAttrIndex.first.get() != NULL && aPntAttrIndex.first == theAttribute) {
450 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
451 aShape->setImpl(new TopoDS_Shape(aBRepShape));
463 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(
464 std::shared_ptr<ModelAPI_Feature>& theFeature,
465 const std::string& theAttribute)
467 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
468 theFeature.get(), theAttribute, SketchPlugin_Point::ID(),
469 SketchPlugin_Point::COORD_ID());
470 if (aPointAttr.get() != NULL)
471 return aPointAttr->pnt();
472 return std::shared_ptr<GeomAPI_Pnt2d>();
475 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPnt2d(QMouseEvent* theEvent,
476 ModuleBase_IViewWindow* theWindow,
477 const FeaturePtr& theSketch)
479 gp_Pnt aPnt = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
481 Handle(V3d_View) aView = theWindow->v3dView();
482 PartSet_Tools::convertTo2D(aPnt, theSketch, aView, aX, anY);
484 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY));
487 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPnt2d(const Handle(V3d_View)& theView,
488 const TopoDS_Shape& theShape,
489 const FeaturePtr& theSketch)
491 GeomPnt2dPtr aPoint2D;
492 if (!theShape.IsNull() && theShape.ShapeType() == TopAbs_VERTEX) {
493 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
494 if (!aVertex.IsNull()) {
495 // the case when the point is taken from the existing vertex
496 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
498 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, aY);
499 aPoint2D.reset(new GeomAPI_Pnt2d(aX, aY));
505 FeaturePtr findFirstCoincidenceByData(const DataPtr& theData,
506 std::shared_ptr<GeomAPI_Pnt2d> thePoint)
508 FeaturePtr aCoincident;
510 const std::set<AttributePtr>& aRefsList = theData->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->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;
534 FeaturePtr PartSet_Tools::findFirstCoincidence(const FeaturePtr& theFeature,
535 std::shared_ptr<GeomAPI_Pnt2d> thePoint)
537 FeaturePtr aCoincident;
538 if (theFeature.get() == NULL)
541 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
542 std::set<AttributePtr>::const_iterator aIt;
543 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
544 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
545 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
546 if (aConstrFeature && aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
547 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
548 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
549 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
550 aCoincident = aConstrFeature;
553 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
554 SketchPlugin_ConstraintCoincidence::ENTITY_B());
555 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
556 aCoincident = aConstrFeature;
563 if (!aCoincident.get()) {
564 std::list<ResultPtr> aResults = theFeature->results();
565 std::list<ResultPtr>::const_iterator aIt;
566 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
567 ResultPtr aResult = *aIt;
568 aCoincident = findFirstCoincidenceByData(aResult->data(), thePoint);
569 if (aCoincident.get())
576 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
577 QList<FeaturePtr>& theCoincidencies,
578 std::string theAttr, QList<bool>& theIsAttributes)
580 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
581 if (aOrig.get() == NULL)
584 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
587 ObjectPtr aObj = aPnt->object();
588 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
589 if (aFeature.get()) {
590 if (!theList.contains(aFeature)) {
591 theList.append(aFeature);
592 theCoincidencies.append(theStartCoin);
593 theIsAttributes.append(true); // point attribute on a feature
594 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
595 std::set<AttributePtr>::const_iterator aIt;
596 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
597 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
598 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
599 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
600 if (!theCoincidencies.contains(aConstrFeature)) {
601 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
602 if (aPnt.get() && aOrig->isEqual(aPnt)) {
603 findCoincidences(aConstrFeature, theList, theCoincidencies,
604 SketchPlugin_ConstraintCoincidence::ENTITY_A(), theIsAttributes);
605 findCoincidences(aConstrFeature, theList, theCoincidencies,
606 SketchPlugin_ConstraintCoincidence::ENTITY_B(), theIsAttributes);
614 ResultConstructionPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
616 FeaturePtr aFeature = ModelAPI_Feature::feature(aPnt->object());
617 if (!theList.contains(aFeature))
618 theList.append(aFeature);
619 theCoincidencies.append(theStartCoin);
620 theIsAttributes.append(false); // point attribute on a feature
622 const std::set<AttributePtr>& aRefsList = aResult->data()->refsToMe();
623 std::set<AttributePtr>::const_iterator aIt;
624 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
625 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
626 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
627 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
628 if (!theCoincidencies.contains(aConstrFeature)) {
629 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
630 if (aPnt.get() && aOrig->isEqual(aPnt)) {
631 findCoincidences(aConstrFeature, theList, theCoincidencies,
632 SketchPlugin_ConstraintCoincidence::ENTITY_A(), theIsAttributes);
633 findCoincidences(aConstrFeature, theList, theCoincidencies,
634 SketchPlugin_ConstraintCoincidence::ENTITY_B(), theIsAttributes);
643 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
645 std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
646 SketchPlugin_Constraint::ENTITY_A());
647 if (aPnt.get() == NULL)
648 aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
655 PointWrapper(AttributePtr theAttribute)
656 : myPoint(std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute)),
657 myArray(std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(theAttribute))
660 int size() const { return myPoint.get() ? 1 : (myArray.get() ? myArray->size() : 0); }
662 GeomPointPtr point(int theIndex, FeaturePtr theSketch)
666 aP2d = myPoint->pnt();
667 else if (myArray.get())
668 aP2d = myArray->pnt(theIndex);
672 aP3d = PartSet_Tools::convertTo3D(aP2d->x(), aP2d->y(), theSketch);
676 bool isArray() const { return myArray.get(); }
679 AttributePoint2DPtr myPoint;
680 AttributePoint2DArrayPtr myArray;
683 std::pair<AttributePtr, int> PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
684 const TopoDS_Shape theShape,
685 FeaturePtr theSketch)
688 AttributePtr anAttribute;
689 int aPointIndex = -1;
690 FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
692 if (theShape.ShapeType() == TopAbs_VERTEX) {
693 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
694 if (!aVertex.IsNull()) {
695 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
696 std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
697 new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
699 // find the given point in the feature attributes
700 std::list<AttributePtr> anAttiributes =
701 aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
702 std::list<AttributePtr> anArrays =
703 aFeature->data()->attributes(GeomDataAPI_Point2DArray::typeId());
704 anAttiributes.insert(anAttiributes.end(), anArrays.begin(), anArrays.end());
706 std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
707 aLast = anAttiributes.end();
708 double aMinDistance = 1.e-6; // searching for point with minimal distance and < 1.e-6
709 for (; anIt != aLast && !anAttribute; anIt++) {
710 PointWrapper aWrapper(*anIt);
711 for (int anIndex = 0, aSize = aWrapper.size(); anIndex < aSize; ++anIndex) {
712 std::shared_ptr<GeomAPI_Pnt> aPnt = aWrapper.point(anIndex, theSketch);
714 double aDistance = aPnt->distance(aValue);
715 if (aDistance < aMinDistance) {
717 if (aWrapper.isArray())
718 aPointIndex = anIndex;
719 aMinDistance = aPnt->distance(aValue);
727 return std::pair<AttributePtr, int>(anAttribute, aPointIndex);
730 void PartSet_Tools::sendSubFeaturesEvent(const CompositeFeaturePtr& theComposite,
731 const Events_ID theEventId)
733 if (!theComposite.get())
736 static Events_Loop* aLoop = Events_Loop::loop();
737 int aNumberOfSubs = theComposite->numberOfSubs();
738 for (int i = 0; i < aNumberOfSubs; i++) {
739 FeaturePtr aSubFeature = theComposite->subFeature(i);
740 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
741 aECreator->sendUpdated(aSubFeature, theEventId);
743 Events_Loop::loop()->flush(theEventId);
746 bool PartSet_Tools::isAuxiliarySketchEntity(const ObjectPtr& theObject)
748 bool isAuxiliaryFeature = false;
750 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
751 std::string anAuxiliaryAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
752 AttributeBooleanPtr anAuxiliaryAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
753 anObjectFeature->data()->attribute(anAuxiliaryAttribute));
754 if (anAuxiliaryAttr.get())
755 isAuxiliaryFeature = anAuxiliaryAttr->value();
758 return isAuxiliaryFeature;
761 bool PartSet_Tools::isIncludeIntoSketchResult(const ObjectPtr& theObject)
763 // check the feature is neither Projection nor IntersectionPoint feature
764 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
765 if (aFeature->getKind() == SketchPlugin_Projection::ID() ||
766 aFeature->getKind() == SketchPlugin_IntersectionPoint::ID())
769 // go through the references to the feature to check
770 // if it was created by Projection or Intersection
771 const std::set<AttributePtr>& aRefs = theObject->data()->refsToMe();
772 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
773 aRefIt != aRefs.end(); ++aRefIt) {
774 AttributePtr anAttr = *aRefIt;
775 std::string anIncludeToResultAttrName;
776 if (anAttr->id() == SketchPlugin_Projection::PROJECTED_FEATURE_ID())
777 anIncludeToResultAttrName = SketchPlugin_Projection::INCLUDE_INTO_RESULT();
778 else if (anAttr->id() == SketchPlugin_IntersectionPoint::INTERSECTION_POINTS_ID())
779 anIncludeToResultAttrName = SketchPlugin_IntersectionPoint::INCLUDE_INTO_RESULT();
781 if (!anIncludeToResultAttrName.empty()) {
782 // check "include into result" flag
783 FeaturePtr aParent = ModelAPI_Feature::feature(anAttr->owner());
784 return aParent->boolean(anIncludeToResultAttrName)->value();
791 ResultPtr PartSet_Tools::createFixedByExternalCenter(
792 const ObjectPtr& theObject,
793 const std::shared_ptr<GeomAPI_Edge>& theEdge,
794 ModelAPI_AttributeSelection::CenterType theType,
795 const CompositeFeaturePtr& theSketch,
797 FeaturePtr& theCreatedFeature)
799 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
803 FeaturePtr aProjectionFeature = theSketch->addFeature(SketchPlugin_Projection::ID());
804 theCreatedFeature = aProjectionFeature;
805 AttributeSelectionPtr anExternalAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
806 aProjectionFeature->attribute(SketchPlugin_Projection::EXTERNAL_FEATURE_ID()));
807 anExternalAttr->setValueCenter(aResult, theEdge, theType, theTemporary);
809 AttributeBooleanPtr anIntoResult = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>
810 (aProjectionFeature->data()->attribute(SketchPlugin_Projection::INCLUDE_INTO_RESULT()));
811 anIntoResult->setValue(SKETCH_PROJECTION_INCLUDE_INTO_RESULT);
812 aProjectionFeature->execute();
814 // if projection feature has not been created, exit
815 AttributeRefAttrPtr aRefAttr = aProjectionFeature->data()->refattr(
816 SketchPlugin_Projection::PROJECTED_FEATURE_ID());
817 if (!aRefAttr || !aRefAttr->isInitialized())
820 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
821 if (aProjection.get() && aProjection->lastResult().get())
822 return aProjection->lastResult();
827 void PartSet_Tools::getFirstAndLastIndexInFolder(const ObjectPtr& theFolder,
828 int& theFirst, int& theLast)
833 DocumentPtr aDoc = theFolder->document();
834 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theFolder);
838 AttributeReferencePtr aFirstFeatAttr =
839 aFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
840 if (!aFirstFeatAttr.get())
842 FeaturePtr aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
843 if (!aFirstFeatureInFolder.get())
846 FeaturePtr aLastFeatureInFolder = aFolder->lastVisibleFeature();
847 if (!aLastFeatureInFolder.get())
850 theFirst = aDoc->index(aFirstFeatureInFolder);
851 theLast = aDoc->index(aLastFeatureInFolder);
855 void PartSet_Tools::getDefaultColor(ObjectPtr theObject, const bool isEmptyColorValid,
856 std::vector<int>& theColor)
859 // get default color from the preferences manager for the given result
860 if (theColor.empty()) {
861 std::string aSection, aName, aDefault;
862 theObject->colorConfigInfo(aSection, aName, aDefault);
863 if (!aSection.empty() && !aName.empty()) {
864 theColor = Config_PropManager::color(aSection, aName);
867 if (!isEmptyColorValid && theColor.empty()) {
868 // all AIS objects, where the color is not set, are in black.
869 // The color should be defined in XML or set in the attribute
870 theColor = Config_PropManager::color("Visualization", "object_default_color");
871 Events_InfoMessage("PartSet_Tools",
872 "A default color is not defined in the preferences for this result type").send();
876 double PartSet_Tools::getDefaultDeflection(const ObjectPtr& theObject)
878 double aDeflection = -1;
879 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
881 bool isConstruction = false;
883 std::string aResultGroup = aResult->groupName();
884 if (aResultGroup == ModelAPI_ResultConstruction::group())
885 isConstruction = true;
886 else if (aResultGroup == ModelAPI_ResultBody::group()) {
887 GeomShapePtr aGeomShape = aResult->shape();
888 if (aGeomShape.get()) {
889 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
890 // correction of deviation for them should not influence to the application performance
891 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
892 isConstruction = !anExp.more();
896 aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
898 aDeflection = Config_PropManager::real("Visualization", "body_deflection");
904 double PartSet_Tools::getDefaultTransparency()
906 return Config_PropManager::integer("Visualization", "shaper_default_transparency") / 100.;