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