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