Salome HOME
Issue #1393. An attempt to make infinite lines and use the edges' direction vectors.
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Middle.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Middle.cpp
4 // Created:     29 February 2016
5 // Author:      Natalia ERMOLAEVA
6
7 #include "SketcherPrs_Middle.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_PositionMgr.h"
10
11 #include <SketchPlugin_Constraint.h>
12
13 #include "Events_Error.h"
14
15 #include <Graphic3d_AspectLine3d.hxx>
16 #include <Prs3d_Root.hxx>
17
18
19 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Middle, SketcherPrs_SymbolPrs);
20 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Middle, SketcherPrs_SymbolPrs);
21
22 static Handle(Image_AlienPixMap) MyPixMap;
23
24 SketcherPrs_Middle::SketcherPrs_Middle(ModelAPI_Feature* theConstraint, 
25                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane) 
26  : SketcherPrs_SymbolPrs(theConstraint, thePlane)
27 {
28   myPntArray = new Graphic3d_ArrayOfPoints(2);
29   myPntArray->AddVertex(0., 0., 0.);
30 }  
31
32 bool SketcherPrs_Middle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
33                                          const std::shared_ptr<GeomAPI_Ax3>&/* thePlane*/)
34 {
35   bool aReadyToDisplay = false;
36
37   ObjectPtr aObj1 = SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_A());
38   ObjectPtr aObj2 = SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_B());
39
40   // one object is a feature Line, other object is a point result. We check shape of point result
41   aReadyToDisplay = aObj1.get() && aObj2.get() &&
42                     (SketcherPrs_Tools::getShape(aObj1).get() != NULL ||
43                      SketcherPrs_Tools::getShape(aObj2).get() != NULL);
44
45   return aReadyToDisplay;
46 }
47
48 bool SketcherPrs_Middle::updatePoints(double theStep) const
49 {
50   if (!IsReadyToDisplay(myConstraint, myPlane))
51     return false;
52   ObjectPtr aPointObject;
53
54   // find a line result to set middle symbol near it
55   AttributePtr anAttribute = SketcherPrs_Tools::getAttribute(myConstraint, SketchPlugin_Constraint::ENTITY_A());
56   if (!anAttribute.get()) {
57     ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
58     std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
59     if (aShape.get() && aShape->isEdge())
60       aPointObject = aObj;
61   }
62   if (!aPointObject.get()) {
63     ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_B());
64     std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
65     if (aShape.get() && aShape->isEdge())
66       aPointObject = aObj;
67   }
68
69   if (!aPointObject.get())
70     return false;
71
72   // Set points of the presentation
73   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
74   gp_Pnt aP1 = aMgr->getPosition(aPointObject, this, theStep);
75   myPntArray->SetVertice(1, aP1);
76   return true;
77 }
78
79 void SketcherPrs_Middle::drawLines(const Handle(Prs3d_Presentation)& thePrs, Quantity_Color theColor) const
80 {
81   Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePrs);
82
83   Handle(Graphic3d_AspectLine3d) aLineAspect = new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID, 2);
84   aGroup->SetPrimitivesAspect(aLineAspect);
85
86   // Draw objects
87   ObjectPtr aObject = SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
88   drawLine(thePrs, theColor, aObject);
89
90   aObject = SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
91   drawLine(thePrs, theColor, aObject);
92 }
93
94 void SketcherPrs_Middle::drawLine(const Handle(Prs3d_Presentation)& thePrs,
95                                   Quantity_Color theColor, const ObjectPtr& theObject) const
96 {
97   FeaturePtr aLineFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
98   if (aLineFeature.get()) {
99     std::list<ResultPtr> aResults = aLineFeature->results();
100     if (aResults.size() == 1) {
101       ResultPtr aResult = aResults.front();
102       std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aResult);
103       if (aLine.get() != NULL)
104         drawShape(aLine, thePrs);
105     }
106   }
107   else {
108     std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(theObject);
109     if (aLine.get() != NULL)
110       drawShape(aLine, thePrs);
111   }
112 }
113