Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / java / src / SampleDisplayAnimationPackage / SampleDisplayAnimationPackage.cxx
1 #include <SampleDisplayAnimationPackage.hxx>
2 #include <AIS_InteractiveContext.hxx>
3 #include <AIS_Shape.hxx>
4 #include <TopoDS_Shape.hxx>
5 #include <BRep_Builder.hxx>
6 #include <BRepTools.hxx>
7 #include <TCollection_AsciiString.hxx>
8 #include <gp_Pnt.hxx>
9 #include <gp_Dir.hxx>
10 #include <gp_Vec.hxx>
11 #include <gp_Ax1.hxx>
12 #include <gp_Ax3.hxx>
13 #include <gp_Trsf.hxx>
14
15
16 #include <V3d_Viewer.hxx>
17 #include <V3d_View.hxx>
18
19 #ifdef WNT
20 #include <WNT_Window.hxx>
21 #include <Graphic3d_WNTGraphicDevice.hxx>
22 #else
23 #include <Xw_Window.hxx>
24 #include <Graphic3d_GraphicDevice.hxx>
25 #endif
26
27
28 /*----------------------------------------------------------------------*/
29
30
31 Handle(AIS_Shape) myAISCrankArm;
32 Handle(AIS_Shape) myAISCylinderHead;
33 Handle(AIS_Shape) myAISPropeller;
34 Handle(AIS_Shape) myAISPiston;
35 Handle(AIS_Shape) myAISEngineBlock;
36
37 Standard_Real myDeviation;
38 Standard_Integer myAngle;
39 Standard_Integer thread;
40
41
42
43 //===============================================================
44 // Function name: CreateViewer
45 //===============================================================
46  Handle(V3d_Viewer) SampleDisplayAnimationPackage::CreateViewer(const Standard_ExtString aName) 
47 {
48 #ifdef WNT
49 static Handle(Graphic3d_WNTGraphicDevice) defaultDevice;
50     
51   if (defaultDevice.IsNull()) 
52     defaultDevice = new Graphic3d_WNTGraphicDevice();
53   return new V3d_Viewer(defaultDevice, aName);
54 #else
55 static Handle(Graphic3d_GraphicDevice) defaultDevice;
56     
57   if (defaultDevice.IsNull()) 
58     defaultDevice = new Graphic3d_GraphicDevice("");
59   return new V3d_Viewer(defaultDevice, aName);
60 #endif //WNT
61 }
62
63 //===============================================================
64 // Function name: SetWindow
65 //===============================================================
66  void SampleDisplayAnimationPackage::SetWindow(const Handle(V3d_View)& aView,
67                                                const Standard_Integer hiwin,
68                                                const Standard_Integer lowin) 
69 {
70 #ifdef WNT
71   Handle(Graphic3d_WNTGraphicDevice) d = 
72     Handle(Graphic3d_WNTGraphicDevice)::DownCast(aView->Viewer()->Device());
73   Handle(WNT_Window) w = new WNT_Window(d,hiwin,lowin);
74 #else
75   Handle(Graphic3d_GraphicDevice) d = 
76     Handle(Graphic3d_GraphicDevice)::DownCast(aView->Viewer()->Device());
77   Handle(Xw_Window) w = new Xw_Window(d,hiwin,lowin,Xw_WQ_3DQUALITY);
78 #endif
79   aView->SetWindow(w);
80 }
81
82
83 //===============================================================
84 // Function name: LoadData
85 //===============================================================
86  Standard_Boolean SampleDisplayAnimationPackage::LoadData(const Handle(AIS_InteractiveContext)& aContext,
87                                                           const Standard_CString aPath) 
88 {
89   myDeviation = 0.0008;
90   thread = 4;
91   myAngle = 0;
92
93
94   BRep_Builder B;
95   TopoDS_Shape CrankArm;
96   TopoDS_Shape CylinderHead;
97   TopoDS_Shape Propeller;
98   TopoDS_Shape Piston;
99   TopoDS_Shape EngineBlock;
100   TCollection_AsciiString aName;
101
102   aName = aPath;  aName += "CrankArm.rle";
103   BRepTools::Read(CrankArm, aName.ToCString(), B);
104   aName = aPath;  aName += "CylinderHead.rle";
105   BRepTools::Read(CylinderHead, aName.ToCString(), B);
106   aName = aPath;  aName += "Propeller.rle";
107   BRepTools::Read(Propeller, aName.ToCString(), B);
108   aName = aPath;  aName += "Piston.rle";
109   BRepTools::Read(Piston, aName.ToCString(), B);
110   aName = aPath;  aName += "EngineBlock.rle";
111   BRepTools::Read(EngineBlock, aName.ToCString(), B);
112
113   if (CrankArm.IsNull() || CylinderHead.IsNull() || Propeller.IsNull() ||
114       Piston.IsNull() || EngineBlock.IsNull())
115     return Standard_False;
116
117
118   aContext->SetDeviationCoefficient(myDeviation);
119
120   myAISCylinderHead = new AIS_Shape(CylinderHead);
121   aContext->SetColor(myAISCylinderHead, Quantity_NOC_WHITE);
122   aContext->SetMaterial(myAISCylinderHead, Graphic3d_NOM_PLASTIC);
123
124   myAISEngineBlock = new AIS_Shape(EngineBlock);
125   aContext->SetColor(myAISEngineBlock, Quantity_NOC_WHITE);
126   aContext->SetMaterial(myAISEngineBlock, Graphic3d_NOM_PLASTIC);
127
128   aContext->Display(myAISCylinderHead, 1, -1, Standard_False, Standard_False);
129   aContext->Display(myAISEngineBlock, 1, -1, Standard_False, Standard_False);
130
131   myAISCrankArm = new AIS_Shape(CrankArm);
132   aContext->SetColor(myAISCrankArm, Quantity_NOC_HOTPINK);
133   aContext->SetMaterial(myAISCrankArm, Graphic3d_NOM_PLASTIC);
134
135   myAISPiston = new AIS_Shape(Piston);
136   aContext->SetColor(myAISPiston, Quantity_NOC_WHITE);
137   aContext->SetMaterial(myAISPiston, Graphic3d_NOM_PLASTIC);
138
139   myAISPropeller = new AIS_Shape(Propeller);
140   aContext->SetColor(myAISPropeller, Quantity_NOC_RED);
141   aContext->SetMaterial(myAISPropeller, Graphic3d_NOM_PLASTIC);
142
143   aContext->Display(myAISCrankArm, 1, -1, Standard_False, Standard_False);
144   aContext->Display(myAISPropeller, 1, -1, Standard_False, Standard_False);
145   aContext->Display(myAISPiston, 1, -1, Standard_True, Standard_False);
146
147   return Standard_True;
148 }
149
150 //===============================================================
151 // Function name: ChangePosition
152 //===============================================================
153  void SampleDisplayAnimationPackage::ChangePosition(const Handle(AIS_InteractiveContext)& aContext) 
154 {
155   Standard_Real angleA;
156   Standard_Real angleB;
157   Standard_Real X;
158   gp_Ax1 ax1(gp_Pnt(0,0,0), gp_Vec(0,0,1));
159
160   myAngle++;
161
162   angleA = thread*myAngle*PI/180;
163   X = Sin(angleA)*3/8;
164   angleB = atan(X/Sqrt(-X*X+1));
165   Standard_Real decal(25*0.6);
166
167
168   // Build a transformation on the display
169   gp_Trsf aPropellerTrsf;
170   aPropellerTrsf.SetRotation(ax1, angleA);
171   aContext->SetLocation(myAISPropeller, aPropellerTrsf);
172
173   gp_Ax3 base(gp_Pnt(3*decal*(1-Cos(angleA)), -3*decal*Sin(angleA),0),
174               gp_Vec(0,0,1), gp_Vec(1,0,0));
175   gp_Trsf aCrankArmTrsf;
176   aCrankArmTrsf.SetTransformation(base.Rotated(gp_Ax1(gp_Pnt(3*decal,0,0), gp_Dir(0,0,1)), angleB));
177   aContext->SetLocation(myAISCrankArm, aCrankArmTrsf);
178
179   gp_Trsf aPistonTrsf;
180   aPistonTrsf.SetTranslation(gp_Vec(-3*decal*(1-Cos(angleA))-8*decal*(1-Cos(angleB)),0,0));
181   aContext->SetLocation(myAISPiston, aPistonTrsf);
182
183   aContext->UpdateCurrentViewer();
184 }
185
186 //===============================================================
187 // Function name: GetDeviationCoefficient
188 //===============================================================
189  Standard_Real SampleDisplayAnimationPackage::GetDeviationCoefficient() 
190 {
191   return myDeviation;
192 }
193
194 //===============================================================
195 // Function name: SetDeviationCoefficient
196 //===============================================================
197  void SampleDisplayAnimationPackage::SetDeviationCoefficient(const Handle(AIS_InteractiveContext)& aContext,
198                                                              const Standard_Real aValue) 
199 {
200   myDeviation = aValue;
201
202   aContext->SetDeviationCoefficient(myDeviation);
203   TopoDS_Shape Propeller = myAISPropeller->Shape();
204   BRepTools::Clean(Propeller);
205
206   myAISPropeller->Set(Propeller);
207   aContext->Deactivate(myAISPropeller);
208   aContext->Redisplay(myAISPropeller);
209 }
210
211 //===============================================================
212 // Function name: GetAngleIncrement
213 //===============================================================
214  Standard_Integer SampleDisplayAnimationPackage::GetAngleIncrement() 
215 {
216   return thread;
217 }
218
219 //===============================================================
220 // Function name: SetAngleIncrement
221 //===============================================================
222  void SampleDisplayAnimationPackage::SetAngleIncrement(const Standard_Integer aValue) 
223 {
224   thread = aValue;
225 }
226
227 //===============================================================
228 // Function name: SaveImage
229 //===============================================================
230 #ifndef WNT
231  Standard_Boolean SampleDisplayAnimationPackage::SaveImage(const Standard_CString ,
232                                                            const Standard_CString ,
233                                                            const Handle(V3d_View)& ) 
234 {
235 #else
236  Standard_Boolean SampleDisplayAnimationPackage::SaveImage(const Standard_CString aFileName,
237                                                            const Standard_CString aFormat,
238                                                            const Handle(V3d_View)& aView) 
239 {
240   Handle(Aspect_Window) anAspectWindow = aView->Window();
241   Handle(WNT_Window) aWNTWindow = Handle(WNT_Window)::DownCast(anAspectWindow);
242
243   if (aFormat == "bmp") aWNTWindow->SetOutputFormat(WNT_TOI_BMP);
244   if (aFormat == "gif") aWNTWindow->SetOutputFormat(WNT_TOI_GIF);
245   if (aFormat == "xwd") aWNTWindow->SetOutputFormat(WNT_TOI_XWD);
246
247   aWNTWindow->Dump(aFileName);
248 #endif
249   return Standard_True;
250 }
251