0022898: IGES import fails in german environment
[occt.git] / src / DDataStd / DDataStd_DatumCommands.cxx
1 // Created on: 1997-07-30
2 // Created by: Denis PASCAL
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 #include <DDataStd.hxx>
23 #include <DDF.hxx>
24 #include <Draw_Interpretor.hxx>
25 #include <Draw_Appli.hxx>
26
27 #include <TDF_Data.hxx>
28 #include <TDF_Label.hxx>
29
30 #include <DBRep.hxx>
31 #include <DrawTrSurf.hxx>
32
33 #include <TopAbs.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Wire.hxx>
38 #include <TopoDS_Face.hxx>
39 #include <TopoDS_Shell.hxx>
40 #include <TopoDS_Solid.hxx>
41
42 #include <Geom_Line.hxx>
43 #include <Geom_Plane.hxx>
44
45 #include <gp_Pnt.hxx>
46 #include <gp_Lin.hxx>
47 #include <gp_Pln.hxx>
48 #include <gp_Dir.hxx>
49 #include <gp_Ax3.hxx>
50
51 #include <TDataXtd_Geometry.hxx>
52 #include <TDataXtd_Point.hxx>
53 #include <TDataXtd_Axis.hxx>
54 #include <TDataXtd_Plane.hxx>
55
56 #include <TNaming_Builder.hxx>
57
58 #include <DDataStd_DrawPresentation.hxx>
59
60 // Unused :
61 #ifdef BLABLA_DEB
62 static void Location (Standard_Integer nb, const char** arg, gp_Ax2& Loc) {
63
64   if (nb == 9) {
65     Standard_Real X  = Draw::Atof(arg[3]);
66     Standard_Real Y  = Draw::Atof(arg[4]);
67     Standard_Real Z  = Draw::Atof(arg[5]);
68     Standard_Real DX = Draw::Atof(arg[6]);
69     Standard_Real DY = Draw::Atof(arg[7]);
70     Standard_Real DZ = Draw::Atof(arg[8]);
71     Loc = gp_Ax2 (gp_Pnt(X,Y,Z), gp_Dir(DX,DY,DZ));
72   }  
73 }
74 #endif
75
76
77 //=======================================================================
78 //function : DDataStd_SetPoint
79 //purpose  : SetPoint (DF, entry, [drawpoint])
80 //=======================================================================
81
82 static Standard_Integer DDataStd_SetPoint (Draw_Interpretor& di,
83                                            Standard_Integer nb, 
84                                            const char** arg) 
85
86   if (nb < 3) return 1;
87   TDF_Label L;
88   Handle(TDF_Data) DF;
89   if (!DDF::GetDF (arg[1], DF)) return 1;
90   DDF::AddLabel (DF, arg[2], L);
91   if (nb == 3) {
92     TDataXtd_Point::Set (L);  
93   }
94   else if (nb == 4) { 
95     gp_Pnt p;
96     if (DrawTrSurf::GetPoint (arg[3],p)) {
97       TDataXtd_Point::Set (L,p);
98     }
99     else {
100       di << "DDataStd_SetPoint : not a point" << "\n";
101       return 1;
102     }
103   }  
104   DDataStd_DrawPresentation::Display (L);
105   return 0;
106 }
107
108
109 //=======================================================================
110 //function : DDataStd_SetAxis
111 //purpose  : SetAxis (DF, entry, drawline])
112 //=======================================================================
113
114 static Standard_Integer DDataStd_SetAxis (Draw_Interpretor& di,
115                                           Standard_Integer nb, 
116                                           const char** arg) 
117 {
118   if (nb < 3) return 1;
119   TDF_Label L;
120   Handle(TDF_Data) DF;
121   if (!DDF::GetDF (arg[1], DF)) return 1;
122   DDF::AddLabel (DF, arg[2], L);
123   if (nb == 3) {
124     TDataXtd_Axis::Set (L);  
125   }
126   else if (nb == 4) { 
127     Handle(Geom_Line) LINE =  Handle(Geom_Line)::DownCast(DrawTrSurf::Get (arg[3]));
128     if (LINE.IsNull()) {
129       di << "DDataStd_SetAxis : not a line" << "\n";
130       return 1;
131     }
132     TDataXtd_Axis::Set (L,LINE->Lin()); 
133   }  
134   DDataStd_DrawPresentation::Display (L);    
135   return 0;
136 }
137
138
139
140 //=======================================================================
141 //function : DDataStd_SetPlane
142 //purpose  : SetPlane (DF, entry, [drawplane])
143 //=======================================================================
144
145 static Standard_Integer DDataStd_SetPlane (Draw_Interpretor& di,
146                                            Standard_Integer nb, 
147                                            const char** arg) 
148 {   
149   if (nb < 3) return 1;
150   TDF_Label L;
151   Handle(TDF_Data) DF;
152   if (!DDF::GetDF (arg[1], DF)) return 1;
153   DDF::AddLabel (DF, arg[2], L);
154   if (nb == 3) {
155     TDataXtd_Plane::Set(L);  
156   }
157   else if (nb == 4) {
158     Handle(Geom_Plane) PLANE =  Handle(Geom_Plane)::DownCast(DrawTrSurf::Get (arg[3]));
159     if (PLANE.IsNull()) {
160       di << "DDataStd_SetPlane : not a plane" << "\n";
161       return 1;
162     }
163     TDataXtd_Plane::Set (L,PLANE->Pln()); 
164   }  
165   DDataStd_DrawPresentation::Display (L);
166   return 0;
167 }
168
169
170 //=======================================================================
171 //function : DDataStd_SetMove
172 //purpose  : 
173 //=======================================================================
174
175 // static Standard_Integer DDataStd_SetMove (Draw_Interpretor&,
176 //                                        Standard_Integer nb, 
177 //                                        const char** arg) 
178 // {  
179 //   if (nb < 3) return 1;
180 //   TDF_Label L;
181 //   Handle(TDF_Data) DF;
182 //   if (!DDF::GetDF (arg[1], DF)) return 1;
183 //   if (!DDF::FindLabel(DF, arg[2], L)) return 1;
184
185 //   TopLoc_Location Loc;
186 //   if (nb == 4) {
187 //     TopoDS_Shape S = DBRep::Get(arg[3]);
188 //     if (S.IsNull()) return 1;
189 //     Loc = S.Location();
190 //   }
191   
192 //   else {
193 //     if (nb < 6) return 1;
194     
195 //     gp_Trsf T;
196 //     Standard_Real x = Draw::Atof(arg[3]);
197 //     Standard_Real y = Draw::Atof(arg[4]);
198 //     Standard_Real z = Draw::Atof(arg[5]);
199     
200 //     if (nb == 6) {
201 //       T.SetTranslation(gp_Vec(x,y,z));
202 //     }
203 //     else if (nb < 10)
204 //       return 1;
205 //     else {
206 //       Standard_Real dx = Draw::Atof(arg[6]);
207 //       Standard_Real dy = Draw::Atof(arg[7]);
208 //       Standard_Real dz = Draw::Atof(arg[8]);
209 //       Standard_Real ang = Draw::Atof(arg[9]);
210 //       T.SetRotation(gp_Ax1(gp_Pnt(x,y,z),
211 //                         gp_Vec(dx,dy,dz)),
212 //                  ang * (M_PI / 180.0));
213 //     }
214 //     Loc = T;
215 //   }
216 //   TDataStd_CoordSys::Move(L,Loc);
217 //   return 0;
218 // }
219
220
221
222 //=======================================================================
223 //function : DDataStd_GetPoint
224 //purpose  : GetPoint (DF, entry, [drawname])
225 //=======================================================================
226
227 static Standard_Integer DDataStd_GetPoint (Draw_Interpretor&,
228                                            Standard_Integer nb, 
229                                            const char** arg) 
230
231   if (nb < 3) return 1;
232   Handle(TDF_Data) DF;
233   if (!DDF::GetDF (arg[1], DF)) return 1;
234   Handle(TDataXtd_Point) A;
235   gp_Pnt P;
236   if (!DDF::Find (DF, arg[2], TDataXtd_Point::GetID(), A)) return 1;
237   if (TDataXtd_Geometry::Point(A->Label(), P)) {
238     if (nb == 4) DrawTrSurf::Set(arg[3], P);
239     else         DrawTrSurf::Set(arg[2], P);
240     return 0;
241   }
242   return 1;
243 }
244
245
246 //=======================================================================
247 //function : DDataStd_GetAxis
248 //purpose  : GetAxis (DF, entry, [drawname])
249 //=======================================================================
250
251 static Standard_Integer DDataStd_GetAxis (Draw_Interpretor&,
252                                           Standard_Integer nb, 
253                                           const char** arg) 
254 {  
255   if (nb < 3) return 1;
256   Handle(TDF_Data) DF;
257   if (!DDF::GetDF(arg[1],DF)) return 1;
258   Handle(TDataXtd_Axis) A;
259   if (!DDF::Find (DF, arg[2], TDataXtd_Axis::GetID(), A)) return 1;
260   gp_Lin l;
261   if (TDataXtd_Geometry::Line(A->Label(),l)) {
262     Handle(Geom_Line) L = new Geom_Line (l);  
263     if (nb == 4) DrawTrSurf::Set (arg[3], L);
264     else         DrawTrSurf::Set (arg[2], L);
265     return 0;
266   }
267   return 1;
268 }
269
270 //=======================================================================
271 //function : DDataStd_GetPlane
272 //purpose  : GetPlane (DF, entry, [drawname])
273 //=======================================================================
274
275 static Standard_Integer DDataStd_GetPlane (Draw_Interpretor&,
276                                            Standard_Integer nb, 
277                                            const char** arg) 
278 {  
279   if (nb < 3) return 1;
280   Handle(TDF_Data) DF;
281   if (!DDF::GetDF(arg[1],DF)) return 1;
282   Handle(TDataXtd_Plane) A;
283   if (!DDF::Find (DF, arg[2], TDataXtd_Plane::GetID(), A)) return 1;
284   gp_Pln p;  
285   if (TDataXtd_Geometry::Plane(A->Label(),p)) {  
286     Handle(Geom_Plane) P = new Geom_Plane (p);  
287     if (nb == 4) DrawTrSurf::Set (arg[3], P);
288     else         DrawTrSurf::Set (arg[2], P);
289     return 0;
290   }
291   return 1;
292 }
293
294 //=======================================================================
295 //function : DDataStd_SetGeometry
296 //purpose  : SetGeometry (DF, entry, [type], [shape])
297 //=======================================================================
298 static Standard_Integer DDataStd_SetGeometry (Draw_Interpretor& di,
299                                               Standard_Integer nb, 
300                                               const char** arg) 
301 {   
302   if (nb < 3) return 1;
303
304   Handle(TDF_Data) DF;
305   if (!DDF::GetDF (arg[1], DF)) return 1;
306
307   TDF_Label L;
308   if (!DDF::FindLabel(DF, arg[2], L)) DDF::AddLabel(DF, arg[2], L);
309
310   if (nb == 5)
311   {
312     // set NS attribute
313     TopoDS_Shape s = DBRep::Get(arg[4]);  
314     if (s.IsNull()) { di <<"shape not found"<< "\n"; return 1;}  
315     TNaming_Builder SI (L);
316     SI.Generated(s);
317   }
318
319   // set geometry attribute
320   Handle(TDataXtd_Geometry) aGA = TDataXtd_Geometry::Set(L);
321
322   // set type
323   TDataXtd_GeometryEnum aType;
324   if (nb > 3)
325   {
326     const char* aT = arg[3];
327     if (strcmp(aT,"any") == 0)      aType = TDataXtd_ANY_GEOM;
328     else if (strcmp(aT,"pnt") == 0) aType = TDataXtd_POINT;
329     else if (strcmp(aT,"lin") == 0) aType = TDataXtd_LINE;
330     else if (strcmp(aT,"cir") == 0) aType = TDataXtd_CIRCLE;
331     else if (strcmp(aT,"ell") == 0) aType = TDataXtd_ELLIPSE;
332     else if (strcmp(aT,"spl") == 0) aType = TDataXtd_SPLINE;
333     else if (strcmp(aT,"pln") == 0) aType = TDataXtd_PLANE;
334     else if (strcmp(aT,"cyl") == 0) aType = TDataXtd_CYLINDER;
335     else
336     {
337       di << "DDataStd_SetGeometry : unknown type, must be one of:" << "\n";
338       di << "any/pnt/lin/cir/ell/spl/pln/cyl" << "\n";
339       return 1;
340     }
341   }
342   else
343   {
344     aType = TDataXtd_Geometry::Type(L);
345   }
346   aGA->SetType(aType);
347
348 //  DDataStd_DrawPresentation::Display (L);
349   return 0;
350 }
351
352 //=======================================================================
353 //function : DDataStd_GetGeometryType
354 //purpose  : GetGeometryType (DF, entry)
355 //=======================================================================
356 static Standard_Integer DDataStd_GetGeometryType (Draw_Interpretor& di,
357                                               Standard_Integer nb, 
358                                               const char** arg) 
359 {   
360   if (nb != 3) return 1;
361
362   Handle(TDF_Data) DF;
363   if (!DDF::GetDF (arg[1], DF)) return 1;
364
365   TDF_Label L;
366   if (!DDF::FindLabel(DF, arg[2], L)) DDF::AddLabel(DF, arg[2], L);
367
368   // get geometry attribute
369   Handle(TDataXtd_Geometry) aGA;
370   if (!L.FindAttribute(TDataXtd_Geometry::GetID(),aGA))
371   {
372     di << "TDataStd_Geometry : attribute not found" << "\n";
373     return 1;
374   }
375
376   // get type
377   TDataXtd_GeometryEnum aType = aGA->GetType();
378   switch (aType)
379   {
380   case TDataXtd_ANY_GEOM:  di << "any"; break;
381   case TDataXtd_POINT:     di << "pnt"; break;
382   case TDataXtd_LINE:      di << "lin"; break;
383   case TDataXtd_CIRCLE:    di << "cir"; break;
384   case TDataXtd_ELLIPSE:   di << "ell"; break;
385   case TDataXtd_SPLINE:    di << "spl"; break;
386   case TDataXtd_PLANE:     di << "pln"; break;
387   case TDataXtd_CYLINDER:  di <<"cyl"; break;
388   default:
389     {
390       di << "DDataStd_GetGeometry : unknown type" << "\n";
391       return 1;
392     }
393   }
394
395   return 0;
396 }
397
398 //=======================================================================
399 //function : DatumCommands
400 //purpose  : 
401 //=======================================================================
402
403 void DDataStd::DatumCommands (Draw_Interpretor& theCommands)
404
405 {  
406   static Standard_Boolean done = Standard_False;
407   if (done) return;
408   done = Standard_True;
409   const char* g = "DData : Standard Attribute Commands";
410   
411   theCommands.Add ("SetPoint", 
412                    "SetPoint (DF, entry, [drawpoint])",
413                    __FILE__, DDataStd_SetPoint, g);
414   
415   theCommands.Add ("SetAxis", 
416                    "SetAxis (DF, entry, [drawline])",
417                    __FILE__, DDataStd_SetAxis, g);
418
419   theCommands.Add ("SetPlane", 
420                    "SetPlane (DF, entry, [drawplane])",
421                    __FILE__, DDataStd_SetPlane, g);
422
423 //  theCommands.Add ("SetMove", 
424  //                  "SetMove (DF, entry, Shape | [x, y, z, [dx, dy, dz, angle]])",
425         //         __FILE__, DDataStd_SetMove, g);  
426
427   theCommands.Add ("GetPoint", 
428                    "GetPoint (DF, entry, [drawname])",
429                    __FILE__, DDataStd_GetPoint, g);
430
431   theCommands.Add ("GetAxis", 
432                    "GetAxis (DF, entry, [drawname])",
433                    __FILE__, DDataStd_GetAxis, g);
434
435   theCommands.Add ("GetPlane", 
436                    "GetPlane (DF, entry, [drawname])",
437                    __FILE__, DDataStd_GetPlane, g);
438
439   theCommands.Add ("SetGeometry", 
440                    "SetGeometry (DF, entry, [type], [shape])",
441                    __FILE__, DDataStd_SetGeometry, g);
442   
443   theCommands.Add ("GetGeometryType", 
444                    "GetGeometryType (DF, entry)",
445                    __FILE__, DDataStd_GetGeometryType, g);
446 }