set import preference by Python not possible

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

set import preference by Python not possible

Postby bernd » Wed May 29, 2019 4:41 pm

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: 211
Joined: Sat Jan 05, 2019 11:46 am
Contact:

Re: set import preference by Python not possible

Postby Moult » Thu May 30, 2019 3:59 am

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
Posts: 8046
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: set import preference by Python not possible

Postby bernd » Thu May 30, 2019 9:30 pm

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
Site Admin
Posts: 11398
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: set import preference by Python not possible

Postby yorik » Mon Jun 03, 2019 5:27 pm

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
Posts: 8046
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: set import preference by Python not possible

Postby bernd » Tue Jun 04, 2019 5:00 pm

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
Posts: 8046
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: set import preference by Python not possible

Postby bernd » Mon Jul 08, 2019 2:59 pm

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

Re: set import preference by Python not possible

Postby Moult » Mon Jul 08, 2019 9:02 pm

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
Posts: 8046
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: set import preference by Python not possible

Postby bernd » Mon Jul 08, 2019 9:59 pm

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

Re: set import preference by Python not possible

Postby bernd » Fri Aug 16, 2019 3:22 am

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

Re: set import preference by Python not possible

Postby bernd » Wed Aug 21, 2019 6:59 am

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.