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 "SketchPlugin_Validators.h"
23 #include "SketchPlugin_Arc.h"
24 #include "SketchPlugin_Circle.h"
25 #include "SketchPlugin_ConstraintCoincidence.h"
26 #include "SketchPlugin_ConstraintDistance.h"
27 #include "SketchPlugin_ConstraintRigid.h"
28 #include "SketchPlugin_ConstraintTangent.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_Lin.h>
64 #include <GeomAPI_Edge.h>
65 #include <GeomAPI_Vertex.h>
66 #include <GeomDataAPI_Point2D.h>
71 const double tolerance = 1.e-7;
73 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
74 const std::list<std::string>& theArguments,
75 Events_InfoMessage& theError) const
77 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
78 theError = "The attribute with the %1 type is not processed";
79 theError.arg(theAttribute->attributeType());
83 // there is a check whether the feature contains a point and a linear edge or two point values
84 std::string aParamA = theArguments.front();
85 SessionPtr aMgr = ModelAPI_Session::get();
86 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
88 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
90 bool isObject = aRefAttr->isObject();
92 // an attribute is a point. A point value is valid always for the distance
95 // 1. check whether the references object is a linear
96 ObjectPtr anObject = aRefAttr->object();
98 const ModelAPI_AttributeValidator* aShapeValidator =
99 dynamic_cast<const ModelAPI_AttributeValidator*>(
100 aFactory->validator("GeomValidators_ShapeType"));
101 std::list<std::string> anArguments;
102 anArguments.push_back("circle");
103 Events_InfoMessage aCircleError;
104 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
105 // the circle line is not a valid case
107 theError = "Circle can not be used in distance constraint";
112 anArguments.push_back("line");
113 Events_InfoMessage aLineError;
114 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
115 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
117 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
118 // If it is a line then we have to check that first attribute id not a line
119 std::shared_ptr<SketchPlugin_Feature> aSFeature =
120 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
121 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
122 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
123 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
124 aFeature->data(), aParamA, aPlane);
126 theError = "One of parameters of distance constraint should be a point";
134 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
135 const std::list<std::string>& theArguments,
136 Events_InfoMessage& theError) const
138 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
139 theError = "The attribute with the %1 type is not processed";
140 theError.arg(theAttribute->attributeType());
144 // there is a check whether the feature contains a point and a linear edge or two point values
145 std::string aParamA = theArguments.front();
146 SessionPtr aMgr = ModelAPI_Session::get();
147 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
149 FeaturePtr anAttributeFeature =
150 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
151 AttributeRefAttrPtr aRefAttr =
152 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
154 bool isObject = aRefAttr->isObject();
155 ObjectPtr anObject = aRefAttr->object();
156 if (isObject && anObject.get()) {
157 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
159 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
160 ObjectPtr aOtherObject = aOtherAttr->object();
161 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
165 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
166 if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
167 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
168 theError = "It refers to a %1, but %2 is neither an %3 nor %4";
169 theError.arg(SketchPlugin_Line::ID()).arg(aParamA)
170 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
174 else if (aRefFea->getKind() == SketchPlugin_Arc::ID() ||
175 aRefFea->getKind() == SketchPlugin_Circle::ID()) {
176 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
177 aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
178 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
179 theError = "It refers to an %1, but %2 is not a %3 or an %4 or a %5";
180 theError.arg(SketchPlugin_Arc::ID()).arg(aParamA)
181 .arg(SketchPlugin_Line::ID()).arg(SketchPlugin_Arc::ID())
182 .arg(SketchPlugin_Circle::ID());
187 theError = "It refers to %1, but should refer to %2 or %3 or %4";
188 theError.arg(aRefFea->getKind()).arg(SketchPlugin_Line::ID())
189 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
195 theError = "It uses an empty object";
202 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
203 const std::list<std::string>& theArguments,
204 Events_InfoMessage& theError) const
206 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
207 theError = "The attribute with the %1 type is not processed";
208 theError.arg(theAttribute->attributeType());
212 std::shared_ptr<SketchPlugin_Feature> aFeature =
213 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
217 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
219 SketchPlugin_Sketch* aSketch = aFeature->sketch();
220 int aNbFeatures = aSketch->numberOfSubs();
221 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
222 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
223 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
225 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
226 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
227 if (aRefAttr->isObject()) {
228 if (aRefAttr->object() == aRAttr->object()) {
229 ObjectPtr anObject = aRefAttr->object();
230 std::string aName = anObject.get() ? anObject->data()->name() : "";
231 theError = "The object %1 has been already fixed.";
236 else if (aRefAttr->attr() == aRAttr->attr()) {
237 AttributePtr anAttribute = aRefAttr->attr();
238 std::string aName = anAttribute.get() ? anAttribute->id() : "";
239 theError = "The attribute %1 has been already fixed.";
247 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
248 const std::list<std::string>& theArguments,
249 Events_InfoMessage& theError) const
251 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
252 theError = "The attribute with the %1 type is not processed";
253 theError.arg(theAttribute->attributeType());
257 std::string aParamA = theArguments.front();
258 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
259 AttributeRefAttrPtr aRefAttr[2];
260 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
261 aRefAttr[1] = aFeature->data()->refattr(aParamA);
263 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
264 theError = "Attributes can not be used in equal constraint";
268 std::string aType[2];
269 std::list<std::string> anArguments;
270 for (int i = 0; i < 2; i++) {
271 ObjectPtr anObject = aRefAttr[i]->object();
272 if (!anObject.get()) {
273 theError = "An empty object is used.";
277 aFeature = ModelAPI_Feature::feature(anObject);
278 if (!aFeature.get()) {
279 theError = "An empty feature is used.";
283 aType[i] = aFeature->getKind();
284 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
285 aFeature->getKind() != SketchPlugin_Circle::ID() &&
286 aFeature->getKind() != SketchPlugin_Arc::ID()) {
287 theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4";
288 theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
289 .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID());
290 // wrong type of attribute
295 if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
296 aType[0] != aType[1]) {
297 theError = "Feature with kinds %1 and %2 can not be equal.";
298 theError.arg(aType[0]).arg(aType[1]);
304 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
305 const std::list<std::string>& theArguments,
306 Events_InfoMessage& theError) const
308 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
309 theError = "The attribute with the %1 type is not processed";
310 theError.arg(theAttribute->attributeType());
314 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
315 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
317 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
318 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
319 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
321 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
322 ObjectPtr aSelObject = aSelAttr->object(anInd);
323 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
324 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
325 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
326 if (aSelObject == *aMirIter) {
327 theError = "The object %1 is a result of mirror";
335 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
336 const std::list<std::string>& theArguments,
337 Events_InfoMessage& theError) const
339 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
340 theError = "The attribute with the %1 type is not processed";
341 theError.arg(theAttribute->attributeType());
345 // there is a check whether the feature contains a point and a linear edge or two point values
346 std::string aParamA = theArguments.front();
347 SessionPtr aMgr = ModelAPI_Session::get();
348 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
350 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
351 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
353 theError = "The %1 attribute should be %2";
354 theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
358 AttributeRefAttrPtr aRefAttrB =
359 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
361 // first attribute is a point, it may coincide with any object
362 if (!aRefAttrA->isObject())
365 ObjectPtr anObject = aRefAttrA->object();
366 if (!anObject.get()) {
367 theError = "%1 attribute has an empty object";
368 theError.arg(aParamA);
371 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
372 if (!aFeature.get()) {
373 theError = "%1 attribute has an empty feature";
374 theError.arg(aParamA);
378 if (aFeature->getKind() == SketchPlugin_Point::ID())
382 // second attribute is a point, it may coincide with any object
383 if (!aRefAttrB->isObject())
386 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
388 theError = "%1 attribute has an empty object";
389 theError.arg(theAttribute->id());
392 if (aFeature->getKind() == SketchPlugin_Point::ID())
395 theError = "There is no an attribute filled by a point";
400 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
401 const std::list<std::string>& theArguments,
402 Events_InfoMessage& theError) const
404 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
405 theError = "The attribute with the %1 type is not processed";
406 theError.arg(theAttribute->attributeType());
410 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
411 AttributeRefListPtr aSelAttr =
412 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
414 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
415 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
416 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
417 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
418 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
419 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
421 std::list<ObjectPtr>::iterator anObjIter;
422 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
423 ObjectPtr aSelObject = aSelAttr->object(anInd);
424 anObjIter = anInitialObjects.begin();
425 for (; anObjIter != anInitialObjects.end(); anObjIter++)
426 if (aSelObject == *anObjIter)
428 if (anObjIter != anInitialObjects.end())
430 anObjIter = aCopiedObjects.begin();
431 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
432 if (aSelObject == *anObjIter) {
433 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
434 theError = "The object %1 is a result of copy";
442 bool SketchPlugin_SolverErrorValidator::isValid(
443 const std::shared_ptr<ModelAPI_Feature>& theFeature,
444 const std::list<std::string>& theArguments,
445 Events_InfoMessage& theError) const
447 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
449 if (!aAttributeString->value().empty()) {
450 theError = aAttributeString->value();
457 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
458 std::string theAttribute)
463 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
464 const FeaturePtr theFeature)
466 for(std::set<AttributePtr>::const_iterator
467 anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
468 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
469 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
472 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
473 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
474 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
475 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
476 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
477 if(anAttrRefA.get()) {
478 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
480 DocumentPtr aDoc = aResA->document();
482 FeaturePtr aFeatureA = aDoc->feature(aResA);
483 if(aFeatureA.get() && aFeatureA == theFeature) {
489 if(anAttrRefB.get()) {
490 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
492 DocumentPtr aDoc = aResB->document();
494 FeaturePtr aFeatureB = aDoc->feature(aResB);
495 if(aFeatureB.get() && aFeatureB == theFeature) {
506 static bool isPointPointCoincidence(const FeaturePtr& theCoincidence)
508 AttributeRefAttrPtr aRefAttr[2] = {
509 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_A()),
510 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_B())
513 bool arePoints = true;
514 for (int i = 0; i < 2 && arePoints; ++i) {
515 if (aRefAttr[i]->isObject()) {
516 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr[i]->object());
517 arePoints = aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID();
519 arePoints = aRefAttr[i]->attr().get() != NULL;
524 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
525 const std::list<std::string>& theArguments,
526 Events_InfoMessage& theError) const
528 AttributeRefAttrPtr aPointRefAttr =
529 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
530 if(!aPointRefAttr.get()) {
531 theError = "Error: Point not selected.";
535 AttributePtr aPointAttribute = aPointRefAttr->attr();
536 if (!aPointAttribute.get()) {
537 theError = "Error: Bad point selected.";
540 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
541 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
543 // Obtain constraint coincidence for the fillet point.
544 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
545 FeaturePtr aConstraintCoincidence;
546 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
547 anIt != aRefsList.cend(); ++anIt) {
548 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
549 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
550 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
551 if (!isPointPointCoincidence(aConstrFeature))
554 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
555 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
556 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
557 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
559 AttributePtr anAttrA = anAttrRefA->attr();
560 if(aPointAttribute == anAttrA) {
561 aConstraintCoincidence = aConstrFeature;
565 AttributePtr anAttrB = anAttrRefB->attr();
566 if(aPointAttribute == anAttrB) {
567 aConstraintCoincidence = aConstrFeature;
573 if(!aConstraintCoincidence.get()) {
574 theError = "Error: one of the selected point does not have coicidence.";
578 // Get coincides from constraint.
579 std::set<FeaturePtr> aCoinsides;
580 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
581 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
584 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
585 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
589 // Remove points and external lines from set of coincides.
590 std::set<FeaturePtr> aNewSetOfCoincides;
591 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
592 anIt != aCoinsides.end(); ++anIt) {
593 std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
594 std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
595 if(aSketchEntity.get() && (aSketchEntity->isCopy() || aSketchEntity->isExternal())) {
598 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
599 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
602 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
603 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
604 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
605 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
606 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
607 if(aDistSelectedArcCenter < tolerance) {
611 aNewSetOfCoincides.insert(*anIt);
613 aCoinsides = aNewSetOfCoincides;
615 // If we still have more than two coincides remove auxilary entities from set of coincides.
616 if(aCoinsides.size() > 2) {
617 aNewSetOfCoincides.clear();
618 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
619 anIt != aCoinsides.end(); ++anIt) {
620 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
621 aNewSetOfCoincides.insert(*anIt);
624 aCoinsides = aNewSetOfCoincides;
627 if(aCoinsides.size() != 2) {
628 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
632 // Check that selected edges don't have tangent constraint.
633 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
634 FeaturePtr aFirstFeature = *anIt++;
635 FeaturePtr aSecondFeature = *anIt;
636 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
637 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
638 theError = "Error: Edges in selected point has tangent constraint.";
642 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
643 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
644 aResIt != aFirstResults.end(); ++aResIt) {
645 ResultPtr aRes = *aResIt;
646 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
647 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
648 theError = "Error: Edges in selected point has tangent constraint.";
653 // Check the features are not tangent
654 std::shared_ptr<GeomAPI_Shape> aFirstShape = aFirstFeature->lastResult()->shape();
655 std::shared_ptr<GeomAPI_Shape> aSecondShape = aSecondFeature->lastResult()->shape();
656 if (!aFirstShape || !aFirstShape->isEdge() ||
657 !aSecondShape || !aSecondShape->isEdge()) {
658 theError = "Error: At least on of the features is not an edge";
662 std::shared_ptr<GeomAPI_Edge> anEdge1 = std::dynamic_pointer_cast<GeomAPI_Edge>(aFirstShape);
663 std::shared_ptr<GeomAPI_Edge> anEdge2 = std::dynamic_pointer_cast<GeomAPI_Edge>(aSecondShape);
665 static const double TOL = 1.e-7;
666 if (anEdge1->isLine() && anEdge2->isLine()) {
667 // Check that lines not collinear
668 std::shared_ptr<GeomAPI_Dir> aDir1 = anEdge1->line()->direction();
669 std::shared_ptr<GeomAPI_Dir> aDir2 = anEdge2->line()->direction();
670 double aCross = aDir1->cross(aDir2)->squareModulus();
671 if (aCross < TOL * TOL)
673 } else if (anEdge1->isArc() && anEdge2->isArc()) {
674 // check the circles are not tangent
675 std::shared_ptr<GeomAPI_Circ> aCirc1 = anEdge1->circle();
676 std::shared_ptr<GeomAPI_Circ> aCirc2 = anEdge2->circle();
677 double aDistCC = aCirc1->center()->distance(aCirc2->center());
678 double aRadSum = aCirc1->radius() + aCirc2->radius();
679 double aRadDiff = fabs(aCirc1->radius() - aCirc2->radius());
680 if (fabs(aDistCC - aRadSum) < TOL || fabs(aDistCC - aRadDiff) < TOL)
683 // check whether line and arc are tangent
684 std::shared_ptr<GeomAPI_Circ> aCirc;
685 std::shared_ptr<GeomAPI_Lin> aLine;
686 if (anEdge1->isLine()) {
687 aLine = anEdge1->line();
688 aCirc = anEdge2->circle();
690 aCirc = anEdge1->circle();
691 aLine = anEdge2->line();
694 double aDistCL = aLine->distance(aCirc->center());
695 if (fabs(aDistCL - aCirc->radius()) < TOL)
702 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
703 const std::list<std::string>& theArguments,
704 Events_InfoMessage& theError) const
706 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
707 theError = "The attribute with the %1 type is not processed";
708 theError.arg(theAttribute->attributeType());
712 // there is a check whether the feature contains a point and a linear edge or two point values
713 std::string aParamA = theArguments.front();
714 SessionPtr aMgr = ModelAPI_Session::get();
715 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
717 FeaturePtr anAttributeFeature =
718 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
719 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
720 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
722 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
725 for (int i = 0; i < 2; ++i) {
726 if (!aRefAttrs[i]->isObject())
729 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
731 if (aNbPoints + aNbLines != 0)
736 if (aFeature->getKind() == SketchPlugin_Point::ID())
738 else if (aFeature->getKind() == SketchPlugin_Line::ID())
743 if (aNbPoints != 1 || aNbLines != 1) {
744 theError = "Middle point constraint allows points and lines only";
750 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
751 const std::list<std::string>& /*theArguments*/,
752 Events_InfoMessage& theError) const
754 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
755 theError = "The attribute with the %1 type is not processed";
756 theError.arg(theAttribute->attributeType());
759 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
760 AttributePtr anAttr = aRefAttr->attr();
762 theError = "The attribute %1 should be a point";
763 theError.arg(theAttribute->id());
767 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
768 const std::string& aFeatureType = anAttrFeature->getKind();
769 if (aFeatureType == SketchPlugin_Arc::ID()) {
770 // selected point should not be a center of arc
771 const std::string& aPntId = anAttr->id();
772 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
773 theError = "The attribute %1 is not supported";
774 theError.arg(aPntId);
778 else if (aFeatureType == SketchPlugin_Line::ID()) {
779 // selected point should be bound point of line
780 const std::string& aPntId = anAttr->id();
781 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
782 theError = "The attribute %1 is not supported";
783 theError.arg(aPntId);
788 theError = "Unable to build tangent arc on %1";
789 theError.arg(anAttrFeature->getKind());
796 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
797 const std::list<std::string>& theArguments,
798 Events_InfoMessage& theError) const
800 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
801 theError = "The attribute with the %1 type is not processed";
802 theError.arg(theAttribute->attributeType());
805 AttributeSelectionPtr anExternalAttr =
806 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
807 std::shared_ptr<GeomAPI_Edge> anEdge;
808 if (anExternalAttr && anExternalAttr->value() && anExternalAttr->value()->isEdge()) {
809 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->value()));
810 } else if(anExternalAttr->context() && anExternalAttr->context()->shape() &&
811 anExternalAttr->context()->shape()->isEdge()) {
812 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->context()->shape()));
816 theError = "The attribute %1 should be an edge";
817 theError.arg(theAttribute->id());
822 std::shared_ptr<SketchPlugin_Sketch> aSketch;
823 std::set<AttributePtr> aRefs = anExternalAttr->owner()->data()->refsToMe();
824 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
825 for (; anIt != aRefs.end(); ++anIt) {
826 CompositeFeaturePtr aComp =
827 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
828 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
829 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
834 theError = "There is no sketch referring to the current feature";
838 // check the edge is intersected with sketch plane
839 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
841 std::list<GeomPointPtr> anIntersectionsPoints;
842 anEdge->intersectWithPlane(aPlane, anIntersectionsPoints);
843 if (anIntersectionsPoints.empty()) {
844 theError = "The edge is not intersected with sketch plane";
850 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
851 const std::list<std::string>& theArguments,
852 Events_InfoMessage& theError) const
856 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
857 theError = "The attribute with the %1 type is not processed";
858 theError.arg(theAttribute->attributeType());
861 AttributeReferencePtr aFeatureAttr =
862 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
864 ObjectPtr anAttrObject = aFeatureAttr->value();
865 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
869 std::string aKind = anAttrFeature->getKind();
870 if (aKind == SketchPlugin_Line::ID() ||
871 aKind == SketchPlugin_Arc::ID() ||
872 aKind == SketchPlugin_Circle::ID()) {
874 std::set<ResultPtr> anEdgeShapes;
875 ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
876 if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
879 // coincidences to the feature
880 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
881 ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
882 SketchPlugin_ConstraintCoincidence::ID(),
883 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
885 GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
886 std::shared_ptr<SketchPlugin_Feature> aSFeature =
887 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
888 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
890 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
891 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
892 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
893 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
894 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
895 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
896 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
897 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
899 typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
900 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
901 std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
902 PointToRefsMap aPointsInfo;
904 ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
905 aX->dir(), aDirY, aPointsInfo);
906 int aCoincidentToFeature = (int)aPointsInfo.size();
907 if (aKind == SketchPlugin_Circle::ID())
908 aValid = aCoincidentToFeature >= 2;
910 aValid = aCoincidentToFeature >= 1;
916 bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
917 const std::list<std::string>& theArguments,
918 Events_InfoMessage& theError) const
922 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
923 theError = "The attribute with the %1 type is not processed";
924 theError.arg(theAttribute->attributeType());
927 AttributeReferencePtr aBaseObjectAttr =
928 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
930 std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
931 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
933 ObjectPtr aBaseObject = aBaseObjectAttr->value();
935 AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
936 aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
937 aBaseObject = aBaseObjectAttr->value();
940 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
944 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
945 std::dynamic_pointer_cast<SketchPlugin_Feature>(aBaseFeature);
946 if (!aSketchFeature.get() || aSketchFeature->isCopy())
949 std::string aKind = aBaseFeature->getKind();
950 if (aKind != SketchPlugin_Line::ID() &&
951 aKind != SketchPlugin_Arc::ID() &&
952 aKind != SketchPlugin_Circle::ID())
956 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
957 aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
959 SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
961 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
962 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
963 anAttributePnt2d->y());
965 std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
966 std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
967 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
968 std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
969 SketchPlugin_Trim::fillObjectShapes(aBaseObject, aSketch->data()->owner(),
970 aCashedShapes, anObjectToPoints);
971 const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
973 return aShapes.size() > 1;
976 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
977 const std::list<std::string>& theArguments,
978 Events_InfoMessage& theError) const
980 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
981 theError = "The attribute with the %1 type is not processed";
982 theError.arg(theAttribute->attributeType());
986 AttributeSelectionPtr aFeatureAttr =
987 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
988 std::shared_ptr<GeomAPI_Edge> anEdge;
989 std::shared_ptr<SketchPlugin_Feature> aSketchFeature;
990 if (aFeatureAttr.get()) {
991 GeomShapePtr aVal = aFeatureAttr->value();
992 ResultPtr aRes = aFeatureAttr->context();
993 if (aVal && aVal->isVertex())
994 return true; // vertex is always could be projected
995 if (aVal && aVal->isEdge()) {
996 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
997 } else if(aRes && aRes->shape()) {
998 if (aRes->shape()->isVertex())
999 return true; // vertex is always could be projected
1000 else if (aRes->shape()->isEdge())
1001 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
1004 // try to convert result to sketch feature
1007 std::dynamic_pointer_cast<SketchPlugin_Feature>(ModelAPI_Feature::feature(aRes));
1011 theError = "The attribute %1 should be an edge or vertex";
1012 theError.arg(theAttribute->id());
1017 std::shared_ptr<SketchPlugin_Sketch> aSketch;
1018 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
1019 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
1020 for (; anIt != aRefs.end(); ++anIt) {
1021 CompositeFeaturePtr aComp =
1022 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
1023 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
1024 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
1029 theError = "There is no sketch referring to the current feature";
1032 if (aSketchFeature && aSketch.get() == aSketchFeature->sketch()) {
1033 theError = "Unable to project feature from the same sketch";
1037 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1038 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
1039 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
1041 if (anEdge->isLine()) {
1042 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1043 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
1044 double aDot = fabs(aNormal->dot(aLineDir));
1045 bool aValid = fabs(aDot - 1.0) >= tolerance * tolerance;
1047 theError = "Error: Edge is already in the sketch plane.";
1050 else if (anEdge->isCircle() || anEdge->isArc()) {
1051 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1052 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
1053 double aDot = fabs(aNormal->dot(aCircNormal));
1054 bool aValid = fabs(aDot - 1.0) < tolerance * tolerance;
1056 theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
1057 : "Error: Arc is already in the sketch plane.");
1061 theError = "Error: Selected object is not line, circle or arc.";
1066 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
1067 const std::set<FeaturePtr>& theSet2)
1069 std::set<FeaturePtr> aCommon;
1070 if (theSet1.empty() || theSet2.empty())
1073 std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1074 for (; anIt2 != theSet2.end(); ++anIt2)
1075 if (theSet1.find(*anIt2) != theSet1.end())
1076 aCommon.insert(*anIt2);
1080 bool SketchPlugin_DifferentReferenceValidator::isValid(
1081 const AttributePtr& theAttribute,
1082 const std::list<std::string>& theArguments,
1083 Events_InfoMessage& theError) const
1085 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1087 int aNbFeaturesReferred = 0;
1088 int aNbAttributesReferred = 0;
1089 std::set<FeaturePtr> aCommonReferredFeatures;
1091 // find all features referred by attributes listed in theArguments
1092 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1093 for (; anArgIt != theArguments.end(); ++anArgIt) {
1094 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1098 std::set<FeaturePtr> aCoincidentFeatures;
1099 if (aRefAttr->isObject()) {
1100 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1102 aCoincidentFeatures.insert(aFeature);
1103 aNbFeaturesReferred += 1;
1106 AttributePoint2DPtr aPoint =
1107 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1109 aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1110 aNbAttributesReferred += 1;
1114 if (aCommonReferredFeatures.empty())
1115 aCommonReferredFeatures = aCoincidentFeatures;
1117 aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1119 if (aCommonReferredFeatures.empty())
1123 bool isOk = aNbFeaturesReferred < 1;
1124 if (aNbFeaturesReferred == 1) {
1125 if (aCommonReferredFeatures.size() == 1) {
1126 FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1127 isOk = aNbAttributesReferred <= 1 ||
1128 aFeature->getKind() == SketchPlugin_Circle::ID() ||
1129 aFeature->getKind() == SketchPlugin_Arc::ID();
1135 theError = "Attributes are referred to the same feature";
1139 bool SketchPlugin_DifferentPointReferenceValidator::isValid(
1140 const AttributePtr& theAttribute,
1141 const std::list<std::string>& theArguments,
1142 Events_InfoMessage& theError) const
1144 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1145 std::set<AttributePoint2DPtr> aReferredCoincidentPoints;
1147 // find all points referred by attributes listed in theArguments
1148 bool hasRefsToPoints = false;
1149 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1150 for (; anArgIt != theArguments.end(); ++anArgIt) {
1151 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1155 if (!aRefAttr->isObject()) {
1156 AttributePoint2DPtr aPoint =
1157 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1158 if (aReferredCoincidentPoints.empty())
1159 aReferredCoincidentPoints = SketchPlugin_Tools::findPointsCoincidentToPoint(aPoint);
1160 else if (aReferredCoincidentPoints.find(aPoint) == aReferredCoincidentPoints.end())
1161 return true; // non-coincident point has been found
1163 hasRefsToPoints = true;
1167 if (hasRefsToPoints)
1168 theError = "Attributes are referred to the same point";
1169 return !hasRefsToPoints;
1172 bool SketchPlugin_CirclePassedPointValidator::isValid(
1173 const AttributePtr& theAttribute,
1174 const std::list<std::string>&,
1175 Events_InfoMessage& theError) const
1177 static const std::string aErrorMessage(
1178 "Passed point refers to the same feature as a center point");
1180 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1182 AttributeRefAttrPtr aCenterRef =
1183 anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1184 AttributeRefAttrPtr aPassedRef =
1185 anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1187 if (!aPassedRef->isObject())
1190 FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1191 if (!aPassedFeature)
1194 if (aCenterRef->isObject()) {
1195 if (aCenterRef->object() == aPassedRef->object()) {
1196 theError = aErrorMessage;
1200 AttributePoint2DPtr aCenterPoint =
1201 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1203 std::set<FeaturePtr> aCoincidentFeatures =
1204 SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1205 // check one of coincident features is a feature referred by passed point
1206 std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1207 for(; anIt != aCoincidentFeatures.end(); ++anIt)
1208 if (*anIt == aPassedFeature) {
1209 theError = aErrorMessage;
1217 bool SketchPlugin_ThirdPointValidator::isValid(
1218 const AttributePtr& theAttribute,
1219 const std::list<std::string>& theArguments,
1220 Events_InfoMessage& theError) const
1222 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1223 return arePointsNotOnLine(anOwner, theError) &&
1224 arePointsNotSeparated(anOwner, theArguments, theError);
1227 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1228 const std::string& thePointAttrName,
1229 const std::string& theRefPointAttrName)
1231 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1232 theMacroCircle->attribute(thePointAttrName));
1233 AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1235 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1237 if (aRefAttr->isObject()) {
1238 // project a point onto selected feature
1239 std::shared_ptr<SketchPlugin_Feature> aFeature =
1240 std::dynamic_pointer_cast<SketchPlugin_Feature>(
1241 ModelAPI_Feature::feature(aRefAttr->object()));
1243 SketchPlugin_Sketch* aSketch = aFeature->sketch();
1244 std::shared_ptr<GeomAPI_Edge> anEdge =
1245 std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1247 std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1248 if (anEdge->isLine())
1249 aPoint3D = anEdge->line()->project(aPoint3D);
1250 else if (anEdge->isCircle())
1251 aPoint3D = anEdge->circle()->project(aPoint3D);
1253 aPoint = aSketch->to2D(aPoint3D);
1257 AttributePoint2DPtr anOtherPoint =
1258 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1260 aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1267 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1268 std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1270 if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1271 thePoints[0] = toPoint(theMacroFeature,
1272 SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1273 SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1274 thePoints[1] = toPoint(theMacroFeature,
1275 SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1276 SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1277 thePoints[2] = toPoint(theMacroFeature,
1278 SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1279 SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1280 } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1281 thePoints[0] = toPoint(theMacroFeature,
1282 SketchPlugin_MacroArc::START_POINT_2_ID(),
1283 SketchPlugin_MacroArc::START_POINT_REF_ID());
1284 thePoints[1] = toPoint(theMacroFeature,
1285 SketchPlugin_MacroArc::END_POINT_2_ID(),
1286 SketchPlugin_MacroArc::END_POINT_REF_ID());
1287 thePoints[2] = toPoint(theMacroFeature,
1288 SketchPlugin_MacroArc::PASSED_POINT_ID(),
1289 SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1293 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1294 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1295 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1297 static const double aTolerance = 1.e-7;
1298 if (thePoint1->distance(thePoint2) < aTolerance ||
1299 thePoint1->distance(thePoint3) < aTolerance)
1302 std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1303 thePoint2->y() - thePoint1->y()));
1304 std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1305 thePoint3->y() - thePoint1->y()));
1306 return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1309 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1310 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1311 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1313 static const double aTolerance = 1.e-7;
1314 std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1315 std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1317 std::shared_ptr<GeomAPI_XYZ> aVec1 = thePoint1->xyz()->decreased(aLineLoc);
1318 // the first point is on the line
1319 if (aVec1->squareModulus() < aTolerance * aTolerance)
1321 std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(aVec1));
1322 std::shared_ptr<GeomAPI_XYZ> aVec2 = thePoint2->xyz()->decreased(aLineLoc);
1323 // the second point is on the line
1324 if (aVec2->squareModulus() < aTolerance * aTolerance)
1326 std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(aVec2));
1328 return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1331 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1332 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1333 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1335 static const double aTolerance = 1.e-7;
1336 std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1337 double aDistP1C = thePoint1->distance(aCenter);
1338 double aDistP2C = thePoint2->distance(aCenter);
1339 return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1342 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1343 const FeaturePtr& theMacroFeature,
1344 Events_InfoMessage& theError) const
1346 static const std::string aErrorPointsOnLine(
1347 "Selected points are on the same line");
1349 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1350 threePointsOfFeature(theMacroFeature, aPoints);
1352 if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1353 theError = aErrorPointsOnLine;
1359 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1360 const FeaturePtr& theMacroFeature,
1361 const std::list<std::string>& theArguments,
1362 Events_InfoMessage& theError) const
1364 static const std::string aErrorPointsApart(
1365 "Selected entity is lying between first two points");
1367 AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1368 FeaturePtr aRefByThird;
1369 if (aThirdPointRef->isObject())
1370 aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1374 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1375 threePointsOfFeature(theMacroFeature, aPoints);
1377 std::shared_ptr<GeomAPI_Edge> aThirdShape =
1378 std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1382 SketchPlugin_Sketch* aSketch =
1383 std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1384 std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1385 std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1388 if (aThirdShape->isLine())
1389 isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1390 else if (aThirdShape->isCircle() || aThirdShape->isArc())
1391 isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1394 theError = aErrorPointsApart;
1398 bool SketchPlugin_ArcEndPointValidator::isValid(
1399 const AttributePtr& theAttribute,
1400 const std::list<std::string>& theArguments,
1401 Events_InfoMessage& theError) const
1403 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1404 AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1406 if(!anEndPointRef.get()) {
1410 ObjectPtr anObject = anEndPointRef->object();
1411 AttributePtr anAttr = anEndPointRef->attr();
1412 if(!anObject.get() && !anAttr.get()) {
1416 if(anEndPointRef->attr().get()) {
1420 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1422 GeomShapePtr aShape = aResult->shape();
1423 if(aShape.get() && aShape->isVertex()) {
1428 aFeature = ModelAPI_Feature::feature(anObject);
1429 if(aFeature.get()) {
1430 if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1438 static GeomShapePtr toInfiniteEdge(const GeomShapePtr theShape)
1440 if(!theShape.get()) {
1444 if(!theShape->isEdge()) {
1448 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
1454 if(anEdge->isLine()) {
1455 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1456 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::line(aLine);
1460 if(anEdge->isCircle() || anEdge->isArc()) {
1461 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1462 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCircle);
1469 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1470 const AttributePtr& theAttribute,
1471 const std::list<std::string>& theArguments,
1472 Events_InfoMessage& theError) const
1474 std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1475 std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1476 AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1478 if(!anEndPointRef.get()) {
1482 GeomShapePtr anArcShape = toInfiniteEdge(anArcFeature->getArcShape(false));
1484 if(!anArcShape.get() || anArcShape->isNull()) {
1488 ObjectPtr anObject = anEndPointRef->object();
1489 AttributePtr anAttr = anEndPointRef->attr();
1490 if(!anObject.get() && !anAttr.get()) {
1494 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1496 GeomShapePtr aShape = aResult->shape();
1497 if (!aShape->isEdge())
1499 aShape = toInfiniteEdge(aShape);
1500 if(aShape.get() && !aShape->isNull()) {
1501 if(anArcShape->isIntersect(aShape)) {
1507 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1508 if(aSelectedFeature.get()) {
1509 std::list<ResultPtr> aResults = aSelectedFeature->results();
1510 for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1511 anIt != aResults.cend();
1514 GeomShapePtr aShape = (*anIt)->shape();
1515 if (!aShape->isEdge())
1517 aShape = toInfiniteEdge(aShape);
1518 if(aShape.get() && !aShape->isNull()) {
1519 if(anArcShape->isIntersect(aShape)) {
1529 bool SketchPlugin_HasNoConstraint::isValid(const AttributePtr& theAttribute,
1530 const std::list<std::string>& theArguments,
1531 Events_InfoMessage& theError) const
1533 std::set<std::string> aFeatureKinds;
1534 for (std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1535 anArgIt != theArguments.end(); anArgIt++) {
1536 aFeatureKinds.insert(*anArgIt);
1539 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1540 theError = "The attribute with the %1 type is not processed";
1541 theError.arg(theAttribute->attributeType());
1545 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
1547 bool isObject = aRefAttr->isObject();
1549 theError = "It uses an empty object";
1552 ObjectPtr anObject = aRefAttr->object();
1553 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
1554 if (!aFeature.get()) {
1555 theError = "The feature of the checked attribute is empty";
1559 FeaturePtr aCurrentFeature = ModelAPI_Feature::feature(aRefAttr->owner());
1561 std::set<AttributePtr> aRefsList = anObject->data()->refsToMe();
1562 std::set<AttributePtr>::const_iterator anIt = aRefsList.begin();
1563 for (; anIt != aRefsList.end(); anIt++) {
1564 FeaturePtr aRefFeature = ModelAPI_Feature::feature((*anIt)->owner());
1565 if (aRefFeature.get() && aCurrentFeature != aRefFeature &&
1566 aFeatureKinds.find(aRefFeature->getKind()) != aFeatureKinds.end())
1567 return false; // constraint is found, that means that the check is not valid
1572 bool SketchPlugin_ReplicationReferenceValidator::isValid(
1573 const AttributePtr& theAttribute,
1574 const std::list<std::string>& theArguments,
1575 Events_InfoMessage& theError) const
1577 AttributeRefAttrPtr aRefAttr =
1578 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1581 theError = "Incorrect attribute";
1586 if (aRefAttr->isObject())
1587 anOwner = aRefAttr->object();
1590 AttributePtr anAttr = aRefAttr->attr();
1591 anOwner = anAttr->owner();
1593 FeaturePtr anAttrOwnerFeature = ModelAPI_Feature::feature(anOwner);
1594 if (!anAttrOwnerFeature)
1596 AttributeBooleanPtr aCopyAttr = anAttrOwnerFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
1597 if (!aCopyAttr || !aCopyAttr->value())
1598 return true; // feature is not a copy, thus valid
1600 // check the copy feature is already referred by the "Multi" feature
1601 FeaturePtr aMultiFeature = ModelAPI_Feature::feature(theAttribute->owner());
1602 AttributeRefListPtr aRefList = aMultiFeature->reflist(theArguments.front());
1603 for (int i = 0; i < aRefList->size(); ++i)
1605 FeaturePtr aRefOwner = ModelAPI_Feature::feature(aRefList->object(i));
1606 if (aRefOwner == anAttrOwnerFeature)
1608 theError = "Attribute refers to the object generated by this feature";
1616 bool SketchPlugin_SketchFeatureValidator::isValid(const AttributePtr& theAttribute,
1617 const std::list<std::string>& theArguments,
1618 Events_InfoMessage& theError) const
1620 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1621 theError = "The attribute with the %1 type is not processed";
1622 theError.arg(theAttribute->attributeType());
1626 // check the attribute refers to a sketch feature
1627 AttributeRefAttrPtr aRefAttr =
1628 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1629 bool isSketchFeature = aRefAttr->isObject();
1630 if (isSketchFeature) {
1631 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1632 isSketchFeature = aFeature.get() != NULL;
1633 if (isSketchFeature) {
1634 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1635 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1636 isSketchFeature = aSketchFeature.get() != NULL;
1640 if (isSketchFeature)
1643 theError = "The object selected is not a sketch feature";
1647 bool SketchPlugin_MultiRotationAngleValidator::isValid(const AttributePtr& theAttribute,
1648 const std::list<std::string>& theArguments,
1649 Events_InfoMessage& theError) const
1651 if (theAttribute->attributeType() != ModelAPI_AttributeDouble::typeId()) {
1652 theError = "The attribute with the %1 type is not processed";
1653 theError.arg(theAttribute->attributeType());
1657 AttributeDoublePtr anAngleAttr =
1658 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
1660 FeaturePtr aMultiRotation = ModelAPI_Feature::feature(theAttribute->owner());
1661 AttributeStringPtr anAngleType =
1662 aMultiRotation->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
1663 AttributeIntegerPtr aNbCopies =
1664 aMultiRotation->integer(SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID());
1666 if (anAngleType->value() != "FullAngle")
1668 double aFullAngleValue = anAngleAttr->value() * (aNbCopies->value() - 1);
1669 if (aFullAngleValue < -1.e-7 || aFullAngleValue > 359.9999999)
1671 theError = "Rotation single angle should produce full angle less than 360 degree";
1677 double aFullAngleValue = anAngleAttr->value();
1678 if (aFullAngleValue < -1.e-7 || aFullAngleValue > 360.0000001)
1680 theError = "Rotation full angle should be in range [0, 360]";