Salome HOME
Issue #1834: Fix length of lines
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Mirror.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Mirror.cpp
4 // Created:     6 April 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_Mirror.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 #include <Prs3d_LineAspect.hxx>
16
17
18
19 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Mirror, SketcherPrs_SymbolPrs);
20 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Mirror, SketcherPrs_SymbolPrs);
21
22 static Handle(Image_AlienPixMap) MyPixMap;
23
24 SketcherPrs_Mirror::SketcherPrs_Mirror(ModelAPI_Feature* theConstraint, 
25                                            const std::shared_ptr<GeomAPI_Ax3>& thePlane) 
26  : SketcherPrs_SymbolPrs(theConstraint, thePlane)
27 {
28 }  
29
30 bool SketcherPrs_Mirror::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
31                                           const std::shared_ptr<GeomAPI_Ax3>&/* thePlane*/)
32 {
33   bool aReadyToDisplay = false;
34
35   // Get axis of mirror
36   ObjectPtr aAxisObj = 
37     SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_A());
38   if (SketcherPrs_Tools::getShape(aAxisObj).get() == NULL)
39     return aReadyToDisplay;
40
41   std::shared_ptr<ModelAPI_Data> aData = theConstraint->data();
42   // Get source objects
43   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB = 
44     aData->reflist(SketchPlugin_Constraint::ENTITY_B());
45   if (anAttrB.get() == NULL)
46     return aReadyToDisplay;
47   // Get mirrored objects
48   std::shared_ptr<ModelAPI_AttributeRefList> anAttrC = 
49     aData->reflist(SketchPlugin_Constraint::ENTITY_C());
50   if (anAttrC.get() == NULL)
51     return aReadyToDisplay;
52
53   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
54   int aNb = anAttrB->size();
55   // If size of source objects and mirrored ones is not equal then the constraint is not computed
56   if (aNb != anAttrC->size())
57     return aReadyToDisplay;
58
59   aReadyToDisplay = true;
60   return aReadyToDisplay;
61 }
62
63 bool SketcherPrs_Mirror::updateIfReadyToDisplay(double theStep) const
64 {
65   if (!IsReadyToDisplay(myConstraint, myPlane))
66     return false;
67
68   // Get axis of mirror
69   ObjectPtr aAxisObj = 
70     SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
71
72   std::shared_ptr<ModelAPI_Data> aData = myConstraint->data();
73   // Get source objects
74   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB = 
75     aData->reflist(SketchPlugin_Constraint::ENTITY_B());
76   // Get mirrored objects
77   std::shared_ptr<ModelAPI_AttributeRefList> anAttrC = 
78     aData->reflist(SketchPlugin_Constraint::ENTITY_C());
79
80   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
81   int aNb = anAttrB->size();
82
83   myPntArray = new Graphic3d_ArrayOfPoints(2 * aNb);
84   int i;
85   ObjectPtr aObj;
86   gp_Pnt aP1;
87   // get position for each source object
88   for (i = 0; i < aNb; i++) {
89     aObj = anAttrB->object(i);
90     if (SketcherPrs_Tools::getShape(aObj).get() == NULL)
91       continue;
92     aP1 = aMgr->getPosition(aObj, this, theStep);
93     myPntArray->SetVertice(i + 1, aP1);
94   }  
95   // Get position of each mirrored object
96   for (i = 0; i < aNb; i++) {
97     aObj = anAttrC->object(i);
98     if (SketcherPrs_Tools::getShape(aObj).get() == NULL)
99       continue;
100     aP1 = aMgr->getPosition(aObj, this, theStep);
101     myPntArray->SetVertice(aNb + i + 1, aP1);
102   }  
103   return true;
104 }
105
106
107 void SketcherPrs_Mirror::drawLines(const Handle(Prs3d_Presentation)& thePrs, 
108   Quantity_Color theColor) const
109 {
110   std::shared_ptr<ModelAPI_Data> aData = myConstraint->data();
111   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB = 
112     aData->reflist(SketchPlugin_Constraint::ENTITY_B());
113   if (anAttrB.get() == NULL)
114     return;
115   std::shared_ptr<ModelAPI_AttributeRefList> anAttrC = 
116     aData->reflist(SketchPlugin_Constraint::ENTITY_C());
117   if (anAttrC.get() == NULL)
118     return;
119
120   int aNb = anAttrB->size();
121   if (aNb != anAttrC->size())
122     return;
123
124   Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePrs);
125
126   // drawListOfShapes uses myDrawer for attributes definition
127   Handle(Prs3d_LineAspect) aLnAspect = new Prs3d_LineAspect(theColor, Aspect_TOL_SOLID, 1);
128   myDrawer->SetLineAspect(aLnAspect);
129
130   // Draw axis line
131   addLine(aGroup, SketchPlugin_Constraint::ENTITY_A());
132
133   // Draw source objects
134   drawListOfShapes(anAttrB, thePrs);
135
136   // draw mirrored objects
137   drawListOfShapes(anAttrC, thePrs);
138 }
139