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 SketcherPrs_PositionMgr* MyPosMgr = NULL;
17 // The class is implemented as a singlton
18 SketcherPrs_PositionMgr* SketcherPrs_PositionMgr::get()
21 MyPosMgr = new SketcherPrs_PositionMgr();
25 SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
30 int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine,
31 const SketcherPrs_SymbolPrs* thePrs)
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];
40 // Add a new [Presentation - Index] pair
41 int aInd = aPosMap.size();
42 aPosMap[thePrs] = aInd;
46 // Create a new map with initial index
49 myShapes[theLine] = aPosMap;
54 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
55 const SketcherPrs_SymbolPrs* thePrs,
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));
69 aPnt1 = aEdge->firstPoint();
70 aPnt2 = aEdge->lastPoint();
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.);
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>();
83 aPnt1 = aCurve->getPoint((aMidParam + aCurve->endParam()) / 2.);
84 aPnt2 = aCurve->getPoint((aMidParam + aCurve->startParam()) / 2.);
86 aVec1 = gp_Vec(aPnt1->impl<gp_Pnt>(), aPnt2->impl<gp_Pnt>());
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>();
93 std::shared_ptr<GeomAPI_Dir> aDir = thePrs->plane()->dirX();
94 aVec1 = gp_Vec(aDir->impl<gp_Dir>());
96 // Compute shifting vector for a one symbol
97 gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
99 aShift.Multiply(theStep * 0.8);
101 // Shift the position coordinate according to position index
102 int aPos = getPositionIndex(theShape, thePrs);
104 if ((aPos % 2) == 0) {
106 aP.Translate(aShift);
111 aM = -(aPos + 2) / 4;
115 aP.Translate(-aShift);
117 if ((aPos - 1) % 4 == 0)
120 aM = -(aPos + 1) / 4;
124 // Normalize vector along the line
126 aVec1.Multiply(theStep);
127 aP.Translate(aVec1.Multiplied(aM));
132 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
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) {
141 aPosMap.erase(aPosMap.find(thePrs));
142 if (aPosMap.size() == 0)
144 aToDel.push_back(aIt->first);
146 // Reindex objects positions in order to avoid spaces
147 PositionsMap::iterator aIt;
149 for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
154 std::list<ObjectPtr>::const_iterator aListIt;
155 for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
156 myShapes.erase(*aListIt);