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 = int(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 =
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));
70 aPnt1 = aEdge->firstPoint();
71 aPnt2 = aEdge->lastPoint();
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.);
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>();
84 aPnt1 = aCurve->getPoint((aMidParam + aCurve->endParam()) / 2.);
85 aPnt2 = aCurve->getPoint((aMidParam + aCurve->startParam()) / 2.);
87 aVec1 = gp_Vec(aPnt1->impl<gp_Pnt>(), aPnt2->impl<gp_Pnt>());
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>();
95 std::shared_ptr<GeomAPI_Dir> aDir = thePrs->plane()->dirX();
96 aVec1 = gp_Vec(aDir->impl<gp_Dir>());
98 // Compute shifting vector for a one symbol
99 gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
101 aShift.Multiply(theStep * 0.8);
103 // Shift the position coordinate according to position index
104 int aPos = getPositionIndex(theShape, thePrs);
106 if ((aPos % 2) == 0) {
108 aP.Translate(aShift);
113 aM = -(aPos + 2) / 4;
117 aP.Translate(-aShift);
119 if ((aPos - 1) % 4 == 0)
122 aM = -(aPos + 1) / 4;
126 // Normalize vector along the line
128 aVec1.Multiply(theStep);
129 aP.Translate(aVec1.Multiplied(aM));
134 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
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) {
143 aPosMap.erase(aPosMap.find(thePrs));
144 if (aPosMap.size() == 0)
146 aToDel.push_back(aIt->first);
148 // Reindex objects positions in order to avoid spaces
149 PositionsMap::iterator aIt;
151 for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
156 std::list<ObjectPtr>::const_iterator aListIt;
157 for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
158 myShapes.erase(*aListIt);