Note that you don't need semicolons on include and use statements.
On Sat, 23 Sept 2023, 05:31 William F. Adams via Discuss, <
discuss@lists.openscad.org> wrote:
---------- Forwarded message ----------
From: "William F. Adams" willadams@aol.com
To: "discuss@lists.openscad.org" discuss@lists.openscad.org
Cc:
Bcc:
Date: Sat, 23 Sep 2023 04:30:36 +0000 (UTC)
Subject: [OpenSCAD] Using the new Python--OpenSCAD to write out files (and
to have Python functions and OpenSCAD variables in a loaded module)
Maybe this is all obvious to folks, but it took me a while to wrap my mind
around how this works:
There are two ways to incorporate files into OpenSCAD:
- use — this will allow definitions in either Python, or in OpenSCAD
and using a mix of OpenSCAD and Python code
- include — this allows using variables from the main OpenSCAD file,
but does not allow directly making use a function defined in Python
Since we want to be able to turn on/off calling Python from w/in OpenSCAD
we require a total of 3 files:
- a Python file which is loaded using use <gcodepreview.py>; — this is
able to define Python functions
- an OpenSCAD file which is loaded using use <pygcodepreview.scad>; —
this wraps the defined Python function in OpenSCAD
- an OpenSCAD file which is loaded using include <gcodepreview.scad>;
— this allows using OpenSCAD variables from the main file to determine
whether or no an OpenSCAD module which calls a Python module should be
called
Thus we have:
gcodepreview.py
def popengcodefile(fn):
global f
f = open(fn, "w")
def writeln(*arguments):
line_to_write = ""
for element in arguments:
line_to_write += element
f.write(line_to_write)
f.write("\n")
def pclosegcodefile():
f.close()
pygcodepreview.scad
//!OpenSCAD
module oopengcodefile(fn) {
popengcodefile(fn);
}
module owritecomment(comment) {
writeln("(",comment,")");
}
module oclosegcodefile() {
pclosegcodefile();
}
gcodepreview.scad
//!OpenSCAD
module opengcodefile(fn) {
if (generategcode == true) {
oopengcodefile(fn);
}
}
module writecomment(comment) {
if (generategcode == true) {
owritecomment(comment);
}
}
module closegcodefile() {
if (generategcode == true) {
oclosegcodefile();
}
}
which is all put together by an OpenSCAD file:
//!OpenSCAD
use <gcodepreview.py>;
use <pygcodepreview.scad>;
include <gcodepreview.scad>;
/* [G-code] /
Gcode_filename = "gcode.nc";
/ [G-code] */
generategcode = true;
a = 300;
opengcodefile(Gcode_filename);
writecomment(str(a));
writecomment("Test");
closegcodefile();
which when run, creates the file:
gcode.nc
(300)
(Test)
Which finally allows me to write out files from OpenSCAD in a workable
fashion.
My thanks to everyone for their patience and generosity in helping me to
this point.
William
---------- Forwarded message ----------
From: "William F. Adams via Discuss" discuss@lists.openscad.org
To: "discuss@lists.openscad.org" discuss@lists.openscad.org
Cc: "William F. Adams" willadams@aol.com
Bcc:
Date: Sat, 23 Sep 2023 04:30:36 +0000 (UTC)
Subject: [OpenSCAD] Using the new Python--OpenSCAD to write out files (and
to have Python functions and OpenSCAD variables in a loaded module)
OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org
On Saturday, September 23, 2023 at 05:47:30 AM EDT, nop head nop.head@gmail.com wrote:
Note that you don't need semicolons on include and use statements.
I believe those are an artifact of my previous use of RapCAD.
William