
    'j/;                        d dl mZ d dlmZmZmZmZ d dlmZm	Z	m
Z
 d dlmZmZmZmZ ddlmZ ddlmZmZmZmZmZ d'd(dZd)dZ	 d*d+dZd,d-dZd.dZ G d d          Zd/d#Zd0d%Zd*d1d&ZdS )2    )annotations)IteratorSequenceOptionalIterable)MeshVertexMergerMeshTransformerMeshBuilder)Matrix44Vec3NULLVECBoundingBox   )entities)BodyLumpNONE_REFFaceShellTbodyr   returnlist[MeshTransformer]c                   t          | t                    st          dt          |                      g }t	                      }t          |           D ]S}|D ]}|                    |           |s5|                    t          j	        |                     t	                      }T|r'|                    t          j	        |                     |S )a  Returns a list of :class:`~ezdxf.render.MeshTransformer` instances from
    the given ACIS :class:`Body` entity.
    The list contains multiple meshes if `merge_lumps` is ``False`` or just a
    single mesh if `merge_lumps` is ``True``.

    The ACIS format stores the faces in counter-clockwise orientation where the
    face-normal points outwards (away) from the solid body (material).

    .. note::

        This function returns meshes build up only from flat polygonal
        :class:`Face` entities, for a tessellation of more complex ACIS
        entities (spline surfaces, tori, cones, ...) is an ACIS kernel
        required which `ezdxf` does not provide.

    Args:
        body: ACIS entity of type :class:`Body`
        merge_lumps: returns all :class:`Lump` entities
            from a body as a single mesh if ``True`` otherwise each :class:`Lump`
            entity is a separated mesh

    Raises:
        TypeError: given `body` entity has invalid type

    expected a body entity, got: )

isinstancer   	TypeErrortyper   flat_polygon_faces_from_bodyadd_faceappendr	   from_builder)r   merge_lumpsmeshesbuilderfacesfaces         I/DATA/AppData/hermes/venv/lib/python3.11/site-packages/ezdxf/acis/mesh.pymesh_from_bodyr(      s    4 dD!! FDT

DDEEE$&F  G-d33 ) ) 	# 	#DT"""" 	)MM/6w??@@@&((G =o27;;<<<M    Iterator[list[Sequence[Vec3]]]c              #    K   t          | t                    st          dt          |                      | j        }| j        }d}|j        s|j        }|j        s/t          t          ||                    V  |j
        }|j        -dS dS )a$  Yields all flat polygon faces from all lumps in the given
    :class:`Body` entity.
    Yields a separated list of faces for each linked :class:`Lump` entity.

    Args:
        body: ACIS entity of type :class:`Body`

    Raises:
        TypeError: given `body` entity has invalid type

    r   N)r   r   r   r   lump	transformis_nonematrixlistflat_polygon_faces_from_lump	next_lump)r   r,   r-   ms       r'   r   r   5   s       dD!! FDT

DDEEE9DI A l /a8899999~ l     r)   Nr,   r   r3   Matrix44 | NoneIterator[Sequence[Vec3]]c              #    K   t          | t                    st          dt          |                      | j        }|j        rdS g }|j        }|j        s(t          }|                                 |j	        j        dk    r	 |j
        j        }n# t          $ r Y nw xY w|}|j        s|j        }	 |j        j        dk    rn|j        r%|                    |j        j        j                   n$|                    |j        j        j                   n# t          $ r Y nOw xY w|j        }||u r9|%t-          |                    |                    V  nt-          |          V  n|j        |j        }|j        &dS dS )av  Yields all flat polygon faces from the given :class:`Lump` entity as
    sequence of :class:`~ezdxf.math.Vec3` instances. Applies the transformation
    :class:`~ezdxf.math.Matrix44` `m` to all vertices if not ``None``.

    Args:
        lump: :class:`Lump` entity
        m: optional transformation matrix

    Raises:
        TypeError: `lump` has invalid ACIS type

    expected a lump entity, got: Nzplane-surfacezstraight-curve)r   r   r   r   shellr.   r&   r   clearsurfaceloopcoedgeAttributeErroredgecurvesenser    
end_vertexpointlocationstart_vertexnext_coedgetupletransform_vertices	next_face)r,   r3   r8   verticesr&   first_coedger<   r>   s           r'   r1   r1   Q   s      dD!! FDT

