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