1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "SketcherPrs_PositionMgr.h"
21 #include "SketcherPrs_Tools.h"
23 #include <GeomAPI_Ax3.h>
24 #include <GeomAPI_Circ.h>
25 #include <GeomAPI_Curve.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Ellipse.h>
28 #include <GeomAPI_Dir.h>
29 #include <GeomAPI_Lin2d.h>
30 #include <GeomAPI_Vertex.h>
32 #include <GeomDataAPI_Point2D.h>
34 #include <SketchPlugin_Arc.h>
35 #include <SketchPlugin_Circle.h>
36 #include <SketchPlugin_Ellipse.h>
37 #include <SketchPlugin_Line.h>
38 #include <SketchPlugin_ConstraintPerpendicular.h>
39 #include <SketchPlugin_ConstraintTangent.h>
41 #include <TopoDS_Vertex.hxx>
42 #include <Geom_Curve.hxx>
43 #include <GeomAPI_ProjectPointOnCurve.hxx>
44 #include <TColGeom_SequenceOfCurve.hxx>
49 static SketcherPrs_PositionMgr* MyPosMgr = NULL;
51 #define PI 3.1415926535897932
53 // The class is implemented as a singlton
54 SketcherPrs_PositionMgr* SketcherPrs_PositionMgr::get()
57 MyPosMgr = new SketcherPrs_PositionMgr();
61 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 return ((theName == SketchPlugin_ConstraintTangent::ID()) ||
93 (theName == SketchPlugin_ConstraintPerpendicular::ID()));
96 bool containsPoint(const FeaturePtr& theFeature, GeomPnt2dPtr thePnt2d, GeomPointPtr thePos)
98 if (theFeature->getKind() == SketchPlugin_Line::ID()) {
99 AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
100 theFeature->data()->attribute(SketchPlugin_Line::START_ID()));
101 AttributePoint2DPtr aSPnt2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
102 theFeature->data()->attribute(SketchPlugin_Line::END_ID()));
104 GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
105 GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
107 if (aPnt1->isEqual(thePnt2d) || aPnt2->isEqual(thePnt2d))
109 } else if ((theFeature->getKind() == SketchPlugin_Circle::ID()) ||
110 (theFeature->getKind() == SketchPlugin_Arc::ID())) {
113 std::list<ResultPtr> aResults = theFeature->results();
114 std::list<ResultPtr>::const_iterator aIt;
115 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
116 GeomShapePtr aShp = SketcherPrs_Tools::getShape((*aIt));
117 if (aShp->isEdge()) {
119 aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
124 double aStart = aCurve->startParam();
125 double aEnd = aCurve->endParam();
126 GeomCirclePtr aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
128 if (aCircle->parameter(thePos, 1.e-4, aParam) && (aParam >= aStart) && (aParam <= aEnd))
135 const std::array<int, 2>& SketcherPrs_PositionMgr::getPositionIndex(GeomPointPtr thePos,
136 const SketcherPrs_SymbolPrs* thePrs)
138 if (myPntShapes.count(thePrs->feature()) == 0) {
139 // Renumerate positions around the specified constraint point for all constraints
140 GeomAx3Ptr aAx3 = thePrs->plane();
141 ModelAPI_CompositeFeature* aOwner = thePrs->sketcher();
142 GeomPnt2dPtr aPnt2d = thePos->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
144 int aNbSubs = aOwner->numberOfSubs();
146 std::list<const ModelAPI_Feature*> aFeaList;
147 for (int i = 0; i < aNbSubs; i++) {
148 FeaturePtr aFeature = aOwner->subFeature(i);
150 bool aUseFeature = ((myPntShapes.count(aFeature.get()) == 1) ||
151 (isPntConstraint(aFeature->getKind())));
153 DataPtr aData = aFeature->data();
154 AttributeRefAttrPtr aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
157 aObj = ModelAPI_Feature::feature(aObjRef->object());
158 bool aContains = false;
159 if (aObj && containsPoint(aObj, aPnt2d, thePos)) {
162 aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
164 aObj = ModelAPI_Feature::feature(aObjRef->object());
165 if (aObj && containsPoint(aObj, aPnt2d, thePos)) {
170 myPntShapes[aFeature.get()][0] = aId;
172 aFeaList.push_back(aFeature.get());
176 int aSize = (int) aFeaList.size();
177 std::list<const ModelAPI_Feature*>::const_iterator aIt;
178 for (aIt = aFeaList.cbegin(); aIt != aFeaList.cend(); aIt++) {
179 myPntShapes[*aIt][1] = aSize;
182 return myPntShapes[thePrs->feature()];
185 //*****************************************************************
186 gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
189 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
190 if (aShape->isEdge()) {
191 std::shared_ptr<GeomAPI_Curve> aCurve =
192 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
194 if (aCurve->isCircle() || aCurve->isEllipse()) {
195 Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
196 GeomAPI_ProjectPointOnCurve anExtr(theP, aCurv);
197 double aParam = anExtr.LowerDistanceParameter();
199 aCurv->D1(aParam, aP, aVec);
200 // 2458: check correct orientation of the vector
201 if (aVec.SquareMagnitude() > Precision::Confusion()) {
202 std::shared_ptr<GeomAPI_Edge> aCircEdge(new GeomAPI_Edge(aShape));
203 double aFirstParam, aLastParam;
204 aCircEdge->getRange(aFirstParam, aLastParam);
205 // if parameter is near the LastParam, make the vector go inside (reverse)
206 double aDelta = aLastParam - aParam;
207 while (aDelta < -Precision::Confusion())
209 while (aDelta > 2. * M_PI - Precision::Confusion())
211 if (fabs(aDelta) < Precision::Confusion())
215 GeomPointPtr aPnt1 = aCurve->getPoint(aCurve->endParam());
216 GeomPointPtr aPnt2 = aCurve->getPoint(aCurve->startParam());
218 gp_Pnt aPn2 = aPnt2->impl<gp_Pnt>();
219 if (aPn2.IsEqual(theP, Precision::Confusion()))
220 aVec = gp_Vec(aPn2, aPnt1->impl<gp_Pnt>());
222 aVec = gp_Vec(aPnt1->impl<gp_Pnt>(), aPn2);
225 aVec = gp_Vec(theDir->impl<gp_Dir>());
230 //*****************************************************************
231 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
232 const SketcherPrs_SymbolPrs* thePrs,
233 double theStep, GeomPointPtr thePnt)
235 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
236 gp_Pnt aP; // Central point
239 return getPointPosition(theShape, thePrs, theStep, thePnt);
241 if (aShape->isEdge()) {
242 std::shared_ptr<GeomAPI_Curve> aCurve =
243 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
244 // this is a circle or arc
245 double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
246 std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
247 aP = aPnt->impl<gp_Pnt>();
250 std::shared_ptr<GeomAPI_Vertex> aVertex =
251 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
252 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
253 aP = aPnt->impl<gp_Pnt>();
257 gp_Vec aVec1 = getVector(theShape, thePrs->plane()->dirX(), aP);
259 // Compute shifting vector for a one symbol
260 gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
262 aShift.Multiply(theStep * 0.8);
264 // Shift the position coordinate according to position index
265 int aPos = getPositionIndex(theShape, thePrs);
267 if ((aPos % 2) == 0) {
269 aP.Translate(aShift);
274 aM = -(aPos + 2) / 4;
278 aP.Translate(-aShift);
280 if ((aPos - 1) % 4 == 0)
283 aM = -(aPos + 1) / 4;
287 // Normalize vector along the line
289 aVec1.Multiply(theStep);
290 aP.Translate(aVec1.Multiplied(aM));
296 //*****************************************************************
297 //! Returns curves connected to the given point
298 std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_SymbolPrs* thePrs)
300 std::list<ObjectPtr> aList;
301 GeomAx3Ptr aAx3 = thePrs->plane();
302 ModelAPI_CompositeFeature* aOwner = thePrs->sketcher();
303 GeomPnt2dPtr aPnt2d = thePnt->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
305 int aNbSubs = aOwner->numberOfSubs();
306 for (int i = 0; i < aNbSubs; i++) {
307 FeaturePtr aFeature = aOwner->subFeature(i);
308 if (!aFeature->firstResult().get() || aFeature->firstResult()->isDisabled())
309 continue; // There is no result
311 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
312 AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
313 aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
314 AttributePoint2DPtr aSPnt2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
315 aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
317 GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
318 GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
320 if (aPnt1->distance(aPnt2d) <= Precision::Confusion() ||
321 aPnt2->distance(aPnt2d) <= Precision::Confusion()) {
322 // a point corresponds to one of the line end
323 GeomShapePtr aShp = SketcherPrs_Tools::getShape(aFeature->firstResult());
324 GeomCurvePtr aCurv = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
325 aList.push_back(aFeature->firstResult());
327 // Check that a point belongs to the curve
328 GeomAPI_Lin2d aLin2d(aPnt1, aPnt2);
329 double aDist = aLin2d.distance(aPnt2d);
330 if (aDist <= Precision::Confusion())
331 aList.push_back(aFeature->firstResult());
336 std::list<ResultPtr> aResults = aFeature->results();
337 std::list<ResultPtr>::const_iterator aIt;
338 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
339 GeomShapePtr aShp = SketcherPrs_Tools::getShape((*aIt));
340 if (aShp->isEdge()) {
342 aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
347 GeomPointPtr aProjPnt;
348 if (aFeature->getKind() == SketchPlugin_Circle::ID() ||
349 aFeature->getKind() == SketchPlugin_Arc::ID()) {
350 GeomCirclePtr aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
351 aProjPnt = aCircle->project(thePnt);
353 else if (aFeature->getKind() == SketchPlugin_Ellipse::ID()) {
354 GeomEllipsePtr anEllipse = GeomEllipsePtr(new GeomAPI_Ellipse(aCurve));
355 aProjPnt = anEllipse->project(thePnt);
357 if (aProjPnt && thePnt->distance(aProjPnt) <= Precision::Confusion())
358 aList.push_back(aResObj);
365 //*****************************************************************
366 gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
367 ObjectPtr theLine, const SketcherPrs_SymbolPrs* thePrs,
368 double theStep, GeomPointPtr thePnt)
370 gp_Pnt aP = thePnt->impl<gp_Pnt>();
371 if (!thePrs->plane().get())
373 GeomDirPtr aNormal = thePrs->plane()->normal();
374 gp_Dir aNormDir = aNormal->impl<gp_Dir>();
376 std::list<ObjectPtr> aCurves = getCurves(thePnt, thePrs);
377 std::list<ObjectPtr>::const_iterator aItCurv;
378 std::list<gp_Vec> aVectorsList;
379 // Calculate all vectors
380 for (aItCurv = aCurves.cbegin(); aItCurv != aCurves.cend(); aItCurv++) {
381 aVectorsList.push_back(getVector((*aItCurv), thePrs->plane()->dirX(), aP));
384 // Position of the symbol
385 const std::array<int, 2>& aPos = getPositionIndex(thePnt, thePrs);
387 // Angle size of a symbol
388 //double aAngleStep = PI * 50./180.;
389 double aAngleStep = PI/4.;
391 std::list<gp_Vec>::const_iterator aItVec;
392 std::list<double> aAngles;
393 std::list<gp_Vec> aVectors;
394 // Select closest vectors and calculate angles between base vector and closest vector
395 for (aItVec = aVectorsList.cbegin(); aItVec != aVectorsList.cend(); aItVec++) {
396 std::list<gp_Vec>::const_iterator aIt;
398 gp_Vec aVec = *aItVec;
399 for (aIt = aVectorsList.cbegin(); aIt != aVectorsList.cend(); aIt++) {
400 double aAng = aVec.AngleWithRef(*aIt, aNormDir);
403 aAng = 2 * PI + aAng;
407 else if (aAng < aMinAng) {
412 if (aMinAng >= aAngleStep) {
413 aVectors.push_back(aVec);
414 aAngles.push_back(aMinAng);
418 gp_Ax1 aRotAx(aP, aNormDir);
420 // If number of angle less then number of symbols then each symbol can be placed
421 // directly inside of the angle
422 if (aAngles.size() >= aPos[1] && !aVectors.empty()) {
424 aVecPos = *(std::next(aVectors.begin(), aId));
426 gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep);
428 aShift.Multiply(theStep * 1.5);
429 return aP.Translated(aShift);
432 // A case when there are a lot of symbols
435 std::list<double>::const_iterator aItAng;
438 bool aHasPlace = false;
439 //int aIntId = 0; // a position inside a one sector
440 while (aPosCount < aPos[1]) {
441 for (aItAng = aAngles.cbegin(), aItVec = aVectors.cbegin();
442 aItAng != aAngles.cend(); ++aItAng, ++aItVec) {
444 int Nb = int(aAng / aAngleStep);
447 if ((!aHasPlace) && (aPosCount >= (aPos[0] + 1))) {
451 //aIntId = aPos[0] - (aPosCount - Nb);
454 if (aPosCount < aPos[1]) {
464 // rotate base vector on a necessary angle
465 gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep + aAngleStep * aPos[0]);
467 aShift.Multiply(theStep * 1.5);
468 return aP.Translated(aShift);
473 //*****************************************************************
474 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
476 std::map<ObjectPtr, PositionsMap>::iterator aIt;
477 std::list<ObjectPtr> aToDel;
478 // Clear map for deleted presentation
479 for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
480 PositionsMap& aPosMap = aIt->second;
481 if (aPosMap.count(thePrs) > 0) {
483 aPosMap.erase(aPosMap.find(thePrs));
484 if (aPosMap.size() == 0)
486 aToDel.push_back(aIt->first);
488 // Reindex objects positions in order to avoid spaces
489 PositionsMap::iterator aIt;
491 for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
496 std::list<ObjectPtr>::const_iterator aListIt;
497 for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
498 myShapes.erase(*aListIt);