PDA

View Full Version : What's Wrong with this script?



riki
05-18-2003, 08:42 AM
Anyone know what's wrtong with this 'Gears.ls' script by Art Howe. It creates an object with 2 inward facing polys. Has been bugging me for a long time, is it possible to fix. Sorry I'm not a programmer. BTW this is the 'Gears.ls' not 'Gear.ls' that ships with LW. It can be found in the Additional Menu.

I've also noticed that it will cuase Modeler to freeze if you select the 'Smooth' option.

Any suggestions??

-----------------------

// shut off warnings (we'll get one when getempty() is called because it
// returns 10 elements, and our array only has 1)
//
// Updated 06.08.01 Art Howe

@warnings
@version 2.3
@script modeler

@define ANGULAR 1
@define SMOOTH 2

@define XAXIS 1
@define YAXIS 2
@define ZAXIS 3

main
{
teeth = 15;
axis = Z;
rad_inner = .7;
rad_outer = 1.0;
thickness = .5;
geartype = 1;

cntr = <0,0,0>;

x = 1;

// request some parameters

reqbegin("Gear");

c1 = ctlchoice("Axis",axis,@"X","Y","Z"@);

c2 = ctlinteger("Number of Teeth",teeth);
c3 = ctldistance("Inner Radius",rad_inner);
c4 = ctldistance("Outer Radius",rad_outer);
c5 = ctldistance("Thickness",thickness);
c6 = ctlchoice("Gear Type",geartype,@"Angular","Smooth"@);
c7 = ctlvector("Center",cntr);

return if !reqpost();

axis = getvalue(c1);
teeth = getvalue(c2);
rad_inner = getvalue(c3);
rad_outer = getvalue(c4);
thickness = getvalue(c5);
geartype = getvalue(c6);
cntr = getvalue(c7);

cx = cntr.x;
cy = cntr.y;
cz = cntr.z - (thickness / 2);

reqend();

t_ang = 360 / teeth / 57.2957794;

lyrsetfg(1);

editbegin();

moninit(teeth,"Generating gear...");

for(i = 0;i < teeth;i++)
{
a1 = t_ang * i;
a2 = a1 + (t_ang * 3 / 6);
a3 = a1 + (t_ang * 4 / 6);
a4 = a1 + (t_ang * 5 / 6);

pt[1] = (rad_inner * sin(a1) + cx);
pt[2] = (rad_inner * cos(a1) + cy);
pt[3] = cz;
ptID[x] = addpoint(pt);
++x;

pt[1] = (rad_inner * sin(a2) + cx);
pt[2] = (rad_inner * cos(a2) + cy);
pt[3] = cz;
ptID[x] = addpoint(pt);
++x;

pt[1] = (rad_outer * sin(a3) + cx);
pt[2] = (rad_outer * cos(a3) + cy);
pt[3] = cz;
ptID[x] = addpoint(pt);
++x;

pt[1] = (rad_outer * sin(a4) + cx);
pt[2] = (rad_outer * cos(a4) + cy);
pt[3] = cz;
ptID[x] = addpoint(pt);

++x;

if(monstep())
{
editend(ABORT); // any non-zero value will
// will abort the operation
return;
}
}

if(geartype == ANGULAR)
addpolygon(ptID,"Gear");
else // Smooth
addcurve(ptID,"Gear");

editend();
monend();

if(geartype == SMOOTH)
freezecurves();

extrude(Z,thickness);

// punch a hole through the center.

(bglayer) = lyrempty();

// only punch a hole if we can get an empty layer to work in

if(bglayer != nil)
{
lyrsetfg(bglayer);

// Make hole diameter 25% of the gear's radius

makedisc(rad_outer * .25, // radius
-.5, // top
thickness + 1, // bottom
Z, // along the Z axis
32, // 32 sides
1, // 1 segment
<cx,cy,cz>); // center vector

lyrsetfg(1);
lyrsetbg(bglayer);

boolean(SUBTRACT);

lyrsetfg(bglayer);
delete();

lyrsetfg(1);
}
else
info("Need at least one empty layer to punch center hole");

switch(axis)
{
case XAXIS:
rotate(90,Y);
break;

case YAXIS:
rotate(90,X);
break;
}
}

Karsten Lueth
05-25-2003, 04:10 AM
Hi riki,
its the boolean(SUBTRACT) operation that causes the error. You can check that by adding a "return;" statement just before the boolean op and then do the boole manually.
Problably the front and back faces are just too complex for the boole op. You could add a "triple();" command after the extrude command to solve this problem, but that would increase the number of polys significantly.

peder
05-27-2003, 08:11 AM
you can fix the flipped polys by inserting "alignpols();" at the position shown below.

boolean(SUBTRACT);
alignpols();

and to fix it for Smooth mode:

if(geartype == SMOOTH)
{
freezecurves();
flip();
}

Peder

riki
05-27-2003, 08:54 AM
Thanks for the replys, I tried peder's method which fixed the poly alignment problem. But the fix for smooth caused Modeler to freeze up . Not sure if I made a mistake typing the code?? Could you take a quick peek.

// shut off warnings (we'll get one when getempty() is called because it
// returns 10 elements, and our array only has 1)
//
// Updated 06.08.01 Art Howe

@warnings
@version 2.3
@script modeler

@define ANGULAR 1
@define SMOOTH 2

