[help] External Python script to modify FreeCAD Spreadsheet

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
rjomega
Posts: 1
Joined: Tue Dec 07, 2021 2:52 pm

[help] External Python script to modify FreeCAD Spreadsheet

Post by rjomega »

I would like to modify the values in the FreeCAD Spreadsheet to produce multiple models with different parameters for optimization but I dont know where to start. I saw on different site their solution but something seems to be wrong on my side. "seteditormode" seems to be not working on my pc because it shows that the mode is still readonly and hidden instead of read and write. Values seems to be not changing also on the spreadsheet. I hope you guys can assist me with this one. thank you

Their solution: https://stackoverflow.com/questions/606 ... hon-module

Code: Select all

FREECADPATH = '/usr/lib/freecad/lib/'
import sys
sys.path.append(FREECADPATH)
from collections import defaultdict

def convert_model(filename, arclen, radius, width, height):
    try:
        import FreeCAD
        from FreeCAD import Vector
    except ValueError:
        print ('import error\n')
    else:
        FreeCAD.open(filename)

        doc = App.ActiveDocument
        sheet = doc.Spreadsheet
        print("mode = "+str(sheet.getEditorMode("A5")))
        sheet.setEditorMode("A5", 0)
        print("mode' = "+str(sheet.getEditorMode("A5")))
        print("arclen = "+str(sheet.arclen))
        print("radius = "+str(sheet.radius))
        print("angle = "+str(sheet.angle))
        print("width = "+str(sheet.width))
        print("height = "+str(sheet.height))
        sheet.set("arclen", str(arclen))
        sheet.set("radius", str(radius))
        sheet.set("width", str(width))
        sheet.set("height", str(height))
        sheet.recompute()
        # verify that the radius and also the dependent angle have changed after the recomputer
        print("arclen' = "+str(sheet.arclen))
        print("radius' = "+str(sheet.radius))
        print("angle' = "+str(sheet.angle))
        print("width' = "+str(sheet.width))
        print("height' = "+str(sheet.height))

        # recompute the model
        doc.recompute()
        print(generate_geometry(doc))
        geometry = generate_geometry(doc)
        #print(geometry[0][0])
        print("generated geometry: "+str(geometry))

        #assert geometry[0][0][2] == Vector(width, 0, 0)

def generate_geometry(doc):
    objects = doc.Objects
    return [tessellate_shape(shaped) for shaped in objects if shaped.TypeId == 'PartDesign::Body']

def tessellate_shape(shaped):
    return shaped.Shape.tessellate(0.1)

def main():
    filename=sys.argv[1]
    arclen=float(sys.argv[2])
    radius=float(sys.argv[3])
    width=float(sys.argv[4])
    height=float(sys.argv[5])
    convert_model(filename, arclen, radius, width, height)

if __name__=='__main__':
   main()
User avatar
onekk
Veteran
Posts: 6097
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: [help] External Python script to modify FreeCAD Spreadsheet

Post by onekk »

This is more related to use "FreeCAD as a Library", you are not using the "internal python interpreter" but a different interpreter, the external python, so maybe some "automatics" actions are not triggered, there were some discussion in this forum, with some intervention of @wmayer explaining some caveats, sadly I don't remember exactly what was said.

Another thing, you maybe are using FreeCAD that was build with a specific python version in mind by a different version of Python interpreter.

So your mileage may vary due to version mismatch.

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