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!
oly
Posts: 29
Joined: Sun Feb 19, 2017 12:52 pm

Remote editor possible ?

Post by oly »

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
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: Remote editor possible ?

Post by Jee-Bee »

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

I have seen somewhere a question about pycharm (https://forum.freecadweb.org/viewtopic.php?t=15051) probably it will work with others too.
oly
Posts: 29
Joined: Sun Feb 19, 2017 12:52 pm

Re: Remote editor possible ?

Post by oly »

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.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Remote editor possible ?

Post by wmayer »

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

Code: Select all

import Web
Web.startServer()
which returns a tuple with IP address (by default 127.0.0.1) 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 = "127.0.0.1"
port=51326 # see above

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

Code: Select all

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

Re: Remote editor possible ?

Post by oly »

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
sys.path.append(FREECADPATH)
import FreeCAD, FreeCADGui
from PyQt4 import QtGui
from FreeCAD import Part
import QtCore

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

#+END_SRC

#+RESULTS:


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')
#+END_SRC

#+RESULTS:
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Remote editor possible ?

Post by Kunda1 »

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:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
oly
Posts: 29
Joined: Sun Feb 19, 2017 12:52 pm

Re: Remote editor possible ?

Post by oly »

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
neil1
Posts: 2
Joined: Sat Nov 03, 2018 5:12 am

Re: Remote editor possible ?

Post by neil1 »

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

Re: Remote editor possible ?

Post by MarkkuTM »

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.
Post Reply