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>
28 #include <GeomAPI_Ax3.h>
29 #include <GeomAPI_Circ.h>
30 #include <GeomAPI_Lin2d.h>
32 #include <GeomDataAPI_Point2D.h>
34 #include <SketchPlugin_Line.h>
35 #include <SketchPlugin_Circle.h>
36 #include <SketchPlugin_Arc.h>
37 #include <SketchPlugin_ConstraintTangent.h>
38 #include <SketchPlugin_ConstraintPerpendicular.h>
40 #include <TopoDS_Vertex.hxx>
41 #include <Geom_Curve.hxx>
42 #include <GeomAPI_ProjectPointOnCurve.hxx>
43 #include <TColGeom_SequenceOfCurve.hxx>
48 static SketcherPrs_PositionMgr* MyPosMgr = NULL;
50 #define PI 3.1415926535897932
52 // The class is implemented as a singlton
53 SketcherPrs_PositionMgr* SketcherPrs_PositionMgr::get()
56 MyPosMgr = new SketcherPrs_PositionMgr();
60 SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
65 int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine,
66 const SketcherPrs_SymbolPrs* thePrs)
68 if (myShapes.count(theLine) == 1) {
69 // Find the map and add new [Presentation - Index] pair
70 PositionsMap& aPosMap = myShapes[theLine];
71 if (aPosMap.count(thePrs) == 1) {
72 // return existing index
73 return aPosMap[thePrs];
75 // Add a new [Presentation - Index] pair
76 int aInd = int(aPosMap.size());
77 aPosMap[thePrs] = aInd;
81 // Create a new map with initial index
84 myShapes[theLine] = aPosMap;
90 bool SketcherPrs_PositionMgr::isPntConstraint(const std::string& theName)
92 static std::list<std::string> aConstraints;
93 if (aConstraints.size() == 0) {
94 aConstraints.push_back(SketchPlugin_ConstraintTangent::ID());
95 aConstraints.push_back(SketchPlugin_ConstraintPerpendicular::ID());
97 std::list<std::string>::const_iterator aIt;
98 for (aIt = aConstraints.cbegin(); aIt != aConstraints.cend(); ++aIt) {
99 if ((*aIt) == theName)
105 bool containsPoint(const FeaturePtr& theFeature, GeomPnt2dPtr thePnt2d, GeomPointPtr thePos)
107 if (theFeature->getKind() == SketchPlugin_Line::ID()) {
108 AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
109 theFeature->data()->attribute(SketchPlugin_Line::START_ID()));
110 AttributePoint2DPtr aSPnt2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
111 theFeature->data()->attribute(SketchPlugin_Line::END_ID()));
113 GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
114 GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
116 if (aPnt1->isEqual(thePnt2d) || aPnt2->isEqual(thePnt2d))
118 } else if ((theFeature->getKind() == SketchPlugin_Circle::ID()) ||
119 (theFeature->getKind() == SketchPlugin_Arc::ID())) {
122 std::list<ResultPtr> aResults = theFeature->results();
123 std::list<ResultPtr>::const_iterator aIt;
124 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
125 GeomShapePtr aShp = SketcherPrs_Tools::getShape((*aIt));
126 if (aShp->isEdge()) {
128 aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
133 double aStart = aCurve->startParam();
134 double aEnd = aCurve->endParam();
135 GeomCirclePtr aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
137 if (aCircle->parameter(thePos, 1.e-4, aParam) && (aParam >= aStart) && (aParam <= aEnd))
144 const std::array<int, 2>& SketcherPrs_PositionMgr::getPositionIndex(GeomPointPtr thePos,
145 const SketcherPrs_SymbolPrs* thePrs)
147 if (myPntShapes.count(thePrs->feature()) == 0) {
148 // Renumerate positions around the specified constraint point for all constraints
149 GeomAx3Ptr aAx3 = thePrs->plane();
150 ModelAPI_CompositeFeature* aOwner = thePrs->sketcher();
151 GeomPnt2dPtr aPnt2d = thePos->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
153 int aNbSubs = aOwner->numberOfSubs();
155 std::list<const ModelAPI_Feature*> aFeaList;
156 for (int i = 0; i < aNbSubs; i++) {
157 FeaturePtr aFeature = aOwner->subFeature(i);
159 if (myPntShapes.count(aFeature.get()) == 1) {
160 myPntShapes[aFeature.get()][0] = aId;
162 aFeaList.push_back(aFeature.get());
164 if (isPntConstraint(aFeature->getKind())) {
165 DataPtr aData = aFeature->data();
166 AttributeRefAttrPtr aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
167 FeaturePtr aObj = ModelAPI_Feature::feature(aObjRef->object());
168 bool aContains = false;
169 if (containsPoint(aObj, aPnt2d, thePos)) {
172 aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
173 aObj = ModelAPI_Feature::feature(aObjRef->object());
174 if (containsPoint(aObj, aPnt2d, thePos)) {
179 myPntShapes[aFeature.get()][0] = aId;
181 aFeaList.push_back(aFeature.get());
186 int aSize = (int) aFeaList.size();
187 std::list<const ModelAPI_Feature*>::const_iterator aIt;
188 for (aIt = aFeaList.cbegin(); aIt != aFeaList.cend(); aIt++) {
189 myPntShapes[*aIt][1] = aSize;
192 return myPntShapes[thePrs->feature()];
195 //*****************************************************************
196 gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
199 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
200 if (aShape->isEdge()) {
201 std::shared_ptr<GeomAPI_Curve> aCurve =
202 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
204 if (aCurve->isCircle()) {
205 Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
206 GeomAPI_ProjectPointOnCurve anExtr(theP, aCurv);
207 double aParam = anExtr.LowerDistanceParameter();
209 aCurv->D1(aParam, aP, aVec);
211 GeomPointPtr aPnt1 = aCurve->getPoint(aCurve->endParam());
212 GeomPointPtr aPnt2 = aCurve->getPoint(aCurve->startParam());
214 gp_Pnt aPn2 = aPnt2->impl<gp_Pnt>();
215 if (aPn2.IsEqual(theP, Precision::Confusion()))
216 aVec = gp_Vec(aPn2, aPnt1->impl<gp_Pnt>());
218 aVec = gp_Vec(aPnt1->impl<gp_Pnt>(), aPn2);
221 aVec = gp_Vec(theDir->impl<gp_Dir>());
226 //*****************************************************************
227 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
228 const SketcherPrs_SymbolPrs* thePrs,
229 double theStep, GeomPointPtr thePnt)
231 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
232 gp_Pnt aP; // Central point
235 return getPointPosition(theShape, thePrs, theStep, thePnt);
237 if (aShape->isEdge()) {
238 std::shared_ptr<GeomAPI_Curve> aCurve =
239 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
240 // this is a circle or arc
241 double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
242 std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
243 aP = aPnt->impl<gp_Pnt>();
246 std::shared_ptr<GeomAPI_Vertex> aVertex =
247 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
248 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
249 aP = aPnt->impl<gp_Pnt>();
253 gp_Vec aVec1 = getVector(theShape, thePrs->plane()->dirX(), aP);
255 // Compute shifting vector for a one symbol
256 gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
258 aShift.Multiply(theStep * 0.8);
260 // Shift the position coordinate according to position index
261 int aPos = getPositionIndex(theShape, thePrs);
263 if ((aPos % 2) == 0) {
265 aP.Translate(aShift);
270 aM = -(aPos + 2) / 4;
274 aP.Translate(-aShift);
276 if ((aPos - 1) % 4 == 0)
279 aM = -(aPos + 1) / 4;
283 // Normalize vector along the line
285 aVec1.Multiply(theStep);
286 aP.Translate(aVec1.Multiplied(aM));
292 //*****************************************************************
293 //! Returns curves connected to the given point
294 std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_SymbolPrs* thePrs)
296 std::list<ObjectPtr> aList;
297 GeomAx3Ptr aAx3 = thePrs->plane();
298 ModelAPI_CompositeFeature* aOwner = thePrs->sketcher();
299 GeomPnt2dPtr aPnt2d = thePnt->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
301 int aNbSubs = aOwner->numberOfSubs();
302 for (int i = 0; i < aNbSubs; i++) {
303 FeaturePtr aFeature = aOwner->subFeature(i);
304 if (!aFeature->firstResult().get()) // There is no result
307 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
308 AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
309 aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
310 AttributePoint2DPtr aSPnt2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
311 aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
313 GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
314 GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
316 if (aPnt1->isEqual(aPnt2d) || aPnt2->isEqual(aPnt2d)) {
317 // a point corresponds to one of the line end
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());
322 // Check that a point belongs to the curve
323 GeomAPI_Lin2d aLin2d(aPnt1, aPnt2);
324 double aDist = aLin2d.distance(aPnt2d);
325 if (aDist <= Precision::Confusion())
326 aList.push_back(aFeature->firstResult());
328 } else if ((aFeature->getKind() == SketchPlugin_Circle::ID()) ||
329 (aFeature->getKind() == SketchPlugin_Arc::ID())) {
332 std::list<ResultPtr> aResults = aFeature->results();
333 std::list<ResultPtr>::const_iterator aIt;
334 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
335 GeomShapePtr aShp = SketcherPrs_Tools::getShape((*aIt));
336 if (aShp->isEdge()) {
338 aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
343 double aStart = aCurve->startParam();
344 double aEnd = aCurve->endParam();
345 GeomCirclePtr aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
346 GeomPointPtr aProjPnt = aCircle->project(thePnt);
347 if (thePnt->distance(aProjPnt) <= Precision::Confusion())
348 aList.push_back(aResObj);
355 //*****************************************************************
356 gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
357 ObjectPtr theLine, const SketcherPrs_SymbolPrs* thePrs,
358 double theStep, GeomPointPtr thePnt)
360 gp_Pnt aP = thePnt->impl<gp_Pnt>();
361 GeomDirPtr aNormal = thePrs->plane()->normal();
362 gp_Dir aNormDir = aNormal->impl<gp_Dir>();
364 std::list<ObjectPtr> aCurves = getCurves(thePnt, thePrs);
365 std::list<ObjectPtr>::const_iterator aItCurv;
366 std::list<gp_Vec> aVectorsList;
367 // Calculate all vectors
368 for (aItCurv = aCurves.cbegin(); aItCurv != aCurves.cend(); aItCurv++) {
369 aVectorsList.push_back(getVector((*aItCurv), thePrs->plane()->dirX(), aP));
372 // Position of the symbol
373 const std::array<int, 2>& aPos = getPositionIndex(thePnt, thePrs);
375 // Angle size of a symbol
376 double aAngleStep = PI * 50./180.;
378 std::list<gp_Vec>::const_iterator aItVec;
379 std::list<double> aAngles;
380 std::list<gp_Vec> aVectors;
381 // Select closest vectors and calculate angles between base vector and closest vector
382 for (aItVec = aVectorsList.cbegin(); aItVec != aVectorsList.cend(); aItVec++) {
383 std::list<gp_Vec>::const_iterator aIt;
385 gp_Vec aVec = *aItVec;
386 for (aIt = aVectorsList.cbegin(); aIt != aVectorsList.cend(); aIt++) {
387 double aAng = aVec.AngleWithRef(*aIt, aNormDir);
390 aAng = 2 * PI + aAng;
394 else if (aAng < aMinAng) {
399 aVectors.push_back(aVec);
400 aAngles.push_back(aMinAng);
405 std::list<double>::const_iterator aItAng;
409 bool aHasPlace = false;
410 //int aIntId = 0; // a position inside a one sector
411 while (aPosCount < aPos[1]) {
412 for (aItAng = aAngles.cbegin(), aItVec = aVectors.cbegin();
413 aItAng != aAngles.cend(); ++aItAng, ++aItVec) {
415 int Nb = int(aAng / aAngleStep);
418 if ((!aHasPlace) && (aPosCount >= (aPos[0] + 1))) {
422 //aIntId = aPos[0] - (aPosCount - Nb);
425 if (aPosCount < aPos[1]) {
434 gp_Ax1 aRotAx(aP, aNormDir);
436 // rotate base vector on a necessary angle
437 gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep + aAngleStep * aPos[0]);
439 aShift.Multiply(theStep * 1.5);
440 return aP.Translated(aShift);
445 //*****************************************************************
446 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
448 std::map<ObjectPtr, PositionsMap>::iterator aIt;
449 std::list<ObjectPtr> aToDel;
450 // Clear map for deleted presentation
451 for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
452 PositionsMap& aPosMap = aIt->second;
453 if (aPosMap.count(thePrs) > 0) {
455 aPosMap.erase(aPosMap.find(thePrs));
456 if (aPosMap.size() == 0)
458 aToDel.push_back(aIt->first);
460 // Reindex objects positions in order to avoid spaces
461 PositionsMap::iterator aIt;
463 for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
468 std::list<ObjectPtr>::const_iterator aListIt;
469 for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
470 myShapes.erase(*aListIt);