discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

A small script to make a ruler

T
Troberg
Thu, Oct 5, 2017 11:52 AM

I occasionally find it useful to make measurements in a model (in complex
models, it's not always easy to get the distance from A to B by
calculations, and you just need a ballpark figure), or simply have a size
reference (for example, when designing my porch, I need something to relate
to, to see how it'll work in reality).

So, I made this simple little script, which draws a ruler of arbitrary
length, with only the scales needed for the actual length drawn (so, for a
half meter ruler, I don't draw meter steps).

Just include with use <filename>, and call it to draw the ruler. You can, of
course, use rotate and translate to move it around as needed (or, if you
like, scal and resize, but that wouldn't make much sense...).

It's also a simple example of parametric design.

Hope you'll find it useful!

Here goes:

// Draws a ruler of specified length.
// Origin of the ruler is at 0, and it is laid out along the X axis.
// Call with: ruler(length);
module ruler(length){
difference(){
//draw ruler
union(){
offset=0;
for(n=[1:4]){
offset=pow(2,n+1)-2;
translate([0,-offset,0])
drawscale(length,pow(10,n-1),pow(2,n));
}
}
//cut of protruding end bits
color([0,0,0])
translate([length,-38,-1])
cube([1000,38,3]);
}
}

//Draws the different scales, as needed. Don't call directly.
module drawscale(length,step,width){
if(length>step){
for(n=[0:step:length-1]){
if(n%(2*step)==0){
color([0,0,0])
translate([n,0,0])
cube([step,width,1]);
}else{
color([1,1,1])
translate([n,0,0])
cube([step,width,1]);
}
}
}
}

//Test
ruler(1201);

--
Sent from: http://forum.openscad.org/

I occasionally find it useful to make measurements in a model (in complex models, it's not always easy to get the distance from A to B by calculations, and you just need a ballpark figure), or simply have a size reference (for example, when designing my porch, I need something to relate to, to see how it'll work in reality). So, I made this simple little script, which draws a ruler of arbitrary length, with only the scales needed for the actual length drawn (so, for a half meter ruler, I don't draw meter steps). Just include with use <filename>, and call it to draw the ruler. You can, of course, use rotate and translate to move it around as needed (or, if you like, scal and resize, but that wouldn't make much sense...). It's also a simple example of parametric design. Hope you'll find it useful! Here goes: // Draws a ruler of specified length. // Origin of the ruler is at 0, and it is laid out along the X axis. // Call with: ruler(length); module ruler(length){ difference(){ //draw ruler union(){ offset=0; for(n=[1:4]){ offset=pow(2,n+1)-2; translate([0,-offset,0]) drawscale(length,pow(10,n-1),pow(2,n)); } } //cut of protruding end bits color([0,0,0]) translate([length,-38,-1]) cube([1000,38,3]); } } //Draws the different scales, as needed. Don't call directly. module drawscale(length,step,width){ if(length>step){ for(n=[0:step:length-1]){ if(n%(2*step)==0){ color([0,0,0]) translate([n,0,0]) cube([step,width,1]); }else{ color([1,1,1]) translate([n,0,0]) cube([step,width,1]); } } } } //Test ruler(1201); -- Sent from: http://forum.openscad.org/
WA
William Adams
Thu, Oct 5, 2017 12:42 PM

Elegant!

License?

On Thu, Oct 5, 2017 at 7:52 AM, Troberg troberg.anders@gmail.com wrote:

I occasionally find it useful to make measurements in a model (in complex
models, it's not always easy to get the distance from A to B by
calculations, and you just need a ballpark figure), or simply have a size
reference (for example, when designing my porch, I need something to relate
to, to see how it'll work in reality).

So, I made this simple little script, which draws a ruler of arbitrary
length, with only the scales needed for the actual length drawn (so, for a
half meter ruler, I don't draw meter steps).

Just include with use <filename>, and call it to draw the ruler. You can,
of
course, use rotate and translate to move it around as needed (or, if you
like, scal and resize, but that wouldn't make much sense...).

It's also a simple example of parametric design.

Hope you'll find it useful!

Here goes:

// Draws a ruler of specified length.
// Origin of the ruler is at 0, and it is laid out along the X axis.
// Call with: ruler(length);
module ruler(length){
difference(){
//draw ruler
union(){
offset=0;
for(n=[1:4]){
offset=pow(2,n+1)-2;
translate([0,-offset,0])
drawscale(length,pow(10,n-1),pow(2,n));
}
}
//cut of protruding end bits
color([0,0,0])
translate([length,-38,-1])
cube([1000,38,3]);
}
}

//Draws the different scales, as needed. Don't call directly.
module drawscale(length,step,width){
if(length>step){
for(n=[0:step:length-1]){
if(n%(2*step)==0){
color([0,0,0])
translate([n,0,0])
cube([step,width,1]);
}else{
color([1,1,1])
translate([n,0,0])
cube([step,width,1]);
}
}
}
}

//Test
ruler(1201);

--
Sent from: http://forum.openscad.org/


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

Elegant! License? On Thu, Oct 5, 2017 at 7:52 AM, Troberg <troberg.anders@gmail.com> wrote: > I occasionally find it useful to make measurements in a model (in complex > models, it's not always easy to get the distance from A to B by > calculations, and you just need a ballpark figure), or simply have a size > reference (for example, when designing my porch, I need something to relate > to, to see how it'll work in reality). > > So, I made this simple little script, which draws a ruler of arbitrary > length, with only the scales needed for the actual length drawn (so, for a > half meter ruler, I don't draw meter steps). > > Just include with use <filename>, and call it to draw the ruler. You can, > of > course, use rotate and translate to move it around as needed (or, if you > like, scal and resize, but that wouldn't make much sense...). > > It's also a simple example of parametric design. > > Hope you'll find it useful! > > Here goes: > > // Draws a ruler of specified length. > // Origin of the ruler is at 0, and it is laid out along the X axis. > // Call with: ruler(length); > module ruler(length){ > difference(){ > //draw ruler > union(){ > offset=0; > for(n=[1:4]){ > offset=pow(2,n+1)-2; > translate([0,-offset,0]) > drawscale(length,pow(10,n-1),pow(2,n)); > } > } > //cut of protruding end bits > color([0,0,0]) > translate([length,-38,-1]) > cube([1000,38,3]); > } > } > > //Draws the different scales, as needed. Don't call directly. > module drawscale(length,step,width){ > if(length>step){ > for(n=[0:step:length-1]){ > if(n%(2*step)==0){ > color([0,0,0]) > translate([n,0,0]) > cube([step,width,1]); > }else{ > color([1,1,1]) > translate([n,0,0]) > cube([step,width,1]); > } > } > } > } > > //Test > ruler(1201); > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
J
jon
Thu, Oct 5, 2017 6:31 PM

I grabbed the code from the original message, and tried to save it as
"ruler.scad", but found the following source code already in
"ruler.scad" on my system.  I assume that I grabbed it from this list a
while back.

module rule(s,dir=[1,0,0],c1=[1,0.5,0.5],c2=[0,0,1]) {
    l = floor(s);
    r_to(dir)
    scale(1/l)
    for(i=[0:l-1]) {
       colour(i,c1,c2) %
       if(i==0)
         translate([l,0,0]) rotate([0,-90,0]) tip(l);
       else if(i==l-1) // the last tip may longer than 1
         translate([il,0,0]) rotate([0,90,0]) tip(l(1+s-l));
       else if(!i%10 && i!=0) translate([i*l,0,0])
         translate([l-l/4,0,-l/8]) linear_extrude(height=l/4)
            rotate(90) text(str(i),halign="center",size=l/2);
       else
         translate([(i+0.5)*l,0,0]) cube([l,l/2,l/2],center=true);
    }
    module tip(s)
        linear_extrude(height=s, scale=0.01) square(l/2,center=true);
    module colour(i,c1,c2)
        if(i%2) color(c2) children();
        else    color(c1) children();
    module r_to(d) // rotate_from_to
       if(norm(d)<1e-16) rotate([0,-90,0]) children();
       else mirror(d/norm(d)+[1,0,0]) mirror([1,0,0]) children();
}

rotate([30,70,15])
{
rule(10*sqrt(3),dir=[1,1,1]);
%cube(10);
}

On 10/5/2017 7:52 AM, Troberg wrote:

I occasionally find it useful to make measurements in a model (in complex
models, it's not always easy to get the distance from A to B by
calculations, and you just need a ballpark figure), or simply have a size
reference (for example, when designing my porch, I need something to relate
to, to see how it'll work in reality).

So, I made this simple little script, which draws a ruler of arbitrary
length, with only the scales needed for the actual length drawn (so, for a
half meter ruler, I don't draw meter steps).

Just include with use <filename>, and call it to draw the ruler. You can, of
course, use rotate and translate to move it around as needed (or, if you
like, scal and resize, but that wouldn't make much sense...).

It's also a simple example of parametric design.

Hope you'll find it useful!

Here goes:

// Draws a ruler of specified length.
// Origin of the ruler is at 0, and it is laid out along the X axis.
// Call with: ruler(length);
module ruler(length){
difference(){
//draw ruler
union(){
offset=0;
for(n=[1:4]){
offset=pow(2,n+1)-2;
translate([0,-offset,0])
drawscale(length,pow(10,n-1),pow(2,n));
}
}
//cut of protruding end bits
color([0,0,0])
translate([length,-38,-1])
cube([1000,38,3]);
}
}

//Draws the different scales, as needed. Don't call directly.
module drawscale(length,step,width){
if(length>step){
for(n=[0:step:length-1]){
if(n%(2*step)==0){
color([0,0,0])
translate([n,0,0])
cube([step,width,1]);
}else{
color([1,1,1])
translate([n,0,0])
cube([step,width,1]);
}
}
}
}

//Test
ruler(1201);

--
Sent from: http://forum.openscad.org/


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

--
Sent from my desktop computer.
I do not receive emails while away from my desk,
nor do I receive texts on my main phone number
(which is a land line).
If you know that I am on the road, please text me.
If you know that I am home, please email me.

I grabbed the code from the original message, and tried to save it as "ruler.scad", but found the following source code already in "ruler.scad" on my system.  I assume that I grabbed it from this list a while back. module rule(s,dir=[1,0,0],c1=[1,0.5,0.5],c2=[0,0,1]) {     l = floor(s);     r_to(dir)     scale(1/l)     for(i=[0:l-1]) {        colour(i,c1,c2) %        if(i==0)          translate([l,0,0]) rotate([0,-90,0]) tip(l);        else if(i==l-1) // the last tip may longer than 1          translate([i*l,0,0]) rotate([0,90,0]) tip(l*(1+s-l));        else if(!i%10 && i!=0) translate([i*l,0,0])          translate([l-l/4,0,-l/8]) linear_extrude(height=l/4)             rotate(90) text(str(i),halign="center",size=l/2);        else          translate([(i+0.5)*l,0,0]) cube([l,l/2,l/2],center=true);     }     module tip(s)         linear_extrude(height=s, scale=0.01) square(l/2,center=true);     module colour(i,c1,c2)         if(i%2) color(c2) children();         else    color(c1) children();     module r_to(d) // rotate_from_to        if(norm(d)<1e-16) rotate([0,-90,0]) children();        else mirror(d/norm(d)+[1,0,0]) mirror([1,0,0]) children(); } rotate([30,70,15]) { rule(10*sqrt(3),dir=[1,1,1]); %cube(10); } On 10/5/2017 7:52 AM, Troberg wrote: > I occasionally find it useful to make measurements in a model (in complex > models, it's not always easy to get the distance from A to B by > calculations, and you just need a ballpark figure), or simply have a size > reference (for example, when designing my porch, I need something to relate > to, to see how it'll work in reality). > > So, I made this simple little script, which draws a ruler of arbitrary > length, with only the scales needed for the actual length drawn (so, for a > half meter ruler, I don't draw meter steps). > > Just include with use <filename>, and call it to draw the ruler. You can, of > course, use rotate and translate to move it around as needed (or, if you > like, scal and resize, but that wouldn't make much sense...). > > It's also a simple example of parametric design. > > Hope you'll find it useful! > > Here goes: > > // Draws a ruler of specified length. > // Origin of the ruler is at 0, and it is laid out along the X axis. > // Call with: ruler(length); > module ruler(length){ > difference(){ > //draw ruler > union(){ > offset=0; > for(n=[1:4]){ > offset=pow(2,n+1)-2; > translate([0,-offset,0]) > drawscale(length,pow(10,n-1),pow(2,n)); > } > } > //cut of protruding end bits > color([0,0,0]) > translate([length,-38,-1]) > cube([1000,38,3]); > } > } > > //Draws the different scales, as needed. Don't call directly. > module drawscale(length,step,width){ > if(length>step){ > for(n=[0:step:length-1]){ > if(n%(2*step)==0){ > color([0,0,0]) > translate([n,0,0]) > cube([step,width,1]); > }else{ > color([1,1,1]) > translate([n,0,0]) > cube([step,width,1]); > } > } > } > } > > //Test > ruler(1201); > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > > -- Sent from my desktop computer. I do not receive emails while away from my desk, nor do I receive texts on my main phone number (which is a land line). If you know that I am on the road, please text me. If you know that I am home, please email me.
T
Troberg
Thu, Oct 5, 2017 6:46 PM

License is "Do as thou wilt shall be the whole of the law!".

It's trivial code, took me 15 minutes to make. I don't really care about
trying to get rich of it or if people modify it. Enjoy it however you want.

--
Sent from: http://forum.openscad.org/

License is "Do as thou wilt shall be the whole of the law!". It's trivial code, took me 15 minutes to make. I don't really care about trying to get rich of it or if people modify it. Enjoy it however you want. -- Sent from: http://forum.openscad.org/
S
stressless
Fri, Oct 6, 2017 11:03 AM

Yes, nice. Thanks !
However, what i'd like to see is something like this :
...
X_ruler(origin_X, offset_y, offset_Z, length, color);
Y_ruler(origin_Y, offset_X, offset_Z, length, color);
Z_ruler(origin_Z, offset_X, offset_Y, length, color);
...
This would allow us to have 3 rulers and move them around to have a quick
view while constructing.
Why don't I do it myself ? Hummm...persistent lack of proficiency...

Thanks again, Dan.


Troberg wrote

License is "Do as thou wilt shall be the whole of the law!".

It's trivial code, took me 15 minutes to make. I don't really care about
trying to get rich of it or if people modify it. Enjoy it however you
want.

--
Sent from: http://forum.openscad.org/


OpenSCAD mailing list

Discuss@.openscad

Yes, nice. Thanks ! However, what i'd like to see is something like this : ... X_ruler(origin_X, offset_y, offset_Z, length, color); Y_ruler(origin_Y, offset_X, offset_Z, length, color); Z_ruler(origin_Z, offset_X, offset_Y, length, color); ... This would allow us to have 3 rulers and move them around to have a quick view while constructing. Why don't I do it myself ? Hummm...persistent lack of proficiency... Thanks again, Dan. * * * * * * * * * * * Troberg wrote > License is "Do as thou wilt shall be the whole of the law!". > > It's trivial code, took me 15 minutes to make. I don't really care about > trying to get rich of it or if people modify it. Enjoy it however you > want. > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@.openscad > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org -- Sent from: http://forum.openscad.org/