简体   繁体   中英

pass multiple commands for FreeCADCmd.exe with python

how to pass multiple commands for FreeCADCmd.exe with python? For example this commands:

import FreeCAD
import Part
import Mesh
shape = Part.Shape()
shape.read('my_shape.step')
doc = App.newDocument('Doc')
pf = doc.addObject("Part::Feature","MyShape")
pf.Shape = shape
Mesh.export([pf], 'my_shape.stl')

here's my code but it gets stuck after first command, I mean "FreeCADCmd.exe":

os.chdir("D:\\program\\FreeCAD 0.19\\bin")
os.system("""FreeCADCmd.exe && import FreeCAD && import Part && import Mesh && shape = Part.Shape() && shape.read('D:\coupling_base.stp') && doc = App.newDocument('Doc') && pf = doc.addObject("Part::Feature","MyShape") && pf.Shape = shape && Mesh.export([pf], 'D:\z1.stl')""")

Looks like the && does not work.

The os.system() function runs a command in a terminal behind the scenes, which is often not a good way to call other programs. Instead you should use the subprocess module.

Try running the command you have inside your os.system() call in a terminal and you'll find it doesn't work there either. This is because it runs each command separated by an && one by one, rather than sending the subsequent commands into the first program.

In this case, it looks like FreeCADCmd.exe is a Python interpreter, so perhaps you could try storing the commands inside a Python script, then running that script?

Put your commands in a file. like 'myfile.py' Then run

FreeCADCmd myfile.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM