0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / AIS / AIS_Axis.cxx
1 // Created on: 1995-08-09
2 // Created by: Arnaud BOUZY/Odile Olivier
3 // Copyright (c) 1995-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 #include <AIS_Axis.hxx>
18
19 #include <Aspect_TypeOfLine.hxx>
20 #include <DsgPrs_XYZAxisPresentation.hxx>
21 #include <Geom_Axis1Placement.hxx>
22 #include <Geom_Axis2Placement.hxx>
23 #include <Geom_Line.hxx>
24 #include <GeomAdaptor_Curve.hxx>
25 #include <gp_Ax1.hxx>
26 #include <gp_Ax2.hxx>
27 #include <Graphic3d_AspectLine3d.hxx>
28 #include <Graphic3d_Structure.hxx>
29 #include <Prs3d_DatumAspect.hxx>
30 #include <Prs3d_Drawer.hxx>
31 #include <Prs3d_LineAspect.hxx>
32 #include <Prs3d_Presentation.hxx>
33 #include <Quantity_Color.hxx>
34 #include <Select3D_SensitiveSegment.hxx>
35 #include <SelectMgr_EntityOwner.hxx>
36 #include <SelectMgr_Selection.hxx>
37 #include <StdPrs_Curve.hxx>
38 #include <TColgp_Array1OfPnt.hxx>
39 #include <TopoDS.hxx>
40 #include <UnitsAPI.hxx>
41
42 IMPLEMENT_STANDARD_RTTIEXT(AIS_Axis,AIS_InteractiveObject)
43
44 //=======================================================================
45 //function : AIS_Axis
46 //purpose  :
47 //=======================================================================
48 AIS_Axis::AIS_Axis(const Handle(Geom_Line)& aComponent):
49 myComponent(aComponent),
50 myTypeOfAxis(AIS_TOAX_Unknown),
51 myIsXYZAxis(Standard_False)
52 {
53   myDrawer->SetLineAspect(new Prs3d_LineAspect
54      (Quantity_NOC_RED,Aspect_TOL_DOTDASH,1.));
55   SetInfiniteState();
56
57   gp_Dir thedir = myComponent->Position().Direction();
58   gp_Pnt loc = myComponent->Position().Location();
59 //POP  Standard_Real aLength = UnitsAPI::CurrentToLS (250000. ,"LENGTH");
60   Standard_Real aLength = UnitsAPI::AnyToLS(250000., "mm");
61   myPfirst = loc.XYZ() + aLength*thedir.XYZ();
62   myPlast = loc.XYZ() - aLength*thedir.XYZ();
63 }
64
65 //=======================================================================
66 //function : AIS_Axis
67 //purpose  :  Xaxis, YAxis, ZAxis
68 //=======================================================================
69 AIS_Axis::AIS_Axis(const Handle(Geom_Axis2Placement)& aComponent,
70                    const AIS_TypeOfAxis anAxisType):
71 myAx2(aComponent),
72 myTypeOfAxis(anAxisType),
73 myIsXYZAxis(Standard_True)
74 {
75   Handle (Prs3d_DatumAspect) DA = new Prs3d_DatumAspect();
76 //POP  Standard_Real aLength = UnitsAPI::CurrentToLS (100. ,"LENGTH"); 
77   Standard_Real aLength;
78   try {
79     aLength = UnitsAPI::AnyToLS(100. ,"mm");
80   } catch (Standard_Failure const&) {
81     aLength = 0.1;
82   }
83   DA->SetAxisLength(aLength,aLength,aLength);
84   Quantity_Color col (Quantity_NOC_TURQUOISE);
85   DA->LineAspect(Prs3d_DP_XAxis)->SetColor(col);
86   DA->LineAspect(Prs3d_DP_YAxis)->SetColor(col);
87   DA->LineAspect(Prs3d_DP_ZAxis)->SetColor(col);
88   myDrawer->SetDatumAspect(DA); 
89   
90   ComputeFields();
91 }
92
93 //=======================================================================
94 //function : AIS_Axis
95 //purpose  : 
96 //=======================================================================
97 AIS_Axis::AIS_Axis(const Handle(Geom_Axis1Placement)& anAxis)
98 :myComponent(new Geom_Line(anAxis->Ax1())),
99  myTypeOfAxis(AIS_TOAX_Unknown),
100  myIsXYZAxis(Standard_False)
101 {
102   myDrawer->SetLineAspect(new Prs3d_LineAspect(Quantity_NOC_RED,Aspect_TOL_DOTDASH,1.));
103   SetInfiniteState();
104
105   gp_Dir thedir = myComponent->Position().Direction();
106   gp_Pnt loc = myComponent->Position().Location();
107 //POP  Standard_Real aLength = UnitsAPI::CurrentToLS (250000. ,"LENGTH");
108   Standard_Real aLength = UnitsAPI::AnyToLS(250000. ,"mm"); 
109   myPfirst = loc.XYZ() + aLength*thedir.XYZ();
110   myPlast = loc.XYZ() - aLength*thedir.XYZ();
111 }
112
113
114 //=======================================================================
115 //function : SetComponent
116 //purpose  : 
117 //=======================================================================
118
119 void AIS_Axis::SetComponent(const Handle(Geom_Line)& aComponent)
120 {
121   myComponent = aComponent;
122   myTypeOfAxis = AIS_TOAX_Unknown;
123   myIsXYZAxis = Standard_False;
124   SetInfiniteState();
125
126   gp_Dir thedir = myComponent->Position().Direction();
127   gp_Pnt loc = myComponent->Position().Location();
128 //POP  Standard_Real aLength = UnitsAPI::CurrentToLS (250000. ,"LENGTH");
129   Standard_Real aLength = UnitsAPI::AnyToLS(250000. ,"mm");
130   myPfirst = loc.XYZ() + aLength*thedir.XYZ();
131   myPlast = loc.XYZ() - aLength*thedir.XYZ();
132 }
133
134
135
136 //=======================================================================
137 //function : SetAxis2Placement
138 //purpose  : 
139 //=======================================================================
140
141 void AIS_Axis::SetAxis2Placement(const Handle(Geom_Axis2Placement)& aComponent,
142                                  const AIS_TypeOfAxis anAxisType)
143 {
144   myAx2 = aComponent;
145   myTypeOfAxis = anAxisType;
146   myIsXYZAxis = Standard_True;
147   ComputeFields();
148 }
149
150 //=======================================================================
151 //function : SetAxis1Placement
152 //purpose  : 
153 //=======================================================================
154
155 void AIS_Axis::SetAxis1Placement(const Handle(Geom_Axis1Placement)& anAxis)
156 {
157   SetComponent(new Geom_Line(anAxis->Ax1()));
158 }
159
160 //=======================================================================
161 //function : Compute
162 //purpose  : 
163 //=======================================================================
164 void AIS_Axis::Compute(const Handle(PrsMgr_PresentationManager3d)&,
165                        const Handle(Prs3d_Presentation)& aPresentation, 
166                        const Standard_Integer)
167 {
168   aPresentation->SetInfiniteState (myInfiniteState);
169
170   aPresentation->SetDisplayPriority(5);
171   if (!myIsXYZAxis ){
172     GeomAdaptor_Curve curv(myComponent);
173     StdPrs_Curve::Add(aPresentation,curv,myDrawer);
174   }
175   else
176   {
177     DsgPrs_XYZAxisPresentation::Add (aPresentation,myLineAspect,myDir,myVal,
178                                      myDrawer->DatumAspect()->ToDrawLabels() ? myText : "",
179                                      myPfirst, myPlast);
180   }
181
182 }
183
184 //=======================================================================
185 //function : ComputeSelection
186 //purpose  : 
187 //=======================================================================
188
189 void AIS_Axis::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
190                                 const Standard_Integer)
191 {
192   Handle(SelectMgr_EntityOwner) eown = new SelectMgr_EntityOwner (this, 3);
193   Handle(Select3D_SensitiveSegment) seg = new Select3D_SensitiveSegment(eown,
194                                                                         myPfirst,
195                                                                         myPlast);
196   aSelection->Add(seg);
197 }
198
199 //=======================================================================
200 //function : SetColor
201 //purpose  :
202 //=======================================================================
203 void AIS_Axis::SetColor(const Quantity_Color &aCol)
204 {
205   hasOwnColor=Standard_True;
206   myDrawer->SetColor (aCol);
207   myDrawer->LineAspect()->SetColor(aCol);
208   
209   const Handle(Prs3d_DatumAspect)& DA = myDrawer->DatumAspect();
210   DA->LineAspect(Prs3d_DP_XAxis)->SetColor(aCol);
211   DA->LineAspect(Prs3d_DP_YAxis)->SetColor(aCol);
212   DA->LineAspect(Prs3d_DP_ZAxis)->SetColor(aCol);
213   SynchronizeAspects();
214 }
215
216 //=======================================================================
217 //function : SetWidth 
218 //purpose  : 
219 //=======================================================================
220 void AIS_Axis::SetWidth(const Standard_Real aValue)
221 {
222   if(aValue<0.0) return;
223   if(aValue==0) UnsetWidth();
224   
225   myDrawer->LineAspect()->SetWidth(aValue);
226   
227   const Handle(Prs3d_DatumAspect)& DA = myDrawer->DatumAspect();
228   DA->LineAspect(Prs3d_DP_XAxis)->SetWidth(aValue);
229   DA->LineAspect(Prs3d_DP_YAxis)->SetWidth(aValue);
230   DA->LineAspect(Prs3d_DP_ZAxis)->SetWidth(aValue);
231   SynchronizeAspects();
232 }
233
234 //=======================================================================
235 //function : ComputeFields
236 //purpose  : 
237 //=======================================================================
238 void AIS_Axis::ComputeFields()
239 {
240   if (myIsXYZAxis){
241     // calcul de myPFirst,myPlast
242     Handle(Prs3d_DatumAspect) DA = myDrawer->DatumAspect();
243     gp_Ax2 anAxis = myAx2->Ax2();
244     const gp_Pnt& Orig = anAxis.Location();
245     const gp_Dir& oX   = anAxis.XDirection();
246     const gp_Dir& oY   = anAxis.YDirection();
247     const gp_Dir& oZ   = anAxis.Direction();
248     Standard_Real xo,yo,zo,x = 0.,y = 0.,z = 0.;
249     Orig.Coord(xo,yo,zo);
250     myPfirst.SetCoord(xo,yo,zo);
251     
252     switch (myTypeOfAxis) {
253     case AIS_TOAX_XAxis:
254       {
255         oX.Coord(x,y,z);
256     myVal = DA->AxisLength(Prs3d_DP_XAxis);
257         myDir = oX;
258         myLineAspect = DA->LineAspect(Prs3d_DP_XAxis);
259         myText = Standard_CString ("X");
260         break;
261       }
262     case AIS_TOAX_YAxis:
263       {
264         oY.Coord(x,y,z);
265         myVal = DA->AxisLength(Prs3d_DP_YAxis);
266         myDir = oY;
267         myLineAspect = DA->LineAspect(Prs3d_DP_YAxis);
268         myText = Standard_CString ("Y");
269         break;
270       }
271     case AIS_TOAX_ZAxis:
272       {
273         oZ.Coord(x,y,z); 
274         myVal = DA->AxisLength(Prs3d_DP_ZAxis);
275         myDir = oZ;
276         myLineAspect = DA->LineAspect(Prs3d_DP_ZAxis);
277         myText = Standard_CString ("Z");
278         break;
279       }
280      default:
281       break;
282     }
283     
284     myComponent = new Geom_Line(Orig,myDir);
285     x = xo + x*myVal;   y = yo + y*myVal;  z = zo + z*myVal;
286     myPlast.SetCoord(x,y,z);
287     SetInfiniteState();
288   }
289 }
290
291 //=======================================================================
292 //function : AcceptDisplayMode
293 //purpose  : 
294 //=======================================================================
295
296  Standard_Boolean  AIS_Axis::
297 AcceptDisplayMode(const Standard_Integer aMode) const
298 {return aMode == 0;}
299
300 //=======================================================================
301 //function : UnsetColor
302 //purpose  : 
303 //=======================================================================
304 void AIS_Axis::UnsetColor()
305 {
306   myDrawer->LineAspect()->SetColor(Quantity_NOC_RED);
307   hasOwnColor = Standard_False;
308
309   myDrawer->DatumAspect()->LineAspect(Prs3d_DP_XAxis)->SetColor(Quantity_NOC_TURQUOISE);
310   myDrawer->DatumAspect()->LineAspect(Prs3d_DP_YAxis)->SetColor(Quantity_NOC_TURQUOISE);
311   myDrawer->DatumAspect()->LineAspect(Prs3d_DP_ZAxis)->SetColor(Quantity_NOC_TURQUOISE);
312   SynchronizeAspects();
313 }
314 //=======================================================================
315 //function : UnsetWidth
316 //purpose  : 
317 //=======================================================================
318
319 void AIS_Axis::UnsetWidth()
320 {
321   myOwnWidth = 0.0f;
322   myDrawer->LineAspect()->SetWidth(1.);
323   myDrawer->DatumAspect()->LineAspect(Prs3d_DP_XAxis)->SetWidth(1.);
324   myDrawer->DatumAspect()->LineAspect(Prs3d_DP_YAxis)->SetWidth(1.);
325   myDrawer->DatumAspect()->LineAspect(Prs3d_DP_ZAxis)->SetWidth(1.);
326   SynchronizeAspects();
327 }