Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Transformation.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_Transformation.h"
22 #include "SketcherPrs_Tools.h"
23 #include "SketcherPrs_PositionMgr.h"
24
25 #include <SketchPlugin_Constraint.h>
26 #include <SketchPlugin_MultiTranslation.h>
27 #include <SketchPlugin_MultiRotation.h>
28 #include <ModelAPI_AttributeRefList.h>
29 #include <GeomDataAPI_Point2D.h>
30
31 #include <Graphic3d_AspectLine3d.hxx>
32 #include <Prs3d_Root.hxx>
33 #include <Prs3d_LineAspect.hxx>
34 #include <Geom_CartesianPoint.hxx>
35 #include <gp_Pnt.hxx>
36 #include <StdPrs_Point.hxx>
37
38
39
40 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Transformation, SketcherPrs_SymbolPrs);
41
42 static Handle(Image_AlienPixMap) MyPixMap;
43
44 SketcherPrs_Transformation::SketcherPrs_Transformation(ModelAPI_Feature* theConstraint,
45                                            const std::shared_ptr<GeomAPI_Ax3>& thePlane,
46                                            bool isTranslation)
47  : SketcherPrs_SymbolPrs(theConstraint, thePlane), myIsTranslation(isTranslation)
48 {
49 }
50
51 bool SketcherPrs_Transformation::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
52                                                   const std::shared_ptr<GeomAPI_Ax3>&/* thePlane*/)
53 {
54   bool aReadyToDisplay = false;
55
56   std::shared_ptr<ModelAPI_Data> aData = theConstraint->data();
57   // Get transformated objects list
58   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB =
59     aData->reflist(SketchPlugin_Constraint::ENTITY_B());
60   if (anAttrB.get() == NULL)
61     return aReadyToDisplay;
62
63   int aNbB = anAttrB->size();
64   aReadyToDisplay = aNbB > 0;
65
66 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
67   // additional check
68   if (theConstraint->getKind() == SketchPlugin_MultiTranslation::ID()) {
69     // If it is translation
70     AttributePoint2DPtr aStart = GeomDataAPI_Point2D::getPoint2D(aData,
71                                             SketchPlugin_MultiTranslation::START_POINT_ID());
72     AttributePoint2DPtr aEnd = GeomDataAPI_Point2D::getPoint2D(aData,
73                                             SketchPlugin_MultiTranslation::END_POINT_ID());
74
75     aReadyToDisplay =
76       aStart.get() && aEnd.get() && aStart->isInitialized() && aEnd->isInitialized();
77   }
78   else if (theConstraint->getKind() == SketchPlugin_MultiRotation::ID()) {
79     // if it is rotation
80     AttributePoint2DPtr aCenter =
81       GeomDataAPI_Point2D::getPoint2D(aData, SketchPlugin_MultiRotation::CENTER_ID());
82     aReadyToDisplay = aCenter.get() && aCenter->isInitialized();
83   }
84 #endif
85   return aReadyToDisplay;
86 }
87
88 bool SketcherPrs_Transformation::updateIfReadyToDisplay(double theStep, bool withColor) const
89 {
90   if (!IsReadyToDisplay(myConstraint, myPlane))
91     return false;
92
93   std::shared_ptr<ModelAPI_Data> aData = myConstraint->data();
94   // Get transformated objects list
95   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB =
96     aData->reflist(SketchPlugin_Constraint::ENTITY_B());
97
98   int aNbB = anAttrB->size();
99   if (aNbB == 0)
100   {
101 #ifdef DEBUG_SENSITIVE_TO_BE_CORRECTED
102   //if (!myPntArray.IsNull())
103     //  mySPoints.Clear();
104 #endif
105   }
106
107   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
108   myPntArray = new Graphic3d_ArrayOfPoints(aNbB, withColor);
109
110   int i;
111   ObjectPtr aObj;
112   gp_Pnt aP1;
113   // Compute points of symbols
114   for (i = 0; i < aNbB; i++) {
115     aObj = anAttrB->object(i);
116     if (SketcherPrs_Tools::getShape(aObj).get() == NULL)
117       continue;
118     aP1 = aMgr->getPosition(aObj, this, theStep);
119     myPntArray->SetVertice(i + 1, aP1);
120   }
121
122   return true;
123 }
124
125 void SketcherPrs_Transformation::drawLines(const Handle(Prs3d_Presentation)& thePrs,
126                                            Quantity_Color theColor) const
127 {
128   std::shared_ptr<ModelAPI_Data> aData = myConstraint->data();
129   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB =
130     aData->reflist(SketchPlugin_Constraint::ENTITY_B());
131   if (anAttrB.get() == NULL)
132     return;
133
134   // drawListOfShapes uses myDrawer for attributes definition
135   //Handle(Prs3d_LineAspect) aLnAspect = new Prs3d_LineAspect(theColor, Aspect_TOL_SOLID, 1);
136   //myDrawer->SetLineAspect(aLnAspect);
137
138   drawListOfShapes(anAttrB, thePrs, theColor);
139   if (myConstraint->getKind() == SketchPlugin_MultiTranslation::ID()) {
140     // If it is translation
141     AttributePoint2DPtr aStart = GeomDataAPI_Point2D::getPoint2D(aData,
142                                             SketchPlugin_MultiTranslation::START_POINT_ID());
143     AttributePoint2DPtr aEnd = GeomDataAPI_Point2D::getPoint2D(aData,
144                                             SketchPlugin_MultiTranslation::END_POINT_ID());
145
146     if (aStart.get() && aEnd.get() && aStart->isInitialized() && aEnd->isInitialized()) {
147       // Add start point
148       std::shared_ptr<GeomAPI_Pnt> aPnt = myPlane->to3D(aStart->x(), aStart->y());
149       Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
150       StdPrs_Point::Add(thePrs, aPoint, myDrawer);
151
152       // Add end point
153       aPnt = myPlane->to3D(aEnd->x(), aEnd->y());
154       aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
155       StdPrs_Point::Add(thePrs, aPoint, myDrawer);
156     }
157   } else if (myConstraint->getKind() == SketchPlugin_MultiRotation::ID()) {
158     // if it is rotation
159     AttributePoint2DPtr aCenter =
160       GeomDataAPI_Point2D::getPoint2D(aData, SketchPlugin_MultiRotation::CENTER_ID());
161     if (aCenter.get() && aCenter->isInitialized()) {
162       // Show center of rotation
163       std::shared_ptr<GeomAPI_Pnt> aPnt = myPlane->to3D(aCenter->x(), aCenter->y());
164       Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
165       StdPrs_Point::Add(thePrs, aPoint, myDrawer);
166     }
167   }
168 }
169