0032668: Documentation - add tutorial for creating a custom AIS Interactive Object
[occt.git] / src / QADraw / QADraw.cxx
1 // Created on: 2002-02-04
2 // Created by: QA Admin
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <QADraw.hxx>
17
18 #include <QABugs.hxx>
19 #include <QADraw.hxx>
20 #include <QADNaming.hxx>
21 #include <QANCollection.hxx>
22
23 #include <AIS_InteractiveContext.hxx>
24 #include <Bnd_Box.hxx>
25 #include <BRepBndLib.hxx>
26 #include <BRepExtrema_DistShapeShape.hxx>
27 #include <BRepBuilderAPI_MakeVertex.hxx>
28 #include <DBRep.hxx>
29 #include <Draw.hxx>
30 #include <Draw_Interpretor.hxx>
31 #include <Draw_PluginMacro.hxx>
32 #include <OSD_Timer.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopTools_SequenceOfShape.hxx>
35 #include <TColgp_SequenceOfXYZ.hxx>
36
37 #include <stdio.h>
38
39 //=======================================================================
40 //function : QATestExtremaSS
41 //purpose  :
42 //=======================================================================
43 static Standard_Integer QATestExtremaSS (Draw_Interpretor& theInterpretor,
44                                          Standard_Integer  theArgNb,
45                                          const char**      theArgs)
46 {
47   if (theArgNb < 3
48    || theArgNb > 4)
49   {
50     std::cerr << "Usage: type help " << theArgs[0] << std::endl;
51     return 1;
52   }
53
54   // Get target shape
55   TopoDS_Shape aShape = DBRep::Get (theArgs[1]);
56   if (aShape.IsNull())
57   {
58     std::cerr << "Error: " << theArgs[1] << " shape is null\n";
59     return 1;
60   }
61
62   // Get step value
63   const Standard_Real aStep = Draw::Atof (theArgs[2]);
64   if (aStep <= 1e-5)
65   {
66     std::cerr << "Error: Step " << aStep << " is too small\n";
67     return 1;
68   }
69
70   Extrema_ExtFlag aFlag = Extrema_ExtFlag_MIN;
71   if (theArgNb > 3)
72   {
73     Standard_Integer aVal = Draw::Atoi (theArgs[3]);
74     if (aVal > 0)
75     {
76       aFlag = aVal == 1 ? Extrema_ExtFlag_MAX : Extrema_ExtFlag_MINMAX;
77     }
78   }
79
80   // Get bounding box of the shape
81   Bnd_Box aBounds;
82   BRepBndLib::Add (aShape, aBounds);
83
84   Standard_Real aXmin, aYmin, aZmin;
85   Standard_Real aXmax, aYmax, aZmax;
86   aBounds.Get (aXmin, aYmin, aZmin,
87                aXmax, aYmax, aZmax);
88
89   const Standard_Real aScaleFactor = 1.5;
90   aXmin *= aScaleFactor;
91   aYmin *= aScaleFactor;
92   aZmin *= aScaleFactor;
93   aXmax *= aScaleFactor;
94   aYmax *= aScaleFactor;
95   aZmax *= aScaleFactor;
96
97   TopTools_SequenceOfShape aList;
98   TColgp_SequenceOfXYZ     aPoints;
99   for (Standard_Real aX = aXmin + 0.5 * aStep; aX < aXmax; aX += aStep)
100   {
101     for (Standard_Real aY = aYmin + 0.5 * aStep; aY < aYmax; aY += aStep)
102     {
103       aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aX, aY, aZmin)));
104       aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aX, aY, aZmax)));
105
106       aPoints.Append (gp_XYZ (aX, aY, aZmin));
107       aPoints.Append (gp_XYZ (aX, aY, aZmax));
108     }
109
110     for (Standard_Real aZ = aZmin + 0.5 * aStep; aZ < aZmax; aZ += aStep)
111     {
112       aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aX, aYmin, aZ)));
113       aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aX, aYmax, aZ)));
114
115       aPoints.Append (gp_XYZ (aX, aYmin, aZ));
116       aPoints.Append (gp_XYZ (aX, aYmax, aZ));
117     }
118   }
119
120   for (Standard_Real aY = aYmin + 0.5 * aStep; aY < aYmax; aY += aStep)
121   {
122     for (Standard_Real aZ = aZmin + 0.5 * aStep; aZ < aZmax; aZ += aStep)
123     {
124       aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aXmin, aY, aZ)));
125       aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aXmax, aY, aZ)));
126
127       aPoints.Append (gp_XYZ (aXmin, aY, aZ));
128       aPoints.Append (gp_XYZ (aXmax, aY, aZ));
129     }
130   }
131
132   const Standard_Integer aNbPoints = aList.Length();
133   theInterpretor << "Number of sampled points: " << aNbPoints << "\n";
134
135   // Perform projection of points
136   OSD_Timer aTimer;
137   aTimer.Start();
138
139   // Perform projection using standard method
140   BRepExtrema_DistShapeShape aTool;
141   aTool.SetFlag (aFlag);
142   aTool.LoadS1 (aShape);
143   for (Standard_Integer anIdx = 1; anIdx <= aNbPoints; ++anIdx)
144   {
145     aTool.LoadS2 (aList (anIdx));
146     aTool.Perform();
147   }
148
149   aTimer.Stop();
150   theInterpretor << "Test for gradient descent complete in: " << aTimer.ElapsedTime() << "\n";
151   return 0;
152 }
153
154 //=======================================================================
155 //function : CommonCommands
156 //purpose  :
157 //=======================================================================
158 void QADraw::CommonCommands (Draw_Interpretor& theCommands)
159 {
160   const char* group = "QA_Commands";
161
162   theCommands.Add ("QATestExtremaSS",
163                    "QATestExtremaSS Shape Step [Flag { MIN = 0 | MAX = 1 | MINMAX = 2 }]",
164                    __FILE__,
165                    QATestExtremaSS,
166                    group);
167
168 // adding commands "rename" leads to the fact that QA commands doesn't work properly OCC23410, use function "renamevar"
169 // theCommands.Add("rename","rename name1 toname1 name2 toname2 ...",__FILE__,QArename,group);
170 }
171
172 //==============================================================================
173 // QADraw::Factory
174 //==============================================================================
175 void QADraw::Factory(Draw_Interpretor& theCommands)
176 {
177   // definition of QA Command
178   QADraw::CommonCommands(theCommands);
179   QADraw::TutorialCommands(theCommands);
180
181   QABugs::Commands(theCommands);
182   QADNaming::AllCommands(theCommands);
183   QANCollection::Commands(theCommands);
184 }
185
186 // Declare entry point PLUGINFACTORY
187 DPLUGIN(QADraw)