Skip to content

Commit

Permalink
Support adding cad geometry from local files and refresh (#394)
Browse files Browse the repository at this point in the history
* Support adding cad geometry from local files and refresh the cad geometry
  • Loading branch information
janvonrickenbach authored Feb 7, 2024
1 parent 9b2a437 commit 1a5c7a2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/ansys/acp/core/_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import typing
from typing import Callable, Optional, Protocol

from . import UnitSystemType
from . import CADGeometry, UnitSystemType
from ._server.acp_instance import ACP
from ._server.common import ServerProtocol
from ._tree_objects import Model
Expand Down Expand Up @@ -254,6 +254,33 @@ def get_local_acp_h5_file(self) -> pathlib.Path:
"""
return self._file_transfer_strategy.get_file(self._model.save, self._model.name + ".acph5")

def add_cad_geometry_from_local_file(self, path: pathlib.Path) -> CADGeometry:
"""Add a local CAD geometry to the ACP model.
Parameters
----------
path:
The path to the CAD geometry file.
"""
uploaded_file = self._add_input_file(path=path)
return self._model.create_cad_geometry(external_path=str(uploaded_file))

def refresh_cad_geometry_from_local_file(
self, path: pathlib.Path, cad_geometry: CADGeometry
) -> None:
"""Refresh the CAD geometry from a local file.
Parameters
----------
path:
The path to the CAD geometry file.
cad_geometry:
The CADGeometry object to refresh.
"""
uploaded_file_path = self._add_input_file(path=path)
cad_geometry.external_path = uploaded_file_path
cad_geometry.refresh()

def _add_input_file(self, path: pathlib.Path) -> pathlib.PurePath:
self._file_transfer_strategy.copy_input_file_to_local_workdir(path=path)
return self._file_transfer_strategy.upload_input_file_to_server(path=path)
Expand Down
32 changes: 32 additions & 0 deletions tests/unittests/test_workflow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pathlib
import shutil
import tempfile

import pytest
Expand Down Expand Up @@ -38,3 +39,34 @@ def test_workflow(acp_instance, model_data_dir, explict_temp_dir):
composite_definitions = workflow.get_local_composite_definitions_file()
assert composite_definitions == workflow.working_directory.path / "ACPCompositeDefinitions.h5"
assert composite_definitions.is_file()


def test_reload_cad_geometry(acp_instance, model_data_dir, load_cad_geometry):
input_file_path = model_data_dir / "minimal_model_2.cdb"

workflow = ACPWorkflow.from_cdb_file(
acp=acp_instance,
cdb_file_path=input_file_path,
)
workflow.model.update()

cad_geometry_file_path = model_data_dir / "square_and_solid.stp"
cad_geometry = workflow.add_cad_geometry_from_local_file(cad_geometry_file_path)

with tempfile.TemporaryDirectory() as tempdir:
copied_path = pathlib.Path(shutil.copy(cad_geometry_file_path, tempdir))

workflow.refresh_cad_geometry_from_local_file(copied_path, cad_geometry)
if acp_instance.is_remote:
assert cad_geometry.external_path == str(copied_path.name)
else:
assert cad_geometry.external_path == str(copied_path)

# Test that refresh works twice with the same local file.
workflow.refresh_cad_geometry_from_local_file(copied_path, cad_geometry)

# Test error when local file does not exist.
pathlib.Path.unlink(copied_path)
with pytest.raises(FileNotFoundError) as excinfo:
workflow.refresh_cad_geometry_from_local_file(copied_path, cad_geometry)
assert "No such file or directory" in str(excinfo.value)

0 comments on commit 1a5c7a2

Please sign in to comment.