Remote editor possible ?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
supa5000
Posts: 1
Joined: Mon Oct 14, 2019 3:57 pm

Re: Remote editor possible ?

Post by supa5000 »

Hello from future (as this is old thread). But i stumbled here while finding out how to do this. For fellow googlers, this is how i have it running smoothly:

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 )
2) Create your own python stuff, and create a "push" client that sends wanted file content to Freecad - (to be runned for example when you press ctrl+F8 in your favorite IDE):

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")
That allows my own .py files (that create single part) to use common "library" called here as supalib (note the username).

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
User avatar
onekk
Veteran
Posts: 6205
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Remote editor possible ?

Post by onekk »

No nedd to copy and paste.

if you open a file in FreeCAD says, pyhton.py and then modify it in an external editor, qhen you save it in the external editor, FreeCAD is smart enough to detect the changes, and ask you if you would reload the file.

Hoping no to have misunderstand your question.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Remote editor possible ?

Post by Kunda1 »

Also relevant:
https://forum.freecadweb.org/viewtopic. ... 3&p=356739
https://pythoncvc.net/?p=869 (FreeCAD external editor with Code – OSS)
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Post Reply