1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketcherPrs_PositionMgr.cpp
4 // Created: 13 March 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "SketcherPrs_PositionMgr.h"
8 #include "SketcherPrs_Tools.h"
10 #include <GeomAPI_Edge.h>
11 #include <GeomAPI_Curve.h>
12 #include <GeomAPI_Vertex.h>
13 #include <GeomAPI_Dir.h>
15 static const int MyStep = 20;
17 static SketcherPrs_PositionMgr* MyPosMgr = NULL;
20 SketcherPrs_PositionMgr* SketcherPrs_PositionMgr::get()
23 MyPosMgr = new SketcherPrs_PositionMgr();
27 SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
32 int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine,
33 Handle(SketcherPrs_SymbolPrs) thePrs)
35 if (myShapes.count(theLine) == 1) {
36 PositionsMap& aPosMap = myShapes[theLine];
37 if (aPosMap.count(thePrs.Access()) == 1) {
38 return aPosMap[thePrs.Access()];
40 int aInd = aPosMap.size();
41 aPosMap[thePrs.Access()] = aInd;
46 aPosMap[thePrs.Access()] = 0;
47 myShapes[theLine] = aPosMap;
52 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
53 Handle(SketcherPrs_SymbolPrs) thePrs)
55 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
56 gp_Pnt aP; // Central point
57 gp_Vec aVec1; // main vector
58 if (aShape->isEdge()) {
59 std::shared_ptr<GeomAPI_Curve> aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
60 std::shared_ptr<GeomAPI_Pnt> aPnt1; // Start point of main vector
61 std::shared_ptr<GeomAPI_Pnt> aPnt2; // End point of main vector
62 if (aCurve->isLine()) {
63 std::shared_ptr<GeomAPI_Edge> aEdge =
64 std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aShape));
66 aPnt1 = aEdge->firstPoint();
67 aPnt2 = aEdge->lastPoint();
69 // Find the middle point
70 aP = gp_Pnt((aPnt1->x() + aPnt2->x())/2.,
71 (aPnt1->y() + aPnt2->y())/2.,
72 (aPnt1->z() + aPnt2->z())/2.);
75 double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
76 std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
77 aP = aPnt->impl<gp_Pnt>();
79 aPnt1 = aCurve->getPoint((aMidParam + aCurve->endParam()) / 2.);
80 aPnt2 = aCurve->getPoint((aMidParam + aCurve->startParam()) / 2.);
82 aVec1 = gp_Vec(aPnt1->impl<gp_Pnt>(), aPnt2->impl<gp_Pnt>());
85 std::shared_ptr<GeomAPI_Vertex> aVertex = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
86 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
87 aP = aPnt->impl<gp_Pnt>();
89 std::shared_ptr<GeomAPI_Dir> aDir = thePrs->plane()->dirX();
90 aVec1 = gp_Vec(aDir->impl<gp_Dir>());
92 gp_Vec aShift = aVec1.Crossed(thePrs->plane()->norm()->impl<gp_Dir>());
94 aShift.Multiply(MyStep);
96 int aPos = getPositionIndex(theShape, thePrs);
98 if ((aPos % 2) == 0) {
100 aP.Translate(aShift);
105 aM = -(aPos + 2) / 4;
109 aP.Translate(-aShift);
114 aM = -(aPos + 1) / 4;
118 // Normalize vector along the line
120 aVec1.Multiply(MyStep);
121 aP.Translate(aVec1.Multiplied(aM));
126 void SketcherPrs_PositionMgr::deleteConstraint(Handle(SketcherPrs_SymbolPrs) thePrs)
128 std::map<ObjectPtr, PositionsMap>::iterator aIt;
129 for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
130 PositionsMap& aPosMap = aIt->second;
131 if (aPosMap.count(thePrs.Access()) > 0)
132 aPosMap.erase(aPosMap.find(thePrs.Access()));
134 for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
135 if (aIt->second.size() == 0) {
137 aIt = myShapes.begin();