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