1) Start Web server in FreeCad - create macro called 'freecad_start.py':
Code: Select all
import Web
FREECAD_PORT = 5000
Web.startServer( "127.0.0.1", FREECAD_PORT )
Code: Select all
import socket
import os
import argparse
import sys
FREECAD_PORT = 5000
FREECAD_IP = "127.0.0.1"
def client( message, prefix = None ):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((FREECAD_IP, FREECAD_PORT))
try:
if prefix:
sock.sendall( prefix )
sock.sendall( message )
response_raw = sock.recv(1024).decode("utf-8")
response = "\n".join( ["Received: '%s'" % x for x in response_raw.split("\n")] )
if response_raw == "None":
print("All good, bye!")
return 0
print("Something went wrong:")
print(response)
return 1
finally:
print("Closing socket")
sock.close()
def main(config):
with open( config.input_file, 'rb' ) as fid:
return client( fid.read() )
def cmd_config():
parser = argparse.ArgumentParser(description='Execute code in remote FreeCad')
parser.add_argument( "input_file", help="Set the job description file")
cmd = parser.parse_args()
return cmd
if __name__=="__main__":
sys.exit( main( cmd_config() ) )
And on the file to be executed one has proper init that closes old documents, like:
Code: Select all
def init( name ="Unnamed" ):
try:
App.closeDocument("Unnamed")
except NameError:
pass
App.newDocument("Unnamed")
App.setActiveDocument("Unnamed")
App.ActiveDocument=App.getDocument("Unnamed")
Gui.ActiveDocument=Gui.getDocument("Unnamed")
Gui.activeDocument().activeView().viewDefaultOrientation()
And to prevent copy pasting some personal utility functions i have the 'prefix' on the code for pusher as follows:
Code: Select all
def reload_my_lib( path ):
reloads = """
import sys
sys.path.append( "%s" )
import supalib
importlib.reload( supalib )
supalib.init()
""" % (path)
return reloads.encode("Utf-8")
For the full source code - to be taken as example - check my Github (https://github.com/susundberg/zephyr-ro ... r/3d_parts)
Cheers to the create team behind this wonderful CAD!
Br,
Pauli