Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Howto get stdout output from cqparts/cadquery scripts #85

Closed
gntech opened this issue May 11, 2018 · 9 comments
Closed

Question: Howto get stdout output from cqparts/cadquery scripts #85

gntech opened this issue May 11, 2018 · 9 comments

Comments

@gntech
Copy link
Contributor

gntech commented May 11, 2018

I like to have print() statements or log.info() statments in my code that shows information about script progress, calculated design values, and so on. Is it possible somehow? Right now it looks like it is swallowed by FreeCAD somehow. Sometimes I get the output from the script after the entire script is finished. I dont get error or tracebacks from the script which is annoying as well.

import os
from ct1 import CoffeTable

table = CoffeTable()

working_folder = os.path.dirname(os.path.realpath(__file__))
dir2d = os.path.join(working_folder, "build/2d")
dir3d = os.path.join(working_folder, "build/3d")
dirgltf = os.path.join(working_folder, "build/gltf")

print(table.tree_str(name="ct1"))

print("Exporting part SVG 2D-views")
if not os.path.isdir(dir2d):
    os.makedirs(dir2d)
table.find('leg_0').local_obj.exportSvg(os.path.join(dir2d, "ct1_leg.svg"), view_vector=(0,1,0))
table.find('shelf_0').local_obj.exportSvg(os.path.join(dir2d, "ct1_shelf.svg"), view_vector=(0,0,-1))
table.find('glass_top').local_obj.exportSvg(os.path.join(dir2d, "ct1_glass_top.svg"), view_vector=(0,0,1))

print("Exporting part STEP 3D-models")
if not os.path.isdir(dir3d):
    os.makedirs(dir3d)
table.find('leg_0').exporter("step")(os.path.join(dir3d, "ct1_leg.step"))
table.find('shelf_0').exporter("step")(os.path.join(dir3d, "ct1_shelf.step"))
table.find('glass_top').exporter("step")(os.path.join(dir3d, "ct1_glass_top.step"))

print("Exporting assembly GLTF-model")
if not os.path.isdir(dirgltf):
    os.makedirs(dirgltf)
table.exporter('gltf')(os.path.join(dir3d, "ct1.gltf"))
@jmwright
Copy link

@gntech From within FreeCAD, you can use FreeCAD.Console.

import FreeCAD

FreeCAD.Console.PrintMessage("Debug\r\n")
FreeCAD.Console.PrintError("Error!\r\n")

At one time I had a print function defined within the CadQuery module that abstracted the FreeCAD boiler plate code out, but it caused problems so I removed it. I probably just didn't implement it correctly and so it caused conflicts with the stock print function.

@jmwright
Copy link

@gntech Looking at the code, it looks like I implemented a print hook in a different way later on. I had forgotten about that. The following prints correctly to the "Report view" for me.

print("HERE")

@gntech
Copy link
Contributor Author

gntech commented May 11, 2018

@jmwright I run the script above from command line like so: python build_ct1.py and I get this output to the terminal.

$ python build_ct1.py
No handlers could be found for logger "cqparts.assembly"
Exporting assembly GLTF-model		

It is very strange, only the last print() statement gets printed

Perhaps there is something about "No handlers could be found for logger"

@gntech
Copy link
Contributor Author

gntech commented May 12, 2018

When I run the script from freecad all print statements are shown. However, it seems they are put on hold so everything prints after the script is done.

@fragmuffin
Copy link
Member

This was also addressed in dcowden/cadquery#200

import cadquery
cadquery.freecad_impl.console_logging.enable()
import logging
log = logging.getLogger(__name__)

log.debug("not shown")
log.info("displayed as normal")
log.warning("shown in amber")
log.warning("shown in red")

also, cqparts uses logging to print debug information.

@fragmuffin
Copy link
Member

@gntech has this answered your question?
also, what worked for you?... I'd like to put this into the documentation somewhere, perhaps as the first tutorial.

@gntech
Copy link
Contributor Author

gntech commented May 13, 2018

Tanks for your suggestion. However, It will be some time before I will be able to test it. I will answer here then

@fragmuffin
Copy link
Member

@gntech no problem. Take your time.

@gntech
Copy link
Contributor Author

gntech commented May 16, 2018

Okay, I have done some troubleshooting and I think it comes down to the suppress_stdout_stderr()-function in cadqeury. I have opened a new issue there, see dcowden/cadquery#267

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants