
    h/;                       S SK Jr  S SKJrJrJrJr  S SKJrJ	r	J
r
  S SKJrJrJrJr  SSKJr  SSKJrJrJrJrJr  SSS jjr    SS	 jr S     SS jjrSSS jjrSS jr " S S5      rSS jrSS jrSSS jjrg
)    )annotations)IteratorSequenceOptionalIterable)MeshVertexMergerMeshTransformerMeshBuilder)Matrix44Vec3NULLVECBoundingBox   )entities)BodyLumpNONE_REFFaceShellc                   [        U [        5      (       d  [        S[        U 5       35      e/ n[	        5       n[        U 5       HU  nU H  nUR                  U5        M     U(       a  M&  UR                  [        R                  " U5      5        [	        5       nMW     U(       a%  UR                  [        R                  " U5      5        U$ )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)bodymerge_lumpsmeshesbuilderfacesfaces         A/var/www/html/env/lib/python3.13/site-packages/ezdxf/acis/mesh.pymesh_from_bodyr&      s    4 dD!!7T
|DEE$&F G-d3DT" {MM/66w?@&(G 4 o227;<M    c              #  `  #    [        U [        5      (       d  [        S[        U 5       35      eU R                  nU R
                  nSnUR                  (       d  UR                  nUR                  (       d6  [        [        X5      5      v   UR                  nUR                  (       d  M5  gg7f)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!!7T
|DEE99DI All/899~~ llls   B(B.,B.Nc              #    #    [        U [        5      (       d  [        S[        U 5       35      eU R                  nUR
                  (       a  g/ nUR                  nUR
                  (       Gde  [        nUR                  5         UR                  R                  S:X  a   UR                  R                  nUnUR
                  (       d  UR                  n UR                  R                  S:w  a  OUR                  (       a0  UR!                  UR"                  R$                  R&                  5        O0UR!                  UR(                  R$                  R&                  5         UR*                  nXeL a.  Ub  [-        UR/                  U5      5      v   O[-        U5      v   OUR
                  (       d  M  UR0                  nUR
                  (       d  GMd  gg! [         a     GN,f = f! [         a     M@  f = f7f)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)   r0   r3   verticesr$   first_coedger7   r9   s           r%   r.   r.   Q   s     dD!!7T
|DEEJJE}}H::Dlll<</#yy// ..;;D::??&66<<OODOO$9$9$B$BCOOD$5$5$;$;$D$DE
 ''F%= 4 4X >??/)- .... ~~A lll "  "  st   BG5G 4G5G$ .G5/A G$ /G50/G$ AG51G5G5
G!G5 G!!G5$
G2.G51G22G5c                   U R                  U5      n [        5       n[        U R                  5      nUR                  R
                  (       dY  U R                  UR                  * 5        [        R                  " 5       n[        R                  " UR                  6 Ul
        XBl        U R                  5        H  n [        U 5      nUR                  U5        M!     U$ )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   rD   centeris_null	translater   	Transformr   r,   r*   separate_mesheslump_from_meshappend_lump)mesh	precisionr   bboxr*   r)   s         r%   body_from_meshrR      s    . !!),D6Dt}}%D;;|$&&(	#--t{{;	"$$&d# ' Kr'   c                    [        5       n[        5       nUR                  U5        [        U 5      nUR	                  5        H  nUR                  U5        M     U$ )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)rO   r)   r3   face_builderr$   s        r%   rM   rM      sP    
 6DGEe(.L'')$ *Kr'   c                  p    \ rS rSrSS jrS rSS jrSS jrSS jrSS jr	        SS jr
SS	 jrS
rg)rU      c                L   UR                  5       nUR                  5         UR                  U l        UR                  U l        [	        UR                  5       5      U l        / U l        UR                  5       R                  U l
        [        5       U l        [        5       U l        g N)copynormalize_facesrD   r#   r-   face_normalsnormalsacis_verticesdiagnoseis_edge_balance_brokendouble_sideddictpartner_coedgesedges)selfrO   	mesh_copys      r%   __init__PolyhedronFaceBuilder.__init__   s~    IIK	!!#$-$6$6*3//
I224546 &..0GG HLv;?6
r'   c                    [        [        U R                  5      5      U l        U R                  R                  5         U R                  R                  5         g r\   )r-   make_verticesrD   ra   rf   r4   rg   )rh   s    r%   resetPolyhedronFaceBuilder.reset   s:    !-">?""$

r'   c                   U R                  5         / n[        U R                  U R                  5       H  u  p#UR                  (       a  M  [        5       nU R                  U5      nUc  M8  X5l        U R                  U5      nUc  MT  UR                  U5        XTl
        SUl        U R                  Ul        UR                  U5        M     U$ )NF)rn   zipr#   r`   rI   r   
make_planenormal	make_loopappend_loopr5   r;   rd   r   )rh   r#   r$   face_normal	acis_faceplaner6   s          r%   rV    PolyhedronFaceBuilder.acis_faces   s    

!$TZZ!>D""IOOD)E}&L>>$'D|!!$' %#IO%)%6%6I"LL# "?$ r'   c                   [        U5      S:  d   S5       e[        R                  " 5       nSUl        U R                  US      Ul         U R                  US      UR
                  -
  R                  5       Ul        U$ ! [         a     g f = f)Nr   zface requires least 2 verticesFr   )	lenr   Plane	reverse_vrD   origin	normalizeu_dirZeroDivisionError)rh   r$   rx   s      r%   rr    PolyhedronFaceBuilder.make_plane   s    4y1}>>>} }}T!W-	==a1ELL@KKMEK  ! 		s   2A? ?
BBc                Z   / n[        USS  5      nUS   US   :w  a  UR                  US   5        [        X5       HF  u  pEU R                  XE5      nU R	                  XEU5      u  Ul        Ul        UR                  U5        MH     [        R                  " 5       nUR                  USS9  U$ )Nr   r   T)close)
r-   r   rq   make_coedge	make_edger9   r;   r   Loopset_coedges)rh   r$   coedgesface2i1i2r7   r6   s           r%   rt   PolyhedronFaceBuilder.make_loop   s    )+T!"X7d2hLLa!$&FB%%b-F(,rv(F%FKNN6" ' }}-r'   c                    X:  a  X!4nOX4n[         R                  " 5       n U R                  U   nUR                  U5        U$ ! [         a    X@R                  U'    U$ f = fr\   )r   Coedgerf   add_partner_coedgeKeyError)rh   index1index2keyr7   partner_coedges         r%   r   !PolyhedronFaceBuilder.make_coedge  sq    ?.C.C"	6!11#6N --f5	  	/(.  % 		/s   A A! A!c                  ^ ^ SUU 4S jjnSnUnUnXg:  a  SnXvpv T R                   Xg4   U4$ ! [         a     Of = f[        R                  " 5       mUTl        STl        U" U5      Tl        STl        U" U5      Tl        T R                  U   R                  T R                  U   5      Tl        T R                  Xg5      Tl        TT R                   Xg4'   TU4$ )Nc                   > TR                   U    nU=R                  S-  sl        UR                  R                  (       a  TUl        U$ )Nr   )ra   	ref_countr9   r+   )indexvertexr9   rh   s     r%   make_vertex4PolyhedronFaceBuilder.make_edge.<locals>.make_vertex  s=    ''.F!{{"""Mr'   FTg        )r   int)rg   r   r   Edger7   r;   r?   start_paramr<   rD   distance	end_parammake_rayr:   )	rh   r   r   parentr   r;   ex1ex2r9   s	   `       @r%   r   PolyhedronFaceBuilder.make_edge  s    	 	 9E	::ch'.. 		 }}
',%c*s+44T]]35GH]]3,
#

38U{s   . 
;;c                    U R                   U   nU R                   U   n[        R                  " 5       nX5l         XC-
  R	                  5       Ul        U$ ! [         a    [        Ul         U$ f = fr\   )rD   r   StraightCurver~   r   	directionr   r   )rh   r   r   v1v2rays         r%   r   PolyhedronFaceBuilder.make_ray=  sl    ]]6"]]6"$$&
	$W//1CM 
 ! 	$#CM
	$s   A A-,A-)ra   rd   rg   r#   r`   rf   rD   N)rO   r
   )returnz
list[Face])r$   Sequence[int]r   zOptional[entities.Plane])r$   r   r   zOptional[entities.Loop])r   r   r   r   r   entities.Coedge)r   r   r   r   r   r   r   ztuple[entities.Edge, bool])r   r   r   r   r   zentities.StraightCurve)__name__
__module____qualname____firstlineno__rj   rn   rV   rr   rt   r   r   r   __static_attributes__ r'   r%   rU   rU      sL    B.
.
  #& 0? 	# D	r'   rU   c              #     #    U  H=  n[         R                  " 5       nXl        [         R                  " 5       nX#l        Uv   M?     g 7fr\   )r   Pointr>   Vertexr=   )rD   vr=   r   s       r%   rm   rm   I  s6      " s   AAc                d   [        U [        5      (       d  [        S[        U 5       35      eU R                  nU R
                  n/ nSnUR                  (       d  UR                  nUR                  (       d9  UR                  [        X5      5        UR                  nUR                  (       d  M9  U$ )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_lumpr/   )r   r)   r*   rD   r0   s        r%   vertices_from_bodyr   R  s    " dD!!7T
|DEE99DIH All*434~~ lll Or'   c                   [        U [        5      (       d  [        S[        U 5       35      e/ nU R                  nUR
                  (       a  U$ UR                  nUR
                  (       d  [        n UR                  R                  nUnUR
                  (       d  UR                  n UR                  UR                  R                  R                  5        UR                  UR                   R                  R                  5        UR"                  nXeL a  OUR
                  (       d  M  UR$                  nUR
                  (       d  M  Ub  ['        UR)                  U5      5      $ U$ ! [         a     Nf = f! [         a     M[  f = f)a!  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

r2   )r   r   r   r   r3   r+   r$   r   r6   r7   r8   r9   r   r?   r=   r>   r<   r@   rC   r-   rB   )r)   r0   rD   r3   r$   rE   r7   r9   s           r%   r   r   r  sC    dD!!7T
|DEEHJJE}}::Dll	99++L ..;;D 1 1 7 7 @ @A 5 5 > >?
 ''F% ... ~~) lll* 	}A((233O'  		 "  s%   2E( (AE8 (
E54E58
FF)T)r   r   r   zlist[MeshTransformer])r   r   r   zIterator[list[Sequence[Vec3]]]r\   )r)   r   r0   Matrix44 | Noner   zIterator[Sequence[Vec3]])   )rO   r
   rP   r   r   r   )rO   r
   r   r   )rD   zIterable[Vec3]r   zIterator[entities.Vertex])r   r   r   
list[Vec3])r)   r   r0   r   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   r.   rR   rM   rU   rm   r   r   r   r'   r%   <module>r      s    # 9 9 G G ; ;  7 7'T
#: &*7
7"77t#LG GT@-r'   