Nabble has removed Mailing-list integration.
Posts created here DO NOT GET SENT TO THE MAILING LIST.
Mailing-list emails DO NOT GET POSTED TO THE FORUM.
So basically the Forum is now out of date, we are looking into migrating the history.

For now you should send emails, people will see them, discuss@lists.openscad.org.

Questions from a new user

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Questions from a new user

Jeff Newbern
Hello,

I've just picked up OpenSCAD and I'm enjoying exploring the tool.  I can see that it has great potential, but I'm being slowed down by the lack of a thorough reference for the scripting language.  I have a few questions that I hope experienced users on this list will be able to answer easily.

First, it seems that the syntax for function declarations is:

function <name>([<formal arguments>]) = <expression> ;

Is there a ternary operator (something like <cond> ? <true_expr> : <false_expr>) so that function return values can be conditional?  Alternatively, is there a function syntax similar to:

function <name>([<formal arguments>]) {
  <body>
}

where the function body can contain an if-else tree with return <expression> statements?

The other thing which seems like it would be useful is to be able to query the bounding volume for an object.  This would let me write functions to automatically align, center, etc. collections of objects.

Also, if you have any general tips for a new user (coding style, proper composition, best practices, etc.) I'm sure I would benefit from your experience.

Thank you,
Jeff

Reply | Threaded
Open this post in threaded view
|

Re: Questions from a new user

Giles Bathgate

On Wed, 2010-03-10 at 22:02 -0500, Jeff Newbern wrote:
> Hello,
>
> I've just picked up OpenSCAD and I'm enjoying exploring the tool.  I
> can see that it has great potential, but I'm being slowed down by the
> lack of a thorough reference for the scripting language.  I have a few
> questions that I hope experienced users on this list will be able to
> answer easily.

Have you read http://en.wikibooks.org/wiki/OpenSCAD_User_Manual ?

> First, it seems that the syntax for function declarations is:
>
>         function <name>([<formal arguments>]) = <expression> ;

not really the syntax is
   
      function <name>(<arguments>) = <expression> ;

e.g.  r_from_dia(d) = d/2;

> Is there a ternary operator (something like <cond> ? <true_expr> :
> <false_expr>) so that function return values can be conditional?

yes there is an undocumented ternary operator the syntax is as you say
the only thing I have found is sometimes you need extra brackets


e.g.    (a==1) ? "True" : "False"

or (a>7) ? (d/2) : (d/3);

>  Alternatively, is there a function syntax similar to:
>
>         function <name>([<formal arguments>]) {
>           <body>
>         }
>
> where the function body can contain an if-else tree with return
> <expression> statements?

I don't think there is yet.

> The other thing which seems like it would be useful is to be able to
> query the bounding volume for an object.  This would let me write
> functions to automatically align, center, etc. collections of objects.
>
> Also, if you have any general tips for a new user (coding style,
> proper composition, best practices, etc.) I'm sure I would benefit
> from your experience.
>
> Thank you,
> Jeff
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad



Reply | Threaded
Open this post in threaded view
|

Re: Questions from a new user

Clifford Wolf
Hello,

On Thu, Mar 11, 2010 at 08:34:03AM +0000, Giles Bathgate wrote:
> yes there is an undocumented ternary operator the syntax is as you say
> the only thing I have found is sometimes you need extra brackets
>
> e.g.    (a==1) ? "True" : "False"
> or (a>7) ? (d/2) : (d/3);

this is a bug. I've fixed that now in the subversion trunk.

I've also added a short description of the operator to the "math operators"
section in the documentation wiki.

> >  Alternatively, is there a function syntax similar to:
> >         function <name>([<formal arguments>]) {
> >           <body>
> >         }
> > where the function body can contain an if-else tree with return
> > <expression> statements?
>
> I don't think there is yet.

ack. there is nothing like that.

regarding the third question: there is a plan to add an interface to
convert objects and even modules to values. this way we could implement
functions like volume() or boundary() that can analyse an object and return
descriptive values about the object. but this is not implemented atm.

yours,
 - clifford

--
perl -le '$_=1;(1x$_)!~/^(11+)\1+$/&&print${_}while$_++<1000'|fmt

Reply | Threaded
Open this post in threaded view
|

Datum points.

Giles Bathgate
In reply to this post by Jeff Newbern
After using openscad for a while I started to find it really difficult
to work out how to do rotations and translations always relative to the
origin.

I ended up doing things like

translate([0,370,0]) rotate([0,0,-90]) translate([0,-8.75,0])
rotate([0,0,30]) translate([5,29,0]) cube([10,10,10]);

Anyway, to save myself some bother I created a module that will
translate an object around an arbitrary point.

//p is the point you want to rotate about
//v is the polar vector
//(or whatever you call a vector of 3 angles in degrees)
module rotate_ex(p,v) {

 for(i = [0:$children-1])
  translate(p)
  rotate(v)
  translate(-p)
  child(i);

}

myDatumPoint = [15,12,5];

rotate_ex(myDatumPoint,[5,60,10]);


This is good, but not as good as it could be, I might want to for
example work out the position of a new datum based on the position
of an old.

For translation this is simple and has already been implemented

NewDatum = myDatumPoint+[1,2,3];

or more generally:

a=[1,2,3];
b=[4,5,6];

c=a+b;

Which behind the scenes openscad does this

c=[1+4,2+5,3+6];

Vector scaling by a scalar is also implemented

NewDatum = myDatumPoint*2;

again more generally

a=[1,2,3];
b=3;

c=a*b;

which behind the scenes openscad does this
c=[1*3,2*3,3*3];

This is quite useful, but in terms of translating datum points what
might be more useful would be a component-wise product

a=[1,2,3];
b=[4,5,6];

c=a.*b;

which then would do the following behind the scenes
c=[1*4,2*5,3*6];

I chose .* as an operator because matlab uses this syntax for component
wise matrix operations, * should not be used because this is not the
cross-product or anything else that is usually meant when we write
[x,y,z]*[a,b,c]

Accompanying ./ and .^ operators might also be useful (assuming you also
take up my previous proposal for an exponentiation operator)

Finally rotation I would like to be able to do

NewDatum = myDatumPoint@[0,60,0];

or more generally

a=[1,2,3];
b=[4,5,6]; //In this case we are going to use the vector as a polar
vector of 3 angles. (in degrees)

c=a@b;


behind the scenes this would have to do a rotational transformation
on the point I don't know how to do that, but it is possible for
example rotation around z axis would be:

c=[1*cos(4)-1*sin(4),2*sin(4)+2*cos(4),3];

With something like this in place I think it should be much easier to
draw object relative to a point or rotated about a point.

Regards

Giles Bathgate.