Salome HOME
Update copyrights
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Rigid.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketcherPrs_Rigid.h"
21 #include "SketcherPrs_Tools.h"
22 #include "SketcherPrs_PositionMgr.h"
23
24 #include <GeomAPI_Pnt.h>
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_Curve.h>
27 #include <GeomAPI_Vertex.h>
28
29 #include <ModelAPI_AttributeRefAttr.h>
30
31 #include <SketchPlugin_Constraint.h>
32
33 #include <gp_Pnt2d.hxx>
34
35 #include <Graphic3d_AspectLine3d.hxx>
36 #include <Prs3d_Root.hxx>
37
38 #include <GeomAdaptor_Curve.hxx>
39 #include <BRep_Tool.hxx>
40 #include <StdPrs_DeflectionCurve.hxx>
41 #include <StdPrs_Point.hxx>
42 #include <Geom_CartesianPoint.hxx>
43
44
45
46 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Rigid, SketcherPrs_SymbolPrs);
47
48 static Handle(Image_AlienPixMap) MyPixMap;
49
50
51
52 SketcherPrs_Rigid::SketcherPrs_Rigid(ModelAPI_Feature* theConstraint,
53   SketchPlugin_Sketch* theSketcher)
54  : SketcherPrs_SymbolPrs(theConstraint, theSketcher)
55 {
56 }
57
58 bool SketcherPrs_Rigid::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
59                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
60 {
61   bool aReadyToDisplay = false;
62
63   std::shared_ptr<ModelAPI_Data> aData = theConstraint->data();
64   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
65     aData->refattr(SketchPlugin_Constraint::ENTITY_A());
66   AttributePtr aRefAttr = anAttr->attr();
67   if (anAttr->isObject()) {
68     // The constraint attached to an object
69     ObjectPtr aObj = anAttr->object();
70     aReadyToDisplay =  SketcherPrs_Tools::getShape(aObj).get() != NULL;
71
72   } else {
73     // The constraint attached to a point
74     std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theConstraint,
75                                                    SketchPlugin_Constraint::ENTITY_A());
76     aReadyToDisplay = aPnt.get() != NULL;
77   }
78   return aReadyToDisplay;
79 }
80
81 bool SketcherPrs_Rigid::updateIfReadyToDisplay(double theStep, bool withColor) const
82 {
83   if (!IsReadyToDisplay(myConstraint, myPlane))
84     return false;
85
86   myPntArray = new Graphic3d_ArrayOfPoints(1, withColor);
87   std::shared_ptr<ModelAPI_Data> aData = myConstraint->data();
88   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
89     aData->refattr(SketchPlugin_Constraint::ENTITY_A());
90   AttributePtr aRefAttr = anAttr->attr();
91   if (anAttr->isObject()) {
92     // The constraint attached to an object
93     ObjectPtr aObj = anAttr->object();
94
95     SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
96     gp_Pnt aP1 = aMgr->getPosition(aObj, this, theStep);
97     myPntArray->AddVertex(aP1);
98   } else {
99     // The constraint attached to a point
100     std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(myConstraint,
101                                                   SketchPlugin_Constraint::ENTITY_A());
102     std::shared_ptr<GeomAPI_Pnt> aPoint = myPlane->to3D(aPnt->x(), aPnt->y() + theStep);
103     myPntArray->AddVertex(aPoint->impl<gp_Pnt>());
104   }
105   return true;
106 }
107
108
109 void SketcherPrs_Rigid::drawLines(const Handle(Prs3d_Presentation)& thePrs,
110                                   Quantity_Color theColor) const
111 {
112   ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
113   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
114   std::shared_ptr<GeomAPI_Shape> aShape;
115   if (aFeature.get()) {
116     // If constraint attached to a feature
117     const std::list<ResultPtr>& aResults = aFeature->results();
118     std::list<ResultPtr>::const_iterator aIt;
119     // Find a shape
120     for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
121       aShape = (*aIt)->shape();
122       if (aShape->isEdge())
123         break;
124     }
125   } else {
126     // Else it is a result
127     aShape = SketcherPrs_Tools::getShape(aObj);
128   }
129   if (aShape.get() == NULL)
130     return;
131
132   Handle(Prs3d_PointAspect) aPntAspect = new Prs3d_PointAspect(Aspect_TOM_PLUS, theColor, 1);
133   myDrawer->SetPointAspect(aPntAspect);
134   drawShape(aShape, thePrs, theColor);
135 }
136