0025399: Visualization - XCAFPrs_AISObject ignores global aspect settings
[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 <BRepBndLib.hxx>
20 #include <gp_Pnt.hxx>
21 #include <Graphic3d_AspectFillArea3d.hxx>
22 #include <Graphic3d_AspectLine3d.hxx>
23 #include <Handle_AIS_Drawer.hxx>
24 #include <Prs3d_DimensionAspect.hxx>
25 #include <Prs3d_IsoAspect.hxx>
26 #include <Prs3d_LineAspect.hxx>
27 #include <Prs3d_ShadingAspect.hxx>
28 #include <Prs3d_Text.hxx>
29 #include <TDataStd_Name.hxx>
30 #include <TDF_LabelSequence.hxx>
31 #include <TPrsStd_AISPresentation.hxx>
32 #include <TopoDS_Iterator.hxx>
33 #include <XCAFDoc_ShapeTool.hxx>
34 #include <XCAFPrs.hxx>
35 #include <XCAFPrs_DataMapOfShapeStyle.hxx>
36 #include <XCAFPrs_DataMapIteratorOfDataMapOfShapeStyle.hxx>
37 #include <XCAFPrs_Style.hxx>
38
39 IMPLEMENT_STANDARD_HANDLE (XCAFPrs_AISObject, AIS_ColoredShape)
40 IMPLEMENT_STANDARD_RTTIEXT(XCAFPrs_AISObject, AIS_ColoredShape)
41
42 //=======================================================================
43 //function : XCAFPrs_AISObject
44 //purpose  : 
45 //=======================================================================
46
47 XCAFPrs_AISObject::XCAFPrs_AISObject (const TDF_Label& theLabel)
48 : AIS_ColoredShape(TopoDS_Shape())
49 {
50   myLabel = theLabel;
51 }
52
53 //=======================================================================
54 //function : DisplayText
55 //purpose  : 
56 //=======================================================================
57
58 static void DisplayText (const TDF_Label& aLabel,
59                          const Handle(Prs3d_Presentation)& aPrs,
60                          const Handle(Prs3d_TextAspect)& anAspect,
61                          const TopLoc_Location& aLocation)
62 {
63   // first label itself
64   Handle (TDataStd_Name) aName;
65   if (aLabel.FindAttribute (TDataStd_Name::GetID(), aName)) {
66     TopoDS_Shape aShape;
67     if (XCAFDoc_ShapeTool::GetShape (aLabel, aShape)) {
68       // find the position to display as middle of the bounding box
69       aShape.Move (aLocation);
70       Bnd_Box aBox;
71       BRepBndLib::Add (aShape, aBox);
72       if ( ! aBox.IsVoid() ) 
73       {
74         Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
75         aBox.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
76         gp_Pnt aPnt (0.5 * (aXmin + aXmax), 0.5 * (aYmin + aYmax), 0.5 * (aZmin + aZmax));
77         Prs3d_Text::Draw( aPrs, anAspect, aName->Get(), aPnt);
78       }
79     }
80   }
81
82   TDF_LabelSequence seq;
83   
84   // attibutes of subshapes
85   if (XCAFDoc_ShapeTool::GetSubShapes (aLabel, seq)) {
86     Standard_Integer i = 1;
87     for (i = 1; i <= seq.Length(); i++) {
88       TDF_Label aL = seq.Value (i);
89       DisplayText (aL, aPrs, anAspect, aLocation); //suppose that subshapes do not contain locations
90     }
91   }
92   
93   // attibutes of components
94   seq.Clear();
95   if (XCAFDoc_ShapeTool::GetComponents (aLabel, seq)) {
96     Standard_Integer i = 1;
97     for (i = 1; i <= seq.Length(); i++) {
98       TDF_Label aL = seq.Value (i);
99       DisplayText (aL, aPrs, anAspect, aLocation);
100       TDF_Label aRefLabel;
101       
102       // attributes of referrences
103       TopLoc_Location aLoc = XCAFDoc_ShapeTool::GetLocation (aL);
104       if (XCAFDoc_ShapeTool::GetReferredShape (aL, aRefLabel)) {
105         DisplayText (aRefLabel, aPrs, anAspect, aLoc);
106       }
107     }
108   }
109 }
110
111 //=======================================================================
112 //function : Compute
113 //purpose  : 
114 //=======================================================================
115 void XCAFPrs_AISObject::Compute (const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
116                                  const Handle(Prs3d_Presentation)&           thePrs,
117                                  const Standard_Integer                      theMode)
118 {
119   thePrs->Clear();
120
121   // abv: 06 Mar 00: to have good colors
122   Handle(TPrsStd_AISPresentation) aPrs = Handle(TPrsStd_AISPresentation)::DownCast (GetOwner());
123   if (aPrs.IsNull() || !aPrs->HasOwnMaterial()) SetMaterial (Graphic3d_NOM_PLASTIC);
124
125   TopoDS_Shape aShape;
126   if (!XCAFDoc_ShapeTool::GetShape (myLabel, aShape) || aShape.IsNull()) return;
127
128   // Shape vide -> Assemblage vide.
129   if (aShape.ShapeType() == TopAbs_COMPOUND)
130   {
131     TopoDS_Iterator anExplor (aShape);
132     if (!anExplor.More())
133     {
134       return;
135     }
136   }
137
138   Set (aShape);
139   ClearCustomAspects();
140
141   // Collecting information on colored subshapes
142   TopLoc_Location aLoc;
143   XCAFPrs_DataMapOfShapeStyle aSettings;
144   XCAFPrs::CollectStyleSettings ( myLabel, aLoc, aSettings );
145
146   // Getting default colors
147   XCAFPrs_Style aDefStyle;
148   DefaultStyle (aDefStyle);
149   Quantity_Color aColorCurv = aDefStyle.GetColorCurv();
150   Quantity_Color aColorSurf = aDefStyle.GetColorSurf();
151
152   SetColors (myDrawer, aColorCurv, aColorSurf);
153
154   // Set colors etc. for current shape according to style
155   for (XCAFPrs_DataMapIteratorOfDataMapOfShapeStyle anIter( aSettings ); anIter.More(); anIter.Next())
156   {
157     Handle(AIS_ColoredDrawer) aDrawer = CustomAspects (anIter.Key());
158     const XCAFPrs_Style& aStyle = anIter.Value();
159     aDrawer->SetHidden (!aStyle.IsVisible());
160
161     aColorCurv = aStyle.IsSetColorCurv() ? aStyle.GetColorCurv() : aDefStyle.GetColorCurv();
162     aColorSurf = aStyle.IsSetColorSurf() ? aStyle.GetColorSurf() : aDefStyle.GetColorSurf();
163
164     SetColors (aDrawer, aColorCurv, aColorSurf);
165   }
166
167   AIS_ColoredShape::Compute (thePresentationManager, thePrs, theMode);
168
169   if (XCAFPrs::GetViewNameMode())
170   {
171     // Displaying Name attributes
172     thePrs->SetDisplayPriority (10);
173     DisplayText (myLabel, thePrs, Attributes()->DimensionAspect()->TextAspect(), TopLoc_Location());//no location
174   }
175 }
176
177 //=======================================================================
178 //function : SetColors
179 //purpose  :
180 //=======================================================================
181 void XCAFPrs_AISObject::SetColors (const Handle(AIS_Drawer)& theDrawer,
182                                    const Quantity_Color&     theColorCurv,
183                                    const Quantity_Color&     theColorSurf)
184 {
185   if (!theDrawer->HasShadingAspect())
186   {
187     theDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
188     *theDrawer->ShadingAspect()->Aspect() = *theDrawer->Link()->ShadingAspect()->Aspect();
189   }
190   if (!theDrawer->HasLineAspect())
191   {
192     theDrawer->SetLineAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
193     *theDrawer->LineAspect()->Aspect() = *theDrawer->Link()->LineAspect()->Aspect();
194   }
195   if (!theDrawer->HasWireAspect())
196   {
197     theDrawer->SetWireAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
198     *theDrawer->WireAspect()->Aspect() = *theDrawer->Link()->WireAspect()->Aspect();
199   }
200   if (!theDrawer->HasUIsoAspect())
201   {
202     theDrawer->SetUIsoAspect (new Prs3d_IsoAspect (Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5, 1));
203     *theDrawer->UIsoAspect()->Aspect() = *theDrawer->Link()->UIsoAspect()->Aspect();
204     theDrawer->UIsoAspect()->SetNumber (theDrawer->Link()->UIsoAspect()->Number());
205   }
206   if (!theDrawer->HasVIsoAspect())
207   {
208     theDrawer->SetVIsoAspect (new Prs3d_IsoAspect (Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5, 1));
209     *theDrawer->VIsoAspect()->Aspect() = *theDrawer->Link()->VIsoAspect()->Aspect();
210     theDrawer->VIsoAspect()->SetNumber (theDrawer->Link()->VIsoAspect()->Number());
211   }
212   if (!theDrawer->HasFreeBoundaryAspect())
213   {
214     theDrawer->SetFreeBoundaryAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
215     *theDrawer->FreeBoundaryAspect()->Aspect() = *theDrawer->Link()->FreeBoundaryAspect()->Aspect();
216   }
217   if (!theDrawer->HasUnFreeBoundaryAspect())
218   {
219     theDrawer->SetUnFreeBoundaryAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
220     *theDrawer->UnFreeBoundaryAspect()->Aspect() = *theDrawer->Link()->UnFreeBoundaryAspect()->Aspect();
221   }
222
223   theDrawer->UnFreeBoundaryAspect()->SetColor (theColorCurv);
224   theDrawer->FreeBoundaryAspect()->SetColor (theColorCurv);
225   theDrawer->WireAspect()->SetColor (theColorCurv);
226
227   Graphic3d_MaterialAspect aMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
228   aMaterial.SetColor (theColorSurf);
229   theDrawer->ShadingAspect()->Aspect()->SetInteriorColor (theColorSurf);
230   theDrawer->ShadingAspect()->Aspect()->SetFrontMaterial (aMaterial);
231   theDrawer->UIsoAspect()->SetColor (theColorSurf);
232   theDrawer->VIsoAspect()->SetColor (theColorSurf);
233 }
234
235 //=======================================================================
236 //function : DefaultStyle
237 //purpose  : DefaultStyle() can be redefined by subclasses in order to set custom default style
238 //=======================================================================
239 void XCAFPrs_AISObject::DefaultStyle (XCAFPrs_Style& theStyle) const
240 {
241   theStyle.SetColorSurf (Quantity_NOC_WHITE);
242   theStyle.SetColorCurv (Quantity_NOC_WHITE);
243 }