discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Error in Code; Don't know what's wrong there

MP
Manfred Preussig
Sun, Jan 8, 2017 11:32 PM

Hello,
first the self-discrption:
I'm using OpenSCad to make small devices than printed by my 3d-printer.
I'm living in germany, male and 52 years old.

My special interests are:
computer
astronomy
my pet (a cute little dog)

Now the reason for this post:
This is the code of a small part of an object. It will be a case for some electronic parts. The
modules here are for fixing the case or some other parts to it. The german module names
mean a block to fix a bold with a nut. Since in the original (complete) case the block is
covered by a second (round) element the nut must be insertable from side to this inner
cut-out. So I wanted to calculate the correct position for the cube making the cut-off to the
block (at one side; Seite means a variable later used to distinguish the position of the cut-off
relative to the whole block).
[code]
module hole(pos=[0,0,0],d=1,h=1,res=15) {
translate(pos)
cylinder($fn=res,h,d1=d,d2=d,center=false);
}
module Schraubklotz(pos=[0,0,0],size=[0,0,0],dir=0,Mpos=0,Res=15,Inn=3) {
translate(pos) {
difference() {
cube(size);
translate([-pos[0],-pos[1],-0.1]) {
hole([(pos[0]+size[0]/2),(pos[1]+size[1]/2),0],Inn,size[2]+0.2,res=Res);
}
}
}
}

module Schraubklotz(pos=[0,0,0],size=[0,0,0],dir=0,Mpos=0,Res=15,Inn=3) {
    translate(pos) {
        difference() {
            cube(size);
            translate([-pos[0],-pos[1],-0.1]) {
            hole([(pos[0]+size[0]/2),(pos[1]+size[1]/2),0],Inn,size[2]+0.2,res=Res);
            }
        }
    }
}

module SK(Pos=[0,0,0],Size=[0,0,0],Dir=0,MPos=0,RES=15,INN=3,Seite=0) {
	
	help = ( Size[1] - 2 * INN )/ 2;
	
	Pos[1]=Pos[1]+help;
	
	difference() {
		Schraubklotz(Pos,Size,Dir,MPos,RES,INN);
		translate(Pos) cube([2*INN,2*INN,Size[2]+0.2]);
	}
}

Schraubklotz([0,0,0],[10,10,10],0,0,6,3);

[/code]
The error is in module SK at the line 'help = ( Size[1] - 2 * INN )/ 2;' where the '2' is marked
red every time the preview' or 'render' functions are activated. I can't find out the reason for
the error. In my opinion the code is correct. 'help' is for calculating the correct position of the
cube inside the difference to make the cut-off at side. This special part is for corrrection of
y-position normally not really helpful.

Many thanks to all helping

Manfred

