0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / DrawDim / DrawDim_PlanarDimensionCommands.cxx
1 // Created on: 1996-06-03
2 // Created by: Denis PASCAL
3 // Copyright (c) 1996-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 <stdio.h>
18
19 #include <DrawDim.hxx>
20
21 #include <Draw_Interpretor.hxx>
22 #include <Draw_Appli.hxx>
23 #include <BRep_Tool.hxx>
24 #include <DBRep.hxx> 
25 #include <Draw.hxx>
26 #include <DBRep.hxx> 
27 #include <DrawDim_PlanarDistance.hxx>
28 #include <DrawDim_PlanarRadius.hxx>
29 #include <DrawDim_PlanarAngle.hxx>
30 #include <TopoDS_Vertex.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <TopoDS.hxx>
33 #include <TopExp.hxx>
34 #include <Geom_Curve.hxx>
35 #include <Geom_Circle.hxx>
36 #include <BRep_Builder.hxx>
37 #include <gp_Pnt.hxx>
38 #include <gp_Circ.hxx>
39 #include <TopTools_MapOfShape.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <gp_Ax3.hxx>
42 #include <gp_Pln.hxx>
43 #include <DrawTrSurf.hxx>
44
45 #ifdef _MSC_VER
46 #include <stdio.h>
47 #endif
48
49 //=======================================================================
50 //function : DrawDim_DISTANCE
51 //purpose  : 
52 //=======================================================================
53
54 static Standard_Integer DrawDim_DISTANCE (Draw_Interpretor& di,
55                                         Standard_Integer nb, 
56                                         const char** arg) 
57 {
58   if (nb == 1) {
59     di << "distance (name, profile[face],point1[vertex],point2[vertex])\n";
60     di << "distance (name, profile[face],line1[edge],line2[edge])\n";    
61     di << "distance (name, profile[face],line[edge],point[vertex])\n";
62   }
63   else {   
64     Handle(DrawDim_PlanarDistance) DIST;
65     if (nb == 5) {
66       TopoDS_Shape aLocalShape = DBRep::Get(arg[2],TopAbs_FACE);
67       TopoDS_Face plan =  TopoDS::Face(aLocalShape);
68 //      TopoDS_Face plan =  TopoDS::Face(DBRep::Get(arg[2],TopAbs_FACE));
69       TopoDS_Shape geom1 = DBRep::Get(arg[3]);
70       TopoDS_Shape geom2 = DBRep::Get(arg[4]);
71       if (!plan.IsNull() && !geom1.IsNull() && !geom2.IsNull()) {
72         if (geom1.ShapeType() == TopAbs_VERTEX && geom2.ShapeType() == TopAbs_VERTEX) {
73           DIST = new  DrawDim_PlanarDistance(TopoDS::Vertex(geom1),TopoDS::Vertex(geom2));
74         }
75         else if (geom1.ShapeType() == TopAbs_VERTEX && geom2.ShapeType() == TopAbs_EDGE) {
76           DIST = new  DrawDim_PlanarDistance(TopoDS::Vertex(geom1),TopoDS::Edge(geom2));
77         }
78         else if (geom1.ShapeType() == TopAbs_EDGE && geom2.ShapeType() == TopAbs_EDGE) {
79           DIST = new  DrawDim_PlanarDistance(TopoDS::Edge(geom1),TopoDS::Edge(geom2));
80         }
81         if (!DIST.IsNull()) {
82           Draw::Set(arg[1],DIST);
83           return 0;
84         }
85       }
86     }
87   }
88   di << "DrawDim_DISTANCE : error\n";
89   return 1;
90 }
91
92
93 //=======================================================================
94 //function : DrawDim_ANGLE
95 //purpose  : 
96 //=======================================================================
97
98 static Standard_Integer DrawDim_ANGLE (Draw_Interpretor& di,
99                                        Standard_Integer nb, 
100                                        const char** arg) 
101 {   
102   if (nb == 1) {
103     di << "angle (name, profile[face],line1[edge],line2[edge])\n";
104   } 
105   else {   
106     Handle(DrawDim_PlanarAngle) DIST;
107     if (nb == 5) {
108       TopoDS_Shape aLocalShape = DBRep::Get(arg[2],TopAbs_FACE);
109       TopoDS_Face plan =  TopoDS::Face(aLocalShape);
110       aLocalShape = DBRep::Get(arg[3],TopAbs_EDGE);
111       TopoDS_Edge line1 = TopoDS::Edge(aLocalShape);
112       aLocalShape = DBRep::Get(arg[4],TopAbs_EDGE);
113       TopoDS_Edge line2 = TopoDS::Edge(aLocalShape);
114 //      TopoDS_Face plan =  TopoDS::Face(DBRep::Get(arg[2],TopAbs_FACE));
115 //      TopoDS_Edge line1 = TopoDS::Edge(DBRep::Get(arg[3],TopAbs_EDGE));
116 //      TopoDS_Edge line2 = TopoDS::Edge(DBRep::Get(arg[4],TopAbs_EDGE));
117       if (!plan.IsNull() && !line1.IsNull() && !line2.IsNull()) {
118         DIST = new  DrawDim_PlanarAngle(plan,line1,line2);
119       }
120       if (!DIST.IsNull()) {
121         Draw::Set(arg[1],DIST);
122         return 0;
123       }
124     }
125   }
126   di << "DrawDim_PlanarAngle : error\n";
127   return 1;
128 }
129
130 //=======================================================================
131 //function : DrawDim_RADIUS
132 //purpose  : 
133 //=======================================================================
134
135 static Standard_Integer DrawDim_RADIUS (Draw_Interpretor& di,
136                                         Standard_Integer nb, 
137                                         const char** arg) 
138 {   
139   if (nb == 1) {   
140     di << "radius (name, profile[face],cercle[edge])\n";     
141   }  
142   else {    
143     Handle(DrawDim_PlanarRadius) DIST;
144     if (nb == 4) {     
145       TopoDS_Shape aLocalShape = DBRep::Get(arg[2],TopAbs_FACE);
146       TopoDS_Face plan =  TopoDS::Face(aLocalShape);
147       aLocalShape = DBRep::Get(arg[3],TopAbs_EDGE);
148       TopoDS_Edge cercle = TopoDS::Edge(aLocalShape);
149 //      TopoDS_Face plan =  TopoDS::Face(DBRep::Get(arg[2],TopAbs_FACE));
150 //      TopoDS_Edge cercle = TopoDS::Edge(DBRep::Get(arg[3],TopAbs_EDGE));
151       if (!plan.IsNull() && !cercle.IsNull()) {  
152         DIST = new  DrawDim_PlanarRadius(cercle);
153       }
154     }
155     if (!DIST.IsNull()) {
156       Draw::Set(arg[1],DIST);
157       return 0;
158     }
159   }
160   di << "DrawDim_PlanarRadius : error\n";
161   return 1;
162 }
163
164
165 //=======================================================================
166 //function : DrawDim_CENTER
167 //purpose  : 
168 //=======================================================================
169
170 static Standard_Integer DrawDim_CENTER (Draw_Interpretor& di,
171                                         Standard_Integer nb, 
172                                         const char** arg) 
173 {  
174   if (nb == 3) {   
175     TopoDS_Shape aLocalShape = DBRep::Get(arg[2],TopAbs_EDGE);
176     TopoDS_Edge edge = TopoDS::Edge(aLocalShape);
177 //    TopoDS_Edge edge = TopoDS::Edge(DBRep::Get(arg[2],TopAbs_EDGE));
178     Standard_Real f,l;
179     Handle(Geom_Curve) curve = BRep_Tool::Curve (edge,f,l);
180     if (curve->IsKind(STANDARD_TYPE(Geom_Circle))) {      
181       gp_Circ circle = Handle(Geom_Circle)::DownCast(curve)->Circ();
182       gp_Pnt center = circle.Location ();
183 //:abv: avoid dependence on TKTopAlgo
184       TopoDS_Vertex vc;
185 //      = BRepBuilderAPI_MakeVertex (center);
186       BRep_Builder B;
187       B.MakeVertex(vc,center,Precision::Confusion());
188       DBRep::Set(arg[1],vc);
189       return 0;
190     }
191   }
192   di << "DrawDim_CENTER : error\n";
193   return 1;
194 }
195
196
197 //=======================================================================
198 //function : DrawDim_VARIABLES
199 //purpose  : 
200 //=======================================================================
201
202 static Standard_Integer DrawDim_VARIABLES (Draw_Interpretor& di,
203                                            Standard_Integer n, 
204                                            const char** a) 
205 {
206   if (n != 2) return 1;
207   TopoDS_Shape aLocalShape = DBRep::Get(a[1],TopAbs_FACE);
208   TopoDS_Face F = TopoDS::Face(aLocalShape);
209 //  TopoDS_Face F = TopoDS::Face(DBRep::Get(a[1],TopAbs_FACE));
210   if (F.IsNull()) return 0;
211   
212   Standard_Integer i = 0;
213   TopoDS_Vertex vf,vl;
214   TopTools_MapOfShape M;
215   M.Add(F);
216   TopExp_Explorer ex (F,TopAbs_EDGE);
217   while (ex.More()) {
218     if (M.Add(ex.Current())) {
219       TopExp::Vertices(TopoDS::Edge(ex.Current()),vf,vl);
220       if (M.Add(vf)) {
221         i++;
222         char* p = (char *)malloc(100);
223         Sprintf(p,"%s_%dv",a[1],i);
224         DBRep::Set(p,vf);
225         di.AppendElement(p);
226         DrawDim::DrawShapeName (vf,p);
227       }
228       if (M.Add(vl)) {
229         i++;
230         char *p = (char *)malloc(100);
231         Sprintf(p,"%s_%dv",a[1],i);
232         DBRep::Set(p,vl);
233         di.AppendElement(p);    
234         DrawDim::DrawShapeName (vl,p);
235       }
236       i++;
237       char *p = (char *)malloc(100);
238       Sprintf(p,"%s_%de",a[1],i);
239       DBRep::Set(p,ex.Current());
240       di.AppendElement(p);
241       DrawDim::DrawShapeName (ex.Current(),p);
242     }
243     ex.Next();
244   }
245   return 0;
246 }
247
248 //=======================================================================
249 //function : DrawDim_SPLACEMENT
250 //purpose  : 
251 //=======================================================================
252
253 static Standard_Integer DrawDim_SPLACEMENT (Draw_Interpretor& di,
254                                            Standard_Integer n, 
255                                            const char** a) 
256 {
257   if (n == 4) {    
258     TopoDS_Shape shape = DBRep::Get(a[1]);  
259     TopoDS_Shape aLocalShape = DBRep::Get(a[2],TopAbs_FACE);
260     TopoDS_Face from = TopoDS::Face(aLocalShape);   
261     aLocalShape = DBRep::Get(a[3],TopAbs_FACE);
262     TopoDS_Face to = TopoDS::Face(aLocalShape);
263 //    TopoDS_Face from = TopoDS::Face(DBRep::Get(a[2],TopAbs_FACE));   
264 //    TopoDS_Face to = TopoDS::Face(DBRep::Get(a[3],TopAbs_FACE));
265     if (!shape.IsNull() && !from.IsNull() && !to.IsNull()) {
266       gp_Pln pfrom,pto;
267       DrawDim::Pln(from,pfrom);  
268       DrawDim::Pln(to,pto);
269       gp_Ax3 axfrom  (pfrom.Position());
270       gp_Ax3 axto  (pto.Position());
271       gp_Trsf  trsf;
272       trsf.SetDisplacement(axfrom,axto);
273       TopLoc_Location move (trsf);
274       shape.Move(move);
275       DBRep::Set(a[1],shape);      
276       return 0;
277     }
278   }  
279   di << "DrawDim_SPlacement : error\n";
280   return 1;
281 }
282
283 //=======================================================================
284 //function : DrawDim_GPLACEMENT
285 //purpose  : 
286 //=======================================================================
287
288 static Standard_Integer DrawDim_GPLACEMENT (Draw_Interpretor& di,
289                                            Standard_Integer n, 
290                                            const char** a) 
291 {
292   if (n == 4) {    
293     Handle(Geom_Geometry) geom = DrawTrSurf::Get(a[1]);  
294     TopoDS_Shape aLocalShape = DBRep::Get(a[2],TopAbs_FACE);
295     TopoDS_Face from = TopoDS::Face(aLocalShape);   
296     aLocalShape = DBRep::Get(a[3],TopAbs_FACE);
297     TopoDS_Face to = TopoDS::Face(aLocalShape);
298 //    TopoDS_Face from = TopoDS::Face(DBRep::Get(a[2],TopAbs_FACE));   
299 //    TopoDS_Face to = TopoDS::Face(DBRep::Get(a[3],TopAbs_FACE));
300     if (!geom.IsNull() && !from.IsNull() && !to.IsNull()) {
301       gp_Pln pfrom,pto;
302       DrawDim::Pln(from,pfrom);  
303       DrawDim::Pln(to,pto);
304       gp_Ax3 axfrom  (pfrom.Position());
305       gp_Ax3 axto  (pto.Position());
306       gp_Trsf  trsf;
307       trsf.SetDisplacement(axfrom,axto);
308       Handle(Geom_Geometry) newgeom = geom->Transformed(trsf);
309       DrawTrSurf::Set(a[1],newgeom);      
310       return 0;
311     }
312   }  
313   di << "DrawDim_Placement : error\n";
314   return 1;
315 }
316  
317
318 //=======================================================================
319 //function : PlanarDimensionCommands
320 //purpose  : 
321 //=======================================================================
322
323 void DrawDim::PlanarDimensionCommands (Draw_Interpretor& theCommands)
324
325   // syntaxes
326   theCommands.Add ("distance",
327                    "distance,no args to get help",
328                    __FILE__, DrawDim_DISTANCE);   
329
330   theCommands.Add ("radius",
331                    "radius, no args to get help",
332                    __FILE__, DrawDim_RADIUS);   
333
334   theCommands.Add ("angle",
335                    "angle, no args to get help",
336                    __FILE__, DrawDim_ANGLE);   
337
338   theCommands.Add ("center",
339                    "to extract center of a circle : center ,name, circle",
340                    __FILE__, DrawDim_CENTER);   
341
342   theCommands.Add ("variables",
343                    "to extract variables of a face",
344                    __FILE__, DrawDim_VARIABLES);  
345
346   theCommands.Add ("splacement",
347                    "to move shape from face to face",
348                    __FILE__, DrawDim_SPLACEMENT);   
349
350   theCommands.Add ("gplacement",
351                    "to move geometry from face to face",
352                    __FILE__, DrawDim_GPLACEMENT); 
353 }