+static void getPointAndLine(const ConstraintPtr& theConstraint, const StoragePtr& theStorage,
+ EntityWrapperPtr& thePoint, EntityWrapperPtr& theLine)
+{
+ for (int i = 0; i < 2; ++i) {
+ AttributePtr anAttr = theConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(i));
+ EntityWrapperPtr anEntity = theStorage->entity(anAttr);
+ if (anEntity->type() == ENTITY_POINT)
+ thePoint = anEntity;
+ else if (anEntity->type() == ENTITY_LINE)
+ theLine = anEntity;
+ }
+}
+
+static void adjustOddPoint(const EntityWrapperPtr& theDistPoint,
+ const EntityWrapperPtr& theDistLine,
+ GCSPointPtr& theOddPoint)
+{
+ if (!theOddPoint)
+ return;
+
+ std::shared_ptr<GeomAPI_Lin2d> aLine = PlaneGCSSolver_Tools::line(theDistLine);
+ std::shared_ptr<GeomAPI_Pnt2d> aPoint = PlaneGCSSolver_Tools::point(theDistPoint);
+
+ std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
+ std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
+
+ double aDot = aPtLineVec->dot(aLineVec);
+ std::shared_ptr<GeomAPI_XY> aProjectedPnt =
+ aLine->location()->xy()->added(aLineVec->multiplied(aDot));
+
+ *(theOddPoint->x) = aProjectedPnt->x();
+ *(theOddPoint->y) = aProjectedPnt->y();
+}
+
+static FeaturePtr getFeature(AttributeRefAttrPtr theRefAttr)
+{
+ ObjectPtr anObj;
+ if (theRefAttr->isObject())
+ anObj = theRefAttr->object();
+ else
+ anObj = theRefAttr->attr()->owner();
+ return ModelAPI_Feature::feature(anObj);
+}
+
+static void calculateDistanceDirection(const ConstraintPtr& theConstraint,
+ const StoragePtr& theStorage,
+ double& theDirX, double& theDirY)
+{
+ std::shared_ptr<GeomDataAPI_Dir> aDistDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+ theConstraint->attribute(SketchPlugin_ConstraintDistance::DIRECTION_ID()));
+ if (aDistDir && aDistDir->isInitialized()) {
+ theDirX = aDistDir->x();
+ theDirY = aDistDir->y();
+ if (fabs(theDirX) > tolerance || fabs(theDirY) > tolerance)
+ return;
+ }
+
+ AttributeRefAttrPtr aRefAttrA = theConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
+ AttributeRefAttrPtr aRefAttrB = theConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
+
+ EntityWrapperPtr aEntityA = theStorage->entity(aRefAttrA);
+ EntityWrapperPtr aEntityB = theStorage->entity(aRefAttrB);
+
+ GCSPointPtr aPoint;
+ if (aEntityA->type() != ENTITY_LINE && aEntityB->type() != ENTITY_LINE) {
+ aPoint = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aEntityA)->point();
+ theDirX = 1.0;
+ theDirY = 0.0;
+
+ EdgeWrapperPtr anEdgeA = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
+ theStorage->entity(getFeature(aRefAttrA)));
+ EdgeWrapperPtr anEdgeB = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
+ theStorage->entity(getFeature(aRefAttrB)));
+
+ if (anEdgeA && anEdgeB) {
+ GCS::DeriVector2 aDirA = anEdgeA->entity()->CalculateNormal(*aPoint);
+ GCS::DeriVector2 aDirB = anEdgeB->entity()->CalculateNormal(*aPoint);
+ double x = -aDirA.x + aDirB.x;
+ double y = -aDirA.y + aDirB.y;
+ double norm = sqrt(x*x + y*y);
+ if (norm > tolerance) {
+ theDirX = x / norm;
+ theDirY = y / norm;
+ }
+ }
+ }
+}
+
+static void moveEntity(const ConstraintPtr& theConstraint,
+ const StoragePtr& theStorage,
+ const double theDX, const double theDY)
+{
+ static const double THE_SHIFT = 1.e-4;
+
+ AttributeRefAttrPtr aRefAttrA = theConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
+ AttributeRefAttrPtr aRefAttrB = theConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
+
+ EntityWrapperPtr aEntityA = theStorage->entity(aRefAttrA);
+ EntityWrapperPtr aEntityB = theStorage->entity(aRefAttrB);
+
+ PointWrapperPtr aPointA = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aEntityA);
+ PointWrapperPtr aPointB = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aEntityB);
+
+ if (aPointA) {
+ *aPointA->point()->x -= THE_SHIFT * theDX;
+ *aPointA->point()->y -= THE_SHIFT * theDY;
+ }
+ else if (aPointB) {
+ *aPointB->point()->x += THE_SHIFT * theDX;
+ *aPointB->point()->y += THE_SHIFT * theDY;
+ }
+}
+
+
+