Salome HOME
Position manager created
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_PositionMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_PositionMgr.cpp
4 // Created:     13 March 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_PositionMgr.h"
8
9 #include <GeomAPI_Edge.h>
10
11 static const int MyStep = 20;
12
13 static SketcherPrs_PositionMgr* MyPosMgr = NULL;
14
15
16 SketcherPrs_PositionMgr* SketcherPrs_PositionMgr::get()
17 {
18   if (MyPosMgr == NULL) 
19     MyPosMgr = new SketcherPrs_PositionMgr();
20   return MyPosMgr;
21 }
22
23 SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
24 {
25 }
26
27
28 int SketcherPrs_PositionMgr::getPositionIndex(std::shared_ptr<GeomAPI_Shape> theLine, 
29                                               Handle(SketcherPrs_SymbolPrs) thePrs)
30 {
31   if (myShapes.count(theLine) == 1) {
32     PositionsMap& aPosMap = myShapes[theLine];
33     if (aPosMap.count(thePrs.Access()) == 1) {
34       return aPosMap[thePrs.Access()];
35     } else {
36       int aInd = aPosMap.size();
37       aPosMap[thePrs.Access()] = aInd;
38       return aInd;
39     }
40   } else {
41     PositionsMap aPosMap;
42     aPosMap[thePrs.Access()] = 0;
43     myShapes[theLine] = aPosMap;
44     return 0;
45   }
46 }
47
48 gp_Pnt SketcherPrs_PositionMgr::getPosition(std::shared_ptr<GeomAPI_Shape> theLine, 
49                                             Handle(SketcherPrs_SymbolPrs) thePrs)
50 {
51   std::shared_ptr<GeomAPI_Edge> aEdge = 
52     std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(theLine));
53
54   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
55   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
56
57   // Find the middle point
58   gp_Pnt aP((aPnt1->x() + aPnt2->x())/2.,
59             (aPnt1->y() + aPnt2->y())/2.,
60             (aPnt1->z() + aPnt2->z())/2.);
61
62   gp_Vec aVec1(aPnt1->impl<gp_Pnt>(), aPnt2->impl<gp_Pnt>());
63   gp_Vec aShift = aVec1.Crossed(thePrs->plane()->norm()->impl<gp_Dir>());
64   aShift.Normalize();
65   aShift.Multiply(MyStep);
66
67   int aPos = getPositionIndex(theLine, thePrs);
68   int aM = 1;
69   if ((aPos % 2) == 0) {
70     // Even position
71     aP.Translate(aShift);
72     if (aPos > 0) {
73       if (aPos % 4 == 0) 
74         aM = aPos / 4;
75       else
76         aM = -(aPos + 2) / 4;
77     }
78   } else {
79     // Odd position
80     aP.Translate(-aShift);
81     if (aPos > 1) {
82       if (aPos % 4 == 0) 
83         aM = (aPos - 1) / 4;
84       else
85         aM = -(aPos + 1) / 4;
86     }
87   }
88   if (aPos > 1) {
89     // Normalize vector along the line
90     aVec1.Normalize();
91     aVec1.Multiply(MyStep);
92     aP.Translate(aVec1.Multiplied(aM));
93   }
94   return aP;
95 }
96
97 void SketcherPrs_PositionMgr::deleteConstraint(Handle(SketcherPrs_SymbolPrs) thePrs)
98 {
99   std::map<std::shared_ptr<GeomAPI_Shape>, PositionsMap>::iterator aIt;
100   for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
101     PositionsMap& aPosMap = aIt->second;
102     if (aPosMap.count(thePrs.Access()) > 0) 
103       aPosMap.erase(aPosMap.find(thePrs.Access()));
104   }
105   for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
106     if (aIt->second.size() == 0) {
107       myShapes.erase(aIt);
108       aIt = myShapes.begin();
109     }
110   }
111 }