Salome HOME
Merge branch 'python_parametric_api' of https://git.salome-platform.org/git/modules...
[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 #include <GeomAPI_Curve.h>
12 #include <GeomAPI_Vertex.h>
13 #include <GeomAPI_Dir.h>
14
15 static SketcherPrs_PositionMgr* MyPosMgr = NULL;
16
17 // The class is implemented as a singlton
18 SketcherPrs_PositionMgr* SketcherPrs_PositionMgr::get()
19 {
20   if (MyPosMgr == NULL) 
21     MyPosMgr = new SketcherPrs_PositionMgr();
22   return MyPosMgr;
23 }
24
25 SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
26 {
27 }
28
29
30 int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine, 
31                                               const SketcherPrs_SymbolPrs* thePrs)
32 {
33   if (myShapes.count(theLine) == 1) {
34     // Find the map and add new [Presentation - Index] pair
35     PositionsMap& aPosMap = myShapes[theLine];
36     if (aPosMap.count(thePrs) == 1) {
37       // return existing index
38       return aPosMap[thePrs];
39     } else {
40       // Add a new [Presentation - Index] pair
41       int aInd = aPosMap.size();
42       aPosMap[thePrs] = aInd;
43       return aInd;
44     }
45   } else {
46     // Create a new map with initial index
47     PositionsMap aPosMap;
48     aPosMap[thePrs] = 0;
49     myShapes[theLine] = aPosMap;
50     return 0;
51   }
52 }
53
54 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape, 
55                                             const SketcherPrs_SymbolPrs* thePrs,
56                                             double theStep)
57 {
58   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
59   gp_Pnt aP; // Central point
60   gp_Vec aVec1; // main vector
61   if (aShape->isEdge()) {
62     std::shared_ptr<GeomAPI_Curve> aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
63     std::shared_ptr<GeomAPI_Pnt> aPnt1; // Start point of main vector
64     std::shared_ptr<GeomAPI_Pnt> aPnt2; // End point of main vector
65     if (aCurve->isLine()) {
66       std::shared_ptr<GeomAPI_Edge> aEdge = 
67         std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aShape));
68
69       aPnt1 = aEdge->firstPoint();
70       aPnt2 = aEdge->lastPoint();
71
72       // Find the middle point
73       aP = gp_Pnt((aPnt1->x() + aPnt2->x())/2.,
74                   (aPnt1->y() + aPnt2->y())/2.,
75                   (aPnt1->z() + aPnt2->z())/2.);
76
77     } else {
78       // this is a circle or arc
79       double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
80       std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
81       aP = aPnt->impl<gp_Pnt>();
82
83       aPnt1 = aCurve->getPoint((aMidParam + aCurve->endParam()) / 2.);
84       aPnt2 = aCurve->getPoint((aMidParam + aCurve->startParam()) / 2.);
85     }
86     aVec1 = gp_Vec(aPnt1->impl<gp_Pnt>(), aPnt2->impl<gp_Pnt>());
87   } else {
88     // This is a point
89     std::shared_ptr<GeomAPI_Vertex> aVertex = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
90     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
91     aP = aPnt->impl<gp_Pnt>();
92
93     std::shared_ptr<GeomAPI_Dir> aDir = thePrs->plane()->dirX();
94     aVec1 = gp_Vec(aDir->impl<gp_Dir>());
95   }
96   // Compute shifting vector for a one symbol
97   gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
98   aShift.Normalize();
99   aShift.Multiply(theStep * 0.8);
100
101   // Shift the position coordinate according to position index
102   int aPos = getPositionIndex(theShape, thePrs);
103   int aM = 1;
104   if ((aPos % 2) == 0) {
105     // Even position
106     aP.Translate(aShift);
107     if (aPos > 0) {
108       if (aPos % 4 == 0) 
109         aM = aPos / 4;
110       else
111         aM = -(aPos + 2) / 4;
112     }
113   } else {
114     // Odd position
115     aP.Translate(-aShift);
116     if (aPos > 1) {
117       if ((aPos - 1) % 4 == 0) 
118         aM = (aPos - 1) / 4;
119       else
120         aM = -(aPos + 1) / 4;
121     }
122   }
123   if (aPos > 1) {
124     // Normalize vector along the line
125     aVec1.Normalize();
126     aVec1.Multiply(theStep);
127     aP.Translate(aVec1.Multiplied(aM));
128   }
129   return aP;
130 }
131
132 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
133 {
134   std::map<ObjectPtr, PositionsMap>::iterator aIt;
135   std::list<ObjectPtr> aToDel;
136   // Clear map for deleted presentation
137   for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
138     PositionsMap& aPosMap = aIt->second;
139     if (aPosMap.count(thePrs) > 0) {
140       // Erase index
141       aPosMap.erase(aPosMap.find(thePrs));
142       if (aPosMap.size() == 0)
143         // Delete the map
144         aToDel.push_back(aIt->first);
145       else {
146         // Reindex objects positions in order to avoid spaces
147         PositionsMap::iterator aIt;
148         int i = 0;
149         for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
150           aIt->second = i;
151       }
152     }
153   }
154   std::list<ObjectPtr>::const_iterator aListIt;
155   for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
156     myShapes.erase(*aListIt);
157   }
158 }