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 email : webmaster.salome@opencascade.com<mailto: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_Fillet.h"
29 #include "SketchPlugin_Line.h"
30 #include "SketchPlugin_MacroArc.h"
31 #include "SketchPlugin_MacroCircle.h"
32 #include "SketchPlugin_Point.h"
33 #include "SketchPlugin_Sketch.h"
34 #include "SketchPlugin_Trim.h"
35 #include "SketchPlugin_Tools.h"
37 #include "SketcherPrs_Tools.h"
39 #include <Events_InfoMessage.h>
41 #include <ModelAPI_Data.h>
42 #include <ModelAPI_Validator.h>
43 #include <ModelAPI_AttributeDouble.h>
44 #include <ModelAPI_AttributeRefAttr.h>
46 #include <ModelAPI_AttributeRefAttrList.h>
47 #include <ModelAPI_AttributeRefList.h>
48 #include <ModelAPI_AttributeSelectionList.h>
49 #include <ModelAPI_AttributeString.h>
50 #include <ModelAPI_Session.h>
51 #include <ModelAPI_Tools.h>
52 #include <ModelAPI_ResultConstruction.h>
54 #include <ModelGeomAlgo_Point2D.h>
55 #include <ModelGeomAlgo_Shape.h>
57 #include <GeomAlgoAPI_EdgeBuilder.h>
58 #include <GeomAlgoAPI_ShapeTools.h>
60 #include <GeomAPI_Circ.h>
61 #include <GeomAPI_Dir2d.h>
62 #include <GeomAPI_Lin.h>
63 #include <GeomAPI_Edge.h>
64 #include <GeomAPI_Vertex.h>
65 #include <GeomDataAPI_Point2D.h>
70 const double tolerance = 1.e-7;
72 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
73 const std::list<std::string>& theArguments,
74 Events_InfoMessage& theError) const
76 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
77 theError = "The attribute with the %1 type is not processed";
78 theError.arg(theAttribute->attributeType());
82 // there is a check whether the feature contains a point and a linear edge or two point values
83 std::string aParamA = theArguments.front();
84 SessionPtr aMgr = ModelAPI_Session::get();
85 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
87 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
89 bool isObject = aRefAttr->isObject();
91 // an attribute is a point. A point value is valid always for the distance
94 // 1. check whether the references object is a linear
95 ObjectPtr anObject = aRefAttr->object();
97 const ModelAPI_AttributeValidator* aShapeValidator =
98 dynamic_cast<const ModelAPI_AttributeValidator*>(
99 aFactory->validator("GeomValidators_ShapeType"));
100 std::list<std::string> anArguments;
101 anArguments.push_back("circle");
102 Events_InfoMessage aCircleError;
103 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
104 // the circle line is not a valid case
106 theError = "Circle can not be used in distance constraint";
111 anArguments.push_back("line");
112 Events_InfoMessage aLineError;
113 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
114 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
116 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
117 // If it is a line then we have to check that first attribute id not a line
118 std::shared_ptr<SketchPlugin_Feature> aSFeature =
119 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
120 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
121 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
122 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
123 aFeature->data(), aParamA, aPlane);
125 theError = "One of parameters of distance constraint should be a point";
133 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
134 const std::list<std::string>& theArguments,
135 Events_InfoMessage& theError) const
137 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
138 theError = "The attribute with the %1 type is not processed";
139 theError.arg(theAttribute->attributeType());
143 // there is a check whether the feature contains a point and a linear edge or two point values
144 std::string aParamA = theArguments.front();
145 SessionPtr aMgr = ModelAPI_Session::get();
146 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
148 FeaturePtr anAttributeFeature =
149 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
150 AttributeRefAttrPtr aRefAttr =
151 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
153 bool isObject = aRefAttr->isObject();
154 ObjectPtr anObject = aRefAttr->object();
155 if (isObject && anObject.get()) {
156 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
158 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
159 ObjectPtr aOtherObject = aOtherAttr->object();
160 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
164 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
165 if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
166 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
167 theError = "It refers to a %1, but %2 is neither an %3 nor %4";
168 theError.arg(SketchPlugin_Line::ID()).arg(aParamA)
169 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
173 else if (aRefFea->getKind() == SketchPlugin_Arc::ID() ||
174 aRefFea->getKind() == SketchPlugin_Circle::ID()) {
175 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
176 aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
177 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
178 theError = "It refers to an %1, but %2 is not a %3 or an %4 or a %5";
179 theError.arg(SketchPlugin_Arc::ID()).arg(aParamA)
180 .arg(SketchPlugin_Line::ID()).arg(SketchPlugin_Arc::ID())
181 .arg(SketchPlugin_Circle::ID());
186 theError = "It refers to %1, but should refer to %2 or %3 or %4";
187 theError.arg(aRefFea->getKind()).arg(SketchPlugin_Line::ID())
188 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
194 theError = "It uses an empty object";
201 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
202 const std::list<std::string>& theArguments,
203 Events_InfoMessage& theError) const
205 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
206 theError = "The attribute with the %1 type is not processed";
207 theError.arg(theAttribute->attributeType());
211 std::shared_ptr<SketchPlugin_Feature> aFeature =
212 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
216 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
218 SketchPlugin_Sketch* aSketch = aFeature->sketch();
219 int aNbFeatures = aSketch->numberOfSubs();
220 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
221 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
222 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
224 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
225 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
226 if (aRefAttr->isObject()) {
227 if (aRefAttr->object() == aRAttr->object()) {
228 ObjectPtr anObject = aRefAttr->object();
229 std::string aName = anObject.get() ? anObject->data()->name() : "";
230 theError = "The object %1 has been already fixed.";
235 else if (aRefAttr->attr() == aRAttr->attr()) {
236 AttributePtr anAttribute = aRefAttr->attr();
237 std::string aName = anAttribute.get() ? anAttribute->id() : "";
238 theError = "The attribute %1 has been already fixed.";
246 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
247 const std::list<std::string>& theArguments,
248 Events_InfoMessage& theError) const
250 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
251 theError = "The attribute with the %1 type is not processed";
252 theError.arg(theAttribute->attributeType());
256 std::string aParamA = theArguments.front();
257 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
258 AttributeRefAttrPtr aRefAttr[2];
259 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
260 aRefAttr[1] = aFeature->data()->refattr(aParamA);
262 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
263 theError = "Attributes can not be used in equal constraint";
267 std::string aType[2];
268 std::list<std::string> anArguments;
269 for (int i = 0; i < 2; i++) {
270 ObjectPtr anObject = aRefAttr[i]->object();
271 if (!anObject.get()) {
272 theError = "An empty object is used.";
276 aFeature = ModelAPI_Feature::feature(anObject);
277 if (!aFeature.get()) {
278 theError = "An empty feature is used.";
282 aType[i] = aFeature->getKind();
283 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
284 aFeature->getKind() != SketchPlugin_Circle::ID() &&
285 aFeature->getKind() != SketchPlugin_Arc::ID()) {
286 theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4";
287 theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
288 .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID());
289 // wrong type of attribute
294 if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
295 aType[0] != aType[1]) {
296 theError = "Feature with kinds %1 and %2 can not be equal.";
297 theError.arg(aType[0]).arg(aType[1]);
303 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
304 const std::list<std::string>& theArguments,
305 Events_InfoMessage& theError) const
307 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
308 theError = "The attribute with the %1 type is not processed";
309 theError.arg(theAttribute->attributeType());
313 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
314 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
316 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
317 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
318 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
320 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
321 ObjectPtr aSelObject = aSelAttr->object(anInd);
322 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
323 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
324 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
325 if (aSelObject == *aMirIter) {
326 theError = "The object %1 is a result of mirror";
334 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
335 const std::list<std::string>& theArguments,
336 Events_InfoMessage& theError) const
338 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
339 theError = "The attribute with the %1 type is not processed";
340 theError.arg(theAttribute->attributeType());
344 // there is a check whether the feature contains a point and a linear edge or two point values
345 std::string aParamA = theArguments.front();
346 SessionPtr aMgr = ModelAPI_Session::get();
347 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
349 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
350 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
352 theError = "The %1 attribute should be %2";
353 theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
357 AttributeRefAttrPtr aRefAttrB =
358 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
360 // first attribute is a point, it may coincide with any object
361 if (!aRefAttrA->isObject())
364 ObjectPtr anObject = aRefAttrA->object();
365 if (!anObject.get()) {
366 theError = "%1 attribute has an empty object";
367 theError.arg(aParamA);
370 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
371 if (!aFeature.get()) {
372 theError = "%1 attribute has an empty feature";
373 theError.arg(aParamA);
377 if (aFeature->getKind() == SketchPlugin_Point::ID())
381 // second attribute is a point, it may coincide with any object
382 if (!aRefAttrB->isObject())
385 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
387 theError = "%1 attribute has an empty object";
388 theError.arg(theAttribute->id());
391 if (aFeature->getKind() == SketchPlugin_Point::ID())
394 theError = "There is no an attribute filled by a point";
399 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
400 const std::list<std::string>& theArguments,
401 Events_InfoMessage& theError) const
403 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
404 theError = "The attribute with the %1 type is not processed";
405 theError.arg(theAttribute->attributeType());
409 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
410 AttributeRefListPtr aSelAttr =
411 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
413 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
414 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
415 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
416 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
417 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
418 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
420 std::list<ObjectPtr>::iterator anObjIter;
421 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
422 ObjectPtr aSelObject = aSelAttr->object(anInd);
423 anObjIter = anInitialObjects.begin();
424 for (; anObjIter != anInitialObjects.end(); anObjIter++)
425 if (aSelObject == *anObjIter)
427 if (anObjIter != anInitialObjects.end())
429 anObjIter = aCopiedObjects.begin();
430 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
431 if (aSelObject == *anObjIter) {
432 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
433 theError = "The object %1 is a result of copy";
441 bool SketchPlugin_SolverErrorValidator::isValid(
442 const std::shared_ptr<ModelAPI_Feature>& theFeature,
443 const std::list<std::string>& theArguments,
444 Events_InfoMessage& theError) const
446 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
448 if (!aAttributeString->value().empty()) {
449 theError = aAttributeString->value();
456 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
457 std::string theAttribute)
462 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
463 const FeaturePtr theFeature)
465 for(std::set<AttributePtr>::const_iterator
466 anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
467 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
468 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
469 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
470 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
471 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
472 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
473 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
474 if(anAttrRefA.get()) {
475 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
477 DocumentPtr aDoc = aResA->document();
479 FeaturePtr aFeatureA = aDoc->feature(aResA);
480 if(aFeatureA.get() && aFeatureA == theFeature) {
486 if(anAttrRefB.get()) {
487 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
489 DocumentPtr aDoc = aResB->document();
491 FeaturePtr aFeatureB = aDoc->feature(aResB);
492 if(aFeatureB.get() && aFeatureB == theFeature) {
503 static bool isPointPointCoincidence(const FeaturePtr& theCoincidence)
505 AttributeRefAttrPtr aRefAttr[2] = {
506 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_A()),
507 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_B())
510 bool arePoints = true;
511 for (int i = 0; i < 2 && arePoints; ++i) {
512 if (aRefAttr[i]->isObject()) {
513 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr[i]->object());
514 arePoints = aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID();
516 arePoints = aRefAttr[i]->attr().get() != NULL;
521 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
522 const std::list<std::string>& theArguments,
523 Events_InfoMessage& theError) const
525 AttributeRefAttrPtr aPointRefAttr =
526 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
527 if(!aPointRefAttr.get()) {
528 theError = "Error: Point not selected.";
532 AttributePtr aPointAttribute = aPointRefAttr->attr();
533 if (!aPointAttribute.get()) {
534 theError = "Error: Bad point selected.";
537 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
538 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
540 // Obtain constraint coincidence for the fillet point.
541 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
542 FeaturePtr aConstraintCoincidence;
543 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
544 anIt != aRefsList.cend(); ++anIt) {
545 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
546 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
547 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
548 if (!isPointPointCoincidence(aConstrFeature))
551 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
552 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
553 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
554 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
556 AttributePtr anAttrA = anAttrRefA->attr();
557 if(aPointAttribute == anAttrA) {
558 aConstraintCoincidence = aConstrFeature;
562 AttributePtr anAttrB = anAttrRefB->attr();
563 if(aPointAttribute == anAttrB) {
564 aConstraintCoincidence = aConstrFeature;
570 if(!aConstraintCoincidence.get()) {
571 theError = "Error: one of the selected point does not have coicidence.";
575 // Get coincides from constraint.
576 std::set<FeaturePtr> aCoinsides;
577 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
578 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
580 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
581 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
584 // Remove points from set of coincides.
585 std::set<FeaturePtr> aNewSetOfCoincides;
586 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
587 anIt != aCoinsides.end(); ++anIt) {
588 std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
589 std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
590 if(aSketchEntity.get() && aSketchEntity->isCopy()) {
593 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
594 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
597 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
598 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
599 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
600 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
601 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
602 if(aDistSelectedArcCenter < tolerance) {
606 aNewSetOfCoincides.insert(*anIt);
608 aCoinsides = aNewSetOfCoincides;
610 // If we still have more than two coincides remove auxilary entities from set of coincides.
611 if(aCoinsides.size() > 2) {
612 aNewSetOfCoincides.clear();
613 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
614 anIt != aCoinsides.end(); ++anIt) {
615 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
616 aNewSetOfCoincides.insert(*anIt);
619 aCoinsides = aNewSetOfCoincides;
622 if(aCoinsides.size() != 2) {
623 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
627 // Check that selected edges don't have tangent constraint.
628 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
629 FeaturePtr aFirstFeature = *anIt++;
630 FeaturePtr aSecondFeature = *anIt;
631 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
632 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
633 theError = "Error: Edges in selected point has tangent constraint.";
637 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
638 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
639 aResIt != aFirstResults.end(); ++aResIt) {
640 ResultPtr aRes = *aResIt;
641 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
642 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
643 theError = "Error: Edges in selected point has tangent constraint.";
648 // Check the features are not tangent
649 std::shared_ptr<GeomAPI_Shape> aFirstShape = aFirstFeature->lastResult()->shape();
650 std::shared_ptr<GeomAPI_Shape> aSecondShape = aSecondFeature->lastResult()->shape();
651 if (!aFirstShape || !aFirstShape->isEdge() ||
652 !aSecondShape || !aSecondShape->isEdge()) {
653 theError = "Error: At least on of the features is not an edge";
657 std::shared_ptr<GeomAPI_Edge> anEdge1 = std::dynamic_pointer_cast<GeomAPI_Edge>(aFirstShape);
658 std::shared_ptr<GeomAPI_Edge> anEdge2 = std::dynamic_pointer_cast<GeomAPI_Edge>(aSecondShape);
660 static const double TOL = 1.e-7;
661 if (anEdge1->isLine() && anEdge2->isLine()) {
662 // Check that lines not collinear
663 std::shared_ptr<GeomAPI_Dir> aDir1 = anEdge1->line()->direction();
664 std::shared_ptr<GeomAPI_Dir> aDir2 = anEdge2->line()->direction();
665 double aCross = aDir1->cross(aDir2)->squareModulus();
666 if (aCross < TOL * TOL)
668 } else if (anEdge1->isArc() && anEdge2->isArc()) {
669 // check the circles are not tangent
670 std::shared_ptr<GeomAPI_Circ> aCirc1 = anEdge1->circle();
671 std::shared_ptr<GeomAPI_Circ> aCirc2 = anEdge2->circle();
672 double aDistCC = aCirc1->center()->distance(aCirc2->center());
673 double aRadSum = aCirc1->radius() + aCirc2->radius();
674 double aRadDiff = fabs(aCirc1->radius() - aCirc2->radius());
675 if (fabs(aDistCC - aRadSum) < TOL || fabs(aDistCC - aRadDiff) < TOL)
678 // check whether line and arc are tangent
679 std::shared_ptr<GeomAPI_Circ> aCirc;
680 std::shared_ptr<GeomAPI_Lin> aLine;
681 if (anEdge1->isLine()) {
682 aLine = anEdge1->line();
683 aCirc = anEdge2->circle();
685 aCirc = anEdge1->circle();
686 aLine = anEdge2->line();
689 double aDistCL = aLine->distance(aCirc->center());
690 if (fabs(aDistCL - aCirc->radius()) < TOL)
697 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
698 const std::list<std::string>& theArguments,
699 Events_InfoMessage& theError) const
701 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
702 theError = "The attribute with the %1 type is not processed";
703 theError.arg(theAttribute->attributeType());
707 // there is a check whether the feature contains a point and a linear edge or two point values
708 std::string aParamA = theArguments.front();
709 SessionPtr aMgr = ModelAPI_Session::get();
710 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
712 FeaturePtr anAttributeFeature =
713 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
714 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
715 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
717 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
720 for (int i = 0; i < 2; ++i) {
721 if (!aRefAttrs[i]->isObject())
724 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
726 if (aNbPoints + aNbLines != 0)
731 if (aFeature->getKind() == SketchPlugin_Point::ID())
733 else if (aFeature->getKind() == SketchPlugin_Line::ID())
738 if (aNbPoints != 1 || aNbLines != 1) {
739 theError = "Middle point constraint allows points and lines only";
745 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
746 const std::list<std::string>& /*theArguments*/,
747 Events_InfoMessage& theError) const
749 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
750 theError = "The attribute with the %1 type is not processed";
751 theError.arg(theAttribute->attributeType());
754 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
755 AttributePtr anAttr = aRefAttr->attr();
757 theError = "The attribute %1 should be a point";
758 theError.arg(theAttribute->id());
762 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
763 const std::string& aFeatureType = anAttrFeature->getKind();
764 if (aFeatureType == SketchPlugin_Arc::ID()) {
765 // selected point should not be a center of arc
766 const std::string& aPntId = anAttr->id();
767 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
768 theError = "The attribute %1 is not supported";
769 theError.arg(aPntId);
773 else if (aFeatureType == SketchPlugin_Line::ID()) {
774 // selected point should be bound point of line
775 const std::string& aPntId = anAttr->id();
776 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
777 theError = "The attribute %1 is not supported";
778 theError.arg(aPntId);
783 theError = "Unable to build tangent arc on %1";
784 theError.arg(anAttrFeature->getKind());
791 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
792 const std::list<std::string>& theArguments,
793 Events_InfoMessage& theError) const
795 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
796 theError = "The attribute with the %1 type is not processed";
797 theError.arg(theAttribute->attributeType());
800 AttributeSelectionPtr aLineAttr =
801 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
802 std::shared_ptr<GeomAPI_Edge> anEdge;
803 if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
804 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
805 } else if(aLineAttr->context() &&
806 aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
807 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
810 if (!anEdge || !anEdge->isLine()) {
811 theError = "The attribute %1 should be a line";
812 theError.arg(theAttribute->id());
816 std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
819 std::shared_ptr<SketchPlugin_Sketch> aSketch;
820 std::set<AttributePtr> aRefs = aLineAttr->owner()->data()->refsToMe();
821 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
822 for (; anIt != aRefs.end(); ++anIt) {
823 CompositeFeaturePtr aComp =
824 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
825 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
826 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
831 theError = "There is no sketch referring to the current feature";
835 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
836 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
837 return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;
840 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
841 const std::list<std::string>& theArguments,
842 Events_InfoMessage& theError) const
846 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
847 theError = "The attribute with the %1 type is not processed";
848 theError.arg(theAttribute->attributeType());
851 AttributeReferencePtr aFeatureAttr =
852 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
854 ObjectPtr anAttrObject = aFeatureAttr->value();
855 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
859 std::string aKind = anAttrFeature->getKind();
860 if (aKind == SketchPlugin_Line::ID() ||
861 aKind == SketchPlugin_Arc::ID() ||
862 aKind == SketchPlugin_Circle::ID()) {
864 std::set<ResultPtr> anEdgeShapes;
865 ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
866 if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
869 // coincidences to the feature
870 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
871 ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
872 SketchPlugin_ConstraintCoincidence::ID(),
873 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
875 GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
876 std::shared_ptr<SketchPlugin_Feature> aSFeature =
877 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
878 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
880 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
881 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
882 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
883 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
884 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
885 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
886 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
887 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
889 typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
890 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
891 std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
892 PointToRefsMap aPointsInfo;
894 ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
895 aX->dir(), aDirY, aPointsInfo);
896 int aCoincidentToFeature = (int)aPointsInfo.size();
897 if (aKind == SketchPlugin_Circle::ID())
898 aValid = aCoincidentToFeature >= 2;
900 aValid = aCoincidentToFeature >= 1;
906 bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
907 const std::list<std::string>& theArguments,
908 Events_InfoMessage& theError) const
912 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
913 theError = "The attribute with the %1 type is not processed";
914 theError.arg(theAttribute->attributeType());
917 AttributeReferencePtr aBaseObjectAttr =
918 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
920 std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
921 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
923 ObjectPtr aBaseObject = aBaseObjectAttr->value();
925 AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
926 aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
927 aBaseObject = aBaseObjectAttr->value();
930 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
934 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
935 std::dynamic_pointer_cast<SketchPlugin_Feature>(aBaseFeature);
936 if (!aSketchFeature.get() || aSketchFeature->isCopy())
939 std::string aKind = aBaseFeature->getKind();
940 if (aKind != SketchPlugin_Line::ID() &&
941 aKind != SketchPlugin_Arc::ID() &&
942 aKind != SketchPlugin_Circle::ID())
946 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
947 aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
949 SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
951 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
952 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
953 anAttributePnt2d->y());
955 std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
956 std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
957 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
958 std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
959 SketchPlugin_Trim::fillObjectShapes(aBaseObject, aSketch->data()->owner(),
960 aCashedShapes, anObjectToPoints);
961 const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
963 return aShapes.size() > 1;
966 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
967 const std::list<std::string>& theArguments,
968 Events_InfoMessage& theError) const
970 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
971 theError = "The attribute with the %1 type is not processed";
972 theError.arg(theAttribute->attributeType());
976 AttributeSelectionPtr aFeatureAttr =
977 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
978 std::shared_ptr<GeomAPI_Edge> anEdge;
979 if (aFeatureAttr.get()) {
980 GeomShapePtr aVal = aFeatureAttr->value();
981 ResultPtr aRes = aFeatureAttr->context();
982 if(aFeatureAttr->value() && aFeatureAttr->value()->isEdge()) {
983 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
984 } else if(aFeatureAttr->context() && aFeatureAttr->context()->shape() &&
985 aFeatureAttr->context()->shape()->isEdge()) {
986 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
990 theError = "The attribute %1 should be an edge";
991 theError.arg(theAttribute->id());
996 std::shared_ptr<SketchPlugin_Sketch> aSketch;
997 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
998 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
999 for (; anIt != aRefs.end(); ++anIt) {
1000 CompositeFeaturePtr aComp =
1001 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
1002 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
1003 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
1008 theError = "There is no sketch referring to the current feature";
1012 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1013 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
1014 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
1016 if (anEdge->isLine()) {
1017 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1018 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
1019 std::shared_ptr<GeomAPI_Pnt> aLineLoc = aLine->location();
1020 double aDot = aNormal->dot(aLineDir);
1021 double aDist = aLineLoc->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
1022 bool aValid = (fabs(aDot) >= tolerance && fabs(aDot) < 1.0 - tolerance) ||
1023 (fabs(aDot) < tolerance && fabs(aDist) > tolerance);
1025 theError = "Error: Edge is already in the sketch plane.";
1028 else if (anEdge->isCircle() || anEdge->isArc()) {
1029 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1030 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
1031 std::shared_ptr<GeomAPI_Pnt> aCircCenter = aCircle->center();
1032 double aDot = fabs(aNormal->dot(aCircNormal));
1033 double aDist = aCircCenter->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
1034 bool aValid = fabs(aDot - 1.0) < tolerance * tolerance && fabs(aDist) > tolerance;
1036 theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
1037 : "Error: Arc is already in the sketch plane.");
1041 theError = "Error: Selected object is not line, circle or arc.";
1046 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
1047 const std::set<FeaturePtr>& theSet2)
1049 std::set<FeaturePtr> aCommon;
1050 if (theSet1.empty() || theSet2.empty())
1053 std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1054 for (; anIt2 != theSet2.end(); ++anIt2)
1055 if (theSet1.find(*anIt2) != theSet1.end())
1056 aCommon.insert(*anIt2);
1060 bool SketchPlugin_DifferentReferenceValidator::isValid(
1061 const AttributePtr& theAttribute,
1062 const std::list<std::string>& theArguments,
1063 Events_InfoMessage& theError) const
1065 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1067 int aNbFeaturesReferred = 0;
1068 int aNbAttributesReferred = 0;
1069 std::set<FeaturePtr> aCommonReferredFeatures;
1071 // find all features referred by attributes listed in theArguments
1072 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1073 for (; anArgIt != theArguments.end(); ++anArgIt) {
1074 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1078 std::set<FeaturePtr> aCoincidentFeatures;
1079 if (aRefAttr->isObject()) {
1080 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1082 aCoincidentFeatures.insert(aFeature);
1083 aNbFeaturesReferred += 1;
1086 AttributePoint2DPtr aPoint =
1087 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1089 aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1090 aNbAttributesReferred += 1;
1094 if (aCommonReferredFeatures.empty())
1095 aCommonReferredFeatures = aCoincidentFeatures;
1097 aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1099 if (aCommonReferredFeatures.empty())
1103 bool isOk = aNbFeaturesReferred < 1;
1104 if (aNbFeaturesReferred == 1) {
1105 if (aCommonReferredFeatures.size() == 1) {
1106 FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1107 isOk = aNbAttributesReferred <= 1 ||
1108 aFeature->getKind() == SketchPlugin_Circle::ID() ||
1109 aFeature->getKind() == SketchPlugin_Arc::ID();
1115 theError = "Attributes are referred to the same feature";
1119 bool SketchPlugin_DifferentPointReferenceValidator::isValid(
1120 const AttributePtr& theAttribute,
1121 const std::list<std::string>& theArguments,
1122 Events_InfoMessage& theError) const
1124 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1125 std::set<AttributePoint2DPtr> aReferredCoincidentPoints;
1127 // find all points referred by attributes listed in theArguments
1128 bool hasRefsToPoints = false;
1129 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1130 for (; anArgIt != theArguments.end(); ++anArgIt) {
1131 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1135 if (!aRefAttr->isObject()) {
1136 AttributePoint2DPtr aPoint =
1137 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1138 if (aReferredCoincidentPoints.empty())
1139 aReferredCoincidentPoints = SketchPlugin_Tools::findPointsCoincidentToPoint(aPoint);
1140 else if (aReferredCoincidentPoints.find(aPoint) == aReferredCoincidentPoints.end())
1141 return true; // non-coincident point has been found
1143 hasRefsToPoints = true;
1147 if (hasRefsToPoints)
1148 theError = "Attributes are referred to the same point";
1149 return !hasRefsToPoints;
1152 bool SketchPlugin_CirclePassedPointValidator::isValid(
1153 const AttributePtr& theAttribute,
1154 const std::list<std::string>&,
1155 Events_InfoMessage& theError) const
1157 static const std::string aErrorMessage(
1158 "Passed point refers to the same feature as a center point");
1160 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1162 AttributeRefAttrPtr aCenterRef =
1163 anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1164 AttributeRefAttrPtr aPassedRef =
1165 anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1167 if (!aPassedRef->isObject())
1170 FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1171 if (!aPassedFeature)
1174 if (aCenterRef->isObject()) {
1175 if (aCenterRef->object() == aPassedRef->object()) {
1176 theError = aErrorMessage;
1180 AttributePoint2DPtr aCenterPoint =
1181 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1183 std::set<FeaturePtr> aCoincidentFeatures =
1184 SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1185 // check one of coincident features is a feature referred by passed point
1186 std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1187 for(; anIt != aCoincidentFeatures.end(); ++anIt)
1188 if (*anIt == aPassedFeature) {
1189 theError = aErrorMessage;
1197 bool SketchPlugin_ThirdPointValidator::isValid(
1198 const AttributePtr& theAttribute,
1199 const std::list<std::string>& theArguments,
1200 Events_InfoMessage& theError) const
1202 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1203 return arePointsNotOnLine(anOwner, theError) &&
1204 arePointsNotSeparated(anOwner, theArguments, theError);
1207 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1208 const std::string& thePointAttrName,
1209 const std::string& theRefPointAttrName)
1211 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1212 theMacroCircle->attribute(thePointAttrName));
1213 AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1215 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1217 if (aRefAttr->isObject()) {
1218 // project a point onto selected feature
1219 std::shared_ptr<SketchPlugin_Feature> aFeature =
1220 std::dynamic_pointer_cast<SketchPlugin_Feature>(
1221 ModelAPI_Feature::feature(aRefAttr->object()));
1223 SketchPlugin_Sketch* aSketch = aFeature->sketch();
1224 std::shared_ptr<GeomAPI_Edge> anEdge =
1225 std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1227 std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1228 if (anEdge->isLine())
1229 aPoint3D = anEdge->line()->project(aPoint3D);
1230 else if (anEdge->isCircle())
1231 aPoint3D = anEdge->circle()->project(aPoint3D);
1233 aPoint = aSketch->to2D(aPoint3D);
1237 AttributePoint2DPtr anOtherPoint =
1238 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1240 aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1247 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1248 std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1250 if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1251 thePoints[0] = toPoint(theMacroFeature,
1252 SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1253 SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1254 thePoints[1] = toPoint(theMacroFeature,
1255 SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1256 SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1257 thePoints[2] = toPoint(theMacroFeature,
1258 SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1259 SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1260 } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1261 thePoints[0] = toPoint(theMacroFeature,
1262 SketchPlugin_MacroArc::START_POINT_2_ID(),
1263 SketchPlugin_MacroArc::START_POINT_REF_ID());
1264 thePoints[1] = toPoint(theMacroFeature,
1265 SketchPlugin_MacroArc::END_POINT_2_ID(),
1266 SketchPlugin_MacroArc::END_POINT_REF_ID());
1267 thePoints[2] = toPoint(theMacroFeature,
1268 SketchPlugin_MacroArc::PASSED_POINT_ID(),
1269 SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1273 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1274 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1275 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1277 static const double aTolerance = 1.e-7;
1278 if (thePoint1->distance(thePoint2) < aTolerance ||
1279 thePoint1->distance(thePoint3) < aTolerance)
1282 std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1283 thePoint2->y() - thePoint1->y()));
1284 std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1285 thePoint3->y() - thePoint1->y()));
1286 return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1289 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1290 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1291 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1293 static const double aTolerance = 1.e-7;
1294 std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1295 std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1297 std::shared_ptr<GeomAPI_XYZ> aVec1 = thePoint1->xyz()->decreased(aLineLoc);
1298 // the first point is on the line
1299 if (aVec1->squareModulus() < aTolerance * aTolerance)
1301 std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(aVec1));
1302 std::shared_ptr<GeomAPI_XYZ> aVec2 = thePoint2->xyz()->decreased(aLineLoc);
1303 // the second point is on the line
1304 if (aVec2->squareModulus() < aTolerance * aTolerance)
1306 std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(aVec2));
1308 return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1311 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1312 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1313 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1315 static const double aTolerance = 1.e-7;
1316 std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1317 double aDistP1C = thePoint1->distance(aCenter);
1318 double aDistP2C = thePoint2->distance(aCenter);
1319 return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1322 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1323 const FeaturePtr& theMacroFeature,
1324 Events_InfoMessage& theError) const
1326 static const std::string aErrorPointsOnLine(
1327 "Selected points are on the same line");
1329 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1330 threePointsOfFeature(theMacroFeature, aPoints);
1332 if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1333 theError = aErrorPointsOnLine;
1339 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1340 const FeaturePtr& theMacroFeature,
1341 const std::list<std::string>& theArguments,
1342 Events_InfoMessage& theError) const
1344 static const std::string aErrorPointsApart(
1345 "Selected entity is lying between first two points");
1347 AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1348 FeaturePtr aRefByThird;
1349 if (aThirdPointRef->isObject())
1350 aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1354 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1355 threePointsOfFeature(theMacroFeature, aPoints);
1357 std::shared_ptr<GeomAPI_Edge> aThirdShape =
1358 std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1362 SketchPlugin_Sketch* aSketch =
1363 std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1364 std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1365 std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1368 if (aThirdShape->isLine())
1369 isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1370 else if (aThirdShape->isCircle() || aThirdShape->isArc())
1371 isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1374 theError = aErrorPointsApart;
1378 bool SketchPlugin_ArcEndPointValidator::isValid(
1379 const AttributePtr& theAttribute,
1380 const std::list<std::string>& theArguments,
1381 Events_InfoMessage& theError) const
1383 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1384 AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1386 if(!anEndPointRef.get()) {
1390 ObjectPtr anObject = anEndPointRef->object();
1391 AttributePtr anAttr = anEndPointRef->attr();
1392 if(!anObject.get() && !anAttr.get()) {
1396 if(anEndPointRef->attr().get()) {
1400 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1402 GeomShapePtr aShape = aResult->shape();
1403 if(aShape.get() && aShape->isVertex()) {
1408 aFeature = ModelAPI_Feature::feature(anObject);
1409 if(aFeature.get()) {
1410 if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1418 static GeomShapePtr toInfiniteEdge(const GeomShapePtr theShape)
1420 if(!theShape.get()) {
1424 if(!theShape->isEdge()) {
1428 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
1434 if(anEdge->isLine()) {
1435 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1436 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::line(aLine);
1440 if(anEdge->isCircle() || anEdge->isArc()) {
1441 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1442 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCircle);
1449 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1450 const AttributePtr& theAttribute,
1451 const std::list<std::string>& theArguments,
1452 Events_InfoMessage& theError) const
1454 std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1455 std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1456 AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1458 if(!anEndPointRef.get()) {
1462 GeomShapePtr anArcShape = toInfiniteEdge(anArcFeature->getArcShape(false));
1464 if(!anArcShape.get() || anArcShape->isNull()) {
1468 ObjectPtr anObject = anEndPointRef->object();
1469 AttributePtr anAttr = anEndPointRef->attr();
1470 if(!anObject.get() && !anAttr.get()) {
1474 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1476 GeomShapePtr aShape = aResult->shape();
1477 if (!aShape->isEdge())
1479 aShape = toInfiniteEdge(aShape);
1480 if(aShape.get() && !aShape->isNull()) {
1481 if(anArcShape->isIntersect(aShape)) {
1487 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1488 if(aSelectedFeature.get()) {
1489 std::list<ResultPtr> aResults = aSelectedFeature->results();
1490 for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1491 anIt != aResults.cend();
1494 GeomShapePtr aShape = (*anIt)->shape();
1495 if (!aShape->isEdge())
1497 aShape = toInfiniteEdge(aShape);
1498 if(aShape.get() && !aShape->isNull()) {
1499 if(anArcShape->isIntersect(aShape)) {