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

Isolate STEPControl_Reader ReadFile #1525

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cadquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)
from .occ_impl import exporters
from .occ_impl import importers
from .occ_impl import message

# these items are the common implementation

Expand Down Expand Up @@ -76,4 +77,5 @@
"Selector",
"plugins",
"Sketch",
"message",
]
35 changes: 29 additions & 6 deletions cadquery/occ_impl/importers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from ... import cq
from ..shapes import Shape
from .dxf import _importDXF
from .. import message
from ...utils import cqmultimethod as multimethod

RAD2DEG = 360.0 / (2 * pi)

Expand Down Expand Up @@ -60,19 +62,40 @@
return cq.Workplane("XY").newObject([shape])


# Loads a STEP file into a CQ.Workplane object
def importStep(fileName: str) -> "cq.Workplane":
def readStep(fileName: str) -> STEPControl_Reader:
"""
Accepts a file name and loads the STEP file into a cadquery Workplane
Read STEP file, return STEPControl_Reader

:param fileName: The path and name of the STEP file to be imported
:param fileName: The path and name of the STEP file
"""

# Now read and return the shape
reader = STEPControl_Reader()
readStatus = reader.ReadFile(fileName)
if readStatus != OCP.IFSelect.IFSelect_RetDone:
raise ValueError("STEP File could not be loaded")
return reader


@multimethod
def importStep(fileName: str) -> "cq.Workplane":
"""
Accepts a file name and loads the STEP file into a cadquery Workplane

:param fileName: The path and name of the STEP file to be imported
"""

return importStep(readStep(fileName))


@importStep.register
def importStep(reader: STEPControl_Reader) -> "cq.Workplane":
"""
Import STEP given a STEPControl_Reader.

:param reader: STEPControl_Reader with loaded file
"""
if reader.WS().LoadedFile() == "":
raise ValueError("STEP reader has no loaded file")

Check warning on line 97 in cadquery/occ_impl/importers/__init__.py

View check run for this annotation

Codecov / codecov/patch

cadquery/occ_impl/importers/__init__.py#L97

Added line #L97 was not covered by tests

for i in range(reader.NbRootsForTransfer()):
reader.TransferRoot(i + 1)

Expand Down
53 changes: 53 additions & 0 deletions cadquery/occ_impl/message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from enum import Enum

from OCP.Message import (
Message_Gravity,
Message_Messenger,
Message_PrinterToReport,
Message_Report,
)
from OCP.Message import Message as OCPMessage


class Level(Enum):
trace = Message_Gravity.Message_Trace
info = Message_Gravity.Message_Info
warning = Message_Gravity.Message_Warning
alarm = Message_Gravity.Message_Alarm
fail = Message_Gravity.Message_Fail


class Message:

messenger: Message_Messenger = OCPMessage.DefaultMessenger_s()

@staticmethod
def add_report() -> Message_Report:
"""
Add a report
"""
printer = Message_PrinterToReport()
report = printer.Report()
Message.messenger.AddPrinter(printer)
return report

Check warning on line 32 in cadquery/occ_impl/message.py

View check run for this annotation

Codecov / codecov/patch

cadquery/occ_impl/message.py#L29-L32

Added lines #L29 - L32 were not covered by tests

@staticmethod
def add_log(fname):
pass

Check warning on line 36 in cadquery/occ_impl/message.py

View check run for this annotation

Codecov / codecov/patch

cadquery/occ_impl/message.py#L36

Added line #L36 was not covered by tests

@staticmethod
def set_trace_level(level=Level.info):
"""
Set trace level used for filtering OCCT messages.

Changes the trace level of the default printer.

:param level: trace level
"""
for printer in Message.messenger.Printers():
printer.SetTraceLevel(level.value)
break


# change default OCCT logging level
Message.set_trace_level(Level.fail)
6 changes: 0 additions & 6 deletions cadquery/occ_impl/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@

from ..utils import cqmultimethod as multimethod

# change default OCCT logging level
from OCP.Message import Message, Message_Gravity

for printer in Message.DefaultMessenger_s().Printers():
printer.SetTraceLevel(Message_Gravity.Message_Fail)

import OCP.TopAbs as ta # Topology type enum
import OCP.GeomAbs as ga # Geometry type enum

Expand Down