Accessing Freecad through Python CGI Script

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
valpocoder
Posts: 5
Joined: Mon Apr 24, 2017 4:30 pm

Accessing Freecad through Python CGI Script

Post by valpocoder »

Hello,
I am trying to create an online application that uses python and freecad to change a model of some hardware my team is working on. I accomplishing this through a CGI script that imports FreeCAD. However, I am having trouble. First and main question, is there any way to use FreeCAD imported into a Python script to change and output a .fcstd file? I can not use the command line, it must all be in a script, so FreeCADcmd is out.

Thank you, and pardon my ignorance :D
User avatar
kkremitzki
Veteran
Posts: 2511
Joined: Thu Mar 03, 2016 9:52 pm
Location: Illinois

Re: Accessing Freecad through Python CGI Script

Post by kkremitzki »

You can start with something like this:

Code: Select all

import sys

FCLIBPATH = '/usr/lib/freecad-daily/lib'
sys.path.append(FCLIBPATH)

import FreeCAD
Like my FreeCAD work? I'd appreciate any level of support via Patreon, Liberapay, or PayPal! Read more about what I do at my blog.
valpocoder
Posts: 5
Joined: Mon Apr 24, 2017 4:30 pm

Re: Accessing Freecad through Python CGI Script

Post by valpocoder »

No luck with that so far. Here is what I have right now, and it is causing an error on my machine saying that it cannot import FreeCAD:

Code: Select all

#!/usr/bin/python
FREECADPATH= '/usr/lib/freecad/bin/FreeCAD' #put path of freecad application path here
import sys, cgi, cgitb
sys.path.append(FREECADPATH)
import FreeCAD
from FreeCAD import App, Part, Mesh
form = cgi.FieldStorage()
...
Also, I am using CentOS and Apache2, I believe.
User avatar
kkremitzki
Veteran
Posts: 2511
Joined: Thu Mar 03, 2016 9:52 pm
Location: Illinois

Re: Accessing Freecad through Python CGI Script

Post by kkremitzki »

You need to use the lib path, not the path to the FreeCAD binary. The correct folder will have FreeCAD.so in it.
Like my FreeCAD work? I'd appreciate any level of support via Patreon, Liberapay, or PayPal! Read more about what I do at my blog.
valpocoder
Posts: 5
Joined: Mon Apr 24, 2017 4:30 pm

Re: Accessing Freecad through Python CGI Script

Post by valpocoder »

Ah, okay good. It can now import Freecad, but is stuck at importing App. Just as a side note, I am purely a software dev with only minimal engineering/cad experience, so assume that I am ignorant (haha). Is "App" necessary to make changes to a file while I am using the embedded Freecad library?
User avatar
sgrogan
Veteran
Posts: 6499
Joined: Wed Oct 22, 2014 5:02 pm

Re: Accessing Freecad through Python CGI Script

Post by sgrogan »

valpocoder wrote:Is "App" necessary to make changes to a file while I am using the embedded Freecad library?
Maybe this helps? viewtopic.php?t=5055
"fight the good fight"
valpocoder
Posts: 5
Joined: Mon Apr 24, 2017 4:30 pm

Re: Accessing Freecad through Python CGI Script

Post by valpocoder »

Ah, yes, that seemed to have solved my problem, thank you very much!
Post Reply