+
+//*****************************************************************
+//! Returns curves connected to the given point
+std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_SymbolPrs* thePrs)
+{
+ std::list<ObjectPtr> aList;
+ GeomAx3Ptr aAx3 = thePrs->plane();
+ ModelAPI_CompositeFeature* aOwner = thePrs->sketcher();
+ GeomPnt2dPtr aPnt2d = thePnt->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
+
+ int aNbSubs = aOwner->numberOfSubs();
+ for (int i = 0; i < aNbSubs; i++) {
+ FeaturePtr aFeature = aOwner->subFeature(i);
+ if (!aFeature->firstResult().get() || aFeature->firstResult()->isDisabled())
+ continue; // There is no result
+
+ if (aFeature->getKind() == SketchPlugin_Line::ID()) {
+ AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
+ AttributePoint2DPtr aSPnt2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
+
+ GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
+ GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
+
+ if (aPnt1->distance(aPnt2d) <= Precision::Confusion() ||
+ aPnt2->distance(aPnt2d) <= Precision::Confusion()) {
+ // a point corresponds to one of the line end
+ GeomShapePtr aShp = SketcherPrs_Tools::getShape(aFeature->firstResult());
+ GeomCurvePtr aCurv = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
+ aList.push_back(aFeature->firstResult());
+ } else {
+ // Check that a point belongs to the curve
+ GeomAPI_Lin2d aLin2d(aPnt1, aPnt2);
+ double aDist = aLin2d.distance(aPnt2d);
+ if (aDist <= Precision::Confusion())
+ aList.push_back(aFeature->firstResult());
+ }
+ } else {
+ GeomCurvePtr aCurve;
+ ObjectPtr aResObj;
+ std::list<ResultPtr> aResults = aFeature->results();
+ std::list<ResultPtr>::const_iterator aIt;
+ for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
+ GeomShapePtr aShp = SketcherPrs_Tools::getShape((*aIt));
+ if (aShp->isEdge()) {
+ aResObj = (*aIt);
+ aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
+ break;
+ }
+ }
+ if (aCurve.get()) {
+ GeomPointPtr aProjPnt;
+ if (aFeature->getKind() == SketchPlugin_Circle::ID() ||
+ aFeature->getKind() == SketchPlugin_Arc::ID()) {
+ GeomCirclePtr aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
+ aProjPnt = aCircle->project(thePnt);
+ }
+ else if (aFeature->getKind() == SketchPlugin_Ellipse::ID()) {
+ GeomEllipsePtr anEllipse = GeomEllipsePtr(new GeomAPI_Ellipse(aCurve));
+ aProjPnt = anEllipse->project(thePnt);
+ }
+ else
+ aProjPnt = aCurve->project(thePnt);
+ if (aProjPnt && thePnt->distance(aProjPnt) <= Precision::Confusion())
+ aList.push_back(aResObj);
+ }
+ }
+ }
+ return aList;
+}
+
+//*****************************************************************
+gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
+ ObjectPtr theLine, const SketcherPrs_SymbolPrs* thePrs,
+ double theStep, GeomPointPtr thePnt)
+{
+ gp_Pnt aP = thePnt->impl<gp_Pnt>();
+ if (!thePrs->plane().get())
+ return aP;
+ GeomDirPtr aNormal = thePrs->plane()->normal();
+ gp_Dir aNormDir = aNormal->impl<gp_Dir>();
+
+ std::list<ObjectPtr> aCurves = getCurves(thePnt, thePrs);
+ std::list<ObjectPtr>::const_iterator aItCurv;
+ std::list<gp_Vec> aVectorsList;
+ // Calculate all vectors
+ for (aItCurv = aCurves.cbegin(); aItCurv != aCurves.cend(); aItCurv++) {
+ aVectorsList.push_back(getVector((*aItCurv), thePrs->plane()->dirX(), aP));
+ }
+
+ // Position of the symbol
+ const std::array<int, 2>& aPos = getPositionIndex(thePnt, thePrs);
+
+ // Angle size of a symbol
+ //double aAngleStep = PI * 50./180.;
+ double aAngleStep = PI/4.;
+
+ std::list<gp_Vec>::const_iterator aItVec;
+ std::list<double> aAngles;
+ std::list<gp_Vec> aVectors;
+ // Select closest vectors and calculate angles between base vector and closest vector
+ for (aItVec = aVectorsList.cbegin(); aItVec != aVectorsList.cend(); aItVec++) {
+ std::list<gp_Vec>::const_iterator aIt;
+ double aMinAng = 0;
+ gp_Vec aVec = *aItVec;
+ for (aIt = aVectorsList.cbegin(); aIt != aVectorsList.cend(); aIt++) {
+ double aAng = aVec.AngleWithRef(*aIt, aNormDir);
+ if (aAng != 0) {
+ if (aAng < 0)
+ aAng = 2 * PI + aAng;
+
+ if (aMinAng == 0)
+ aMinAng = aAng;
+ else if (aAng < aMinAng) {
+ aMinAng = aAng;
+ }
+ }
+ }
+ if (aMinAng >= aAngleStep) {
+ aVectors.push_back(aVec);
+ aAngles.push_back(aMinAng);
+ }
+ }
+
+ gp_Ax1 aRotAx(aP, aNormDir);
+ gp_Vec aVecPos;
+ // If number of angle less then number of symbols then each symbol can be placed
+ // directly inside of the angle
+ if ((int)aAngles.size() >= aPos[1] && !aVectors.empty()) {
+ int aId = aPos[0];
+ aVecPos = *(std::next(aVectors.begin(), aId));
+
+ gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep);
+ aShift.Normalize();
+ aShift.Multiply(theStep * 1.5);
+ return aP.Translated(aShift);
+ }
+
+ // A case when there are a lot of symbols
+ int aPosCount = 0;
+ double aAng;
+ std::list<double>::const_iterator aItAng;
+
+ bool aHasPlace = false;
+ while (aPosCount < aPos[1]) {
+ for (aItAng = aAngles.cbegin(), aItVec = aVectors.cbegin();
+ aItAng != aAngles.cend(); ++aItAng, ++aItVec) {
+ aAng = (*aItAng);
+ int Nb = int(aAng / aAngleStep);
+ aPosCount += Nb;
+
+ if ((!aHasPlace) && (aPosCount >= (aPos[0] + 1))) {
+ aHasPlace = true;
+ aVecPos = (*aItVec);
+ }
+ }
+ if (aPosCount < aPos[1]) {
+ aAngleStep -= 0.1;
+ aHasPlace = false;
+ aPosCount = 0;
+ }
+ if (aAngleStep <= 0)
+ break;
+ }
+
+ if (aHasPlace) {
+ // rotate base vector on a necessary angle
+ gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep + aAngleStep * aPos[0]);
+ aShift.Normalize();
+ aShift.Multiply(theStep * 1.5);
+ return aP.Translated(aShift);
+ }
+ return aP;
+}
+
+//*****************************************************************
+void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)