0028188: Improving layer color storage mechanism for DXF Import/Export
[occt.git] / src / XCAFPrs / XCAFPrs_AISObject.cxx
CommitLineData
b311480e 1// Created on: 2000-08-11
2// Created by: Andrey BETENEV
973c2be1 3// Copyright (c) 2000-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
74618acf 16#include <XCAFPrs_AISObject.hxx>
17
18#include <AIS_DisplayMode.hxx>
3ea0a91b 19#include <BRep_Builder.hxx>
74618acf 20#include <BRepBndLib.hxx>
7fd59977 21#include <gp_Pnt.hxx>
74618acf 22#include <Graphic3d_AspectFillArea3d.hxx>
e21b2bc6 23#include <Graphic3d_AspectLine3d.hxx>
6262338c 24#include <Prs3d_Drawer.hxx>
a6eb515f 25#include <Prs3d_DimensionAspect.hxx>
74618acf 26#include <Prs3d_IsoAspect.hxx>
7fd59977 27#include <Prs3d_LineAspect.hxx>
7fd59977 28#include <Prs3d_ShadingAspect.hxx>
74618acf 29#include <Prs3d_Text.hxx>
7fd59977 30#include <TDataStd_Name.hxx>
74618acf 31#include <TDF_LabelSequence.hxx>
7fd59977 32#include <TPrsStd_AISPresentation.hxx>
74618acf 33#include <TopoDS_Iterator.hxx>
34#include <XCAFDoc_ShapeTool.hxx>
35#include <XCAFPrs.hxx>
08b183fe 36#include <XCAFPrs_IndexedDataMapOfShapeStyle.hxx>
37#include <XCAFPrs_DataMapIteratorOfIndexedDataMapOfShapeStyle.hxx>
74618acf 38#include <XCAFPrs_Style.hxx>
7fd59977 39
7fd59977 40
92efcf78 41IMPLEMENT_STANDARD_RTTIEXT(XCAFPrs_AISObject,AIS_ColoredShape)
42
7fd59977 43//=======================================================================
44//function : XCAFPrs_AISObject
45//purpose :
46//=======================================================================
47
74618acf 48XCAFPrs_AISObject::XCAFPrs_AISObject (const TDF_Label& theLabel)
e5b8456d 49: AIS_ColoredShape(TopoDS_Shape()),
50 myToSyncStyles (Standard_True)
5cbef0fe 51{
905db769 52 // define plastic material by default for proper color reproduction
e5b8456d 53 setMaterial (myDrawer, Graphic3d_NOM_PLASTIC, Standard_False, Standard_False);
54 hasOwnMaterial = Standard_True;
55
74618acf 56 myLabel = theLabel;
7fd59977 57}
58
59//=======================================================================
60//function : DisplayText
61//purpose :
62//=======================================================================
63
64static 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));
4ad142d9 83 Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (aPrs), anAspect, aName->Get(), aPnt);
7fd59977 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 }
7fd59977 115}
74618acf 116
7fd59977 117//=======================================================================
e5b8456d 118//function : DispatchStyles
119//purpose :
7fd59977 120//=======================================================================
e5b8456d 121void XCAFPrs_AISObject::DispatchStyles (const Standard_Boolean theToSyncStyles)
74618acf 122{
e5b8456d 123 myToSyncStyles = theToSyncStyles;
124 myShapeColors.Clear();
7fd59977 125
e5b8456d 126 TopoDS_Shape aShape;
127 if (!XCAFDoc_ShapeTool::GetShape (myLabel, aShape) || aShape.IsNull())
74618acf 128 {
e5b8456d 129 Set (TopoDS_Shape());
130 return;
7fd59977 131 }
74618acf 132 Set (aShape);
7fd59977 133
74618acf 134 // Collecting information on colored subshapes
135 TopLoc_Location aLoc;
08b183fe 136 XCAFPrs_IndexedDataMapOfShapeStyle aSettings;
e5b8456d 137 XCAFPrs::CollectStyleSettings (myLabel, aLoc, aSettings);
74618acf 138
139 // Getting default colors
140 XCAFPrs_Style aDefStyle;
141 DefaultStyle (aDefStyle);
142 Quantity_Color aColorCurv = aDefStyle.GetColorCurv();
a71a71de 143 Quantity_ColorRGBA aColorSurf = aDefStyle.GetColorSurfRGBA();
74618acf 144
145 SetColors (myDrawer, aColorCurv, aColorSurf);
146
3ea0a91b 147 // collect sub-shapes with the same style into compounds
148 BRep_Builder aBuilder;
08b183fe 149 NCollection_IndexedDataMap<XCAFPrs_Style, TopoDS_Compound, XCAFPrs_Style> aStyleGroups;
150 for (XCAFPrs_DataMapIteratorOfIndexedDataMapOfShapeStyle aStyledShapeIter (aSettings);
3ea0a91b 151 aStyledShapeIter.More(); aStyledShapeIter.Next())
74618acf 152 {
3ea0a91b 153 TopoDS_Compound aComp;
08b183fe 154 if (aStyleGroups.FindFromKey (aStyledShapeIter.Value(), aComp))
3ea0a91b 155 {
156 aBuilder.Add (aComp, aStyledShapeIter.Key());
157 continue;
158 }
159
160 aBuilder.MakeCompound (aComp);
161 aBuilder.Add (aComp, aStyledShapeIter.Key());
08b183fe 162 TopoDS_Compound* aMapShape = aStyleGroups.ChangeSeek (aStyledShapeIter.Value());
163 if (aMapShape == NULL)
164 aStyleGroups.Add (aStyledShapeIter.Value(), aComp);
165 else
166 *aMapShape = aComp;
3ea0a91b 167 }
168 aSettings.Clear();
169
170 // assign custom aspects
08b183fe 171 for (NCollection_IndexedDataMap<XCAFPrs_Style, TopoDS_Compound, XCAFPrs_Style>::Iterator aStyleGroupIter (aStyleGroups);
3ea0a91b 172 aStyleGroupIter.More(); aStyleGroupIter.Next())
173 {
174 const TopoDS_Compound& aComp = aStyleGroupIter.Value();
175 TopoDS_Iterator aShapeIter (aComp);
51740958 176 TopoDS_Shape aShapeCur = aShapeIter.Value();
3ea0a91b 177 aShapeIter.Next();
178 if (aShapeIter.More())
179 {
51740958 180 aShapeCur = aComp;
3ea0a91b 181 }
182
51740958 183 Handle(AIS_ColoredDrawer) aDrawer = CustomAspects (aShapeCur);
3ea0a91b 184 const XCAFPrs_Style& aStyle = aStyleGroupIter.Key();
86766b0e 185 aDrawer->SetHidden (!aStyle.IsVisible());
74618acf 186
a71a71de 187 aColorCurv = aStyle.IsSetColorCurv() ? aStyle.GetColorCurv() : aDefStyle.GetColorCurv();
188 aColorSurf = aStyle.IsSetColorSurf() ? aStyle.GetColorSurfRGBA() : aDefStyle.GetColorSurfRGBA();
74618acf 189
190 SetColors (aDrawer, aColorCurv, aColorSurf);
7fd59977 191 }
3ea0a91b 192 aStyleGroups.Clear();
e5b8456d 193}
194
195//=======================================================================
196//function : Compute
197//purpose :
198//=======================================================================
199void XCAFPrs_AISObject::Compute (const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
200 const Handle(Prs3d_Presentation)& thePrs,
201 const Standard_Integer theMode)
202{
203 // update shape and sub-shapes styles only on first compute, or on first recompute
204 if (myToSyncStyles)
205 {
206 Standard_Boolean toMapStyles = myToSyncStyles;
207 for (PrsMgr_Presentations::Iterator aPrsIter (myPresentations); aPrsIter.More(); aPrsIter.Next())
208 {
209 if (aPrsIter.Value().Presentation()->Presentation() != thePrs
210 && !aPrsIter.Value().Presentation()->MustBeUpdated())
211 {
212 toMapStyles = Standard_False;
213 break;
214 }
215 }
216 if (toMapStyles)
217 {
218 DispatchStyles (Standard_True);
219 }
220 }
221 if (myshape.IsNull())
222 {
223 return;
224 }
225
226 if (myshape.ShapeType() == TopAbs_COMPOUND)
227 {
228 TopoDS_Iterator anExplor (myshape);
229 if (!anExplor.More())
230 {
231 return;
232 }
233 }
74618acf 234
235 AIS_ColoredShape::Compute (thePresentationManager, thePrs, theMode);
236
237 if (XCAFPrs::GetViewNameMode())
238 {
239 // Displaying Name attributes
240 thePrs->SetDisplayPriority (10);
241 DisplayText (myLabel, thePrs, Attributes()->DimensionAspect()->TextAspect(), TopLoc_Location());//no location
7fd59977 242 }
74618acf 243}
244
245//=======================================================================
246//function : SetColors
247//purpose :
248//=======================================================================
6262338c 249void XCAFPrs_AISObject::SetColors (const Handle(Prs3d_Drawer)& theDrawer,
250 const Quantity_Color& theColorCurv,
a71a71de 251 const Quantity_ColorRGBA& theColorSurf)
74618acf 252{
6262338c 253 if (!theDrawer->HasOwnShadingAspect())
74618acf 254 {
255 theDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
6262338c 256 if (theDrawer->HasLink())
257 {
258 *theDrawer->ShadingAspect()->Aspect() = *theDrawer->Link()->ShadingAspect()->Aspect();
259 }
74618acf 260 }
6262338c 261 if (!theDrawer->HasOwnLineAspect())
74618acf 262 {
263 theDrawer->SetLineAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
6262338c 264 if (theDrawer->HasLink())
265 {
266 *theDrawer->LineAspect()->Aspect() = *theDrawer->Link()->LineAspect()->Aspect();
267 }
74618acf 268 }
6262338c 269 if (!theDrawer->HasOwnWireAspect())
74618acf 270 {
271 theDrawer->SetWireAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
6262338c 272 if (theDrawer->HasLink())
273 {
274 *theDrawer->WireAspect()->Aspect() = *theDrawer->Link()->WireAspect()->Aspect();
275 }
74618acf 276 }
6262338c 277 if (!theDrawer->HasOwnUIsoAspect())
74618acf 278 {
279 theDrawer->SetUIsoAspect (new Prs3d_IsoAspect (Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5, 1));
6262338c 280 if (theDrawer->HasLink())
281 {
282 *theDrawer->UIsoAspect()->Aspect() = *theDrawer->Link()->UIsoAspect()->Aspect();
283 theDrawer->UIsoAspect()->SetNumber (theDrawer->Link()->UIsoAspect()->Number());
284 }
74618acf 285 }
6262338c 286 if (!theDrawer->HasOwnVIsoAspect())
74618acf 287 {
288 theDrawer->SetVIsoAspect (new Prs3d_IsoAspect (Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5, 1));
6262338c 289 if (theDrawer->HasLink())
290 {
291 *theDrawer->VIsoAspect()->Aspect() = *theDrawer->Link()->VIsoAspect()->Aspect();
292 theDrawer->VIsoAspect()->SetNumber (theDrawer->Link()->VIsoAspect()->Number());
293 }
74618acf 294 }
6262338c 295 if (!theDrawer->HasOwnFreeBoundaryAspect())
74618acf 296 {
297 theDrawer->SetFreeBoundaryAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
6262338c 298 if (theDrawer->HasLink())
299 {
300 *theDrawer->FreeBoundaryAspect()->Aspect() = *theDrawer->Link()->FreeBoundaryAspect()->Aspect();
301 }
74618acf 302 }
6262338c 303 if (!theDrawer->HasOwnUnFreeBoundaryAspect())
74618acf 304 {
305 theDrawer->SetUnFreeBoundaryAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
6262338c 306 if (theDrawer->HasLink())
307 {
308 *theDrawer->UnFreeBoundaryAspect()->Aspect() = *theDrawer->Link()->UnFreeBoundaryAspect()->Aspect();
309 }
74618acf 310 }
311
312 theDrawer->UnFreeBoundaryAspect()->SetColor (theColorCurv);
313 theDrawer->FreeBoundaryAspect()->SetColor (theColorCurv);
314 theDrawer->WireAspect()->SetColor (theColorCurv);
315
316 Graphic3d_MaterialAspect aMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
a71a71de 317 aMaterial.SetColor (theColorSurf.GetRGB());
318 aMaterial.SetAlpha (theColorSurf.Alpha());
74618acf 319 theDrawer->ShadingAspect()->Aspect()->SetInteriorColor (theColorSurf);
320 theDrawer->ShadingAspect()->Aspect()->SetFrontMaterial (aMaterial);
a71a71de 321 theDrawer->UIsoAspect()->SetColor (theColorSurf.GetRGB());
322 theDrawer->VIsoAspect()->SetColor (theColorSurf.GetRGB());
7fd59977 323}
576ab394
RL
324
325//=======================================================================
326//function : DefaultStyle
327//purpose : DefaultStyle() can be redefined by subclasses in order to set custom default style
328//=======================================================================
74618acf 329void XCAFPrs_AISObject::DefaultStyle (XCAFPrs_Style& theStyle) const
576ab394 330{
74618acf 331 theStyle.SetColorSurf (Quantity_NOC_WHITE);
332 theStyle.SetColorCurv (Quantity_NOC_WHITE);
576ab394 333}
e5b8456d 334
335// =======================================================================
336// function : SetMaterial
337// purpose :
338// =======================================================================
339void XCAFPrs_AISObject::SetMaterial (const Graphic3d_MaterialAspect& theMaterial)
340{
341 XCAFPrs_Style aDefStyle;
342 DefaultStyle (aDefStyle);
343 setMaterial (myDrawer, theMaterial, HasColor(), IsTransparent());
344 SetColors (myDrawer, aDefStyle.GetColorCurv(), aDefStyle.GetColorSurf());
345 for (AIS_DataMapOfShapeDrawer::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
346 {
347 const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
348
349 // take current color
a71a71de 350 const Quantity_Color aColorCurv = aDrawer->WireAspect()->Aspect()->Color();
351 const Quantity_ColorRGBA aSurfColor = aDrawer->ShadingAspect()->Aspect()->InteriorColorRGBA();
e5b8456d 352
353 // SetColors() will take the material from myDrawer
354 SetColors (aDrawer, aColorCurv, aSurfColor);
355 }
356 SynchronizeAspects();
357}