mò
"®åAc           @   s^   d  Z  d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d Z d	 Z	 d
 „  Z
 d S(   sG   Utilities for translating between dxf and native cadvas (.pkl) format.
c         C   s.   |  i ƒ  i ƒ  } |  i ƒ  i ƒ  } | | f S(   s:   Parse a pair of lines containing 'group code' and 'value'.N(   t   at   nextt   stript	   groupcodet   value(   R    R   R   (    (    t!   /home/fenn/3d/cadvas-0.2.2/dxf.pyt
   _parsepair   s     c         C   s@   x9 t  |  ƒ \ } } | d j o | | j o d Sq q Wd S(   s   Go to secname and stop.i   t   2N(   R   R    t   gct   valt   secname(   R    R
   R   R	   (    (    R   t   _gotosection   s       c         C   s    t  |  d ƒ d } x† t |  ƒ \ } } | d j o@ | d j o3 t |  ƒ \ } } | d j o t | ƒ } q— q | d j o | d j o | Sq q Wd	 S(
   s^   Parse through HEADER section and detect whether units
    are set for metric(1) or english(0).t   HEADERi    i   t   9s   $MEASUREMENTt   70t   0t   ENDSECN(   R   R    t   unitsR   R   R	   t   int(   R    R	   R   R   (    (    R   t
   _get_units   s       c         C   si   h  } d } xV t |  ƒ \ } } | d j o( | d j o
 d } n | | | f Sq | | | <q Wd S(   st   Return a dictionary of groupcodes : values for the next
    entity in the ENTITIES section. Go until groupcode == 0.i   R   R   i    N(   t
   entitydictt   flagR   R    R   R	   (   R    R   R	   R   R   (    (    R   t   _process_entity&   s       
c   
      C   s-  t  t |  ƒ ƒ } t | ƒ }	 t | d ƒ g  } g  } g  } | | | g } t | ƒ \ } } xÈ | d j o& t | ƒ \ } }  } | i | ƒ n| | d j o& t | ƒ \ } }  } | i | ƒ nI | d j o& t | ƒ \ } }  } | i | ƒ n t | ƒ \ } }  } |  p |	 | f Sqa qa Wd S(   sS   Parse contents of the dxf file, looking for units and
    all the drawing entities.t   ENTITIESi   t   LINEt   CIRCLEt   ARCN(   t   itert   opent   fR    R   R   R   t   linest   circlest   arcst   entitiesR   R   R	   R   t   edt   append(
   R   R    R   R	   R    R"   R   R!   R   R   (    (    R   t   _parse_file4   s.       c         C   sê  t  |  ƒ \ } } | \ } } }	 | o
 d } n d } h  } h  } h  } d } x~ | D]v } t | d ƒ | t | d ƒ | f } t | d ƒ | t | d ƒ | f } | | f } | d 7} | | | <qW Wd } xj | D]b } t | d ƒ | t | d ƒ | f } t | d	 ƒ | } | | f } | d 7} | | | <qÞ Wd } x |	 D]ˆ } t | d ƒ | t | d ƒ | f } t | d	 ƒ | } t | d
 ƒ } t | d ƒ }
 | | | |
 f } | d 7} | | | <qQW| | | g S(   s2   Generate cadvas native CAD data from dxf entities.f1.0f25.399999999999999i    t   10t   20t   11t   21i   t   40t   50t   51N(   R$   R   R   R!   R   R   R    t   scalet   gldictt   gcdictt   gadictt   kt   linet   floatt   p1t   p2t   coordst   circt   cntrt   radiust   arct   a0t   a1(   R   R9   R8   R,   R.   R6   R!   R   R   R    R;   R:   R7   R1   R-   R4   R3   R0   R   R/   R5   (    (    R   t   _gen_cadvas_entitiesN   sH     
 ((
 (
 (
c         C   s=   h  } t |  ƒ \ } } } | | d <| | d <| | d <| S(   Nt   glR   t   ga(   t   drawdictR<   t   infR-   R.   R/   (   R@   R.   R/   R?   R-   (    (    R   t
   dxf2nativev   s    


sØ  999
DXF file created from CADvas
0
SECTION
2
HEADER
9
$ACADVER
1
AC1006
9
$INSBASE
10
0.0
20
0.0
30
0.0
9
$EXTMIN
10
0.0
20
0.0
9
$EXTMAX
10
1000.0
20
1000.0
0
ENDSEC
0
SECTION
2
TABLES
0
TABLE
2
LTYPE
70
1
0
LTYPE
2
CONTINUOUS
70
64
3
Solid line
72
65
73
0
40
0.000000
0
ENDTAB
0
TABLE
2
LAYER
70
6
0
LAYER
2
1
70
64
62
7
6
CONTINUOUS
0
LAYER
2
2
70
64
62
7
6
CONTINUOUS
0
ENDTAB
0
TABLE
2
STYLE
70
0
0
ENDTAB
0
ENDSEC
0
SECTION
2
BLOCKS
0
ENDSEC
  0
SECTION
  2
ENTITIESs   
  0
ENDSEC
  0
EOFc         C   sU  d } |  d } |  d } |  d }	 d } xT | i ƒ  D]F \ } }
 | d | d | | d | |
 d | |
 d | f } q7 WxE | i ƒ  D]7 \ } } | d	 | d | | d | | | f } qŽ WxQ |	 i ƒ  D]C \ } } } } | d
 | d | | d | | | | | f } qÖ Wt | t } t | d ƒ } | i | ƒ | i ƒ  d  S(   Nf25.399999999999999R=   R   R>   t    s+   
  0
LINE
  8
0
 10
%f
 20
%f
 11
%f
 21
%fi    i   s&   
  0
CIRCLE
  8
0
 10
%f
 20
%f
 40
%fs1   
  0
ARC
  8
0
 10
%f
 20
%f
 40
%f
 50
%f
 51
%ft   w(   R,   R?   R-   R.   R/   t
   entitytextt   valuest   p0R3   t   pct   rR:   R;   t	   template1t	   template2t   dxftextR   t   dxfR   t   writet   close(   R?   RL   RF   R,   R.   R:   R   R;   RG   R/   R3   RK   RH   RD   R-   (    (    R   t
   native2dxfõ   s$    


 > / 5N(   t   __doc__R   R   R   R   R$   R<   RA   RI   RJ   RO   (
   RJ   RI   R$   R   R   R   RO   R   R<   RA   (    (    R   t   ?   s   						(	k
