0028463: OCAF loses an interactive object after copying
[occt.git] / src / TPrsStd / TPrsStd_GeometryDriver.cxx
1 // Created on: 1999-09-30
2 // Created by: Sergey RUIN
3 // Copyright (c) 1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <AIS_Circle.hxx>
19 #include <AIS_InteractiveContext.hxx>
20 #include <AIS_InteractiveObject.hxx>
21 #include <AIS_Line.hxx>
22 #include <AIS_Point.hxx>
23 #include <AIS_Shape.hxx>
24 #include <BRepBuilderAPI_MakeEdge.hxx>
25 #include <Geom_CartesianPoint.hxx>
26 #include <Geom_Circle.hxx>
27 #include <Geom_Line.hxx>
28 #include <gp_Circ.hxx>
29 #include <gp_Elips.hxx>
30 #include <gp_Lin.hxx>
31 #include <gp_Pnt.hxx>
32 #include <Standard_Type.hxx>
33 #include <TDataXtd.hxx>
34 #include <TDataXtd_Geometry.hxx>
35 #include <TDF_Label.hxx>
36 #include <TNaming_NamedShape.hxx>
37 #include <TNaming_Tool.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TPrsStd_GeometryDriver.hxx>
41
42 IMPLEMENT_STANDARD_RTTIEXT(TPrsStd_GeometryDriver,TPrsStd_Driver)
43
44 //=======================================================================
45 //function :
46 //purpose  : 
47 //=======================================================================
48 TPrsStd_GeometryDriver::TPrsStd_GeometryDriver()
49 {
50 }
51
52
53 //=======================================================================
54 //function :
55 //purpose  : 
56 //=======================================================================
57 Standard_Boolean TPrsStd_GeometryDriver::Update(const TDF_Label& aLabel,
58                                                Handle(AIS_InteractiveObject)& anAISObject) 
59 {
60   Handle(TDataXtd_Geometry) aGeom;
61   Handle(TNaming_NamedShape) NS;
62   TDataXtd_GeometryEnum GeomType;
63
64   if (!aLabel.FindAttribute(TDataXtd_Geometry::GetID(), aGeom)) {
65     if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), NS) ) {
66       GeomType = TDataXtd_Geometry::Type(aLabel);
67     }
68     else {
69       return Standard_False; 
70     }
71   }
72   else {
73     GeomType = aGeom->GetType();
74   }
75   
76   switch (GeomType)  {
77   case  TDataXtd_POINT   :
78     {
79       gp_Pnt pt; 
80       if (!TDataXtd_Geometry::Point(aLabel,pt)) return Standard_False;
81       Handle(Geom_Point) apt = new Geom_CartesianPoint(pt);
82       Handle(AIS_Point) ais1;
83       if( anAISObject.IsNull() ) ais1 = new AIS_Point(apt);
84       else {    
85         ais1 = Handle(AIS_Point)::DownCast(anAISObject);
86         if (ais1.IsNull()) 
87           ais1 = new AIS_Point(apt);
88         else {
89           ais1->SetComponent(apt);
90           ais1->ResetTransformation();
91           ais1->SetToUpdate();
92           ais1->UpdateSelection();
93         }
94         
95       }
96       anAISObject = ais1;
97       anAISObject->SetColor(Quantity_NOC_RED);
98     }
99   break;
100   case  TDataXtd_LINE   :
101     {
102       gp_Lin ln; 
103       if (!TDataXtd_Geometry::Line(aLabel,ln)) return Standard_False;
104       Handle(Geom_Line) aln = new Geom_Line(ln);
105       Handle(AIS_Line) ais2;
106       if( anAISObject.IsNull() ) ais2 = new AIS_Line(aln);
107       else {
108         ais2 = Handle(AIS_Line)::DownCast(anAISObject);
109         if (ais2.IsNull()) 
110           ais2 = new AIS_Line(aln);
111         else {
112           ais2->SetLine(aln);
113           ais2->ResetTransformation();
114           ais2->SetToUpdate();
115           ais2->UpdateSelection();
116         }
117       }
118       anAISObject = ais2;
119       anAISObject->SetColor(Quantity_NOC_RED);
120       anAISObject->SetInfiniteState(Standard_True);
121       break;
122     }
123   case  TDataXtd_CIRCLE   :
124     {
125       Handle(AIS_Line) ais2;
126       gp_Circ cir; 
127       if (!TDataXtd_Geometry::Circle(aLabel,cir)) return Standard_False; 
128       Handle(Geom_Circle) acir = new Geom_Circle(cir);
129       Handle(AIS_Circle) ais3;
130       if (anAISObject.IsNull()) ais3 = new AIS_Circle(acir);
131       else {
132         ais3 = Handle(AIS_Circle)::DownCast(anAISObject);
133         if (ais3.IsNull()) 
134           ais3 = new AIS_Circle(acir);
135         else {
136           ais3->SetCircle(acir);
137           ais3->ResetTransformation();
138           ais3->SetToUpdate();
139           ais3->UpdateSelection();
140         }
141       }
142       anAISObject = ais3;
143       anAISObject->SetColor(Quantity_NOC_RED);
144       break;
145     }
146   case  TDataXtd_ELLIPSE   :
147     {
148       gp_Elips elp; 
149       if (!TDataXtd_Geometry::Ellipse(aLabel, elp)) return Standard_False;
150       BRepBuilderAPI_MakeEdge mkEdge(elp);
151       if( !mkEdge.IsDone() ) return Standard_False;
152       Handle(AIS_Shape) ais;
153       if (anAISObject.IsNull()) ais = new AIS_Shape(mkEdge);
154       else {
155           ais = Handle(AIS_Shape)::DownCast(anAISObject);
156           if (ais.IsNull()) 
157             ais = new AIS_Shape(mkEdge);
158           else {
159             ais->ResetTransformation();
160             ais->Set(mkEdge);
161             ais->SetToUpdate();
162             ais->UpdateSelection();
163           }
164       }   
165       anAISObject = ais;
166       anAISObject->SetColor(Quantity_NOC_RED);
167       break;
168     }
169   default:
170     return Standard_False;
171   }
172   
173   return Standard_True;
174 }
175