cadquery icon indicating copy to clipboard operation
cadquery copied to clipboard

Isolate STEPControl_Reader ReadFile

Open lorenzncode opened this issue 1 year ago • 2 comments

Interface to OCCT Message

lorenzncode avatar Feb 18 '24 21:02 lorenzncode

Codecov Report

Attention: 8 lines in your changes are missing coverage. Please review.

Comparison is base (153ed3f) 94.48% compared to head (4853b7b) 94.37%.

Files Patch % Lines
cadquery/occ_impl/message.py 76.92% 5 Missing and 1 partial :warning:
cadquery/occ_impl/importers/__init__.py 80.00% 1 Missing and 1 partial :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1525      +/-   ##
==========================================
- Coverage   94.48%   94.37%   -0.11%     
==========================================
  Files          28       29       +1     
  Lines        5780     5813      +33     
  Branches     1071     1077       +6     
==========================================
+ Hits         5461     5486      +25     
- Misses        193      199       +6     
- Partials      126      128       +2     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Feb 18 '24 21:02 codecov[bot]

This would allow to process the STEPControl_Reader object without continuing to import the STEP file for example to extract header info. Another use case might be to abort STEP import if certain alerts or messages are issued on read. There may be other more general purpose OCCT message reporting uses.

import cadquery as cq

report = cq.message.Message.add_report()
reader = cq.importers.readStep("BROKEN_FILE.STEP")
alerts = report.GetAlerts(cq.message.Level.info.value)

if alerts:
    raise ValueError("STEP file read resulted in alerts, skip importStep")
else:
    res = cq.importers.importStep(reader)
import cadquery as cq

def import_step_abort_on_alert(filename, report=None):
    """
    Import STEP
    Abort in case of OCCT alert info messages

    This is NOT guaranteed to eliminate all crashes.
    It is an example based a known corrupted STEP file.  See issue #1521.

    It *might* help filter some problematic STEP files.
    In other cases perhaps trace level messages must be inspected or there 
    may not be any way to know up front whether a STEP file is problematic.
    """

    if report is None:
        report = cq.message.Message.add_report()

    report.Clear(cq.message.Level.info.value)
    reader = cq.importers.readStep(filename)
    alerts = report.GetAlerts(cq.message.Level.info.value)

    if alerts:
        raise ValueError("abort STEP import")
    else:
        res = cq.importers.importStep(reader)

    return res


step_files = ("BROKEN_FILE.STEP", "good.step", "BROKEN_FILE.STEP", "good.step")

report = cq.message.Message.add_report()
for f in step_files:
    try:
        res = import_step_abort_on_alert(f, report)
    except ValueError:
        res = None

lorenzncode avatar Feb 19 '24 00:02 lorenzncode