1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchPlugin_Validators.h"
22 #include "SketchPlugin_Arc.h"
23 #include "SketchPlugin_Circle.h"
24 #include "SketchPlugin_ConstraintCoincidence.h"
25 #include "SketchPlugin_ConstraintDistance.h"
26 #include "SketchPlugin_ConstraintRigid.h"
27 #include "SketchPlugin_ConstraintTangent.h"
28 #include "SketchPlugin_Ellipse.h"
29 #include "SketchPlugin_Fillet.h"
30 #include "SketchPlugin_Line.h"
31 #include "SketchPlugin_MacroArc.h"
32 #include "SketchPlugin_MacroCircle.h"
33 #include "SketchPlugin_MultiRotation.h"
34 #include "SketchPlugin_Point.h"
35 #include "SketchPlugin_Sketch.h"
36 #include "SketchPlugin_Trim.h"
37 #include "SketchPlugin_Tools.h"
39 #include "SketcherPrs_Tools.h"
41 #include <Events_InfoMessage.h>
43 #include <ModelAPI_Data.h>
44 #include <ModelAPI_Validator.h>
45 #include <ModelAPI_AttributeDouble.h>
46 #include <ModelAPI_AttributeInteger.h>
47 #include <ModelAPI_AttributeRefAttr.h>
48 #include <ModelAPI_AttributeRefList.h>
49 #include <ModelAPI_AttributeSelectionList.h>
50 #include <ModelAPI_AttributeString.h>
51 #include <ModelAPI_Session.h>
52 #include <ModelAPI_Tools.h>
53 #include <ModelAPI_ResultConstruction.h>
55 #include <ModelGeomAlgo_Point2D.h>
56 #include <ModelGeomAlgo_Shape.h>
58 #include <GeomAlgoAPI_EdgeBuilder.h>
59 #include <GeomAlgoAPI_ShapeTools.h>
61 #include <GeomAPI_Circ.h>
62 #include <GeomAPI_Dir2d.h>
63 #include <GeomAPI_Ellipse.h>
64 #include <GeomAPI_Lin.h>
65 #include <GeomAPI_Edge.h>
66 #include <GeomAPI_Vertex.h>
67 #include <GeomDataAPI_Point2D.h>
72 const double tolerance = 1.e-7;
74 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
75 const std::list<std::string>& theArguments,
76 Events_InfoMessage& theError) const
78 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
79 theError = "The attribute with the %1 type is not processed";
80 theError.arg(theAttribute->attributeType());
84 // there is a check whether the feature contains a point and a linear edge or two point values
85 std::string aParamA = theArguments.front();
86 SessionPtr aMgr = ModelAPI_Session::get();
87 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
89 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
91 bool isObject = aRefAttr->isObject();
93 // an attribute is a point. A point value is valid always for the distance
96 // 1. check whether the references object is a linear
97 ObjectPtr anObject = aRefAttr->object();
99 const ModelAPI_AttributeValidator* aShapeValidator =
100 dynamic_cast<const ModelAPI_AttributeValidator*>(
101 aFactory->validator("GeomValidators_ShapeType"));
102 std::list<std::string> anArguments;
103 anArguments.push_back("circle");
104 Events_InfoMessage aCircleError;
105 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
106 // the circle line is not a valid case
108 theError = "Circle can not be used in distance constraint";
113 anArguments.push_back("line");
114 Events_InfoMessage aLineError;
115 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
116 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
118 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
119 // If it is a line then we have to check that first attribute id not a line
120 std::shared_ptr<SketchPlugin_Feature> aSFeature =
121 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
122 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
123 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
124 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
125 aFeature->data(), aParamA, aPlane);
127 theError = "One of parameters of distance constraint should be a point";
135 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
136 const std::list<std::string>& theArguments,
137 Events_InfoMessage& theError) const
139 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
140 theError = "The attribute with the %1 type is not processed";
141 theError.arg(theAttribute->attributeType());
145 // there is a check whether the feature contains a point and a linear edge or two point values
146 std::string aParamA = theArguments.front();
147 SessionPtr aMgr = ModelAPI_Session::get();
148 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
150 FeaturePtr anAttributeFeature =
151 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
152 AttributeRefAttrPtr aRefAttr =
153 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
155 bool isObject = aRefAttr->isObject();
156 ObjectPtr anObject = aRefAttr->object();
157 if (isObject && anObject.get()) {
158 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
160 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
161 ObjectPtr aOtherObject = aOtherAttr->object();
162 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
166 if (aRefFea->getKind() == SketchPlugin_Line::ID() &&
167 aOtherFea->getKind() == SketchPlugin_Line::ID()) {
168 theError = "Two segments cannot be tangent";
174 theError = "It uses an empty object";
181 bool SketchPlugin_PerpendicularAttrValidator::isValid(const AttributePtr& theAttribute,
182 const std::list<std::string>& theArguments,
183 Events_InfoMessage& theError) const
185 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
186 theError = "The attribute with the %1 type is not processed";
187 theError.arg(theAttribute->attributeType());
191 std::string aParamA = theArguments.front();
192 SessionPtr aMgr = ModelAPI_Session::get();
193 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
195 FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
196 AttributeRefAttrPtr aRefAttr =
197 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
199 bool isObject = aRefAttr->isObject();
200 ObjectPtr anObject = aRefAttr->object();
201 if (isObject && anObject.get()) {
202 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
204 AttributeRefAttrPtr aOtherAttr = anOwner->refattr(aParamA);
205 ObjectPtr aOtherObject = aOtherAttr->object();
206 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
210 // at least one feature should be a line
211 if (aRefFea->getKind() != SketchPlugin_Line::ID() &&
212 aOtherFea->getKind() != SketchPlugin_Line::ID()) {
213 theError = "At least one feature should be a line";
218 theError = "It uses an empty object";
225 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
226 const std::list<std::string>& theArguments,
227 Events_InfoMessage& theError) const
229 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
230 theError = "The attribute with the %1 type is not processed";
231 theError.arg(theAttribute->attributeType());
235 std::shared_ptr<SketchPlugin_Feature> aFeature =
236 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
240 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
242 SketchPlugin_Sketch* aSketch = aFeature->sketch();
243 int aNbFeatures = aSketch->numberOfSubs();
244 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
245 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
246 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
248 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
249 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
250 if (aRefAttr->isObject()) {
251 if (aRefAttr->object() == aRAttr->object()) {
252 ObjectPtr anObject = aRefAttr->object();
253 std::string aName = anObject.get() ? anObject->data()->name() : "";
254 theError = "The object %1 has been already fixed.";
259 else if (aRefAttr->attr() == aRAttr->attr()) {
260 AttributePtr anAttribute = aRefAttr->attr();
261 std::string aName = anAttribute.get() ? anAttribute->id() : "";
262 theError = "The attribute %1 has been already fixed.";
270 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
271 const std::list<std::string>& theArguments,
272 Events_InfoMessage& theError) const
274 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
275 theError = "The attribute with the %1 type is not processed";
276 theError.arg(theAttribute->attributeType());
280 std::string aParamA = theArguments.front();
281 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
282 AttributeRefAttrPtr aRefAttr[2];
283 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
284 aRefAttr[1] = aFeature->data()->refattr(aParamA);
286 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
287 theError = "Attributes can not be used in equal constraint";
291 std::string aType[2];
292 std::list<std::string> anArguments;
293 for (int i = 0; i < 2; i++) {
294 ObjectPtr anObject = aRefAttr[i]->object();
295 if (!anObject.get()) {
296 theError = "An empty object is used.";
300 aFeature = ModelAPI_Feature::feature(anObject);
301 if (!aFeature.get()) {
302 theError = "An empty feature is used.";
306 aType[i] = aFeature->getKind();
307 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
308 aFeature->getKind() != SketchPlugin_Circle::ID() &&
309 aFeature->getKind() != SketchPlugin_Arc::ID() &&
310 aFeature->getKind() != SketchPlugin_Ellipse::ID()) {
311 theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4 or %5";
312 theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
313 .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID())
314 .arg(SketchPlugin_Ellipse::ID());
315 // wrong type of attribute
320 bool isOk = aType[0] == aType[1];
322 // circle and arc may be equal
323 isOk = (aType[0] == SketchPlugin_Arc::ID() && aType[1] == SketchPlugin_Circle::ID())
324 || (aType[0] == SketchPlugin_Circle::ID() && aType[1] == SketchPlugin_Arc::ID());
327 // ellipse and elliptic arc may be equal
331 theError = "Feature with kinds %1 and %2 can not be equal.";
332 theError.arg(aType[0]).arg(aType[1]);
338 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
339 const std::list<std::string>& theArguments,
340 Events_InfoMessage& theError) const
342 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
343 theError = "The attribute with the %1 type is not processed";
344 theError.arg(theAttribute->attributeType());
348 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
349 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
351 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
352 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
353 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
355 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
356 ObjectPtr aSelObject = aSelAttr->object(anInd);
357 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
358 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
359 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
360 if (aSelObject == *aMirIter) {
361 theError = "The object %1 is a result of mirror";
369 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
370 const std::list<std::string>& theArguments,
371 Events_InfoMessage& theError) const
373 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
374 theError = "The attribute with the %1 type is not processed";
375 theError.arg(theAttribute->attributeType());
379 // there is a check whether the feature contains a point and a linear edge or two point values
380 std::string aParamA = theArguments.front();
381 SessionPtr aMgr = ModelAPI_Session::get();
382 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
384 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
385 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
387 theError = "The %1 attribute should be %2";
388 theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
392 AttributeRefAttrPtr aRefAttrB =
393 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
395 // first attribute is a point, it may coincide with any object
396 if (!aRefAttrA->isObject())
399 ObjectPtr anObject = aRefAttrA->object();
400 if (!anObject.get()) {
401 theError = "%1 attribute has an empty object";
402 theError.arg(aParamA);
405 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
406 if (!aFeature.get()) {
407 theError = "%1 attribute has an empty feature";
408 theError.arg(aParamA);
412 if (aFeature->getKind() == SketchPlugin_Point::ID())
416 // second attribute is a point, it may coincide with any object
417 if (!aRefAttrB->isObject())
420 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
422 theError = "%1 attribute has an empty object";
423 theError.arg(theAttribute->id());
426 if (aFeature->getKind() == SketchPlugin_Point::ID())
429 theError = "There is no an attribute filled by a point";
434 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
435 const std::list<std::string>& theArguments,
436 Events_InfoMessage& theError) const
438 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
439 theError = "The attribute with the %1 type is not processed";
440 theError.arg(theAttribute->attributeType());
444 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
445 AttributeRefListPtr aSelAttr =
446 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
448 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
449 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
450 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
451 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
452 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
453 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
455 std::list<ObjectPtr>::iterator anObjIter;
456 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
457 ObjectPtr aSelObject = aSelAttr->object(anInd);
458 anObjIter = anInitialObjects.begin();
459 for (; anObjIter != anInitialObjects.end(); anObjIter++)
460 if (aSelObject == *anObjIter)
462 if (anObjIter != anInitialObjects.end())
464 anObjIter = aCopiedObjects.begin();
465 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
466 if (aSelObject == *anObjIter) {
467 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
468 theError = "The object %1 is a result of copy";
476 bool SketchPlugin_SolverErrorValidator::isValid(
477 const std::shared_ptr<ModelAPI_Feature>& theFeature,
478 const std::list<std::string>& theArguments,
479 Events_InfoMessage& theError) const
481 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
483 if (!aAttributeString->value().empty()) {
484 theError = aAttributeString->value();
491 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
492 std::string theAttribute)
497 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
498 const FeaturePtr theFeature)
500 for(std::set<AttributePtr>::const_iterator
501 anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
502 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
503 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
506 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
507 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
508 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
509 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
510 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
511 if(anAttrRefA.get()) {
512 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
514 DocumentPtr aDoc = aResA->document();
516 FeaturePtr aFeatureA = aDoc->feature(aResA);
517 if(aFeatureA.get() && aFeatureA == theFeature) {
523 if(anAttrRefB.get()) {
524 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
526 DocumentPtr aDoc = aResB->document();
528 FeaturePtr aFeatureB = aDoc->feature(aResB);
529 if(aFeatureB.get() && aFeatureB == theFeature) {
540 static bool isPointPointCoincidence(const FeaturePtr& theCoincidence)
542 AttributeRefAttrPtr aRefAttr[2] = {
543 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_A()),
544 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_B())
547 bool arePoints = true;
548 for (int i = 0; i < 2 && arePoints; ++i) {
549 if (aRefAttr[i]->isObject()) {
550 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr[i]->object());
551 arePoints = aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID();
553 arePoints = aRefAttr[i]->attr().get() != NULL;
558 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
559 const std::list<std::string>& theArguments,
560 Events_InfoMessage& theError) const
562 AttributeRefAttrPtr aPointRefAttr =
563 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
564 if(!aPointRefAttr.get()) {
565 theError = "Error: Point not selected.";
569 AttributePtr aPointAttribute = aPointRefAttr->attr();
570 if (!aPointAttribute.get()) {
571 theError = "Error: Bad point selected.";
574 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
575 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
577 // Obtain constraint coincidence for the fillet point.
578 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
579 FeaturePtr aConstraintCoincidence;
580 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
581 anIt != aRefsList.cend(); ++anIt) {
582 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
583 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
584 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
585 if (!isPointPointCoincidence(aConstrFeature))
588 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
589 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
590 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
591 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
593 AttributePtr anAttrA = anAttrRefA->attr();
594 if(aPointAttribute == anAttrA) {
595 aConstraintCoincidence = aConstrFeature;
599 AttributePtr anAttrB = anAttrRefB->attr();
600 if(aPointAttribute == anAttrB) {
601 aConstraintCoincidence = aConstrFeature;
607 if(!aConstraintCoincidence.get()) {
608 theError = "Error: one of the selected point does not have coicidence.";
612 // Get coincides from constraint.
613 std::set<FeaturePtr> aCoinsides;
614 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
615 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
618 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
619 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
623 // Remove points and external lines from set of coincides.
624 std::set<FeaturePtr> aNewSetOfCoincides;
625 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
626 anIt != aCoinsides.end(); ++anIt) {
627 std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
628 std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
629 if(aSketchEntity.get() && (aSketchEntity->isCopy() || aSketchEntity->isExternal())) {
632 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
633 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
636 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
637 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
638 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
639 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
640 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
641 if(aDistSelectedArcCenter < tolerance) {
645 aNewSetOfCoincides.insert(*anIt);
647 aCoinsides = aNewSetOfCoincides;
649 // If we still have more than two coincides remove auxilary entities from set of coincides.
650 if(aCoinsides.size() > 2) {
651 aNewSetOfCoincides.clear();
652 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
653 anIt != aCoinsides.end(); ++anIt) {
654 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
655 aNewSetOfCoincides.insert(*anIt);
658 aCoinsides = aNewSetOfCoincides;
661 if(aCoinsides.size() != 2) {
662 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
666 // Check that selected edges don't have tangent constraint.
667 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
668 FeaturePtr aFirstFeature = *anIt++;
669 FeaturePtr aSecondFeature = *anIt;
670 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
671 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
672 theError = "Error: Edges in selected point has tangent constraint.";
676 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
677 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
678 aResIt != aFirstResults.end(); ++aResIt) {
679 ResultPtr aRes = *aResIt;
680 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
681 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
682 theError = "Error: Edges in selected point has tangent constraint.";
687 // Check the features are not tangent
688 std::shared_ptr<GeomAPI_Shape> aFirstShape = aFirstFeature->lastResult()->shape();
689 std::shared_ptr<GeomAPI_Shape> aSecondShape = aSecondFeature->lastResult()->shape();
690 if (!aFirstShape || !aFirstShape->isEdge() ||
691 !aSecondShape || !aSecondShape->isEdge()) {
692 theError = "Error: At least on of the features is not an edge";
696 std::shared_ptr<GeomAPI_Edge> anEdge1 = std::dynamic_pointer_cast<GeomAPI_Edge>(aFirstShape);
697 std::shared_ptr<GeomAPI_Edge> anEdge2 = std::dynamic_pointer_cast<GeomAPI_Edge>(aSecondShape);
699 static const double TOL = 1.e-7;
700 if (anEdge1->isLine() && anEdge2->isLine()) {
701 // Check that lines not collinear
702 std::shared_ptr<GeomAPI_Dir> aDir1 = anEdge1->line()->direction();
703 std::shared_ptr<GeomAPI_Dir> aDir2 = anEdge2->line()->direction();
704 double aCross = aDir1->cross(aDir2)->squareModulus();
705 if (aCross < TOL * TOL)
707 } else if (anEdge1->isArc() && anEdge2->isArc()) {
708 // check the circles are not tangent
709 std::shared_ptr<GeomAPI_Circ> aCirc1 = anEdge1->circle();
710 std::shared_ptr<GeomAPI_Circ> aCirc2 = anEdge2->circle();
711 double aDistCC = aCirc1->center()->distance(aCirc2->center());
712 double aRadSum = aCirc1->radius() + aCirc2->radius();
713 double aRadDiff = fabs(aCirc1->radius() - aCirc2->radius());
714 if (fabs(aDistCC - aRadSum) < TOL || fabs(aDistCC - aRadDiff) < TOL)
717 // check whether line and arc are tangent
718 std::shared_ptr<GeomAPI_Circ> aCirc;
719 std::shared_ptr<GeomAPI_Lin> aLine;
720 if (anEdge1->isLine()) {
721 aLine = anEdge1->line();
722 aCirc = anEdge2->circle();
724 aCirc = anEdge1->circle();
725 aLine = anEdge2->line();
728 double aDistCL = aLine->distance(aCirc->center());
729 if (fabs(aDistCL - aCirc->radius()) < TOL)
736 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
737 const std::list<std::string>& theArguments,
738 Events_InfoMessage& theError) const
740 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
741 theError = "The attribute with the %1 type is not processed";
742 theError.arg(theAttribute->attributeType());
746 // there is a check whether the feature contains a point and a linear edge or two point values
747 std::string aParamA = theArguments.front();
748 SessionPtr aMgr = ModelAPI_Session::get();
749 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
751 FeaturePtr anAttributeFeature =
752 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
753 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
754 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
756 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
759 for (int i = 0; i < 2; ++i) {
760 if (!aRefAttrs[i]->isObject())
763 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
765 if (aNbPoints + aNbLines != 0)
770 if (aFeature->getKind() == SketchPlugin_Point::ID())
772 else if (aFeature->getKind() == SketchPlugin_Line::ID() ||
773 aFeature->getKind() == SketchPlugin_Arc::ID())
778 if (aNbPoints != 1 || aNbLines != 1) {
779 theError = "Middle point constraint allows points and lines only";
785 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
786 const std::list<std::string>& /*theArguments*/,
787 Events_InfoMessage& theError) const
789 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
790 theError = "The attribute with the %1 type is not processed";
791 theError.arg(theAttribute->attributeType());
794 FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
795 AttributeStringPtr anArcTypeAttr = anOwner->string(SketchPlugin_MacroArc::ARC_TYPE());
796 if (anArcTypeAttr && anArcTypeAttr->value() != SketchPlugin_MacroArc::ARC_TYPE_BY_TANGENT_EDGE())
797 return true; // not applicable for non-tangent arcs
799 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
800 AttributePtr anAttr = aRefAttr->attr();
802 theError = "The attribute %1 should be a point";
803 theError.arg(theAttribute->id());
807 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
808 const std::string& aFeatureType = anAttrFeature->getKind();
809 if (aFeatureType == SketchPlugin_Arc::ID()) {
810 // selected point should not be a center of arc
811 const std::string& aPntId = anAttr->id();
812 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
813 theError = "The attribute %1 is not supported";
814 theError.arg(aPntId);
818 else if (aFeatureType == SketchPlugin_Line::ID()) {
819 // selected point should be bound point of line
820 const std::string& aPntId = anAttr->id();
821 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
822 theError = "The attribute %1 is not supported";
823 theError.arg(aPntId);
828 theError = "Unable to build tangent arc on %1";
829 theError.arg(anAttrFeature->getKind());
836 bool SketchPlugin_ArcTransversalPointValidator::isValid(
837 const AttributePtr& theAttribute,
838 const std::list<std::string>& /*theArguments*/,
839 Events_InfoMessage& theError) const
841 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
842 theError = "The attribute with the %1 type is not processed";
843 theError.arg(theAttribute->attributeType());
846 FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
847 AttributeStringPtr anArcTypeAttr = anOwner->string(SketchPlugin_MacroArc::ARC_TYPE());
849 anArcTypeAttr->value() != SketchPlugin_MacroArc::ARC_TYPE_BY_TRANSVERSAL_LINE())
850 return true; // not applicable for non-transversal arcs
852 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
853 AttributePtr anAttr = aRefAttr->attr();
855 theError = "The attribute %1 should be a point";
856 theError.arg(theAttribute->id());
860 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
861 const std::string& aFeatureType = anAttrFeature->getKind();
862 if (aFeatureType == SketchPlugin_Line::ID()) {
863 // selected point should be bound point of line
864 const std::string& aPntId = anAttr->id();
865 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
866 theError = "The attribute %1 is not supported";
867 theError.arg(aPntId);
872 theError = "Unable to build transversal arc on %1";
873 theError.arg(anAttrFeature->getKind());
880 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
881 const std::list<std::string>& theArguments,
882 Events_InfoMessage& theError) const
884 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
885 theError = "The attribute with the %1 type is not processed";
886 theError.arg(theAttribute->attributeType());
889 AttributeSelectionPtr anExternalAttr =
890 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
891 std::shared_ptr<GeomAPI_Edge> anEdge;
892 if (anExternalAttr && anExternalAttr->value() && anExternalAttr->value()->isEdge()) {
893 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->value()));
894 } else if(anExternalAttr->context() && anExternalAttr->context()->shape() &&
895 anExternalAttr->context()->shape()->isEdge()) {
896 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->context()->shape()));
900 theError = "The attribute %1 should be an edge";
901 theError.arg(theAttribute->id());
906 std::shared_ptr<SketchPlugin_Sketch> aSketch;
907 std::set<AttributePtr> aRefs = anExternalAttr->owner()->data()->refsToMe();
908 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
909 for (; anIt != aRefs.end(); ++anIt) {
910 CompositeFeaturePtr aComp =
911 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
912 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
913 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
918 theError = "There is no sketch referring to the current feature";
922 // check the edge is intersected with sketch plane
923 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
925 std::list<GeomPointPtr> anIntersectionsPoints;
926 anEdge->intersectWithPlane(aPlane, anIntersectionsPoints);
927 if (anIntersectionsPoints.empty()) {
928 theError = "The edge is not intersected with sketch plane";
934 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
935 const std::list<std::string>& theArguments,
936 Events_InfoMessage& theError) const
940 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
941 theError = "The attribute with the %1 type is not processed";
942 theError.arg(theAttribute->attributeType());
945 AttributeReferencePtr aFeatureAttr =
946 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
948 ObjectPtr anAttrObject = aFeatureAttr->value();
949 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
953 std::string aKind = anAttrFeature->getKind();
954 if (aKind == SketchPlugin_Line::ID() ||
955 aKind == SketchPlugin_Arc::ID() ||
956 aKind == SketchPlugin_Circle::ID()) {
958 std::set<ResultPtr> anEdgeShapes;
959 ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
960 if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
963 // coincidences to the feature
964 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
965 ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
966 SketchPlugin_ConstraintCoincidence::ID(),
967 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
969 GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
970 std::shared_ptr<SketchPlugin_Feature> aSFeature =
971 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
972 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
974 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
975 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
976 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
977 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
978 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
979 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
980 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
981 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
983 typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
984 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
985 std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
986 PointToRefsMap aPointsInfo;
988 ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
989 aX->dir(), aDirY, aPointsInfo);
990 int aCoincidentToFeature = (int)aPointsInfo.size();
991 if (aKind == SketchPlugin_Circle::ID())
992 aValid = aCoincidentToFeature >= 2;
994 aValid = aCoincidentToFeature >= 1;
1000 bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
1001 const std::list<std::string>& theArguments,
1002 Events_InfoMessage& theError) const
1004 bool aValid = false;
1006 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
1007 theError = "The attribute with the %1 type is not processed";
1008 theError.arg(theAttribute->attributeType());
1011 AttributeReferencePtr aBaseObjectAttr =
1012 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
1014 std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
1015 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
1017 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1019 AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
1020 aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
1021 aBaseObject = aBaseObjectAttr->value();
1024 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
1028 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1029 std::dynamic_pointer_cast<SketchPlugin_Feature>(aBaseFeature);
1030 if (!aSketchFeature.get() || aSketchFeature->isCopy())
1033 std::string aKind = aBaseFeature->getKind();
1034 if (aKind != SketchPlugin_Line::ID() &&
1035 aKind != SketchPlugin_Arc::ID() &&
1036 aKind != SketchPlugin_Circle::ID())
1040 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1041 aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
1043 SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
1045 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
1046 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
1047 anAttributePnt2d->y());
1049 std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
1050 std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
1051 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
1052 std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
1053 SketchPlugin_Trim::fillObjectShapes(aBaseObject, aSketch->data()->owner(),
1054 aCashedShapes, anObjectToPoints);
1055 const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
1057 return aShapes.size() > 1;
1060 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
1061 const std::list<std::string>& theArguments,
1062 Events_InfoMessage& theError) const
1064 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
1065 theError = "The attribute with the %1 type is not processed";
1066 theError.arg(theAttribute->attributeType());
1070 AttributeSelectionPtr aFeatureAttr =
1071 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
1072 std::shared_ptr<GeomAPI_Edge> anEdge;
1073 std::shared_ptr<SketchPlugin_Feature> aSketchFeature;
1074 if (aFeatureAttr.get()) {
1075 GeomShapePtr aVal = aFeatureAttr->value();
1076 ResultPtr aRes = aFeatureAttr->context();
1077 if (aVal && aVal->isVertex())
1078 return true; // vertex is always could be projected
1079 if (aVal && aVal->isEdge()) {
1080 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
1081 } else if(aRes && aRes->shape()) {
1082 if (aRes->shape()->isVertex())
1083 return true; // vertex is always could be projected
1084 else if (aRes->shape()->isEdge())
1085 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
1088 // try to convert result to sketch feature
1091 std::dynamic_pointer_cast<SketchPlugin_Feature>(ModelAPI_Feature::feature(aRes));
1095 theError = "The attribute %1 should be an edge or vertex";
1096 theError.arg(theAttribute->id());
1101 std::shared_ptr<SketchPlugin_Sketch> aSketch;
1102 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
1103 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
1104 for (; anIt != aRefs.end(); ++anIt) {
1105 CompositeFeaturePtr aComp =
1106 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
1107 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
1108 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
1113 theError = "There is no sketch referring to the current feature";
1116 if (aSketchFeature && aSketch.get() == aSketchFeature->sketch()) {
1117 theError = "Unable to project feature from the same sketch";
1121 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1122 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
1123 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
1125 if (anEdge->isLine()) {
1126 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1127 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
1128 double aDot = fabs(aNormal->dot(aLineDir));
1129 bool aValid = fabs(aDot - 1.0) >= tolerance * tolerance;
1131 theError = "Error: Line is orthogonal to the sketch plane.";
1134 else if (anEdge->isCircle() || anEdge->isArc()) {
1135 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1136 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
1137 double aDot = fabs(aNormal->dot(aCircNormal));
1138 bool aValid = aDot >= tolerance * tolerance;
1140 theError.arg(anEdge->isCircle() ? "Error: Circle is orthogonal to the sketch plane."
1141 : "Error: Arc is orthogonal to the sketch plane.");
1144 else if (anEdge->isEllipse()) {
1145 std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
1146 std::shared_ptr<GeomAPI_Dir> anEllipseNormal = anEllipse->normal();
1147 double aDot = fabs(aNormal->dot(anEllipseNormal));
1148 bool aValid = aDot >= tolerance * tolerance;
1150 theError.arg(anEdge->isEllipse() ? "Error: Ellipse is orthogonal to the sketch plane."
1151 : "Error: Elliptic Arc is orthogonal to the sketch plane.");
1155 theError = "Error: Selected object is not line, circle or arc.";
1160 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
1161 const std::set<FeaturePtr>& theSet2)
1163 std::set<FeaturePtr> aCommon;
1164 if (theSet1.empty() || theSet2.empty())
1167 std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1168 for (; anIt2 != theSet2.end(); ++anIt2)
1169 if (theSet1.find(*anIt2) != theSet1.end())
1170 aCommon.insert(*anIt2);
1174 bool SketchPlugin_DifferentReferenceValidator::isValid(
1175 const AttributePtr& theAttribute,
1176 const std::list<std::string>& theArguments,
1177 Events_InfoMessage& theError) const
1179 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1181 int aNbFeaturesReferred = 0;
1182 int aNbAttributesReferred = 0;
1183 std::set<FeaturePtr> aCommonReferredFeatures;
1185 // find all features referred by attributes listed in theArguments
1186 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1187 for (; anArgIt != theArguments.end(); ++anArgIt) {
1188 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1192 std::set<FeaturePtr> aCoincidentFeatures;
1193 if (aRefAttr->isObject()) {
1194 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1196 aCoincidentFeatures.insert(aFeature);
1197 aNbFeaturesReferred += 1;
1200 AttributePoint2DPtr aPoint =
1201 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1203 aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1204 aNbAttributesReferred += 1;
1208 if (aCommonReferredFeatures.empty())
1209 aCommonReferredFeatures = aCoincidentFeatures;
1211 aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1213 if (aCommonReferredFeatures.empty())
1217 bool isOk = aNbFeaturesReferred < 1;
1218 if (aNbFeaturesReferred == 1) {
1219 if (aCommonReferredFeatures.size() == 1) {
1220 FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1221 isOk = aNbAttributesReferred <= 1 ||
1222 aFeature->getKind() == SketchPlugin_Circle::ID() ||
1223 aFeature->getKind() == SketchPlugin_Arc::ID();
1229 theError = "Attributes are referred to the same feature";
1233 bool SketchPlugin_DifferentPointReferenceValidator::isValid(
1234 const AttributePtr& theAttribute,
1235 const std::list<std::string>& theArguments,
1236 Events_InfoMessage& theError) const
1238 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1239 std::set<AttributePoint2DPtr> aReferredCoincidentPoints;
1241 // find all points referred by attributes listed in theArguments
1242 bool hasRefsToPoints = false;
1243 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1244 for (; anArgIt != theArguments.end(); ++anArgIt) {
1245 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1249 if (!aRefAttr->isObject()) {
1250 AttributePoint2DPtr aPoint =
1251 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1252 if (aReferredCoincidentPoints.empty())
1253 aReferredCoincidentPoints = SketchPlugin_Tools::findPointsCoincidentToPoint(aPoint);
1254 else if (aReferredCoincidentPoints.find(aPoint) == aReferredCoincidentPoints.end())
1255 return true; // non-coincident point has been found
1257 hasRefsToPoints = true;
1261 if (hasRefsToPoints)
1262 theError = "Attributes are referred to the same point";
1263 return !hasRefsToPoints;
1266 bool SketchPlugin_CirclePassedPointValidator::isValid(
1267 const AttributePtr& theAttribute,
1268 const std::list<std::string>&,
1269 Events_InfoMessage& theError) const
1271 static const std::string aErrorMessage(
1272 "Passed point refers to the same feature as a center point");
1274 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1276 AttributeRefAttrPtr aCenterRef =
1277 anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1278 AttributeRefAttrPtr aPassedRef =
1279 anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1281 if (!aPassedRef->isObject())
1284 FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1285 if (!aPassedFeature)
1288 if (aCenterRef->isObject()) {
1289 if (aCenterRef->object() == aPassedRef->object()) {
1290 theError = aErrorMessage;
1294 AttributePoint2DPtr aCenterPoint =
1295 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1297 std::set<FeaturePtr> aCoincidentFeatures =
1298 SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1299 // check one of coincident features is a feature referred by passed point
1300 std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1301 for(; anIt != aCoincidentFeatures.end(); ++anIt)
1302 if (*anIt == aPassedFeature) {
1303 theError = aErrorMessage;
1311 bool SketchPlugin_ThirdPointValidator::isValid(
1312 const AttributePtr& theAttribute,
1313 const std::list<std::string>& theArguments,
1314 Events_InfoMessage& theError) const
1316 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1317 return arePointsNotOnLine(anOwner, theError) &&
1318 arePointsNotSeparated(anOwner, theArguments, theError);
1321 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1322 const std::string& thePointAttrName,
1323 const std::string& theRefPointAttrName)
1325 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1326 theMacroCircle->attribute(thePointAttrName));
1327 AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1329 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1331 if (aRefAttr->isObject()) {
1332 // project a point onto selected feature
1333 std::shared_ptr<SketchPlugin_Feature> aFeature =
1334 std::dynamic_pointer_cast<SketchPlugin_Feature>(
1335 ModelAPI_Feature::feature(aRefAttr->object()));
1337 SketchPlugin_Sketch* aSketch = aFeature->sketch();
1338 std::shared_ptr<GeomAPI_Edge> anEdge =
1339 std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1341 std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1342 if (anEdge->isLine())
1343 aPoint3D = anEdge->line()->project(aPoint3D);
1344 else if (anEdge->isCircle())
1345 aPoint3D = anEdge->circle()->project(aPoint3D);
1347 aPoint = aSketch->to2D(aPoint3D);
1351 AttributePoint2DPtr anOtherPoint =
1352 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1354 aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1361 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1362 std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1364 if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1365 thePoints[0] = toPoint(theMacroFeature,
1366 SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1367 SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1368 thePoints[1] = toPoint(theMacroFeature,
1369 SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1370 SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1371 thePoints[2] = toPoint(theMacroFeature,
1372 SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1373 SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1374 } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1375 thePoints[0] = toPoint(theMacroFeature,
1376 SketchPlugin_MacroArc::START_POINT_2_ID(),
1377 SketchPlugin_MacroArc::START_POINT_REF_ID());
1378 thePoints[1] = toPoint(theMacroFeature,
1379 SketchPlugin_MacroArc::END_POINT_2_ID(),
1380 SketchPlugin_MacroArc::END_POINT_REF_ID());
1381 thePoints[2] = toPoint(theMacroFeature,
1382 SketchPlugin_MacroArc::PASSED_POINT_ID(),
1383 SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1387 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1388 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1389 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1391 static const double aTolerance = 1.e-7;
1392 if (thePoint1->distance(thePoint2) < aTolerance ||
1393 thePoint1->distance(thePoint3) < aTolerance)
1396 std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1397 thePoint2->y() - thePoint1->y()));
1398 std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1399 thePoint3->y() - thePoint1->y()));
1400 return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1403 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1404 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1405 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1407 static const double aTolerance = 1.e-7;
1408 std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1409 std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1411 std::shared_ptr<GeomAPI_XYZ> aVec1 = thePoint1->xyz()->decreased(aLineLoc);
1412 // the first point is on the line
1413 if (aVec1->squareModulus() < aTolerance * aTolerance)
1415 std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(aVec1));
1416 std::shared_ptr<GeomAPI_XYZ> aVec2 = thePoint2->xyz()->decreased(aLineLoc);
1417 // the second point is on the line
1418 if (aVec2->squareModulus() < aTolerance * aTolerance)
1420 std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(aVec2));
1422 return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1425 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1426 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1427 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1429 static const double aTolerance = 1.e-7;
1430 std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1431 double aDistP1C = thePoint1->distance(aCenter);
1432 double aDistP2C = thePoint2->distance(aCenter);
1433 return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1436 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1437 const FeaturePtr& theMacroFeature,
1438 Events_InfoMessage& theError) const
1440 static const std::string aErrorPointsOnLine(
1441 "Selected points are on the same line");
1443 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1444 threePointsOfFeature(theMacroFeature, aPoints);
1446 if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1447 theError = aErrorPointsOnLine;
1453 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1454 const FeaturePtr& theMacroFeature,
1455 const std::list<std::string>& theArguments,
1456 Events_InfoMessage& theError) const
1458 static const std::string aErrorPointsApart(
1459 "Selected entity is lying between first two points");
1461 AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1462 FeaturePtr aRefByThird;
1463 if (aThirdPointRef->isObject())
1464 aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1468 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1469 threePointsOfFeature(theMacroFeature, aPoints);
1471 std::shared_ptr<GeomAPI_Edge> aThirdShape =
1472 std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1476 SketchPlugin_Sketch* aSketch =
1477 std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1478 std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1479 std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1482 if (aThirdShape->isLine())
1483 isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1484 else if (aThirdShape->isCircle() || aThirdShape->isArc())
1485 isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1488 theError = aErrorPointsApart;
1492 bool SketchPlugin_ArcEndPointValidator::isValid(
1493 const AttributePtr& theAttribute,
1494 const std::list<std::string>& theArguments,
1495 Events_InfoMessage& theError) const
1497 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1498 AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1500 if(!anEndPointRef.get()) {
1504 ObjectPtr anObject = anEndPointRef->object();
1505 AttributePtr anAttr = anEndPointRef->attr();
1506 if(!anObject.get() && !anAttr.get()) {
1510 if(anEndPointRef->attr().get()) {
1514 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1516 GeomShapePtr aShape = aResult->shape();
1517 if(aShape.get() && aShape->isVertex()) {
1522 aFeature = ModelAPI_Feature::feature(anObject);
1523 if(aFeature.get()) {
1524 if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1532 static GeomShapePtr toInfiniteEdge(const GeomShapePtr theShape)
1534 if(!theShape.get()) {
1538 if(!theShape->isEdge()) {
1542 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
1548 if(anEdge->isLine()) {
1549 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1550 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::line(aLine);
1554 if(anEdge->isCircle() || anEdge->isArc()) {
1555 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1556 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCircle);
1563 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1564 const AttributePtr& theAttribute,
1565 const std::list<std::string>& theArguments,
1566 Events_InfoMessage& theError) const
1568 std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1569 std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1570 AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1572 if(!anEndPointRef.get()) {
1576 GeomShapePtr anArcShape = toInfiniteEdge(anArcFeature->getArcShape(false));
1578 if(!anArcShape.get() || anArcShape->isNull()) {
1582 ObjectPtr anObject = anEndPointRef->object();
1583 AttributePtr anAttr = anEndPointRef->attr();
1584 if(!anObject.get() && !anAttr.get()) {
1588 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1590 GeomShapePtr aShape = aResult->shape();
1591 if (!aShape->isEdge())
1593 aShape = toInfiniteEdge(aShape);
1594 if(aShape.get() && !aShape->isNull()) {
1595 if(anArcShape->isIntersect(aShape)) {
1601 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1602 if(aSelectedFeature.get()) {
1603 std::list<ResultPtr> aResults = aSelectedFeature->results();
1604 for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1605 anIt != aResults.cend();
1608 GeomShapePtr aShape = (*anIt)->shape();
1609 if (!aShape->isEdge())
1611 aShape = toInfiniteEdge(aShape);
1612 if(aShape.get() && !aShape->isNull()) {
1613 if(anArcShape->isIntersect(aShape)) {
1623 bool SketchPlugin_HasNoConstraint::isValid(const AttributePtr& theAttribute,
1624 const std::list<std::string>& theArguments,
1625 Events_InfoMessage& theError) const
1627 std::set<std::string> aFeatureKinds;
1628 for (std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1629 anArgIt != theArguments.end(); anArgIt++) {
1630 aFeatureKinds.insert(*anArgIt);
1633 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1634 theError = "The attribute with the %1 type is not processed";
1635 theError.arg(theAttribute->attributeType());
1639 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
1641 bool isObject = aRefAttr->isObject();
1643 theError = "It uses an empty object";
1646 ObjectPtr anObject = aRefAttr->object();
1647 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
1648 if (!aFeature.get()) {
1649 theError = "The feature of the checked attribute is empty";
1653 FeaturePtr aCurrentFeature = ModelAPI_Feature::feature(aRefAttr->owner());
1655 std::set<AttributePtr> aRefsList = anObject->data()->refsToMe();
1656 std::set<AttributePtr>::const_iterator anIt = aRefsList.begin();
1657 for (; anIt != aRefsList.end(); anIt++) {
1658 FeaturePtr aRefFeature = ModelAPI_Feature::feature((*anIt)->owner());
1659 if (aRefFeature.get() && aCurrentFeature != aRefFeature &&
1660 aFeatureKinds.find(aRefFeature->getKind()) != aFeatureKinds.end())
1661 return false; // constraint is found, that means that the check is not valid
1666 bool SketchPlugin_ReplicationReferenceValidator::isValid(
1667 const AttributePtr& theAttribute,
1668 const std::list<std::string>& theArguments,
1669 Events_InfoMessage& theError) const
1671 AttributeRefAttrPtr aRefAttr =
1672 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1675 theError = "Incorrect attribute";
1680 if (aRefAttr->isObject())
1681 anOwner = aRefAttr->object();
1684 AttributePtr anAttr = aRefAttr->attr();
1685 anOwner = anAttr->owner();
1687 FeaturePtr anAttrOwnerFeature = ModelAPI_Feature::feature(anOwner);
1688 if (!anAttrOwnerFeature)
1690 AttributeBooleanPtr aCopyAttr = anAttrOwnerFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
1691 if (!aCopyAttr || !aCopyAttr->value())
1692 return true; // feature is not a copy, thus valid
1694 // check the copy feature is already referred by the "Multi" feature
1695 FeaturePtr aMultiFeature = ModelAPI_Feature::feature(theAttribute->owner());
1696 AttributeRefListPtr aRefList = aMultiFeature->reflist(theArguments.front());
1697 for (int i = 0; i < aRefList->size(); ++i)
1699 FeaturePtr aRefOwner = ModelAPI_Feature::feature(aRefList->object(i));
1700 if (aRefOwner == anAttrOwnerFeature)
1702 theError = "Attribute refers to the object generated by this feature";
1710 bool SketchPlugin_SketchFeatureValidator::isValid(const AttributePtr& theAttribute,
1711 const std::list<std::string>& theArguments,
1712 Events_InfoMessage& theError) const
1714 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1715 theError = "The attribute with the %1 type is not processed";
1716 theError.arg(theAttribute->attributeType());
1720 // check the attribute refers to a sketch feature
1721 AttributeRefAttrPtr aRefAttr =
1722 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1723 bool isSketchFeature = aRefAttr->isObject();
1724 if (isSketchFeature) {
1725 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1726 isSketchFeature = aFeature.get() != NULL;
1727 if (isSketchFeature) {
1728 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1729 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1730 isSketchFeature = aSketchFeature.get() != NULL;
1734 if (isSketchFeature)
1737 theError = "The object selected is not a sketch feature";
1741 bool SketchPlugin_MultiRotationAngleValidator::isValid(const AttributePtr& theAttribute,
1742 const std::list<std::string>& theArguments,
1743 Events_InfoMessage& theError) const
1745 if (theAttribute->attributeType() != ModelAPI_AttributeDouble::typeId()) {
1746 theError = "The attribute with the %1 type is not processed";
1747 theError.arg(theAttribute->attributeType());
1751 AttributeDoublePtr anAngleAttr =
1752 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
1754 FeaturePtr aMultiRotation = ModelAPI_Feature::feature(theAttribute->owner());
1755 AttributeStringPtr anAngleType =
1756 aMultiRotation->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
1757 AttributeIntegerPtr aNbCopies =
1758 aMultiRotation->integer(SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID());
1760 if (anAngleType->value() != "FullAngle")
1762 double aFullAngleValue = anAngleAttr->value() * (aNbCopies->value() - 1);
1763 if (aFullAngleValue < -1.e-7 || aFullAngleValue > 359.9999999)
1765 theError = "Rotation single angle should produce full angle less than 360 degree";
1771 double aFullAngleValue = anAngleAttr->value();
1772 if (aFullAngleValue < -1.e-7 || aFullAngleValue > 360.0000001)
1774 theError = "Rotation full angle should be in range [0, 360]";