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;
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 PositionsMap& aPosMap = myShapes[theLine];
35 if (aPosMap.count(thePrs) == 1) {
36 return aPosMap[thePrs];
38 int aInd = aPosMap.size();
39 aPosMap[thePrs] = aInd;
45 myShapes[theLine] = aPosMap;
50 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
51 const SketcherPrs_SymbolPrs* thePrs,
54 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
55 gp_Pnt aP; // Central point
56 gp_Vec aVec1; // main vector
57 if (aShape->isEdge()) {
58 std::shared_ptr<GeomAPI_Curve> aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
59 std::shared_ptr<GeomAPI_Pnt> aPnt1; // Start point of main vector
60 std::shared_ptr<GeomAPI_Pnt> aPnt2; // End point of main vector
61 if (aCurve->isLine()) {
62 std::shared_ptr<GeomAPI_Edge> aEdge =
63 std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aShape));
65 aPnt1 = aEdge->firstPoint();
66 aPnt2 = aEdge->lastPoint();
68 // Find the middle point
69 aP = gp_Pnt((aPnt1->x() + aPnt2->x())/2.,
70 (aPnt1->y() + aPnt2->y())/2.,
71 (aPnt1->z() + aPnt2->z())/2.);
74 double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
75 std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
76 aP = aPnt->impl<gp_Pnt>();
78 aPnt1 = aCurve->getPoint((aMidParam + aCurve->endParam()) / 2.);
79 aPnt2 = aCurve->getPoint((aMidParam + aCurve->startParam()) / 2.);
81 aVec1 = gp_Vec(aPnt1->impl<gp_Pnt>(), aPnt2->impl<gp_Pnt>());
84 std::shared_ptr<GeomAPI_Vertex> aVertex = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
85 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
86 aP = aPnt->impl<gp_Pnt>();
88 std::shared_ptr<GeomAPI_Dir> aDir = thePrs->plane()->dirX();
89 aVec1 = gp_Vec(aDir->impl<gp_Dir>());
91 gp_Vec aShift = aVec1.Crossed(thePrs->plane()->norm()->impl<gp_Dir>());
93 aShift.Multiply(theStep * 0.8);
95 int aPos = getPositionIndex(theShape, thePrs);
97 if ((aPos % 2) == 0) {
104 aM = -(aPos + 2) / 4;
108 aP.Translate(-aShift);
110 if ((aPos - 1) % 4 == 0)
113 aM = -(aPos + 1) / 4;
117 // Normalize vector along the line
119 aVec1.Multiply(theStep);
120 aP.Translate(aVec1.Multiplied(aM));
125 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
127 std::map<ObjectPtr, PositionsMap>::iterator aIt;
128 std::list<ObjectPtr> aToDel;
129 for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
130 PositionsMap& aPosMap = aIt->second;
131 if (aPosMap.count(thePrs) > 0) {
132 aPosMap.erase(aPosMap.find(thePrs));
133 if (aPosMap.size() == 0)
134 aToDel.push_back(aIt->first);
137 std::list<ObjectPtr>::const_iterator aListIt;
138 for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
139 myShapes.erase(*aListIt);