0024659: misprints in draw documentation
[occt.git] / dox / tutorial / tutorial.md
CommitLineData
e5bd0d98 1 Tutorial {#tutorial}
765b3e07 2=======
3
e5bd0d98 4@tableofcontents
5
765b3e07 6@section sec1 Overview
7
8
9This tutorial will teach you how to use Open CASCADE Technology services to model a 3D object. The purpose of this tutorial is not to describe all Open CASCADE Technology classes but to help you start thinking in terms of Open CASCADE Technology as a tool.
10
11
12@subsection OCCT_TUTORIAL_SUB1_1 Prerequisites
13
14This tutorial assumes that you have experience in using and setting up C++.
15From a programming standpoint, Open CASCADE Technology is designed to enhance your C++ tools with 3D modeling classes, methods and functions. The combination of all these resources will allow you to create substantial applications.
16
17@subsection OCCT_TUTORIAL_SUB1_2 The Model
18
19To illustrate the use of classes provided in the 3D geometric modeling toolkits, you will create a bottle as shown:
20
e5bd0d98 21@image html /tutorial/images/tutorial_image001.png
22@image latex /tutorial/images/tutorial_image001.png
765b3e07 23
24In the tutorial we will create, step-by-step, a function that will model a bottle as shown above. You will find the complete source code of this tutorial, including the very function *MakeBottle* in the distribution of Open CASCADE Technology. The function body is provided in the file samples/qt/Tutorial/src/MakeBottle.cxx.
25
26@subsection OCCT_TUTORIAL_SUB1_3 Model Specifications
27
28We first define the bottle specifications as follows:
29
30| Object Parameter | Parameter Name | Parameter Value |
31| :--------------: | :------------: | :-------------: |
32| Bottle height | MyHeight | 70mm |
33| Bottle width | MyWidth | 50mm |
34| Bottle thickness | MyThickness | 30mm |
35
36In addition, we decide that the bottle's profile (base) will be centered on the origin of the global Cartesian coordinate system.
37
e5bd0d98 38@image html /tutorial/images/tutorial_image002.png
39@image latex /tutorial/images/tutorial_image002.png
765b3e07 40
41This modeling requires four steps:
42
43 * build the bottle's Profile
44 * build the bottle's Body
45 * build the Threading on the bottle's neck
46 * build the result compound
47
48
49@section sec2 Building the Profile
50
51@subsection OCCT_TUTORIAL_SUB2_1 Defining Support Points
52
53To create the bottle's profile, you first create characteristic points with their coordinates as shown below in the (XOY) plane. These points will be the supports that define the geometry of the profile.
54
e5bd0d98 55@image html /tutorial/images/tutorial_image003.png
56@image latex /tutorial/images/tutorial_image003.png
765b3e07 57
58There are two classes to describe a 3D Cartesian point from its X, Y and Z coordinates in Open CASCADE Technology:
59
60 * the primitive geometric *gp_Pnt* class
61 * the transient *Geom_CartesianPoint* class manipulated by handle
62
63A handle is a type of smart pointer that provides automatic memory management.
64To choose the best class for this application, consider the following:
65
66 * *gp_Pnt* is manipulated by value. Like all objects of its kind, it will have a limited lifetime.
67 * *Geom_CartesianPoint* is manipulated by handle and may have multiple references and a long lifetime.
68
69Since all the points you will define are only used to create the profile's curves, an object with a limited lifetime will do. Choose the *gp_Pnt* class.
70To instantiate a *gp_Pnt* object, just specify the X, Y, and Z coordinates of the points in the global cartesian coordinate system:
71
72~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
73 gp_Pnt aPnt1(-myWidth / 2., 0, 0);
74 gp_Pnt aPnt2(-myWidth / 2., -myThickness / 4., 0);
75 gp_Pnt aPnt3(0, -myThickness / 2., 0);
76 gp_Pnt aPnt4(myWidth / 2., -myThickness / 4., 0);
77 gp_Pnt aPnt5(myWidth / 2., 0, 0);
78~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79
80Once your objects are instantiated, you can use methods provided by the class to access and modify its data. For example, to get the X coordinate of a point:
81
82~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
83Standard_Real xValue1 = aPnt1.X();
84~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
85
86@subsection OCCT_TUTORIAL_SUB2_2 Profile: Defining the Geometry
87With the help of the previously defined points, you can compute a part of the bottle's profile geometry. As shown in the figure below, it will consist of two segments and one arc.
88
e5bd0d98 89@image html /tutorial/images/tutorial_image004.png
90@image latex /tutorial/images/tutorial_image004.png
765b3e07 91
92To create such entities, you need a specific data structure, which implements 3D geometric objects. This can be found in the Geom package of Open CASCADE Technology.
93In Open CASCADE Technology a package is a group of classes providing related functionality. The classes have names that start with the name of a package they belong to. For example, *Geom_Line* and *Geom_Circle* classes belong to the *Geom* package. The *Geom* package implements 3D geometric objects: elementary curves and surfaces are provided as well as more complex ones (such as *Bezier* and *BSpline*).
94However, the *Geom* package provides only the data structure of geometric entities. You can directly instantiate classes belonging to *Geom*, but it is easier to compute elementary curves and surfaces by using the *GC* package.
95This is because the *GC* provides two algorithm classes which are exactly what is required for our profile:
96
97 * Class *GC_MakeSegment* to create a segment. One of its constructors allows you to define a segment by two end points P1 and P2
98 * Class *GC_MakeArcOfCircle* to create an arc of a circle. A useful constructor creates an arc from two end points P1 and P3 and going through P2.
99
100Both of these classes return a *Geom_TrimmedCurve* manipulated by handle. This entity represents a base curve (line or circle, in our case), limited between two of its parameter values. For example, circle C is parameterized between 0 and 2PI. If you need to create a quarter of a circle, you create a *Geom_TrimmedCurve* on C limited between 0 and M_PI/2.
101
102~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
103 Handle(Geom_TrimmedCurve) aArcOfCircle = GC_MakeArcOfCircle(aPnt2,aPnt3,aPnt4);
104 Handle(Geom_TrimmedCurve) aSegment1 = GC_MakeSegment(aPnt1, aPnt2);
105 Handle(Geom_TrimmedCurve) aSegment2 = GC_MakeSegment(aPnt4, aPnt5);
106~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107
108All *GC* classes provide a casting method to obtain a result automatically with a function-like call. Note that this method will raise an exception if construction has failed. To handle possible errors more explicitly, you may use the *IsDone* and *Value* methods. For example:
109
110~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
111 GC_MakeSegment mkSeg (aPnt1, aPnt2);
112 Handle(Geom_TrimmedCurve) aSegment1;
113 if(mkSegment.IsDone()){
114 aSegment1 = mkSeg.Value();
115 }
116 else {
117 // handle error
118 }
119~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120
121
122@subsection OCCT_TUTORIAL_SUB2_3 Profile: Defining the Topology
123
124
125You have created the support geometry of one part of the profile but these curves are independent with no relations between each other.
126To simplify the modeling, it would be right to manipulate these three curves as a single entity.
127This can be done by using the topological data structure of Open CASCADE Technology defined in the *TopoDS* package: it defines relationships between geometric entities which can be linked together to represent complex shapes.
128Each object of the *TopoDS* package, inheriting from the *TopoDS_Shape* class, describes a topological shape as described below:
129
130| Shape | Open CASCADE Technology Class | Description |
131| :-------- | :---------------------------- | :------------------------------------------------------------ |
132| Vertex | TopoDS_Vertex | Zero dimensional shape corresponding to a point in geometry. |
133| Edge | TopoDS_Edge | One-dimensional shape corresponding to a curve and bounded by a vertex at each extremity.|
134| Wire | TopoDS_Wire | Sequence of edges connected by vertices. |
135| Face | TopoDS_Face | Part of a surface bounded by a closed wire(s). |
136| Shell | TopoDS_Shell | Set of faces connected by edges. |
137| Solid | TopoDS_Solid | Part of 3D space bounded by Shells. |
138| CompSolid | TopoDS_CompSolid | Set of solids connected by their faces. |
139| Compound | TopoDS_Compound | Set of any other shapes described above. |
140
141Referring to the previous table, to build the profile, you will create:
142
143 * Three edges out of the previously computed curves.
144 * One wire with these edges.
145
e5bd0d98 146@image html /tutorial/images/tutorial_image005.png
147@image latex /tutorial/images/tutorial_image005.png
765b3e07 148
149However, the *TopoDS* package provides only the data structure of the topological entities. Algorithm classes available to compute standard topological objects can be found in the *BRepBuilderAPI* package.
150To create an edge, you use the BRepBuilderAPI_MakeEdge class with the previously computed curves:
151
152~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
153 TopoDS_Edge aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1);
154 TopoDS_Edge aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle);
155 TopoDS_Edge aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2);
156~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
157
158In Open CASCADE Technology, you can create edges in several ways. One possibility is to create an edge directly from two points, in which case the underlying geometry of this edge is a line, bounded by two vertices being automatically computed from the two input points. For example, aEdge1 and aEdge3 could have been computed in a simpler way:
159
160~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
161 TopoDS_Edge aEdge1 = BRepBuilderAPI_MakeEdge(aPnt1, aPnt3);
162 TopoDS_Edge aEdge2 = BRepBuilderAPI_MakeEdge(aPnt4, aPnt5);
163~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
164
165To connect the edges, you need to create a wire with the *BRepBuilderAPI_MakeWire* class. There are two ways of building a wire with this class:
166
167 * directly from one to four edges
168 * by adding other wire(s) or edge(s) to an existing wire (this is explained later in this tutorial)
169
170When building a wire from less than four edges, as in the present case, you can use the constructor directly as follows:
171
172~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
173 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire(aEdge1, aEdge2, aEdge3);
174~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
175
176
177@subsection OCCT_TUTORIAL_SUB2_4 Profile: Completing the Profile
178
179
180Once the first part of your wire is created you need to compute the complete profile. A simple way to do this is to:
181
182 * compute a new wire by reflecting the existing one.
183 * add the reflected wire to the initial one.
184
e5bd0d98 185@image html /tutorial/images/tutorial_image006.png
186@image latex /tutorial/images/tutorial_image006.png
765b3e07 187
188To apply a transformation on shapes (including wires), you first need to define the properties of a 3D geometric transformation by using the gp_Trsf class. This transformation can be a translation, a rotation, a scale, a reflection, or a combination of these.
189In our case, we need to define a reflection with respect to the X axis of the global coordinate system. An axis, defined with the gp_Ax1 class, is built out of a point and has a direction (3D unitary vector). There are two ways to define this axis.
190The first way is to define it from scratch, using its geometric definition:
191
192 * X axis is located at (0, 0, 0) - use the *gp_Pnt* class.
193 * X axis direction is (1, 0, 0) - use the *gp_Dir* class. A *gp_Dir* instance is created out of its X, Y and Z coordinates.
194
195~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
196 gp_Pnt aOrigin(0, 0, 0);
197 gp_Dir xDir(1, 0, 0);
198 gp_Ax1 xAxis(aOrigin, xDir);
199~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200
201The second and simplest way is to use the geometric constants defined in the gp package (origin, main directions and axis of the global coordinate system). To get the X axis, just call the *gp::OX* method:
202
203~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
204 gp_Ax1 xAxis = gp::OX();
205~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
206
207As previously explained, the 3D geometric transformation is defined with the *gp_Trsf* class. There are two different ways to use this class:
208
209 * by defining a transformation matrix by all its values
210 * by using the appropriate methods corresponding to the required transformation (SetTranslation for a translation, SetMirror for a reflection, etc.): the matrix is automatically computed.
211
212Since the simplest approach is always the best one, you should use the SetMirror method with the axis as the center of symmetry.
213
214~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
215 gp_Trsf aTrsf;
216 aTrsf.SetMirror(xAxis);
217~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
218
219You now have all necessary data to apply the transformation with the BRepBuilderAPI_Transform class by specifying:
220
221 * the shape on which the transformation must be applied.
222 * the geometric transformation
223
224~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
225 BRepBuilderAPI_Transform aBRepTrsf(aWire, aTrsf);
226~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
227
228*BRepBuilderAPI_Transform* does not modify the nature of the shape: the result of the reflected wire remains a wire. But the function-like call or the *BRepBuilderAPI_Transform::Shape* method returns a *TopoDS_Shape* object:
229
230~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
231 TopoDS_Shape aMirroredShape = aBRepTrsf.Shape();
232~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
233
234What you need is a method to consider the resulting reflected shape as a wire. The *TopoDS* global functions provide this kind of service by casting a shape into its real type. To cast the transformed wire, use the *TopoDS::Wire* method.
235
236~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
237 TopoDS_Wire aMirroredWire = TopoDS::Wire(aMirroredShape);
238~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239
240The bottle's profile is almost finished. You have created two wires: *aWire* and *aMirroredWire*. You need to concatenate them to compute a single shape. To do this, you use the *BRepBuilderAPI_MakeWire* class as follows:
241
242 * create an instance of *BRepBuilderAPI_MakeWire*.
243 * add all edges of the two wires by using the *Add* method on this object.
244
245~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
246 BRepBuilderAPI_MakeWire mkWire;
247 mkWire.Add(aWire);
248 mkWire.Add(aMirroredWire);
249 TopoDS_Wire myWireProfile = mkWire.Wire();
250~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
251
252
253@section sec3 Building the Body
254
255
256@subsection OCCT_TUTORIAL_SUB3_1 Prism the Profile
257
258
259To compute the main body of the bottle, you need to create a solid shape. The simplest way is to use the previously created profile and to sweep it along a direction. The *Prism* functionality of Open CASCADE Technology is the most appropriate for that task. It accepts a shape and a direction as input and generates a new shape according to the following rules:
260
261| Shape | Generates |
262| :----- | :----------------- |
263| Vertex | Edge |
264| Edge | Face |
265| Wire | Shell |
266| Face | Solid |
267| Shell | Compound of Solids |
268
e5bd0d98 269@image html /tutorial/images/tutorial_image007.png
270@image latex /tutorial/images/tutorial_image007.png
765b3e07 271
272Your current profile is a wire. Referring to the Shape/Generates table, you need to compute a face out of its wire to generate a solid.
273To create a face, use the *BRepBuilderAPI_MakeFace* class. As previously explained, a face is a part of a surface bounded by a closed wire. Generally, *BRepBuilderAPI_MakeFace* computes a face out of a surface and one or more wires.
274When the wire lies on a plane, the surface is automatically computed.
275
276~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
277 TopoDS_Face myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile);
278~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
279
280The *BRepPrimAPI* package provides all the classes to create topological primitive constructions: boxes, cones, cylinders, spheres, etc. Among them is the *BRepPrimAPI_MakePrism* class. As specified above, the prism is defined by:
281
282 * the basis shape to sweep;
283 * a vector for a finite prism or a direction for finite and infinite prisms.
284
285You want the solid to be finite, swept along the Z axis and to be myHeight height. The vector, defined with the *gp_Vec* class on its X, Y and Z coordinates, is:
286
287~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
288 gp_Vec aPrismVec(0, 0, myHeight);
289~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
290
291All the necessary data to create the main body of your bottle is now available. Just apply the *BRepPrimAPI_MakePrism* class to compute the solid:
292
293~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
294 TopoDS_Shape myBody = BRepPrimAPI_MakePrism(myFaceProfile, aPrismVec);
295~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
296
297
298@subsection OCCT_TUTORIAL_SUB3_2 Applying Fillets
299
300
301The edges of the bottle's body are very sharp. To replace them by rounded faces, you use the *Fillet* functionality of Open CASCADE Technology.
302For our purposes, we will specify that fillets must be:
303
304 * applied on all edges of the shape
305 * have a radius of *myThickness* / 12
306
e5bd0d98 307@image html /tutorial/images/tutorial_image008.png
308@image latex /tutorial/images/tutorial_image008.png
765b3e07 309
310To apply fillets on the edges of a shape, you use the *BRepFilletAPI_MakeFillet* class. This class is normally used as follows:
311
312 * Specify the shape to be filleted in the *BRepFilletAPI_MakeFillet* constructor.
313 * Add the fillet descriptions (an edge and a radius) using the *Add* method (you can add as many edges as you need).
314 * Ask for the resulting filleted shape with the *Shape* method.
315
316~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
317BRepFilletAPI_MakeFillet mkFillet(myBody);
318~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
319
320To add the fillet description, you need to know the edges belonging to your shape. The best solution is to explore your solid to retrieve its edges. This kind of functionality is provided with the *TopExp_Explorer* class, which explores the data structure described in a *TopoDS_Shape* and extracts the sub-shapes you specifically need.
321Generally, this explorer is created by providing the following information:
322
323 * the shape to explore
324 * the type of sub-shapes to be found. This information is given with the *TopAbs_ShapeEnum* enumeration.
325
326~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
327TopExp_Explorer anEdgeExplorer(myBody, TopAbs_EDGE);
328~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
329
330An explorer is usually applied in a loop by using its three main methods:
331
332 * *More()* to know if there are more sub-shapes to explore.
333 * *Current()* to know which is the currently explored sub-shape (used only if the *More()* method returns true).
334 * *Next()* to move onto the next sub-shape to explore.
335
336
337~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
338 while(anEdgeExplorer.More()){
339 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExplorer.Current());
340 //Add edge to fillet algorithm
341 ...
342 anEdgeExplorer.Next();
343 }
344~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
345
346In the explorer loop, you have found all the edges of the bottle shape. Each one must then be added in the *BRepFilletAPI_MakeFillet* instance with the *Add()* method. Do not forget to specify the radius of the fillet along with it.
347
348~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
349 mkFillet.Add(myThickness / 12., anEdge);
350~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
351
352Once this is done, you perform the last step of the procedure by asking for the filleted shape.
353
354~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
355 myBody = mkFillet.Shape();
356~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
357
358
359@subsection OCCT_TUTORIAL_SUB3_3 Adding the Neck
360
361
362To add a neck to the bottle, you will create a cylinder and fuse it to the body. The cylinder is to be positioned on the top face of the body with a radius of *myThickness* / 4. and a height of *myHeight* / 10.
363
e5bd0d98 364@image html /tutorial/images/tutorial_image009.png
365@image latex /tutorial/images/tutorial_image009.png
765b3e07 366
367To position the cylinder, you need to define a coordinate system with the *gp_Ax2* class defining a right-handed coordinate system from a point and two directions - the main (Z) axis direction and the X direction (the Y direction is computed from these two).
368To align the neck with the center of the top face, being in the global coordinate system (0, 0, *myHeight*), with its normal on the global Z axis, your local coordinate system can be defined as follows:
369
370~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
371 gp_Pnt neckLocation(0, 0, myHeight);
372 gp_Dir neckAxis = gp::DZ();
373 gp_Ax2 neckAx2(neckLocation, neckAxis);
374~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
375
376To create a cylinder, use another class from the primitives construction package: the *BRepPrimAPI_MakeCylinder* class. The information you must provide is:
377
378 * the coordinate system where the cylinder will be located;
379 * the radius and height.
380
381~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
382 Standard_Real myNeckRadius = myThickness / 4.;
383 Standard_Real myNeckHeight = myHeight / 10;
384 BRepPrimAPI_MakeCylinder MKCylinder(neckAx2, myNeckRadius, myNeckHeight);
385 TopoDS_Shape myNeck = MKCylinder.Shape();
386~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
387
388You now have two separate parts: a main body and a neck that you need to fuse together.
389The *BRepAlgoAPI* package provides services to perform Boolean operations between shapes, and especially: *common* (Boolean intersection), *cut* (Boolean subtraction) and *fuse* (Boolean union).
390Use *BRepAlgoAPI_Fuse* to fuse the two shapes:
391
392~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
393 myBody = BRepAlgoAPI_Fuse(myBody, myNeck);
394~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
395
396
397@subsection OCCT_TUTORIAL_SUB3_4 Creating a Hollowed Solid
398
399
400Since a real bottle is used to contain liquid material, you should now create a hollowed solid from the bottle's top face.
401In Open CASCADE Technology, a hollowed solid is called a *Thick* *Solid* and is internally computed as follows:
402
403 * Remove one or more faces from the initial solid to obtain the first wall W1 of the hollowed solid.
404 * Create a parallel wall W2 from W1 at a distance D. If D is positive, W2 will be outside the initial solid, otherwise it will be inside.
405 * Compute a solid from the two walls W1 and W2.
406
e5bd0d98 407@image html /tutorial/images/tutorial_image010.png
408@image latex /tutorial/images/tutorial_image010.png
765b3e07 409
410To compute a thick solid, you create an instance of the *BRepOffsetAPI_MakeThickSolid* class by giving the following information:
411
412 * The shape, which must be hollowed.
413 * The tolerance used for the computation (tolerance criterion for coincidence in generated shapes).
414 * The thickness between the two walls W1 and W2 (distance D).
415 * The face(s) to be removed from the original solid to compute the first wall W1.
416
417The challenging part in this procedure is to find the face to remove from your shape - the top face of the neck, which:
418
419 * has a plane (planar surface) as underlying geometry;
420 * is the highest face (in Z coordinates) of the bottle.
421
422To find the face with such characteristics, you will once again use an explorer to iterate on all the bottle's faces to find the appropriate one.
423
424~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
425 for(TopExp_Explorer aFaceExplorer(myBody, TopAbs_FACE) ; aFaceExplorer.More() ; aFaceExplorer.Next()){
426 TopoDS_Face aFace = TopoDS::Face(aFaceExplorer.Current());
427 }
428~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
429
430For each detected face, you need to access the geometric properties of the shape: use the *BRep_Tool* class for that. The most commonly used methods of this class are:
431
432 * *Surface* to access the surface of a face;
433 * *Curve* to access the 3D curve of an edge;
434 * *Point* to access the 3D point of a vertex.
435
436~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
437Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
438~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
439
440As you can see, the *BRep_Tool::Surface* method returns an instance of the *Geom_Surface* class manipulated by handle. However, the *Geom_Surface* class does not provide information about the real type of the object *aSurface*, which could be an instance of *Geom_Plane*, *Geom_CylindricalSurface*, etc.
441All objects manipulated by handle, like *Geom_Surface*, inherit from the *Standard_Transient* class which provides two very useful methods concerning types:
442
443 * *DynamicType* to know the real type of the object
444 * *IsKind* to know if the object inherits from one particular type
445
446DynamicType returns the real type of the object, but you need to compare it with the existing known types to determine whether *aSurface* is a plane, a cylindrical surface or some other type.
447To compare a given type with the type you seek, use the *STANDARD_TYPE* macro, which returns the type of a class:
448
449~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
450 if(aSurface->DynamicType() == STANDARD_TYPE(Geom_Plane)){
451 //
452 }
453~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
454
455If this comparison is true, you know that the *aSurface* real type is *Geom_Plane*. You can then convert it from *Geom_Surface* to *Geom_Plane* by using the *DownCast()* method provided by each class inheriting *Standard_Transient*. As its name implies, this static method is used to downcast objects to a given type with the following syntax:
456
457~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
458 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aSurface);
459~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
460
461Remember that the goal of all these conversions is to find the highest face of the bottle lying on a plane. Suppose that you have these two global variables:
462
463~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
464 TopoDS_Face faceToRemove;
465 Standard_Real zMax = -1;
466~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
467
468You can easily find the plane whose origin is the biggest in Z knowing that the location of the plane is given with the *Geom_Plane::Location* method. For example:
469
470~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
471 gp_Pnt aPnt = aPlane->Location();
472 Standard_Real aZ = aPnt.Z();
473 if(aZ > zMax){
474 zMax = aZ;
475 faceToRemove = aFace;
476 }
477~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
478
479You have now found the top face of the neck. Your final step before creating the hollowed solid is to put this face in a list. Since more than one face can be removed from the initial solid, the *BRepOffsetAPI_MakeThickSolid* constructor takes a list of faces as arguments.
480Open CASCADE Technology provides many collections for different kinds of objects: see *TColGeom* package for collections of objects from *Geom* package, *TColgp* package for collections of objects from gp package, etc.
481The collection for shapes can be found in the *TopTools* package. As *BRepOffsetAPI_MakeThickSolid* requires a list, use the *TopTools_ListOfShape* class.
482
483~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
484 TopTools_ListOfShape facesToRemove;
485 facesToRemove.Append(faceToRemove);
486~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
487
488All the necessary data are now available so you can create your hollowed solid by calling the *BRepOffsetAPI_MakeThickSolid* constructor:
489
490~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
491 MyBody = BRepOffsetAPI_MakeThickSolid(myBody, facesToRemove, -myThickness / 50, 1.e-3);
492~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
493
494
495@section sec4 Building the Threading
496
497
498@subsection OCCT_TUTORIAL_SUB4_1 Creating Surfaces
499
500
501Up to now, you have learned how to create edges out of 3D curves.
502You will now learn how to create an edge out of a 2D curve and a surface.
503To learn this aspect of Open CASCADE Technology, you will build helicoidal profiles out of 2D curves on cylindrical surfaces. The theory is more complex than in previous steps, but applying it is very simple.
504As a first step, you compute these cylindrical surfaces. You are already familiar with curves of the *Geom* package. Now you can create a cylindrical surface (*Geom_CylindricalSurface*) using:
505
506 * a coordinate system;
507 * a radius.
508
509Using the same coordinate system *neckAx2* used to position the neck, you create two cylindrical surfaces *Geom_CylindricalSurface* with the following radii:
510
e5bd0d98 511@image html /tutorial/images/tutorial_image011.png
512@image latex /tutorial/images/tutorial_image011.png
765b3e07 513
514Notice that one of the cylindrical surfaces is smaller than the neck. There is a good reason for this: after the thread creation, you will fuse it with the neck. So, we must make sure that the two shapes remain in contact.
515
516~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
517 Handle(Geom_CylindricalSurface) aCyl1 = new Geom_CylindricalSurface(neckAx2, myNeckRadius * 0.99);
518
519 Handle(Geom_CylindricalSurface) aCyl2 = new Geom_CylindricalSurface(neckAx2, myNeckRadius * 1.05);
520~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
521
522
523@subsection OCCT_TUTORIAL_SUB4_2 Defining 2D Curves
524
525
526To create the neck of the bottle, you made a solid cylinder based on a cylindrical surface. You will create the profile of threading by creating 2D curves on such a surface.
527All geometries defined in the *Geom* package are parameterized. This means that each curve or surface from Geom is computed with a parametric equation.
528A *Geom_CylindricalSurface* surface is defined with the following parametric equation:
529
530P(U, V) = O + R * (cos(U) * xDir + sin(U) * yDir) + V * zDir, where :
531
532 * P is the point defined by parameters (U, V).
533 * O, *Dir, yDir and zDir are respectively the origin, the X direction, Y direction and Z direction of the cylindrical surface local coordinate system.
534 * R is the radius of the cylindrical surface.
535 * U range is [0, 2PI] and V is infinite.
536
e5bd0d98 537@image html /tutorial/images/tutorial_image012.png
538@image latex /tutorial/images/tutorial_image012.png
765b3e07 539
540The advantage of having such parameterized geometries is that you can compute, for any (U, V) parameters of the surface:
541
542 * the 3D point;
543 * the derivative vectors of order 1, 2 to N at this point.
544
545There is another advantage of these parametric equations: you can consider a surface as a 2D parametric space defined with a (U, V) coordinate system. For example, consider the parametric ranges of the neck's surface:
546
e5bd0d98 547@image html /tutorial/images/tutorial_image013.png
548@image latex /tutorial/images/tutorial_image013.png
765b3e07 549
550Suppose that you create a 2D line on this parametric (U, V) space and compute its 3D parametric curve. Depending on the line definition, results are as follows:
551
552| Case | Parametric Equation | Parametric Curve |
553| :------------ | :----------------------------------------------------------- | :---------------------------------------------------------------------------- |
554| U = 0 | P(V) = O + V * zDir | Line parallel to the Z direction |
555| V = 0 | P(U) = O + R * (cos(U) * xDir + sin(U) * yDir) | Circle parallel to the (O, X, Y) plane |
556| U != 0 V != 0 | P(U, V) = O + R * (cos(U) * xDir + sin(U) * yDir) + V * zDir | Helicoidal curve describing the evolution of height and angle on the cylinder |
557
558The helicoidal curve type is exactly what you need. On the neck's surface, the evolution laws of this curve will be:
559
560 * In V parameter: between 0 and myHeighNeck for the height description
561 * In U parameter: between 0 and 2PI for the angle description. But, since a cylindrical surface is U periodic, you can decide to extend this angle evolution to 4PI as shown in the following drawing:
562
e5bd0d98 563@image html /tutorial/images/tutorial_image014.png
564@image latex /tutorial/images/tutorial_image014.png
765b3e07 565
566In this (U, V) parametric space, you will create a local (X, Y) coordinate system to position the curves to be created. This coordinate system will be defined with:
567
568 * A center located in the middle of the neck's cylinder parametric space at (2*PI, myNeckHeight / 2) in U, V coordinates.
569 * A X direction defined with the (2*PI, myNeckHeight/4) vector in U, V coordinates, so that the curves occupy half of the neck's surfaces.
570
e5bd0d98 571@image html /tutorial/images/tutorial_image015.png
572@image latex /tutorial/images/tutorial_image015.png
765b3e07 573
574To use 2D primitive geometry types of Open CASCADE Technology for defining a point and a coordinate system, you will once again instantiate classes from gp:
575
576 * To define a 2D point from its X and Y coordinates, use the *gp_Pnt2d* class.
577 * To define a 2D direction (unit vector) from its X and Y coordinates, use the gp_Dir2d class. The coordinates will automatically be normalized.
578 * To define a 2D right-handed coordinate system, use the *gp_Ax2d* class, which is computed from a point (origin of the coordinate system) and a direction - the X direction of the coordinate system. The Y direction will be automatically computed.
579
580~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
581 gp_Pnt2d aPnt(2. * M_PI, myNeckHeight / 2.);
582 gp_Dir2d aDir(2. * M_PI, myNeckHeight / 4.);
583 gp_Ax2d anAx2d(aPnt, aDir);
584~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
585
586You will now define the curves. As previously mentioned, these thread profiles are computed on two cylindrical surfaces. In the following figure, curves on the left define the base (on *aCyl1* surface) and the curves on the right define the top of the thread's shape (on *aCyl2* surface).
587
e5bd0d98 588@image html /tutorial/images/tutorial_image016.png
589@image latex /tutorial/images/tutorial_image016.png
765b3e07 590
591You have already used the *Geom* package to define 3D geometric entities. For 2D, you will use the *Geom2d* package. As for *Geom*, all geometries are parameterized. For example, a *Geom2d_Ellipse* ellipse is defined from:
592
593 * a coordinate system whose origin is the ellipse center;
594 * a major radius on the major axis defined by the X direction of the coordinate system;
595 * a minor radius on the minor axis defined by the Y direction of the coordinate system.
596
597Supposing that:
598
599 * Both ellipses have the same major radius of 2*PI,
600 * Minor radius of the first ellipse is myNeckHeight / 10,
601 * And the minor radius value of the second ellipse is a fourth of the first one,
602
603Your ellipses are defined as follows:
604
605~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
606 Standard_Real aMajor = 2. * M_PI;
607 Standard_Real aMinor = myNeckHeight / 10;
608 Handle(Geom2d_Ellipse) anEllipse1 = new Geom2d_Ellipse(anAx2d, aMajor, aMinor);
609 Handle(Geom2d_Ellipse) anEllipse2 = new Geom2d_Ellipse(anAx2d, aMajor, aMinor / 4);
610~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
611
612To describe portions of curves for the arcs drawn above, you define *Geom2d_TrimmedCurve* trimmed curves out of the created ellipses and two parameters to limit them.
613As the parametric equation of an ellipse is P(U) = O + (MajorRadius * cos(U) * XDirection) + (MinorRadius * sin(U) * YDirection), the ellipses need to be limited between 0 and M_PI.
614
615~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
616 Handle(Geom2d_TrimmedCurve) anArc1 = new Geom2d_TrimmedCurve(anEllipse1, 0, M_PI);
617 Handle(Geom2d_TrimmedCurve) anArc2 = new Geom2d_TrimmedCurve(anEllipse2, 0, M_PI);
618~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
619
620The last step consists in defining the segment, which is the same for the two profiles: a line limited by the first and the last point of one of the arcs.
621To access the point corresponding to the parameter of a curve or a surface, you use the Value or D0 method (meaning 0th derivative), D1 method is for first derivative, D2 for the second one.
622
623~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
624 gp_Pnt2d anEllipsePnt1 = anEllipse1->Value(0);
625 gp_Pnt2d anEllipsePnt2;
626 anEllipse1->D0(M_PI, anEllipsePnt2);
627~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
628
629When creating the bottle's profile, you used classes from the *GC* package, providing algorithms to create elementary geometries.
630In 2D geometry, this kind of algorithms is found in the *GCE2d* package. Class names and behaviors are similar to those in *GC*. For example, to create a 2D segment out of two points:
631
632~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
633 Handle(Geom2d_TrimmedCurve) aSegment = GCE2d_MakeSegment(anEllipsePnt1, anEllipsePnt2);
634~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
635
636
637@subsection OCCT_TUTORIAL_SUB4_3 Building Edges and Wires
638
639
640As you did when creating the base profile of the bottle, you can now:
641
642 * compute the edges of the neck's threading.
643 * compute two wires out of these edges.
644
e5bd0d98 645@image html /tutorial/images/tutorial_image017.png
646@image latex /tutorial/images/tutorial_image017.png
765b3e07 647
648Previously, you have built:
649
650 * two cylindrical surfaces of the threading
651 * three 2D curves defining the base geometry of the threading
652
653To compute the edges out of these curves, once again use the *BRepBuilderAPI_MakeEdge* class. One of its constructors allows you to build an edge out of a curve described in the 2D parametric space of a surface.
654
655~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
656 TopoDS_Edge anEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(anArc1, aCyl1);
657 TopoDS_Edge anEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment, aCyl1);
658 TopoDS_Edge anEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(anArc2, aCyl2);
659 TopoDS_Edge anEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment, aCyl2);
660~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
661
662Now, you can create the two profiles of the threading, lying on each surface.
663
664~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
665 TopoDS_Wire threadingWire1 = BRepBuilderAPI_MakeWire(anEdge1OnSurf1, anEdge2OnSurf1);
666 TopoDS_Wire threadingWire2 = BRepBuilderAPI_MakeWire(anEdge1OnSurf2, anEdge2OnSurf2);
667~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
668
669Remember that these wires were built out of a surface and 2D curves.
670One important data item is missing as far as these wires are concerned: there is no information on the 3D curves. Fortunately, you do not need to compute this yourself, which can be a difficult task since the mathematics can be quite complex.
671When a shape contains all the necessary information except 3D curves, Open CASCADE Technology provides a tool to build them automatically. In the BRepLib tool package, you can use the *BuildCurves3d* method to compute 3D curves for all the edges of a shape.
672
673~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
674 BRepLib::BuildCurves3d(threadingWire1);
675 BRepLib::BuildCurves3d(threadingWire2);
676~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
677
678
679@subsection OCCT_TUTORIAL_SUB4_4 Creating Threading
680
681
682You have computed the wires of the threading. The threading will be a solid shape, so you must now compute the faces of the wires, the faces allowing you to join the wires, the shell out of these faces and then the solid itself. This can be a lengthy operation.
683There are always faster ways to build a solid when the base topology is defined. You would like to create a solid out of two wires. Open CASCADE Technology provides a quick way to do this by building a loft: a shell or a solid passing through a set of wires in a given sequence.
684The loft function is implemented in the *BRepOffsetAPI_ThruSections* class, which you use as follows:
685
e5bd0d98 686@image html /tutorial/images/tutorial_image018.png
687@image latex /tutorial/images/tutorial_image018.png
765b3e07 688
689 * Initialize the algorithm by creating an instance of the class. The first parameter of this constructor must be specified if you want to create a solid. By default, *BRepOffsetAPI_ThruSections* builds a shell.
690 * Add the successive wires using the AddWire method.
691 * Use the *CheckCompatibility* method to activate (or deactivate) the option that checks whether the wires have the same number of edges. In this case, wires have two edges each, so you can deactivate this option.
692 * Ask for the resulting loft shape with the Shape method.
693
694~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
695 BRepOffsetAPI_ThruSections aTool(Standard_True);
696 aTool.AddWire(threadingWire1); aTool.AddWire(threadingWire2);
697 aTool.CheckCompatibility(Standard_False);
698 TopoDS_Shape myThreading = aTool.Shape();
699~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
700
701
702@section sec5 Building the Resulting Compound
703
704
705You are almost done building the bottle. Use the *TopoDS_Compound* and *BRep_Builder* classes to build single shape from *myBody* and *myThreading*:
706
707~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
708 TopoDS_Compound aRes;
709 BRep_Builder aBuilder;
710 aBuilder.MakeCompound (aRes);
711 aBuilder.Add (aRes, myBody);
712 aBuilder.Add (aRes, myThreading);
713~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
714
715Congratulations! Your bottle is complete. Here is the result snapshot of the Tutorial application:
716
e5bd0d98 717@image html /tutorial/images/tutorial_image019.png
718@image latex /tutorial/images/tutorial_image019.png
765b3e07 719
720We hope that this tutorial has provided you with a feel for the industrial strength power of Open CASCADE Technology.
721If you want to know more and develop major projects using Open CASCADE Technology, we invite you to study our training, support, and consulting services on our site at http://www.opencascade.org/support. Our professional services can maximize the power of your Open CASCADE Technology applications.
722
723
724@section sec6 Appendix
725
726
727Complete definition of MakeBottle function (defined in the file src/MakeBottle.cxx of the Tutorial):
728
729~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
730 TopoDS_Shape MakeBottle(const Standard_Real myWidth, const Standard_Real myHeight,
731 const Standard_Real myThickness)
732 {
733 // Profile : Define Support Points
734 gp_Pnt aPnt1(-myWidth / 2., 0, 0);
735 gp_Pnt aPnt2(-myWidth / 2., -myThickness / 4., 0);
736 gp_Pnt aPnt3(0, -myThickness / 2., 0);
737 gp_Pnt aPnt4(myWidth / 2., -myThickness / 4., 0);
738 gp_Pnt aPnt5(myWidth / 2., 0, 0);
739
740 // Profile : Define the Geometry
741 Handle(Geom_TrimmedCurve) anArcOfCircle = GC_MakeArcOfCircle(aPnt2,aPnt3,aPnt4);
742 Handle(Geom_TrimmedCurve) aSegment1 = GC_MakeSegment(aPnt1, aPnt2);
743 Handle(Geom_TrimmedCurve) aSegment2 = GC_MakeSegment(aPnt4, aPnt5);
744
745 // Profile : Define the Topology
746 TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge(aSegment1);
747 TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge(anArcOfCircle);
748 TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge(aSegment2);
749 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire(anEdge1, anEdge2, anEdge3);
750
751 // Complete Profile
752 gp_Ax1 xAxis = gp::OX();
753 gp_Trsf aTrsf;
754
755 aTrsf.SetMirror(xAxis);
756 BRepBuilderAPI_Transform aBRepTrsf(aWire, aTrsf);
757 TopoDS_Shape aMirroredShape = aBRepTrsf.Shape();
758 TopoDS_Wire aMirroredWire = TopoDS::Wire(aMirroredShape);
759
760 BRepBuilderAPI_MakeWire mkWire;
761 mkWire.Add(aWire);
762 mkWire.Add(aMirroredWire);
763 TopoDS_Wire myWireProfile = mkWire.Wire();
764
765 // Body : Prism the Profile
766 TopoDS_Face myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile);
767 gp_Vec aPrismVec(0, 0, myHeight);
768 TopoDS_Shape myBody = BRepPrimAPI_MakePrism(myFaceProfile, aPrismVec);
769
770 // Body : Apply Fillets
771 BRepFilletAPI_MakeFillet mkFillet(myBody);
772 TopExp_Explorer anEdgeExplorer(myBody, TopAbs_EDGE);
773 while(anEdgeExplorer.More()){
774 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExplorer.Current());
775 //Add edge to fillet algorithm
776 mkFillet.Add(myThickness / 12., anEdge);
777 anEdgeExplorer.Next();
778 }
779
780 myBody = mkFillet.Shape();
781
782 // Body : Add the Neck
783 gp_Pnt neckLocation(0, 0, myHeight);
784 gp_Dir neckAxis = gp::DZ();
785 gp_Ax2 neckAx2(neckLocation, neckAxis);
786
787 Standard_Real myNeckRadius = myThickness / 4.;
788 Standard_Real myNeckHeight = myHeight / 10.;
789
790 BRepPrimAPI_MakeCylinder MKCylinder(neckAx2, myNeckRadius, myNeckHeight);
791 TopoDS_Shape myNeck = MKCylinder.Shape();
792
793 myBody = BRepAlgoAPI_Fuse(myBody, myNeck);
794
795 // Body : Create a Hollowed Solid
796 TopoDS_Face faceToRemove;
797 Standard_Real zMax = -1;
798
799 for(TopExp_Explorer aFaceExplorer(myBody, TopAbs_FACE); aFaceExplorer.More(); aFaceExplorer.Next()){
800 TopoDS_Face aFace = TopoDS::Face(aFaceExplorer.Current());
801 // Check if <aFace> is the top face of the bottle's neck
802 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
803 if(aSurface->DynamicType() == STANDARD_TYPE(Geom_Plane)){
804 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aSurface);
805 gp_Pnt aPnt = aPlane->Location();
806 Standard_Real aZ = aPnt.Z();
807 if(aZ > zMax){
808 zMax = aZ;
809 faceToRemove = aFace;
810 }
811 }
812 }
813
814 TopTools_ListOfShape facesToRemove;
815 facesToRemove.Append(faceToRemove);
816 myBody = BRepOffsetAPI_MakeThickSolid(myBody, facesToRemove, -myThickness / 50, 1.e-3);
817 // Threading : Create Surfaces
818 Handle(Geom_CylindricalSurface) aCyl1 = new Geom_CylindricalSurface(neckAx2, myNeckRadius * 0.99);
819 Handle(Geom_CylindricalSurface) aCyl2 = new Geom_CylindricalSurface(neckAx2, myNeckRadius * 1.05);
820
821 // Threading : Define 2D Curves
822 gp_Pnt2d aPnt(2. * M_PI, myNeckHeight / 2.);
823 gp_Dir2d aDir(2. * M_PI, myNeckHeight / 4.);
824 gp_Ax2d anAx2d(aPnt, aDir);
825
826 Standard_Real aMajor = 2. * M_PI;
827 Standard_Real aMinor = myNeckHeight / 10;
828
829 Handle(Geom2d_Ellipse) anEllipse1 = new Geom2d_Ellipse(anAx2d, aMajor, aMinor);
830 Handle(Geom2d_Ellipse) anEllipse2 = new Geom2d_Ellipse(anAx2d, aMajor, aMinor / 4);
831 Handle(Geom2d_TrimmedCurve) anArc1 = new Geom2d_TrimmedCurve(anEllipse1, 0, M_PI);
832 Handle(Geom2d_TrimmedCurve) anArc2 = new Geom2d_TrimmedCurve(anEllipse2, 0, M_PI);
833 gp_Pnt2d anEllipsePnt1 = anEllipse1->Value(0);
834 gp_Pnt2d anEllipsePnt2 = anEllipse1->Value(M_PI);
835
836 Handle(Geom2d_TrimmedCurve) aSegment = GCE2d_MakeSegment(anEllipsePnt1, anEllipsePnt2);
837 // Threading : Build Edges and Wires
838 TopoDS_Edge anEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(anArc1, aCyl1);
839 TopoDS_Edge anEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment, aCyl1);
840 TopoDS_Edge anEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(anArc2, aCyl2);
841 TopoDS_Edge anEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment, aCyl2);
842 TopoDS_Wire threadingWire1 = BRepBuilderAPI_MakeWire(anEdge1OnSurf1, anEdge2OnSurf1);
843 TopoDS_Wire threadingWire2 = BRepBuilderAPI_MakeWire(anEdge1OnSurf2, anEdge2OnSurf2);
844 BRepLib::BuildCurves3d(threadingWire1);
845 BRepLib::BuildCurves3d(threadingWire2);
846
847 // Create Threading
848 BRepOffsetAPI_ThruSections aTool(Standard_True);
849 aTool.AddWire(threadingWire1);
850 aTool.AddWire(threadingWire2);
851 aTool.CheckCompatibility(Standard_False);
852
853 TopoDS_Shape myThreading = aTool.Shape();
854
855 // Building the Resulting Compound
856 TopoDS_Compound aRes;
857 BRep_Builder aBuilder;
858 aBuilder.MakeCompound (aRes);
859 aBuilder.Add (aRes, myBody);
860 aBuilder.Add (aRes, myThreading);
861
862 return aRes;
863 }
864~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~