]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_PositionMgr.cpp
Salome HOME
8897fa16e1566da48e06ea8e27fb2c23c5bf0e81
[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 #include <GeomAPI_Ax3.h>
29
30 #include <SketchPlugin_Line.h>
31 #include <SketchPlugin_Circle.h>
32 #include <SketchPlugin_Arc.h>
33
34 #include <BRepExtrema_ExtPC.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <Geom_Curve.hxx>
37 #include <TColGeom_SequenceOfCurve.hxx>
38
39 static SketcherPrs_PositionMgr* MyPosMgr = NULL;
40
41 // The class is implemented as a singlton
42 SketcherPrs_PositionMgr* SketcherPrs_PositionMgr::get()
43 {
44   if (MyPosMgr == NULL)
45     MyPosMgr = new SketcherPrs_PositionMgr();
46   return MyPosMgr;
47 }
48
49 SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
50 {
51 }
52
53
54 int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine,
55                                               const SketcherPrs_SymbolPrs* thePrs)
56 {
57   if (myShapes.count(theLine) == 1) {
58     // Find the map and add new [Presentation - Index] pair
59     PositionsMap& aPosMap = myShapes[theLine];
60     if (aPosMap.count(thePrs) == 1) {
61       // return existing index
62       return aPosMap[thePrs];
63     } else {
64       // Add a new [Presentation - Index] pair
65       int aInd = int(aPosMap.size());
66       aPosMap[thePrs] = aInd;
67       return aInd;
68     }
69   } else {
70     // Create a new map with initial index
71     PositionsMap aPosMap;
72     aPosMap[thePrs] = 0;
73     myShapes[theLine] = aPosMap;
74     return 0;
75   }
76 }
77
78
79 gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
80 {
81   gp_Vec aVec;
82   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
83   if (aShape->isEdge()) {
84     std::shared_ptr<GeomAPI_Curve> aCurve =
85       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
86
87     if (aCurve->isCircle()) {
88       GeomEdgePtr aEdgePtr(new GeomAPI_Edge(aShape));
89       GeomVertexPtr aVertexPtr(new GeomAPI_Vertex(theP.X(), theP.Y(), theP.Z()));
90       BRepExtrema_ExtPC aExtrema(aVertexPtr->impl<TopoDS_Vertex>(),
91                                  aEdgePtr->impl<TopoDS_Edge>());
92       int aNb = aExtrema.NbExt();
93       if (aNb > 0) {
94         for (int i = 1; i <= aNb; i++) {
95           if (aExtrema.IsMin(i)) {
96             double aParam = aExtrema.Parameter(i);
97             Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
98             gp_Pnt aP;
99             aCurv->D1(aParam, aP, aVec);
100             break;
101           }
102         }
103       }
104     } else {
105       double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
106       GeomPointPtr aPnt1 = aCurve->getPoint((aMidParam + aCurve->endParam()) / 2.);
107       GeomPointPtr aPnt2 = aCurve->getPoint((aMidParam + aCurve->startParam()) / 2.);
108
109       gp_Pnt aPn2 = aPnt2->impl<gp_Pnt>();
110       if (aPn2.IsEqual(theP, Precision::Confusion()))
111         aVec = gp_Vec(aPn2, aPnt1->impl<gp_Pnt>());
112       else
113         aVec = gp_Vec(aPnt1->impl<gp_Pnt>(), aPn2);
114     }
115   } else {
116     aVec = gp_Vec(theDir->impl<gp_Dir>());
117   }
118   return aVec;
119 }
120
121 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
122                                             const SketcherPrs_SymbolPrs* thePrs,
123                                             double theStep, GeomPointPtr thePnt)
124 {
125   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
126   gp_Pnt aP; // Central point
127
128   if (thePnt.get()) {
129     return getPointPosition(theShape, thePrs, theStep, thePnt);
130   } else {
131     if (aShape->isEdge()) {
132       std::shared_ptr<GeomAPI_Curve> aCurve =
133         std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
134       // this is a circle or arc
135       double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
136       std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
137       aP = aPnt->impl<gp_Pnt>();
138     } else {
139       // This is a point
140       std::shared_ptr<GeomAPI_Vertex> aVertex =
141         std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
142       std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
143       aP = aPnt->impl<gp_Pnt>();
144     }
145   }
146   // main vector
147   gp_Vec aVec1 = getVector(theShape, thePrs->plane()->dirX(), aP);
148
149   // Compute shifting vector for a one symbol
150   gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
151   aShift.Normalize();
152   aShift.Multiply(theStep * 0.8);
153
154   // Shift the position coordinate according to position index
155   int aPos = getPositionIndex(theShape, thePrs);
156   int aM = 1;
157   if ((aPos % 2) == 0) {
158     // Even position
159     aP.Translate(aShift);
160     if (aPos > 0) {
161       if (aPos % 4 == 0)
162         aM = aPos / 4;
163       else
164         aM = -(aPos + 2) / 4;
165     }
166   } else {
167     // Odd position
168     aP.Translate(-aShift);
169     if (aPos > 1) {
170       if ((aPos - 1) % 4 == 0)
171         aM = (aPos - 1) / 4;
172       else
173         aM = -(aPos + 1) / 4;
174     }
175   }
176   if (aPos > 1) {
177     // Normalize vector along the line
178     aVec1.Normalize();
179     aVec1.Multiply(theStep);
180     aP.Translate(aVec1.Multiplied(aM));
181   }
182   return aP;
183 }
184
185
186 //*****************************************************************
187 //! Returns curves connected to the given point
188 TColGeom_SequenceOfCurve getCurves(const GeomPointPtr& thePnt, const SketcherPrs_SymbolPrs* thePrs)
189 {
190   TColGeom_SequenceOfCurve aList;
191   //GeomAx3Ptr aAx3 = thePrs->plane();
192   //CompositeFeaturePtr aOwner = thePrs->sketcher();
193   //GeomPnt2dPtr aPnt2d = thePnt->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
194
195   //int aNbSubs = aOwner->numberOfSubs();
196   //for (int i = 0; i < aNbSubs; i++) {
197   //  FeaturePtr aFeature = aOwner->subFeature(i);
198   //  if (aFeature->getKind() == SketchPlugin_Line::ID()) {
199   //    GeomPnt2dPtr aPnt1 =
200   //      SketcherPrs_Tools::getPoint(aFeature.get(), SketchPlugin_Line::START_ID());
201   //    GeomPnt2dPtr aPnt2 =
202   //      SketcherPrs_Tools::getPoint(aFeature.get(), SketchPlugin_Line::END_ID());
203   //    if (aPnt1->isEqual(aPnt2d) || aPnt2->isEqual(aPnt2d)) {
204   //      GeomShapePtr aShp = SketcherPrs_Tools::getShape(aFeature->firstResult());
205   //      GeomCurvePtr aCurv = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
206   //      aList.Append(aCurv->impl<Handle(Geom_Curve)>());
207   //    }
208   //  } else if ((aFeature->getKind() == SketchPlugin_Circle::ID()) ||
209   //            (aFeature->getKind() == SketchPlugin_Arc::ID())) {
210   //    GeomShapePtr aShp = SketcherPrs_Tools::getShape(aFeature->firstResult());
211   //    GeomCurvePtr aCurv = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
212   //  }
213   //}
214   return aList;
215 }
216
217 //*****************************************************************
218 gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
219   ObjectPtr theLine, const SketcherPrs_SymbolPrs* thePrs,
220   double theStep, GeomPointPtr thePnt)
221 {
222   TColGeom_SequenceOfCurve aCurves = getCurves(thePnt, thePrs);
223
224   gp_Pnt aP = thePnt->impl<gp_Pnt>();
225   gp_Vec aVec1 = getVector(theLine, thePrs->plane()->dirX(), aP);
226
227   // Compute shifting vector for a one symbol
228   gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
229   aShift.Normalize();
230   aShift.Multiply(theStep * 1.5);
231   aP.Translate(aShift);
232   return aP;
233 }
234
235 //*****************************************************************
236 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
237 {
238   std::map<ObjectPtr, PositionsMap>::iterator aIt;
239   std::list<ObjectPtr> aToDel;
240   // Clear map for deleted presentation
241   for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
242     PositionsMap& aPosMap = aIt->second;
243     if (aPosMap.count(thePrs) > 0) {
244       // Erase index
245       aPosMap.erase(aPosMap.find(thePrs));
246       if (aPosMap.size() == 0)
247         // Delete the map
248         aToDel.push_back(aIt->first);
249       else {
250         // Reindex objects positions in order to avoid spaces
251         PositionsMap::iterator aIt;
252         int i = 0;
253         for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
254           aIt->second = i;
255       }
256     }
257   }
258   std::list<ObjectPtr>::const_iterator aListIt;
259   for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
260     myShapes.erase(*aListIt);
261   }
262 }