Salome HOME
#1188 - crash when delete segmnet used in rotation/translation
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Transformation.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Transformation.cpp
4 // Created:     16 February 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_Transformation.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_PositionMgr.h"
10
11 #include <SketchPlugin_Constraint.h>
12 #include <SketchPlugin_MultiTranslation.h>
13 #include <SketchPlugin_MultiRotation.h>
14 #include <ModelAPI_AttributeRefList.h>
15 #include <GeomDataAPI_Point2D.h>
16
17 #include <Graphic3d_AspectLine3d.hxx>
18 #include <Prs3d_Root.hxx>
19 #include <Prs3d_LineAspect.hxx>
20 #include <Geom_CartesianPoint.hxx>
21 #include <gp_Pnt.hxx>
22 #include <StdPrs_Point.hxx>
23
24
25
26 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Transformation, SketcherPrs_SymbolPrs);
27 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Transformation, SketcherPrs_SymbolPrs);
28
29 static Handle(Image_AlienPixMap) MyPixMap;
30
31 SketcherPrs_Transformation::SketcherPrs_Transformation(ModelAPI_Feature* theConstraint, 
32                                            const std::shared_ptr<GeomAPI_Ax3>& thePlane,
33                                            bool isTranslation) 
34  : SketcherPrs_SymbolPrs(theConstraint, thePlane), myIsTranslation(isTranslation)
35 {
36 }  
37
38 bool SketcherPrs_Transformation::updatePoints(double theStep) const 
39 {
40   std::shared_ptr<ModelAPI_Data> aData = myConstraint->data();
41   // Get transformated objects list
42   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB = aData->reflist(SketchPlugin_Constraint::ENTITY_B());
43   if (anAttrB.get() == NULL)
44     return false;
45
46   int aNbB = anAttrB->size();
47   if (aNbB == 0)
48   {
49 #ifdef DEBUG_SENSITIVE_TO_BE_CORRECTED
50   //if (!myPntArray.IsNull())
51     //  mySPoints.Clear();
52 #endif
53     return false;
54   }
55
56   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
57   myPntArray = new Graphic3d_ArrayOfPoints(aNbB);
58
59   int i;
60   ObjectPtr aObj;
61   gp_Pnt aP1;
62   // Compute points of symbols
63   for (i = 0; i < aNbB; i++) {
64     aObj = anAttrB->object(i);
65     if (SketcherPrs_Tools::getShape(aObj).get() == NULL)
66       continue;
67     aP1 = aMgr->getPosition(aObj, this, theStep);
68     myPntArray->SetVertice(i + 1, aP1);
69   }  
70
71   return true;
72 }
73
74 void SketcherPrs_Transformation::drawLines(const Handle(Prs3d_Presentation)& thePrs, Quantity_Color theColor) const
75 {
76   std::shared_ptr<ModelAPI_Data> aData = myConstraint->data();
77   std::shared_ptr<ModelAPI_AttributeRefList> anAttrB = aData->reflist(SketchPlugin_Constraint::ENTITY_B());
78   if (anAttrB.get() == NULL)
79     return;
80
81   Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePrs);
82
83   //Handle(Graphic3d_AspectLine3d) aLineAspect = new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID, 2);
84   //aGroup->SetPrimitivesAspect(aLineAspect);
85
86   // drawListOfShapes uses myDrawer for attributes definition
87   Handle(Prs3d_LineAspect) aLnAspect = new Prs3d_LineAspect(theColor, Aspect_TOL_SOLID, 1);
88   myDrawer->SetLineAspect(aLnAspect);
89
90   drawListOfShapes(anAttrB, thePrs);
91   if (myConstraint->getKind() == SketchPlugin_MultiTranslation::ID()) {
92     // If it is translation
93     AttributePoint2DPtr aStart = GeomDataAPI_Point2D::getPoint2D(aData,
94                                             SketchPlugin_MultiTranslation::START_POINT_ID());
95     AttributePoint2DPtr aEnd = GeomDataAPI_Point2D::getPoint2D(aData,
96                                             SketchPlugin_MultiTranslation::END_POINT_ID());
97
98     if (aStart.get() && aEnd.get() && aStart->isInitialized() && aEnd->isInitialized()) {
99       // Add start point
100       std::shared_ptr<GeomAPI_Pnt> aPnt = myPlane->to3D(aStart->x(), aStart->y());
101       Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
102       StdPrs_Point::Add(thePrs, aPoint, myDrawer);
103
104       // Add end point
105       aPnt = myPlane->to3D(aEnd->x(), aEnd->y());
106       aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
107       StdPrs_Point::Add(thePrs, aPoint, myDrawer);
108     }
109   } else if (myConstraint->getKind() == SketchPlugin_MultiRotation::ID()) {
110     // if it is rotation
111     AttributePoint2DPtr aCenter = GeomDataAPI_Point2D::getPoint2D(aData, SketchPlugin_MultiRotation::CENTER_ID());
112     if (aCenter.get() && aCenter->isInitialized()) {
113       // Show center of rotation
114       std::shared_ptr<GeomAPI_Pnt> aPnt = myPlane->to3D(aCenter->x(), aCenter->y());
115       Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
116       StdPrs_Point::Add(thePrs, aPoint, myDrawer);
117     }
118   }
119 }
120