Salome HOME
Issue #1834: Fix length of lines
[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 = int(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 = 
63       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
64     std::shared_ptr<GeomAPI_Pnt> aPnt1; // Start point of main vector
65     std::shared_ptr<GeomAPI_Pnt> aPnt2; // End point of main vector
66     if (aCurve->isLine()) {
67       std::shared_ptr<GeomAPI_Edge> aEdge = 
68         std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aShape));
69
70       aPnt1 = aEdge->firstPoint();
71       aPnt2 = aEdge->lastPoint();
72
73       // Find the middle point
74       aP = gp_Pnt((aPnt1->x() + aPnt2->x())/2.,
75                   (aPnt1->y() + aPnt2->y())/2.,
76                   (aPnt1->z() + aPnt2->z())/2.);
77
78     } else {
79       // this is a circle or arc
80       double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
81       std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
82       aP = aPnt->impl<gp_Pnt>();
83
84       aPnt1 = aCurve->getPoint((aMidParam + aCurve->endParam()) / 2.);
85       aPnt2 = aCurve->getPoint((aMidParam + aCurve->startParam()) / 2.);
86     }
87     aVec1 = gp_Vec(aPnt1->impl<gp_Pnt>(), aPnt2->impl<gp_Pnt>());
88   } else {
89     // This is a point
90     std::shared_ptr<GeomAPI_Vertex> aVertex = 
91       std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
92     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
93     aP = aPnt->impl<gp_Pnt>();
94
95     std::shared_ptr<GeomAPI_Dir> aDir = thePrs->plane()->dirX();
96     aVec1 = gp_Vec(aDir->impl<gp_Dir>());
97   }
98   // Compute shifting vector for a one symbol
99   gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
100   aShift.Normalize();
101   aShift.Multiply(theStep * 0.8);
102
103   // Shift the position coordinate according to position index
104   int aPos = getPositionIndex(theShape, thePrs);
105   int aM = 1;
106   if ((aPos % 2) == 0) {
107     // Even position
108     aP.Translate(aShift);
109     if (aPos > 0) {
110       if (aPos % 4 == 0) 
111         aM = aPos / 4;
112       else
113         aM = -(aPos + 2) / 4;
114     }
115   } else {
116     // Odd position
117     aP.Translate(-aShift);
118     if (aPos > 1) {
119       if ((aPos - 1) % 4 == 0) 
120         aM = (aPos - 1) / 4;
121       else
122         aM = -(aPos + 1) / 4;
123     }
124   }
125   if (aPos > 1) {
126     // Normalize vector along the line
127     aVec1.Normalize();
128     aVec1.Multiply(theStep);
129     aP.Translate(aVec1.Multiplied(aM));
130   }
131   return aP;
132 }
133
134 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
135 {
136   std::map<ObjectPtr, PositionsMap>::iterator aIt;
137   std::list<ObjectPtr> aToDel;
138   // Clear map for deleted presentation
139   for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
140     PositionsMap& aPosMap = aIt->second;
141     if (aPosMap.count(thePrs) > 0) {
142       // Erase index
143       aPosMap.erase(aPosMap.find(thePrs));
144       if (aPosMap.size() == 0)
145         // Delete the map
146         aToDel.push_back(aIt->first);
147       else {
148         // Reindex objects positions in order to avoid spaces
149         PositionsMap::iterator aIt;
150         int i = 0;
151         for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
152           aIt->second = i;
153       }
154     }
155   }
156   std::list<ObjectPtr>::const_iterator aListIt;
157   for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
158     myShapes.erase(*aListIt);
159   }
160 }