]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_SketchDrawer.cpp
Salome HOME
fd4deeddbf7f747d381759b64ca8d5c6b038d6be
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_SketchDrawer.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 "SketchPlugin_SketchDrawer.h"
22
23 #include "SketchPlugin_Arc.h"
24 #include "SketchPlugin_Line.h"
25 #include "SketchPlugin_Circle.h"
26 #include "SketchPlugin_Point.h"
27 #include "SketchPlugin_Sketch.h"
28 #include "SketchPlugin_ConstraintDistance.h"
29 #include "SketchPlugin_ConstraintCoincidence.h"
30 #include "SketchPlugin_ConstraintLength.h"
31 #include "SketchPlugin_ConstraintRadius.h"
32 #include "SketchPlugin_ConstraintVertical.h"
33 #include "SketchPlugin_ConstraintHorizontal.h"
34 #include "SketchPlugin_ConstraintDistanceVertical.h"
35 #include "SketchPlugin_ConstraintDistanceHorizontal.h"
36 #include "SketchPlugin_Tools.h"
37
38 #include <GeomAPI_Face.h>
39 #include <GeomAPI_Edge.h>
40 #include <GeomAPI_Circ.h>
41 #include <GeomAPI_ShapeExplorer.h>
42 #include <GeomAPI_DataMapOfShapeShape.h>
43
44 #include <ModelAPI_AttributeSelection.h>
45 #include <ModelAPI_AttributeDouble.h>
46 #include <ModelAPI_AttributeBoolean.h>
47 #include <ModelAPI_ResultConstruction.h>
48
49 #include <cmath>
50
51 static const double kTOL = 1.e-6;
52
53 SketchPlugin_SketchDrawer::SketchPlugin_SketchDrawer() : ModelAPI_Feature()
54 {}
55
56 void SketchPlugin_SketchDrawer::initAttributes()
57 {
58   data()->addAttribute(BASE_ID(), ModelAPI_AttributeSelection::typeId());
59   data()->addAttribute(PLANE_ID(), ModelAPI_AttributeSelection::typeId());
60   data()->addAttribute(ADD_DIMENSIONS_ID(), ModelAPI_AttributeBoolean::typeId());
61 }
62
63 // sets a point attribute of the feature by 3D point on the sketch
64 static void setPoint(FeaturePtr theFeature, const std::string& theAttrID,
65   std::shared_ptr<SketchPlugin_Sketch> theSketch, GeomPointPtr thePoint,
66   std::list<std::pair<GeomPnt2dPtr, std::string> >& aPoints)
67 {
68   GeomPnt2dPtr aPnt2D = theSketch->to2D(thePoint);
69   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
70     theFeature->attribute(theAttrID))->setValue(aPnt2D);
71   aPoints.push_back(std::pair<GeomPnt2dPtr, std::string>(aPnt2D, theAttrID));
72 }
73
74 void SketchPlugin_SketchDrawer::execute()
75 {
76   GeomShapePtr aBase = selection(BASE_ID())->value();
77   if (!aBase.get() && selection(BASE_ID())->context().get())
78     aBase = selection(BASE_ID())->context()->shape();
79   if (!aBase.get()) {
80     setError("Error: a base shape can not be obtained");
81     return; // invalid case
82   }
83
84   ObjectPtr aPlaneContext = selection(PLANE_ID())->contextObject();
85   GeomShapePtr aPlaneShape = selection(PLANE_ID())->value();
86   if (!aPlaneShape.get() && aPlaneContext.get())
87     aPlaneShape = selection(PLANE_ID())->context()->shape();
88   if (!aPlaneShape.get() || aPlaneShape->shapeType() != GeomAPI_Shape::FACE) {
89     setError("Error: a sketch plane can not be obtained");
90     return; // invalid case
91   }
92   GeomFacePtr aPlaneFace(new GeomAPI_Face(aPlaneShape));
93   GeomPlanePtr aPlane = aPlaneFace->getPlane();
94
95   // create and initialize sketch
96   DocumentPtr aMyDoc = document();
97   std::shared_ptr<SketchPlugin_Sketch> aSketch =
98     std::dynamic_pointer_cast<SketchPlugin_Sketch>(aMyDoc->addFeature(SketchPlugin_Sketch::ID()));
99   // by selection of plane
100   aSketch->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->
101     setValue(selection(PLANE_ID())->context(), aPlaneShape);
102   // remove reference, but keep the sketch position
103   aSketch->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->
104     setValue(ResultPtr(), GeomShapePtr());
105
106   bool addDimensions = boolean(ADD_DIMENSIONS_ID())->value();
107   // iterate all edges of the base to find all edges that belong to this plane
108   GeomAPI_DataMapOfShapeShape alreadyProcessed;
109   std::list<AttributePoint2DPtr> aCreatedPoints;// points to check and set coincidence
110   for(GeomAPI_ShapeExplorer anEdges(aBase, GeomAPI_Shape::EDGE); anEdges.more(); anEdges.next()) {
111     if (!alreadyProcessed.bind(anEdges.current(), anEdges.current()))
112       continue; // skip duplicated edges
113     GeomEdgePtr anEdge(new GeomAPI_Edge(anEdges.current()));
114     if (anEdge->isDegenerated())
115       continue; // skip degenerated edges
116     GeomPointPtr aStart = anEdge->firstPoint();
117     GeomPointPtr anEnd = anEdge->lastPoint();
118     if (aPlane->distance(aStart) >= kTOL || aPlane->distance(anEnd) >= kTOL)
119       continue; // skip edges not in plane
120
121     FeaturePtr anItem;
122     std::list<std::pair<GeomPnt2dPtr, std::string> > aPoints; // created point to attribute ID
123     if (anEdge->isLine()) { // line is already in the plane: create by two points
124       anItem = aSketch->addFeature(SketchPlugin_Line::ID());
125       setPoint(anItem, SketchPlugin_Line::START_ID(), aSketch, aStart, aPoints);
126       setPoint(anItem, SketchPlugin_Line::END_ID(), aSketch, anEnd, aPoints);
127       anItem->execute(); // for constraints setting on result
128       // add a vertical or horizontal constraints
129       bool isHorVertConstr = true;
130       if (fabs(aPoints.front().first->x() - aPoints.back().first->x()) < kTOL) {
131         FeaturePtr aVert = aSketch->addFeature(SketchPlugin_ConstraintVertical::ID());
132         aVert->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(anItem->firstResult());
133       } else if (fabs(aPoints.front().first->y() - aPoints.back().first->y()) < kTOL) {
134         FeaturePtr aHor = aSketch->addFeature(SketchPlugin_ConstraintHorizontal::ID());
135         aHor->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(anItem->firstResult());
136       } else {
137         isHorVertConstr = false;
138       }
139       if (addDimensions) {
140         if (isHorVertConstr) { // only length constraint is enough
141           FeaturePtr aLen = aSketch->addFeature(SketchPlugin_ConstraintLength::ID());
142           aLen->refattr(SketchPlugin_ConstraintLength::ENTITY_A())
143             ->setObject(anItem->firstResult());
144           aLen->real(SketchPlugin_ConstraintLength::VALUE())->setValue(anEdge->length());
145         } else { // set horizontal and vertical distance constraints
146           FeaturePtr aVDist = aSketch->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
147           aVDist->refattr(SketchPlugin_Constraint::ENTITY_A())
148             ->setAttr(anItem->attribute(SketchPlugin_Line::START_ID()));
149           aVDist->refattr(SketchPlugin_Constraint::ENTITY_B())
150             ->setAttr(anItem->attribute(SketchPlugin_Line::END_ID()));
151           aVDist->real(SketchPlugin_ConstraintDistanceVertical::VALUE())
152             ->setValue(aPoints.back().first->y() - aPoints.front().first->y());
153           FeaturePtr aHDist = aSketch->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
154           aHDist->refattr(SketchPlugin_Constraint::ENTITY_A())
155             ->setAttr(anItem->attribute(SketchPlugin_Line::START_ID()));
156           aHDist->refattr(SketchPlugin_Constraint::ENTITY_B())
157             ->setAttr(anItem->attribute(SketchPlugin_Line::END_ID()));
158           aHDist->real(SketchPlugin_ConstraintDistanceVertical::VALUE())
159             ->setValue(aPoints.back().first->x() - aPoints.front().first->x());
160         }
161       }
162     } else if (anEdge->isArc()) { // check also center
163       GeomPointPtr aCenter = anEdge->circle()->center();
164       if (aPlane->distance(aCenter) >= kTOL)
165         continue;
166       // create arc by 3 points
167       anItem = aSketch->addFeature(SketchPlugin_Arc::ID());
168       setPoint(anItem, SketchPlugin_Arc::CENTER_ID(), aSketch, aCenter, aPoints);
169       setPoint(anItem, SketchPlugin_Arc::START_ID(), aSketch, aStart, aPoints);
170       setPoint(anItem, SketchPlugin_Arc::END_ID(), aSketch, anEnd, aPoints);
171       anItem->execute(); // for constraints setting on result
172       // set radius constraint
173       if (addDimensions) {
174         FeaturePtr aRad = aSketch->addFeature(SketchPlugin_ConstraintRadius::ID());
175         aRad->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(anItem->lastResult());
176       }
177     } else if (anEdge->isCircle()) { // check also center and middle (at value 2.)
178       GeomPointPtr aCenter = anEdge->circle()->center();
179       if (aPlane->distance(aCenter) >= kTOL || aPlane->distance(anEdge->middlePoint()) >= kTOL)
180         continue;
181       // circle by center and radius
182       anItem = aSketch->addFeature(SketchPlugin_Circle::ID());
183       setPoint(anItem, SketchPlugin_Circle::CENTER_ID(), aSketch, aCenter, aPoints);
184       anItem->real(SketchPlugin_Circle::RADIUS_ID())->setValue(anEdge->circle()->radius());
185       anItem->execute(); // for constraints setting on result
186       // set radius constraint
187       if (addDimensions) {
188         FeaturePtr aRad = aSketch->addFeature(SketchPlugin_ConstraintRadius::ID());
189         aRad->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(anItem->lastResult());
190       }
191     } else {
192       continue; // other types of edges are not supported, only lines, circles and arcs
193     }
194     // check some resulting points are coincident to existing
195     std::list<std::pair<GeomPnt2dPtr, std::string> >::iterator aPIter = aPoints.begin();
196     for(; aPIter != aPoints.end(); aPIter++) {
197       AttributePoint2DPtr aPointAttr =
198         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anItem->attribute(aPIter->second));
199       std::list<AttributePoint2DPtr>::iterator aCoincIter = aCreatedPoints.begin();
200       for(; aCoincIter != aCreatedPoints.end(); aCoincIter++) {
201         double aDX = (*aCoincIter)->x() - aPIter->first->x();
202         if (fabs(aDX) >= kTOL)
203           continue;
204         double aDY = (*aCoincIter)->y() - aPIter->first->y();
205         if (fabs(aDY) >= kTOL)
206           continue;
207         // create a coincidence constraint
208         FeaturePtr aCoinc = aSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
209         aCoinc->refattr(SketchPlugin_Constraint::ENTITY_A())->setAttr(aPointAttr);
210         aCoinc->refattr(SketchPlugin_Constraint::ENTITY_B())->setAttr(*aCoincIter);
211         break; // only one coincidence per point
212       }
213       aCreatedPoints.push_back(aPointAttr);
214     }
215   }
216   aMyDoc->setCurrentFeature(aSketch, false);
217 }