OpenScad beginners tutorial: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
Line 119: Line 119:
</source>
</source>


== Merging of objects ==
== CSG Modelling ==
 
=== Merging of objects ===


The result of union will be "baked", i.e. if you stack up cubes aligned against each other, the result will be one shape (no internal walls). This is ideal behavior for rapman printing
The result of union will be "baked", i.e. if you stack up cubes aligned against each other, the result will be one shape (no internal walls). This is ideal behavior for rapman printing
Line 128: Line 130:
  }
  }


=== Difference ===
Make sure that one of the difference object fully touches with a face. Else the resulting object will be manyfold ....
<source>
difference() {    
    union () {
cylinder(h= height_mm, r1 = bottom_r_mm, r2 = top_r_mm, center = true);
// add an extra x and y
cube (size = [bottom_r_mm*2 + 2, bottom_r_mm*2 + 2, height_mm], center = true);
    }
    // make the cubes real big, it doesn't matter here
    // forward
    translate ([ 0,  -(cube_width/2 + bottom_r_mm),  0])
{
    cube (size = [cube_width, cube_width, height_mm+4], center = true);
}
    // left
    translate ([ cube_width/2 + bottom_r_mm,      0,    0])
{
    cube(size = [cube_width, cube_width, height_mm+4], center = true);
}
    // right
    translate ([ -(cube_width/2 + bottom_r_mm),  0,  0])
{
    cube(size = [cube_width, cube_width, height_mm+4], center = true);
}
}
</source>


== Global dynamic variables ==
== Global dynamic variables ==

Revision as of 00:22, 22 March 2010

Draft

Introduction

