]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_PositionMgr.cpp
Salome HOME
51796972caa61b15eafaae8d8422a27ebf7d29e1
[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 #include <GeomAPI_Circ.h>
30
31 #include <GeomDataAPI_Point2D.h>
32
33 #include <SketchPlugin_Line.h>
34 #include <SketchPlugin_Circle.h>
35 #include <SketchPlugin_Arc.h>
36 #include <SketchPlugin_ConstraintTangent.h>
37 #include <SketchPlugin_ConstraintPerpendicular.h>
38
39 #include <BRepExtrema_ExtPC.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <Geom_Curve.hxx>
42 #include <TColGeom_SequenceOfCurve.hxx>
43 #include <gp_Dir.hxx>
44
45 #include <array>
46
47 static SketcherPrs_PositionMgr* MyPosMgr = NULL;
48
49 #define PI 3.1415926535897932
50
51 // The class is implemented as a singlton
52 SketcherPrs_PositionMgr* SketcherPrs_PositionMgr::get()
53 {
54   if (MyPosMgr == NULL)
55     MyPosMgr = new SketcherPrs_PositionMgr();
56   return MyPosMgr;
57 }
58
59 SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
60 {
61 }
62
63
64 int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine,
65                                               const SketcherPrs_SymbolPrs* thePrs)
66 {
67   if (myShapes.count(theLine) == 1) {
68     // Find the map and add new [Presentation - Index] pair
69     PositionsMap& aPosMap = myShapes[theLine];
70     if (aPosMap.count(thePrs) == 1) {
71       // return existing index
72       return aPosMap[thePrs];
73     } else {
74       // Add a new [Presentation - Index] pair
75       int aInd = int(aPosMap.size());
76       aPosMap[thePrs] = aInd;
77       return aInd;
78     }
79   } else {
80     // Create a new map with initial index
81     PositionsMap aPosMap;
82     aPosMap[thePrs] = 0;
83     myShapes[theLine] = aPosMap;
84     return 0;
85   }
86 }
87
88
89 bool SketcherPrs_PositionMgr::isPntConstraint(const std::string& theName)
90 {
91   static std::list<std::string> aConstraints;
92   if (aConstraints.size() == 0) {
93     aConstraints.push_back(SketchPlugin_ConstraintTangent::ID());
94     aConstraints.push_back(SketchPlugin_ConstraintPerpendicular::ID());
95   }
96   std::list<std::string>::const_iterator aIt;
97   for (aIt = aConstraints.cbegin(); aIt != aConstraints.cend(); ++aIt) {
98     if ((*aIt) == theName)
99       return true;
100   }
101   return false;
102 }
103
104 bool containsPoint(const FeaturePtr& theFeature, GeomPnt2dPtr thePnt2d, GeomPointPtr thePos)
105 {
106   if (theFeature->getKind() == SketchPlugin_Line::ID()) {
107     AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
108       theFeature->data()->attribute(SketchPlugin_Line::START_ID()));
109     AttributePoint2DPtr aSPnt2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
110       theFeature->data()->attribute(SketchPlugin_Line::END_ID()));
111
112     GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
113     GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
114
115     if (aPnt1->isEqual(thePnt2d) || aPnt2->isEqual(thePnt2d))
116       return true;
117   } else if ((theFeature->getKind() == SketchPlugin_Circle::ID()) ||
118              (theFeature->getKind() == SketchPlugin_Arc::ID())) {
119     GeomCurvePtr aCurve;
120     ObjectPtr aResObj;
121     std::list<ResultPtr> aResults = theFeature->results();
122     std::list<ResultPtr>::const_iterator aIt;
123     for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
124       GeomShapePtr aShp = SketcherPrs_Tools::getShape((*aIt));
125       if (aShp->isEdge()) {
126         aResObj = (*aIt);
127         aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
128         break;
129       }
130     }
131     if (aCurve.get()) {
132       double aStart = aCurve->startParam();
133       double aEnd = aCurve->endParam();
134       GeomCirclePtr  aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
135       double aParam;
136       if (aCircle->parameter(thePos, 1.e-4, aParam) && (aParam >= aStart) && (aParam <= aEnd))
137         return true;
138     }
139   }
140   return false;
141 }
142
143 int SketcherPrs_PositionMgr::getPositionIndex(GeomPointPtr thePos,
144                                               const SketcherPrs_SymbolPrs* thePrs)
145 {
146   if (myPntShapes.count(thePrs->feature()) == 0) {
147     // Renumerate positions around the specified constraint point for all constraints
148     GeomAx3Ptr aAx3 = thePrs->plane();
149     ModelAPI_CompositeFeature* aOwner = thePrs->sketcher();
150     GeomPnt2dPtr aPnt2d = thePos->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
151
152     int aNbSubs = aOwner->numberOfSubs();
153     int aId = 0;
154     for (int i = 0; i < aNbSubs; i++) {
155       FeaturePtr aFeature = aOwner->subFeature(i);
156
157       if (myPntShapes.count(aFeature.get()) == 1) {
158         myPntShapes[aFeature.get()] = aId;
159         aId++;
160       } else {
161         if (isPntConstraint(aFeature->getKind())) {
162           DataPtr aData = aFeature->data();
163           AttributeRefAttrPtr aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
164           FeaturePtr aObj = ModelAPI_Feature::feature(aObjRef->object());
165           bool aContains = false;
166           if (containsPoint(aObj, aPnt2d, thePos)) {
167             aContains = true;
168           } else {
169             aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
170             aObj = ModelAPI_Feature::feature(aObjRef->object());
171             if (containsPoint(aObj, aPnt2d, thePos)) {
172               aContains = true;
173             }
174           }
175           if (aContains) {
176             myPntShapes[aFeature.get()] = aId;
177             aId++;
178           }
179         }
180       }
181     }
182   }
183   return myPntShapes[thePrs->feature()];
184 }
185
186
187 gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
188 {
189   gp_Vec aVec;
190   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
191   if (aShape->isEdge()) {
192     std::shared_ptr<GeomAPI_Curve> aCurve =
193       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
194
195     if (aCurve->isCircle()) {
196       GeomEdgePtr aEdgePtr(new GeomAPI_Edge(aShape));
197       GeomVertexPtr aVertexPtr(new GeomAPI_Vertex(theP.X(), theP.Y(), theP.Z()));
198       BRepExtrema_ExtPC aExtrema(aVertexPtr->impl<TopoDS_Vertex>(),
199                                  aEdgePtr->impl<TopoDS_Edge>());
200       int aNb = aExtrema.NbExt();
201       if (aNb > 0) {
202         for (int i = 1; i <= aNb; i++) {
203           if (aExtrema.IsMin(i)) {
204             double aParam = aExtrema.Parameter(i);
205             Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
206             gp_Pnt aP;
207             aCurv->D1(aParam, aP, aVec);
208             break;
209           }
210         }
211       }
212     } else {
213       GeomPointPtr aPnt1 = aCurve->getPoint(aCurve->endParam());
214       GeomPointPtr aPnt2 = aCurve->getPoint(aCurve->startParam());
215
216       gp_Pnt aPn2 = aPnt2->impl<gp_Pnt>();
217       if (aPn2.IsEqual(theP, Precision::Confusion()))
218         aVec = gp_Vec(aPn2, aPnt1->impl<gp_Pnt>());
219       else
220         aVec = gp_Vec(aPnt1->impl<gp_Pnt>(), aPn2);
221     }
222   } else {
223     aVec = gp_Vec(theDir->impl<gp_Dir>());
224   }
225   return aVec;
226 }
227
228 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
229                                             const SketcherPrs_SymbolPrs* thePrs,
230                                             double theStep, GeomPointPtr thePnt)
231 {
232   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
233   gp_Pnt aP; // Central point
234
235   if (thePnt.get()) {
236     return getPointPosition(theShape, thePrs, theStep, thePnt);
237   } else {
238     if (aShape->isEdge()) {
239       std::shared_ptr<GeomAPI_Curve> aCurve =
240         std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
241       // this is a circle or arc
242       double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
243       std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
244       aP = aPnt->impl<gp_Pnt>();
245     } else {
246       // This is a point
247       std::shared_ptr<GeomAPI_Vertex> aVertex =
248         std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
249       std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
250       aP = aPnt->impl<gp_Pnt>();
251     }
252   }
253   // main vector
254   gp_Vec aVec1 = getVector(theShape, thePrs->plane()->dirX(), aP);
255
256   // Compute shifting vector for a one symbol
257   gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
258   aShift.Normalize();
259   aShift.Multiply(theStep * 0.8);
260
261   // Shift the position coordinate according to position index
262   int aPos = getPositionIndex(theShape, thePrs);
263   int aM = 1;
264   if ((aPos % 2) == 0) {
265     // Even position
266     aP.Translate(aShift);
267     if (aPos > 0) {
268       if (aPos % 4 == 0)
269         aM = aPos / 4;
270       else
271         aM = -(aPos + 2) / 4;
272     }
273   } else {
274     // Odd position
275     aP.Translate(-aShift);
276     if (aPos > 1) {
277       if ((aPos - 1) % 4 == 0)
278         aM = (aPos - 1) / 4;
279       else
280         aM = -(aPos + 1) / 4;
281     }
282   }
283   if (aPos > 1) {
284     // Normalize vector along the line
285     aVec1.Normalize();
286     aVec1.Multiply(theStep);
287     aP.Translate(aVec1.Multiplied(aM));
288   }
289   return aP;
290 }
291
292
293 //*****************************************************************
294 //! Returns curves connected to the given point
295 std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_SymbolPrs* thePrs)
296 {
297   std::list<ObjectPtr> aList;
298   GeomAx3Ptr aAx3 = thePrs->plane();
299   ModelAPI_CompositeFeature* aOwner = thePrs->sketcher();
300   GeomPnt2dPtr aPnt2d = thePnt->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
301
302   int aNbSubs = aOwner->numberOfSubs();
303   for (int i = 0; i < aNbSubs; i++) {
304     FeaturePtr aFeature = aOwner->subFeature(i);
305     if (!aFeature->firstResult().get()) // There is no result
306       continue;
307
308     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
309       AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
310         aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
311       AttributePoint2DPtr aSPnt2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
312         aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
313
314       GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
315       GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
316
317       if (aPnt1->isEqual(aPnt2d) || aPnt2->isEqual(aPnt2d)) {
318         GeomShapePtr aShp = SketcherPrs_Tools::getShape(aFeature->firstResult());
319         GeomCurvePtr aCurv = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
320         aList.push_back(aFeature->firstResult());
321       }
322     } else if ((aFeature->getKind() == SketchPlugin_Circle::ID()) ||
323               (aFeature->getKind() == SketchPlugin_Arc::ID())) {
324       GeomCurvePtr aCurve;
325       ObjectPtr aResObj;
326       std::list<ResultPtr> aResults = aFeature->results();
327       std::list<ResultPtr>::const_iterator aIt;
328       for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
329         GeomShapePtr aShp = SketcherPrs_Tools::getShape((*aIt));
330         if (aShp->isEdge()) {
331           aResObj = (*aIt);
332           aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
333           break;
334         }
335       }
336       if (aCurve.get()) {
337         double aStart = aCurve->startParam();
338         double aEnd = aCurve->endParam();
339         GeomCirclePtr  aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
340         double aParam;
341         if (aCircle->parameter(thePnt, 1.e-4, aParam) && (aParam >= aStart) && (aParam <= aEnd))
342           aList.push_back(aResObj);
343       }
344     }
345   }
346   return aList;
347 }
348
349 //*****************************************************************
350 gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
351   ObjectPtr theLine, const SketcherPrs_SymbolPrs* thePrs,
352   double theStep, GeomPointPtr thePnt)
353 {
354   gp_Pnt aP = thePnt->impl<gp_Pnt>();
355   GeomDirPtr aNormal = thePrs->plane()->normal();
356   gp_Dir aNormDir = aNormal->impl<gp_Dir>();
357
358   std::list<ObjectPtr> aCurves = getCurves(thePnt, thePrs);
359   std::list<ObjectPtr>::const_iterator aItCurv;
360   std::list<gp_Vec> aVectorsList;
361   // Calculate all vectors
362   for (aItCurv = aCurves.cbegin(); aItCurv != aCurves.cend(); aItCurv++) {
363     aVectorsList.push_back(getVector((*aItCurv), thePrs->plane()->dirX(), aP));
364   }
365
366   std::list<gp_Vec>::const_iterator aItVec;
367   std::map<double, gp_Vec> aAngVectors;
368   // Select closest vectors and calculate angles between base vector and closest vector
369   for (aItVec = aVectorsList.cbegin(); aItVec != aVectorsList.cend(); aItVec++) {
370     std::list<gp_Vec>::const_iterator aIt;
371     double aMinAng = 0;
372     gp_Vec aVec = *aItVec;
373     for (aIt = aVectorsList.cbegin(); aIt != aVectorsList.cend(); aIt++) {
374       double aAng = aVec.AngleWithRef(*aIt, aNormDir);
375       if (aAng != 0) {
376         if (aAng < 0)
377           aAng = 2 * PI + aAng;
378
379         if (aMinAng == 0)
380           aMinAng = aAng;
381         else if (aAng < aMinAng) {
382           aMinAng = aAng;
383         }
384       }
385     }
386     aAngVectors[aMinAng] = aVec;
387   }
388
389   // Angle size of a symbol for a first level
390   static const double aAngleStep = PI * 50./180.;
391
392   // Position of the symbol
393   int aPos = getPositionIndex(thePnt, thePrs);
394
395   //std::list<double>::const_iterator aItAng;
396   gp_Ax1 aRotAx(aP, aNormDir);
397   int aPosId = 0; // Last used position
398   double aAng;
399   gp_Vec aPrevVec;
400   std::map<double, gp_Vec>::const_iterator aItAng;
401   for (aItAng = aAngVectors.cbegin(); aItAng != aAngVectors.cend(); ++aItAng) {
402     aAng = aItAng->first;
403     aPrevVec = aItAng->second;
404     if (aAng >= aAngleStep) {
405       gp_Vec aShift;
406       int Nb = int(aAng / aAngleStep);
407       if ((aPos >= aPosId) && (aPos < (aPosId + Nb))) {
408         // rotate base vector on a necessary angle
409         aShift = aPrevVec.Rotated(aRotAx, aAngleStep + aAngleStep * (aPos - aPosId));
410         aShift.Normalize();
411         aShift.Multiply(theStep * 1.5);
412         return aP.Translated(aShift);
413       }
414       aPosId += Nb;
415     }
416   }
417   gp_Vec aShift = aPrevVec.Rotated(aRotAx, aAngleStep);
418   aShift.Normalize();
419   aShift.Multiply(theStep * 1.5);
420   return aP.Translated(aShift);
421 }
422
423 //*****************************************************************
424 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
425 {
426   std::map<ObjectPtr, PositionsMap>::iterator aIt;
427   std::list<ObjectPtr> aToDel;
428   // Clear map for deleted presentation
429   for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
430     PositionsMap& aPosMap = aIt->second;
431     if (aPosMap.count(thePrs) > 0) {
432       // Erase index
433       aPosMap.erase(aPosMap.find(thePrs));
434       if (aPosMap.size() == 0)
435         // Delete the map
436         aToDel.push_back(aIt->first);
437       else {
438         // Reindex objects positions in order to avoid spaces
439         PositionsMap::iterator aIt;
440         int i = 0;
441         for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
442           aIt->second = i;
443       }
444     }
445   }
446   std::list<ObjectPtr>::const_iterator aListIt;
447   for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
448     myShapes.erase(*aListIt);
449   }
450 }