Hello, first the self-discrption: I'm using OpenSCad to make small devices than printed by my 3d-printer. I'm living in germany, male and 52 years old. My special interests are: computer astronomy my pet (a cute little dog) Now the reason for this post: This is the code of a small part of an object. It will be a case for some electronic parts. The modules here are for fixing the case or some other parts to it. The german module names mean a block to fix a bold with a nut. Since in the original (complete) case the block is covered by a second (round) element the nut must be insertable from side to this inner cut-out. So I wanted to calculate the correct position for the cube making the cut-off to the block (at one side; Seite means a variable later used to distinguish the position of the cut-off relative to the whole block). [code] module hole(pos=[0,0,0],d=1,h=1,res=15) { translate(pos) cylinder($fn=res,h,d1=d,d2=d,center=false); } module Schraubklotz(pos=[0,0,0],size=[0,0,0],dir=0,Mpos=0,Res=15,Inn=3) { translate(pos) { difference() { cube(size); translate([-pos[0],-pos[1],-0.1]) { hole([(pos[0]+size[0]/2),(pos[1]+size[1]/2),0],Inn,size[2]+0.2,res=Res); } } } } module Schraubklotz(pos=[0,0,0],size=[0,0,0],dir=0,Mpos=0,Res=15,Inn=3) { translate(pos) { difference() { cube(size); translate([-pos[0],-pos[1],-0.1]) { hole([(pos[0]+size[0]/2),(pos[1]+size[1]/2),0],Inn,size[2]+0.2,res=Res); } } } } module SK(Pos=[0,0,0],Size=[0,0,0],Dir=0,MPos=0,RES=15,INN=3,Seite=0) { help = ( Size[1] - 2 * INN )/ 2; Pos[1]=Pos[1]+help; difference() { Schraubklotz(Pos,Size,Dir,MPos,RES,INN); translate(Pos) cube([2*INN,2*INN,Size[2]+0.2]); } } Schraubklotz([0,0,0],[10,10,10],0,0,6,3); [/code] The error is in module SK at the line 'help = ( Size[1] - 2 * INN )/ 2;' where the '2' is marked red every time the preview' or 'render' functions are activated. I can't find out the reason for the error. In my opinion the code is correct. 'help' is for calculating the correct position of the cube inside the difference to make the cut-off at side. This special part is for corrrection of y-position normally not really helpful. Many thanks to all helping Manfred
NH
nop head
Sun, Jan 8, 2017 11:58 PM

I see the error marker on the next line. I.e.

Pos[1]=Pos[1]+help;
^

There are no variables in OpenSCAD, only named constants, so it never makes
sense to assign a value to an element of a list. The list has already been
created with fixed values.

On 8 January 2017 at 23:32, Manfred Preussig PreussigM@web.de wrote:

Hello,
first the self-discrption:
I'm using OpenSCad to make small devices than printed by my 3d-printer.
I'm living in germany, male and 52 years old.

My special interests are:
computer
astronomy
my pet (a cute little dog)

Now the reason for this post:
This is the code of a small part of an object. It will be a case for some
electronic parts. The modules here are for fixing the case or some other
parts to it. The german module names mean a block to fix a bold with a nut.
Since in the original (complete) case the block is covered by a second
(round) element the nut must be insertable from side to this inner cut-out.
So I wanted to calculate the correct position for the cube making the
cut-off to the block (at one side; Seite means a variable later used to
distinguish the position of the cut-off relative to the whole block).
[code]
module hole(pos=[0,0,0],d=1,h=1,res=15) {
translate(pos)
cylinder($fn=res,h,d1=d,d2=d,center=false);
}
module Schraubklotz(pos=[0,0,0],size=[0,0,0],dir=0,Mpos=0,Res=15,Inn=3) {
translate(pos) {
difference() {
cube(size);
translate([-pos[0],-pos[1],-0.1]) {
hole([(pos[0]+size[0]/2),(pos[1]+size[1]/2),0],Inn,size[2]+
0.2,res=Res);
}
}
}
}

module Schraubklotz(pos=[0,0,0],size=[0,0,0],dir=0,Mpos=0,Res=15,Inn=3) {
translate(pos) {
difference() {
cube(size);
translate([-pos[0],-pos[1],-0.1]) {
hole([(pos[0]+size[0]/2),(pos[1]+size[1]/2),0],Inn,size[2]+
0.2,res=Res);
}
}
}
}

module SK(Pos=[0,0,0],Size=[0,0,0],Dir=0,MPos=0,RES=15,INN=3,Seite=0) {

         help = ( Size[1] - 2 * INN )/ 2;

         Pos[1]=Pos[1]+help;

         difference() {
                    Schraubklotz(Pos,Size,Dir,MPos,RES,INN);
                    translate(Pos) cube([2*INN,2*INN,Size[2]+0.2]);
         }

}