@define XAXIS 1
@define YAXIS 2
@define ZAXIS 3

main
{
teeth = 15;
axis = Z;
rad_inner = .7;
rad_outer = 1.0;
thickness = .5;
geartype = 1;

cntr = <0,0,0>;

x = 1;

// request some parameters

reqbegin("Gear");

c1 = ctlchoice("Axis",axis,@"X","Y","Z"@);

c2 = ctlinteger("Number of Teeth",teeth);
c3 = ctldistance("Inner Radius",rad_inner);
c4 = ctldistance("Outer Radius",rad_outer);
c5 = ctldistance("Thickness",thickness);
c6 = ctlchoice("Gear Type",geartype,@"Angular","Smooth"@);
c7 = ctlvector("Center",cntr);

return if !reqpost();

axis = getvalue(c1);
teeth = getvalue(c2);
rad_inner = getvalue(c3);
rad_outer = getvalue(c4);
thickness = getvalue(c5);
geartype = getvalue(c6);
cntr = getvalue(c7);

cx = cntr.x;
cy = cntr.y;
cz = cntr.z - (thickness / 2);

reqend();

t_ang = 360 / teeth / 57.2957794;

lyrsetfg(1);

editbegin();

moninit(teeth,"Generating gear...");

for(i = 0;i < teeth;i++)
{
a1 = t_ang * i;
a2 = a1 + (t_ang * 3 / 6);
a3 = a1 + (t_ang * 4 / 6);
a4 = a1 + (t_ang * 5 / 6);

pt[1] = (rad_inner * sin(a1) + cx);
pt[2] = (rad_inner * cos(a1) + cy);
pt[3] = cz;
ptID[x] = addpoint(pt);
++x;

pt[1] = (rad_inner * sin(a2) + cx);
pt[2] = (rad_inner * cos(a2) + cy);
pt[3] = cz;
ptID[x] = addpoint(pt);
++x;

pt[1] = (rad_outer * sin(a3) + cx);
pt[2] = (rad_outer * cos(a3) + cy);
pt[3] = cz;
ptID[x] = addpoint(pt);
++x;

pt[1] = (rad_outer * sin(a4) + cx);
pt[2] = (rad_outer * cos(a4) + cy);
pt[3] = cz;
ptID[x] = addpoint(pt);

++x;

if(monstep())
{
editend(ABORT); // any non-zero value will
// will abort the operation
return;
}
}

if(geartype == ANGULAR)
addpolygon(ptID,"Gear");
else // Smooth
addcurve(ptID,"Gear");

editend();
monend();

if(geartype == SMOOTH)
{
freezecurves();
flip();
}

extrude(Z,thickness);

// punch a hole through the center.

(bglayer) = lyrempty();

// only punch a hole if we can get an empty layer to work in

if(bglayer != nil)
{
lyrsetfg(bglayer);

// Make hole diameter 25% of the gear's radius

makedisc(rad_outer * .25, // radius
-.5, // top
thickness + 1, // bottom
Z, // along the Z axis
32, // 32 sides
1, // 1 segment
<cx,cy,cz>); // center vector

lyrsetfg(1);
lyrsetbg(bglayer);

boolean(SUBTRACT);

lyrsetfg(bglayer);
delete();

lyrsetfg(1);
alignpols();
}
else
info("Need at least one empty layer to punch center hole");

switch(axis)
{
case XAXIS:
rotate(90,Y);
break;

case YAXIS:
rotate(90,X);
break;
}
}

peder
05-27-2003, 09:09 AM
The code corrections you have done seems right.

Are you sure you are not just impatient??? The smoth option takes a while to perform.

Try to let it run for a while after it freezes.

On my setup it freezes for a couple of minutes, but it comes back to life :-)

Peder

riki
05-27-2003, 09:26 AM
Just tried it again, this time I waited about 2 minutes but still no luck. Not sure what's going wrong??? Maybe it's a platform issue?? I'm on a Mac OS9.2 with LW 7.5c

peder
05-27-2003, 02:11 PM
Try this!

info("About to freeze curves");
freezecurves();
info("Curves frozen");
flip();

This will show you if it is the freeze command that is locking up modeler, witch is my guess. This is also the command i suspect is the only heavy processing candidate in this script.

If you only get the first message "About to freeze curves" my suggestion is to leave it running a while (at least 5 minutes) just to see if it snaps out of it. :-)

If you get both messages and then it freezes ... well then we'll have to take it from there.

By the way i'm running 7.5b on a Intel 2ghz.

Good luck

Peder

riki
05-27-2003, 09:05 PM
Thanks Peder, I tried your change and just left it for about 5mins and what do you know, it worked.

This is the Smooth version

http://www.suture.net/files/0012.jpg

But now Angular is messed up

http://www.suture.net/files/0013.jpg

peder
05-28-2003, 01:12 AM
Are you sure you still have the alignpols(); statement in there?
Are you using the default settings/values? If not please post them!

It works for me!

Peder

riki
05-28-2003, 01:22 AM
Thanks peder can you post the working version, that's probably the easiest way to check.

peder
05-28-2003, 01:50 AM
The last one you posted workes fine!

Peder

riki
05-28-2003, 01:55 AM
Yes I think so, is was just my impatience. Now how do we go about getting Newtek to update their version?? :-)

peder
05-29-2003, 03:02 AM
Already sent them the update :-)

Peder