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