return aPnt;
}
+std::set<FeaturePtr> findCoincidentConstraints(const FeaturePtr& theFeature)
+{
+ std::set<FeaturePtr> aCoincident;
+ const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
+ std::set<AttributePtr>::const_iterator aIt;
+ for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
+ FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aIt)->owner());
+ if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID())
+ aCoincident.insert(aConstrFeature);
+ }
+ return aCoincident;
+}
+
void findCoincidences(const FeaturePtr theStartCoin,
const std::string& theAttr,
std::set<FeaturePtr>& theList)
return;
}
theList.insert(aObj);
- const std::set<AttributePtr>& aRefsList = aObj->data()->refsToMe();
- std::set<AttributePtr>::const_iterator aIt;
- for(aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
- std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
- FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
- if(aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
- std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincidencePoint(aConstrFeature);
- if(aPnt.get() && aOrig->isEqual(aPnt)) {
- findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A(), theList);
- findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B(), theList);
- }
+ std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(aObj);
+ std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
+ for (; aCIt != aCoincidences.end(); ++aCIt) {
+ FeaturePtr aConstrFeature = *aCIt;
+ std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincidencePoint(aConstrFeature);
+ if(aPnt.get() && aOrig->isEqual(aPnt)) {
+ findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A(), theList);
+ findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B(), theList);
}
}
}
}
+std::set<FeaturePtr> findFeaturesCoincidentToPoint(const AttributePoint2DPtr& thePoint)
+{
+ std::set<FeaturePtr> aCoincidentFeatures;
+
+ FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
+ aCoincidentFeatures.insert(anOwner);
+
+ std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(anOwner);
+ std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
+ for (; aCIt != aCoincidences.end(); ++aCIt) {
+ bool isPointUsedInCoincidence = false;
+ AttributeRefAttrPtr anOtherCoincidentAttr;
+ for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
+ AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
+ if (!aRefAttr)
+ continue;
+ if (!aRefAttr->isObject() && aRefAttr->attr() == thePoint)
+ isPointUsedInCoincidence = true;
+ else
+ anOtherCoincidentAttr = aRefAttr;
+ }
+
+ if (isPointUsedInCoincidence) {
+ ObjectPtr anObj;
+ if (anOtherCoincidentAttr->isObject())
+ anObj = anOtherCoincidentAttr->object();
+ else
+ anObj = anOtherCoincidentAttr->attr()->owner();
+ aCoincidentFeatures.insert(ModelAPI_Feature::feature(anObj));
+ }
+ }
+
+ return aCoincidentFeatures;
+}
+
void resetAttribute(SketchPlugin_Feature* theFeature,
const std::string& theId)
{
#include "SketchPlugin_Circle.h"
#include "SketchPlugin_ConstraintCoincidence.h"
#include "SketchPlugin_ConstraintDistance.h"
-#include "SketchPlugin_Fillet.h"
#include "SketchPlugin_ConstraintRigid.h"
#include "SketchPlugin_ConstraintTangent.h"
+#include "SketchPlugin_Fillet.h"
#include "SketchPlugin_Line.h"
+#include "SketchPlugin_MacroCircle.h"
#include "SketchPlugin_Point.h"
#include "SketchPlugin_Sketch.h"
#include "SketchPlugin_Trim.h"
#include <ModelGeomAlgo_Shape.h>
#include <GeomAPI_Circ.h>
+#include <GeomAPI_Dir2d.h>
#include <GeomAPI_Lin.h>
#include <GeomAPI_Edge.h>
#include <GeomAPI_Vertex.h>
theError = "Error: Selected object is not line, circle or arc.";
return false;
}
+
+
+static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
+ const std::set<FeaturePtr>& theSet2)
+{
+ std::set<FeaturePtr> aCommon;
+ if (theSet1.empty() || theSet2.empty())
+ return aCommon;
+
+ std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
+ for (; anIt2 != theSet2.end(); ++anIt2)
+ if (theSet1.find(*anIt2) != theSet1.end())
+ aCommon.insert(*anIt2);
+ return aCommon;
+}
+
+bool SketchPlugin_DifferentReferenceValidator::isValid(
+ const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments,
+ Events_InfoMessage& theError) const
+{
+ FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
+
+ int aNbFeaturesReferred = 0;
+ int aNbAttributesReferred = 0;
+ std::set<FeaturePtr> aCommonReferredFeatures;
+
+ // find all features referred by attributes listed in theArguments
+ std::list<std::string>::const_iterator anArgIt = theArguments.begin();
+ for (; anArgIt != theArguments.end(); ++anArgIt) {
+ AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
+ if (!aRefAttr)
+ continue;
+
+ std::set<FeaturePtr> aCoincidentFeatures;
+ if (aRefAttr->isObject()) {
+ FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
+ if (aFeature) {
+ aCoincidentFeatures.insert(aFeature);
+ aNbFeaturesReferred += 1;
+ }
+ } else {
+ AttributePoint2DPtr aPoint =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
+ if (aPoint) {
+ aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
+ aNbAttributesReferred += 1;
+ }
+ }
+
+ if (aCommonReferredFeatures.empty())
+ aCommonReferredFeatures = aCoincidentFeatures;
+ else
+ aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
+
+ if (aCommonReferredFeatures.empty())
+ return true;
+ }
+
+ bool isOk = aNbFeaturesReferred < 1;
+ if (aNbFeaturesReferred == 1) {
+ if (aCommonReferredFeatures.size() == 1) {
+ FeaturePtr aFeature = *aCommonReferredFeatures.begin();
+ isOk = aNbAttributesReferred <= 1 ||
+ aFeature->getKind() == SketchPlugin_Circle::ID() ||
+ aFeature->getKind() == SketchPlugin_Arc::ID();
+ } else
+ isOk = false;
+ }
+
+ if (!isOk)
+ theError = "Attributes are referred to the same feature";
+ return isOk;
+}
+
+bool SketchPlugin_CirclePassedPointValidator::isValid(
+ const AttributePtr& theAttribute,
+ const std::list<std::string>&,
+ Events_InfoMessage& theError) const
+{
+ static const std::string aErrorMessage(
+ "Passed point refers to the same feature as a center point");
+
+ FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
+
+ AttributeRefAttrPtr aCenterRef =
+ anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
+ AttributeRefAttrPtr aPassedRef =
+ anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
+
+ if (!aPassedRef->isObject())
+ return true;
+
+ FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
+ if (!aPassedFeature)
+ return true;
+
+ if (aCenterRef->isObject()) {
+ FeaturePtr aCenterFeature = ModelAPI_Feature::feature(aCenterRef->object());
+ if (aCenterFeature == aPassedFeature) {
+ theError = aErrorMessage;
+ return false;
+ }
+ } else {
+ AttributePoint2DPtr aCenterPoint =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
+ if (aCenterPoint) {
+ std::set<FeaturePtr> aCoincidentFeatures =
+ SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
+ // check one of coincident features is a feature referred by passed point
+ std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
+ for(; anIt != aCoincidentFeatures.end(); ++anIt)
+ if (*anIt == aPassedFeature) {
+ theError = aErrorMessage;
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+bool SketchPlugin_ThirdPointValidator::isValid(
+ const AttributePtr& theAttribute,
+ const std::list<std::string>&,
+ Events_InfoMessage& theError) const
+{
+ FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
+ return arePointsNotOnLine(anOwner, theError) && arePointsNotSeparated(anOwner, theError);
+}
+
+static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
+ const std::string& thePointAttrName,
+ const std::string& theRefPointAttrName)
+{
+ AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ theMacroCircle->attribute(thePointAttrName));
+ AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
+
+ std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
+ if (aRefAttr) {
+ if (aRefAttr->isObject()) {
+ // project a point onto selected feature
+ std::shared_ptr<SketchPlugin_Feature> aFeature =
+ std::dynamic_pointer_cast<SketchPlugin_Feature>(
+ ModelAPI_Feature::feature(aRefAttr->object()));
+ if (aFeature) {
+ SketchPlugin_Sketch* aSketch = aFeature->sketch();
+ std::shared_ptr<GeomAPI_Edge> anEdge =
+ std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
+ if (anEdge) {
+ std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
+ if (anEdge->isLine())
+ aPoint3D = anEdge->line()->project(aPoint3D);
+ else if (anEdge->isCircle())
+ aPoint3D = anEdge->circle()->project(aPoint3D);
+ aPoint = aSketch->to2D(aPoint3D);
+ }
+ }
+ } else {
+ AttributePoint2DPtr anOtherPoint =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
+ if (anOtherPoint)
+ aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
+ }
+ }
+
+ return aPoint;
+}
+
+static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
+ const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
+ const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
+{
+ static const double aTolerance = 1.e-7;
+ if (thePoint1->distance(thePoint2) < aTolerance ||
+ thePoint1->distance(thePoint3) < aTolerance)
+ return true;
+
+ std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
+ thePoint2->y() - thePoint1->y()));
+ std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
+ thePoint3->y() - thePoint1->y()));
+ return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
+}
+
+static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
+ const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
+ const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
+{
+ static const double aTolerance = 1.e-7;
+ std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
+ std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
+ std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(thePoint1->xyz()->decreased(aLineLoc)));
+ std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(thePoint2->xyz()->decreased(aLineLoc)));
+ return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
+}
+
+static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
+ const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
+ const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
+{
+ static const double aTolerance = 1.e-7;
+ std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
+ double aDistP1C = thePoint1->distance(aCenter);
+ double aDistP2C = thePoint2->distance(aCenter);
+ return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
+}
+
+bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
+ const FeaturePtr& theMacroCircle,
+ Events_InfoMessage& theError) const
+{
+ static const std::string aErrorPointsOnLine(
+ "Selected points are on the same line");
+
+ std::shared_ptr<GeomAPI_Pnt2d> aFirstPoint = toPoint(theMacroCircle,
+ SketchPlugin_MacroCircle::FIRST_POINT_ID(),
+ SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
+ std::shared_ptr<GeomAPI_Pnt2d> aSecondPoint = toPoint(theMacroCircle,
+ SketchPlugin_MacroCircle::SECOND_POINT_ID(),
+ SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
+ std::shared_ptr<GeomAPI_Pnt2d> aThirdPoint = toPoint(theMacroCircle,
+ SketchPlugin_MacroCircle::THIRD_POINT_ID(),
+ SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
+
+ if (isPointsOnLine(aFirstPoint, aSecondPoint, aThirdPoint)) {
+ theError = aErrorPointsOnLine;
+ return false;
+ }
+ return true;
+}
+
+bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
+ const FeaturePtr& theMacroCircle,
+ Events_InfoMessage& theError) const
+{
+ static const std::string aErrorPointsApart(
+ "Selected entity is lying between first two points");
+
+ AttributeRefAttrPtr aThirdPointRef =
+ theMacroCircle->refattr(SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
+ FeaturePtr aRefByThird;
+ if (aThirdPointRef->isObject())
+ aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
+ if (!aRefByThird)
+ return true;
+
+ std::shared_ptr<GeomAPI_Pnt2d> aFirstPoint = toPoint(theMacroCircle,
+ SketchPlugin_MacroCircle::FIRST_POINT_ID(),
+ SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
+ std::shared_ptr<GeomAPI_Pnt2d> aSecondPoint = toPoint(theMacroCircle,
+ SketchPlugin_MacroCircle::SECOND_POINT_ID(),
+ SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
+
+ std::shared_ptr<GeomAPI_Edge> aThirdShape =
+ std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
+ if (!aThirdShape)
+ return true;
+
+ SketchPlugin_Sketch* aSketch =
+ std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroCircle)->sketch();
+ std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aFirstPoint->x(), aFirstPoint->y());
+ std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aSecondPoint->x(), aSecondPoint->y());
+
+ bool isOk = true;
+ if (aThirdShape->isLine())
+ isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
+ else if (aThirdShape->isCircle() || aThirdShape->isArc())
+ isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
+
+ if (!isOk)
+ theError = aErrorPointsApart;
+ return isOk;
+}
Events_InfoMessage& theError) const;
};
+/**\class SketchPlugin_DifferentReferenceValidator
+ * \ingroup Validators
+ * \brief Validator for attributes of a sketch feature.
+ *
+ * It checks that at least one of specified attributes
+ * refers to another feature in respect to each other.
+ */
+class SketchPlugin_DifferentReferenceValidator : public ModelAPI_AttributeValidator
+{
+ public:
+ //! returns true if attribute is valid
+ //! \param theAttribute the checked attribute
+ //! \param theArguments arguments of the attribute
+ //! \param theError error message
+ virtual bool isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments,
+ Events_InfoMessage& theError) const;
+};
+
+/**\class SketchPlugin_CirclePassedPointValidator
+ * \ingroup Validators
+ * \brief Validator for passed point of MacroCircle feature.
+ *
+ * Checks that passed point does not refer to the feature, the center is coincident to.
+ */
+class SketchPlugin_CirclePassedPointValidator : public ModelAPI_AttributeValidator
+{
+ public:
+ //! returns true if attribute is valid
+ //! \param theAttribute the checked attribute
+ //! \param theArguments arguments of the attribute
+ //! \param theError error message
+ virtual bool isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>&,
+ Events_InfoMessage& theError) const;
+};
+
+/**\class SketchPlugin_ThirdPointValidator
+ * \ingroup Validators
+ * \brief Validator for the third point of MacroCircle feature.
+ *
+ * Checks that third point does not lie on a line passed through the first two points.
+ * Checks that third point does not refer to feature lying between the first two points.
+ */
+class SketchPlugin_ThirdPointValidator : public ModelAPI_AttributeValidator
+{
+ public:
+ //! returns true if attribute is valid
+ //! \param theAttribute the checked attribute
+ //! \param theArguments arguments of the attribute
+ //! \param theError error message
+ virtual bool isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>&,
+ Events_InfoMessage& theError) const;
+
+private:
+ //! returns true if three points have not been placed on the same line
+ bool arePointsNotOnLine(const FeaturePtr& theMacroCircle,
+ Events_InfoMessage& theError) const;
+
+ //! returns true if the first two points have not been separated
+ //! by a feature referred by thrid point
+ bool arePointsNotSeparated(const FeaturePtr& theMacroCircle,
+ Events_InfoMessage& theError) const;
+};
+
#endif