Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Mirror.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "SketcherPrs_Mirror.h"
21 #include "SketcherPrs_Tools.h"
22 #include "SketcherPrs_PositionMgr.h"
23
24 #include <SketchPlugin_Constraint.h>
25
26 #include <Graphic3d_AspectLine3d.hxx>
27 #include <Prs3d_Root.hxx>
28 #include <Prs3d_LineAspect.hxx>
29
30
31
32 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Mirror, SketcherPrs_SymbolPrs);
33
34 static Handle(Image_AlienPixMap) MyPixMap;
35
36 SketcherPrs_Mirror::SketcherPrs_Mirror(ModelAPI_Feature* theConstraint,
37                                            const std::shared_ptr<GeomAPI_Ax3>& thePlane)
38  : SketcherPrs_SymbolPrs(theConstraint, thePlane)
39 {
40 }
41
42 bool SketcherPrs_Mirror::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
43                                           const std::shared_ptr<GeomAPI_Ax3>&/* thePlane*/)
44 {
45   bool aReadyToDisplay = false;
46
47   // Get axis of mirror
48   ObjectPtr aAxisObj =
49     SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_A());
50   if (SketcherPrs_Tools::getShape(aAxisObj).get() == NULL)
51     return aReadyToDisplay;
52
53   std::shared_ptr<ModelAPI_Data> aData = theConstraint->data();
54   // Get source objects
55   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB =
56     aData->reflist(SketchPlugin_Constraint::ENTITY_B());
57   if (anAttrB.get() == NULL)
58     return aReadyToDisplay;
59   // Get mirrored objects
60   std::shared_ptr<ModelAPI_AttributeRefList> anAttrC =
61     aData->reflist(SketchPlugin_Constraint::ENTITY_C());
62   if (anAttrC.get() == NULL)
63     return aReadyToDisplay;
64
65   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
66   int aNb = anAttrB->size();
67   // If size of source objects and mirrored ones is not equal then the constraint is not computed
68   if (aNb != anAttrC->size())
69     return aReadyToDisplay;
70
71   aReadyToDisplay = true;
72   return aReadyToDisplay;
73 }
74
75 bool SketcherPrs_Mirror::updateIfReadyToDisplay(double theStep, bool withColor) const
76 {
77   if (!IsReadyToDisplay(myConstraint, myPlane))
78     return false;
79
80   // Get axis of mirror
81   ObjectPtr aAxisObj =
82     SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
83
84   std::shared_ptr<ModelAPI_Data> aData = myConstraint->data();
85   // Get source objects
86   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB =
87     aData->reflist(SketchPlugin_Constraint::ENTITY_B());
88   // Get mirrored objects
89   std::shared_ptr<ModelAPI_AttributeRefList> anAttrC =
90     aData->reflist(SketchPlugin_Constraint::ENTITY_C());
91
92   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
93   int aNb = anAttrB->size();
94
95   myPntArray = new Graphic3d_ArrayOfPoints(2 * aNb, withColor);
96   int i;
97   ObjectPtr aObj;
98   gp_Pnt aP1;
99   // get position for each source object
100   for (i = 0; i < aNb; i++) {
101     aObj = anAttrB->object(i);
102     if (SketcherPrs_Tools::getShape(aObj).get() == NULL)
103       continue;
104     aP1 = aMgr->getPosition(aObj, this, theStep);
105     myPntArray->SetVertice(i + 1, aP1);
106   }
107   // Get position of each mirrored object
108   for (i = 0; i < aNb; i++) {
109     aObj = anAttrC->object(i);
110     if (SketcherPrs_Tools::getShape(aObj).get() == NULL)
111       continue;
112     aP1 = aMgr->getPosition(aObj, this, theStep);
113     myPntArray->SetVertice(aNb + i + 1, aP1);
114   }
115   return true;
116 }
117
118
119 void SketcherPrs_Mirror::drawLines(const Handle(Prs3d_Presentation)& thePrs,
120   Quantity_Color theColor) const
121 {
122   std::shared_ptr<ModelAPI_Data> aData = myConstraint->data();
123   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB =
124     aData->reflist(SketchPlugin_Constraint::ENTITY_B());
125   if (anAttrB.get() == NULL)
126     return;
127   std::shared_ptr<ModelAPI_AttributeRefList> anAttrC =
128     aData->reflist(SketchPlugin_Constraint::ENTITY_C());
129   if (anAttrC.get() == NULL)
130     return;
131
132   int aNb = anAttrB->size();
133   if (aNb != anAttrC->size())
134     return;
135
136   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup(thePrs);
137
138   // drawListOfShapes uses myDrawer for attributes definition
139   Handle(Prs3d_LineAspect) aLnAspect = new Prs3d_LineAspect(theColor, Aspect_TOL_SOLID, 1);
140   myDrawer->SetLineAspect(aLnAspect);
141
142   // Draw axis line
143   addLine(aGroup, SketchPlugin_Constraint::ENTITY_A());
144
145   // Draw source objects
146   drawListOfShapes(anAttrB, thePrs, theColor);
147
148   // draw mirrored objects
149   drawListOfShapes(anAttrC, thePrs, theColor);
150 }
151