1 // Created on: 2000-08-11
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <XCAFPrs_AISObject.ixx>
22 #include <TCollection_ExtendedString.hxx>
24 #include <Prs3d_Text.hxx>
25 #include <Prs3d_LengthAspect.hxx>
26 #include <XCAFDoc_ShapeTool.hxx>
28 #include <Standard_ErrorHandler.hxx>
29 #include <Standard_Failure.hxx>
31 #include <Precision.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS_Iterator.hxx>
34 #include <BRepTools.hxx>
36 #include <StdPrs_WFDeflectionShape.hxx>
37 #include <StdPrs_ShadedShape.hxx>
38 #include <StdPrs_WFShape.hxx>
39 #include <AIS_Drawer.hxx>
40 #include <Graphic3d_Array1OfVertex.hxx>
41 #include <Graphic3d_Group.hxx>
42 #include <Quantity_Color.hxx>
43 #include <Aspect_TypeOfLine.hxx>
44 #include <Prs3d_LineAspect.hxx>
45 #include <Graphic3d_AspectLine3d.hxx>
46 #include <BRep_Builder.hxx>
47 #include <TopoDS_Compound.hxx>
48 #include <XCAFPrs_Style.hxx>
49 #include <TopoDS_Shape.hxx>
50 #include <XCAFPrs_DataMapOfShapeStyle.hxx>
51 #include <TDF_LabelSequence.hxx>
52 #include <XCAFPrs_DataMapOfStyleShape.hxx>
54 #include <XCAFPrs_DataMapIteratorOfDataMapOfStyleShape.hxx>
55 #include <Graphic3d_AspectFillArea3d.hxx>
56 #include <Aspect_InteriorStyle.hxx>
57 #include <Aspect_TypeOfLine.hxx>
58 #include <Prs3d_ShadingAspect.hxx>
60 #include <Prs3d_IsoAspect.hxx>
61 #include <XCAFPrs.hxx>
63 #include <TDataStd_Name.hxx>
64 #include <BRepBndLib.hxx>
66 #include <TPrsStd_AISPresentation.hxx>
73 //=======================================================================
74 //function : XCAFPrs_AISObject
76 //=======================================================================
78 XCAFPrs_AISObject::XCAFPrs_AISObject (const TDF_Label &lab) : AIS_Shape(TopoDS_Shape())
83 if ( XCAFDoc_ShapeTool::GetShape ( myLabel, shape ) && ! shape.IsNull() )
87 //=======================================================================
90 //=======================================================================
92 void XCAFPrs_AISObject::SetColor(const Quantity_Color &aCol)
94 AIS_Shape::SetColor(aCol);
98 //=======================================================================
99 //function : UnsetColor
101 //=======================================================================
103 void XCAFPrs_AISObject::UnsetColor()
107 AIS_Shape::UnsetColor();
112 myToRecomputeModes.Clear();
116 //=======================================================================
117 //function : SetMaterial
119 //=======================================================================
121 void XCAFPrs_AISObject::SetMaterial(const Graphic3d_NameOfMaterial aMat)
123 AIS_Shape::SetMaterial(aMat);
127 //=======================================================================
128 //function : SetMaterial
130 //=======================================================================
132 void XCAFPrs_AISObject::SetMaterial(const Graphic3d_MaterialAspect& aMat)
134 AIS_Shape::SetMaterial(aMat);
138 //=======================================================================
139 //function : UnsetMaterial
141 //=======================================================================
143 void XCAFPrs_AISObject::UnsetMaterial()
147 AIS_Shape::UnsetMaterial();
152 myToRecomputeModes.Clear();
156 //=======================================================================
157 //function : SetTransparency
159 //=======================================================================
161 void XCAFPrs_AISObject::SetTransparency(const Standard_Real AValue)
163 AIS_Shape::SetTransparency(AValue);
167 //=======================================================================
168 //function : UnsetTransparency
170 //=======================================================================
172 void XCAFPrs_AISObject::UnsetTransparency()
174 AIS_Shape::UnsetTransparency();
178 //=======================================================================
179 //function : AddStyledItem
181 //=======================================================================
183 void XCAFPrs_AISObject::AddStyledItem (const XCAFPrs_Style &style,
184 const TopoDS_Shape &shape,
185 const Handle(PrsMgr_PresentationManager3d)&, // aPresentationManager,
186 const Handle(Prs3d_Presentation)& aPrs,
187 const Standard_Integer aMode)
189 // remember current color settings
190 Handle(Graphic3d_AspectFillArea3d) a4bis = myDrawer->ShadingAspect()->Aspect();
191 Aspect_InteriorStyle aStyle;
192 Quantity_Color aIntColor, aEdgeColor;
193 Aspect_TypeOfLine aType;
194 Standard_Real aWidth;
195 a4bis->Values(aStyle,aIntColor,aEdgeColor,aType,aWidth);
196 Graphic3d_MaterialAspect FMAT = a4bis->FrontMaterial();
197 Quantity_Color aFColor = FMAT.Color();
199 Quantity_Color aColor1, aColor2;
200 Aspect_TypeOfLine aLine1, aLine2;
201 Standard_Real aWigth1, aWigth2;
202 Handle(Prs3d_LineAspect) waUFB = myDrawer->UnFreeBoundaryAspect();
203 waUFB->Aspect()->Values(aColor1,aLine1,aWigth1);
204 Handle(Prs3d_LineAspect) waFB = myDrawer->FreeBoundaryAspect();
205 waFB->Aspect()->Values(aColor2,aLine2,aWigth2);
207 Quantity_Color aColor;
208 Aspect_TypeOfLine aLine;
209 Standard_Real aWigth;
210 Handle(Prs3d_LineAspect) wa = myDrawer->WireAspect();
211 wa->Aspect()->Values(aColor,aLine,aWigth);
213 Quantity_Color aColorU, aColorV;
214 Aspect_TypeOfLine aLineU, aLineV;
215 Standard_Real aWigthU, aWigthV;
216 Handle(Prs3d_IsoAspect) UIso = myDrawer->UIsoAspect();
217 Handle(Prs3d_IsoAspect) VIso = myDrawer->VIsoAspect();
218 UIso->Aspect()->Values(aColorU,aLineU,aWigthU);
219 VIso->Aspect()->Values(aColorV,aLineV,aWigthV);
221 // Set colors etc. for current shape according to style
222 if ( style.IsSetColorCurv() ) {
223 Quantity_Color Color = style.GetColorCurv();
224 waUFB->SetColor ( Color.Name() );
225 waFB->SetColor ( Color.Name() );
226 wa->SetColor ( Color.Name() );
228 if ( style.IsSetColorSurf() ) {
229 Quantity_Color Color = style.GetColorSurf();
230 a4bis->SetInteriorColor(Color);
231 FMAT.SetColor(Color);
232 a4bis->SetFrontMaterial(FMAT);
233 UIso->SetColor ( Color.Name() );
234 VIso->SetColor ( Color.Name() );
237 // force drawing isos on planes
238 Standard_Boolean drawIsosPln = myDrawer->IsoOnPlane();
239 myDrawer->SetIsoOnPlane (Standard_True);
241 // add shape to presentation
244 try { OCC_CATCH_SIGNALS StdPrs_WFDeflectionShape::Add(aPrs,shape,myDrawer); }
245 catch (Standard_Failure) {
247 cout << "AIS_Shape::Compute() failed: exception " <<
248 Standard_Failure::Caught()->DynamicType()->Name() << ": " <<
249 Standard_Failure::Caught()->GetMessageString() << endl;
251 // cout << "a Shape should be incorrect: No Compute can be maked on it "<< endl;
252 // on calcule une presentation de la boite englobante
253 // Compute(aPresentationManager,aPrs,2);
259 Standard_Real prevangle ;
260 Standard_Real newangle ;
261 Standard_Real prevcoeff ;
262 Standard_Real newcoeff ;
264 if (OwnDeviationAngle(newangle,prevangle) ||
265 OwnDeviationCoefficient(newcoeff,prevcoeff))
266 if (Abs (newangle - prevangle) > Precision::Angular() ||
267 Abs (newcoeff - prevcoeff) > Precision::Confusion() ) {
269 cout << "AIS_Shape : compute"<<endl;
270 cout << "newangl : " << newangle << " # de " << "prevangl : " << prevangle << " OU "<<endl;
271 cout << "newcoeff : " << newcoeff << " # de " << "prevcoeff : " << prevcoeff << endl;
273 BRepTools::Clean(shape);
276 //shading seulement a partir de face...
279 if ((Standard_Integer) shape.ShapeType()>4)
280 StdPrs_WFDeflectionShape::Add(aPrs,shape,myDrawer);
282 myDrawer->SetShadingAspectGlobal(Standard_False);
284 StdPrs_WFDeflectionShape::Add(aPrs,shape,myDrawer);
286 StdPrs_ShadedShape::Add(aPrs,shape,myDrawer);
289 catch (Standard_Failure) {
291 cout << "AIS_Shape::Compute() in ShadingMode failed: exception " <<
292 Standard_Failure::Caught()->DynamicType()->Name() << ": " <<
293 Standard_Failure::Caught()->GetMessageString() << endl;
295 // last resort: try to display as wireframe
298 StdPrs_WFShape::Add(aPrs,shape,myDrawer);
300 catch (Standard_Failure) {
308 if (IsInfinite()) StdPrs_WFDeflectionShape::Add(aPrs,shape,myDrawer);
309 else DisplayBox(aPrs,BoundingBox(),myDrawer);
313 // Restore initial settings
314 if ( style.IsSetColorCurv() ) {
315 waUFB->SetColor ( aColor1.Name() );
316 waFB->SetColor ( aColor2.Name() );
317 wa->SetColor ( aColor.Name() );
319 if ( style.IsSetColorSurf() ) {
320 a4bis->SetInteriorColor(aIntColor);
321 FMAT.SetColor(aFColor);
322 a4bis->SetFrontMaterial(FMAT);
323 UIso->SetColor ( aColorU );
324 VIso->SetColor ( aColorV );
326 myDrawer->SetIsoOnPlane (drawIsosPln);
329 //=======================================================================
330 //function : DisplayText
332 //=======================================================================
334 static void DisplayText (const TDF_Label& aLabel,
335 const Handle(Prs3d_Presentation)& aPrs,
336 const Handle(Prs3d_TextAspect)& anAspect,
337 const TopLoc_Location& aLocation)
339 // first label itself
340 Handle (TDataStd_Name) aName;
341 if (aLabel.FindAttribute (TDataStd_Name::GetID(), aName)) {
343 if (XCAFDoc_ShapeTool::GetShape (aLabel, aShape)) {
344 // find the position to display as middle of the bounding box
345 aShape.Move (aLocation);
347 BRepBndLib::Add (aShape, aBox);
348 if ( ! aBox.IsVoid() )
350 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
351 aBox.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
352 gp_Pnt aPnt (0.5 * (aXmin + aXmax), 0.5 * (aYmin + aYmax), 0.5 * (aZmin + aZmax));
353 Prs3d_Text::Draw( aPrs, anAspect, aName->Get(), aPnt);
358 TDF_LabelSequence seq;
360 // attibutes of subshapes
361 if (XCAFDoc_ShapeTool::GetSubShapes (aLabel, seq)) {
362 Standard_Integer i = 1;
363 for (i = 1; i <= seq.Length(); i++) {
364 TDF_Label aL = seq.Value (i);
365 DisplayText (aL, aPrs, anAspect, aLocation); //suppose that subshapes do not contain locations
369 // attibutes of components
371 if (XCAFDoc_ShapeTool::GetComponents (aLabel, seq)) {
372 Standard_Integer i = 1;
373 for (i = 1; i <= seq.Length(); i++) {
374 TDF_Label aL = seq.Value (i);
375 DisplayText (aL, aPrs, anAspect, aLocation);
378 // attributes of referrences
379 TopLoc_Location aLoc = XCAFDoc_ShapeTool::GetLocation (aL);
380 if (XCAFDoc_ShapeTool::GetReferredShape (aL, aRefLabel)) {
381 DisplayText (aRefLabel, aPrs, anAspect, aLoc);
387 //=======================================================================
390 //=======================================================================
391 // The Compute() method is copied from AIS_Shape::Compute and enhanced to
392 // support different color settings for different subshapes of a single shape
394 void XCAFPrs_AISObject::Compute (const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
395 const Handle(Prs3d_Presentation)& aPrs,
396 const Standard_Integer aMode)
399 //cout << "XCAFPrs_AISObject: Update called" << endl;
403 // abv: 06 Mar 00: to have good colors
404 Handle(TPrsStd_AISPresentation) prs = Handle(TPrsStd_AISPresentation)::DownCast ( GetOwner() );
405 if ( prs.IsNull() || !prs->HasOwnMaterial() )
406 AIS_Shape::SetMaterial ( Graphic3d_NOM_PLASTIC );
409 if ( ! XCAFDoc_ShapeTool::GetShape ( myLabel, shape ) || shape.IsNull() ) return;
411 // wire,edge,vertex -> pas de HLR + priorite display superieure
412 Standard_Integer TheType = (Standard_Integer)shape.ShapeType();
413 if(TheType>4 && TheType<8) {
414 aPrs->SetVisual(Graphic3d_TOS_ALL);
415 aPrs->SetDisplayPriority(TheType+2);
417 // Shape vide -> Assemblage vide.
418 if (shape.ShapeType() == TopAbs_COMPOUND) {
419 TopoDS_Iterator anExplor (shape);
420 if (!anExplor.More()) {
424 if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
426 // collect information on colored subshapes
428 XCAFPrs_DataMapOfShapeStyle settings;
429 XCAFPrs::CollectStyleSettings ( myLabel, L, settings );
431 //cout << "Styles collected" << endl;
434 // dispatch (sub)shapes by their styles
435 XCAFPrs_DataMapOfStyleShape items;
436 XCAFPrs_Style DefStyle;
437 Quantity_Color White ( Quantity_NOC_WHITE );
438 DefStyle.SetColorSurf ( White );
439 DefStyle.SetColorCurv ( White );
440 XCAFPrs::DispatchStyles ( shape, settings, items, DefStyle );
442 //cout << "Dispatch done" << endl;
445 // add subshapes to presentation (one shape per style)
446 XCAFPrs_DataMapIteratorOfDataMapOfStyleShape it ( items );
448 //Standard_Integer i=1;
450 for ( ; it.More(); it.Next() ) {
451 XCAFPrs_Style s = it.Key();
453 //cout << "Style " << i << ": [" <<
454 // ( s.IsSetColorSurf() ? Quantity_Color::StringName ( s.GetColorSurf().Name() ) : "" ) << ", " <<
455 // ( s.IsSetColorCurv() ? Quantity_Color::StringName ( s.GetColorCurv().Name() ) : "" ) << "]" <<
456 //" --> si_" << i << ( s.IsVisible() ? "" : " <invisible>" ) << endl;
459 if (! s.IsVisible() ) continue;
460 AddStyledItem ( s, it.Value(), aPresentationManager, aPrs, aMode );
463 if ( XCAFPrs::GetViewNameMode() ) {
464 // Displaying Name attributes
466 //cout << "Now display name of shapes" << endl;
468 aPrs->SetDisplayPriority(10);
469 DisplayText (myLabel, aPrs, Attributes()->LengthAspect()->TextAspect(), TopLoc_Location());//no location
472 //cout << "Compute finished" << endl;
475 aPrs->ReCompute(); // for hidden line recomputation if necessary...