Salome HOME
Issue #1393. An attempt to make infinite lines and use the edges' direction vectors.
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Rigid.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Rigid.cpp
4 // Created:     16 February 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_Rigid.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_PositionMgr.h"
10
11 #include <GeomAPI_Pnt.h>
12 #include <GeomAPI_Edge.h>
13 #include <GeomAPI_Curve.h>
14 #include <GeomAPI_Vertex.h>
15
16 #include <ModelAPI_AttributeRefAttr.h>
17
18 #include <SketchPlugin_Constraint.h>
19
20 #include <gp_Pnt2d.hxx>
21
22 #include <Graphic3d_AspectLine3d.hxx>
23 #include <Prs3d_Root.hxx>
24
25 #include <GeomAdaptor_Curve.hxx>
26 #include <BRep_Tool.hxx>
27 #include <StdPrs_DeflectionCurve.hxx>
28 #include <StdPrs_Point.hxx>
29 #include <Geom_CartesianPoint.hxx>
30
31
32
33 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Rigid, SketcherPrs_SymbolPrs);
34 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Rigid, SketcherPrs_SymbolPrs);
35
36 static Handle(Image_AlienPixMap) MyPixMap; 
37
38
39
40 SketcherPrs_Rigid::SketcherPrs_Rigid(ModelAPI_Feature* theConstraint, 
41                                            const std::shared_ptr<GeomAPI_Ax3>& thePlane) 
42  : SketcherPrs_SymbolPrs(theConstraint, thePlane)
43 {
44   // Set default points array
45   myPntArray = new Graphic3d_ArrayOfPoints(1);
46   myPntArray->AddVertex(0., 0., 0.);
47 }  
48
49 bool SketcherPrs_Rigid::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
50                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
51 {
52   bool aReadyToDisplay = false;
53
54   std::shared_ptr<ModelAPI_Data> aData = theConstraint->data();
55   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
56   AttributePtr aRefAttr = anAttr->attr();
57   if (anAttr->isObject()) {
58     // The constraint attached to an object
59     ObjectPtr aObj = anAttr->object();
60     aReadyToDisplay =  SketcherPrs_Tools::getShape(aObj).get() != NULL;
61
62   } else {
63     // The constraint attached to a point
64     std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theConstraint,
65                                                                  SketchPlugin_Constraint::ENTITY_A());
66     aReadyToDisplay = aPnt.get() != NULL;
67   }
68   return aReadyToDisplay;
69 }
70
71 bool SketcherPrs_Rigid::updatePoints(double theStep) const 
72 {
73   if (!IsReadyToDisplay(myConstraint, myPlane))
74     return false;
75
76   std::shared_ptr<ModelAPI_Data> aData = myConstraint->data();
77   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
78   AttributePtr aRefAttr = anAttr->attr();
79   if (anAttr->isObject()) {
80     // The constraint attached to an object
81     ObjectPtr aObj = anAttr->object();
82
83     SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
84     gp_Pnt aP1 = aMgr->getPosition(aObj, this, theStep);
85     myPntArray->SetVertice(1, aP1);
86   } else {
87     // The constraint attached to a point
88     std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(myConstraint,
89                                                   SketchPlugin_Constraint::ENTITY_A());
90     std::shared_ptr<GeomAPI_Pnt> aPoint = myPlane->to3D(aPnt->x(), aPnt->y() + theStep);
91     myPntArray->SetVertice(1, aPoint->impl<gp_Pnt>());
92   }
93   return true;
94 }
95
96
97 void SketcherPrs_Rigid::drawLines(const Handle(Prs3d_Presentation)& thePrs, Quantity_Color theColor) const
98 {
99   ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
100   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
101   std::shared_ptr<GeomAPI_Shape> aShape;
102   if (aFeature.get()) {
103     // If constraint attached to a feature
104     const std::list<ResultPtr>& aResults = aFeature->results();
105     std::list<ResultPtr>::const_iterator aIt;
106     // Find a shape
107     for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
108       aShape = (*aIt)->shape();
109       if (aShape->isEdge())
110         break;
111     }
112   } else {
113     // Else it is a result
114     aShape = SketcherPrs_Tools::getShape(aObj);
115   }
116   if (aShape.get() == NULL)
117     return;
118
119   Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePrs);
120   Handle(Graphic3d_AspectLine3d) aLineAspect = new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID, 2);
121   aGroup->SetPrimitivesAspect(aLineAspect);
122
123   Handle(Prs3d_PointAspect) aPntAspect = new Prs3d_PointAspect(Aspect_TOM_PLUS, theColor, 1);
124   myDrawer->SetPointAspect(aPntAspect);
125   drawShape(aShape, thePrs);
126 }
127