1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "PartSet_WidgetPoint2d.h"
22 #include <PartSet_Tools.h>
23 #include <PartSet_Module.h>
24 #include <PartSet_SketcherReentrantMgr.h>
25 #include <PartSet_ExternalObjectsMgr.h>
26 #include <PartSet_CenterPrs.h>
28 #include <XGUI_Tools.h>
29 #include <XGUI_Workshop.h>
30 #include <XGUI_Displayer.h>
32 #include <ModuleBase_ParamSpinBox.h>
33 #include <ModuleBase_Tools.h>
34 #include <ModuleBase_IViewer.h>
35 #include <ModuleBase_IViewWindow.h>
36 #include <ModuleBase_ISelection.h>
37 #include <ModuleBase_ViewerPrs.h>
38 #include <ModuleBase_WidgetValidator.h>
39 #include <ModuleBase_WidgetValidated.h>
40 #include <ModuleBase_LabelValue.h>
42 #include <Config_Keywords.h>
43 #include <Config_WidgetAPI.h>
45 #include <Events_Loop.h>
46 #include <Events_InfoMessage.h>
47 #include <ModelAPI_Events.h>
48 #include <ModelAPI_AttributeBoolean.h>
49 #include <ModelAPI_AttributeRefAttr.h>
50 #include <ModelAPI_AttributeRefList.h>
51 #include <ModelAPI_Validator.h>
52 #include <ModelAPI_Session.h>
54 #include <ModelAPI_Feature.h>
55 #include <ModelAPI_Data.h>
56 #include <ModelAPI_Object.h>
57 #include <GeomDataAPI_Point2D.h>
58 #include <GeomAPI_Pnt2d.h>
60 #include <GeomAPI_ShapeExplorer.h>
61 #include <GeomAPI_Vertex.h>
63 #include <SketchPlugin_Feature.h>
64 #include <SketchPlugin_ConstraintCoincidence.h>
65 #include <SketchPlugin_Line.h>
66 #include <SketchPlugin_Arc.h>
67 #include <SketchPlugin_Circle.h>
68 #include <SketchPlugin_Point.h>
71 #include <QGridLayout>
74 #include <QMouseEvent>
75 #include <QApplication>
78 #include <TopoDS_Vertex.hxx>
79 #include <BRep_Tool.hxx>
80 #include <Geom_Point.hxx>
85 const double MaxCoordinate = 1e12;
87 static QStringList MyFeaturesForCoincedence;
89 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
90 ModuleBase_IWorkshop* theWorkshop,
91 const Config_WidgetAPI* theData)
92 : ModuleBase_ModelWidget(theParent, theData), myWorkshop(theWorkshop),
93 myValueIsCashed(false), myIsFeatureVisibleInCash(true),
94 myXValueInCash(0), myYValueInCash(0)
96 myRefAttribute = theData->getProperty("reference_attribute");
97 if (MyFeaturesForCoincedence.isEmpty()) {
98 MyFeaturesForCoincedence << SketchPlugin_Line::ID().c_str()
99 << SketchPlugin_Arc::ID().c_str()
100 << SketchPlugin_Point::ID().c_str()
101 << SketchPlugin_Circle::ID().c_str();
104 // the control should accept the focus, so the boolean flag is corrected to be true
105 myIsObligatory = true;
106 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
107 myGroupBox = new QGroupBox(aPageName, theParent);
108 myGroupBox->setFlat(false);
110 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
112 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
113 ModuleBase_Tools::adjustMargins(aGroupLay);
114 aGroupLay->setSpacing(2);
115 aGroupLay->setColumnStretch(1, 1);
117 QLabel* aLabel = new QLabel(myGroupBox);
119 myXSpin = new ModuleBase_LabelValue(myGroupBox, tr("X"));
120 //ModuleBase_ParamSpinBox(myGroupBox);
121 //myXSpin->setAcceptVariables(aAcceptVariables);
122 //myXSpin->setMinimum(-DBL_MAX);
123 //myXSpin->setMaximum(DBL_MAX);
124 //myXSpin->setToolTip(tr("X"));
125 aGroupLay->addWidget(myXSpin, 0, 1);
127 //connect(myXSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
128 //myXSpin->setValueEnabled(isValueEnabled());
131 //QLabel* aLabel = new QLabel(myGroupBox);
132 //aLabel->setText(tr("Y "));
133 //aGroupLay->addWidget(aLabel, 1, 0);
135 myYSpin = new ModuleBase_LabelValue(myGroupBox, tr("Y"));
136 //ModuleBase_ParamSpinBox(myGroupBox);
137 //myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
138 //myYSpin->setAcceptVariables(aAcceptVariables);
139 //myYSpin->setMinimum(-DBL_MAX);
140 //myYSpin->setMaximum(DBL_MAX);
141 //myYSpin->setToolTip(tr("Y"));
142 aGroupLay->addWidget(myYSpin, 1, 1);
144 //connect(myYSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
145 //myYSpin->setValueEnabled(isValueEnabled());
147 QVBoxLayout* aLayout = new QVBoxLayout(this);
148 ModuleBase_Tools::zeroMargins(aLayout);
149 aLayout->addWidget(myGroupBox);
152 myWidgetValidator = new ModuleBase_WidgetValidator(this, myWorkshop);
153 myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"),
154 theData->getProperty("can_create_external"), true);
157 bool PartSet_WidgetPoint2D::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
161 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
162 if (aModule->sketchReentranceMgr()->isInternalEditActive())
163 return true; /// when internal edit is started a new feature is created. I has not results, AIS
165 /// the selection is not possible if the current feature has no presentation for the current
166 /// attribute not in AIS not in results. If so, no object in current feature where make
167 /// coincidence, so selection is not necessary
168 GeomShapePtr anAISShape;
169 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(myFeature);
172 anAIS = aPrs->getAISObject(anAIS);
174 anAISShape = anAIS->getShape();
177 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = myFeature->results();
178 if (!anAISShape.get() && aResults.empty())
181 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
183 return isValidSelectionForAttribute_(theValue, myFeature->attribute(attributeID()));
185 bool aFoundPoint = false;
186 /// Avoid coincidence build to passed point. Coincidence is build later only if there are no
187 /// reference attribute.
188 /// The condition is that the selected feature has shape that has after explore a point
189 /// equal to clicked one.
190 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
191 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192 aData->attribute(attributeID()));
193 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
194 if (anAISShape.get())
195 aFoundPoint = shapeExploreHasVertex(anAISShape, aPoint, mySketch);
197 /// analysis of results
198 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
199 for (; aRIter != aResults.cend() && !aFoundPoint; aRIter++) {
200 ResultPtr aResult = *aRIter;
201 if (aResult.get() && aResult->shape().get()) {
202 GeomShapePtr aShape = aResult->shape();
203 aFoundPoint = shapeExploreHasVertex(aShape, aPoint, mySketch);
211 //********************************************************************
212 bool PartSet_WidgetPoint2D::isValidSelectionForAttribute_(
213 const ModuleBase_ViewerPrsPtr& theValue,
214 const AttributePtr& theAttribute)
218 // stores the current values of the widget attribute
219 bool isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked;
221 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
222 ModuleBase_WidgetValidated::blockFeatureAttribute(aRefAttr, myFeature, true,
223 isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
224 myWidgetValidator->storeAttributeValue(aRefAttr);
226 // saves the owner value to the widget attribute
227 aValid = setSelectionCustom(theValue);
229 // checks the attribute validity
230 aValid = myWidgetValidator->isValidAttribute(theAttribute);
232 // restores the current values of the widget attribute
233 myWidgetValidator->restoreAttributeValue(aRefAttr, aValid);
234 myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
236 ModuleBase_WidgetValidated::blockFeatureAttribute(aRefAttr, myFeature, false, isFlushesActived,
237 isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
241 bool PartSet_WidgetPoint2D::setSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
244 GeomShapePtr aShape = theValue->shape();
245 if (aShape.get() && !aShape->isNull()) {
246 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
247 double aX = 0, aY = 0;
248 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
249 if (getPoint2d(aView, aTDShape, aX, aY)) {
250 fillRefAttribute(aX, aY, theValue);
253 else if (aTDShape.ShapeType() == TopAbs_EDGE) {
254 fillRefAttribute(theValue);
261 bool PartSet_WidgetPoint2D::resetCustom()
264 if (!isUseReset() || isComputedDefault()
265 /*|| myXSpin->hasVariable() || myYSpin->hasVariable()*/) {
269 if (myValueIsCashed) {
270 // if the restored value should be hidden, aDone = true to set
271 // reset state for the widget in the parent
272 aDone = restoreCurentValue();
273 emit objectUpdated();
277 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
278 // it is important to block the spin box control in order to do not through out the
279 // locking of the validating state.
280 myXSpin->setValue(isOk ? aDefValue : 0.0);
281 myYSpin->setValue(isOk ? aDefValue : 0.0);
283 //ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
284 //ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
292 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
294 delete myExternalObjectMgr;
297 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
298 const bool theToValidate)
301 if (theValues.empty())
304 ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
306 if (!theToValidate || myWidgetValidator->isValidSelection(aValue)) {
307 GeomShapePtr aShape = aValue->shape();
308 if (aShape.get() && !aShape->isNull()) {
309 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
310 double aX = 0, aY = 0;
311 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
312 if (getPoint2d(aView, aTDShape, aX, aY)) {
313 isDone = setPoint(aX, aY);
314 setConstraintToPoint(aX, aY, aValue);
321 void PartSet_WidgetPoint2D::selectContent()
323 // myXSpin->selectAll();
326 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
328 if (fabs(theX) >= MaxCoordinate)
330 if (fabs(theY) >= MaxCoordinate)
333 myXSpin->setValue(theX);
334 myYSpin->setValue(theY);
336 //ModuleBase_Tools::setSpinValue(myXSpin, theX);
337 //ModuleBase_Tools::setSpinValue(myYSpin, theY);
343 bool PartSet_WidgetPoint2D::storeValueCustom()
345 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
346 if (!aData || !aData->isValid()) // can be on abort of sketcher element
348 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
349 aData->attribute(attributeID()));
351 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
352 bool isBlocked = that->blockSignals(true);
353 bool isImmutable = aPoint->setImmutable(true);
355 // if text is not empty then setValue will be ignored
356 // so we should set the text at first
357 //aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
358 // myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
359 //aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
360 // !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
362 if (myFeature->isMacro()) {
363 // Moving points of macro-features has been processed directly (without solver)
364 aPoint->setValue(myXSpin->value(), myYSpin->value());
365 updateObject(myFeature);
368 if (!aPoint->isInitialized())
369 aPoint->setValue(0., 0.);
371 std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(
372 new ModelAPI_ObjectMovedMessage(this));
373 aMessage->setMovedAttribute(aPoint);
374 aMessage->setOriginalPosition(aPoint->pnt());
375 aMessage->setCurrentPosition(myXSpin->value(), myYSpin->value());
376 Events_Loop::loop()->send(aMessage);
379 aPoint->setImmutable(isImmutable);
380 that->blockSignals(isBlocked);
385 bool PartSet_WidgetPoint2D::restoreValueCustom()
387 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
388 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
389 aData->attribute(attributeID()));
390 double aValueX = aPoint->isInitialized() ? aPoint->x() : 0.;
391 double aValueY = aPoint->isInitialized() ? aPoint->y() : 0.;
392 myXSpin->setValue(aValueX);
393 myYSpin->setValue(aValueY);
398 void PartSet_WidgetPoint2D::storeCurentValue()
400 // do not use cash if a variable is used
401 //if (myXSpin->hasVariable() || myYSpin->hasVariable())
404 myValueIsCashed = true;
405 myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
406 XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
407 myXValueInCash = myXSpin->value();
408 myYValueInCash = myYSpin->value();
411 bool PartSet_WidgetPoint2D::restoreCurentValue()
413 bool aRestoredAndHidden = true;
415 bool isVisible = myIsFeatureVisibleInCash;
416 // fill the control widgets by the cashed value
418 myValueIsCashed = false;
419 myIsFeatureVisibleInCash = true;
420 myXSpin->setValue(myXValueInCash);
421 myYSpin->setValue(myYValueInCash);
422 //ModuleBase_Tools::setSpinValue(myXSpin, myXValueInCash);
423 //ModuleBase_Tools::setSpinValue(myYSpin, myYValueInCash);
425 // store value to the model
428 setValueState(Stored);
429 aRestoredAndHidden = false;
432 aRestoredAndHidden = true;
434 return aRestoredAndHidden;
437 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
439 QList<QWidget*> aControls;
440 aControls.append(myXSpin);
441 aControls.append(myYSpin);
445 //********************************************************************
446 void PartSet_WidgetPoint2D::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
448 theModuleSelectionModes = -1;
449 theModes << TopAbs_VERTEX;
450 theModes << TopAbs_EDGE;
453 //********************************************************************
454 void PartSet_WidgetPoint2D::activateCustom()
456 if (!isEditingMode()) {
457 FeaturePtr aFeature = feature();
458 if (aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID())
463 void PartSet_WidgetPoint2D::setHighlighted(bool isHighlighted)
467 void PartSet_WidgetPoint2D::deactivate()
469 // the value of the control should be stored to model if it was not
470 // initialized yet. It is important when we leave this control by Tab key.
471 // It should not be performed by the widget activation as the preview
472 // is visualized with default value. Line point is moved to origin.
473 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
474 if (anAttribute && !anAttribute->isInitialized())
477 ModuleBase_ModelWidget::deactivate();
480 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
481 const TopoDS_Shape& theShape,
482 double& theX, double& theY) const
484 if (!theShape.IsNull()) {
485 if (theShape.ShapeType() == TopAbs_VERTEX) {
486 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
487 if (!aVertex.IsNull()) {
488 // A case when point is taken from existing vertex
489 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
490 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
498 bool PartSet_WidgetPoint2D::setConstraintToPoint(double theClickedX, double theClickedY,
499 const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
501 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
503 fillRefAttribute(theClickedX, theClickedY, theValue);
505 FeaturePtr aFeature = feature();
506 std::string anAttribute = attributeID();
511 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
512 new GeomAPI_Pnt2d(theClickedX, theClickedY));
513 AttributePoint2DPtr aClickedFeaturePoint = findFirstEqualPointInSketch(mySketch,
514 aFeature, aClickedPoint);
515 if (!aClickedFeaturePoint.get())
518 // aRefAttr->setAttr(aClickedFeaturePoint);
520 // find a feature point by the selection mode
521 AttributePoint2DPtr aFeaturePoint;
522 if (aFeature->isMacro()) {
523 // the macro feature will be removed after the operation is stopped, so we need to build
524 // coicidence to possible sub-features
525 aFeaturePoint = findFirstEqualPointInArgumentFeatures(aFeature, aClickedPoint);
528 aFeaturePoint = std::dynamic_pointer_cast<
529 GeomDataAPI_Point2D>(aFeature->data()->attribute(anAttribute));
531 if (!aFeaturePoint.get())
534 PartSet_Tools::createConstraint(mySketch, aClickedFeaturePoint, aFeaturePoint);
539 bool PartSet_WidgetPoint2D::setConstraintToObject(const ObjectPtr& theObject)
541 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
542 if (aRefAttr.get()) {
543 fillRefAttribute(theObject);
546 AttributePoint2DPtr aFeaturePoint;
548 if (feature()->isMacro()) {
549 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
550 AttributePoint2DPtr anAttrPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
551 if (anAttrPoint.get()) {
552 // the macro feature will be removed after the operation is stopped, so we need to build
553 // coicidence to possible sub-features
554 aFeaturePoint = findFirstEqualPointInArgumentFeatures(feature(),
559 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
560 aFeaturePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
562 if (!aFeaturePoint.get())
565 // Create point-edge coincedence
566 FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
567 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
569 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
570 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
572 aRef1->setAttr(aFeaturePoint);
574 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
575 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
576 aRef2->setObject(theObject);
578 // we need to flush created signal in order to coincidence is processed by solver
579 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
584 void PartSet_WidgetPoint2D::mouseReleased(ModuleBase_IViewWindow* theWindow, QMouseEvent* theEvent)
586 // the contex menu release by the right button should not be processed by this widget
587 if (theEvent->button() != Qt::LeftButton)
590 ModuleBase_ISelection* aSelection = myWorkshop->selection();
591 Handle(V3d_View) aView = theWindow->v3dView();
593 QList<ModuleBase_ViewerPrsPtr> aList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
594 ModuleBase_ViewerPrsPtr aFirstValue =
595 aList.size() > 0 ? aList.first() : ModuleBase_ViewerPrsPtr();
596 if (!aFirstValue.get() && myPreSelected.get()) {
597 aFirstValue = myPreSelected;
600 // if we have selection and use it
601 if (aFirstValue.get() && isValidSelectionCustom(aFirstValue) &&
602 aFirstValue->shape().get()) { /// Trihedron Axis may be selected, but shape is empty
603 GeomShapePtr aGeomShape = aFirstValue->shape();
604 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
605 ObjectPtr aObject = aFirstValue->object();
607 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
608 bool anExternal = false;
609 std::shared_ptr<SketchPlugin_Feature> aSPFeature;
610 if (aSelectedFeature.get() != NULL)
611 aSPFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
613 ResultPtr aFixedObject;
614 bool aSketchExternalFeature = aSPFeature.get() && aSPFeature->isExternal();
615 if ((!aSPFeature && !aShape.IsNull()) || aSketchExternalFeature) {
616 aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
617 if (aSketchExternalFeature && !aFixedObject.get()) {/// local selection on external feature
622 if (!aFixedObject.get())
624 FeaturePtr aCreatedFeature;
625 aFixedObject = PartSet_Tools::createFixedObjectByExternal(aGeomShape, aObject, mySketch,
626 false, aCreatedFeature);
631 double aX = 0, aY = 0;
632 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
633 // do not create a constraint to the point, which already used by the feature
634 // if the feature contains the point, focus is not switched
638 if (getPoint2d(aView, aShape, aX, aY))
641 if (aShape.ShapeType() == TopAbs_EDGE) {
642 // point is taken from mouse event and set in attribute. It should be done before set
643 // coinident constraint to the external line. If a point is created, it should be in
644 // the mouse clicked point
645 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(),
646 theWindow->v3dView());
647 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, aY);
650 setValueState(Stored); // in case of edge selection, Apply state should also be updated
652 bool anOrphanPoint = aShape.ShapeType() == TopAbs_VERTEX ||
653 isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
655 // we should not stop reentrant operation on external objects because
656 anOrphanPoint = true;
657 // they are not participate in the contour creation excepting external vertices
658 if (aShape.ShapeType() == TopAbs_VERTEX) {
659 FeaturePtr aFixedFeature = ModelAPI_Feature::feature(aFixedObject);
660 if (aFixedFeature.get() && aFixedFeature->getKind() == SketchPlugin_Point::ID()) {
661 anOrphanPoint = isOrphanPoint(aFixedFeature, mySketch, aX, aY);
665 if (aFixedObject.get())
666 setConstraintToObject(aFixedObject);
667 // fignal updated should be flushed in order to visualize possible created
668 // external objects e.g. selection of trihedron axis when input end arc point
669 updateObject(feature());
672 emit vertexSelected(); // it stops the reentrant operation
674 emit focusOutWidget(this);
678 double aX = 0, aY = 0;
679 bool isProcessed = false;
680 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
681 // when the point is selected, the coordinates of the point should be set into the attribute
682 // if the feature contains the point, focus is not switched
686 bool anOrphanPoint = isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
687 // do not set a coincidence constraint in the attribute if the feature contains a point
688 // with the same coordinates. It is important for line creation in order to do not set
689 // the same constraints for the same points, oterwise the result line has zero length.
690 bool isAuxiliaryFeature = false;
691 if (getPoint2d(aView, aShape, aX, aY)) {
693 setConstraintToPoint(aX, aY, aFirstValue);
695 else if (aShape.ShapeType() == TopAbs_EDGE) {
696 // point is taken from mouse event and set in attribute. It should be done before setting
697 // coinident constraint to the external line. If a point is created, it should be in
698 // the mouse clicked point
699 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
700 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, aY);
702 setConstraintToObject(aObject);
703 setValueState(Stored); // in case of edge selection, Apply state should also be updated
704 isAuxiliaryFeature = PartSet_Tools::isAuxiliarySketchEntity(aObject);
706 // it is important to perform updateObject() in order to the current value is
707 // processed by Sketch Solver. Test case: line is created from a previous point
708 // to some distance, but in the area of the highlighting of the point. Constraint
709 // coincidence is created, after the solver is performed, the distance between the
710 // points of the line becomes less than the tolerance. Validator of the line returns
711 // false, the line will be aborted, but sketch stays valid.
712 updateObject(feature());
713 if (!anOrphanPoint && !anExternal && !isAuxiliaryFeature)
714 emit vertexSelected();
715 emit focusOutWidget(this);
719 // The selection could be a center of an external circular object
720 else if (aFirstValue.get() && (!aFirstValue->interactive().IsNull())) {
721 Handle(PartSet_CenterPrs) aAIS =
722 Handle(PartSet_CenterPrs)::DownCast(aFirstValue->interactive());
723 if (!aAIS.IsNull()) {
724 gp_Pnt aPntComp = aAIS->Component()->Pnt();
725 GeomVertexPtr aVertPtr(new GeomAPI_Vertex(aPntComp.X(), aPntComp.Y(), aPntComp.Z()));
726 TopoDS_Shape aShape = aVertPtr->impl<TopoDS_Shape>();
728 ResultPtr aFixedObject =
729 PartSet_Tools::findFixedObjectByExternal(aShape, aAIS->object(), mySketch);
730 if (!aFixedObject.get())
732 FeaturePtr aCreatedFeature;
733 aFixedObject = PartSet_Tools::createFixedByExternalCenter(aAIS->object(), aAIS->edge(),
734 aAIS->centerType(), mySketch, false, aCreatedFeature);
736 if (aFixedObject.get())
737 setConstraintToObject(aFixedObject);
738 // fignal updated should be flushed in order to visualize possible created
739 // external objects e.g. selection of trihedron axis when input end arc point
740 updateObject(feature());
742 double aX = 0, aY = 0;
743 if (getPoint2d(aView, aShape, aX, aY)) {
744 // do not create a constraint to the point, which already used by the feature
745 // if the feature contains the point, focus is not switched
748 emit vertexSelected(); // it stops the reentrant operation
749 emit focusOutWidget(this);
753 // A case when point is taken from mouse event
754 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
755 double aX = 0, aY = 0;
756 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, aY);
758 // if the feature contains the point, focus is not switched
759 if (!setPoint(aX, aY) || isFeatureContainsPoint(myFeature, aX, aY))
762 emit focusOutWidget(this);
766 void PartSet_WidgetPoint2D::setPreSelection(
767 const std::shared_ptr<ModuleBase_ViewerPrs>& thePreSelected,
768 ModuleBase_IViewWindow* theWnd,
769 QMouseEvent* theEvent)
771 myPreSelected = thePreSelected;
772 mouseReleased(theWnd, theEvent);
773 myPreSelected = ModuleBase_ViewerPrsPtr();
776 void PartSet_WidgetPoint2D::getGeomSelection_(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue,
777 ObjectPtr& theObject,
778 GeomShapePtr& theShape)
780 myExternalObjectMgr->getGeomSelection(theValue, theObject, theShape, myWorkshop, sketch(), true);
783 void PartSet_WidgetPoint2D::mouseMoved(ModuleBase_IViewWindow* theWindow, QMouseEvent* theEvent)
785 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
787 if (isEditingMode() || aModule->sketchReentranceMgr()->isInternalEditActive())
790 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
792 double aX = 0, aY = 0;
793 PartSet_Tools::convertTo2D(aPoint, mySketch, theWindow->v3dView(), aX, aY);
794 if (myState != ModifiedInViewer)
796 // we need to block the value state change
797 bool isBlocked = blockValueState(true);
799 blockValueState(isBlocked);
800 setValueState(ModifiedInViewer);
803 double PartSet_WidgetPoint2D::x() const
805 return myXSpin->value();
808 double PartSet_WidgetPoint2D::y() const
810 return myYSpin->value();
814 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
815 double theX, double theY)
817 bool aPointIsFound = false;
819 if (feature()->getKind() != SketchPlugin_Line::ID())
820 return aPointIsFound;
822 AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
824 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
825 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
826 std::list<AttributePtr> anAttributes =
827 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
828 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
829 for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
830 AttributePoint2DPtr aPoint2DAttribute =
831 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
832 if (aPoint2DAttribute == aWidgetAttribute)
834 if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
835 aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
838 return aPointIsFound;
841 void PartSet_WidgetPoint2D::initializeValueByActivate()
845 /*void PartSet_WidgetPoint2D::onValuesChanged()
847 emit valuesChanged();
850 bool PartSet_WidgetPoint2D::processEnter()
853 /*bool isModified = getValueState() == ModifiedInPP;
855 bool isXModified = myXSpin->hasFocus();
856 emit valuesChanged();
858 myXSpin->selectAll();
860 myYSpin->selectAll();
865 bool PartSet_WidgetPoint2D::useSelectedShapes() const
870 bool PartSet_WidgetPoint2D::isOrphanPoint(const FeaturePtr& theFeature,
871 const CompositeFeaturePtr& theSketch,
872 double theX, double theY)
874 bool anOrphanPoint = false;
875 if (theFeature.get()) {
876 AttributePoint2DPtr aPointAttr;
877 std::string aFeatureKind = theFeature->getKind();
878 if (aFeatureKind == SketchPlugin_Point::ID())
879 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
880 theFeature->attribute(SketchPlugin_Point::COORD_ID()));
881 else if (aFeatureKind == SketchPlugin_Circle::ID())
882 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
883 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
885 else if (aFeatureKind == SketchPlugin_Arc::ID())
886 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
887 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
889 /// check that the geometry point with the given coordinates is the checked point
890 /// e.g. in arc the (x,y) point can not coicide to the center point and it automatically
891 /// means that this point is not an orphant one.
892 if (aPointAttr.get()) {
893 std::shared_ptr<GeomAPI_Pnt2d> aCheckedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
894 new GeomAPI_Pnt2d(theX, theY));
895 if (!aCheckedPoint->isEqual(aPointAttr->pnt()))
896 return anOrphanPoint;
899 if (aPointAttr.get()) {
900 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
901 // we need to find coincidence features in results also, because external object(point)
902 // uses refs to me in another feature.
903 FeaturePtr aCoincidence = PartSet_Tools::findFirstCoincidence(theFeature, aPoint);
904 anOrphanPoint = true;
905 // if there is at least one concident line to the point, the point is not an orphant
906 if (aCoincidence.get()) {
907 QList<FeaturePtr> aCoinsideLines;
908 QList<FeaturePtr> aCoins;
909 QList<bool> anIsAttributes;
910 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
911 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
913 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
914 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
916 QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(),
917 aLast = aCoinsideLines.end();
918 for (; anIt != aLast && anOrphanPoint; anIt++) {
919 anOrphanPoint = (*anIt)->getKind() != SketchPlugin_Line::ID();
924 return anOrphanPoint;
927 bool PartSet_WidgetPoint2D::shapeExploreHasVertex(const GeomShapePtr& theShape,
928 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
929 const CompositeFeaturePtr& theSketch)
931 std::shared_ptr<GeomAPI_Pnt> aPoint = PartSet_Tools::point3D(thePoint, theSketch);
933 bool aContainPoint = false;
934 GeomAPI_ShapeExplorer anExp(theShape, GeomAPI_Shape::VERTEX);
935 for(; anExp.more() && !aContainPoint; anExp.next()) {
936 std::shared_ptr<GeomAPI_Shape> aVertexInCompSolid = anExp.current();
937 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aVertexInCompSolid));
939 aContainPoint = aPoint->isEqual(aVertex->point());
941 return aContainPoint;
944 AttributeRefAttrPtr PartSet_WidgetPoint2D::attributeRefAttr() const
946 AttributeRefAttrPtr anAttribute;
947 if (myRefAttribute.empty())
950 AttributePtr anAttributeRef = feature()->attribute(myRefAttribute);
951 if (!anAttributeRef.get())
954 return std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttributeRef);
957 void PartSet_WidgetPoint2D::fillRefAttribute(double theClickedX, double theClickedY,
958 const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
960 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
964 FeaturePtr aFeature = feature();
965 std::string anAttribute = attributeID();
967 if (aFeature.get()) {
968 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
969 new GeomAPI_Pnt2d(theClickedX, theClickedY));
970 AttributePoint2DPtr aClickedFeaturePoint = findFirstEqualPointInSketch(mySketch,
971 aFeature, aClickedPoint);
972 if (aClickedFeaturePoint.get())
973 aRefAttr->setAttr(aClickedFeaturePoint);
975 ObjectPtr anObject = getGeomSelection(theValue);
977 aRefAttr->setObject(anObject);
982 void PartSet_WidgetPoint2D::fillRefAttribute(const ModuleBase_ViewerPrsPtr& theValue)
984 fillRefAttribute(getGeomSelection(theValue));
987 void PartSet_WidgetPoint2D::fillRefAttribute(const ObjectPtr& theObject)
989 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
991 aRefAttr->setObject(theObject);
994 std::shared_ptr<GeomDataAPI_Point2D> PartSet_WidgetPoint2D::findFirstEqualPointInArgumentFeatures(
995 const FeaturePtr& theFeature, const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
997 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
999 // may be feature is not updated yet, execute is not performed and references features
1000 // are not created. Case: rectangle macro feature
1001 ModuleBase_Tools::flushUpdated(theFeature);
1003 std::list<AttributePtr> anAttributes = theFeature->data()->attributes(
1004 ModelAPI_AttributeRefList::typeId());
1005 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
1006 for (; anIt != aLast && !aFeaturePoint.get(); anIt++) {
1007 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
1008 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
1009 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFeaturePoint.get(); i++) {
1010 ObjectPtr anObject = aCurSelList->object(i);
1011 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
1013 aFeaturePoint = findFirstEqualPoint(aFeature, thePoint);
1016 return aFeaturePoint;
1019 std::shared_ptr<GeomDataAPI_Point2D> PartSet_WidgetPoint2D::findFirstEqualPoint(
1020 const FeaturePtr& theFeature,
1021 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1023 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
1025 // find the given point in the feature attributes
1026 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
1027 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
1028 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
1029 aLast = anAttiributes.end();
1030 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
1032 for (; anIt != aLast && !aFPoint; anIt++) {
1033 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
1034 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
1035 if (aCurPoint && aCurPoint->isInitialized() &&
1036 aValidators->isCase(theFeature, aCurPoint->id()) &&
1037 (aCurPoint->pnt()->distance(thePoint) < Precision::Confusion())) {
1038 aFPoint = aCurPoint;
1045 std::shared_ptr<GeomDataAPI_Point2D> PartSet_WidgetPoint2D::findFirstEqualPointInSketch(
1046 const CompositeFeaturePtr& theSketch,
1047 const FeaturePtr& theSkipFeature,
1048 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1050 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
1051 // the constraint is created between the feature point and the found sketch point
1052 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1053 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
1054 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
1056 std::list<ObjectPtr> aFeatures = aRefList->list();
1057 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
1058 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
1060 std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
1061 for (; anIt != aLast; anIt++) {
1062 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1063 if (!aFeature.get() || (theSkipFeature.get() && theSkipFeature == aFeature))
1065 aFPoint = PartSet_WidgetPoint2D::findFirstEqualPoint(aFeature, thePoint);
1072 ObjectPtr PartSet_WidgetPoint2D::getGeomSelection(const ModuleBase_ViewerPrsPtr& theValue)
1075 GeomShapePtr aShape;
1076 ModuleBase_ISelection* aSelection = myWorkshop->selection();
1077 anObject = aSelection->getResult(theValue);
1078 aShape = aSelection->getShape(theValue);
1079 myExternalObjectMgr->getGeomSelection(theValue, anObject, aShape, myWorkshop, sketch(), true);