0028738: Data Exchange, XCAFPrs_Style - add transparency property
[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 IMPLEMENT_STANDARD_RTTIEXT(XCAFPrs_AISObject,AIS_ColoredShape)
42
43 //=======================================================================
44 //function : XCAFPrs_AISObject
45 //purpose  : 
46 //=======================================================================
47
48 XCAFPrs_AISObject::XCAFPrs_AISObject (const TDF_Label& theLabel)
49 : AIS_ColoredShape(TopoDS_Shape()),
50   myToSyncStyles (Standard_True)
51 {
52   // define plastic material by default for proper color reproduction
53   setMaterial (myDrawer, Graphic3d_NOM_PLASTIC, Standard_False, Standard_False);
54   hasOwnMaterial = Standard_True;
55
56   myLabel = theLabel;
57 }
58
59 //=======================================================================
60 //function : DisplayText
61 //purpose  : 
62 //=======================================================================
63
64 static void DisplayText (const TDF_Label& aLabel,
65                          const Handle(Prs3d_Presentation)& aPrs,
66                          const Handle(Prs3d_TextAspect)& anAspect,
67                          const TopLoc_Location& aLocation)
68 {
69   // first label itself
70   Handle (TDataStd_Name) aName;
71   if (aLabel.FindAttribute (TDataStd_Name::GetID(), aName)) {
72     TopoDS_Shape aShape;
73     if (XCAFDoc_ShapeTool::GetShape (aLabel, aShape)) {
74       // find the position to display as middle of the bounding box
75       aShape.Move (aLocation);
76       Bnd_Box aBox;
77       BRepBndLib::Add (aShape, aBox);
78       if ( ! aBox.IsVoid() ) 
79       {
80         Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
81         aBox.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
82         gp_Pnt aPnt (0.5 * (aXmin + aXmax), 0.5 * (aYmin + aYmax), 0.5 * (aZmin + aZmax));
83         Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (aPrs), anAspect, aName->Get(), aPnt);
84       }
85     }
86   }
87
88   TDF_LabelSequence seq;
89   
90   // attibutes of subshapes
91   if (XCAFDoc_ShapeTool::GetSubShapes (aLabel, seq)) {
92     Standard_Integer i = 1;
93     for (i = 1; i <= seq.Length(); i++) {
94       TDF_Label aL = seq.Value (i);
95       DisplayText (aL, aPrs, anAspect, aLocation); //suppose that subshapes do not contain locations
96     }
97   }
98   
99   // attibutes of components
100   seq.Clear();
101   if (XCAFDoc_ShapeTool::GetComponents (aLabel, seq)) {
102     Standard_Integer i = 1;
103     for (i = 1; i <= seq.Length(); i++) {
104       TDF_Label aL = seq.Value (i);
105       DisplayText (aL, aPrs, anAspect, aLocation);
106       TDF_Label aRefLabel;
107       
108       // attributes of referrences
109       TopLoc_Location aLoc = XCAFDoc_ShapeTool::GetLocation (aL);
110       if (XCAFDoc_ShapeTool::GetReferredShape (aL, aRefLabel)) {
111         DisplayText (aRefLabel, aPrs, anAspect, aLoc);
112       }
113     }
114   }
115 }
116
117 //=======================================================================
118 //function : DispatchStyles
119 //purpose  :
120 //=======================================================================
121 void XCAFPrs_AISObject::DispatchStyles (const Standard_Boolean theToSyncStyles)
122 {
123   myToSyncStyles = theToSyncStyles;
124   myShapeColors.Clear();
125
126   TopoDS_Shape aShape;
127   if (!XCAFDoc_ShapeTool::GetShape (myLabel, aShape) || aShape.IsNull())
128   {
129     Set (TopoDS_Shape());
130     return;
131   }
132   Set (aShape);
133
134   // Collecting information on colored subshapes
135   TopLoc_Location aLoc;
136   XCAFPrs_DataMapOfShapeStyle aSettings;
137   XCAFPrs::CollectStyleSettings (myLabel, aLoc, aSettings);
138
139   // Getting default colors
140   XCAFPrs_Style aDefStyle;
141   DefaultStyle (aDefStyle);
142   Quantity_Color aColorCurv = aDefStyle.GetColorCurv();
143   Quantity_ColorRGBA aColorSurf = aDefStyle.GetColorSurfRGBA();
144
145   SetColors (myDrawer, aColorCurv, aColorSurf);
146
147   // collect sub-shapes with the same style into compounds
148   BRep_Builder aBuilder;
149   NCollection_DataMap<XCAFPrs_Style, TopoDS_Compound, XCAFPrs_Style> aStyleGroups;
150   for (XCAFPrs_DataMapIteratorOfDataMapOfShapeStyle aStyledShapeIter (aSettings);
151        aStyledShapeIter.More(); aStyledShapeIter.Next())
152   {
153     TopoDS_Compound aComp;
154     if (aStyleGroups.Find (aStyledShapeIter.Value(), aComp))
155     {
156       aBuilder.Add (aComp, aStyledShapeIter.Key());
157       continue;
158     }
159
160     aBuilder.MakeCompound (aComp);
161     aBuilder.Add (aComp, aStyledShapeIter.Key());
162     aStyleGroups.Bind (aStyledShapeIter.Value(), aComp);
163   }
164   aSettings.Clear();
165
166   // assign custom aspects
167   for (NCollection_DataMap<XCAFPrs_Style, TopoDS_Compound, XCAFPrs_Style>::Iterator aStyleGroupIter (aStyleGroups);
168        aStyleGroupIter.More(); aStyleGroupIter.Next())
169   {
170     const TopoDS_Compound& aComp = aStyleGroupIter.Value();
171     TopoDS_Iterator aShapeIter (aComp);
172     TopoDS_Shape aShapeCur = aShapeIter.Value();
173     aShapeIter.Next();
174     if (aShapeIter.More())
175     {
176       aShapeCur = aComp;
177     }
178
179     Handle(AIS_ColoredDrawer) aDrawer = CustomAspects (aShapeCur);
180     const XCAFPrs_Style& aStyle = aStyleGroupIter.Key();
181     aDrawer->SetHidden (!aStyle.IsVisible());
182
183     aColorCurv = aStyle.IsSetColorCurv() ? aStyle.GetColorCurv()     : aDefStyle.GetColorCurv();
184     aColorSurf = aStyle.IsSetColorSurf() ? aStyle.GetColorSurfRGBA() : aDefStyle.GetColorSurfRGBA();
185
186     SetColors (aDrawer, aColorCurv, aColorSurf);
187   }
188   aStyleGroups.Clear();
189 }
190
191 //=======================================================================
192 //function : Compute
193 //purpose  :
194 //=======================================================================
195 void XCAFPrs_AISObject::Compute (const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
196                                  const Handle(Prs3d_Presentation)&           thePrs,
197                                  const Standard_Integer                      theMode)
198 {
199   // update shape and sub-shapes styles only on first compute, or on first recompute
200   if (myToSyncStyles)
201   {
202     Standard_Boolean toMapStyles = myToSyncStyles;
203     for (PrsMgr_Presentations::Iterator aPrsIter (myPresentations); aPrsIter.More(); aPrsIter.Next())
204     {
205       if (aPrsIter.Value().Presentation()->Presentation() != thePrs
206       && !aPrsIter.Value().Presentation()->MustBeUpdated())
207       {
208         toMapStyles = Standard_False;
209         break;
210       }
211     }
212     if (toMapStyles)
213     {
214       DispatchStyles (Standard_True);
215     }
216   }
217   if (myshape.IsNull())
218   {
219     return;
220   }
221
222   if (myshape.ShapeType() == TopAbs_COMPOUND)
223   {
224     TopoDS_Iterator anExplor (myshape);
225     if (!anExplor.More())
226     {
227       return;
228     }
229   }
230
231   AIS_ColoredShape::Compute (thePresentationManager, thePrs, theMode);
232
233   if (XCAFPrs::GetViewNameMode())
234   {
235     // Displaying Name attributes
236     thePrs->SetDisplayPriority (10);
237     DisplayText (myLabel, thePrs, Attributes()->DimensionAspect()->TextAspect(), TopLoc_Location());//no location
238   }
239 }
240
241 //=======================================================================
242 //function : SetColors
243 //purpose  :
244 //=======================================================================
245 void XCAFPrs_AISObject::SetColors (const Handle(Prs3d_Drawer)& theDrawer,
246                                    const Quantity_Color&       theColorCurv,
247                                    const Quantity_ColorRGBA&   theColorSurf)
248 {
249   if (!theDrawer->HasOwnShadingAspect())
250   {
251     theDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
252     if (theDrawer->HasLink())
253     {
254       *theDrawer->ShadingAspect()->Aspect() = *theDrawer->Link()->ShadingAspect()->Aspect();
255     }
256   }
257   if (!theDrawer->HasOwnLineAspect())
258   {
259     theDrawer->SetLineAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
260     if (theDrawer->HasLink())
261     {
262       *theDrawer->LineAspect()->Aspect() = *theDrawer->Link()->LineAspect()->Aspect();
263     }
264   }
265   if (!theDrawer->HasOwnWireAspect())
266   {
267     theDrawer->SetWireAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
268     if (theDrawer->HasLink())
269     {
270       *theDrawer->WireAspect()->Aspect() = *theDrawer->Link()->WireAspect()->Aspect();
271     }
272   }
273   if (!theDrawer->HasOwnUIsoAspect())
274   {
275     theDrawer->SetUIsoAspect (new Prs3d_IsoAspect (Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5, 1));
276     if (theDrawer->HasLink())
277     {
278       *theDrawer->UIsoAspect()->Aspect() = *theDrawer->Link()->UIsoAspect()->Aspect();
279       theDrawer->UIsoAspect()->SetNumber (theDrawer->Link()->UIsoAspect()->Number());
280     }
281   }
282   if (!theDrawer->HasOwnVIsoAspect())
283   {
284     theDrawer->SetVIsoAspect (new Prs3d_IsoAspect (Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5, 1));
285     if (theDrawer->HasLink())
286     {
287       *theDrawer->VIsoAspect()->Aspect() = *theDrawer->Link()->VIsoAspect()->Aspect();
288       theDrawer->VIsoAspect()->SetNumber (theDrawer->Link()->VIsoAspect()->Number());
289     }
290   }
291   if (!theDrawer->HasOwnFreeBoundaryAspect())
292   {
293     theDrawer->SetFreeBoundaryAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
294     if (theDrawer->HasLink())
295     {
296       *theDrawer->FreeBoundaryAspect()->Aspect() = *theDrawer->Link()->FreeBoundaryAspect()->Aspect();
297     }
298   }
299   if (!theDrawer->HasOwnUnFreeBoundaryAspect())
300   {
301     theDrawer->SetUnFreeBoundaryAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
302     if (theDrawer->HasLink())
303     {
304       *theDrawer->UnFreeBoundaryAspect()->Aspect() = *theDrawer->Link()->UnFreeBoundaryAspect()->Aspect();
305     }
306   }
307
308   theDrawer->UnFreeBoundaryAspect()->SetColor (theColorCurv);
309   theDrawer->FreeBoundaryAspect()->SetColor (theColorCurv);
310   theDrawer->WireAspect()->SetColor (theColorCurv);
311
312   Graphic3d_MaterialAspect aMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
313   aMaterial.SetColor (theColorSurf.GetRGB());
314   aMaterial.SetAlpha (theColorSurf.Alpha());
315   theDrawer->ShadingAspect()->Aspect()->SetInteriorColor (theColorSurf);
316   theDrawer->ShadingAspect()->Aspect()->SetFrontMaterial (aMaterial);
317   theDrawer->UIsoAspect()->SetColor (theColorSurf.GetRGB());
318   theDrawer->VIsoAspect()->SetColor (theColorSurf.GetRGB());
319 }
320
321 //=======================================================================
322 //function : DefaultStyle
323 //purpose  : DefaultStyle() can be redefined by subclasses in order to set custom default style
324 //=======================================================================
325 void XCAFPrs_AISObject::DefaultStyle (XCAFPrs_Style& theStyle) const
326 {
327   theStyle.SetColorSurf (Quantity_NOC_WHITE);
328   theStyle.SetColorCurv (Quantity_NOC_WHITE);
329 }
330
331 // =======================================================================
332 // function : SetMaterial
333 // purpose  :
334 // =======================================================================
335 void XCAFPrs_AISObject::SetMaterial (const Graphic3d_MaterialAspect& theMaterial)
336 {
337   XCAFPrs_Style aDefStyle;
338   DefaultStyle (aDefStyle);
339   setMaterial (myDrawer, theMaterial, HasColor(), IsTransparent());
340   SetColors (myDrawer, aDefStyle.GetColorCurv(), aDefStyle.GetColorSurf());
341   for (AIS_DataMapOfShapeDrawer::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
342   {
343     const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
344
345     // take current color
346     const Quantity_Color     aColorCurv = aDrawer->WireAspect()->Aspect()->Color();
347     const Quantity_ColorRGBA aSurfColor = aDrawer->ShadingAspect()->Aspect()->InteriorColorRGBA();
348
349     // SetColors() will take the material from myDrawer
350     SetColors (aDrawer, aColorCurv, aSurfColor);
351   }
352   SynchronizeAspects();
353 }