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