windows, no ifcopenshell does not print an error message

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

windows, no ifcopenshell does not print an error message

Post by bernd »

If ifcopenshell is missing in a Python shell (not the one in FreeCAD !) there is no error message printed. This was tested with the 0.19 dev package ...

code: start a python shell

Code: Select all

import sys, os
sys.path.append('C:/0_BHA_privat/progr/FreeCAD_0.19.xxxxx_Py3Qt5/bin')  # FreeCAD
#sys.path.append('C:/0_BHA_privat/progr/FreeCAD_0.19.xxxxx_Py3Qt5/bin/Lib/site-packages')  # IfcOpenShell
ifcfilepath = os.path.join(os.path.expanduser('~'), 'Desktop', 'column.ifc')

import FreeCAD, Arch, importIFC
FreeCAD.newDocument("mydoc")
col = Arch.makeStructure(None, 400, 400, 2500)
col.Document.recompute()
importIFC.export([col], ifcfilepath)
no ifc will be exorted.

output:

Code: Select all

>>> import sys, os
>>> sys.path.append('C:/0_BHA_privat/progr/FreeCAD_0.19.xxxxx_Py3Qt5/bin')  # FreeCAD
>>> #sys.path.append('C:/0_BHA_privat/progr/FreeCAD_0.19.xxxxx_Py3Qt5/bin/Lib/site-packages')  # IfcOpenShell
... ifcfilepath = os.path.join(os.path.expanduser('~'), 'Desktop', 'column.ifc')
>>>
>>> import FreeCAD, Arch, importIFC
>>> FreeCAD.newDocument("mydoc")
<Document object at 000002521844ADE0>
>>> col = Arch.makeStructure(None, 400, 400, 2500)
>>> col.Document.recompute()
1
>>> importIFC.export([col], ifcfilepath)
>>>

but import ifcopenshell gives an error:

Code: Select all

>>> import ifcopenshell
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'ifcopenshell'
>>>
if the ifcopenshell path is added (see first code and uncomment the sys.path.append()) everything works fine.

But ther should be an errormessage, but for some reason it is not printed ! https://github.com/FreeCAD/FreeCAD/blob ... C.py#L1502

Could someone confirm this?
User avatar
hlg
Posts: 39
Joined: Fri Jul 12, 2019 10:11 am

Re: windows, no ifcopenshell does not print an error message

Post by hlg »

Yes, I can confirm this. Looks like FreeCAD.Console.PrintError (and all other FreeCAD.Console methods) do only have an effect when used with FreeCAD in GUI or cmd mode, not when used as a library. May be on purpose.
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: windows, no ifcopenshell does not print an error message

Post by bernd »

Good find :D

in FreeCAD:
code:

Code: Select all

import FreeCAD
print('hello FreeCAD 1')
FreeCAD.Console.PrintError('hello FreeCAD 2')
output:

Code: Select all

>>> 
>>> import FreeCAD
>>> print('hello FreeCAD 1')
hello FreeCAD 1
>>> FreeCAD.Console.PrintError('hello FreeCAD 2')
>>> 
but in addition in the Report view and in the bash FreeCAD was started in there are nice red messages 8-) :

Code: Select all

hello FreeCAD 2



in Python console outside FreeCAD:
code:

Code: Select all

import sys
sys.path.append("/usr/local/lib")  # path to FreeCAD.so
import FreeCAD
print('hello FreeCAD 1')
FreeCAD.Console.PrintError('hello FreeCAD 2')
output:

Code: Select all

>>> 
>>> import sys
>>> sys.path.append("/usr/local/lib")  # path to FreeCAD.so
>>> import FreeCAD
>>> print('hello FreeCAD 1')
hello FreeCAD 1
>>> FreeCAD.Console.PrintError('hello FreeCAD 2')
>>> 


how to redirect this into the Python console? Or is there any other possibility to catch the FreeCAD Console prints?
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: windows, no ifcopenshell does not print an error message

Post by wmayer »

A few weeks ago I implemented a feature request to suppress messages, warnings, errors and log messages when using FreeCAD as a Python module because there are uses cases where these messages break certain workflows. See git commit 1406c03d1

As you can see the application parameter "LoggingConsole" is only set for the GUI and command line versions but not for the module version. The effect is that at initialization time the console observer won't be instantiated any more and thus nothing will be printed to the console when using the FreeCAD API. Errors raised by Python are still printed to the console because it uses its own mechanism.

So, to solve your issue we can add a function to the FreeCAD module to explicitly create the console observer after the import. Feel free to open a ticket for it.
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: windows, no ifcopenshell does not print an error message

Post by bernd »

wmayer wrote: Wed Aug 07, 2019 8:17 am A few weeks ago I implemented a feature request to suppress messages, warnings, errors and log messages when using FreeCAD as a Python module because there are uses cases where these messages break certain workflows. See git commit 1406c03d1
issue #4000
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: windows, no ifcopenshell does not print an error message

Post by bernd »

wmayer wrote: Wed Aug 07, 2019 8:17 am As you can see the application parameter "LoggingConsole" is only set for the GUI and command line versions but not for the module version. The effect is that at initialization time the console observer won't be instantiated any more and thus nothing will be printed to the console when using the FreeCAD API. Errors raised by Python are still printed to the console because it uses its own mechanism.
got the point :)


wmayer wrote: Wed Aug 07, 2019 8:17 am So, to solve your issue we can add a function to the FreeCAD module to explicitly create the console observer after the import. Feel free to open a ticket for it.
issue #4081
User avatar
hlg
Posts: 39
Joined: Fri Jul 12, 2019 10:11 am

Re: windows, no ifcopenshell does not print an error message

Post by hlg »

Ticket #4081 says:
In FEM, Arch and Draft we gone make heavy usage of console prints. In lot of cases not an error is raised but with a try except the problem is catched and an error is printed to the user someing has not been done, but the method runs further.
I think there is a catch when you use FreeCAD as a Python module and error handling is done this way inside the module. You would not be able to handle the errors yourself even if you wanted. Wouldn't it be better to have core methods that raise proper errors with meaningful messages? These could be used from external Python applications as well as from internal wrapper methods. Only the latter would catch and print those messages to the FreeCAD console.
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: windows, no ifcopenshell does not print an error message

Post by bernd »

good point ... if we would like to export to ifc and there is no ifcopenshell we may should raise an error instead of just print an console error and return.

As you said if one writes some script he can catch the error if needed.

I need to think if we have examples in FEM where the feature reequest makes sense anyway.
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: windows, no ifcopenshell does not print an error message

Post by wmayer »

Post Reply