DDEEEJE} H:Dl  <//#y/!   . 	;D:?&666< FOODO$9$BCCCCOOD$5$;$DEEE!     'F%%= 4 4X > >??????//)))- . 	. ~A l          s+   B 
BB2D AD 
D! D!   meshr
   	precisionintc                   |                      |          } t                      }t          | j                  }|j        j        sM|                     |j                    t          j                    }t          j        |j         |_
        ||_        |                                 D ]&} t          |           }|                    |           '|S )a#  Returns a :term:`ACIS` :class:`~ezdxf.acis.entities.Body` entity from a
    :class:`~ezdxf.render.MeshBuilder` instance.

    This entity can be assigned to a :class:`~ezdxf.entities.Solid3d` DXF entity
    as :term:`SAT` or :term:`SAB` data according to the version your DXF
    document uses (SAT for DXF R2000 to R2010 and SAB for DXF R2013 and later).

    If the `mesh` contains multiple separated meshes, each mesh will be a
    separated :class:`~ezdxf.acis.entities.Lump` node.
    If each mesh should get its own :class:`~ezdxf.acis.entities.Body` entity,
    separate the meshes beforehand by the method
    :meth:`~ezdxf.render.MeshBuilder.separate_meshes`.

    A closed mesh creates a solid body and an open mesh creates an open (hollow)
    shell. The detection if the mesh is open or closed is based on the edges
    of the mesh: if **all** edges of mesh have two adjacent faces the mesh is
    closed.

    The current implementation applies automatically a vertex optimization,
    which merges coincident vertices into a single vertex.

    )optimize_verticesr   r   rI   centeris_null	translater   	Transformr   r/   r-   separate_mesheslump_from_meshappend_lump)rL   rM   r   bboxr-   r,   s         r'   body_from_meshrY      s    . !!),,D66Dt}%%D; #|$$$&((	#-t{;	"$$&&  d##Kr)   c                    t                      }t                      }|                    |           t          |           }|                                D ]}|                    |           |S )zReturns a :class:`~ezdxf.acis.entities.Lump` entity from a
    :class:`~ezdxf.render.MeshBuilder` instance. The `mesh` has to be a single
    body or shell!
    )r   r   append_shellPolyhedronFaceBuilder
acis_facesappend_face)rL   r,   r8   face_builderr&   s        r'   rV   rV      sn    
 66DGGEe(..L''))    $Kr)   c                  L    e Zd ZddZd ZddZddZddZddZddZ	ddZ
dS ) r\   rL   r
   c                h   |                                 }|                                 |j        | _        |j        | _        t	          |                                          | _        g | _        |                                j	        | _
        t                      | _        t                      | _        d S N)copynormalize_facesrI   r%   r0   face_normalsnormalsacis_verticesdiagnoseis_edge_balance_brokendouble_sideddictpartner_coedgesedges)selfrL   	mesh_copys      r'   __init__zPolyhedronFaceBuilder.__init__   s    IIKK	!!###$-$6*3/
I22445546 &..00G HLvv;?66


r)   c                    t          t          | j                            | _        | j                                         | j                                         d S rb   )r0   make_verticesrI   rg   rl   r9   rm   )rn   s    r'   resetzPolyhedronFaceBuilder.reset   sK    !-">">??""$$$
r)   r   
list[Face]c                   |                                   g }t          | j        | j                  D ]\  }}|j        rt                      }|                     |          }|3||_        |                     |          }|R|	                    |           ||_
        d|_        | j        |_        |                    |           |S )NF)rs   zipr%   rf   rR   r   
make_planenormal	make_loopappend_loopr:   r@   rj   r    )rn   r%   r&   face_normal	acis_faceplaner;   s          r'   r]   z PolyhedronFaceBuilder.acis_faces   s    

!$TZ!>!> 	$ 	$D+" IOOD))E}&EL>>$''D|!!$''' %I#IO%)%6I"LL#### r)   r&   Sequence[int]Optional[entities.Plane]c                .   t          |          dk    s
J d            t          j                    }d|_        | j        |d                  |_        	 | j        |d                  |j        z
                                  |_        n# t          $ r Y d S w xY w|S )Nr   zface requires least 2 verticesFr   )	lenr   Plane	reverse_vrI   origin	normalizeu_dirZeroDivisionError)rn   r&   r}   s      r'   rw   z PolyhedronFaceBuilder.make_plane   s    4yy1}}}>}}}  }T!W-	=a1EL@KKMMEKK  	 	 	44	s   2B 
