Help to check FC version

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
johnwang
Veteran
Posts: 1382
Joined: Sun Jan 27, 2019 12:41 am

Help to check FC version

Post by johnwang »

Hi,

How to check FC version?
If FC version=FCver, I want something like this:

Code: Select all

If FCver<0.20:
	import freecad.plot.Plot as Plot
else:	
	from FreeCAD.Plot import Plot
Does it work?

Cheers,

John
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
User avatar
onekk
Veteran
Posts: 6199
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Help to check FC version

Post by onekk »

It seems not correct, as usual version is returned as a list containing some data.

Now I'm on mobile so no mean to check for proper code, but If you search in the forum probably you will find easily some hints.

Code: Select all

FreeCAD.Version()
['0', '19', '24366 (Git)', 'https://github.com/conda-forge/freecad-feedstock', '2021/12/04 21:56:30', '(HEAD detached at 0f9259c)', '0f9259cda103ae1824ac16c68ac9b4a0d54b05fc']
you could make some assumptions, so you could aggregate at least first and second element:

Code: Select all

vers = FreeCAD.Version()
print(vers)

ver_num = float(f"{vers[0]}.{vers[1]}")
sub_ver = int(f"{vers[2][:5]}")
print(ver_num, sub_ver)
This way you could check major version as this is a float and subsequent check if needed sub_version (extracted from the third element guessing that is have a 5 figures digit as start element)

Sadly third element is not guaranteed to be correctly formatted for some versions around, making difficult to discriminate between "sub versions" or "build version", this is a know problem and has been discussed in the forum,

https://forum.freecadweb.org/viewtopic. ... 41#p598441

Hope it helps.

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/
Syres
Veteran
Posts: 2898
Joined: Thu Aug 09, 2018 11:14 am

Re: Help to check FC version

Post by Syres »

To expand on @onekk's input and to 'borrow' what's already in use in the CfdOF Wb, this is the code @oliveroxtoby uses:

Code: Select all

# Check FreeCAD version
ver = FreeCAD.Version()
major_ver = int(ver[0])
minor_vers = ver[1].split('.')
minor_ver = int(minor_vers[0])
message = ""
try:
    import matplotlib
except ImportError:
    matplot_msg = "Could not load matplotlib package (required by Plot module)"
    message += matplot_msg + '\n'
    print(matplot_msg)

plot_ok = False
if major_ver > 0 or minor_ver >= 20:
    try:
        from FreeCAD.Plot import Plot  # Build-in plot module
        plot_ok = True
    except ImportError:
        plot_msg = "Could not load Plot module\nAttempting to use Plot workbench instead"
        message += plot_msg + "\n"
        print(plot_msg)
if not plot_ok:
    try:
        import freecad.plot.Plot as Plot  # Plot workbench
    except ImportError:
        plot_msg = "Could not load legacy Plot module"
        message += plot_msg + '\n'
        print(plot_msg)
Edit: Tested Code in AirPlaneDesign Wb
User avatar
johnwang
Veteran
Posts: 1382
Joined: Sun Jan 27, 2019 12:41 am

Re: Help to check FC version

Post by johnwang »

onekk wrote: Tue Jun 28, 2022 12:41 pm
Thank you very much.
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
User avatar
johnwang
Veteran
Posts: 1382
Joined: Sun Jan 27, 2019 12:41 am

Re: Help to check FC version

Post by johnwang »

Syres wrote: Tue Jun 28, 2022 3:44 pm Edit: Tested Code in AirPlaneDesign Wb
That's very nice. Thank you very much.
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
Post Reply