Salome HOME
Issue #2208: Development in progress
[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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketcherPrs_Mirror.h"
22 #include "SketcherPrs_Tools.h"
23 #include "SketcherPrs_PositionMgr.h"
24
25 #include <SketchPlugin_Constraint.h>
26
27 #include <Graphic3d_AspectLine3d.hxx>
28 #include <Prs3d_Root.hxx>
29 #include <Prs3d_LineAspect.hxx>
30
31
32
33 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Mirror, SketcherPrs_SymbolPrs);
34
35 static Handle(Image_AlienPixMap) MyPixMap;
36
37 SketcherPrs_Mirror::SketcherPrs_Mirror(ModelAPI_Feature* theConstraint,
38                                       ModelAPI_CompositeFeature* theSketcher,
39                                            const std::shared_ptr<GeomAPI_Ax3>& thePlane)
40  : SketcherPrs_SymbolPrs(theConstraint, theSketcher, thePlane)
41 {
42 }
43
44 bool SketcherPrs_Mirror::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
45                                           const std::shared_ptr<GeomAPI_Ax3>&/* thePlane*/)
46 {
47   bool aReadyToDisplay = false;
48
49   // Get axis of mirror
50   ObjectPtr aAxisObj =
51     SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_A());
52   if (SketcherPrs_Tools::getShape(aAxisObj).get() == NULL)
53     return aReadyToDisplay;
54
55   std::shared_ptr<ModelAPI_Data> aData = theConstraint->data();
56   // Get source objects
57   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB =
58     aData->reflist(SketchPlugin_Constraint::ENTITY_B());
59   if (anAttrB.get() == NULL)
60     return aReadyToDisplay;
61   // Get mirrored objects
62   std::shared_ptr<ModelAPI_AttributeRefList> anAttrC =
63     aData->reflist(SketchPlugin_Constraint::ENTITY_C());
64   if (anAttrC.get() == NULL)
65     return aReadyToDisplay;
66
67   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
68   int aNb = anAttrB->size();
69   // If size of source objects and mirrored ones is not equal then the constraint is not computed
70   if (aNb != anAttrC->size())
71     return aReadyToDisplay;
72
73   aReadyToDisplay = true;
74   return aReadyToDisplay;
75 }
76
77 bool SketcherPrs_Mirror::updateIfReadyToDisplay(double theStep, bool withColor) const
78 {
79   if (!IsReadyToDisplay(myConstraint, myPlane))
80     return false;
81
82   // Get axis of mirror
83   ObjectPtr aAxisObj =
84     SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
85
86   std::shared_ptr<ModelAPI_Data> aData = myConstraint->data();
87   // Get source objects
88   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB =
89     aData->reflist(SketchPlugin_Constraint::ENTITY_B());
90   // Get mirrored objects
91   std::shared_ptr<ModelAPI_AttributeRefList> anAttrC =
92     aData->reflist(SketchPlugin_Constraint::ENTITY_C());
93
94   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
95   int aNb = anAttrB->size();
96
97   myPntArray = new Graphic3d_ArrayOfPoints(2 * aNb, withColor);
98   int i;
99   ObjectPtr aObj;
100   gp_Pnt aP1;
101   // get position for each source object
102   for (i = 0; i < aNb; i++) {
103     aObj = anAttrB->object(i);
104     if (SketcherPrs_Tools::getShape(aObj).get() == NULL)
105       continue;
106     aP1 = aMgr->getPosition(aObj, this, theStep);
107     myPntArray->SetVertice(i + 1, aP1);
108   }
109   // Get position of each mirrored object
110   for (i = 0; i < aNb; i++) {
111     aObj = anAttrC->object(i);
112     if (SketcherPrs_Tools::getShape(aObj).get() == NULL)
113       continue;
114     aP1 = aMgr->getPosition(aObj, this, theStep);
115     myPntArray->SetVertice(aNb + i + 1, aP1);
116   }
117   return true;
118 }
119
120
121 void SketcherPrs_Mirror::drawLines(const Handle(Prs3d_Presentation)& thePrs,
122   Quantity_Color theColor) const
123 {
124   std::shared_ptr<ModelAPI_Data> aData = myConstraint->data();
125   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB =
126     aData->reflist(SketchPlugin_Constraint::ENTITY_B());
127   if (anAttrB.get() == NULL)
128     return;
129   std::shared_ptr<ModelAPI_AttributeRefList> anAttrC =
130     aData->reflist(SketchPlugin_Constraint::ENTITY_C());
131   if (anAttrC.get() == NULL)
132     return;
133
134   int aNb = anAttrB->size();
135   if (aNb != anAttrC->size())
136     return;
137
138   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup(thePrs);
139
140   // drawListOfShapes uses myDrawer for attributes definition
141   Handle(Prs3d_LineAspect) aLnAspect = new Prs3d_LineAspect(theColor, Aspect_TOL_SOLID, 1);
142   myDrawer->SetLineAspect(aLnAspect);
143
144   // Draw axis line
145   addLine(aGroup, SketchPlugin_Constraint::ENTITY_A());
146
147   // Draw source objects
148   drawListOfShapes(anAttrB, thePrs, theColor);
149
150   // draw mirrored objects
151   drawListOfShapes(anAttrC, thePrs, theColor);
152 }
153