# Tutoriel d'OpenScad/Solides primitifs

Cette page nécessite une traduction. Cette page est en anglais. Si vous connaissez cette langue et le sujet de la page, vous pouvez poursuivre ou corriger la traduction.

### Cube

La fonction 'cube' crée un cube dans le premier quadrant. Lorsque la valeur 'center' (centre est vraie (true), le cube sera centré à l'origine. Les arguments sont optionnels et donnés dans l'ordre
    Paramètres:
       size
          une seule valeur, chaque arrête du cube sera de dimension x
          trilpet de valeur [x,y,z], le parallélépipède rectangle aura les dimensions x, y et z
       center
          false (par défaut), un coin est sur l'origine (0,0,0), le volume est créé dans le premier quadrant
          true, le cube est centré sur l'origine (0,0,0)
    exemples:

### Sphère

La fonction 'sphere' crée une sphère à l'orgine. En indiquant le rayon, l'argument 'r' est optionnel. Si l'utilisateur désire indiquer le diamètre, l'utilisation du paramètre 'd' est alors obligatoire.
Paramètres

#### r

    Rayon de la sphère. La résolution de la sphère dépend de sa taille et des variables $fa, $fs et $fn. Pour de plus amples informations sur ces variables consultez : Tutoriel_d'OpenScad/Autres possibilités du language

#### d

    Diamètre de la sphère. (note: d n'est valable que depuis la version 2014.03)

#### $fa

    fragments d'angles en degrés

#### $fs

    taille de fragments en millimètre

#### $fn

    résolution
} Exemples

### cylinder

Creates a cylinder or cone centered about the z axis. When center is true, it is also centered vertically along the z axis.
Parameter names are optional if given in the order shown here. If a parameter is named, all following parameters must also be named.
NOTE: If r, d, d1 or d2 are used they must be named.
    Parameters
       h : height of the cylinder or cone
       r : radius of cylinder. r1 = r2 = r.
       r1 : radius, bottom of cone.
       r2 : radius, top of cone.
       d : diameter of cylinder. r1 = r2 = d /2.
       d1 : diameter, bottom of cone. r1 = d1 /2
       d2 : diameter, top of cone. r2 = d2 /2
          (NOTE: d,d1,d2 require 2014.03 or later. Debian is currently known to be behind this)
       center
          false (default), z ranges from 0 to h
          true, z ranges from -h/2 to +h/2
       $fa : minimum angle (in degrees) of each fragment.
       $fs : minimum circumferential length of each fragment.
       $fn : fixed number of fragments in 360 degrees. Values of 3 or more override $fa and $fs
          $fa, $fs and $fn must be named. click here for more details,.
* center = false
* center = true
    use of $fn
Larger values of $fn create smoother, more circular, surfaces at the cost of longer rendering time. Some use medium values during development for the faster rendering, then change to a larger value for the final F6 rendering.
However, use of small values can produce some interesting non circular objects. A few examples are show here:
    undersized holes
When using cylinder with difference to place holes in objects, the holes will be undersized. This is because circular paths are approximated with polygons inscribed within in a circle. The points of the polygon are on the circle, but straight lines between are inside. To have all of the hole larger than the true circle, the polygon must lie wholly outside of the circle (circumscribed). Modules for circumscribed holes
Notes on accuracy Circle objects are approximated. The algorithm for doing this matters when you want 3d printed holes to be the right size. Current behavior is illustrated in a diagram . Discussion regarding optionally changing this behavior happening in a Pull Request

### polyhedron

A polyhedron is the most general 3D primitive solid. It can be used to create any regular or irregular shape including those with concave as well as convex features. Curved surfaces are approximated by a series of flat surfaces.
    Parameters
       points
          Vector of 3d points or vertices. Each point is in turn a vector, [x,y,z], of its coordinates.
          Points may be defined in any order. N points are referenced, in the order defined, as 0 to N-1.
       triangles (deprecated in version 2014.03, use faces)
          Vector of faces which collectively enclose the solid. Each face is a vector containing the indices (0 based) of 3 points from the points vector.
       faces (introduced in version 2014.03)
          Vector of faces which collectively enclose the solid. Each face is a vector containing the indices (0 based) of 3 or more points from the points vector.
          Faces may be defined in any order. Define enough faces to fully enclose the solid, with no overlap.
          Points which describe a single face must all be on the same plane.
       convexity
          Integer. The convexity parameter specifies the maximum number of faces a ray intersecting the object might penetrate. This parameter is only needed for correctly displaying the object in OpenCSG preview mode. It has no effect on the polyhedron rendering. For display problems, setting it to 10 should work fine for most cases.
All faces must have points ordered in the same direction . OpenSCAD prefers clockwise when looking at each face from outside inwards. The back is viewed from the back, the bottom from the bottom, etc..
    Example 1 Using polyhedron to generate cube( [ 10, 7, 5 ] );
    Example 2 A square base pyramid:
    Example 3 A rectangular prism:

#### Debugging polyhedra

Mistakes in defining polyhedra include not having all faces with the same order, overlap of faces and missing faces or portions of faces. As a general rule, the polyhedron faces should also satisfy (manifold conditions):
* exactly two faces should meet at any polyhedron edge.
* if two faces have a vertex in common, they should be in the same cycle face-edge around the vertex.
The first rule eliminates polyhedron like two cubes with a common edge and the second excludes polyhedron like two cubes with a common vertex.
When viewed from the outside, the points describing each face must be in the same order . OpenSCAD prefers CW, and provides a mechanism for detecting CCW. When the thrown together view (F12) is used with F5, CCW faces are shown in pink. Reorder the points for incorrect faces. Rotate the object to view all faces. The pink view can be turned off with F10.
OpenSCAD allows, temporarily, commenting out part of the face descriptions so that only the remaining faces are displayed. Use // to comment out the rest of the line. Use /* and */ to start and end a comment block. This can be part of a line or extend over several lines. Viewing only part of the faces can be helpful in determining the right points for an individual face. Note that a solid is not shown, only the faces. If using F12, all faces have one pink side. Commenting some faces helps also to show any internal face.

#### Mis-ordered faces

    Example 4 a more complex polyhedron with mis-ordered faces
When you select 'Thrown together' from the view menu and compile the design (not compile and render!) you will see a preview with the mis-oriented polygons highlighted. Unfortunately this highlighting is not possible in the OpenCSG preview mode because it would interfere with the way the OpenCSG preview mode is implemented.)
Below you can see the code and the picture of such a problematic polyhedron, the bad polygons (faces or compositions of faces) are in pink.
A correct polyhedron would be the following:
Beginner's tip:
If you don't really understand "orientation", try to identify the mis-oriented pink faces and then permute the references to the points vectors until you get it right. E.g. in the above example, the third triangle ([0,4,5]) was wrong and we fixed it as [4,0,5]. In addition, you may select "Show Edges" from the "View Menu", print a screen capture and number both the points and the faces. In our example, the points are annotated in black and the faces in blue. Turn the object around and make a second copy from the back if needed. This way you can keep track.
Clockwise Technique:
Orientation is determined by clockwise indexing. This means that if you're looking at the triangle (in this case [4,0,5]) from the outside you'll see that the path is clockwise around the center of the face. The winding order [4,0,5] is clockwise and therefore good. The winding order [0,4,5] is counter-clockwise and therefore bad. Likewise, any other clockwise order of [4,0,5] works: [5,4,0] & [0,5,4] are good too. If you use the clockwise technique, you'll always have your faces outside (outside of OpenSCAD, other programs do use counter-clockwise as the outside though).
Think of it as a Left Hand Rule:
If you hold the face and the fingers of your hand curls is the same order as the points, then your thumb points outwards.
Succinct description of a 'Polyhedron'
Each point, in the point list, is defined with a 3-tuple x,y,z position specification. Points in the point list are automatically given an identifier starting at zero for use in the faces list (0,1,2,3,... etc).
Each face, in the faces list, is defined by selecting 3 or more of the points (using the point identifier) out of the point list.
e.g. faces=[ [0,1,2] ] defines a triangle from the first point (points are zero referenced) to the second point and then to the third point.
When looking at any face from the outside, the face must list all points in a clockwise order.

#### Alternate Face Descriptions

Before 2014.03, faces could only be described via triangles. Since 2014.03, a face description can have any number of points. The points, all in the same plane, must be listed in the proper order. Since version ???, the face vertices may not be planar: OpenSCAD will do its best to internally subdivide the face in triangles. Note that this may lead to different results depending on the chosen face triangulation. If a specific result is needed, the non planar face should be broken in triangular pieces by the user.
An alternate (correct) face definition for example 4:

#### Point repetitions in a polyhedron point list

The point list of the polyhedron definition may have repetitions. When two or more points have the same coordinates they are considered the same polyhedron vertex. So, the following polyhedron:
define the same tetrahedron as: