Remote editor possible ?

Need help, or want to share a macro? Post here!
Posts: 29
Joined: Sun Feb 19, 2017 12:52 pm

Remote editor possible ?

Postby oly » Mon Jul 31, 2017 7:01 pm

Anyway to inject code into freecad, basically I want a way to send my script from my ide into freecad and have it run the code instead of having to copy and paste from an editor into freecad.

can you pass a script in from the command line of a running instance for example or something similar
Posts: 2304
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: Remote editor possible ?

Postby Jee-Bee » Mon Jul 31, 2017 7:22 pm

Just use the macro editor or use the FreeCAD python in your IDE

I have seen somewhere a question about pycharm ( probably it will work with others too.
Posts: 29
Joined: Sun Feb 19, 2017 12:52 pm

Re: Remote editor possible ?

Postby oly » Mon Jul 31, 2017 8:04 pm

Interesting not quite what i am after, I was hoping to use the existing running freecad instance and just send a block of text to it.

In that method it sounds like you import freecad and have to create an instance each time you run your code which i would imagine is quite slow and annoying if your doing quick iterative editing.

but good to know about none the less.
Site Admin
Posts: 17305
Joined: Thu Feb 19, 2009 10:32 am

Re: Remote editor possible ?

Postby wmayer » Mon Jul 31, 2017 8:52 pm

You can also give this a try. Inside FreeCAD run:

Code: Select all

import Web
which returns a tuple with IP address (by default and a port number e.g. 51326.

Use this port number and run this in the IDE:

Code: Select all

import socket
import threading
import SocketServer

ip = ""
port=51326 # see above

def client(ip, port, message):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
        response = sock.recv(1024)
        print "Received: {}".format(response)

Code: Select all

client(ip, port, "print ('Hello World 1')")
client(ip, port, "import FreeCAD\nFreeCAD.newDocument()")
Posts: 29
Joined: Sun Feb 19, 2017 12:52 pm

Re: Remote editor possible ?

Postby oly » Tue Aug 01, 2017 7:12 pm

Thanks for all the suggestions, i actually found another way although its a bit buggy currently.

Basically using org babel from emacs, i think some of my issues may be the python code setup may not be 100% whats needed.

Using this method you can have shared code between blocks with out relaunching the gui, you shoudl in theory be able to show at the start save the image output and have the results inserted direct into your ide, so you can edit your code hit run and see the results, and document what your doing at the same time.

Hoping this maybe useful to others or some one maybe able to suggest improvements, currently tested with spacemacs on ubuntu using freecad 0.16 and 0.17.

Code: Select all

Initialise the gui, and create a document by doing this here we can reuse the vars 
this means we dont needd to start the gui every time. 
Run me first with CTRL c CTRL c
#+BEGIN_SRC python :python python2 :session :results both
FREECADPATH = "/usr/lib/freecad/lib"
FREECADPATH = "/usr/lib/freecad-daily/lib"
import sys
import FreeCAD, FreeCADGui
from PyQt4 import QtGui
from FreeCAD import Part
import QtCore

App = QtGui.QApplication(sys.argv)
doc = FreeCAD.newDocument()



Example of creating a few objects.
Probably we would want to destroy the current document and create new for each run.
Be nice if this worked better for faster more iterative coding.
Also if we can use save image below the output should go below in the results
this did not work in my test below for some reason
#+BEGIN_SRC python :python python2 :session :results both :exports file
textPartObject = doc.addObject("App::Part", "Text")
textBodyObject = doc.addObject("PartDesign::Body", "Body")
sketchObject = doc.addObject("Sketcher::SketchObject", "Text Bounds")
sketchObject.addGeometry(Part.LineSegment(App.Vector(100,100,0), App.Vector(-100, -100, 0)), True)
doc.activeView().saveImage('/tmp/tet_file.png', 1650, 650, 'Current')

User avatar
Posts: 10518
Joined: Thu Jan 05, 2017 9:03 pm

Re: Remote editor possible ?

Postby Kunda1 » Tue Aug 01, 2017 7:31 pm

we should definately document this on the wiki
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
Posts: 29
Joined: Sun Feb 19, 2017 12:52 pm

Re: Remote editor possible ?

Postby oly » Tue Aug 01, 2017 7:52 pm

Yeah definitely although might be better to get it working better first, had some random freecad crashes and i cant get the image save to work in that example.

hoping others have a bit more knowledge of freecad and babel in general, relative noob to org been using it less than a year :p
Posts: 2
Joined: Sat Nov 03, 2018 5:12 am

Re: Remote editor possible ?

Postby neil1 » Tue Dec 04, 2018 9:24 pm

Does org babel keep a python interpreter session running in the background, waiting to receive chunks of code to be executed?
Posts: 33
Joined: Sun Dec 31, 2017 6:57 pm

Re: Remote editor possible ?

Postby MarkkuTM » Fri Jan 04, 2019 9:56 pm

I use Notepad++ (quick and lightweight) for writing code for testing on FreeCAD 0.17. I save test files (Windows10) to C:\ProgramFIles\FreeCAD 0.17\bin and then turn on FreeCAD and load them into the python console with the command < import testfilename > the first time I use the test file. After an edit and save, to use it again during the same open session of FreeCAD use the command < reload(testfilename) > (on a new FreeCAD session, start again with < import testfilename >). This method is quick and interactive, with no cutting and pasting. Just use the Python2.7 version of commands in scripts.

Depending on what modules are imported by the file under test, and what actions it is meant to accomplish, there may or may not have to be a new active document opened for proper operation of a script. With appropriate imported modules and commands, a script can be made to launch a new active document and window, create the desired model and render it in any view you wish properly sized for the active document window.

The updated (2018) scripting documentation contains these and other related instructions scattered across a few different examples. With a little experimentation and copying of what the Gui itself produces as python code for each action, I found that it is possible to create functioning code by working interactively and quickly between Notepad++ and the FreeCAD Python console with immediate feedback and results showing on the FreeCAD gui and console.