BBOptional[entities.Loop]c                   g }t          |dd                    }|d         |d         k    r|                    |d                    t          ||          D ]T\  }}|                     ||          }|                     |||          \  |_        |_        |                    |           Ut          j                    }|	                    |d           |S )Nr   r   T)close)
r0   r    rv   make_coedge	make_edger>   r@   r   Loopset_coedges)rn   r&   coedgesface2i1i2r<   r;   s           r'   ry   zPolyhedronFaceBuilder.make_loop   s    )+T!""X7d2hLLa!!!$&& 	# 	#FB%%b"--F(,r2v(F(F%FKNN6""""}---r)   index1rN   index2entities.Coedgec                    ||k    r||f}n||f}t          j                    }	 | j        |         }|                    |           n# t          $ r || j        |<   Y nw xY w|S rb   )r   Coedgerl   add_partner_coedgeKeyError)rn   r   r   keyr<   partner_coedges         r'   r   z!PolyhedronFaceBuilder.make_coedge  s    F??&.CC&.C""	6!1#6N --f5555  	/ 	/ 	/(.D %%%	/ s   A AAparenttuple[entities.Edge, bool]c                    d fd}d}|}|}||k    rd}||}}	  j         ||f         |fS # t          $ r Y nw xY wt          j                    |_        d_         ||          _        d_         ||          _         j	        |         
                     j	        |                   _                             ||          _         j         ||f<   |fS )NindexrN   c                h    j         |          }|xj        dz  c_        |j        j        r|_        |S )Nr   )rg   	ref_countr>   r.   )r   vertexr>   rn   s     r'   make_vertexz4PolyhedronFaceBuilder.make_edge.<locals>.make_vertex  s=    '.F!{" #"Mr)   FTg        )r   rN   )rm   r   r   Edger<   r@   rD   start_paramrA   rI   distance	end_parammake_rayr?   )	rn   r   r   r   r   r@   ex1ex2r>   s	   `       @r'   r   zPolyhedronFaceBuilder.make_edge  s"   	 	 	 	 	 	 	 99ECC	:c3h'.. 	 	 	D	 }
'K,,%+c**s+44T]35GHH]]3,,
#
38U{s   . 
;;entities.StraightCurvec                    | j         |         }| j         |         }t          j                    }||_        	 ||z
                                  |_        n# t          $ r t          |_        Y nw xY w|S rb   )rI   r   StraightCurver   r   	directionr   r   )rn   r   r   v1v2rays         r'   r   zPolyhedronFaceBuilder.make_ray=  sx    ]6"]6"$&&
	$"W//11CMM  	$ 	$ 	$#CMMM	$
s   A A,+A,N)rL   r
   )r   rt   )r&   r~   r   r   )r&   r~   r   r   )r   rN   r   rN   r   r   )r   rN   r   rN   r   r   r   r   )r   rN   r   rN   r   r   )__name__
__module____qualname__rp   rs   r]   rw   ry   r   r   r    r)   r'   r\   r\      s        B B B B.  
   .
 
 
 
             D	 	 	 	 	 	r)   r\   rI   Iterable[Vec3]Iterator[entities.Vertex]c              #     K   | D ]:}t          j                    }||_        t          j                    }||_        |V  ;d S rb   )r   PointrC   VertexrB   )rI   vrB   r   s       r'   rr   rr   I  sR          "" r)   
list[Vec3]c                   t          | t                    st          dt          |                      | j        }| j        }g }d}|j        s|j        }|j        s1|                    t          ||                     |j
        }|j        1|S )a  Returns all stored vertices in the given :class:`Body` entity.
    The result is not optimized, meaning the vertices are in no particular order and
    there are duplicates.

    This function can be useful to determining the approximate bounding box of an 
    :term:`ACIS` entity.  The result is exact for polyhedra with flat faces with 
    straight edges, but not for bodies with curved edges and faces.

    Args:
        body: ACIS entity of type :class:`Body`

    Raises:
        TypeError: given `body` entity has invalid type

    r   N)r   r   r   r   r,   r-   r.   r/   extendvertices_from_lumpr2   )r   r,   r-   rI   r3   s        r'   vertices_from_bodyr   R  s    " dD!! FDT

DDEEE9DIH A l *433444~ l  Or)   c                V   t          | t                    st          dt          |                      g }| j        }|j        r|S |j        }|j        st          }	 |j        j	        }n# t          $ r Y nw xY w|}|j        st|j        }	 |                    |j        j        j                   |                    |j        j        j                   n# t          $ r Y nw xY w|j        }||u rn|j        t|j        }|j        |"t'          |                    |                    S |S )aA  Returns all stored vertices from a given :class:`Lump` entity. 
    Applies the transformation :class:`~ezdxf.math.Matrix44` `m` to all vertices if not 
    ``None``.

    Args:
        lump: :class:`Lump` entity
        m: optional transformation matrix

    Raises:
        TypeError: `lump` has invalid ACIS type

    r7   )r   r   r   r   r8   r.   r&   r   r;   r<   r=   r>   r    rD   rB   rC   rA   rE   rH   r0   rG   )r,   r3   rI   r8   r&   rJ   r<   r>   s           r'   r   r   r  sl    dD!! FDT

DDEEEHJE} :Dl 	9+LL 	 	 	D	. 	;D 1 7 @AAA 5 >????!     'F%% . 	 ~) l * 	}A((22333Os%   A* *
A76A7AC 
C! C!)T)r   r   r   r   )r   r   r   r*   rb   )r,   r   r3   r4   r   r5   )rK   )rL   r
   rM   rN   r   r   )rL   r
   r   r   )rI   r   r   r   )r   r   r   r   )r,   r   r3   r4   r   r   ) 
__future__r   typingr   r   r   r   ezdxf.renderr   r	   r
   
ezdxf.mathr   r   r   r    r   r   r   r   r   r   r(   r   r1   rY   rV   r\   rr   r   r   r   r)   r'   <module>r      s   # " " " " " 9 9 9 9 9 9 9 9 9 9 9 9 G G G G G G G G G G ; ; ; ; ; ; ; ; ; ; ; ;       7 7 7 7 7 7 7 7 7 7 7 7 7 7' ' ' ' 'T   : &*7 7 7 7 7t# # # # #L   G G G G G G G GT      @- - - - - - -r)   