Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b221d2168e | |||
| 5ad615eb5b | |||
| 59bcacfd6d | |||
| d56282a140 | |||
| 2396810930 | |||
| 564b4a8012 | |||
| a06c9023fc | |||
| 4bcfe2cb2d | |||
| 4f32286a9b |
@@ -6,7 +6,7 @@
|
||||
"dockerFile": "../Dockerfile",
|
||||
|
||||
// Use 'postCreateCommand' to run commands after the container is created.
|
||||
// "postCreateCommand": "poetry install --no-root",
|
||||
"postCreateCommand": "poetry install --no-root",
|
||||
|
||||
// Configure tool-specific properties.
|
||||
"customizations": {
|
||||
|
||||
@@ -4,8 +4,6 @@ dist/
|
||||
*.egg-info/
|
||||
.cache/
|
||||
*.log
|
||||
.ruff_cache/
|
||||
.venv/
|
||||
.env/
|
||||
.git/
|
||||
Dockerfile copy*
|
||||
@@ -36,5 +36,5 @@ jobs:
|
||||
speckle_function_id: ${{ secrets.SPECKLE_FUNCTION_ID }}
|
||||
speckle_function_input_schema_file_path: ${{ env.FUNCTION_SCHEMA_FILE_NAME }}
|
||||
speckle_function_command: 'python -u main.py run'
|
||||
speckle_function_recommended_cpu_m: 2000
|
||||
speckle_function_recommended_memory_mi: 100
|
||||
speckle_function_recommended_cpu_m: 8000
|
||||
speckle_function_recommended_memory_mi: 8000
|
||||
|
||||
Regular → Executable
+3
-1
@@ -246,4 +246,6 @@ COPY . /home/speckle
|
||||
|
||||
# Using poetry, we generate a list of requirements, save them to requirements.txt, and then use pip to install them
|
||||
RUN poetry export --format requirements.txt --output /home/speckle/requirements.txt --without-hashes && \
|
||||
pip install --requirement /home/speckle/requirements.txt
|
||||
pip install --requirement /home/speckle/requirements.txt
|
||||
|
||||
RUN poetry install --no-root
|
||||
+36
-34
@@ -1,7 +1,13 @@
|
||||
from collections import defaultdict
|
||||
from concurrent.futures import ProcessPoolExecutor, as_completed
|
||||
from typing import List, Tuple, Any, Optional
|
||||
|
||||
import pymesh
|
||||
try:
|
||||
import pymesh
|
||||
except ImportError:
|
||||
from Geometry.mocks import mypymesh
|
||||
|
||||
pymesh = mypymesh
|
||||
|
||||
from speckle_automate import AutomationContext
|
||||
|
||||
@@ -10,7 +16,7 @@ from Geometry.mesh import cast
|
||||
|
||||
|
||||
def detect_clashes_old(
|
||||
reference_elements: List[Element], latest_elements: List[Element], _tolerance: float
|
||||
reference_elements: List[Element], latest_elements: List[Element], _tolerance: float
|
||||
) -> list[tuple[str, str, float]]:
|
||||
"""
|
||||
Detect clashes between two sets of mesh elements using Pymesh.
|
||||
@@ -41,11 +47,11 @@ def detect_clashes_old(
|
||||
continue
|
||||
|
||||
intersection = pymesh.boolean(
|
||||
ref_pymesh, latest_pymesh, operation="intersection"
|
||||
latest_pymesh, ref_pymesh, operation="intersection"
|
||||
)
|
||||
|
||||
if (
|
||||
intersection and intersection.volume > 0
|
||||
intersection and intersection.volume > 0
|
||||
): # TODO: could tolerance relate to this?
|
||||
severity = intersection.volume / min(
|
||||
ref_pymesh.volume, latest_pymesh.volume
|
||||
@@ -57,8 +63,8 @@ def detect_clashes_old(
|
||||
|
||||
|
||||
def check_for_clash(
|
||||
ref_element: Element, latest_element: Element
|
||||
) -> Optional[tuple[Any, Any, Any]]:
|
||||
ref_element: Element, latest_element: Element
|
||||
) -> Optional[tuple[Any, Any]]:
|
||||
"""
|
||||
Check for a clash between two elements and calculate the severity of the clash.
|
||||
|
||||
@@ -69,6 +75,7 @@ def check_for_clash(
|
||||
Returns:
|
||||
Tuple[str, str, float]: A tuple containing the IDs of the clashing elements and the severity, if a clash is found.
|
||||
"""
|
||||
|
||||
for ref_mesh in ref_element.meshes:
|
||||
for latest_mesh in latest_element.meshes:
|
||||
ref_pymesh = cast(ref_mesh, pymesh.Mesh)
|
||||
@@ -77,20 +84,17 @@ def check_for_clash(
|
||||
if not ref_pymesh or not latest_pymesh:
|
||||
continue
|
||||
|
||||
intersection = pymesh.boolean(
|
||||
ref_pymesh, latest_pymesh, operation="intersection"
|
||||
)
|
||||
intersection = pymesh.boolean(latest_pymesh, ref_pymesh, operation="intersection")
|
||||
|
||||
if intersection and intersection.volume > 0:
|
||||
severity = intersection.volume / min(
|
||||
ref_pymesh.volume, latest_pymesh.volume
|
||||
)
|
||||
return ref_element.id, latest_element.id, severity
|
||||
|
||||
return ref_element.id, latest_element.id
|
||||
return None
|
||||
|
||||
|
||||
def detect_clashes(
|
||||
reference_elements: List[Element], latest_elements: List[Element], _tolerance: float
|
||||
) -> List[Tuple[str, str, float]]:
|
||||
reference_elements: List[Element], latest_elements: List[Element], _tolerance: float
|
||||
) -> List[Tuple[str, str]]:
|
||||
"""
|
||||
Detect clashes between two sets of mesh elements using parallel processing.
|
||||
|
||||
@@ -118,31 +122,29 @@ def detect_clashes(
|
||||
|
||||
|
||||
def detect_and_report_clashes(
|
||||
reference_elements: list[Element],
|
||||
latest_elements: list[Element],
|
||||
tolerance: float,
|
||||
automate_context: AutomationContext,
|
||||
) -> list[tuple[str, str, float]]:
|
||||
reference_elements: list[Element],
|
||||
latest_elements: list[Element],
|
||||
tolerance: float,
|
||||
automate_context: AutomationContext,
|
||||
) -> list[tuple[str, str]]:
|
||||
|
||||
clashes = detect_clashes(reference_elements, latest_elements, tolerance)
|
||||
|
||||
total_clashes = len(clashes)
|
||||
padding_length = len(str(total_clashes))
|
||||
grouped_clashes = defaultdict(list)
|
||||
|
||||
for i, (ref_id, latest_id, severity) in enumerate(clashes, start=1):
|
||||
clash_number = str(i).zfill(padding_length)
|
||||
combined_message = f"Clash {clash_number}: between {ref_id} and {latest_id} with severity {severity:.2f}"
|
||||
object_ids = [ref_id, latest_id]
|
||||
for ref, latest in clashes:
|
||||
if not latest:
|
||||
continue
|
||||
grouped_clashes[ref].append(latest)
|
||||
|
||||
# Assuming severity levels: Low (<0.25), Medium (0.25-0.75), High (>0.75) TODO: Determine severity levels
|
||||
if severity > 0.75:
|
||||
category = "High"
|
||||
elif severity > 0.25:
|
||||
category = "Medium"
|
||||
else:
|
||||
category = "Low"
|
||||
for group_number, clashing_objects in enumerate(grouped_clashes.items(), start=1):
|
||||
|
||||
ref_id, latest_elements = clashing_objects
|
||||
|
||||
all_clashing_objects = [ref_id] + [element_id for element_id in latest_elements]
|
||||
|
||||
automate_context.attach_error_to_objects(
|
||||
category=category, object_ids=object_ids, message=combined_message
|
||||
category="Clash", object_ids=all_clashing_objects, message=str(group_number)
|
||||
)
|
||||
|
||||
return clashes
|
||||
|
||||
+16
-14
@@ -1,19 +1,15 @@
|
||||
from typing import Union, Type, TYPE_CHECKING
|
||||
from typing import Union, Type
|
||||
|
||||
try:
|
||||
import pymesh
|
||||
except ImportError:
|
||||
from Geometry.mocks import mypymesh
|
||||
|
||||
import pymesh
|
||||
|
||||
pymesh = mypymesh
|
||||
|
||||
import trimesh
|
||||
|
||||
|
||||
class MockPyMesh:
|
||||
def __init__(self, vertices, faces):
|
||||
self.vertices = vertices or []
|
||||
self.faces = faces or []
|
||||
|
||||
def boolean(self, other, operation):
|
||||
return MockPyMesh([], [])
|
||||
from Geometry.helpers import triangulate_face
|
||||
|
||||
|
||||
def trimesh_to_pymesh(mesh: trimesh.Trimesh) -> pymesh.Mesh:
|
||||
@@ -39,7 +35,7 @@ def pymesh_to_trimesh(mesh: pymesh.Mesh) -> trimesh.Trimesh:
|
||||
|
||||
|
||||
def cast(
|
||||
mesh: Union[trimesh.Trimesh, pymesh.Mesh], target_type: Type
|
||||
mesh: Union[trimesh.Trimesh, pymesh.Mesh], target_type: Type
|
||||
) -> Union[trimesh.Trimesh, pymesh.Mesh]:
|
||||
"""
|
||||
Casts a mesh object to a specified type.
|
||||
@@ -74,7 +70,7 @@ def speckle_mesh_to_trimesh(input_mesh: SpeckleMesh) -> trimesh.Trimesh:
|
||||
face_vertex_count = input_mesh.faces[i]
|
||||
i += 1 # Skip the vertex count
|
||||
|
||||
face_vertex_indices = input_mesh.faces[i : i + face_vertex_count]
|
||||
face_vertex_indices = input_mesh.faces[i: i + face_vertex_count]
|
||||
|
||||
face_vertices = [
|
||||
Vector.from_list(vertices[idx].tolist()) for idx in face_vertex_indices
|
||||
@@ -90,4 +86,10 @@ def speckle_mesh_to_trimesh(input_mesh: SpeckleMesh) -> trimesh.Trimesh:
|
||||
|
||||
i += face_vertex_count
|
||||
|
||||
return trimesh.Trimesh(vertices=vertices, faces=np.array(faces))
|
||||
t_mesh = trimesh.Trimesh(vertices=vertices, faces=np.array(faces))
|
||||
|
||||
obbox = t_mesh.bounding_box_oriented
|
||||
|
||||
obbox_mesh = obbox.to_mesh()
|
||||
|
||||
return obbox_mesh
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
class mypymesh:
|
||||
class Mesh:
|
||||
def __init__(self, vertices, faces):
|
||||
self.vertices = vertices
|
||||
self.faces = faces
|
||||
|
||||
@property
|
||||
def volume(self):
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def boolean(mesh_a, mesh_b, operation):
|
||||
return mypymesh.Mesh([], [])
|
||||
|
||||
@staticmethod
|
||||
def form_mesh(vertices, faces):
|
||||
return mypymesh.Mesh(vertices, faces)
|
||||
+3
-5
@@ -30,12 +30,12 @@ class ElementCheckRules:
|
||||
"""Rule: Check if a parameter is displayable."""
|
||||
return (
|
||||
lambda parameter: parameter.displayValue
|
||||
and parameter.displayValue is not None
|
||||
and parameter.displayValue is not None
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def speckle_type_rule(
|
||||
desired_type: Union[str, List[str]]
|
||||
desired_type: Union[str, List[str]]
|
||||
) -> Callable[[Base], bool]:
|
||||
"""Rule: Check if a parameter's speckle_type matches the desired type."""
|
||||
|
||||
@@ -43,9 +43,7 @@ class ElementCheckRules:
|
||||
if isinstance(desired_type, str):
|
||||
desired_type = [desired_type]
|
||||
|
||||
print(desired_type)
|
||||
|
||||
return (
|
||||
lambda speckle_object: getattr(speckle_object, "speckle_type", None)
|
||||
in desired_type
|
||||
in desired_type
|
||||
)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use the automation_context module to wrap your function in an Automate context helper
|
||||
"""
|
||||
from collections import defaultdict
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import Field
|
||||
@@ -36,27 +37,26 @@ class FunctionInputs(AutomateBase):
|
||||
title="Static Model Name",
|
||||
description="Name of the static structural model.",
|
||||
)
|
||||
|
||||
|
||||
tolerance: float = Field(
|
||||
default=25.0,
|
||||
title="Tolerance",
|
||||
description="Specify the tolerance value for the analysis. \
|
||||
Negative values relaxes the test, positive values make it more strict.",
|
||||
readonly=True,
|
||||
)
|
||||
tolerance_unit: str = Field( # Using the SpecklePy Units enum here
|
||||
default=Units.mm,
|
||||
json_schema_extra={"examples": ["mm", "cm", "m"]},
|
||||
title="Tolerance Unit",
|
||||
description="Unit of the tolerance value.",
|
||||
readonly=True,
|
||||
)
|
||||
tolerance: float = Field(
|
||||
default=25.0,
|
||||
title="Tolerance",
|
||||
description="Specify the tolerance value for the analysis. \
|
||||
Negative values relaxes the test, positive values make it more strict.",
|
||||
json_schema_extra={
|
||||
"readOnly": True,
|
||||
}
|
||||
)
|
||||
tolerance_unit: str = Field( # Using the SpecklePy Units enum here
|
||||
default=Units.mm,
|
||||
json_schema_extra={"examples": ["mm", "cm", "m"], "readOnly": True},
|
||||
title="Tolerance Unit",
|
||||
description="Unit of the tolerance value.",
|
||||
)
|
||||
|
||||
|
||||
def automate_function(
|
||||
automate_context: AutomationContext,
|
||||
function_inputs: FunctionInputs,
|
||||
automate_context: AutomationContext,
|
||||
function_inputs: FunctionInputs,
|
||||
) -> None:
|
||||
"""This is an example Speckle Automate function.
|
||||
|
||||
@@ -71,9 +71,10 @@ def automate_function(
|
||||
changed_model_version = automate_context.receive_version()
|
||||
|
||||
try:
|
||||
reference_model_version = get_reference_model(
|
||||
reference_model_version, reference_model_id, reference_model_version_id = get_reference_model(
|
||||
automate_context, function_inputs.static_model_name
|
||||
)
|
||||
print(f"Reference model id: {reference_model_id}, version id: {reference_model_version_id}")
|
||||
|
||||
except Exception as ex:
|
||||
automate_context.mark_run_failed(status_message=str(ex))
|
||||
@@ -116,6 +117,7 @@ def automate_function(
|
||||
for base_obj, id, transform in reference_objects
|
||||
if visible_beams_rule(base_obj)
|
||||
]
|
||||
|
||||
latest_displayable_objects = [
|
||||
(base_obj, id, transform)
|
||||
for base_obj, id, transform in latest_objects
|
||||
@@ -129,26 +131,27 @@ def automate_function(
|
||||
speckle_to_element(obj) for obj in latest_displayable_objects
|
||||
]
|
||||
|
||||
# using trimesh library process all these meshes in the form of A vs B
|
||||
# and get the clashes
|
||||
tolerance = function_inputs.tolerance
|
||||
|
||||
if len(reference_mesh_elements) == 0 or len(latest_mesh_elements) == 0:
|
||||
automate_context.mark_run_failed(
|
||||
status_message="Clash detection failed. No objects to compare."
|
||||
)
|
||||
return
|
||||
|
||||
clashes = detect_and_report_clashes(
|
||||
reference_mesh_elements, latest_mesh_elements, tolerance, automate_context
|
||||
)
|
||||
|
||||
# all object count
|
||||
# all reference objects count
|
||||
# all latest objects count
|
||||
|
||||
|
||||
percentage_reference_objects_clashing = (
|
||||
len(set([ref_id for ref_id, latest_id, severity in clashes]))
|
||||
/ len(reference_mesh_elements)
|
||||
* 100
|
||||
len(set([ref_id for ref_id, latest_id in clashes]))
|
||||
/ len(reference_mesh_elements)
|
||||
* 100
|
||||
)
|
||||
percentage_latest_objects_clashing = (
|
||||
len(set([latest_id for ref_id, latest_id, severity in clashes]))
|
||||
/ len(latest_mesh_elements)
|
||||
* 100
|
||||
len(set([latest_id for ref_id, latest_id in clashes]))
|
||||
/ len(latest_mesh_elements)
|
||||
* 100
|
||||
)
|
||||
|
||||
# all clashes count
|
||||
@@ -164,14 +167,18 @@ def automate_function(
|
||||
f"{percentage_latest_objects_clashing}%."
|
||||
)
|
||||
|
||||
reference_view = [f"{reference_model_id}@{reference_model_version_id}"]
|
||||
|
||||
automate_context.set_context_view(reference_view)
|
||||
|
||||
automate_context.mark_run_success(
|
||||
status_message="Clash detection completed. " + clash_report_message
|
||||
)
|
||||
|
||||
|
||||
def get_reference_model(
|
||||
automate_context: AutomationContext, static_model_name: str
|
||||
) -> Base:
|
||||
automate_context: AutomationContext, static_model_name: str
|
||||
) -> tuple[Base, Optional[str], Optional[str]]:
|
||||
# the static reference model will be retrieved from the project using model name stored in the inputs
|
||||
speckle_client = automate_context.speckle_client
|
||||
project_id = automate_context.automation_run_data.project_id
|
||||
@@ -205,7 +212,7 @@ def get_reference_model(
|
||||
remote_transport,
|
||||
) # receive the static model
|
||||
|
||||
return latest_reference_model_version
|
||||
return latest_reference_model_version, model.id, reference_model_commits[0].id
|
||||
|
||||
|
||||
# make sure to call the function with the executor
|
||||
|
||||
Generated
+1291
File diff suppressed because it is too large
Load Diff
+7
-7
@@ -9,13 +9,13 @@ readme = "README.md"
|
||||
python = "^3.10"
|
||||
specklepy = "2.17.11"
|
||||
trimesh = "^4.0.4"
|
||||
pymesh = {git = "https://github.com/PyMesh/PyMesh.git"}
|
||||
pytest = "^7.4.2"
|
||||
python-dotenv = "^1.0.0"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
black = "^23.3.0"
|
||||
mypy = "^1.3.0"
|
||||
ruff = "^0.0.271"
|
||||
pytest = "^7.4.2"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
@@ -23,11 +23,11 @@ build-backend = "poetry.core.masonry.api"
|
||||
|
||||
[tool.ruff]
|
||||
select = [
|
||||
"E", # pycodestyle
|
||||
"F", # pyflakes
|
||||
"UP", # pyupgrade
|
||||
"D", # pydocstyle
|
||||
"I", # isort
|
||||
"E", # pycodestyle
|
||||
"F", # pyflakes
|
||||
"UP", # pyupgrade
|
||||
"D", # pydocstyle
|
||||
"I", # isort
|
||||
]
|
||||
|
||||
[tool.ruff.pydocstyle]
|
||||
|
||||
+17
-17
@@ -23,12 +23,12 @@ def crypto_random_string(length: int) -> str:
|
||||
|
||||
|
||||
def register_new_automation(
|
||||
project_id: str,
|
||||
model_id: str,
|
||||
speckle_client: SpeckleClient,
|
||||
automation_id: str,
|
||||
automation_name: str,
|
||||
automation_revision_id: str,
|
||||
project_id: str,
|
||||
model_id: str,
|
||||
speckle_client: SpeckleClient,
|
||||
automation_id: str,
|
||||
automation_name: str,
|
||||
automation_revision_id: str,
|
||||
):
|
||||
"""Register a new automation in the speckle server."""
|
||||
query = gql(
|
||||
@@ -97,13 +97,13 @@ def test_object() -> Base:
|
||||
# fixture to mock the AutomationRunData that would be generated by a full Automation Run
|
||||
def fake_automation_run_data(request, test_client: SpeckleClient) -> AutomationRunData:
|
||||
server_url = request.config.SPECKLE_SERVER_URL
|
||||
project_id = "4f064f09e6"
|
||||
model_id = "5a16cf52af"
|
||||
project_id = "7d8e96669a"
|
||||
model_id = "efeb71387b"
|
||||
|
||||
function_name = "Clash Test"
|
||||
|
||||
automation_id = crypto_random_string(10)
|
||||
automation_name = "Local Test Automation"
|
||||
automation_name = "Long running clash test"
|
||||
automation_revision_id = crypto_random_string(10)
|
||||
|
||||
register_new_automation(
|
||||
@@ -119,7 +119,7 @@ def fake_automation_run_data(request, test_client: SpeckleClient) -> AutomationR
|
||||
project_id=project_id,
|
||||
model_id=model_id,
|
||||
branch_name="main",
|
||||
version_id="2b16327448",
|
||||
version_id="2eb06c1034",
|
||||
speckle_server_url=server_url,
|
||||
# These ids would be available with a valid registered Automation definition.
|
||||
automation_id=automation_id,
|
||||
@@ -142,7 +142,7 @@ def test_function_run(fake_automation_run_data: AutomationRunData, speckle_token
|
||||
context,
|
||||
automate_function,
|
||||
FunctionInputs(
|
||||
tolerance=0.1, tolerance_unit="mm", static_model_name="structural"
|
||||
tolerance=0.1, tolerance_unit="mm", static_model_name="simple beams"
|
||||
),
|
||||
)
|
||||
|
||||
@@ -156,26 +156,26 @@ def context(fake_automation_run_data: AutomationRunData, speckle_token: str):
|
||||
|
||||
def test_non_existent_model(context, test_client: SpeckleClient):
|
||||
with pytest.raises(
|
||||
Exception, match="The static model named does not exist, skipping the function."
|
||||
Exception, match="The static model named does not exist, skipping the function."
|
||||
):
|
||||
get_reference_model(context, "Fake Name")
|
||||
|
||||
|
||||
def test_model_with_no_versions(context, test_client: SpeckleClient):
|
||||
with pytest.raises(
|
||||
Exception, match="The static model has no versions, skipping the function."
|
||||
Exception, match="The static model has no versions, skipping the function."
|
||||
):
|
||||
get_reference_model(context, "blank")
|
||||
|
||||
|
||||
def test_same_as_changed_model(context, test_client: SpeckleClient):
|
||||
with pytest.raises(
|
||||
Exception,
|
||||
match="The static model is the same as the changed model, skipping the function.",
|
||||
Exception,
|
||||
match="The static model is the same as the changed model, skipping the function.",
|
||||
):
|
||||
get_reference_model(context, "hvac")
|
||||
get_reference_model(context, "clash simple")
|
||||
|
||||
|
||||
def test_valid_reference_model(context, test_client: SpeckleClient):
|
||||
reference_model = get_reference_model(context, "structural")
|
||||
reference_model = get_reference_model(context, "simple beams")
|
||||
assert reference_model is not None
|
||||
|
||||
Reference in New Issue
Block a user