windows, no ifcopenshell does not print an error message

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

windows, no ifcopenshell does not print an error message

Postby bernd » Tue Jul 30, 2019 9:05 pm

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?
hlg
Posts: 29
Joined: Fri Jul 12, 2019 10:11 am

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

Postby hlg » Tue Aug 06, 2019 4:54 pm

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

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

Postby bernd » Tue Aug 06, 2019 7:01 pm

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
Site Admin
Posts: 16300
Joined: Thu Feb 19, 2009 10:32 am

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

Postby wmayer » 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

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

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

Postby bernd » Wed Aug 07, 2019 7:35 pm

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

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

Postby bernd » Wed Aug 07, 2019 7:44 pm

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
hlg
Posts: 29
Joined: Fri Jul 12, 2019 10:11 am

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

Postby hlg » Thu Aug 08, 2019 10:43 am

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

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

Postby bernd » Thu Aug 08, 2019 7:35 pm

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.