1 // Created on: 2000-08-11
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <XCAFPrs_AISObject.hxx>
18 #include <AIS_DisplayMode.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRepBndLib.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>
41 //=======================================================================
42 //function : XCAFPrs_AISObject
44 //=======================================================================
46 XCAFPrs_AISObject::XCAFPrs_AISObject (const TDF_Label& theLabel)
47 : AIS_ColoredShape(TopoDS_Shape())
52 //=======================================================================
53 //function : DisplayText
55 //=======================================================================
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)
63 Handle (TDataStd_Name) aName;
64 if (aLabel.FindAttribute (TDataStd_Name::GetID(), aName)) {
66 if (XCAFDoc_ShapeTool::GetShape (aLabel, aShape)) {
67 // find the position to display as middle of the bounding box
68 aShape.Move (aLocation);
70 BRepBndLib::Add (aShape, aBox);
71 if ( ! aBox.IsVoid() )
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);
81 TDF_LabelSequence seq;
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
92 // attibutes of components
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);
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);
110 //=======================================================================
113 //=======================================================================
114 void XCAFPrs_AISObject::Compute (const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
115 const Handle(Prs3d_Presentation)& thePrs,
116 const Standard_Integer theMode)
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);
125 if (!XCAFDoc_ShapeTool::GetShape (myLabel, aShape) || aShape.IsNull()) return;
127 // Shape vide -> Assemblage vide.
128 if (aShape.ShapeType() == TopAbs_COMPOUND)
130 TopoDS_Iterator anExplor (aShape);
131 if (!anExplor.More())
138 ClearCustomAspects();
140 // Collecting information on colored subshapes
141 TopLoc_Location aLoc;
142 XCAFPrs_DataMapOfShapeStyle aSettings;
143 XCAFPrs::CollectStyleSettings ( myLabel, aLoc, aSettings );
145 // Getting default colors
146 XCAFPrs_Style aDefStyle;
147 DefaultStyle (aDefStyle);
148 Quantity_Color aColorCurv = aDefStyle.GetColorCurv();
149 Quantity_Color aColorSurf = aDefStyle.GetColorSurf();
151 SetColors (myDrawer, aColorCurv, aColorSurf);
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())
159 TopoDS_Compound aComp;
160 if (aStyleGroups.Find (aStyledShapeIter.Value(), aComp))
162 aBuilder.Add (aComp, aStyledShapeIter.Key());
166 aBuilder.MakeCompound (aComp);
167 aBuilder.Add (aComp, aStyledShapeIter.Key());
168 aStyleGroups.Bind (aStyledShapeIter.Value(), aComp);
172 // assign custom aspects
173 for (NCollection_DataMap<XCAFPrs_Style, TopoDS_Compound, XCAFPrs_Style>::Iterator aStyleGroupIter (aStyleGroups);
174 aStyleGroupIter.More(); aStyleGroupIter.Next())
176 const TopoDS_Compound& aComp = aStyleGroupIter.Value();
177 TopoDS_Iterator aShapeIter (aComp);
178 TopoDS_Shape aShape = aShapeIter.Value();
180 if (aShapeIter.More())
185 Handle(AIS_ColoredDrawer) aDrawer = CustomAspects (aShape);
186 const XCAFPrs_Style& aStyle = aStyleGroupIter.Key();
187 aDrawer->SetHidden (!aStyle.IsVisible());
189 aColorCurv = aStyle.IsSetColorCurv() ? aStyle.GetColorCurv() : aDefStyle.GetColorCurv();
190 aColorSurf = aStyle.IsSetColorSurf() ? aStyle.GetColorSurf() : aDefStyle.GetColorSurf();
192 SetColors (aDrawer, aColorCurv, aColorSurf);
194 aStyleGroups.Clear();
196 AIS_ColoredShape::Compute (thePresentationManager, thePrs, theMode);
198 if (XCAFPrs::GetViewNameMode())
200 // Displaying Name attributes
201 thePrs->SetDisplayPriority (10);
202 DisplayText (myLabel, thePrs, Attributes()->DimensionAspect()->TextAspect(), TopLoc_Location());//no location
206 //=======================================================================
207 //function : SetColors
209 //=======================================================================
210 void XCAFPrs_AISObject::SetColors (const Handle(Prs3d_Drawer)& theDrawer,
211 const Quantity_Color& theColorCurv,
212 const Quantity_Color& theColorSurf)
214 if (!theDrawer->HasOwnShadingAspect())
216 theDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
217 if (theDrawer->HasLink())
219 *theDrawer->ShadingAspect()->Aspect() = *theDrawer->Link()->ShadingAspect()->Aspect();
222 if (!theDrawer->HasOwnLineAspect())
224 theDrawer->SetLineAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
225 if (theDrawer->HasLink())
227 *theDrawer->LineAspect()->Aspect() = *theDrawer->Link()->LineAspect()->Aspect();
230 if (!theDrawer->HasOwnWireAspect())
232 theDrawer->SetWireAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
233 if (theDrawer->HasLink())
235 *theDrawer->WireAspect()->Aspect() = *theDrawer->Link()->WireAspect()->Aspect();
238 if (!theDrawer->HasOwnUIsoAspect())
240 theDrawer->SetUIsoAspect (new Prs3d_IsoAspect (Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5, 1));
241 if (theDrawer->HasLink())
243 *theDrawer->UIsoAspect()->Aspect() = *theDrawer->Link()->UIsoAspect()->Aspect();
244 theDrawer->UIsoAspect()->SetNumber (theDrawer->Link()->UIsoAspect()->Number());
247 if (!theDrawer->HasOwnVIsoAspect())
249 theDrawer->SetVIsoAspect (new Prs3d_IsoAspect (Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5, 1));
250 if (theDrawer->HasLink())
252 *theDrawer->VIsoAspect()->Aspect() = *theDrawer->Link()->VIsoAspect()->Aspect();
253 theDrawer->VIsoAspect()->SetNumber (theDrawer->Link()->VIsoAspect()->Number());
256 if (!theDrawer->HasOwnFreeBoundaryAspect())
258 theDrawer->SetFreeBoundaryAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
259 if (theDrawer->HasLink())
261 *theDrawer->FreeBoundaryAspect()->Aspect() = *theDrawer->Link()->FreeBoundaryAspect()->Aspect();
264 if (!theDrawer->HasOwnUnFreeBoundaryAspect())
266 theDrawer->SetUnFreeBoundaryAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
267 if (theDrawer->HasLink())
269 *theDrawer->UnFreeBoundaryAspect()->Aspect() = *theDrawer->Link()->UnFreeBoundaryAspect()->Aspect();
273 theDrawer->UnFreeBoundaryAspect()->SetColor (theColorCurv);
274 theDrawer->FreeBoundaryAspect()->SetColor (theColorCurv);
275 theDrawer->WireAspect()->SetColor (theColorCurv);
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);
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
291 theStyle.SetColorSurf (Quantity_NOC_WHITE);
292 theStyle.SetColorCurv (Quantity_NOC_WHITE);