Home > database >  pass multiple commands for FreeCADCmd.exe with python
pass multiple commands for FreeCADCmd.exe with python

Time:05-26

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.

CodePudding user response:

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?

  • Related