Schraubklotz([0,0,0],[10,10,10],0,0,6,3);
[/code]
The error is in module SK at the line 'help = ( Size[1] - 2 * INN )/ 2;'
where the '2' is marked red every time the preview' or 'render' functions
are activated. I can't find out the reason for the error. In my opinion the
code is correct. 'help' is for calculating the correct position of the cube
inside the difference to make the cut-off at side. This special part is for
corrrection of y-position normally not really helpful.

Many thanks to all helping

Manfred


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

I see the error marker on the next line. I.e. Pos[1]=Pos[1]+help; ^ There are no variables in OpenSCAD, only named constants, so it never makes sense to assign a value to an element of a list. The list has already been created with fixed values. On 8 January 2017 at 23:32, Manfred Preussig <PreussigM@web.de> wrote: > Hello, > first the self-discrption: > I'm using OpenSCad to make small devices than printed by my 3d-printer. > I'm living in germany, male and 52 years old. > > My special interests are: > computer > astronomy > my pet (a cute little dog) > > Now the reason for this post: > This is the code of a small part of an object. It will be a case for some > electronic parts. The modules here are for fixing the case or some other > parts to it. The german module names mean a block to fix a bold with a nut. > Since in the original (complete) case the block is covered by a second > (round) element the nut must be insertable from side to this inner cut-out. > So I wanted to calculate the correct position for the cube making the > cut-off to the block (at one side; Seite means a variable later used to > distinguish the position of the cut-off relative to the whole block). > [code] > module hole(pos=[0,0,0],d=1,h=1,res=15) { > translate(pos) > cylinder($fn=res,h,d1=d,d2=d,center=false); > } > module Schraubklotz(pos=[0,0,0],size=[0,0,0],dir=0,Mpos=0,Res=15,Inn=3) { > translate(pos) { > difference() { > cube(size); > translate([-pos[0],-pos[1],-0.1]) { > hole([(pos[0]+size[0]/2),(pos[1]+size[1]/2),0],Inn,size[2]+ > 0.2,res=Res); > } > } > } > } > > module Schraubklotz(pos=[0,0,0],size=[0,0,0],dir=0,Mpos=0,Res=15,Inn=3) { > translate(pos) { > difference() { > cube(size); > translate([-pos[0],-pos[1],-0.1]) { > hole([(pos[0]+size[0]/2),(pos[1]+size[1]/2),0],Inn,size[2]+ > 0.2,res=Res); > } > } > } > } > > module SK(Pos=[0,0,0],Size=[0,0,0],Dir=0,MPos=0,RES=15,INN=3,Seite=0) { > > help = ( Size[1] - 2 * INN )/ 2; > > Pos[1]=Pos[1]+help; > > difference() { > Schraubklotz(Pos,Size,Dir,MPos,RES,INN); > translate(Pos) cube([2*INN,2*INN,Size[2]+0.2]); > } > } > > Schraubklotz([0,0,0],[10,10,10],0,0,6,3); > [/code] > The error is in module SK at the line 'help = ( Size[1] - 2 * INN )/ 2;' > where the '2' is marked red every time the preview' or 'render' functions > are activated. I can't find out the reason for the error. In my opinion the > code is correct. 'help' is for calculating the correct position of the cube > inside the difference to make the cut-off at side. This special part is for > corrrection of y-position normally not really helpful. > > Many thanks to all helping > > Manfred > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > >
P
Parkinbot
Mon, Jan 9, 2017 12:09 AM

Manfred, The problem is that OpenSCAD claims to be a functional language and
thus does not support variables that may change their values after being
initialized. This sounds weird to someone who is used to procedural
programming languages, and maybe also confusing. To understand what is going
on, read this:
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#Variables

To solve your "problem", you could initialize a new "variable" with the
modified value

module SK(Pos=[0,0,0],Size=[0,0,0],Dir=0,MPos=0,RES=15,INN=3,Seite=0) {

         help = ( Size[1] - 2 * INN )/ 2;
         Pos_ = [Pos[0], Pos[1]+help, Pos[2]]; 
         difference() {
                    Schraubklotz(Pos_,Size,Dir,MPos,RES,INN);
                    translate(Pos_) cube([2*INN,2*INN,Size[2]+0.2]);
         }

}

--
View this message in context: http://forum.openscad.org/Error-in-Code-Don-t-know-what-s-wrong-there-tp19952p19954.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Manfred, The problem is that OpenSCAD claims to be a functional language and thus does not support variables that may change their values after being initialized. This sounds weird to someone who is used to procedural programming languages, and maybe also confusing. To understand what is going on, read this: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#Variables To solve your "problem", you could initialize a new "variable" with the modified value > module SK(Pos=[0,0,0],Size=[0,0,0],Dir=0,MPos=0,RES=15,INN=3,Seite=0) { > > help = ( Size[1] - 2 * INN )/ 2; > Pos_ = [Pos[0], Pos[1]+help, Pos[2]]; > difference() { > Schraubklotz(Pos_,Size,Dir,MPos,RES,INN); > translate(Pos_) cube([2*INN,2*INN,Size[2]+0.2]); > } > } -- View this message in context: http://forum.openscad.org/Error-in-Code-Don-t-know-what-s-wrong-there-tp19952p19954.html Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Mon, Jan 9, 2017 12:21 AM

Hi Manfred,

Welcome to the forum.

There are two issues here, why the red marker is on the wrong line, and the
real issue.

When I preview your code the error is:
http://forum.openscad.org/file/n19955/manfred.jpg

So, let's look at the real issue first.

You cannot assign to an element of a vector.
You cannot also do increments of the form x = x +1.

[well I see two other replies have arrived while I typed the above, so
enough on that topic]

So why does the error highlight the wrong place for you?

What version of OpenSCAD are you using, on what operating system?

Is the code inside a use<> or include<>?

Could you post a screen capture showing the highlighted error?


Admin - PM me if you need anything, or if I've done something stupid...

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”  Fight it! http://www.ourfairdeal.org/  time is running out!

View this message in context: http://forum.openscad.org/Error-in-Code-Don-t-know-what-s-wrong-there-tp19952p19955.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Hi Manfred, Welcome to the forum. There are two issues here, why the red marker is on the wrong line, and the real issue. When I preview your code the error is: <http://forum.openscad.org/file/n19955/manfred.jpg> So, let's look at the real issue first. You cannot assign to an element of a vector. You cannot also do increments of the form x = x +1. [well I see two other replies have arrived while I typed the above, so enough on that topic] So why does the error highlight the wrong place for you? What version of OpenSCAD are you using, on what operating system? Is the code inside a use<> or include<>? Could you post a screen capture showing the highlighted error? ----- Admin - PM me if you need anything, or if I've done something stupid... Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out! -- View this message in context: http://forum.openscad.org/Error-in-Code-Don-t-know-what-s-wrong-there-tp19952p19955.html Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Mon, Jan 9, 2017 1:37 AM

Technically, in this instance, you could do;

Pos=[ Pos[0], Pos[1]+help,Pos[2] ];

but that is only by the circumstances with the module and parameters.
It is not good to do it that way. It will form bad habits which will bite
you later.


Admin - PM me if you need anything, or if I've done something stupid...

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”  Fight it! http://www.ourfairdeal.org/  time is running out!

View this message in context: http://forum.openscad.org/Error-in-Code-Don-t-know-what-s-wrong-there-tp19952p19957.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

*Technically*, in this instance, you could do; Pos=[ Pos[0], Pos[1]+help,Pos[2] ]; but that is only by the circumstances with the module and parameters. It is not good to do it that way. It will form bad habits which will bite you later. ----- Admin - PM me if you need anything, or if I've done something stupid... Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out! -- View this message in context: http://forum.openscad.org/Error-in-Code-Don-t-know-what-s-wrong-there-tp19952p19957.html Sent from the OpenSCAD mailing list archive at Nabble.com.