0026586: Eliminate compile warnings obtained by building occt with vc14: declaration...
[occt.git] / src / XCAFPrs / XCAFPrs_AISObject.cxx
1 // Created on: 2000-08-11
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <XCAFPrs_AISObject.hxx>
17
18 #include <AIS_DisplayMode.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRepBndLib.hxx>
21 #include <gp_Pnt.hxx>
22 #include <Graphic3d_AspectFillArea3d.hxx>
23 #include <Graphic3d_AspectLine3d.hxx>
24 #include <Prs3d_Drawer.hxx>
25 #include <Prs3d_DimensionAspect.hxx>
26 #include <Prs3d_IsoAspect.hxx>
27 #include <Prs3d_LineAspect.hxx>
28 #include <Prs3d_ShadingAspect.hxx>
29 #include <Prs3d_Text.hxx>
30 #include <TDataStd_Name.hxx>
31 #include <TDF_LabelSequence.hxx>
32 #include <TPrsStd_AISPresentation.hxx>
33 #include <TopoDS_Iterator.hxx>
34 #include <XCAFDoc_ShapeTool.hxx>
35 #include <XCAFPrs.hxx>
36 #include <XCAFPrs_DataMapOfShapeStyle.hxx>
37 #include <XCAFPrs_DataMapIteratorOfDataMapOfShapeStyle.hxx>
38 #include <XCAFPrs_Style.hxx>
39
40
41 //=======================================================================
42 //function : XCAFPrs_AISObject
43 //purpose  : 
44 //=======================================================================
45
46 XCAFPrs_AISObject::XCAFPrs_AISObject (const TDF_Label& theLabel)
47 : AIS_ColoredShape(TopoDS_Shape())
48 {
49   myLabel = theLabel;
50 }
51
52 //=======================================================================
53 //function : DisplayText
54 //purpose  : 
55 //=======================================================================
56
57 static void DisplayText (const TDF_Label& aLabel,
58                          const Handle(Prs3d_Presentation)& aPrs,
59                          const Handle(Prs3d_TextAspect)& anAspect,
60                          const TopLoc_Location& aLocation)
61 {
62   // first label itself
63   Handle (TDataStd_Name) aName;
64   if (aLabel.FindAttribute (TDataStd_Name::GetID(), aName)) {
65     TopoDS_Shape aShape;
66     if (XCAFDoc_ShapeTool::GetShape (aLabel, aShape)) {
67       // find the position to display as middle of the bounding box
68       aShape.Move (aLocation);
69       Bnd_Box aBox;
70       BRepBndLib::Add (aShape, aBox);
71       if ( ! aBox.IsVoid() ) 
72       {
73         Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
74         aBox.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
75         gp_Pnt aPnt (0.5 * (aXmin + aXmax), 0.5 * (aYmin + aYmax), 0.5 * (aZmin + aZmax));
76         Prs3d_Text::Draw( aPrs, anAspect, aName->Get(), aPnt);
77       }
78     }
79   }
80
81   TDF_LabelSequence seq;
82   
83   // attibutes of subshapes
84   if (XCAFDoc_ShapeTool::GetSubShapes (aLabel, seq)) {
85     Standard_Integer i = 1;
86     for (i = 1; i <= seq.Length(); i++) {
87       TDF_Label aL = seq.Value (i);
88       DisplayText (aL, aPrs, anAspect, aLocation); //suppose that subshapes do not contain locations
89     }
90   }
91   
92   // attibutes of components
93   seq.Clear();
94   if (XCAFDoc_ShapeTool::GetComponents (aLabel, seq)) {
95     Standard_Integer i = 1;
96     for (i = 1; i <= seq.Length(); i++) {
97       TDF_Label aL = seq.Value (i);
98       DisplayText (aL, aPrs, anAspect, aLocation);
99       TDF_Label aRefLabel;
100       
101       // attributes of referrences
102       TopLoc_Location aLoc = XCAFDoc_ShapeTool::GetLocation (aL);
103       if (XCAFDoc_ShapeTool::GetReferredShape (aL, aRefLabel)) {
104         DisplayText (aRefLabel, aPrs, anAspect, aLoc);
105       }
106     }
107   }
108 }
109
110 //=======================================================================
111 //function : Compute
112 //purpose  : 
113 //=======================================================================
114 void XCAFPrs_AISObject::Compute (const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
115                                  const Handle(Prs3d_Presentation)&           thePrs,
116                                  const Standard_Integer                      theMode)
117 {
118   thePrs->Clear();
119
120   // abv: 06 Mar 00: to have good colors
121   Handle(TPrsStd_AISPresentation) aPrs = Handle(TPrsStd_AISPresentation)::DownCast (GetOwner());
122   if (aPrs.IsNull() || !aPrs->HasOwnMaterial()) SetMaterial (Graphic3d_NOM_PLASTIC);
123
124   TopoDS_Shape aShape;
125   if (!XCAFDoc_ShapeTool::GetShape (myLabel, aShape) || aShape.IsNull()) return;
126
127   // Shape vide -> Assemblage vide.
128   if (aShape.ShapeType() == TopAbs_COMPOUND)
129   {
130     TopoDS_Iterator anExplor (aShape);
131     if (!anExplor.More())
132     {
133       return;
134     }
135   }
136
137   Set (aShape);
138   ClearCustomAspects();
139
140   // Collecting information on colored subshapes
141   TopLoc_Location aLoc;
142   XCAFPrs_DataMapOfShapeStyle aSettings;
143   XCAFPrs::CollectStyleSettings ( myLabel, aLoc, aSettings );
144
145   // Getting default colors
146   XCAFPrs_Style aDefStyle;
147   DefaultStyle (aDefStyle);
148   Quantity_Color aColorCurv = aDefStyle.GetColorCurv();
149   Quantity_Color aColorSurf = aDefStyle.GetColorSurf();
150
151   SetColors (myDrawer, aColorCurv, aColorSurf);
152
153   // collect sub-shapes with the same style into compounds
154   BRep_Builder aBuilder;
155   NCollection_DataMap<XCAFPrs_Style, TopoDS_Compound, XCAFPrs_Style> aStyleGroups;
156   for (XCAFPrs_DataMapIteratorOfDataMapOfShapeStyle aStyledShapeIter (aSettings);
157        aStyledShapeIter.More(); aStyledShapeIter.Next())
158   {
159     TopoDS_Compound aComp;
160     if (aStyleGroups.Find (aStyledShapeIter.Value(), aComp))
161     {
162       aBuilder.Add (aComp, aStyledShapeIter.Key());
163       continue;
164     }
165
166     aBuilder.MakeCompound (aComp);
167     aBuilder.Add (aComp, aStyledShapeIter.Key());
168     aStyleGroups.Bind (aStyledShapeIter.Value(), aComp);
169   }
170   aSettings.Clear();
171
172   // assign custom aspects
173   for (NCollection_DataMap<XCAFPrs_Style, TopoDS_Compound, XCAFPrs_Style>::Iterator aStyleGroupIter (aStyleGroups);
174        aStyleGroupIter.More(); aStyleGroupIter.Next())
175   {
176     const TopoDS_Compound& aComp = aStyleGroupIter.Value();
177     TopoDS_Iterator aShapeIter (aComp);
178     TopoDS_Shape aShapeCur = aShapeIter.Value();
179     aShapeIter.Next();
180     if (aShapeIter.More())
181     {
182       aShapeCur = aComp;
183     }
184
185     Handle(AIS_ColoredDrawer) aDrawer = CustomAspects (aShapeCur);
186     const XCAFPrs_Style& aStyle = aStyleGroupIter.Key();
187     aDrawer->SetHidden (!aStyle.IsVisible());
188
189     aColorCurv = aStyle.IsSetColorCurv() ? aStyle.GetColorCurv() : aDefStyle.GetColorCurv();
190     aColorSurf = aStyle.IsSetColorSurf() ? aStyle.GetColorSurf() : aDefStyle.GetColorSurf();
191
192     SetColors (aDrawer, aColorCurv, aColorSurf);
193   }
194   aStyleGroups.Clear();
195
196   AIS_ColoredShape::Compute (thePresentationManager, thePrs, theMode);
197
198   if (XCAFPrs::GetViewNameMode())
199   {
200     // Displaying Name attributes
201     thePrs->SetDisplayPriority (10);
202     DisplayText (myLabel, thePrs, Attributes()->DimensionAspect()->TextAspect(), TopLoc_Location());//no location
203   }
204 }
205
206 //=======================================================================
207 //function : SetColors
208 //purpose  :
209 //=======================================================================
210 void XCAFPrs_AISObject::SetColors (const Handle(Prs3d_Drawer)& theDrawer,
211                                    const Quantity_Color&       theColorCurv,
212                                    const Quantity_Color&       theColorSurf)
213 {
214   if (!theDrawer->HasOwnShadingAspect())
215   {
216     theDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
217     if (theDrawer->HasLink())
218     {
219       *theDrawer->ShadingAspect()->Aspect() = *theDrawer->Link()->ShadingAspect()->Aspect();
220     }
221   }
222   if (!theDrawer->HasOwnLineAspect())
223   {
224     theDrawer->SetLineAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
225     if (theDrawer->HasLink())
226     {
227       *theDrawer->LineAspect()->Aspect() = *theDrawer->Link()->LineAspect()->Aspect();
228     }
229   }
230   if (!theDrawer->HasOwnWireAspect())
231   {
232     theDrawer->SetWireAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
233     if (theDrawer->HasLink())
234     {
235       *theDrawer->WireAspect()->Aspect() = *theDrawer->Link()->WireAspect()->Aspect();
236     }
237   }
238   if (!theDrawer->HasOwnUIsoAspect())
239   {
240     theDrawer->SetUIsoAspect (new Prs3d_IsoAspect (Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5, 1));
241     if (theDrawer->HasLink())
242     {
243       *theDrawer->UIsoAspect()->Aspect() = *theDrawer->Link()->UIsoAspect()->Aspect();
244       theDrawer->UIsoAspect()->SetNumber (theDrawer->Link()->UIsoAspect()->Number());
245     }
246   }
247   if (!theDrawer->HasOwnVIsoAspect())
248   {
249     theDrawer->SetVIsoAspect (new Prs3d_IsoAspect (Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5, 1));
250     if (theDrawer->HasLink())
251     {
252       *theDrawer->VIsoAspect()->Aspect() = *theDrawer->Link()->VIsoAspect()->Aspect();
253       theDrawer->VIsoAspect()->SetNumber (theDrawer->Link()->VIsoAspect()->Number());
254     }
255   }
256   if (!theDrawer->HasOwnFreeBoundaryAspect())
257   {
258     theDrawer->SetFreeBoundaryAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
259     if (theDrawer->HasLink())
260     {
261       *theDrawer->FreeBoundaryAspect()->Aspect() = *theDrawer->Link()->FreeBoundaryAspect()->Aspect();
262     }
263   }
264   if (!theDrawer->HasOwnUnFreeBoundaryAspect())
265   {
266     theDrawer->SetUnFreeBoundaryAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
267     if (theDrawer->HasLink())
268     {
269       *theDrawer->UnFreeBoundaryAspect()->Aspect() = *theDrawer->Link()->UnFreeBoundaryAspect()->Aspect();
270     }
271   }
272
273   theDrawer->UnFreeBoundaryAspect()->SetColor (theColorCurv);
274   theDrawer->FreeBoundaryAspect()->SetColor (theColorCurv);
275   theDrawer->WireAspect()->SetColor (theColorCurv);
276
277   Graphic3d_MaterialAspect aMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
278   aMaterial.SetColor (theColorSurf);
279   theDrawer->ShadingAspect()->Aspect()->SetInteriorColor (theColorSurf);
280   theDrawer->ShadingAspect()->Aspect()->SetFrontMaterial (aMaterial);
281   theDrawer->UIsoAspect()->SetColor (theColorSurf);
282   theDrawer->VIsoAspect()->SetColor (theColorSurf);
283 }
284
285 //=======================================================================
286 //function : DefaultStyle
287 //purpose  : DefaultStyle() can be redefined by subclasses in order to set custom default style
288 //=======================================================================
289 void XCAFPrs_AISObject::DefaultStyle (XCAFPrs_Style& theStyle) const
290 {
291   theStyle.SetColorSurf (Quantity_NOC_WHITE);
292   theStyle.SetColorCurv (Quantity_NOC_WHITE);
293 }