Salome HOME
af78d67e810e4454db868f2fd4f1e1ac59ec3b56
[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 <Graphic3d_AspectLine3d.hxx>
14 #include <Prs3d_Root.hxx>
15
16
17 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Middle, SketcherPrs_SymbolPrs);
18
19 static Handle(Image_AlienPixMap) MyPixMap;
20
21 SketcherPrs_Middle::SketcherPrs_Middle(ModelAPI_Feature* theConstraint,
22                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
23  : SketcherPrs_SymbolPrs(theConstraint, thePlane)
24 {
25 }
26
27 bool SketcherPrs_Middle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
28                                          const std::shared_ptr<GeomAPI_Ax3>&/* thePlane*/)
29 {
30   bool aReadyToDisplay = false;
31
32   ObjectPtr aObj1 =
33     SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_A());
34   ObjectPtr aObj2 =
35     SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_B());
36
37   // one object is a feature Line, other object is a point result. We check shape of point result
38   aReadyToDisplay = aObj1.get() && aObj2.get() &&
39                     (SketcherPrs_Tools::getShape(aObj1).get() != NULL ||
40                      SketcherPrs_Tools::getShape(aObj2).get() != NULL);
41
42   return aReadyToDisplay;
43 }
44
45 bool SketcherPrs_Middle::updateIfReadyToDisplay(double theStep) const
46 {
47   if (!IsReadyToDisplay(myConstraint, myPlane))
48     return false;
49   ObjectPtr aPointObject;
50
51   // find a line result to set middle symbol near it
52   AttributePtr anAttribute =
53     SketcherPrs_Tools::getAttribute(myConstraint, SketchPlugin_Constraint::ENTITY_A());
54   if (!anAttribute.get()) {
55     ObjectPtr aObj =
56       SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
57     std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
58     if (aShape.get() && aShape->isEdge())
59       aPointObject = aObj;
60   }
61   if (!aPointObject.get()) {
62     ObjectPtr aObj =
63       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,
80                                    Quantity_Color theColor) const
81 {
82   Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePrs);
83
84   Handle(Graphic3d_AspectLine3d) aLineAspect =
85     new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID, 2);
86   aGroup->SetPrimitivesAspect(aLineAspect);
87
88   // Draw objects
89   ObjectPtr aObject =
90     SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
91   drawLine(thePrs, theColor, aObject);
92
93   aObject = SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
94   drawLine(thePrs, theColor, aObject);
95 }
96
97 void SketcherPrs_Middle::drawLine(const Handle(Prs3d_Presentation)& thePrs,
98                                   Quantity_Color theColor, const ObjectPtr& theObject) const
99 {
100   FeaturePtr aLineFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
101   if (aLineFeature.get()) {
102     std::list<ResultPtr> aResults = aLineFeature->results();
103     if (aResults.size() == 1) {
104       ResultPtr aResult = aResults.front();
105       std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aResult);
106       if (aLine.get() != NULL)
107         drawShape(aLine, thePrs);
108     }
109   }
110   else {
111     std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(theObject);
112     if (aLine.get() != NULL)
113       drawShape(aLine, thePrs);
114   }
115 }
116