set import preference by Python not possible

This forum section is only for IFC-related issues
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

set import preference by Python not possible

Post by bernd »

I would like to set some import preferences but would not like to change the FreeCAD preference by Python. Means for example I would like to set SEPARATE_OPENINGS = True for one import or as long as getPreferences is called from importIFC. The problem is getPreferences is called inside the insert method.

How about to make some changes to be able to do such imports and export. I played a bit but could not find a smart solution so far. Thus I have nothing to show to make a suggestion.

https://github.com/FreeCAD/FreeCAD/blob ... FC.py#L281

May be some dictionary. Get the preferences as a dictionary, reset one preference, call insert method with this dictionary. If it is not given the insert method uses getPreference. This might be simpler by the use of the globals, but I am not sure.

bernd
User avatar
Moult
Posts: 321
Joined: Sat Jan 05, 2019 11:46 am
Contact:

Re: set import preference by Python not possible

Post by Moult »

This is a classic case similar to using dependency injection to separate dependencies. In this case, the preferences is a dependency.

It should be refactored so that there are no globals, that the preferences is its own object, which is passed into the insert() function as an argument. This allows you to overload the preferences.

Would you like to give a shot to this refactoring? If not, I don't mind doing this refactor.
I also blog about 3D rendering, architecture, software and other on thinkMoult.com. RSS / Atom feed available for your convenience.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: set import preference by Python not possible

Post by bernd »

Moult wrote: Thu May 30, 2019 3:59 am ... If not, I don't mind doing this refactor.
Since my FEM TODO is loooong I don't mind either if you would go for it.

Bernd
User avatar
yorik
Founder
Posts: 13665
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: set import preference by Python not possible

Post by yorik »

yeah that importer was never really thought to be used from python yet... There are a lot of very easy improvements to bring...
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: set import preference by Python not possible

Post by bernd »

once this is implemented we would be able to set up some unit tests. I may help here. Since I make heavy usage of ifc exporter this would really be very useful. Over the years it has happened a few times we gone introduce a regresion wheras make some improvements on some other place.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: set import preference by Python not possible

Post by bernd »

Moult wrote: Thu May 30, 2019 3:59 am ... I don't mind doing this refactor.
any updates here ?
User avatar
Moult
Posts: 321
Joined: Sat Jan 05, 2019 11:46 am
Contact:

Re: set import preference by Python not possible

Post by Moult »

Sorry, I was working on another PR related to IFC import and hoped for it to be merged first to prevent merge conflicts ( https://github.com/FreeCAD/FreeCAD/pull/2259/ ) - I'll submit the PR for the refactor tonight (or tomorrow)!
I also blog about 3D rendering, architecture, software and other on thinkMoult.com. RSS / Atom feed available for your convenience.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: set import preference by Python not possible

Post by bernd »

Great, keep in mind: FreeCAD moto is: It's done when it's done! :D
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: set import preference by Python not possible

Post by bernd »

small reminder ...
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: set import preference by Python not possible

Post by bernd »

from another topic ...
yorik wrote: Tue Aug 20, 2019 10:50 pm I did some more cleaning and move more stuff from importIFC.py to importIFCHelper.py
This is becoming a lot cleaner now...

I had an idea to solve the problem of allowing users (I mean bernd :D) to override settings while still read the preferences again at each import (in case they were modified inbetween):

we move all the import code to another function, and we make insert() a very minimal one:

Code: Select all

def insert(file):
    readpreferences()
    do_the_real_import()
so you can do that yourself, and override any of the settings after readpreferences()

AFAIR I tried something similar myself, but failed due to the global variables. But I can not remember. I am happy with anything which would work.

BTW: it is not just me. If we can set the preference we can start to make unit tests! Since they should be passed no matter which setting the user have on his machine, we need to set them for unit tests.
Post Reply