stl_validator.cpython-312.pyc•5.64 kB
�
-��gz � �l � d dl Z d dlZd dlZd dlZd dlmZmZ ej e� Z G d� d� Z
y)� N)�Tuple�Optionalc � � e Zd ZdZededeeee f fd�� Z ededeeee f fd�� Z
ededeeee f fd�� Zededeeee f fd�� Zy) �STLValidatorzd
Validates STL files to ensure they are manifold (watertight) and suitable for 3D printing.
�stl_file�returnc � � t j j | � sdd| � �fS t j j | � }|dk( ry t | d� 5 }|j d� }|dk( rt j | � \ }}nt j | � \ }}||fcddd� S # 1 sw Y yxY w# t $ r;}t j d t |� � �� dd t |� � �fcY d}~S d}~ww xY w)
z�
Validate an STL file to ensure it is manifold and suitable for 3D printing.
Args:
stl_file: Path to the STL file to validate
Returns:
Tuple of (is_valid, error_message)
FzSTL file not found: r )FzSTL file is empty�rb� s solidNzError validating STL file: )
�os�path�exists�getsize�open�readr �_validate_ascii_stl�_validate_binary_stl� Exception�logger�error�str)r � file_size�f�header�is_validr �es �;/home/ubuntu/openscad-mcp-server/src/utils/stl_validator.py�validate_stlzSTLValidator.validate_stl s� � � �w�w�~�~�h�'��0��
�;�;�;� �G�G�O�O�H�-� ���>�-�
A��h��%���������X�%�&2�&F�&F�x�&P�O�H�e� '3�&G�&G��&Q�O�H�e���� &�%�%�� � A��L�L�6�s�1�v�h�?�@��7��A��x�@�@�@�� A�s= �
B: �AB.�$ B: �.B7�3B: �7B: �: C>�0C9�3C>�9C>c �� � t | d� 5 }|j � }ddd� j � j d� sy|j � j d� sy|j d� }|j d� }|d k( ry
||dz k7 r
dd
|dz � d|� �fS y# 1 sw Y ��xY w# t $ r;}t j dt |� � �� ddt |� � �fcY d}~S d}~ww xY w)zValidate an ASCII STL file.�rN�solid)Fz)Invalid ASCII STL: Missing 'solid' header�endsolid)Fz,Invalid ASCII STL: Missing 'endsolid' footerzfacet normal�vertexr )Fz"Invalid ASCII STL: No facets found� FzInvalid ASCII STL: Expected z vertices, found �TNzError validating ASCII STL: )
r r �strip�
startswith�endswith�countr r r r )r r �content�facet_count�vertex_countr s r r z STLValidator._validate_ascii_stl2 s� � � B��h��$���&�&�(�� %� �=�=�?�-�-�g�6�I��=�=�?�+�+�J�7�L� "�-�-��7�K�"�=�=��2�L��a��B��{�Q��.�� <�[�1�_�<M�M^�_k�^l�m�m�m��) %�$��* � B��L�L�7��A��x�@�A��8��Q���A�A�A�� B�sE �B1 �B%�'B1 �B1 �''B1 �B1 �%B.�*B1 �1 C5�:0C0�*C5�0C5c �� � t | d� 5 }|j d� |j d� }t |� dk7 r
ddd� yt j |d�� }d|d z z }t j j | � }||k7 rd
d|� d|� �fcddd� S ddd� y
# 1 sw Y yxY w# t $ r;}t j dt |� � �� d
dt |� � �fcY d}~S d}~ww xY w)zValidate a binary STL file.r
�P � N)Fz"Invalid binary STL: File too short�little)� byteorder�T �2 Fz"Invalid binary STL: Expected size z, actual size r% zError validating binary STL: )
r �seekr �len�int�
from_bytesr r
r r r r r )r r �triangle_count_bytes�triangle_count�
expected_size�actual_sizer s r r z!STLValidator._validate_binary_stlO s� � � C��h��%�����r�
� ()�v�v�a�y�$��+�,��1�F� &�%� "%���0D�PX��!Y�� !#�n�r�&9� :�
� �g�g�o�o�h�7���-�/� �$F�}�o�Uc�do�cp�"q�q�# &�%�&