1 // File: XCAFPrs_AISObject.cxx
2 // Created: Fri Aug 11 16:49:09 2000
3 // Author: Andrey BETENEV
5 #include <XCAFPrs_AISObject.ixx>
6 #include <TCollection_ExtendedString.hxx>
8 #include <Prs3d_Text.hxx>
9 #include <Prs3d_LengthAspect.hxx>
10 #include <XCAFDoc_ShapeTool.hxx>
12 #include <Standard_ErrorHandler.hxx>
13 #include <Standard_Failure.hxx>
15 #include <Precision.hxx>
16 #include <TopoDS_Shape.hxx>
17 #include <TopoDS_Iterator.hxx>
18 #include <BRepTools.hxx>
20 #include <StdPrs_WFDeflectionShape.hxx>
21 #include <StdPrs_ShadedShape.hxx>
22 #include <StdPrs_WFShape.hxx>
23 #include <AIS_Drawer.hxx>
24 #include <Graphic3d_Array1OfVertex.hxx>
25 #include <Graphic3d_Group.hxx>
26 #include <Quantity_Color.hxx>
27 #include <Aspect_TypeOfLine.hxx>
28 #include <Prs3d_LineAspect.hxx>
29 #include <Graphic3d_AspectLine3d.hxx>
30 #include <BRep_Builder.hxx>
31 #include <TopoDS_Compound.hxx>
32 #include <XCAFPrs_Style.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <XCAFPrs_DataMapOfShapeStyle.hxx>
35 #include <TDF_LabelSequence.hxx>
36 #include <XCAFPrs_DataMapOfStyleShape.hxx>
38 #include <XCAFPrs_DataMapIteratorOfDataMapOfStyleShape.hxx>
39 #include <Graphic3d_AspectFillArea3d.hxx>
40 #include <Aspect_InteriorStyle.hxx>
41 #include <Aspect_TypeOfLine.hxx>
42 #include <Prs3d_ShadingAspect.hxx>
44 #include <Prs3d_IsoAspect.hxx>
45 #include <XCAFPrs.hxx>
47 #include <TDataStd_Name.hxx>
48 #include <BRepBndLib.hxx>
50 #include <TPrsStd_AISPresentation.hxx>
57 //=======================================================================
58 //function : XCAFPrs_AISObject
60 //=======================================================================
62 XCAFPrs_AISObject::XCAFPrs_AISObject (const TDF_Label &lab) : AIS_Shape(TopoDS_Shape())
67 if ( XCAFDoc_ShapeTool::GetShape ( myLabel, shape ) && ! shape.IsNull() )
71 //=======================================================================
74 //=======================================================================
76 void XCAFPrs_AISObject::SetColor(const Quantity_Color &aCol)
78 AIS_Shape::SetColor(aCol);
82 //=======================================================================
83 //function : UnsetColor
85 //=======================================================================
87 void XCAFPrs_AISObject::UnsetColor()
91 AIS_Shape::UnsetColor();
96 myToRecomputeModes.Clear();
100 //=======================================================================
101 //function : SetMaterial
103 //=======================================================================
105 void XCAFPrs_AISObject::SetMaterial(const Graphic3d_NameOfMaterial aMat)
107 AIS_Shape::SetMaterial(aMat);
111 //=======================================================================
112 //function : SetMaterial
114 //=======================================================================
116 void XCAFPrs_AISObject::SetMaterial(const Graphic3d_MaterialAspect& aMat)
118 AIS_Shape::SetMaterial(aMat);
122 //=======================================================================
123 //function : UnsetMaterial
125 //=======================================================================
127 void XCAFPrs_AISObject::UnsetMaterial()
131 AIS_Shape::UnsetMaterial();
136 myToRecomputeModes.Clear();
140 //=======================================================================
141 //function : SetTransparency
143 //=======================================================================
145 void XCAFPrs_AISObject::SetTransparency(const Standard_Real AValue)
147 AIS_Shape::SetTransparency(AValue);
151 //=======================================================================
152 //function : UnsetTransparency
154 //=======================================================================
156 void XCAFPrs_AISObject::UnsetTransparency()
158 AIS_Shape::UnsetTransparency();
162 //=======================================================================
163 //function : DisplayBox
165 //=======================================================================
167 static void DisplayBox(const Handle(Prs3d_Presentation)& aPrs,
169 const Handle(Prs3d_Drawer)& aDrawer)
171 Standard_Real X[2],Y[2],Z[2];
172 Standard_Integer Indx [16] ;
177 Indx [0]=1;Indx [1]=2;Indx [2]=4;Indx [3]=3;
178 Indx [4]=5;Indx [5]=6;Indx [6]=8;Indx [7]=7;
179 Indx [8]=1;Indx [9]=3;Indx [10]=7;Indx [11]=5;
180 Indx [12]=2;Indx [13]=4;Indx [14]=8;Indx [15]=6;
181 B.Get(X[0], Y[0], Z[0], X[1], Y[1], Z[1]);
183 Indx [0]=1;Indx [1]=2;Indx [2]=3;Indx [3]=4;Indx [4]=5;Indx [5]=6;Indx [6]=7;
184 Indx [7]=8;Indx [8]=1;Indx [9]=2;Indx [10]=6;Indx [10]=5;Indx [10]=3;
185 Indx [10]=4;Indx [10]=8;Indx [10]=7;
186 B.Get(X[1], Y[1], Z[1], X[2], Y[2], Z[2]);
189 Graphic3d_Array1OfVertex V(1,8);
190 Standard_Integer Rank(0);
191 for(Standard_Integer k=0;k<=1;k++)
192 for(Standard_Integer j=0;j<=1;j++)
193 for(Standard_Integer i=0;i<=1;i++)
194 V(++Rank) = Graphic3d_Vertex(X[i],Y[j],Z[k]);
197 Handle(Graphic3d_Group) G = Prs3d_Root::CurrentGroup(aPrs);
201 aDrawer->LineAspect()->Aspect()->Values(Q,A,W);
204 G->SetGroupPrimitivesAspect(new Graphic3d_AspectLine3d(Q,Aspect_TOL_DOTDASH,W));
206 G->BeginPrimitives();Standard_Integer I,J;
207 Graphic3d_Array1OfVertex VVV (1,5);
210 VVV.SetValue(J,V(Indx[J+4*I-5]));
212 VVV.SetValue(5,VVV(1));
218 //=======================================================================
219 //function : AddStyledItem
221 //=======================================================================
223 void XCAFPrs_AISObject::AddStyledItem (const XCAFPrs_Style &style,
224 const TopoDS_Shape &shape,
225 const Handle(PrsMgr_PresentationManager3d)&, // aPresentationManager,
226 const Handle(Prs3d_Presentation)& aPrs,
227 const Standard_Integer aMode)
229 // remember current color settings
230 Handle(Graphic3d_AspectFillArea3d) a4bis = myDrawer->ShadingAspect()->Aspect();
231 Aspect_InteriorStyle aStyle;
232 Quantity_Color aIntColor, aEdgeColor;
233 Aspect_TypeOfLine aType;
234 Standard_Real aWidth;
235 a4bis->Values(aStyle,aIntColor,aEdgeColor,aType,aWidth);
236 Graphic3d_MaterialAspect FMAT = a4bis->FrontMaterial();
237 Quantity_Color aFColor = FMAT.Color();
239 Quantity_Color aColor1, aColor2;
240 Aspect_TypeOfLine aLine1, aLine2;
241 Standard_Real aWigth1, aWigth2;
242 Handle(Prs3d_LineAspect) waUFB = myDrawer->UnFreeBoundaryAspect();
243 waUFB->Aspect()->Values(aColor1,aLine1,aWigth1);
244 Handle(Prs3d_LineAspect) waFB = myDrawer->FreeBoundaryAspect();
245 waFB->Aspect()->Values(aColor2,aLine2,aWigth2);
247 Quantity_Color aColor;
248 Aspect_TypeOfLine aLine;
249 Standard_Real aWigth;
250 Handle(Prs3d_LineAspect) wa = myDrawer->WireAspect();
251 wa->Aspect()->Values(aColor,aLine,aWigth);
253 Quantity_Color aColorU, aColorV;
254 Aspect_TypeOfLine aLineU, aLineV;
255 Standard_Real aWigthU, aWigthV;
256 Handle(Prs3d_IsoAspect) UIso = myDrawer->UIsoAspect();
257 Handle(Prs3d_IsoAspect) VIso = myDrawer->VIsoAspect();
258 UIso->Aspect()->Values(aColorU,aLineU,aWigthU);
259 VIso->Aspect()->Values(aColorV,aLineV,aWigthV);
261 // Set colors etc. for current shape according to style
262 if ( style.IsSetColorCurv() ) {
263 Quantity_Color Color = style.GetColorCurv();
264 waUFB->SetColor ( Color.Name() );
265 waFB->SetColor ( Color.Name() );
266 wa->SetColor ( Color.Name() );
268 if ( style.IsSetColorSurf() ) {
269 Quantity_Color Color = style.GetColorSurf();
270 a4bis->SetInteriorColor(Color);
271 FMAT.SetColor(Color);
272 a4bis->SetFrontMaterial(FMAT);
273 UIso->SetColor ( Color.Name() );
274 VIso->SetColor ( Color.Name() );
277 // force drawing isos on planes
278 Standard_Boolean drawIsosPln = myDrawer->IsoOnPlane();
279 myDrawer->SetIsoOnPlane (Standard_True);
281 // add shape to presentation
284 try { OCC_CATCH_SIGNALS StdPrs_WFDeflectionShape::Add(aPrs,shape,myDrawer); }
285 catch (Standard_Failure) {
287 cout << "AIS_Shape::Compute() failed: exception " <<
288 Standard_Failure::Caught()->DynamicType()->Name() << ": " <<
289 Standard_Failure::Caught()->GetMessageString() << endl;
291 // cout << "a Shape should be incorrect: No Compute can be maked on it "<< endl;
292 // on calcule une presentation de la boite englobante
293 // Compute(aPresentationManager,aPrs,2);
299 Standard_Real prevangle ;
300 Standard_Real newangle ;
301 Standard_Real prevcoeff ;
302 Standard_Real newcoeff ;
304 if (OwnDeviationAngle(newangle,prevangle) ||
305 OwnDeviationCoefficient(newcoeff,prevcoeff))
306 if (Abs (newangle - prevangle) > Precision::Angular() ||
307 Abs (newcoeff - prevcoeff) > Precision::Confusion() ) {
309 cout << "AIS_Shape : compute"<<endl;
310 cout << "newangl : " << newangle << " # de " << "prevangl : " << prevangle << " OU "<<endl;
311 cout << "newcoeff : " << newcoeff << " # de " << "prevcoeff : " << prevcoeff << endl;
313 BRepTools::Clean(shape);
316 //shading seulement a partir de face...
319 if ((Standard_Integer) shape.ShapeType()>4)
320 StdPrs_WFDeflectionShape::Add(aPrs,shape,myDrawer);
322 myDrawer->SetShadingAspectGlobal(Standard_False);
324 StdPrs_WFDeflectionShape::Add(aPrs,shape,myDrawer);
326 StdPrs_ShadedShape::Add(aPrs,shape,myDrawer);
329 catch (Standard_Failure) {
331 cout << "AIS_Shape::Compute() in ShadingMode failed: exception " <<
332 Standard_Failure::Caught()->DynamicType()->Name() << ": " <<
333 Standard_Failure::Caught()->GetMessageString() << endl;
335 // last resort: try to display as wireframe
338 StdPrs_WFShape::Add(aPrs,shape,myDrawer);
340 catch (Standard_Failure) {
348 if (IsInfinite()) StdPrs_WFDeflectionShape::Add(aPrs,shape,myDrawer);
349 else DisplayBox(aPrs,BoundingBox(),myDrawer);
353 // Restore initial settings
354 if ( style.IsSetColorCurv() ) {
355 waUFB->SetColor ( aColor1.Name() );
356 waFB->SetColor ( aColor2.Name() );
357 wa->SetColor ( aColor.Name() );
359 if ( style.IsSetColorSurf() ) {
360 a4bis->SetInteriorColor(aIntColor);
361 FMAT.SetColor(aFColor);
362 a4bis->SetFrontMaterial(FMAT);
363 UIso->SetColor ( aColorU );
364 VIso->SetColor ( aColorV );
366 myDrawer->SetIsoOnPlane (drawIsosPln);
369 //=======================================================================
370 //function : DisplayText
372 //=======================================================================
374 static void DisplayText (const TDF_Label& aLabel,
375 const Handle(Prs3d_Presentation)& aPrs,
376 const Handle(Prs3d_TextAspect)& anAspect,
377 const TopLoc_Location& aLocation)
379 // first label itself
380 Handle (TDataStd_Name) aName;
381 if (aLabel.FindAttribute (TDataStd_Name::GetID(), aName)) {
383 if (XCAFDoc_ShapeTool::GetShape (aLabel, aShape)) {
384 // find the position to display as middle of the bounding box
385 aShape.Move (aLocation);
387 BRepBndLib::Add (aShape, aBox);
388 if ( ! aBox.IsVoid() )
390 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
391 aBox.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
392 gp_Pnt aPnt (0.5 * (aXmin + aXmax), 0.5 * (aYmin + aYmax), 0.5 * (aZmin + aZmax));
393 Prs3d_Text::Draw( aPrs, anAspect, aName->Get(), aPnt);
398 TDF_LabelSequence seq;
400 // attibutes of subshapes
401 if (XCAFDoc_ShapeTool::GetSubShapes (aLabel, seq)) {
402 Standard_Integer i = 1;
403 for (i = 1; i <= seq.Length(); i++) {
404 TDF_Label aL = seq.Value (i);
405 DisplayText (aL, aPrs, anAspect, aLocation); //suppose that subshapes do not contain locations
409 // attibutes of components
411 if (XCAFDoc_ShapeTool::GetComponents (aLabel, seq)) {
412 Standard_Integer i = 1;
413 for (i = 1; i <= seq.Length(); i++) {
414 TDF_Label aL = seq.Value (i);
415 DisplayText (aL, aPrs, anAspect, aLocation);
418 // attributes of referrences
419 TopLoc_Location aLoc = XCAFDoc_ShapeTool::GetLocation (aL);
420 if (XCAFDoc_ShapeTool::GetReferredShape (aL, aRefLabel)) {
421 DisplayText (aRefLabel, aPrs, anAspect, aLoc);
427 //=======================================================================
430 //=======================================================================
431 // The Compute() method is copied from AIS_Shape::Compute and enhanced to
432 // support different color settings for different subshapes of a single shape
434 void XCAFPrs_AISObject::Compute (const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
435 const Handle(Prs3d_Presentation)& aPrs,
436 const Standard_Integer aMode)
439 //cout << "XCAFPrs_AISObject: Update called" << endl;
443 // abv: 06 Mar 00: to have good colors
444 Handle(TPrsStd_AISPresentation) prs = Handle(TPrsStd_AISPresentation)::DownCast ( GetOwner() );
445 if ( prs.IsNull() || !prs->HasOwnMaterial() )
446 AIS_Shape::SetMaterial ( Graphic3d_NOM_PLASTIC );
449 if ( ! XCAFDoc_ShapeTool::GetShape ( myLabel, shape ) || shape.IsNull() ) return;
451 // wire,edge,vertex -> pas de HLR + priorite display superieure
452 Standard_Integer TheType = (Standard_Integer)shape.ShapeType();
453 if(TheType>4 && TheType<8) {
454 aPrs->SetVisual(Graphic3d_TOS_ALL);
455 aPrs->SetDisplayPriority(TheType+2);
457 // Shape vide -> Assemblage vide.
458 if (shape.ShapeType() == TopAbs_COMPOUND) {
459 TopoDS_Iterator anExplor (shape);
460 if (!anExplor.More()) {
464 if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
466 // collect information on colored subshapes
468 XCAFPrs_DataMapOfShapeStyle settings;
469 XCAFPrs::CollectStyleSettings ( myLabel, L, settings );
471 //cout << "Styles collected" << endl;
474 // dispatch (sub)shapes by their styles
475 XCAFPrs_DataMapOfStyleShape items;
476 XCAFPrs_Style DefStyle;
477 Quantity_Color White ( Quantity_NOC_WHITE );
478 DefStyle.SetColorSurf ( White );
479 DefStyle.SetColorCurv ( White );
480 XCAFPrs::DispatchStyles ( shape, settings, items, DefStyle );
482 //cout << "Dispatch done" << endl;
485 // add subshapes to presentation (one shape per style)
486 XCAFPrs_DataMapIteratorOfDataMapOfStyleShape it ( items );
488 //Standard_Integer i=1;
490 for ( ; it.More(); it.Next() ) {
491 XCAFPrs_Style s = it.Key();
493 //cout << "Style " << i << ": [" <<
494 // ( s.IsSetColorSurf() ? Quantity_Color::StringName ( s.GetColorSurf().Name() ) : "" ) << ", " <<
495 // ( s.IsSetColorCurv() ? Quantity_Color::StringName ( s.GetColorCurv().Name() ) : "" ) << "]" <<
496 //" --> si_" << i << ( s.IsVisible() ? "" : " <invisible>" ) << endl;
499 if (! s.IsVisible() ) continue;
500 AddStyledItem ( s, it.Value(), aPresentationManager, aPrs, aMode );
503 if ( XCAFPrs::GetViewNameMode() ) {
504 // Displaying Name attributes
506 //cout << "Now display name of shapes" << endl;
508 aPrs->SetDisplayPriority(10);
509 DisplayText (myLabel, aPrs, Attributes()->LengthAspect()->TextAspect(), TopLoc_Location());//no location
512 //cout << "Compute finished" << endl;
515 aPrs->ReCompute(); // for hidden line recomputation if necessary...