]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_PositionMgr.cpp
Salome HOME
Issue #2208: positioning of tangent and perpendicular symbols
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_PositionMgr.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketcherPrs_PositionMgr.h"
22 #include "SketcherPrs_Tools.h"
23
24 #include <GeomAPI_Edge.h>
25 #include <GeomAPI_Curve.h>
26 #include <GeomAPI_Vertex.h>
27 #include <GeomAPI_Dir.h>
28
29 #include <BRepExtrema_ExtPC.hxx>
30 #include <TopoDS_Vertex.hxx>
31 #include <Geom_Curve.hxx>
32
33 static SketcherPrs_PositionMgr* MyPosMgr = NULL;
34
35 // The class is implemented as a singlton
36 SketcherPrs_PositionMgr* SketcherPrs_PositionMgr::get()
37 {
38   if (MyPosMgr == NULL)
39     MyPosMgr = new SketcherPrs_PositionMgr();
40   return MyPosMgr;
41 }
42
43 SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
44 {
45 }
46
47
48 int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine,
49                                               const SketcherPrs_SymbolPrs* thePrs)
50 {
51   if (myShapes.count(theLine) == 1) {
52     // Find the map and add new [Presentation - Index] pair
53     PositionsMap& aPosMap = myShapes[theLine];
54     if (aPosMap.count(thePrs) == 1) {
55       // return existing index
56       return aPosMap[thePrs];
57     } else {
58       // Add a new [Presentation - Index] pair
59       int aInd = int(aPosMap.size());
60       aPosMap[thePrs] = aInd;
61       return aInd;
62     }
63   } else {
64     // Create a new map with initial index
65     PositionsMap aPosMap;
66     aPosMap[thePrs] = 0;
67     myShapes[theLine] = aPosMap;
68     return 0;
69   }
70 }
71
72
73 gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
74 {
75   gp_Vec aVec;
76   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
77   if (aShape->isEdge()) {
78     std::shared_ptr<GeomAPI_Curve> aCurve =
79       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
80
81     if (aCurve->isCircle()) {
82       GeomEdgePtr aEdgePtr(new GeomAPI_Edge(aShape));
83       GeomVertexPtr aVertexPtr(new GeomAPI_Vertex(theP.X(), theP.Y(), theP.Z()));
84       BRepExtrema_ExtPC aExtrema(aVertexPtr->impl<TopoDS_Vertex>(),
85                                  aEdgePtr->impl<TopoDS_Edge>());
86       int aNb = aExtrema.NbExt();
87       if (aNb > 0) {
88         for (int i = 1; i <= aNb; i++) {
89           if (aExtrema.IsMin(i)) {
90             double aParam = aExtrema.Parameter(i);
91             Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
92             gp_Pnt aP;
93             aCurv->D1(aParam, aP, aVec);
94             break;
95           }
96         }
97       }
98     } else {
99       double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
100       GeomPointPtr aPnt1 = aCurve->getPoint((aMidParam + aCurve->endParam()) / 2.);
101       GeomPointPtr aPnt2 = aCurve->getPoint((aMidParam + aCurve->startParam()) / 2.);
102
103       gp_Pnt aPn2 = aPnt2->impl<gp_Pnt>();
104       if (aPn2.IsEqual(theP, Precision::Confusion()))
105         aVec = gp_Vec(aPn2, aPnt1->impl<gp_Pnt>());
106       else
107         aVec = gp_Vec(aPnt1->impl<gp_Pnt>(), aPn2);
108     }
109   } else {
110     aVec = gp_Vec(theDir->impl<gp_Dir>());
111   }
112   return aVec;
113 }
114
115 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
116                                             const SketcherPrs_SymbolPrs* thePrs,
117                                             double theStep, GeomPointPtr thePnt)
118 {
119   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
120   gp_Pnt aP; // Central point
121
122   if (thePnt.get()) {
123     return getPointPosition(theShape, thePrs, theStep, thePnt);
124   } else {
125     if (aShape->isEdge()) {
126       std::shared_ptr<GeomAPI_Curve> aCurve =
127         std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
128       // this is a circle or arc
129       double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
130       std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
131       aP = aPnt->impl<gp_Pnt>();
132     } else {
133       // This is a point
134       std::shared_ptr<GeomAPI_Vertex> aVertex =
135         std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
136       std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
137       aP = aPnt->impl<gp_Pnt>();
138     }
139   }
140   // main vector
141   gp_Vec aVec1 = getVector(theShape, thePrs->plane()->dirX(), aP);
142
143   // Compute shifting vector for a one symbol
144   gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
145   aShift.Normalize();
146   aShift.Multiply(theStep * 0.8);
147
148   // Shift the position coordinate according to position index
149   int aPos = getPositionIndex(theShape, thePrs);
150   int aM = 1;
151   if ((aPos % 2) == 0) {
152     // Even position
153     aP.Translate(aShift);
154     if (aPos > 0) {
155       if (aPos % 4 == 0)
156         aM = aPos / 4;
157       else
158         aM = -(aPos + 2) / 4;
159     }
160   } else {
161     // Odd position
162     aP.Translate(-aShift);
163     if (aPos > 1) {
164       if ((aPos - 1) % 4 == 0)
165         aM = (aPos - 1) / 4;
166       else
167         aM = -(aPos + 1) / 4;
168     }
169   }
170   if (aPos > 1) {
171     // Normalize vector along the line
172     aVec1.Normalize();
173     aVec1.Multiply(theStep);
174     aP.Translate(aVec1.Multiplied(aM));
175   }
176   return aP;
177 }
178
179 gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
180   ObjectPtr theLine, const SketcherPrs_SymbolPrs* thePrs,
181   double theStep, GeomPointPtr thePnt)
182 {
183   gp_Pnt aP = thePnt->impl<gp_Pnt>();
184   gp_Vec aVec1 = getVector(theLine, thePrs->plane()->dirX(), aP);
185
186   // Compute shifting vector for a one symbol
187   gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
188   aShift.Normalize();
189   aShift.Multiply(theStep * 1.5);
190   aP.Translate(aShift);
191   return aP;
192 }
193
194
195 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
196 {
197   std::map<ObjectPtr, PositionsMap>::iterator aIt;
198   std::list<ObjectPtr> aToDel;
199   // Clear map for deleted presentation
200   for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
201     PositionsMap& aPosMap = aIt->second;
202     if (aPosMap.count(thePrs) > 0) {
203       // Erase index
204       aPosMap.erase(aPosMap.find(thePrs));
205       if (aPosMap.size() == 0)
206         // Delete the map
207         aToDel.push_back(aIt->first);
208       else {
209         // Reindex objects positions in order to avoid spaces
210         PositionsMap::iterator aIt;
211         int i = 0;
212         for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
213           aIt->second = i;
214       }
215     }
216   }
217   std::list<ObjectPtr>::const_iterator aListIt;
218   for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
219     myShapes.erase(*aListIt);
220   }
221 }