1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_WidgetPoint2D.cpp
4 // Created: 25 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include "PartSet_WidgetPoint2d.h"
8 #include <PartSet_Tools.h>
9 #include <PartSet_Module.h>
10 #include <PartSet_SketcherReetntrantMgr.h>
12 #include <XGUI_Tools.h>
13 #include <XGUI_Workshop.h>
14 #include <XGUI_Displayer.h>
16 #include <ModuleBase_ParamSpinBox.h>
17 #include <ModuleBase_Tools.h>
18 #include <ModuleBase_IViewer.h>
19 #include <ModuleBase_IViewWindow.h>
20 #include <ModuleBase_ISelection.h>
21 #include <ModuleBase_ViewerPrs.h>
22 #include <ModuleBase_WidgetValidator.h>
23 #include <ModuleBase_LabelValue.h>
25 #include <Config_Keywords.h>
26 #include <Config_WidgetAPI.h>
28 #include <Events_Loop.h>
29 #include <ModelAPI_Events.h>
30 #include <ModelAPI_AttributeBoolean.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32 #include <ModelAPI_AttributeRefList.h>
33 #include <ModelAPI_Validator.h>
34 #include <ModelAPI_Session.h>
36 #include <ModelAPI_Feature.h>
37 #include <ModelAPI_Data.h>
38 #include <ModelAPI_Object.h>
39 #include <GeomDataAPI_Point2D.h>
40 #include <GeomAPI_Pnt2d.h>
42 #include <GeomAPI_ShapeExplorer.h>
43 #include <GeomAPI_Vertex.h>
45 #include <SketchPlugin_Feature.h>
46 #include <SketchPlugin_ConstraintCoincidence.h>
47 #include <SketchPlugin_Line.h>
48 #include <SketchPlugin_Arc.h>
49 #include <SketchPlugin_Circle.h>
50 #include <SketchPlugin_Point.h>
53 #include <QGridLayout>
56 #include <QMouseEvent>
57 #include <QApplication>
60 #include <TopoDS_Vertex.hxx>
61 #include <BRep_Tool.hxx>
66 const double MaxCoordinate = 1e12;
68 static QStringList MyFeaturesForCoincedence;
70 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
71 ModuleBase_IWorkshop* theWorkshop,
72 const Config_WidgetAPI* theData)
73 : ModuleBase_ModelWidget(theParent, theData), myWorkshop(theWorkshop),
74 myValueIsCashed(false), myIsFeatureVisibleInCash(true),
75 myXValueInCash(0), myYValueInCash(0)
77 myRefAttribute = theData->getProperty("reference_attribute");
78 if (MyFeaturesForCoincedence.isEmpty()) {
79 MyFeaturesForCoincedence << SketchPlugin_Line::ID().c_str()
80 << SketchPlugin_Arc::ID().c_str()
81 << SketchPlugin_Point::ID().c_str()
82 << SketchPlugin_Circle::ID().c_str();
85 // the control should accept the focus, so the boolean flag is corrected to be true
86 myIsObligatory = true;
87 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
88 myGroupBox = new QGroupBox(aPageName, theParent);
89 myGroupBox->setFlat(false);
91 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
93 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
94 ModuleBase_Tools::adjustMargins(aGroupLay);
95 aGroupLay->setSpacing(2);
96 aGroupLay->setColumnStretch(1, 1);
98 QLabel* aLabel = new QLabel(myGroupBox);
100 myXSpin = new ModuleBase_LabelValue(myGroupBox, tr("X"));
101 //ModuleBase_ParamSpinBox(myGroupBox);
102 //myXSpin->setAcceptVariables(aAcceptVariables);
103 //myXSpin->setMinimum(-DBL_MAX);
104 //myXSpin->setMaximum(DBL_MAX);
105 //myXSpin->setToolTip(tr("X"));
106 aGroupLay->addWidget(myXSpin, 0, 1);
108 //connect(myXSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
109 //myXSpin->setValueEnabled(isValueEnabled());
112 //QLabel* aLabel = new QLabel(myGroupBox);
113 //aLabel->setText(tr("Y "));
114 //aGroupLay->addWidget(aLabel, 1, 0);
116 myYSpin = new ModuleBase_LabelValue(myGroupBox, tr("Y"));
117 //ModuleBase_ParamSpinBox(myGroupBox);
118 //myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
119 //myYSpin->setAcceptVariables(aAcceptVariables);
120 //myYSpin->setMinimum(-DBL_MAX);
121 //myYSpin->setMaximum(DBL_MAX);
122 //myYSpin->setToolTip(tr("Y"));
123 aGroupLay->addWidget(myYSpin, 1, 1);
125 //connect(myYSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
126 //myYSpin->setValueEnabled(isValueEnabled());
128 QVBoxLayout* aLayout = new QVBoxLayout(this);
129 ModuleBase_Tools::zeroMargins(aLayout);
130 aLayout->addWidget(myGroupBox);
133 myWidgetValidator = new ModuleBase_WidgetValidator(this, myWorkshop);
136 bool PartSet_WidgetPoint2D::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
140 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
141 if (aModule->sketchReentranceMgr()->isInternalEditActive())
142 return true; /// when internal edit is started a new feature is created. I has not results, AIS
144 /// the selection is not possible if the current feature has no presentation for the current
145 /// attribute not in AIS not in results. If so, no object in current feature where make
146 /// coincidence, so selection is not necessary
147 GeomShapePtr anAISShape;
148 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(myFeature);
151 anAIS = aPrs->getAISObject(anAIS);
153 anAISShape = anAIS->getShape();
156 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = myFeature->results();
157 if (!anAISShape.get() && aResults.empty())
160 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
161 if (!aRefAttr.get()) {
162 bool aFoundPoint = false;
163 /// Avoid coincidence build to passed point. Coincidence is build later only if there are no
164 /// reference attribute.
165 /// The condition is that the selected feature has shape that has after explore a point
166 /// equal to clicked one.
167 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
168 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
169 aData->attribute(attributeID()));
170 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
171 if (anAISShape.get())
172 aFoundPoint = shapeExploreHasVertex(anAISShape, aPoint, mySketch);
174 /// analysis of results
175 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
176 for (; aRIter != aResults.cend() && !aFoundPoint; aRIter++) {
177 ResultPtr aResult = *aRIter;
178 if (aResult.get() && aResult->shape().get()) {
179 GeomShapePtr aShape = aResult->shape();
180 aFoundPoint = shapeExploreHasVertex(aShape, aPoint, mySketch);
188 bool PartSet_WidgetPoint2D::resetCustom()
191 if (!isUseReset() || isComputedDefault()
192 /*|| myXSpin->hasVariable() || myYSpin->hasVariable()*/) {
196 if (myValueIsCashed) {
197 // if the restored value should be hidden, aDone = true to set
198 // reset state for the widget in the parent
199 aDone = restoreCurentValue();
200 emit objectUpdated();
204 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
205 // it is important to block the spin box control in order to do not through out the
206 // locking of the validating state.
207 myXSpin->setValue(isOk ? aDefValue : 0.0);
208 myYSpin->setValue(isOk ? aDefValue : 0.0);
210 //ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
211 //ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
219 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
223 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
224 const bool theToValidate)
227 if (theValues.empty())
230 ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
231 GeomShapePtr aShape = aValue->shape();
232 if (aShape.get() && !aShape->isNull()) {
233 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
235 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
236 if (getPoint2d(aView, aTDShape, aX, aY)) {
237 isDone = setPoint(aX, aY);
238 setConstraintToPoint(aX, aY);
244 void PartSet_WidgetPoint2D::selectContent()
246 // myXSpin->selectAll();
249 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
251 if (fabs(theX) >= MaxCoordinate)
253 if (fabs(theY) >= MaxCoordinate)
256 myXSpin->setValue(theX);
257 myYSpin->setValue(theY);
259 //ModuleBase_Tools::setSpinValue(myXSpin, theX);
260 //ModuleBase_Tools::setSpinValue(myYSpin, theY);
266 bool PartSet_WidgetPoint2D::storeValueCustom()
268 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
269 if (!aData) // can be on abort of sketcher element
271 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
272 aData->attribute(attributeID()));
274 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
275 bool isBlocked = that->blockSignals(true);
276 bool isImmutable = aPoint->setImmutable(true);
278 // if text is not empty then setValue will be ignored
279 // so we should set the text at first
280 //aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
281 // myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
282 //aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
283 // !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
284 aPoint->setValue(myXSpin->value(), myYSpin->value());
286 // after movement the solver will call the update event: optimization
287 moveObject(myFeature);
288 aPoint->setImmutable(isImmutable);
289 that->blockSignals(isBlocked);
294 bool PartSet_WidgetPoint2D::restoreValueCustom()
296 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
297 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
298 aData->attribute(attributeID()));
299 double aValueX = aPoint->isInitialized() ? aPoint->x() : 0.;
300 double aValueY = aPoint->isInitialized() ? aPoint->y() : 0.;
301 myXSpin->setValue(aValueX);
302 myYSpin->setValue(aValueY);
307 void PartSet_WidgetPoint2D::storeCurentValue()
309 // do not use cash if a variable is used
310 //if (myXSpin->hasVariable() || myYSpin->hasVariable())
313 myValueIsCashed = true;
314 myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
315 XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
316 myXValueInCash = myXSpin->value();
317 myYValueInCash = myYSpin->value();
320 bool PartSet_WidgetPoint2D::restoreCurentValue()
322 bool aRestoredAndHidden = true;
324 bool isVisible = myIsFeatureVisibleInCash;
325 // fill the control widgets by the cashed value
327 myValueIsCashed = false;
328 myIsFeatureVisibleInCash = true;
329 myXSpin->setValue(myXValueInCash);
330 myYSpin->setValue(myYValueInCash);
331 //ModuleBase_Tools::setSpinValue(myXSpin, myXValueInCash);
332 //ModuleBase_Tools::setSpinValue(myYSpin, myYValueInCash);
334 // store value to the model
337 setValueState(Stored);
338 aRestoredAndHidden = false;
341 aRestoredAndHidden = true;
343 return aRestoredAndHidden;
346 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
348 QList<QWidget*> aControls;
349 aControls.append(myXSpin);
350 aControls.append(myYSpin);
355 void PartSet_WidgetPoint2D::activateCustom()
358 aModes << TopAbs_VERTEX;
359 aModes << TopAbs_EDGE;
360 myWorkshop->activateSubShapesSelection(aModes);
362 if (!isEditingMode()) {
363 FeaturePtr aFeature = feature();
364 if (aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID())
369 void PartSet_WidgetPoint2D::setHighlighted(bool isHighlighted)
373 void PartSet_WidgetPoint2D::deactivate()
375 // the value of the control should be stored to model if it was not
376 // initialized yet. It is important when we leave this control by Tab key.
377 // It should not be performed by the widget activation as the preview
378 // is visualized with default value. Line point is moved to origin.
379 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
380 if (anAttribute && !anAttribute->isInitialized())
383 ModuleBase_ModelWidget::deactivate();
384 myWorkshop->deactivateSubShapesSelection();
387 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
388 const TopoDS_Shape& theShape,
389 double& theX, double& theY) const
391 if (!theShape.IsNull()) {
392 if (theShape.ShapeType() == TopAbs_VERTEX) {
393 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
394 if (!aVertex.IsNull()) {
395 // A case when point is taken from existing vertex
396 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
397 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
405 bool PartSet_WidgetPoint2D::setConstraintToPoint(double theClickedX, double theClickedY)
407 FeaturePtr aFeature = feature();
408 std::string anAttribute = attributeID();
413 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
414 new GeomAPI_Pnt2d(theClickedX, theClickedY));
415 AttributePoint2DPtr aClickedFeaturePoint = findFirstEqualPointInSketch(mySketch,
416 aFeature, aClickedPoint);
417 if (!aClickedFeaturePoint.get())
420 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
422 aRefAttr->setAttr(aClickedFeaturePoint);
424 // find a feature point by the selection mode
425 AttributePoint2DPtr aFeaturePoint;
426 if (aFeature->isMacro()) {
427 // the macro feature will be removed after the operation is stopped, so we need to build
428 // coicidence to possible sub-features
429 aFeaturePoint = findFirstEqualPointInArgumentFeatures(aFeature, aClickedPoint);
432 aFeaturePoint = std::dynamic_pointer_cast<
433 GeomDataAPI_Point2D>(aFeature->data()->attribute(anAttribute));
435 if (!aFeaturePoint.get())
438 PartSet_Tools::createConstraint(mySketch, aClickedFeaturePoint, aFeaturePoint);
443 bool PartSet_WidgetPoint2D::setConstraintToObject(const ObjectPtr& theObject)
445 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
446 if (aRefAttr.get()) {
447 aRefAttr->setObject(theObject);
450 AttributePoint2DPtr aFeaturePoint;
452 if (feature()->isMacro()) {
453 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
454 AttributePoint2DPtr anAttrPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
455 if (anAttrPoint.get()) {
456 // the macro feature will be removed after the operation is stopped, so we need to build
457 // coicidence to possible sub-features
458 aFeaturePoint = findFirstEqualPointInArgumentFeatures(feature(),
463 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
464 aFeaturePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
466 if (!aFeaturePoint.get())
469 // Create point-edge coincedence
470 FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
471 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
473 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
474 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
476 aRef1->setAttr(aFeaturePoint);
478 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
479 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
480 aRef2->setObject(theObject);
482 // we need to flush created signal in order to coincidence is processed by solver
483 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
488 void PartSet_WidgetPoint2D::mouseReleased(ModuleBase_IViewWindow* theWindow, QMouseEvent* theEvent)
490 // the contex menu release by the right button should not be processed by this widget
491 if (theEvent->button() != Qt::LeftButton)
494 ModuleBase_ISelection* aSelection = myWorkshop->selection();
495 Handle(V3d_View) aView = theWindow->v3dView();
497 QList<ModuleBase_ViewerPrsPtr> aList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
498 ModuleBase_ViewerPrsPtr aFirstValue =
499 aList.size() > 0 ? aList.first() : ModuleBase_ViewerPrsPtr();
500 if (!aFirstValue.get() && myPreSelected.get()) {
501 aFirstValue = myPreSelected;
503 // if we have selection and use it
504 if (aFirstValue.get() && isValidSelectionCustom(aFirstValue) &&
505 aFirstValue->shape().get()) { /// Trihedron Axis may be selected, but shape is empty
506 GeomShapePtr aGeomShape = aFirstValue->shape();
507 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
508 ObjectPtr aObject = aFirstValue->object();
510 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
511 bool anExternal = false;
512 std::shared_ptr<SketchPlugin_Feature> aSPFeature;
513 if (aSelectedFeature.get() != NULL)
514 aSPFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
516 ResultPtr aFixedObject;
517 bool aSketchExternalFeature = aSPFeature.get() && aSPFeature->isExternal();
518 if ((!aSPFeature && !aShape.IsNull()) || aSketchExternalFeature) {
519 aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
520 if (aSketchExternalFeature && !aFixedObject.get()) {/// local selection on external feature
525 if (!aFixedObject.get())
526 aFixedObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
531 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
532 // do not create a constraint to the point, which already used by the feature
533 // if the feature contains the point, focus is not switched
537 if (getPoint2d(aView, aShape, aX, aY))
540 setValueState(Stored); // in case of edge selection, Apply state should also be updated
541 bool anOrphanPoint = aShape.ShapeType() == TopAbs_VERTEX ||
542 isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
544 // we should not stop reentrant operation on external objects because
545 anOrphanPoint = true;
546 // they are not participate in the contour creation excepting external vertices
547 if (aShape.ShapeType() == TopAbs_VERTEX) {
548 FeaturePtr aFixedFeature = ModelAPI_Feature::feature(aFixedObject);
549 if (aFixedFeature.get() && aFixedFeature->getKind() == SketchPlugin_Point::ID()) {
550 anOrphanPoint = isOrphanPoint(aFixedFeature, mySketch, aX, aY);
554 if (aFixedObject.get())
555 setConstraintToObject(aFixedObject);
556 // fignal updated should be flushed in order to visualize possible created
557 // external objects e.g. selection of trihedron axis when input end arc point
558 updateObject(feature());
561 emit vertexSelected(); // it stops the reentrant operation
563 emit focusOutWidget(this);
568 bool isProcessed = false;
569 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
570 // when the point is selected, the coordinates of the point should be set into the attribute
571 // if the feature contains the point, focus is not switched
575 bool anOrphanPoint = isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
576 // do not set a coincidence constraint in the attribute if the feature contains a point
577 // with the same coordinates. It is important for line creation in order to do not set
578 // the same constraints for the same points, oterwise the result line has zero length.
579 bool isAuxiliaryFeature = false;
580 if (getPoint2d(aView, aShape, aX, aY)) {
582 setConstraintToPoint(aX, aY);
584 else if (aShape.ShapeType() == TopAbs_EDGE) {
585 // point is taken from mouse event and set in attribute. It should be done before setting
586 // coinident constraint to the external line. If a point is created, it should be in
587 // the mouse clicked point
588 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
589 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, aY);
591 setConstraintToObject(aObject);
592 setValueState(Stored); // in case of edge selection, Apply state should also be updated
593 isAuxiliaryFeature = PartSet_Tools::isAuxiliarySketchEntity(aObject);
595 // it is important to perform updateObject() in order to the current value is
596 // processed by Sketch Solver. Test case: line is created from a previous point
597 // to some distance, but in the area of the highlighting of the point. Constraint
598 // coincidence is created, after the solver is performed, the distance between the
599 // points of the line becomes less than the tolerance. Validator of the line returns
600 // false, the line will be aborted, but sketch stays valid.
601 updateObject(feature());
602 if (!anOrphanPoint && !anExternal && !isAuxiliaryFeature)
603 emit vertexSelected();
604 emit focusOutWidget(this);
608 // End of Bug dependent fragment
610 // A case when point is taken from mouse event
611 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
613 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
615 // if the feature contains the point, focus is not switched
616 if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
619 emit focusOutWidget(this);
623 void PartSet_WidgetPoint2D::setPreSelection(
624 const std::shared_ptr<ModuleBase_ViewerPrs>& thePreSelected,
625 ModuleBase_IViewWindow* theWnd,
626 QMouseEvent* theEvent)
628 myPreSelected = thePreSelected;
629 mouseReleased(theWnd, theEvent);
630 myPreSelected = ModuleBase_ViewerPrsPtr();
633 void PartSet_WidgetPoint2D::mouseMoved(ModuleBase_IViewWindow* theWindow, QMouseEvent* theEvent)
635 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
637 if (isEditingMode() || aModule->sketchReentranceMgr()->isInternalEditStarted())
640 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
643 PartSet_Tools::convertTo2D(aPoint, mySketch, theWindow->v3dView(), aX, anY);
644 if (myState != ModifiedInViewer)
646 // we need to block the value state change
647 bool isBlocked = blockValueState(true);
649 blockValueState(isBlocked);
650 setValueState(ModifiedInViewer);
653 double PartSet_WidgetPoint2D::x() const
655 return myXSpin->value();
658 double PartSet_WidgetPoint2D::y() const
660 return myYSpin->value();
664 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
665 double theX, double theY)
667 bool aPointIsFound = false;
669 if (feature()->getKind() != SketchPlugin_Line::ID())
670 return aPointIsFound;
672 AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
674 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
675 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
676 std::list<AttributePtr> anAttributes =
677 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
678 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
679 for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
680 AttributePoint2DPtr aPoint2DAttribute =
681 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
682 if (aPoint2DAttribute == aWidgetAttribute)
684 if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
685 aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
688 return aPointIsFound;
691 void PartSet_WidgetPoint2D::initializeValueByActivate()
695 /*void PartSet_WidgetPoint2D::onValuesChanged()
697 emit valuesChanged();
700 bool PartSet_WidgetPoint2D::processEnter()
703 /*bool isModified = getValueState() == ModifiedInPP;
705 bool isXModified = myXSpin->hasFocus();
706 emit valuesChanged();
708 myXSpin->selectAll();
710 myYSpin->selectAll();
715 bool PartSet_WidgetPoint2D::useSelectedShapes() const
720 bool PartSet_WidgetPoint2D::isOrphanPoint(const FeaturePtr& theFeature,
721 const CompositeFeaturePtr& theSketch,
722 double theX, double theY)
724 bool anOrphanPoint = false;
725 if (theFeature.get()) {
726 AttributePoint2DPtr aPointAttr;
727 std::string aFeatureKind = theFeature->getKind();
728 if (aFeatureKind == SketchPlugin_Point::ID())
729 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
730 theFeature->attribute(SketchPlugin_Point::COORD_ID()));
731 else if (aFeatureKind == SketchPlugin_Circle::ID())
732 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
733 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
735 else if (aFeatureKind == SketchPlugin_Arc::ID())
736 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
737 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
739 /// check that the geometry point with the given coordinates is the checked point
740 /// e.g. in arc the (x,y) point can not coicide to the center point and it automatically
741 /// means that this point is not an orphant one.
742 if (aPointAttr.get()) {
743 std::shared_ptr<GeomAPI_Pnt2d> aCheckedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
744 new GeomAPI_Pnt2d(theX, theY));
745 if (!aCheckedPoint->isEqual(aPointAttr->pnt()))
746 return anOrphanPoint;
749 if (aPointAttr.get()) {
750 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
751 // we need to find coincidence features in results also, because external object(point)
752 // uses refs to me in another feature.
753 FeaturePtr aCoincidence = PartSet_Tools::findFirstCoincidence(theFeature, aPoint);
754 anOrphanPoint = true;
755 // if there is at least one concident line to the point, the point is not an orphant
756 if (aCoincidence.get()) {
757 QList<FeaturePtr> aCoinsideLines;
758 QList<FeaturePtr> aCoins;
759 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
760 SketchPlugin_ConstraintCoincidence::ENTITY_A());
761 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
762 SketchPlugin_ConstraintCoincidence::ENTITY_B());
763 QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(),
764 aLast = aCoinsideLines.end();
765 for (; anIt != aLast && anOrphanPoint; anIt++) {
766 anOrphanPoint = (*anIt)->getKind() != SketchPlugin_Line::ID();
771 return anOrphanPoint;
774 bool PartSet_WidgetPoint2D::shapeExploreHasVertex(const GeomShapePtr& theShape,
775 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
776 const CompositeFeaturePtr& theSketch)
778 std::shared_ptr<GeomAPI_Pnt> aPoint = PartSet_Tools::point3D(thePoint, theSketch);
780 bool aContainPoint = false;
781 GeomAPI_ShapeExplorer anExp(theShape, GeomAPI_Shape::VERTEX);
782 for(; anExp.more() && !aContainPoint; anExp.next()) {
783 std::shared_ptr<GeomAPI_Shape> aVertexInCompSolid = anExp.current();
784 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aVertexInCompSolid));
786 aContainPoint = aPoint->isEqual(aVertex->point());
788 return aContainPoint;
791 AttributeRefAttrPtr PartSet_WidgetPoint2D::attributeRefAttr() const
793 AttributeRefAttrPtr anAttribute;
794 if (myRefAttribute.empty())
797 AttributePtr anAttributeRef = feature()->attribute(myRefAttribute);
798 if (!anAttributeRef.get())
801 return std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttributeRef);
804 std::shared_ptr<GeomDataAPI_Point2D> PartSet_WidgetPoint2D::findFirstEqualPointInArgumentFeatures(
805 const FeaturePtr& theFeature, const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
807 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
809 // may be feature is not updated yet, execute is not performed and references features
810 // are not created. Case: rectangle macro feature
811 ModuleBase_Tools::flushUpdated(theFeature);
813 std::list<AttributePtr> anAttributes = theFeature->data()->attributes(
814 ModelAPI_AttributeRefList::typeId());
815 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
816 for (; anIt != aLast && !aFeaturePoint.get(); anIt++) {
817 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
818 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
819 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFeaturePoint.get(); i++) {
820 ObjectPtr anObject = aCurSelList->object(i);
821 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
823 aFeaturePoint = findFirstEqualPoint(aFeature, thePoint);
826 return aFeaturePoint;
829 std::shared_ptr<GeomDataAPI_Point2D> PartSet_WidgetPoint2D::findFirstEqualPoint(
830 const FeaturePtr& theFeature,
831 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
833 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
835 // find the given point in the feature attributes
836 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
837 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
838 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
839 aLast = anAttiributes.end();
840 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
842 for (; anIt != aLast && !aFPoint; anIt++) {
843 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
844 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
845 if (aCurPoint && aCurPoint->isInitialized() &&
846 aValidators->isCase(theFeature, aCurPoint->id()) &&
847 (aCurPoint->pnt()->distance(thePoint) < Precision::Confusion())) {
855 std::shared_ptr<GeomDataAPI_Point2D> PartSet_WidgetPoint2D::findFirstEqualPointInSketch(
856 const CompositeFeaturePtr& theSketch,
857 const FeaturePtr& theSkipFeature,
858 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
860 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
861 // the constraint is created between the feature point and the found sketch point
862 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
863 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
864 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
866 std::list<ObjectPtr> aFeatures = aRefList->list();
867 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
868 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
870 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
871 for (; anIt != aLast; anIt++) {
872 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
873 if (!aFeature.get() || (theSkipFeature.get() && theSkipFeature == aFeature))
875 aFPoint = PartSet_WidgetPoint2D::findFirstEqualPoint(aFeature, thePoint);