gsohler / openscad

OpenSCAD - The Programmers Solid 3D CAD Modeller
https://www.openscad.org
Other
26 stars 7 forks source link

Call OpenSCAD functionality in Python file from Python file #39

Open WillAdams opened 23 hours ago

WillAdams commented 23 hours ago

Duplicate of: https://old.reddit.com/r/pythonscad/comments/1fqzw2s/best_practices_for_global_variables_in/

Given a Python file, pythontest.py which contains:

from openscad import *

def popengcodefile(fn):
    global f
    f = open(fn, "w")

def writeln(*arguments):
    global f
    line_to_write = ""
    for element in arguments:
        line_to_write += element
    f.write(line_to_write)
    f.write("\n")

def psetupstock(stocklength, stockwidth, stockthickness, zeroheight, stockorigin):
    global Base_filename
    global generategcode
    global stock
    stock = os.cube([stocklength, stockwidth, stockthickness])
    if generategcode == True:
        writeln("(Design File: " + Base_filename + ")")

and a test.py file which calls it as:

from openscad import *
from pythontest import *

# [Export] */
Base_filename = "export" 
# [Export] */
generategcode = True

global filename_gcode
filename_gcode = Base_filename + ".nc"

popengcodefile(filename_gcode)
#pythontest.popengcodefile("export_set.nc")

psetupstock(1,2,3,"Top","Center")
#pythontest.psetupstock(1,2,3,"Top","Center")

# Create a cube and a cylinder that overlap
cy = cylinder(15, center = True)

# Substract the cylinder from the cube
diff = stock.difference(cy)
#diff = pythontest.stock.difference(cy)

# Display the result
output(diff)

the result is:

ERROR: Traceback (most recent call last): File "", line 15, in File "C:\Users\willa\AppData\Local\Programs\Python\Python311\Lib\pythontest.py", line 17, in psetupstock global generategcode ^^^^ NameError: name 'cube' is not defined

What arrangement of commands and files would allow one to:

?

gsohler commented 20 hours ago

Fortunately this is an easy one. in pythontest.py you included openscad plain into context so you have to write: stock=cube(xxx)

or you can import as import openscad as os

hope this helps

WillAdams commented 15 hours ago

Changing that to:

    stock = cube(stocklength, stockwidth, stockthickness)

I still get:

ERROR: Traceback (most recent call last): File "", line 15, in File "C:\Users\willa\AppData\Local\Programs\Python\Python311\Lib\pythontest.py", line 17, in psetupstock global generategcode ^^^^ NameError: name 'cube' is not defined

Is there a sample/template of a pair of .py file where one has global variables, and the other does branching/3D modeling based on those variables when called from the first file?

gsohler commented 13 hours ago

I think, that a typical object oriented approach would best fit your needs.

This will eliminate the need of any global variable and even gives you the feeling that you can work with the gcodewriter, not against. Actually I encourage, to use pythonscad in Python mode, because OO approach is another thing, which can barely be immitated in SCAD language.

pythontest.py

from openscad import *

class gcodewriter:

    def __init__(self, basename = "", generategcode = False):
        self.Base_filename = basename
        self.generategcode = generategcode

    def open(self):
        self.f = open(self.Base_filename + ".nc", "w")

    def writeln(self, *arguments):
        line_to_write = ""
        for element in arguments:
            line_to_write += element
        self.f.write(line_to_write)
        self.f.write("\n")

    def psetupstock(self, stocklength, stockwidth, stockthickness, zeroheight, stockorigin):
        self.stock = cube([stocklength, stockwidth, stockthickness])
        if self.generategcode == True:
            self.writeln("(Design File: " + self.Base_filename + ")")

and test.py


from openscad import *
from pythontest import *

gw = gcodewriter("export",True)

gw.open()

gw.psetupstock(1,2,3,"Top","Center")

# Create a cube and a cylinder that overlap
cy = cylinder(15, center = True)

# Substract the cylinder from the cube
diff = gw.stock.difference(cy)

# Display the result
diff.show()
WillAdams commented 4 hours ago

I get:

ERROR: Traceback (most recent call last): File "", line 4, in NameError: name 'gcodewriter' is not defined