object properties from template

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
Zolko
Veteran
Posts: 2213
Joined: Mon Dec 17, 2018 10:02 am

object properties from template

Post by Zolko »

Hello,

an important functionality for assemblies is a BOM: Bill Of Materials. In order to work well, parts need to have some fields set, like ID, reference number, name, supplier, material ... these info fields are specific to people and projects, so I would like to have the possibility to create and apply templates to objects. The basic idea is that objects have additional properties added to it (with obj.addProperty()) and to have these properties set in a template somewhere, such that this template can be applied which would pre-fill the object's properties.

I've seen that the Path WB does allow to do this for tools, and stores the templates (or tool definitions) in the user's FreeCAD settings folder as JSON files.

Is there some documentation somewhere on the use of JSON files are, how to create and how to read them ? Thanx
try the Assembly4 workbench for FreCAD — tutorials here and here
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: object properties from template

Post by TheMarkster »

Zolko wrote: Fri Sep 17, 2021 12:59 pm
Is there some documentation somewhere on the use of JSON files are, how to create and how to read them ? Thanx
JSON files are fairly easy to deal with. Code is untested.

Code: Select all

import json

some_dictionary = {"a":1,"b":2,"c":3}
filename = "json.txt"

with open(filename,"w") as outfile:
    json.dump(some_dictionary,outfile)

f = open(filename)
some_other_dictionary = json.load(f)
f.close()
User avatar
Zolko
Veteran
Posts: 2213
Joined: Mon Dec 17, 2018 10:02 am

Re: object properties from template

Post by Zolko »

TheMarkster wrote: Sat Sep 18, 2021 3:49 am JSON files are fairly easy to deal with. Code is untested.
great, thanx, will try.

as a second question, I'd like to save the JSON templates into the user config directory (where the user.cfg file is): what is the way to retrieve the pah to that directory ?
try the Assembly4 workbench for FreCAD — tutorials here and here
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: object properties from template

Post by TheMarkster »

Zolko wrote: Sun Sep 19, 2021 2:47 pm
TheMarkster wrote: Sat Sep 18, 2021 3:49 am JSON files are fairly easy to deal with. Code is untested.
great, thanx, will try.

as a second question, I'd like to save the JSON templates into the user config directory (where the user.cfg file is): what is the way to retrieve the pah to that directory ?

Code: Select all

App.getUserAppDataDir()
User avatar
Zolko
Veteran
Posts: 2213
Joined: Mon Dec 17, 2018 10:02 am

Re: object properties from template

Post by Zolko »

TheMarkster wrote: Sun Sep 19, 2021 2:57 pm

Code: Select all

App.getUserAppDataDir()
thanx
try the Assembly4 workbench for FreCAD — tutorials here and here
Post Reply