“OpenSCAD is a software for creating solid 3D CAD objects. It is free software and available for Linux/UNIX, MS Windows and Apples OS X. Unlike most free software for creating 3D models (such as the famous application Blender) it does not focus on the artistic aspects of 3D modelling but instead on the CAD aspects. Thus it might be the application you are looking for when you are planning to create 3D models of machine parts but pretty sure is not what you are looking for when you are more interested in creating computer-animated movies. OpenSCAD is not an interactive modeller. Instead it is something like a 3D-compiler that reads in a script file that describes the object and renders the 3D model from this script file (see examples below). This gives you (the designer) full control over the modelling process and enables you to easily change any step in the modelling process or make designes that are defined by configurable parameters.” (http://openscad.org/, retrieved 22:58, 17 March 2010 (UTC)

Compiling code

To compile (see the result)
  • menu: Compile
  • or menu: Reload and compile. I prefer using an exteranl editor (i.e. emacs to get "real" indentation support and good syntax high-lighting in java mode).
To render for real and to export
  • menu: Design-> Compile and render
  • menu: Design-> Export as .STL

Example code

This code is a derivative of the parametric lego duplo Domonoky. We also uploaded it to Thingyverse.

//the duplo itself
// parameters are: 
// width: 1 =standard 4x4 duplo with.
// length: 1= standard 4x4 duplo length
// height: 1= minimal duplo height
// nibbles: true or false
 
duplo(1,1,3,true);


module duplo(width,length,height,nibbles) 
{
	//size definitions
	
	ns = 8.4;  //nibble start offset
	no = 6.53; //nibbleoffset
	nbo = 16; // nibble bottom offset
	
	duplowidth = 31.66;
	duplolength=31.66;
	duploheight=9.6;
	duplowall = 1.55;	
	
	
	//the cube
	difference() {
		cube([width*duplowidth,length*duplolength,height*duploheight],true);
		translate([0,0,-duplowall]) 	
			cube([width*duplowidth - 2*duplowall,length*duplolength-2*duplowall,height*duploheight],true);
	}

	//nibbles on top
         if  (nibbles)
     	   {
	    	for(j=[1:length])
		{
			for (i = [1:width])
			{
				translate([i*ns+(i-1)*no,j*ns+(j-1)*no,6.9+(height-1)*duploheight/2]) duplonibble();
				translate([i*-ns+(i-1)*-no,j*ns+(j-1)*no,6.9+(height-1)*duploheight/2]) duplonibble();
				translate([i*ns+(i-1)*no,j*-ns+(j-1)*-no,6.9+(height-1)*duploheight/2]) duplonibble();
				translate([i*-ns+(i-1)*-no,j*-ns+(j-1)*-no,6.9+(height-1)*duploheight/2]) duplonibble();
			}
		}
	        }

	//nibble bottom
	for(j=[1:length])
	{
		for (i = [1:width])
		{
			translate([(i-1)*nbo,(j-1)*nbo,0]) duplobottomnibble(height*duploheight);
			translate([(i-1)*-nbo,(j-1)*-nbo,0]) duplobottomnibble(height*duploheight);
			translate([(i-1)*-nbo,(j-1)*nbo,0]) duplobottomnibble(height*duploheight);
			translate([(i-1)*nbo,(j-1)*-nbo,0]) duplobottomnibble(height*duploheight);
		}
	}
	//little walls inside[http://www.thingiverse.com/Domonoky Domonoky]
	difference() 
	{
		union()
	 	{
			for(j=[1:length])
			{	
				for (i = [1:width])
				{
					translate([0,j*ns+(j-1)*no,0 ]) cube([width*duplowidth,1.35,height*duploheight],true);
					translate([0,j*-ns+(j-1)*-no,0 ]) cube([width*duplowidth,1.35,height*duploheight],true);
					translate([i*ns+(i-1)*no,0,0 ]) cube([1.35,length*duplolength,,height*duploheight],true);
					translate([i*-ns+(i-1)*-no,0,0 ]) cube([1.35,length*duplolength,height*duploheight],true);
				}
			}
		}
		cube([width*duplowidth - 4*duplowall,length*duplolength-4*duplowall,height*duploheight+2],true);
	}
}


module duplonibble()
{
	difference() {
		cylinder(r=4.7,h=4.5,center=true,$fs = 0.01);
		cylinder(r=3.4,h=5.5,center=true,$fs = 0.01);
	}
}

module duplobottomnibble(height)[http://www.thingiverse.com/Domonoky Domonoky]
{
	difference() {
		cylinder(r=6.6,h=height,center=true,$fs = 0.01);
		cylinder(r=5.3,h=height+1,center=true,$fs = 0.01);
	}

}

CSG Modelling

Merging of objects

The result of union will be "baked", i.e. if you stack up cubes aligned against each other, the result will be one shape (no internal walls). This is ideal behavior for rapman printing

union() {
  translate(0,0,x) import_stl("duck.stl");
  duplo(2,2,1.5,false);
}


Difference

Make sure that one of the difference object fully touches with a face. Else the resulting object will be manyfold ....

	difference() {	     
		    union () {
			cylinder(h= height_mm, r1 = bottom_r_mm, r2 = top_r_mm, center = true);
			// add an extra x and y
			cube (size = [bottom_r_mm*2 + 2, bottom_r_mm*2 + 2, height_mm], center = true);
		    }
		    // make the cubes real big, it doesn't matter here
		    // forward
		    translate ([ 0,  -(cube_width/2 + bottom_r_mm),   0])
			{
			    cube (size = [cube_width, cube_width, height_mm+4], center = true);
			}
		    // left
		    translate ([ cube_width/2 + bottom_r_mm,      0,    0])
			{
			    cube(size = [cube_width, cube_width, height_mm+4], center = true);
			}
		    // right
		    translate ([ -(cube_width/2 + bottom_r_mm),   0,   0])
			{
			    cube(size = [cube_width, cube_width, height_mm+4], center = true);
			}
		}

Global dynamic variables

The $fa, $fs and $fn special variables control the number of facets used to generate an arc. You can override these in each module (i.e. more precisely they work with dynamic scoping)

  • $fa is the angle of the fragments. It sets the minimum angle for a fragment. Even a huge circle does not have more fragments than 360 divided by this number. The default value is 12 (i.e. 30 fragments for a full circle).
  • $fs is the size of the fragments and defines the minimum size of a fragment. Because of this variable very small circles have a smaller number of fragments then specified using $fa. The default value is 1.
  • $fn is the number of fragments. I is usually 0. When this variable has a value greater than zero, the other two variables are ignored and full cirle is rendered using this number of fragments. The default value is 0.

When $fa and $fs are used to determine the number of fragments for a circle, then OpenSCAD will never use less than 5 fragments.

The shape of the circle can be improved by setting one of the special parameters $fn, $fs, and $fa.

Links

Tutorials
Other stuff

Credits and copyright modification