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_SketcherReentrantMgr.h>
11 #include <PartSet_ExternalObjectsMgr.h>
12 #include <PartSet_CenterPrs.h>
14 #include <XGUI_Tools.h>
15 #include <XGUI_Workshop.h>
16 #include <XGUI_Displayer.h>
18 #include <ModuleBase_ParamSpinBox.h>
19 #include <ModuleBase_Tools.h>
20 #include <ModuleBase_IViewer.h>
21 #include <ModuleBase_IViewWindow.h>
22 #include <ModuleBase_ISelection.h>
23 #include <ModuleBase_ViewerPrs.h>
24 #include <ModuleBase_WidgetValidator.h>
25 #include <ModuleBase_WidgetValidated.h>
26 #include <ModuleBase_LabelValue.h>
28 #include <Config_Keywords.h>
29 #include <Config_WidgetAPI.h>
31 #include <Events_Loop.h>
32 #include <Events_InfoMessage.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_AttributeBoolean.h>
35 #include <ModelAPI_AttributeRefAttr.h>
36 #include <ModelAPI_AttributeRefList.h>
37 #include <ModelAPI_Validator.h>
38 #include <ModelAPI_Session.h>
40 #include <ModelAPI_Feature.h>
41 #include <ModelAPI_Data.h>
42 #include <ModelAPI_Object.h>
43 #include <GeomDataAPI_Point2D.h>
44 #include <GeomAPI_Pnt2d.h>
46 #include <GeomAPI_ShapeExplorer.h>
47 #include <GeomAPI_Vertex.h>
49 #include <SketchPlugin_Feature.h>
50 #include <SketchPlugin_ConstraintCoincidence.h>
51 #include <SketchPlugin_Line.h>
52 #include <SketchPlugin_Arc.h>
53 #include <SketchPlugin_Circle.h>
54 #include <SketchPlugin_Point.h>
57 #include <QGridLayout>
60 #include <QMouseEvent>
61 #include <QApplication>
64 #include <TopoDS_Vertex.hxx>
65 #include <BRep_Tool.hxx>
66 #include <Geom_Point.hxx>
71 const double MaxCoordinate = 1e12;
73 static QStringList MyFeaturesForCoincedence;
75 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
76 ModuleBase_IWorkshop* theWorkshop,
77 const Config_WidgetAPI* theData)
78 : ModuleBase_ModelWidget(theParent, theData), myWorkshop(theWorkshop),
79 myValueIsCashed(false), myIsFeatureVisibleInCash(true),
80 myXValueInCash(0), myYValueInCash(0)
82 myRefAttribute = theData->getProperty("reference_attribute");
83 if (MyFeaturesForCoincedence.isEmpty()) {
84 MyFeaturesForCoincedence << SketchPlugin_Line::ID().c_str()
85 << SketchPlugin_Arc::ID().c_str()
86 << SketchPlugin_Point::ID().c_str()
87 << SketchPlugin_Circle::ID().c_str();
90 // the control should accept the focus, so the boolean flag is corrected to be true
91 myIsObligatory = true;
92 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
93 myGroupBox = new QGroupBox(aPageName, theParent);
94 myGroupBox->setFlat(false);
96 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
98 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
99 ModuleBase_Tools::adjustMargins(aGroupLay);
100 aGroupLay->setSpacing(2);
101 aGroupLay->setColumnStretch(1, 1);
103 QLabel* aLabel = new QLabel(myGroupBox);
105 myXSpin = new ModuleBase_LabelValue(myGroupBox, tr("X"));
106 //ModuleBase_ParamSpinBox(myGroupBox);
107 //myXSpin->setAcceptVariables(aAcceptVariables);
108 //myXSpin->setMinimum(-DBL_MAX);
109 //myXSpin->setMaximum(DBL_MAX);
110 //myXSpin->setToolTip(tr("X"));
111 aGroupLay->addWidget(myXSpin, 0, 1);
113 //connect(myXSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
114 //myXSpin->setValueEnabled(isValueEnabled());
117 //QLabel* aLabel = new QLabel(myGroupBox);
118 //aLabel->setText(tr("Y "));
119 //aGroupLay->addWidget(aLabel, 1, 0);
121 myYSpin = new ModuleBase_LabelValue(myGroupBox, tr("Y"));
122 //ModuleBase_ParamSpinBox(myGroupBox);
123 //myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
124 //myYSpin->setAcceptVariables(aAcceptVariables);
125 //myYSpin->setMinimum(-DBL_MAX);
126 //myYSpin->setMaximum(DBL_MAX);
127 //myYSpin->setToolTip(tr("Y"));
128 aGroupLay->addWidget(myYSpin, 1, 1);
130 //connect(myYSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
131 //myYSpin->setValueEnabled(isValueEnabled());
133 QVBoxLayout* aLayout = new QVBoxLayout(this);
134 ModuleBase_Tools::zeroMargins(aLayout);
135 aLayout->addWidget(myGroupBox);
138 myWidgetValidator = new ModuleBase_WidgetValidator(this, myWorkshop);
139 myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"),
140 theData->getProperty("can_create_external"), true);
143 bool PartSet_WidgetPoint2D::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
147 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
148 if (aModule->sketchReentranceMgr()->isInternalEditActive())
149 return true; /// when internal edit is started a new feature is created. I has not results, AIS
151 /// the selection is not possible if the current feature has no presentation for the current
152 /// attribute not in AIS not in results. If so, no object in current feature where make
153 /// coincidence, so selection is not necessary
154 GeomShapePtr anAISShape;
155 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(myFeature);
158 anAIS = aPrs->getAISObject(anAIS);
160 anAISShape = anAIS->getShape();
163 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = myFeature->results();
164 if (!anAISShape.get() && aResults.empty())
167 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
169 return isValidSelectionForAttribute_(theValue, myFeature->attribute(attributeID()));
171 bool aFoundPoint = false;
172 /// Avoid coincidence build to passed point. Coincidence is build later only if there are no
173 /// reference attribute.
174 /// The condition is that the selected feature has shape that has after explore a point
175 /// equal to clicked one.
176 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
177 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
178 aData->attribute(attributeID()));
179 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
180 if (anAISShape.get())
181 aFoundPoint = shapeExploreHasVertex(anAISShape, aPoint, mySketch);
183 /// analysis of results
184 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
185 for (; aRIter != aResults.cend() && !aFoundPoint; aRIter++) {
186 ResultPtr aResult = *aRIter;
187 if (aResult.get() && aResult->shape().get()) {
188 GeomShapePtr aShape = aResult->shape();
189 aFoundPoint = shapeExploreHasVertex(aShape, aPoint, mySketch);
197 //********************************************************************
198 bool PartSet_WidgetPoint2D::isValidSelectionForAttribute_(
199 const ModuleBase_ViewerPrsPtr& theValue,
200 const AttributePtr& theAttribute)
204 // stores the current values of the widget attribute
205 bool isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked;
207 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
208 ModuleBase_WidgetValidated::blockFeatureAttribute(aRefAttr, myFeature, true,
209 isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
210 myWidgetValidator->storeAttributeValue(aRefAttr);
212 // saves the owner value to the widget attribute
213 aValid = setSelectionCustom(theValue);
215 // checks the attribute validity
216 aValid = myWidgetValidator->isValidAttribute(theAttribute);
218 // restores the current values of the widget attribute
219 myWidgetValidator->restoreAttributeValue(aRefAttr, aValid);
220 myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
222 ModuleBase_WidgetValidated::blockFeatureAttribute(aRefAttr, myFeature, false, isFlushesActived,
223 isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
227 bool PartSet_WidgetPoint2D::setSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
230 GeomShapePtr aShape = theValue->shape();
231 if (aShape.get() && !aShape->isNull()) {
232 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
234 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
235 if (getPoint2d(aView, aTDShape, aX, aY)) {
236 fillRefAttribute(aX, aY, theValue);
239 else if (aTDShape.ShapeType() == TopAbs_EDGE) {
240 fillRefAttribute(theValue);
247 bool PartSet_WidgetPoint2D::resetCustom()
250 if (!isUseReset() || isComputedDefault()
251 /*|| myXSpin->hasVariable() || myYSpin->hasVariable()*/) {
255 if (myValueIsCashed) {
256 // if the restored value should be hidden, aDone = true to set
257 // reset state for the widget in the parent
258 aDone = restoreCurentValue();
259 emit objectUpdated();
263 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
264 // it is important to block the spin box control in order to do not through out the
265 // locking of the validating state.
266 myXSpin->setValue(isOk ? aDefValue : 0.0);
267 myYSpin->setValue(isOk ? aDefValue : 0.0);
269 //ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
270 //ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
278 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
280 delete myExternalObjectMgr;
283 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
284 const bool theToValidate)
287 if (theValues.empty())
290 ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
292 if (!theToValidate || myWidgetValidator->isValidSelection(aValue)) {
293 GeomShapePtr aShape = aValue->shape();
294 if (aShape.get() && !aShape->isNull()) {
295 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
297 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
298 if (getPoint2d(aView, aTDShape, aX, aY)) {
299 isDone = setPoint(aX, aY);
300 setConstraintToPoint(aX, aY, aValue);
307 void PartSet_WidgetPoint2D::selectContent()
309 // myXSpin->selectAll();
312 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
314 if (fabs(theX) >= MaxCoordinate)
316 if (fabs(theY) >= MaxCoordinate)
319 myXSpin->setValue(theX);
320 myYSpin->setValue(theY);
322 //ModuleBase_Tools::setSpinValue(myXSpin, theX);
323 //ModuleBase_Tools::setSpinValue(myYSpin, theY);
329 bool PartSet_WidgetPoint2D::storeValueCustom()
331 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
332 if (!aData) // can be on abort of sketcher element
334 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
335 aData->attribute(attributeID()));
337 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
338 bool isBlocked = that->blockSignals(true);
339 bool isImmutable = aPoint->setImmutable(true);
341 // if text is not empty then setValue will be ignored
342 // so we should set the text at first
343 //aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
344 // myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
345 //aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
346 // !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
347 aPoint->setValue(myXSpin->value(), myYSpin->value());
349 // after movement the solver will call the update event: optimization
350 #ifndef SUPPORT_NEW_MOVE
351 moveObject(myFeature);
353 updateObject(myFeature);
355 aPoint->setImmutable(isImmutable);
356 that->blockSignals(isBlocked);
361 bool PartSet_WidgetPoint2D::restoreValueCustom()
363 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
364 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
365 aData->attribute(attributeID()));
366 double aValueX = aPoint->isInitialized() ? aPoint->x() : 0.;
367 double aValueY = aPoint->isInitialized() ? aPoint->y() : 0.;
368 myXSpin->setValue(aValueX);
369 myYSpin->setValue(aValueY);
374 void PartSet_WidgetPoint2D::storeCurentValue()
376 // do not use cash if a variable is used
377 //if (myXSpin->hasVariable() || myYSpin->hasVariable())
380 myValueIsCashed = true;
381 myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
382 XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
383 myXValueInCash = myXSpin->value();
384 myYValueInCash = myYSpin->value();
387 bool PartSet_WidgetPoint2D::restoreCurentValue()
389 bool aRestoredAndHidden = true;
391 bool isVisible = myIsFeatureVisibleInCash;
392 // fill the control widgets by the cashed value
394 myValueIsCashed = false;
395 myIsFeatureVisibleInCash = true;
396 myXSpin->setValue(myXValueInCash);
397 myYSpin->setValue(myYValueInCash);
398 //ModuleBase_Tools::setSpinValue(myXSpin, myXValueInCash);
399 //ModuleBase_Tools::setSpinValue(myYSpin, myYValueInCash);
401 // store value to the model
404 setValueState(Stored);
405 aRestoredAndHidden = false;
408 aRestoredAndHidden = true;
410 return aRestoredAndHidden;
413 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
415 QList<QWidget*> aControls;
416 aControls.append(myXSpin);
417 aControls.append(myYSpin);
422 void PartSet_WidgetPoint2D::activateCustom()
425 aModes << TopAbs_VERTEX;
426 aModes << TopAbs_EDGE;
427 myWorkshop->activateSubShapesSelection(aModes);
429 if (!isEditingMode()) {
430 FeaturePtr aFeature = feature();
431 if (aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID())
436 void PartSet_WidgetPoint2D::setHighlighted(bool isHighlighted)
440 void PartSet_WidgetPoint2D::deactivate()
442 // the value of the control should be stored to model if it was not
443 // initialized yet. It is important when we leave this control by Tab key.
444 // It should not be performed by the widget activation as the preview
445 // is visualized with default value. Line point is moved to origin.
446 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
447 if (anAttribute && !anAttribute->isInitialized())
450 ModuleBase_ModelWidget::deactivate();
451 myWorkshop->deactivateSubShapesSelection();
454 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
455 const TopoDS_Shape& theShape,
456 double& theX, double& theY) const
458 if (!theShape.IsNull()) {
459 if (theShape.ShapeType() == TopAbs_VERTEX) {
460 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
461 if (!aVertex.IsNull()) {
462 // A case when point is taken from existing vertex
463 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
464 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
472 bool PartSet_WidgetPoint2D::setConstraintToPoint(double theClickedX, double theClickedY,
473 const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
475 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
477 fillRefAttribute(theClickedX, theClickedY, theValue);
479 FeaturePtr aFeature = feature();
480 std::string anAttribute = attributeID();
485 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
486 new GeomAPI_Pnt2d(theClickedX, theClickedY));
487 AttributePoint2DPtr aClickedFeaturePoint = findFirstEqualPointInSketch(mySketch,
488 aFeature, aClickedPoint);
489 if (!aClickedFeaturePoint.get())
492 // aRefAttr->setAttr(aClickedFeaturePoint);
494 // find a feature point by the selection mode
495 AttributePoint2DPtr aFeaturePoint;
496 if (aFeature->isMacro()) {
497 // the macro feature will be removed after the operation is stopped, so we need to build
498 // coicidence to possible sub-features
499 aFeaturePoint = findFirstEqualPointInArgumentFeatures(aFeature, aClickedPoint);
502 aFeaturePoint = std::dynamic_pointer_cast<
503 GeomDataAPI_Point2D>(aFeature->data()->attribute(anAttribute));
505 if (!aFeaturePoint.get())
508 PartSet_Tools::createConstraint(mySketch, aClickedFeaturePoint, aFeaturePoint);
513 bool PartSet_WidgetPoint2D::setConstraintToObject(const ObjectPtr& theObject)
515 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
516 if (aRefAttr.get()) {
517 fillRefAttribute(theObject);
520 AttributePoint2DPtr aFeaturePoint;
522 if (feature()->isMacro()) {
523 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
524 AttributePoint2DPtr anAttrPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
525 if (anAttrPoint.get()) {
526 // the macro feature will be removed after the operation is stopped, so we need to build
527 // coicidence to possible sub-features
528 aFeaturePoint = findFirstEqualPointInArgumentFeatures(feature(),
533 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
534 aFeaturePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
536 if (!aFeaturePoint.get())
539 // Create point-edge coincedence
540 FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
541 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
543 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
544 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
546 aRef1->setAttr(aFeaturePoint);
548 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
549 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
550 aRef2->setObject(theObject);
552 // we need to flush created signal in order to coincidence is processed by solver
553 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
558 void PartSet_WidgetPoint2D::mouseReleased(ModuleBase_IViewWindow* theWindow, QMouseEvent* theEvent)
560 // the contex menu release by the right button should not be processed by this widget
561 if (theEvent->button() != Qt::LeftButton)
564 ModuleBase_ISelection* aSelection = myWorkshop->selection();
565 Handle(V3d_View) aView = theWindow->v3dView();
567 QList<ModuleBase_ViewerPrsPtr> aList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
568 ModuleBase_ViewerPrsPtr aFirstValue =
569 aList.size() > 0 ? aList.first() : ModuleBase_ViewerPrsPtr();
570 if (!aFirstValue.get() && myPreSelected.get()) {
571 aFirstValue = myPreSelected;
574 // if we have selection and use it
575 if (aFirstValue.get() && isValidSelectionCustom(aFirstValue) &&
576 aFirstValue->shape().get()) { /// Trihedron Axis may be selected, but shape is empty
577 GeomShapePtr aGeomShape = aFirstValue->shape();
578 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
579 ObjectPtr aObject = aFirstValue->object();
581 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
582 bool anExternal = false;
583 std::shared_ptr<SketchPlugin_Feature> aSPFeature;
584 if (aSelectedFeature.get() != NULL)
585 aSPFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
587 ResultPtr aFixedObject;
588 bool aSketchExternalFeature = aSPFeature.get() && aSPFeature->isExternal();
589 if ((!aSPFeature && !aShape.IsNull()) || aSketchExternalFeature) {
590 aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
591 if (aSketchExternalFeature && !aFixedObject.get()) {/// local selection on external feature
596 if (!aFixedObject.get())
597 aFixedObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
602 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
603 // do not create a constraint to the point, which already used by the feature
604 // if the feature contains the point, focus is not switched
608 if (getPoint2d(aView, aShape, aX, aY))
611 if (aShape.ShapeType() == TopAbs_EDGE) {
612 // point is taken from mouse event and set in attribute. It should be done before set
613 // coinident constraint to the external line. If a point is created, it should be in
614 // the mouse clicked point
615 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(),
616 theWindow->v3dView());
617 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, aY);
620 setValueState(Stored); // in case of edge selection, Apply state should also be updated
622 bool anOrphanPoint = aShape.ShapeType() == TopAbs_VERTEX ||
623 isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
625 // we should not stop reentrant operation on external objects because
626 anOrphanPoint = true;
627 // they are not participate in the contour creation excepting external vertices
628 if (aShape.ShapeType() == TopAbs_VERTEX) {
629 FeaturePtr aFixedFeature = ModelAPI_Feature::feature(aFixedObject);
630 if (aFixedFeature.get() && aFixedFeature->getKind() == SketchPlugin_Point::ID()) {
631 anOrphanPoint = isOrphanPoint(aFixedFeature, mySketch, aX, aY);
635 if (aFixedObject.get())
636 setConstraintToObject(aFixedObject);
637 // fignal updated should be flushed in order to visualize possible created
638 // external objects e.g. selection of trihedron axis when input end arc point
639 updateObject(feature());
642 emit vertexSelected(); // it stops the reentrant operation
644 emit focusOutWidget(this);
649 bool isProcessed = false;
650 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
651 // when the point is selected, the coordinates of the point should be set into the attribute
652 // if the feature contains the point, focus is not switched
656 bool anOrphanPoint = isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
657 // do not set a coincidence constraint in the attribute if the feature contains a point
658 // with the same coordinates. It is important for line creation in order to do not set
659 // the same constraints for the same points, oterwise the result line has zero length.
660 bool isAuxiliaryFeature = false;
661 if (getPoint2d(aView, aShape, aX, aY)) {
663 setConstraintToPoint(aX, aY, aFirstValue);
665 else if (aShape.ShapeType() == TopAbs_EDGE) {
666 // point is taken from mouse event and set in attribute. It should be done before setting
667 // coinident constraint to the external line. If a point is created, it should be in
668 // the mouse clicked point
669 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
670 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, aY);
672 setConstraintToObject(aObject);
673 setValueState(Stored); // in case of edge selection, Apply state should also be updated
674 isAuxiliaryFeature = PartSet_Tools::isAuxiliarySketchEntity(aObject);
676 // it is important to perform updateObject() in order to the current value is
677 // processed by Sketch Solver. Test case: line is created from a previous point
678 // to some distance, but in the area of the highlighting of the point. Constraint
679 // coincidence is created, after the solver is performed, the distance between the
680 // points of the line becomes less than the tolerance. Validator of the line returns
681 // false, the line will be aborted, but sketch stays valid.
682 updateObject(feature());
683 if (!anOrphanPoint && !anExternal && !isAuxiliaryFeature)
684 emit vertexSelected();
685 emit focusOutWidget(this);
689 // The selection could be a center of an external circular object
690 else if (aFirstValue.get() && (!aFirstValue->interactive().IsNull())) {
691 Handle(PartSet_CenterPrs) aAIS =
692 Handle(PartSet_CenterPrs)::DownCast(aFirstValue->interactive());
693 if (!aAIS.IsNull()) {
694 gp_Pnt aPntComp = aAIS->Component()->Pnt();
695 GeomVertexPtr aVertPtr(new GeomAPI_Vertex(aPntComp.X(), aPntComp.Y(), aPntComp.Z()));
696 TopoDS_Shape aShape = aVertPtr->impl<TopoDS_Shape>();
698 ResultPtr aFixedObject =
699 PartSet_Tools::findFixedObjectByExternal(aShape, aAIS->object(), mySketch);
700 if (!aFixedObject.get())
701 aFixedObject = PartSet_Tools::createFixedByExternalCenter(aAIS->object(), aAIS->edge(),
702 aAIS->centerType(), mySketch);
703 if (aFixedObject.get())
704 setConstraintToObject(aFixedObject);
705 // fignal updated should be flushed in order to visualize possible created
706 // external objects e.g. selection of trihedron axis when input end arc point
707 updateObject(feature());
710 if (getPoint2d(aView, aShape, aX, aY)) {
711 // do not create a constraint to the point, which already used by the feature
712 // if the feature contains the point, focus is not switched
715 emit vertexSelected(); // it stops the reentrant operation
716 emit focusOutWidget(this);
720 // A case when point is taken from mouse event
721 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
723 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
725 // if the feature contains the point, focus is not switched
726 if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
729 emit focusOutWidget(this);
733 void PartSet_WidgetPoint2D::setPreSelection(
734 const std::shared_ptr<ModuleBase_ViewerPrs>& thePreSelected,
735 ModuleBase_IViewWindow* theWnd,
736 QMouseEvent* theEvent)
738 myPreSelected = thePreSelected;
739 mouseReleased(theWnd, theEvent);
740 myPreSelected = ModuleBase_ViewerPrsPtr();
743 void PartSet_WidgetPoint2D::getGeomSelection_(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue,
744 ObjectPtr& theObject,
745 GeomShapePtr& theShape)
747 myExternalObjectMgr->getGeomSelection(theValue, theObject, theShape, myWorkshop, sketch(), true);
750 void PartSet_WidgetPoint2D::mouseMoved(ModuleBase_IViewWindow* theWindow, QMouseEvent* theEvent)
752 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
754 if (isEditingMode() || aModule->sketchReentranceMgr()->isInternalEditActive())
757 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
760 PartSet_Tools::convertTo2D(aPoint, mySketch, theWindow->v3dView(), aX, anY);
761 if (myState != ModifiedInViewer)
763 // we need to block the value state change
764 bool isBlocked = blockValueState(true);
766 blockValueState(isBlocked);
767 setValueState(ModifiedInViewer);
770 double PartSet_WidgetPoint2D::x() const
772 return myXSpin->value();
775 double PartSet_WidgetPoint2D::y() const
777 return myYSpin->value();
781 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
782 double theX, double theY)
784 bool aPointIsFound = false;
786 if (feature()->getKind() != SketchPlugin_Line::ID())
787 return aPointIsFound;
789 AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
791 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
792 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
793 std::list<AttributePtr> anAttributes =
794 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
795 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
796 for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
797 AttributePoint2DPtr aPoint2DAttribute =
798 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
799 if (aPoint2DAttribute == aWidgetAttribute)
801 if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
802 aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
805 return aPointIsFound;
808 void PartSet_WidgetPoint2D::initializeValueByActivate()
812 /*void PartSet_WidgetPoint2D::onValuesChanged()
814 emit valuesChanged();
817 bool PartSet_WidgetPoint2D::processEnter()
820 /*bool isModified = getValueState() == ModifiedInPP;
822 bool isXModified = myXSpin->hasFocus();
823 emit valuesChanged();
825 myXSpin->selectAll();
827 myYSpin->selectAll();
832 bool PartSet_WidgetPoint2D::useSelectedShapes() const
837 bool PartSet_WidgetPoint2D::isOrphanPoint(const FeaturePtr& theFeature,
838 const CompositeFeaturePtr& theSketch,
839 double theX, double theY)
841 bool anOrphanPoint = false;
842 if (theFeature.get()) {
843 AttributePoint2DPtr aPointAttr;
844 std::string aFeatureKind = theFeature->getKind();
845 if (aFeatureKind == SketchPlugin_Point::ID())
846 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
847 theFeature->attribute(SketchPlugin_Point::COORD_ID()));
848 else if (aFeatureKind == SketchPlugin_Circle::ID())
849 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
850 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
852 else if (aFeatureKind == SketchPlugin_Arc::ID())
853 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
854 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
856 /// check that the geometry point with the given coordinates is the checked point
857 /// e.g. in arc the (x,y) point can not coicide to the center point and it automatically
858 /// means that this point is not an orphant one.
859 if (aPointAttr.get()) {
860 std::shared_ptr<GeomAPI_Pnt2d> aCheckedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
861 new GeomAPI_Pnt2d(theX, theY));
862 if (!aCheckedPoint->isEqual(aPointAttr->pnt()))
863 return anOrphanPoint;
866 if (aPointAttr.get()) {
867 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
868 // we need to find coincidence features in results also, because external object(point)
869 // uses refs to me in another feature.
870 FeaturePtr aCoincidence = PartSet_Tools::findFirstCoincidence(theFeature, aPoint);
871 anOrphanPoint = true;
872 // if there is at least one concident line to the point, the point is not an orphant
873 if (aCoincidence.get()) {
874 QList<FeaturePtr> aCoinsideLines;
875 QList<FeaturePtr> aCoins;
876 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
877 SketchPlugin_ConstraintCoincidence::ENTITY_A());
878 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
879 SketchPlugin_ConstraintCoincidence::ENTITY_B());
880 QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(),
881 aLast = aCoinsideLines.end();
882 for (; anIt != aLast && anOrphanPoint; anIt++) {
883 anOrphanPoint = (*anIt)->getKind() != SketchPlugin_Line::ID();
888 return anOrphanPoint;
891 bool PartSet_WidgetPoint2D::shapeExploreHasVertex(const GeomShapePtr& theShape,
892 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
893 const CompositeFeaturePtr& theSketch)
895 std::shared_ptr<GeomAPI_Pnt> aPoint = PartSet_Tools::point3D(thePoint, theSketch);
897 bool aContainPoint = false;
898 GeomAPI_ShapeExplorer anExp(theShape, GeomAPI_Shape::VERTEX);
899 for(; anExp.more() && !aContainPoint; anExp.next()) {
900 std::shared_ptr<GeomAPI_Shape> aVertexInCompSolid = anExp.current();
901 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aVertexInCompSolid));
903 aContainPoint = aPoint->isEqual(aVertex->point());
905 return aContainPoint;
908 AttributeRefAttrPtr PartSet_WidgetPoint2D::attributeRefAttr() const
910 AttributeRefAttrPtr anAttribute;
911 if (myRefAttribute.empty())
914 AttributePtr anAttributeRef = feature()->attribute(myRefAttribute);
915 if (!anAttributeRef.get())
918 return std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttributeRef);
921 void PartSet_WidgetPoint2D::fillRefAttribute(double theClickedX, double theClickedY,
922 const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
924 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
928 FeaturePtr aFeature = feature();
929 std::string anAttribute = attributeID();
931 if (aFeature.get()) {
932 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
933 new GeomAPI_Pnt2d(theClickedX, theClickedY));
934 AttributePoint2DPtr aClickedFeaturePoint = findFirstEqualPointInSketch(mySketch,
935 aFeature, aClickedPoint);
936 if (aClickedFeaturePoint.get())
937 aRefAttr->setAttr(aClickedFeaturePoint);
939 ObjectPtr anObject = getGeomSelection(theValue);
941 aRefAttr->setObject(anObject);
946 void PartSet_WidgetPoint2D::fillRefAttribute(const ModuleBase_ViewerPrsPtr& theValue)
948 fillRefAttribute(getGeomSelection(theValue));
951 void PartSet_WidgetPoint2D::fillRefAttribute(const ObjectPtr& theObject)
953 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
955 aRefAttr->setObject(theObject);
958 std::shared_ptr<GeomDataAPI_Point2D> PartSet_WidgetPoint2D::findFirstEqualPointInArgumentFeatures(
959 const FeaturePtr& theFeature, const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
961 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
963 // may be feature is not updated yet, execute is not performed and references features
964 // are not created. Case: rectangle macro feature
965 ModuleBase_Tools::flushUpdated(theFeature);
967 std::list<AttributePtr> anAttributes = theFeature->data()->attributes(
968 ModelAPI_AttributeRefList::typeId());
969 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
970 for (; anIt != aLast && !aFeaturePoint.get(); anIt++) {
971 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
972 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
973 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFeaturePoint.get(); i++) {
974 ObjectPtr anObject = aCurSelList->object(i);
975 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
977 aFeaturePoint = findFirstEqualPoint(aFeature, thePoint);
980 return aFeaturePoint;
983 std::shared_ptr<GeomDataAPI_Point2D> PartSet_WidgetPoint2D::findFirstEqualPoint(
984 const FeaturePtr& theFeature,
985 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
987 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
989 // find the given point in the feature attributes
990 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
991 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
992 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
993 aLast = anAttiributes.end();
994 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
996 for (; anIt != aLast && !aFPoint; anIt++) {
997 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
998 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
999 if (aCurPoint && aCurPoint->isInitialized() &&
1000 aValidators->isCase(theFeature, aCurPoint->id()) &&
1001 (aCurPoint->pnt()->distance(thePoint) < Precision::Confusion())) {
1002 aFPoint = aCurPoint;
1009 std::shared_ptr<GeomDataAPI_Point2D> PartSet_WidgetPoint2D::findFirstEqualPointInSketch(
1010 const CompositeFeaturePtr& theSketch,
1011 const FeaturePtr& theSkipFeature,
1012 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1014 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
1015 // the constraint is created between the feature point and the found sketch point
1016 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1017 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
1018 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
1020 std::list<ObjectPtr> aFeatures = aRefList->list();
1021 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
1022 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
1024 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
1025 for (; anIt != aLast; anIt++) {
1026 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1027 if (!aFeature.get() || (theSkipFeature.get() && theSkipFeature == aFeature))
1029 aFPoint = PartSet_WidgetPoint2D::findFirstEqualPoint(aFeature, thePoint);
1036 ObjectPtr PartSet_WidgetPoint2D::getGeomSelection(const ModuleBase_ViewerPrsPtr& theValue)
1039 GeomShapePtr aShape;
1040 ModuleBase_ISelection* aSelection = myWorkshop->selection();
1041 anObject = aSelection->getResult(theValue);
1042 aShape = aSelection->getShape(theValue);
1043 myExternalObjectMgr->getGeomSelection(theValue, anObject, aShape, myWorkshop, sketch(), true);