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_Edge> anEdge = aShape->edge();
192 std::shared_ptr<GeomAPI_Curve> aCurve =
193 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
195 if (anEdge->isClosed()) {
196 double aPeriod = aCurve->endParam() - aCurve->startParam();
197 Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
198 GeomAPI_ProjectPointOnCurve anExtr(theP, aCurv);
199 double aParam = anExtr.LowerDistanceParameter();
201 aCurv->D1(aParam, aP, aVec);
202 // 2458: check correct orientation of the vector
203 if (aVec.SquareMagnitude() > Precision::Confusion()) {
204 std::shared_ptr<GeomAPI_Edge> aCircEdge(new GeomAPI_Edge(aShape));
205 double aFirstParam, aLastParam;
206 aCircEdge->getRange(aFirstParam, aLastParam);
207 // if parameter is near the LastParam, make the vector go inside (reverse)
208 double aDelta = aLastParam - aParam;
209 while (aDelta < -Precision::Confusion())
211 while (aDelta > aPeriod + Precision::Confusion())
213 if (fabs(aDelta) < Precision::Confusion())
217 GeomPointPtr aPnt1 = aCurve->getPoint(aCurve->endParam());
218 GeomPointPtr aPnt2 = aCurve->getPoint(aCurve->startParam());
220 gp_Pnt aPn2 = aPnt2->impl<gp_Pnt>();
221 if (aPn2.IsEqual(theP, Precision::Confusion()))
222 aVec = gp_Vec(aPn2, aPnt1->impl<gp_Pnt>());
224 aVec = gp_Vec(aPnt1->impl<gp_Pnt>(), aPn2);
227 aVec = gp_Vec(theDir->impl<gp_Dir>());
232 //*****************************************************************
233 gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
234 const SketcherPrs_SymbolPrs* thePrs,
235 double theStep, GeomPointPtr thePnt)
237 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
238 gp_Pnt aP; // Central point
241 return getPointPosition(theShape, thePrs, theStep, thePnt);
243 if (aShape->isEdge()) {
244 std::shared_ptr<GeomAPI_Curve> aCurve =
245 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
246 // this is a circle or arc
247 double aMidParam = (aCurve->startParam() + aCurve->endParam()) / 2.;
248 std::shared_ptr<GeomAPI_Pnt> aPnt = aCurve->getPoint(aMidParam);
249 aP = aPnt->impl<gp_Pnt>();
252 std::shared_ptr<GeomAPI_Vertex> aVertex =
253 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
254 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
255 aP = aPnt->impl<gp_Pnt>();
259 gp_Vec aVec1 = getVector(theShape, thePrs->plane()->dirX(), aP);
261 // Compute shifting vector for a one symbol
262 gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
263 if (aShift.SquareMagnitude() > Precision::SquareConfusion())
265 aShift.Multiply(theStep * 0.8);
267 // Shift the position coordinate according to position index
268 int aPos = getPositionIndex(theShape, thePrs);
270 if ((aPos % 2) == 0) {
272 aP.Translate(aShift);
277 aM = -(aPos + 2) / 4;
281 aP.Translate(-aShift);
283 if ((aPos - 1) % 4 == 0)
286 aM = -(aPos + 1) / 4;
290 // Normalize vector along the line
292 aVec1.Multiply(theStep);
293 aP.Translate(aVec1.Multiplied(aM));
299 //*****************************************************************
300 //! Returns curves connected to the given point
301 std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_SymbolPrs* thePrs)
303 std::list<ObjectPtr> aList;
304 GeomAx3Ptr aAx3 = thePrs->plane();
305 ModelAPI_CompositeFeature* aOwner = thePrs->sketcher();
306 GeomPnt2dPtr aPnt2d = thePnt->to2D(aAx3->origin(), aAx3->dirX(), aAx3->dirY());
308 int aNbSubs = aOwner->numberOfSubs();
309 for (int i = 0; i < aNbSubs; i++) {
310 FeaturePtr aFeature = aOwner->subFeature(i);
311 if (!aFeature->firstResult().get() || aFeature->firstResult()->isDisabled())
312 continue; // There is no result
314 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
315 AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
316 aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
317 AttributePoint2DPtr aSPnt2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
318 aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
320 GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
321 GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
323 if (aPnt1->distance(aPnt2d) <= Precision::Confusion() ||
324 aPnt2->distance(aPnt2d) <= Precision::Confusion()) {
325 // a point corresponds to one of the line end
326 GeomShapePtr aShp = SketcherPrs_Tools::getShape(aFeature->firstResult());
327 GeomCurvePtr aCurv = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
328 aList.push_back(aFeature->firstResult());
330 // Check that a point belongs to the curve
331 GeomAPI_Lin2d aLin2d(aPnt1, aPnt2);
332 double aDist = aLin2d.distance(aPnt2d);
333 if (aDist <= Precision::Confusion())
334 aList.push_back(aFeature->firstResult());
339 std::list<ResultPtr> aResults = aFeature->results();
340 std::list<ResultPtr>::const_iterator aIt;
341 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
342 GeomShapePtr aShp = SketcherPrs_Tools::getShape((*aIt));
343 if (aShp->isEdge()) {
345 aCurve = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
350 GeomPointPtr aProjPnt;
351 if (aFeature->getKind() == SketchPlugin_Circle::ID() ||
352 aFeature->getKind() == SketchPlugin_Arc::ID()) {
353 GeomCirclePtr aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
354 aProjPnt = aCircle->project(thePnt);
356 else if (aFeature->getKind() == SketchPlugin_Ellipse::ID()) {
357 GeomEllipsePtr anEllipse = GeomEllipsePtr(new GeomAPI_Ellipse(aCurve));
358 aProjPnt = anEllipse->project(thePnt);
361 aProjPnt = aCurve->project(thePnt);
362 if (aProjPnt && thePnt->distance(aProjPnt) <= Precision::Confusion())
363 aList.push_back(aResObj);
370 //*****************************************************************
371 gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
372 ObjectPtr theLine, const SketcherPrs_SymbolPrs* thePrs,
373 double theStep, GeomPointPtr thePnt)
375 gp_Pnt aP = thePnt->impl<gp_Pnt>();
376 if (!thePrs->plane().get())
378 GeomDirPtr aNormal = thePrs->plane()->normal();
379 gp_Dir aNormDir = aNormal->impl<gp_Dir>();
381 std::list<ObjectPtr> aCurves = getCurves(thePnt, thePrs);
382 std::list<ObjectPtr>::const_iterator aItCurv;
383 std::list<gp_Vec> aVectorsList;
384 // Calculate all vectors
385 for (aItCurv = aCurves.cbegin(); aItCurv != aCurves.cend(); aItCurv++) {
386 aVectorsList.push_back(getVector((*aItCurv), thePrs->plane()->dirX(), aP));
389 // Position of the symbol
390 const std::array<int, 2>& aPos = getPositionIndex(thePnt, thePrs);
392 // Angle size of a symbol
393 //double aAngleStep = PI * 50./180.;
394 double aAngleStep = PI/4.;
396 std::list<gp_Vec>::const_iterator aItVec;
397 std::list<double> aAngles;
398 std::list<gp_Vec> aVectors;
399 // Select closest vectors and calculate angles between base vector and closest vector
400 for (aItVec = aVectorsList.cbegin(); aItVec != aVectorsList.cend(); aItVec++) {
401 std::list<gp_Vec>::const_iterator aIt;
403 gp_Vec aVec = *aItVec;
404 for (aIt = aVectorsList.cbegin(); aIt != aVectorsList.cend(); aIt++) {
405 double aAng = aVec.AngleWithRef(*aIt, aNormDir);
408 aAng = 2 * PI + aAng;
412 else if (aAng < aMinAng) {
417 if (aMinAng >= aAngleStep) {
418 aVectors.push_back(aVec);
419 aAngles.push_back(aMinAng);
423 gp_Ax1 aRotAx(aP, aNormDir);
425 // If number of angle less then number of symbols then each symbol can be placed
426 // directly inside of the angle
427 if (aAngles.size() >= aPos[1] && !aVectors.empty()) {
429 aVecPos = *(std::next(aVectors.begin(), aId));
431 gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep);
433 aShift.Multiply(theStep * 1.5);
434 return aP.Translated(aShift);
437 // A case when there are a lot of symbols
440 std::list<double>::const_iterator aItAng;
443 bool aHasPlace = false;
444 //int aIntId = 0; // a position inside a one sector
445 while (aPosCount < aPos[1]) {
446 for (aItAng = aAngles.cbegin(), aItVec = aVectors.cbegin();
447 aItAng != aAngles.cend(); ++aItAng, ++aItVec) {
449 int Nb = int(aAng / aAngleStep);
452 if ((!aHasPlace) && (aPosCount >= (aPos[0] + 1))) {
456 //aIntId = aPos[0] - (aPosCount - Nb);
459 if (aPosCount < aPos[1]) {
469 // rotate base vector on a necessary angle
470 gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep + aAngleStep * aPos[0]);
472 aShift.Multiply(theStep * 1.5);
473 return aP.Translated(aShift);
478 //*****************************************************************
479 void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* thePrs)
481 std::map<ObjectPtr, PositionsMap>::iterator aIt;
482 std::list<ObjectPtr> aToDel;
483 // Clear map for deleted presentation
484 for (aIt = myShapes.begin(); aIt != myShapes.end(); ++aIt) {
485 PositionsMap& aPosMap = aIt->second;
486 if (aPosMap.count(thePrs) > 0) {
488 aPosMap.erase(aPosMap.find(thePrs));
489 if (aPosMap.size() == 0)
491 aToDel.push_back(aIt->first);
493 // Reindex objects positions in order to avoid spaces
494 PositionsMap::iterator aIt;
496 for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
501 std::list<ObjectPtr>::const_iterator aListIt;
502 for (aListIt = aToDel.cbegin(); aListIt != aToDel.cend(); ++aListIt) {
503 myShapes.erase(*aListIt);