Salome HOME
#1404 Random crash with Shaper: correction for coincidence constraint: correction...
[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 }
29
30 bool SketcherPrs_Middle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
31                                          const std::shared_ptr<GeomAPI_Ax3>&/* thePlane*/)
32 {
33   bool aReadyToDisplay = false;
34
35   ObjectPtr aObj1 = SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_A());
36   ObjectPtr aObj2 = SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_B());
37
38   // one object is a feature Line, other object is a point result. We check shape of point result
39   aReadyToDisplay = aObj1.get() && aObj2.get() &&
40                     (SketcherPrs_Tools::getShape(aObj1).get() != NULL ||
41                      SketcherPrs_Tools::getShape(aObj2).get() != NULL);
42
43   return aReadyToDisplay;
44 }
45
46 bool SketcherPrs_Middle::updateIfReadyToDisplay(double theStep) const
47 {
48   if (!IsReadyToDisplay(myConstraint, myPlane))
49     return false;
50   ObjectPtr aPointObject;
51
52   // find a line result to set middle symbol near it
53   AttributePtr anAttribute = SketcherPrs_Tools::getAttribute(myConstraint, SketchPlugin_Constraint::ENTITY_A());
54   if (!anAttribute.get()) {
55     ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
56     std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
57     if (aShape.get() && aShape->isEdge())
58       aPointObject = aObj;
59   }
60   if (!aPointObject.get()) {
61     ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_B());
62     std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
63     if (aShape.get() && aShape->isEdge())
64       aPointObject = aObj;
65   }
66
67   if (!aPointObject.get())
68     return false;
69
70   // Set points of the presentation
71   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
72   gp_Pnt aP1 = aMgr->getPosition(aPointObject, this, theStep);
73   myPntArray->SetVertice(1, aP1);
74   return true;
75 }
76
77 void SketcherPrs_Middle::drawLines(const Handle(Prs3d_Presentation)& thePrs, Quantity_Color theColor) const
78 {
79   Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePrs);
80
81   Handle(Graphic3d_AspectLine3d) aLineAspect = new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID, 2);
82   aGroup->SetPrimitivesAspect(aLineAspect);
83
84   // Draw objects
85   ObjectPtr aObject = SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
86   drawLine(thePrs, theColor, aObject);
87
88   aObject = SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
89   drawLine(thePrs, theColor, aObject);
90 }
91
92 void SketcherPrs_Middle::drawLine(const Handle(Prs3d_Presentation)& thePrs,
93                                   Quantity_Color theColor, const ObjectPtr& theObject) const
94 {
95   FeaturePtr aLineFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
96   if (aLineFeature.get()) {
97     std::list<ResultPtr> aResults = aLineFeature->results();
98     if (aResults.size() == 1) {
99       ResultPtr aResult = aResults.front();
100       std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aResult);
101       if (aLine.get() != NULL)
102         drawShape(aLine, thePrs);
103     }
104   }
105   else {
106     std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(theObject);
107     if (aLine.get() != NULL)
108       drawShape(aLine, thePrs);
109   }
110 }
111