1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketcherPrs_PositionMgr.h"
22 #include "SketcherPrs_Tools.h"
24 #include <GeomAPI_Edge.h>
25 #include <GeomAPI_Curve.h>
26 #include <GeomAPI_Vertex.h>
27 #include <GeomAPI_Dir.h>
29 static SketcherPrs_PositionMgr* MyPosMgr = NULL;
31 // The class is implemented as a singlton
32 SketcherPrs_PositionMgr* SketcherPrs_PositionMgr::get()
35 MyPosMgr = new SketcherPrs_PositionMgr();
39 SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
44 int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine,
45 const SketcherPrs_SymbolPrs* thePrs)
47 if (myShapes.count(theLine) == 1) {
48 // Find the map and add new [Presentation - Index] pair
49 PositionsMap& aPosMap = myShapes[theLine];
50 if (aPosMap.count(thePrs) == 1) {
51 // return existing index
52 return aPosMap[thePrs];
54 // Add a new [Presentation - Index] pair
55 int aInd = int(aPosMap.size());
56 aPosMap[thePrs] = aInd;
60 // Create a new map with initial index
63 myShapes[theLine] = aPosMap;
68 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
69 const SketcherPrs_SymbolPrs* thePrs,
72 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
73 gp_Pnt aP; // Central point
74 gp_Vec aVec1; // main vector
75 if (aShape->isEdge()) {
76 std::shared_ptr<GeomAPI_Curve> aCurve =
77 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
78 std::shared_ptr<GeomAPI_Pnt> aPnt1; // Start point of main vector
79 std::shared_ptr<GeomAPI_Pnt> aPnt2; // End point of main vector
80 if (aCurve->isLine()) {
81 std::shared_ptr<GeomAPI_Edge> aEdge =
82 std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aShape));
84 aPnt1 = aEdge->firstPoint();
85 aPnt2 = aEdge->lastPoint();
87 // Find the middle point
88 aP = gp_Pnt((aPnt1->x() + aPnt2->x())/2.,
89 (aPnt1->y() + aPnt2->y())/2.,
90 (aPnt1->z() + aPnt2->z())/2.);
93 // this is a circle or arc
94 double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
95 std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
96 aP = aPnt->impl<gp_Pnt>();
98 aPnt1 = aCurve->getPoint((aMidParam + aCurve->endParam()) / 2.);
99 aPnt2 = aCurve->getPoint((aMidParam + aCurve->startParam()) / 2.);
101 aVec1 = gp_Vec(aPnt1->impl<gp_Pnt>(), aPnt2->impl<gp_Pnt>());
104 std::shared_ptr<GeomAPI_Vertex> aVertex =
105 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
106 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
107 aP = aPnt->impl<gp_Pnt>();
109 std::shared_ptr<GeomAPI_Dir> aDir = thePrs->plane()->dirX();
110 aVec1 = gp_Vec(aDir->impl<gp_Dir>());
112 // Compute shifting vector for a one symbol
113 gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
115 aShift.Multiply(theStep * 0.8);
117 // Shift the position coordinate according to position index
118 int aPos = getPositionIndex(theShape, thePrs);
120 if ((aPos % 2) == 0) {
122 aP.Translate(aShift);
127 aM = -(aPos + 2) / 4;
131 aP.Translate(-aShift);
133 if ((aPos - 1) % 4 == 0)
136 aM = -(aPos + 1) / 4;
140 // Normalize vector along the line
142 aVec1.Multiply(theStep);
143 aP.Translate(aVec1.Multiplied(aM));
148 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
150 std::map<ObjectPtr, PositionsMap>::iterator aIt;
151 std::list<ObjectPtr> aToDel;
152 // Clear map for deleted presentation
153 for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
154 PositionsMap& aPosMap = aIt->second;
155 if (aPosMap.count(thePrs) > 0) {
157 aPosMap.erase(aPosMap.find(thePrs));
158 if (aPosMap.size() == 0)
160 aToDel.push_back(aIt->first);
162 // Reindex objects positions in order to avoid spaces
163 PositionsMap::iterator aIt;
165 for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
170 std::list<ObjectPtr>::const_iterator aListIt;
171 for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
172 myShapes.erase(*aListIt);