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>
13 #include <gp_Trsf.hxx>
16 #include <V3d_Viewer.hxx>
17 #include <V3d_View.hxx>
20 #include <WNT_Window.hxx>
21 #include <Graphic3d_WNTGraphicDevice.hxx>
23 #include <Xw_Window.hxx>
24 #include <Graphic3d_GraphicDevice.hxx>
28 /*----------------------------------------------------------------------*/
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;
37 Standard_Real myDeviation;
38 Standard_Integer myAngle;
39 Standard_Integer thread;
43 //===============================================================
44 // Function name: CreateViewer
45 //===============================================================
46 Handle(V3d_Viewer) SampleDisplayAnimationPackage::CreateViewer(const Standard_ExtString aName)
49 static Handle(Graphic3d_WNTGraphicDevice) defaultDevice;
51 if (defaultDevice.IsNull())
52 defaultDevice = new Graphic3d_WNTGraphicDevice();
53 return new V3d_Viewer(defaultDevice, aName);
55 static Handle(Graphic3d_GraphicDevice) defaultDevice;
57 if (defaultDevice.IsNull())
58 defaultDevice = new Graphic3d_GraphicDevice("");
59 return new V3d_Viewer(defaultDevice, aName);
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)
71 Handle(Graphic3d_WNTGraphicDevice) d =
72 Handle(Graphic3d_WNTGraphicDevice)::DownCast(aView->Viewer()->Device());
73 Handle(WNT_Window) w = new WNT_Window(d,hiwin,lowin);
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);
83 //===============================================================
84 // Function name: LoadData
85 //===============================================================
86 Standard_Boolean SampleDisplayAnimationPackage::LoadData(const Handle(AIS_InteractiveContext)& aContext,
87 const Standard_CString aPath)
95 TopoDS_Shape CrankArm;
96 TopoDS_Shape CylinderHead;
97 TopoDS_Shape Propeller;
99 TopoDS_Shape EngineBlock;
100 TCollection_AsciiString aName;
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);
113 if (CrankArm.IsNull() || CylinderHead.IsNull() || Propeller.IsNull() ||
114 Piston.IsNull() || EngineBlock.IsNull())
115 return Standard_False;
118 aContext->SetDeviationCoefficient(myDeviation);
120 myAISCylinderHead = new AIS_Shape(CylinderHead);
121 aContext->SetColor(myAISCylinderHead, Quantity_NOC_WHITE);
122 aContext->SetMaterial(myAISCylinderHead, Graphic3d_NOM_PLASTIC);
124 myAISEngineBlock = new AIS_Shape(EngineBlock);
125 aContext->SetColor(myAISEngineBlock, Quantity_NOC_WHITE);
126 aContext->SetMaterial(myAISEngineBlock, Graphic3d_NOM_PLASTIC);
128 aContext->Display(myAISCylinderHead, 1, -1, Standard_False, Standard_False);
129 aContext->Display(myAISEngineBlock, 1, -1, Standard_False, Standard_False);
131 myAISCrankArm = new AIS_Shape(CrankArm);
132 aContext->SetColor(myAISCrankArm, Quantity_NOC_HOTPINK);
133 aContext->SetMaterial(myAISCrankArm, Graphic3d_NOM_PLASTIC);
135 myAISPiston = new AIS_Shape(Piston);
136 aContext->SetColor(myAISPiston, Quantity_NOC_WHITE);
137 aContext->SetMaterial(myAISPiston, Graphic3d_NOM_PLASTIC);
139 myAISPropeller = new AIS_Shape(Propeller);
140 aContext->SetColor(myAISPropeller, Quantity_NOC_RED);
141 aContext->SetMaterial(myAISPropeller, Graphic3d_NOM_PLASTIC);
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);
147 return Standard_True;
150 //===============================================================
151 // Function name: ChangePosition
152 //===============================================================
153 void SampleDisplayAnimationPackage::ChangePosition(const Handle(AIS_InteractiveContext)& aContext)
155 Standard_Real angleA;
156 Standard_Real angleB;
158 gp_Ax1 ax1(gp_Pnt(0,0,0), gp_Vec(0,0,1));
162 angleA = thread*myAngle*PI/180;
164 angleB = atan(X/Sqrt(-X*X+1));
165 Standard_Real decal(25*0.6);
168 // Build a transformation on the display
169 gp_Trsf aPropellerTrsf;
170 aPropellerTrsf.SetRotation(ax1, angleA);
171 aContext->SetLocation(myAISPropeller, aPropellerTrsf);
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);
180 aPistonTrsf.SetTranslation(gp_Vec(-3*decal*(1-Cos(angleA))-8*decal*(1-Cos(angleB)),0,0));
181 aContext->SetLocation(myAISPiston, aPistonTrsf);
183 aContext->UpdateCurrentViewer();
186 //===============================================================
187 // Function name: GetDeviationCoefficient
188 //===============================================================
189 Standard_Real SampleDisplayAnimationPackage::GetDeviationCoefficient()
194 //===============================================================
195 // Function name: SetDeviationCoefficient
196 //===============================================================
197 void SampleDisplayAnimationPackage::SetDeviationCoefficient(const Handle(AIS_InteractiveContext)& aContext,
198 const Standard_Real aValue)
200 myDeviation = aValue;
202 aContext->SetDeviationCoefficient(myDeviation);
203 TopoDS_Shape Propeller = myAISPropeller->Shape();
204 BRepTools::Clean(Propeller);
206 myAISPropeller->Set(Propeller);
207 aContext->Deactivate(myAISPropeller);
208 aContext->Redisplay(myAISPropeller);
211 //===============================================================
212 // Function name: GetAngleIncrement
213 //===============================================================
214 Standard_Integer SampleDisplayAnimationPackage::GetAngleIncrement()
219 //===============================================================
220 // Function name: SetAngleIncrement
221 //===============================================================
222 void SampleDisplayAnimationPackage::SetAngleIncrement(const Standard_Integer aValue)
227 //===============================================================
228 // Function name: SaveImage
229 //===============================================================
231 Standard_Boolean SampleDisplayAnimationPackage::SaveImage(const Standard_CString ,
232 const Standard_CString ,
233 const Handle(V3d_View)& )
236 Standard_Boolean SampleDisplayAnimationPackage::SaveImage(const Standard_CString aFileName,
237 const Standard_CString aFormat,
238 const Handle(V3d_View)& aView)
240 Handle(Aspect_Window) anAspectWindow = aView->Window();
241 Handle(WNT_Window) aWNTWindow = Handle(WNT_Window)::DownCast(anAspectWindow);
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);
247 aWNTWindow->Dump(aFileName);
249 return Standard_True;