Salome HOME
Clean Property panel before starting of a new operation.
[modules/shaper.git] / src / PartSet / PartSet_Presentation.cpp
1 // File:        PartSet_Presentation.h
2 // Created:     02 June 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_Presentation.h>
6 #include <PartSet_Tools.h>
7
8 #include <ModelAPI_Feature.h>
9 #include <ModelAPI_Data.h>
10
11 #include <GeomDataAPI_Point.h>
12 #include <GeomDataAPI_Point2D.h>
13 #include <GeomDataAPI_Dir.h>
14
15 #include <SketchPlugin_Sketch.h>
16 #include <SketchPlugin_Line.h>
17 #include <SketchPlugin_Constraint.h>
18 #include <SketchPlugin_ConstraintLength.h>
19
20 #include <AIS_InteractiveObject.hxx>
21 #include <AIS_LengthDimension.hxx>
22 #include <AIS_Shape.hxx>
23
24 #include <gp_Pln.hxx>
25 #include <gp_Pnt.hxx>
26
27 const Quantity_NameOfColor SKETCH_PLANE_COLOR = Quantity_NOC_CHOCOLATE; /// the plane edge color
28 const int SKETCH_WIDTH = 4; /// the plane edge width
29
30 Handle(AIS_InteractiveObject) PartSet_Presentation::createPresentation(
31                                          FeaturePtr theFeature,
32                                          FeaturePtr theSketch,
33                                          const TopoDS_Shape& theShape,
34                                          Handle_AIS_InteractiveObject thePrevPrs)
35 {
36   Handle(AIS_InteractiveObject) anAIS;
37
38   if (theFeature->getKind() == SKETCH_CONSTRAINT_LENGTH_KIND)
39     anAIS = createSketchConstraintLength(theFeature, theSketch, thePrevPrs);
40   else {
41     anAIS = createFeature(theFeature, theShape, thePrevPrs);
42     if (theFeature->getKind() == SKETCH_KIND)
43     {
44       Handle(AIS_Shape) aShapeAIS = Handle(AIS_Shape)::DownCast(anAIS);
45       aShapeAIS->SetColor(Quantity_Color(SKETCH_PLANE_COLOR));
46       aShapeAIS->SetWidth(SKETCH_WIDTH);
47       aShapeAIS->Redisplay();
48     }
49   }
50
51   return anAIS;
52 }
53
54 Handle(AIS_InteractiveObject) PartSet_Presentation::createFeature(
55                                               FeaturePtr theFeature,
56                                               const TopoDS_Shape& theShape,
57                                               Handle_AIS_InteractiveObject thePrevPrs)
58 {
59   Handle(AIS_InteractiveObject) anAIS = thePrevPrs;
60   if (!anAIS.IsNull())
61   {
62     Handle(AIS_Shape) aShapeAIS = Handle(AIS_Shape)::DownCast(anAIS);
63     if (!aShapeAIS.IsNull()) {
64       // if the AIS object is displayed in the opened local context in some mode, additional
65       // AIS sub objects are created there. They should be rebuild for correct selecting.
66       // It is possible to correct it by closing local context before the shape set and opening
67       // after. Another workaround to thrown down the selection and reselecting the AIS.
68       // If there was a problem here, try the first solution with close/open local context.
69       aShapeAIS->Set(theShape);
70       aShapeAIS->Redisplay(Standard_True);
71     }
72   }
73   else
74   {
75     anAIS = new AIS_Shape(theShape);
76   }
77   return anAIS;
78 }
79
80 Handle(AIS_InteractiveObject) PartSet_Presentation::createSketchConstraintLength(
81                                          FeaturePtr theFeature,
82                                          FeaturePtr theSketch,
83                                          Handle(AIS_InteractiveObject) thePrevPrs)
84 {
85   if (!theFeature || !theSketch)
86     return thePrevPrs;
87
88   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
89   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
90     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
91   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
92     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
93   gp_Pln aPlane(aNormal->x(), aNormal->y(), aNormal->z(), 0/*D*/);
94
95   aData = theFeature->data();
96   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
97           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
98   if (!anAttr)
99     return thePrevPrs;
100   FeaturePtr aFeature = anAttr->feature();
101   if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
102     return thePrevPrs;
103
104   aData = aFeature->data();
105   if (!aData->isValid())
106     return thePrevPrs;
107
108   boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
109         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
110   boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
111         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
112
113   gp_Pnt aPoint1, aPoint2;
114   PartSet_Tools::convertTo3D(aPointStart->x(), aPointStart->y(), theSketch, aPoint1);
115   PartSet_Tools::convertTo3D(aPointEnd->x(), aPointEnd->y(), theSketch, aPoint2);
116
117   //Build dimension here
118   gp_Pnt aP1 = aPoint1;
119   gp_Pnt aP2 = aPoint2;
120
121   Handle(AIS_InteractiveObject) anAIS = thePrevPrs;
122   if (anAIS.IsNull())
123   {
124     Handle(AIS_LengthDimension) aLenDim = new AIS_LengthDimension (aP1, aP2, aPlane);
125
126     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
127     anAspect->MakeArrows3d (Standard_False);
128     anAspect->MakeText3d(false/*is text 3d*/);
129     anAspect->TextAspect()->SetHeight(28);
130     anAspect->MakeTextShaded(false/*is test shaded*/);
131     aLenDim->DimensionAspect()->MakeUnitsDisplayed(false/*is units displayed*/);
132     /*if (isUnitsDisplayed)
133     {
134       aLenDim->SetDisplayUnits (aDimDlg->GetUnits ());
135     }*/
136     aLenDim->SetDimensionAspect (anAspect);
137     aLenDim->SetFlyout(12);
138
139     anAIS = aLenDim;
140   }
141   else {
142     // update presentation
143     Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
144     if (!aDimAIS.IsNull()) {
145       aDimAIS->SetMeasuredGeometry(aPoint1, aPoint2, aPlane);
146       aDimAIS->Redisplay(Standard_True);
147     }
148   }
149   return anAIS;
150 }