Skip to content
Snippets Groups Projects
io_import_scene_dxf.py 75.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • Remigiusz Fiedler's avatar
    Remigiusz Fiedler committed
            print("VERTS")
            for v in self.verts:
                print(v.location)
            print("END VERTS")
    
        def build(self, vn=0):
            verts = []
            lines = []
            v_start = vn
            for vert in self.verts:
                verts.append(vert.location)
                lines.append((vn, vn+1))
                vn += 1
            if self.flags & PL_CLOSED:
                lines[-1] = (vn-1, v_start)
            else:
                lines.pop()
            if self.normal!=Vector((0,0,1)):
                ma = getOCS(self.normal)
                if ma:
    
                    verts = [ma * v for v in verts]
    
    Remigiusz Fiedler's avatar
    Remigiusz Fiedler committed
            return((verts, lines, [], vn-1))
    
    #
    #    class CShape(CEntity):
    #    2 : 'name', 
    #    10 : 'insertion_point.x', 20 : 'insertion_point.y', 30 : 'insertion_point.z', 
    #    39 : 'thickness',
    #    40 : 'size', 41 : 'x_scale', 
    #    50 : 'rotation_angle', 51 : 'oblique_angle',     
    #
    
    class CShape(CEntity):
        def __init__(self):
            CEntity.__init__(self, 'SHAPE', None)
            self.name = ""
            self.insertion_point = Vector()
            self.thickness = 0.0
            self.size = 1.0
            self.x_scale = 1.0
            self.rotation_angle = 0.0
            self.oblique_angle = 0.0
    
        def display(self):
            CEntity.display(self)
            print("%s" % (self.name))
            print(self.insertion_point)
    
    #
    #    class CSpline(CEntity):
    #    10 : ['new_control_point(data)'], 20 : 'control_point.y', 30 : 'control_point.z', 
    #    11 : ['new_fit_point(data)'], 21 : 'fit_point.y', 31 : 'fit_point.z', 
    #    40 : ['new_knot_value(data)'], 
    #    12 : 'start_tangent.x', 22 : 'start_tangent.y', 32 : 'start_tangent.z', 
    #    13 : 'end_tangent.x', 23 : 'end_tangent.y', 33 : 'end_tangent.z', 
    #    41 : 'weight', 42 : 'knot_tol', 43 : 'control_point_tol', 44 : 'fit_tol',
    #    70 : 'flag', 71 : 'degree', 
    #    72 : 'num_knots', 73 : 'num_control_points', 74 : 'num_fit_points',
    #    210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
    #
    
    class CSpline(CEntity):
        def __init__(self):
            CEntity.__init__(self, 'SPLINE', 'Mesh')
            self.control_points = []
            self.fit_points = []
            self.knot_values = []
            self.control_point = None
            self.fit_point = None
            self.knot_value = None
            self.start_tangent = Vector()
            self.end_tangent = Vector()
            self.weight = 1.0
            self.knot_tol = 1e-6
            self.control_point_tol = 1e-6
            self.fit_tol = 1e-6
            self.flag = 0
            self.degree = 3
            self.num_knots = 0
            self.num_control_points = 0
            self.num_fit_points = 0
            self.thickness = 0.0
            self.normal = Vector((0,0,1))
            
        def new_control_point(self, data):
            self.control_point = Vector()
            self.control_point.x = data
            self.control_points.append(self.control_point)
            
        def new_fit_point(self, data):
            self.fit_point = Vector()
            self.fit_point.x = data
            self.fit_points.append(self.fit_point)
    
        def new_knot_value(self, data):
            self.knot_value = data
            self.knot_values.append(self.knot_value)
            
        def display(self):
            #not testet yet (migius)
            CEntity.display(self)
            print("CONTROL")
            for p in self.control_points:
                print(p)
            print("FIT")
            for p in self.fit_points:
                print(p)
            print("KNOT")
            for v in self.knot_values:
                print(v)
    
        def build(self, vn=0):
            verts = []
            lines = []
            for vert in self.control_points:
                verts.append(vert)
                lines.append((vn, vn+1))
                vn += 1
            lines.pop()
            return((verts, lines, [], vn))
    
    
    #
    #    class CSolid(CEntity):
    #    10 : 'point0.x', 20 : 'point0.y', 30 : 'point0.z', 
    #    11 : 'point1.x', 21 : 'point1.y', 31 : 'point1.z', 
    #    12 : 'point2.x', 22 : 'point2.y', 32 : 'point2.z', 
    #    13 : 'point3.x', 23 : 'point3.y', 33 : 'point3.z', 
    #    39 : 'thickness',
    #
    
    class CSolid(CEntity):
        def __init__(self):
            CEntity.__init__(self, 'SOLID', 'Mesh')
            self.point0 = Vector()
            self.point1 = Vector()
            self.point2 = Vector()
            self.point3 = Vector()
            self.normal = Vector((0,0,1))
            self.thickness = 0.0
            
        def display(self):
            CEntity.display(self)
            print(self.point0)
            print(self.point1)
            print(self.point2)
            print(self.point3)
    
        def build(self, vn=0):
            points, edges, faces = [],[],[]
            if self.point2 == self.point3:
                points = [self.point0, self.point1, self.point2]
            else:
                points = [self.point0, self.point1, self.point2, self.point3]
            pn = len(points)
            v0 = vn
            
            thic = self.thickness
            t_vector = Vector((0, 0, thic))
            if thic != 0 and (toggle & T_ThicON):
                thic_points = [v + t_vector for v in points]
                if thic < 0.0:
                    thic_points.extend(points)
                    points = thic_points
                else:
                    points.extend(thic_points)
    
                if   pn == 4:
                    faces = [[0,1,3,2], [4,6,7,5], [0,4,5,1],
                             [1,5,7,3], [3,7,6,2], [2,6,4,0]]
                elif pn == 3:
                    faces = [[0,1,2], [3,5,4], [0,3,4,1], [1,4,5,2], [2,5,3,0]]
                elif pn == 2: faces = [[0,1,3,2]]
                vn += 2*pn
            else:
                if   pn == 4: faces = [[0,2,3,1]]
                elif pn == 3: faces = [[0,2,1]]
                elif pn == 2:
                    edges = [[0,1]]
                    self.drawtype = 'Mesh'
                vn += pn
            if self.normal!=Vector((0,0,1)):
                ma = getOCS(self.normal)
                if ma:
    
                    points = [ma * v for v in points]
    
    Remigiusz Fiedler's avatar
    Remigiusz Fiedler committed
            return((points, edges, faces, vn))
            
    #
    #    class CText(CEntity):
    #    1 : 'text', 7 : 'style',
    #    10 : 'insertion_point.x', 20 : 'insertion_point.y', 30 : 'insertion_point.z', 
    #    11 : 'alignment_point.x', 21 : 'alignment_point.y', 31 : 'alignment_point.z', 
    #    40 : 'height', 41 : 'x_scale', 
    #    50 : 'rotation_angle', 51 : 'oblique_angle', 
    #    71 : 'flags', 72 : 'horizontal_justification',  73 : 'vertical_justification',    
    #
    
    class CText(CEntity):
        def __init__(self):
            CEntity.__init__(self, 'TEXT', 'Text')
            self.text = ""
            self.style = ""
            self.insertion_point = Vector()
            self.alignment_point = Vector()
            self.height = 1.0
            self.x_scale = 1.0
            self.rotation_angle = 0.0
            self.oblique_angle = 0.0
            self.flags = 0
            self.horizontal_justification = 0.0
            self.vertical_justification = 0.0
            self.thickness = 0.0
            self.normal = Vector((0,0,1))
           
        def display(self):
            CEntity.display(self)
            print("%s %s" % (self.text, self.style))
            print(self.insertion_point)
            print(self.alignment_point)
            
        def draw(self):
            drawText(self.text,  self.insertion_point, self.height, self.x_scale, self.rotation_angle, self.oblique_angle, self.normal)
            return
    
    
    def drawText(text, loc, size, spacing, angle, shear, normal=Vector((0,0,1))):
    
        #print('angle_deg=',angle)
        bpy.ops.object.text_add(
            view_align=False, 
            enter_editmode=False, 
            location= loc, 
            #rotation=(0, 0, angle), #need radians here
            )
        cu = bpy.context.object.data
        cu.body = text
        cu.size = size #up 2.56
        cu.space_word = spacing #up 2.56
        cu.shear = shear
        if angle!=0.0 or normal!=Vector((0,0,1)):
            obj = bpy.context.object
            transform(normal, angle, obj)
        return
    
    Remigiusz Fiedler's avatar
    Remigiusz Fiedler committed
    
    #
    #    class CTolerance(CEntity):
    #    3 : 'style',
    #    10 : 'insertion_point.x', 20 : 'insertion_point.y', 30 : 'insertion_point.z', 
    #    11 : 'direction.x', 21 : 'direction.y', 31 : 'direction.z', 
    #
    
    class CTolerance(CEntity):
        def __init__(self):
            CEntity.__init__(self, 'TOLERANCE', None)
            self.stype = ""
            self.insertion_point = Vector()
            self.direction = Vector()
    
    #
    #    class CTrace(CEntity):
    #    10 : 'point0.x', 20 : 'point0.y', 30 : 'point0.z', 
    #    11 : 'point1.x', 21 : 'point1.y', 31 : 'point1.z', 
    #    12 : 'point2.x', 22 : 'point2.y', 32 : 'point2.z', 
    #    13 : 'point3.x', 23 : 'point3.y', 33 : 'point3.z', 
    #    39 : 'thickness',
    #
    
    class CTrace(CEntity):
        def __init__(self):
            CEntity.__init__(self, 'TRACE', 'Mesh')
            self.point0 = Vector()
            self.point1 = Vector()
            self.point2 = Vector()
            self.point3 = Vector()
            self.normal = Vector((0,0,1))
            self.thickness = 0.0
        
        def display(self):
            CEntity.display(self)
            print(self.point0)
            print(self.point1)
            print(self.point2)
            print(self.point3)
       
        def build(self, vn=0):
            points, edges, faces = [],[],[]
            if self.point2 == self.point3:
                points = [self.point0, self.point2, self.point1]
            else:
                points = [self.point0, self.point2, self.point1, self.point3]
            pn = len(points)
            v0 = vn
            thic = self.thickness
            t_vector = Vector((0, 0, thic))
            if thic != 0 and (toggle & T_ThicON):
                thic_points = [v + t_vector for v in points]
                if thic < 0.0:
                    thic_points.extend(points)
                    points = thic_points
                else:
                    points.extend(thic_points)
    
                if   pn == 4:
                    faces = [[0,1,3,2], [4,6,7,5], [0,4,5,1],
                             [1,5,7,3], [3,7,6,2], [2,6,4,0]]
                elif pn == 3:
                    faces = [[0,1,2], [3,5,4], [0,3,4,1], [1,4,5,2], [2,5,3,0]]
                elif pn == 2: faces = [[0,1,3,2]]
                vn += 2*pn
            else:
                if   pn == 4: faces = [[0,2,3,1]]
                elif pn == 3: faces = [[0,2,1]]
                elif pn == 2:
                    edges = [[0,1]]
                    self.drawtype = 'Mesh'
            if self.normal!=Vector((0,0,1)):
                ma = getOCS(self.normal)
                if ma:
    
                    points = [ma * v for v in points]
    
    Remigiusz Fiedler's avatar
    Remigiusz Fiedler committed
            return ((points, edges, faces, vn))
    
    #
    #    class CVertex(CEntity):
    #    10 : 'location.x', 20 : 'location.y', 30 : 'location.z', 
    #    40 : 'start_width', 41 : 'end_width', 42 : 'bulge', 
    #    50 : 'tangent',
    #    70 : 'flags',
    #    71 : 'index1', 72 : 'index2', 73 : 'index3', 74 : 'index4', 
    #
    
    class CVertex(CEntity):
        def __init__(self):
            CEntity.__init__(self, 'VERTEX', None)
            self.location = Vector()
            self.start_width = 0.0
            self.end_width = 0.0
            self.bulge = 0.0
            self.tangent = 0.0
            self.flags = 0
    
        def display(self):
            return
    
        def draw(self):
            return
    
    #            
    #    class CViewPort(CEntity):
    #    10 : 'center.x', 20 : 'center.y', 30 : 'center.z', 
    #    12 : 'view_center.x', 22 : 'view_center.y', 32 : 'view_center.z', 
    #    13 : 'snap_base.x', 23 : 'snap_base.y', 33 : 'snap_base.z', 
    #    14 : 'snap_spacing.x', 24 : 'snap_spacing.y', 34 : 'snap_spacing.z', 
    #    15 : 'grid_spacing.x', 25 : 'grid_spacing.y', 35 : 'grid_spacing.z', 
    #    16 : 'view_direction.x', 26 : 'view_direction.y', 36 : 'view_direction.z', 
    #    40 : 'width', 41 : 'height',
    #    68 : 'status', 69 : 'id',
    #
    
    class CViewPort(CEntity):
        def __init__(self):
            CEntity.__init__(self, 'VIEWPORT', None)
            self.center = Vector()
            self.view_center = Vector()
            self.snap_base = Vector()
            self.snap_spacing = Vector()
            self.grid_spacing = Vector()
            self.view_direction = Vector()
            self.width = 1.0
            self.height = 1.0
            self.status = 0
            self.id = 0
    
        def draw(self):
            # Todo
            return
    
    #
    #    class CWipeOut(CEntity):
    #    10 : 'point.x', 20 : 'point.y', 30 : 'point.z', 
    #    11 : 'direction.x', 21 : 'direction.y', 31 : 'direction.z', 
    #
    
    class CWipeOut(CEntity):
        def __init__(self):
            CEntity.__init__(self, 'WIPEOUT', None)
            self.point = Vector()
            self.direction = Vector()
    
    #
    #
    #
    WORLDX = Vector((1.0,0.0,0.0))
    WORLDY = Vector((0.0,1.0,0.0))
    WORLDZ = Vector((0.0,0.0,1.0))
    
    
    def getOCS(az):  #-----------------------------------------------------------------
        """An implimentation of the Arbitrary Axis Algorithm.
        """
        #decide if we need to transform our coords
        #if az[0] == 0 and az[1] == 0: 
        if abs(az.x) < 0.00001 and abs(az.y) < 0.00001:
            if az.z > 0.0:
                return False
            elif az.z < 0.0:
    
                return Matrix((-WORLDX, WORLDY*1, -WORLDZ)).transposed()
    
    Remigiusz Fiedler's avatar
    Remigiusz Fiedler committed
    
        cap = 0.015625 # square polar cap value (1/64.0)
        if abs(az.x) < cap and abs(az.y) < cap:
            ax = WORLDY.cross(az)
        else:
            ax = WORLDZ.cross(az)
    
    Remigiusz Fiedler's avatar
    Remigiusz Fiedler committed
        ay = az.cross(ax)
    
        # Matrices are now constructed from rows, transpose to make the rows into cols
        return Matrix((ax, ay, az)).transposed()
    
    Remigiusz Fiedler's avatar
    Remigiusz Fiedler committed
    
    
    
    def transform(normal, rotation, obj):  #--------------------------------------------
        """Use the calculated ocs to determine the objects location/orientation in space.
        """
    
    Remigiusz Fiedler's avatar
    Remigiusz Fiedler committed
        o = Vector(obj.location)
    
            ma_new.resize_4x4()
            ma = ma_new
            o = ma * o
    
    Remigiusz Fiedler's avatar
    Remigiusz Fiedler committed
    
        if rotation != 0:
    
            rmat = Matrix.Rotation(radians(rotation), 4, 'Z')
            ma = ma * rmat
    
    Remigiusz Fiedler's avatar
    Remigiusz Fiedler committed
    
    
    Remigiusz Fiedler's avatar
    Remigiusz Fiedler committed
    1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
        obj.location = o
    
    
    DxfEntityAttributes = {
    '3DFACE'    : {
        10 : 'point0.x', 20 : 'point0.y', 30 : 'point0.z', 
        11 : 'point1.x', 21 : 'point1.y', 31 : 'point1.z', 
        12 : 'point2.x', 22 : 'point2.y', 32 : 'point2.z', 
        13 : 'point3.x', 23 : 'point3.y', 33 : 'point3.z', 
        70 : 'flags',
        },
    
    '3DSOLID'    : {
        1 : 'data', 3 : 'more', 70 : 'version',
        },
    
    'ACAD_PROXY_ENTITY'    : {
        70 : 'format',
        90 : 'id', 91 : 'class', 92 : 'graphics_size', 93 : 'entity_size', 95: 'format',
        310 : 'data', 330 : 'id1', 340 : 'id2', 350 : 'id3', 360 : 'id4', 
        },
    
    'ARC'        : {
        10 : 'center.x', 20 : 'center.y', 30 : 'center.z', 
        40 : 'radius',
        50 : 'start_angle', 51 : 'end_angle',
        39 : 'thickness',
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
    
    'ARCALIGNEDTEXT'    : {
        1 : 'text', 2 : 'font', 3 : 'bigfont', 7 : 'style',
        10 : 'center.x', 20 : 'center.y', 30 : 'center.z', 
        40 : 'radius', 41 : 'width', 42 : 'height', 43 : 'spacing', 
        44 : 'offset', 45 : 'right_offset', 46 : 'left_offset', 
        50 : 'start_angle', 51 : 'end_angle',
        70 : 'order', 71 : 'direction', 72 : 'alignment', 73 : 'side', 
        74 : 'bold', 75 : 'italic', 76 : 'underline',
        77 : 'character_set', 78 : 'pitch', 79 : 'fonttype',
        90 : 'color',
        280 : 'wizard', 330 : 'id'
        },
    
    'ATTDEF'    : {
        1 : 'text', 2 : 'tag', 3 : 'prompt', 7 : 'style',
        10 : 'insertion_point.x', 20 : 'insertion_point.y', 30 : 'insertion_point.z', 
        11 : 'alignment_point.x', 21 : 'alignment_point.y', 31 : 'alignment_point.z', 
        40 : 'height', 41 : 'x_scale', 
        50 : 'rotation_angle', 51 : 'oblique_angle', 
        70 : 'flags', 71 : 'text_generation_flags', 
        72 : 'horizontal_justification',  74 : 'vertical_justification',    
        },
    
    
    'ATTRIB'    : {
        1 : 'text', 2 : 'tag', 3 : 'prompt', 7 : 'style',
        10 : 'insertion_point.x', 20 : 'insertion_point.y', 30 : 'insertion_point.z', 
        11 : 'alignment_point.x', 21 : 'alignment_point.y', 31 : 'alignment_point.z', 
        40 : 'height', 41 : 'x_scale', 
        50 : 'rotation_angle', 51 : 'oblique_angle', 
        70 : 'flags', 73 : 'length', 
        71 : 'text_generation_flags', 72 : 'horizontal_justification',  74 : 'vertical_justification',     
        },
    
    'BLOCK'        : {
        1 : 'xref', 2 : 'name', 3 : 'also_name', 
        10 : 'base_point.x', 20 : 'base_point.y', 30 : 'base_point.z', 
        40 : 'size', 41 : 'x_scale', 
        50 : 'rotation_angle', 51 : 'oblique_angle',     
        70 : 'flags', 
        },
    
    'CIRCLE'    : {
        10 : 'center.x', 20 : 'center.y', 30 : 'center.z', 
        40 : 'radius',
        39 : 'thickness',
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
    
    'DIMENSION'    : {
        1 : 'text', 2 : 'name', 3 : 'style',
        10 : 'def_point.x', 20 : 'def_point.y', 30 : 'def_point.z', 
        11 : 'mid_point.x', 21 : 'mid_point.y', 31 : 'mid_point.z', 
        12 : 'vector.x', 22 : 'vector.y', 32 : 'vector.z', 
        13 : 'def_point2.x', 23 : 'def_point2.y', 33 : 'def_point2.z', 
        14 : 'vector2.x', 24 : 'vector2.y', 34 : 'vector2.z', 
        15 : 'vector3.x', 25 : 'vector3.y', 35 : 'vector3.z', 
        16 : 'vector4.x', 26 : 'vector4.y', 36 : 'vector4.z', 
        70 : 'dimtype',
        },
    
    'ELLIPSE'    : {
        10 : 'center.x', 20 : 'center.y', 30 : 'center.z', 
        11 : 'end_point.x', 21 : 'end_point.y', 31 : 'end_point.z', 
        40 : 'ratio', 41 : 'start', 42 : 'end',
        39 : 'thickness',
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
    
    'HATCH'        : {
        2 : 'pattern',
        10 : 'point.x', 20 : 'point.y', 30 : 'point.z', 
        41 : 'scale', 47 : 'pixelsize', 52 : 'angle',
        70 : 'fill', 71 : 'associativity', 75: 'style', 77 : 'double', 
        78 : 'numlines', 91 : 'numpaths', 98 : 'numseeds',
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
    
    'IMAGE'        : {
        10 : 'insertion_point.x', 20 : 'insertion_point.y', 30 : 'insertion_point.z', 
        11 : 'u_vector.x', 21 : 'u_vector.y', 31 : 'u_vector.z', 
        12 : 'v_vector.x', 22 : 'v_vector.y', 32 : 'v_vector.z', 
        13 : 'size.x', 23 : 'size.y', 33 : 'size.z', 
        14 : 'clip.x', 24 : 'clip.y', 34 : 'clip.z', 
        70 : 'display', 71 : 'cliptype', 
        90 : 'version',
        280 : 'clipstate', 281 : 'brightness', 282 : 'contrast', 283 : 'fade', 
        340 : 'image', 360 : 'reactor',
        },
    
    'INSERT'    : {
        1 : 'attributes_follow', 2 : 'name',
        10 : 'insertion_point.x', 20 : 'insertion_point.y', 30 : 'insertion_point.z', 
        41 : 'x_scale', 42 : 'y_scale', 43 : 'z_scale', 
        44 : 'column_spacing', 45 : 'row_spacing', 
        50 : 'rotation_angle', 66 : 'attributes_follow',
        70 : 'column_count', 71 : 'row_count', 
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
    
    'LEADER'    : {
        3 : 'style',
        10 : ['new_vertex(data)'], 20 : 'vertex.y', 30 : 'vertex.z', 
        40 : 'height', 41 : 'width',
        71 : 'arrowhead', 72 : 'pathtype', 73 : 'creation', 
        74 : 'hookdir', 75 : 'hookline', 76 : 'numverts', 77 : 'color',
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        211 : 'horizon.x', 221 : 'horizon.y', 231 : 'horizon.z', 
        212 : 'offset_ins.x', 222 : 'offset_ins.y', 232 : 'offset_ins.z', 
        213 : 'offset_ann.x', 223 : 'offset_ann.y', 233 : 'offset_ann.z', 
        },
    
    'LINE'        : {
        10 : 'start_point.x', 20 : 'start_point.y', 30 : 'start_point.z', 
        11 : 'end_point.x', 21 : 'end_point.y', 31 : 'end_point.z', 
        39 : 'thickness',
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
    
    'LWPOLYLINE'    : {
        10 : ['new_vertex(data)'], 20 : 'vertex.y', 30 : 'vertex.z', 
        38 : 'elevation', 39 : 'thickness',
        40 : 'start_width', 41 : 'end_width', 42 : 'bulge', 43 : 'constant_width',
        70 : 'flags', 90 : 'numverts',
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
            
    'MLINE'    : {
        10 : 'start_point.x', 20 : 'start_point.y', 30 : 'start_point.z', 
        11 : ['new_vertex(data)'], 21 : 'vertex.y', 31 : 'vertex.z', 
        12 : ['new_seg_dir(data)'], 22 : 'seg_dir.y', 32 : 'seg_dir.z', 
        13 : ['new_miter_dir(data)'], 23 : 'miter_dir.y', 33 : 'miter_dir.z', 
        39 : 'thickness',
        40 : 'scale', 41 : 'elem_param', 42 : 'fill_param',
        70 : 'justification', 71 : 'flags',
        72 : 'numverts', 73 : 'numelems', 74 : 'numparam', 75 : 'numfills',
        340 : 'id',
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
            
    'MTEXT'        : {
        1 : 'text', 3: 'more_text', 7 : 'style',
        10 : 'insertion_point.x', 20 : 'insertion_point.y', 30 : 'insertion_point.z', 
        11 : 'alignment_point.x', 21 : 'alignment_point.y', 31 : 'alignment_point.z', 
        40 : 'nominal_height', 41 : 'reference_width', 42: 'width', 43 : 'height', 44 : 'line_spacing',
        50 : 'rotation_angle', 
        71 : 'attachment_point', 72 : 'drawing_direction',  73 : 'spacing_style',    
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
    
    'POINT'        : {
        10 : 'point.x', 20 : 'point.y', 30 : 'point.z', 
        39 : 'thickness', 50 : 'orientation',
        },
    
    'POLYLINE'    : {
        1 : 'verts_follow', 2 : 'name',
        10 : 'elevation.x', 20 : 'elevation.y', 30 : 'elevation.z', 
        39 : 'thickness',
        40 : 'start_width', 41 : 'end_width', 
        66 : 'verts_follow_flag',
        70 : 'flags', 71 : 'row_count', 72 : 'column_count', 
        73 : 'row_density', 74 : 'column_density', 75 : 'linetype',
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
    
    'RAY'        : {
        10 : 'point.x', 20 : 'point.y', 30 : 'point.z', 
        11 : 'direction.x', 21 : 'direction.y', 31 : 'direction.z', 
        },
    
    'RTEXT'        : {
        1 : 'text', 7 : 'style',
        10 : 'insertion_point.x', 20 : 'insertion_point.y', 30 : 'insertion_point.z', 
        39 : 'thickness',
        40 : 'height', 
        50 : 'rotation_angle',
        70 : 'flags',
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
    
    'SHAPE'        : {
        2 : 'name', 
        10 : 'insertion_point.x', 20 : 'insertion_point.y', 30 : 'insertion_point.z', 
        39 : 'thickness',
        40 : 'size', 41 : 'x_scale', 
        50 : 'rotation_angle', 51 : 'oblique_angle',     
        39 : 'thickness',
        },
    
    'SOLID'        : {
        10 : 'point0.x', 20 : 'point0.y', 30 : 'point0.z', 
        11 : 'point1.x', 21 : 'point1.y', 31 : 'point1.z', 
        12 : 'point2.x', 22 : 'point2.y', 32 : 'point2.z', 
        13 : 'point3.x', 23 : 'point3.y', 33 : 'point3.z', 
        39 : 'thickness',
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
    
    'SPLINE'    : {
        10 : ['new_control_point(data)'], 20 : 'control_point.y', 30 : 'control_point.z', 
        11 : ['new_fit_point(data)'], 21 : 'fit_point.y', 31 : 'fit_point.z', 
        40 : ['new_knot_value(data)'], 
        12 : 'start_tangent.x', 22 : 'start_tangent.y', 32 : 'start_tangent.z', 
        13 : 'end_tangent.x', 23 : 'end_tangent.y', 33 : 'end_tangent.z', 
        39 : 'thickness',
        41 : 'weight', 42 : 'knot_tol', 43 : 'control_point_tol', 44 : 'fit_tol',
        70 : 'flag', 71 : 'degree', 
        72 : 'num_knots', 73 : 'num_control_points', 74 : 'num_fit_points',
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
    
    'TEXT'        : {
        1 : 'text', 7 : 'style',
        10 : 'insertion_point.x', 20 : 'insertion_point.y', 30 : 'insertion_point.z', 
        11 : 'alignment_point.x', 21 : 'alignment_point.y', 31 : 'alignment_point.z', 
        40 : 'height', 41 : 'x_scale', 
        50 : 'rotation_angle', 51 : 'oblique_angle', 
        71 : 'flags', 72 : 'horizontal_justification',  73 : 'vertical_justification',    
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
    
    'TOLERANCE'    : {
        3 : 'style',
        10 : 'insertion_point.x', 20 : 'insertion_point.y', 30 : 'insertion_point.z', 
        11 : 'direction.x', 21 : 'direction.y', 31 : 'direction.z', 
        },
    
    'TRACE'        : {
        10 : 'point0.x', 20 : 'point0.y', 30 : 'point0.z', 
        11 : 'point1.x', 21 : 'point1.y', 31 : 'point1.z', 
        12 : 'point2.x', 22 : 'point2.y', 32 : 'point2.z', 
        13 : 'point3.x', 23 : 'point3.y', 33 : 'point3.z', 
        39 : 'thickness',
        210 : 'normal.x', 220 : 'normal.y', 230 : 'normal.z', 
        },
    
    'VERTEX'    : {
        10 : 'location.x', 20 : 'location.y', 30 : 'location.z', 
        40 : 'start_width', 41 : 'end_width', 42 : 'bulge', 
        50 : 'tangent',
        70 : 'flags',
        71 : 'index1', 72 : 'index2', 73 : 'index3', 74 : 'index4', 
        },
    
    'VIEWPORT'    : {
        10 : 'center.x', 20 : 'center.y', 30 : 'center.z', 
        12 : 'view_center.x', 22 : 'view_center.y', 32 : 'view_center.z', 
        13 : 'snap_base.x', 23 : 'snap_base.y', 33 : 'snap_base.z', 
        14 : 'snap_spacing.x', 24 : 'snap_spacing.y', 34 : 'snap_spacing.z', 
        15 : 'grid_spacing.x', 25 : 'grid_spacing.y', 35 : 'grid_spacing.z', 
        16 : 'view_direction.x', 26 : 'view_direction.y', 36 : 'view_direction.z', 
        40 : 'width', 41 : 'height',
        68 : 'status', 69 : 'id',
        },
    
    'WIPEOUT'    : {
        10 : 'point.x', 20 : 'point.y', 30 : 'point.z', 
        11 : 'direction.x', 21 : 'direction.y', 31 : 'direction.z', 
        },
    
    }
    
    
    #
    #    Flags
    #
    
    # Polyline flags
    PL_CLOSED         = 0x01
    PL_CURVE_FIT_VERTS    = 0x02
    PL_SPLINE_FIT_VERTS    = 0x04
    PL_3D_POLYLINE        = 0x08
    PL_3D_POLYGON_MESH    = 0x10
    PL_CLOSED_IN_N_DIR    = 0x20
    PL_POLYFACE_MESH    = 0x40
    PL_CONTINUOUS        = 0x80
    
    
    # Vertex flags
    VX_EXTRA_FLAG_CREATED        = 0x01
    VX_CURVE_FIT_TANGENT_DEFINED    = 0x02
    VX_SPLINE_VERTEX_CREATED    = 0x08
    VX_SPLINE_FRAME_CONTROL_POINT    = 0x10
    VX_3D_POLYLINE_VERTEX        = 0x20
    VX_3D_POLYGON_MESH_VERTEX    = 0x40
    VX_POLYFACE_MESH_VERTEX        = 0x80
    
    # 3DFACE flags
    
    F3D_EDGE0_INVISIBLE = 0x01
    F3D_EDGE1_INVISIBLE = 0x02
    F3D_EDGE2_INVISIBLE = 0x04
    F3D_EDGE3_INVISIBLE = 0x08
    
    #
    #    readDxfFile(filePath):
    #
    
    def readDxfFile(fileName):    
        global toggle, theCodec
    
        print( "Opening DXF file "+ fileName )
    
        # fp= open(fileName, "rU")
        fp = codecs.open(fileName, "r", encoding=theCodec)
        first = True
        statements = []
        no = 0
        for line in fp: 
            word = line.strip()
            no += 1
            if first:
                if word:
                    code = int(word)
                    first = False
            else:
                if toggle & T_Verbose:
                    print("%4d: %4d %s" % (no, code, word))
                if code < 10:
                    data = word
                elif code < 60:
                    data = float(word)
                elif code < 100:
                    data = int(word)
                elif code < 140:
                    data = word
                elif code < 150:
                    data = float(word)
                elif code < 200:
                    data = int(word)
                elif code < 300:
                    data = float(word)
                elif code < 370:
                    data = word
                elif code < 390:
                    data = int(word)
                elif code < 400:
                    data = word
                elif code < 410:
                    data = int(word)
                elif code < 1010:
                    data = word
                elif code < 1060:
                    data = float(word)
                elif code < 1080:
                    data = int(word)
    
                statements.append((code,data))
                first = True
        fp.close()
    
        statements.reverse()
        sections = {}
        handles = {}
        while statements:
            (code,data) = statements.pop()
            if code == 0:
                if data == 'SECTION':
                    section = CSection()
            elif code == 2:
                section.type = data
                if data == 'HEADER':
                    parseHeader(section, statements, handles)
                    known = False
                elif data == 'CLASSES':
                    parseClasses(section, statements, handles)
                    known = False
                elif data == 'TABLES':
                    parseTables(section, statements, handles)
                    known = False
                elif data == 'BLOCKS':
                    parseBlocks(section, statements, handles)
                    known = False
                elif data == 'ENTITIES':
                    parseEntities(section, statements, handles)
                    known = False
                elif data == 'OBJECTS':
                    parseObjects(section, statements, handles)
    
                elif data == 'THUMBNAILIMAGE':
                    parseThumbnail(section, statements, handles)
    
    Remigiusz Fiedler's avatar
    Remigiusz Fiedler committed
                sections[data] = section
            elif code == 999:
                pass
            else:
                raise NameError("Unexpected code in SECTION context: %d %s" % (code,data))
    
        if toggle & T_Verbose:
            for (typ,section) in sections.items():
                section.display()
        return sections
        
    
    #
    #     0
    #    SECTION
    #      2
    #    HEADER
    #    
    #      9
    #    $<variable>
    #    <group code>
    #    <value>
    #    
    #      0
    #    ENDSEC
    
        
    def parseHeader(section, statements, handles):
        while statements:
            (code,data) = statements.pop()
            if code == 0:
                if data == 'ENDSEC':
                    return
    
        return
    
    
    #      0
    #    SECTION
    #      2
    #    CLASSES
    #    
    #      0
    #    CLASS
    #      1
    #    <class dxf record>
    #      2
    #    <class name>
    #      3
    #    <app name>
    #    90
    #    <flag>
    #    280
    #    <flag>
    #    281
    #    <flag>
    #    
    #      0
    #    ENDSEC         
    
    def parseClasses(section, statements, handles):
        while statements:
            (code,data) = statements.pop()
            if code == 0:
                if data == 'ENDSEC':
                    return
    
        return
        
    
    #      0
    #    SECTION
    #      2
    #    TABLES
    #    
    #      0
    #    TABLE
    #      2
    #    <table type>
    #      5
    #    <handle>
    #    100
    #    AcDbSymbolTable
    #    70
    #    <max. entries>
    #    
    #      0
    #    <table type>
    #      5
    #    <handle>
    #    100
    #    AcDbSymbolTableRecord
    #    .
    #    . <data>
    #    .
    #    
    #      0
    #    ENDTAB
    #    
    #      0
    #    ENDSEC 
    
    #
    #      APPID (application identification table)
    #
    #      BLOCK_RECORD (block reference table)
    #
    #      DIMSTYLE (dimension style table)
    #
    #      LAYER (layer table)
    #
    #      LTYPE (linetype table)
    #
    #      STYLE (text style table)
    #
    #      UCS (User Coordinate System table)
    #
    #      VIEW (view table)
    #
    #      VPORT (viewport configuration table)
    
    
    def parseTables(section, statements, handles):
        tables = []
        section.data = tables
        while statements:
            (code,data) = statements.pop()
            if code == 0:
                if data == 'ENDSEC':
                    return
        '''
                    known = False
                elif data == 'TABLE':
                    table = CTable()
                    tables.append(table)
                    known = False
                elif data == 'ENDTAB':
                    pass
                    known = False
                elif data == table.type:
                    parseTableType
                    table = CTable()
                    tables.append(table)
                    table.type = word
            elif code == 2:
                table.type = word
            elif code == 5:
                table.handle = word
                handles[word] = table
            elif code == 330:
                table.owner = word
            elif code == 100:
                table.subclass = word
            elif code == 70: