Skip to content
Snippets Groups Projects
io_import_scene_dxf.py 73.6 KiB
Newer Older
Remigiusz Fiedler's avatar
Remigiusz Fiedler committed
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

bl_info = {
Luca Bonavita's avatar
Luca Bonavita committed
    'name': 'Import Autocad DXF (.dxf)',
    'author': 'Thomas Larsson, Remigiusz Fiedler',
    'version': (0, 1, 5),
Luca Bonavita's avatar
Luca Bonavita committed
    'blender': (2, 5, 6),
Luca Bonavita's avatar
Luca Bonavita committed
    'location': 'File > Import',
    'description': 'Import files in the Autocad DXF format (.dxf)',
    'warning': 'only a part of DXF specification is supported, WIP',
Luca Bonavita's avatar
Luca Bonavita committed
    'wiki_url': 'http://wiki.blender.org/index.php/Extensions:2.5/Py/'\
        'Scripts/Import-Export/DXF_Importer',
    'tracker_url': 'https://projects.blender.org/tracker/index.php?'\
        'func=detail&aid=23480',
    'support': 'OFFICIAL',
    'category': 'Import-Export',
    }
Luca Bonavita's avatar
Luca Bonavita committed

Remigiusz Fiedler's avatar
Remigiusz Fiedler committed
"""
Release note by migius (DXF support maintainer) 2011.01.02:
Script supports only a small part of DXF specification:
- imports LINE, ARC, CIRCLE, ELLIPSE, SOLID, TRACE, POLYLINE, LWPOLYLINE
- imports TEXT, MTEXT
- supports 3d-rotation of entities (210 group)
- supports THICKNESS for SOLID, TRACE, LINE, ARC, CIRCLE, ELLIPSE
- ignores WIDTH, THICKNESS, BULGE in POLYLINE/LWPOLYLINE
- ignores face-data in POLYFACE / POLYMESH
- ignores TEXT 2d-rotation
- ignores hierarchies (BLOCK, INSERT, GROUP)
- ignores LAYER
- ignores COLOR, LINEWIDTH, LINESTYLE

This script is a temporary solution.
Probably no more improvements will be done to this script.
The full-feature importer script from 2.49 will be back in 2.6 release.

Installation:
Place this file to Blender addons directory
  (on Windows it is %Blender_directory%\2.53\scripts\addons\)
Remigiusz Fiedler's avatar
Remigiusz Fiedler committed
You must activate the script in the "Add-Ons" tab (user preferences).
Access it from File > Import menu.

History:
ver 0.1.5 - 2011.02.05 by migius for r.34661
- changed support level to OFFICIAL
- fixed missing last point at building Mesh-ARCs (by pildanovak)
- fixed for changes in API and mathutils by campbell
ver 0.1.4 - 2011.01.13 by migius
- modified for latest API in rev.34300 (by Filiciss Muhgue)
Remigiusz Fiedler's avatar
Remigiusz Fiedler committed
ver 0.1.3 - 2011.01.02 by migius
- added draw curves as sequence for "Draw_as_Curve"
- added toggle "Draw as one" as user preset in UI
- added draw POINT as mesh-vertex
- added draw_THICKNESS for LINE, ARC, CIRCLE, ELLIPSE, LWPOLYLINE and POLYLINE
- added draw_THICKNESS for SOLID, TRACE
ver 0.1.2 - 2010.12.27 by migius
- added draw() for TRACE
- fixed wrong vertex order in SOLID
- added CIRCLE resolution as user preset in UI
- added closing segment for circular LWPOLYLINE and POLYLINE
- fixed registering for 2.55beta
ver 0.1.1 - 2010.09.07 by migius
- fixed dxf-file names recognition limited to ".dxf"
- fixed registering for 2.53beta
ver 0.1 - 2010.06.10 by Thomas Larsson
"""

__version__ = '.'.join([str(s) for s in bl_info['version']])
Remigiusz Fiedler's avatar
Remigiusz Fiedler committed

import os
import codecs
import math
from math import sin, cos, radians
import bpy
import mathutils
from mathutils import Vector, Matrix

#
#    Global flags
#

T_Merge = 0x01
T_NewScene = 0x02
T_Curves = 0x04
T_DrawOne = 0x08
T_Debug = 0x10
T_Verbose = 0x20
T_ThicON = 0x40

toggle = T_Merge | T_NewScene | T_DrawOne | T_ThicON
theCircleRes = 32
theMergeLimit = 1e-5

#
#    class CSection:
#

class CSection:
    type = None

    def __init__(self):
        self.data = []

    def display(self):
        print("Section", self.type)
        for datum in self.data:
            datum.display()

#
#    class CTable:
#

class CTable:
    def __init__(self):
        self.type = None
        self.name = None
        self.handle = None
        self.owner = None
        self.subclass = None
        self.nEntries = 0
    def display(self):
        print("Table %s %s %s %s %s %d" % (self.type, self.name, self.handle, self.owner, self.subclass, self.nEntries))

#
#    class CEntity:
#
class CEntity:
    def __init__(self, typ, drawtype):
        self.type = typ
        self.drawtype = drawtype
        self.handle = None
        self.owner = None
        self.subclass = None
        self.layer = 0
        self.color = 0
        self.invisible = 0
        self.linetype_name = ''
        self.linetype_scale = 1.0
        self.paperspace = 0
        #self.normal = Vector((0,0,1))

    def display(self):
        print("Entity %s %s %s %s %s %s %x" % 
            (self.type, self.handle, self.owner, self.subclass, self.layer, self.color, self.invisible))

    def build(self, vn=0):
        global toggle
        if toggle & T_Debug:
            raise NameError("Warning: can not build - unsupported entity type: %s" % self.type)
        return(([], [], [], vn)) 

    def draw(self):
        global toggle
        if toggle & T_Debug:
            raise NameError("Warning: can not draw - unsupported entity type: %s" % self.type)
        return


DxfCommonAttributes = {
    5 : 'handle',
    6 : 'linetype_name',
    8 : 'layer',
    48 : 'linetype_scale',
    60 : 'invisible',
    62 : 'color',
    67 : 'paperspace',
    100 : 'subclass',
    330 : 'owner',
    360 : 'owner',
}

#
#    class C3dFace(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', 
#    70 : 'flags',
#

class C3dFace(CEntity):
    def __init__(self):
        CEntity.__init__(self, '3DFACE', 'Mesh')
        self.point0 = Vector()
        self.point1 = Vector()
        self.point2 = Vector()
        self.point3 = Vector()

    def display(self):
        CEntity.display(self)
        print(self.point0)
        print(self.point1)
        print(self.point2)
        print(self.point3)

    def build(self, vn=0):
        verts = [self.point0, self.point1, self.point2]
        if self.point3 == Vector((0,0,0)) or self.point2 == self.point3:
            faces = [(vn+0, vn+1, vn+2)]
            vn += 3
        else:
            verts.append( self.point3 )
            faces = [(vn+0, vn+1, vn+2, vn+3)]
            vn += 4            
        return((verts, [], faces, vn))

#
#    class C3dSolid(CEntity):
#    1 : 'data', 3 : 'more', 70 : 'version',
#

class C3dSolid(CEntity):
    def __init__(self):
        CEntity.__init__(self, '3DSOLID', 'Mesh')
        self.data = None
        self.more = None
        self.version = 0

#
#    class CAcadProxyEntity(CEntity):
#    70 : 'format',
#    90 : 'id', 91 : 'class', 92 : 'graphics_size', 93 : 'entity_size', 95: 'format',
#    310 : 'data', 330 : 'id1', 340 : 'id2', 350 : 'id3', 360 : 'id4', 
#

class CAcadProxyEntity(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'ACAD_PROXY_ENTITY', None)


#
#    class CArc(CEntity):
#    10 : 'center.x', 20 : 'center.y', 30 : 'center.z', 
#    40 : 'radius',
#    50 : 'start_angle', 51 : 'end_angle'
#

class CArc(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'ARC', 'Mesh')
        self.center = Vector()
        self.radius = 0.0
        self.start_angle = 0.0
        self.end_angle = 0.0
        self.thickness = 0.0
        self.normal = Vector((0,0,1))
        
    def display(self):
        CEntity.display(self)
        print(self.center)
        print("%.4f %.4f %.4f " % (self.radius, self.start_angle, self.end_angle))

    def build(self, vn=0):
        start, end = self.start_angle, self.end_angle
        if end > 360: end = end % 360.0
        if end < start: end +=360.0
        angle = end - start

        deg2rad = math.pi/180.0
        start *= deg2rad
        end *= deg2rad
        dphi = end - start
        phi0 = start
        w = dphi/theCircleRes
        r = self.radius
        center = self.center
        v0 = vn
        points = []
        edges, faces = [], []
        for n in range(theCircleRes + 1):
Remigiusz Fiedler's avatar
Remigiusz Fiedler committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
            s = math.sin(n*w + phi0)
            c = math.cos(n*w + phi0)
            v = center + Vector((r*c, r*s, 0.0))
            points.append(v)
        pn = len(points)
        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)
            faces = [(v0+nr+0,v0+nr+1,v0+pn+nr+1,v0+pn+nr+0) for nr in range(pn)]
            faces.pop()
            self.drawtype = 'Mesh'
            vn += 2*pn
        else:
            edges = [(v0+nr+0,v0+nr+1) for nr in range(pn)]
            edges.pop()
            vn += pn

        if self.normal!=Vector((0,0,1)):
            ma = getOCS(self.normal)
            if ma:
                #ma.invert()
                points = [v * ma for v in points]
        #print ('arc vn=', vn)
        #print ('faces=', len(faces))
        return ((points, edges, faces, vn))

#
#    class CArcAlignedText(CEntity):
#    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'
#

class CArcAlignedText(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'ARCALIGNEDTEXT', 'Mesh')
        self.text = ""
        self.style = ""
        self.center = Vector()
        self.radius = 0.0
        self.width = 1.0
        self.height = 1.0
        self.spacing = 1.0
        self.offset = 0.0
        self.right_offset = 0.0
        self.left_offset = 0.0
        self.start_angle = 0.0
        self.end_angle = 0.0
        self.order = 0
        self.directions = 0
        self.alignment = 0
        self.side = 0
        self.bold = 0
        self.italic = 0
        self.underline = 0
        self.character_set = 0
        self.pitch = 0
        self.fonttype = 0
        self.color = 0
        self.wizard = None
        self.id = None
        self.normal = Vector((0,0,1))


#
#    class CAttdef(CEntity):
#    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',    
#

class CAttdef(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'ATTDEF', None)
        self.value = ""
        self.tag = ""
        self.prompt = ""
        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.text_generation_flags = 0
        self.horizontal_justification = 0.0
        self.vertical_justification = 0.0
        self.normal = Vector((0,0,1))

    def draw(self):
        drawText(self.text,  self.insertion_point, self.height, self.x_scale, self.rotation_angle, self.oblique_angle, self.normal)
        return

#
#    class CAttrib(CEntity):
#    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',     
#

class CAttrib(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'ATTRIB', None)
        self.text = ""
        self.tag = ""
        self.prompt = ""

        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.length = 1.0
        self.text_generation_flags = 0
        self.horizontal_justification = 0.0
        self.vertical_justification = 0.0
        self.normal = Vector((0,0,1))

    def draw(self):
        drawText(self.text,  self.insertion_point, self.height, self.x_scale, self.rotation_angle, self.oblique_angle, self.normal)
        return


#
#    class CBlock(CEntity):
#    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', 
#

class CBlock(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'BLOCK', None)
        self.xref = ""
        self.name = ""
        self.also_name = ""
        self.base_point = Vector()
        self.size = 1.0
        self.x_scale = 1.0
        self.rotation_angle = 0.0
        self.oblique_angle = 0.0
        self.flags = 0
        self.normal = Vector((0,0,1))

    def display(self):
        CEntity.display(self)
        print("%s %s %s " % (self.xref, self.name, self.also_name))
        print(self.base_point)

    def draw(self):
        # Todo
        return

#
#    class CCircle(CEntity):
#    10 : 'center.x', 20 : 'center.y', 30 : 'center.z', 
#    40 : 'radius'
#

class CCircle(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'CIRCLE', 'Mesh')
        self.center = Vector()
        self.radius = 0.0
        self.thickness = 0.0
        self.normal = Vector((0,0,1))

    def display(self):
        CEntity.display(self)
        print(self.center)
        print("%.4f" % self.radius)

    def build(self, vn=0):
        w = 2*math.pi/theCircleRes
        r = self.radius
        center = self.center
        points = []
        edges, faces = [], []
        v0 = vn
        for n in range(theCircleRes):
            s = math.sin(n*w)
            c = math.cos(n*w)
            v = center + Vector((r*c, r*s, 0))
            points.append(v)

        pn = len(points)
        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)
            faces = [(v0+nr,v0+nr+1,pn+v0+nr+1,pn+v0+nr) for nr in range(pn)]
            nr = pn -1
            faces[-1] = (v0+nr,v0,pn+v0,pn+v0+nr)
            self.drawtype = 'Mesh'
            vn += 2*pn
        else:
            edges = [(v0+nr,v0+nr+1) for nr in range(pn)]
            nr = pn -1
            edges[-1] = (v0+nr,v0)
            vn += pn
        if self.normal!=Vector((0,0,1)):
            ma = getOCS(self.normal)
            if ma:
                #ma.invert()
                points = [v * ma for v in points]
        #print ('cir vn=', vn)
        #print ('faces=',len(faces))
        return( (points, edges, faces, vn) )
            
#
#    class CDimension(CEntity):
#    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',
#

class CDimension(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'DIMENSION', None)
        self.text = ""
        self.name = ""
        self.style = ""
        self.def_point = Vector()
        self.mid_point = Vector()
        self.vector = Vector()
        self.def_point2 = Vector()
        self.vector2 = Vector()
        self.vector3 = Vector()
        self.vector4 = Vector()
        self.dimtype = 0
        self.normal = Vector((0,0,1))

    def draw(self):
        return

#
#    class CEllipse(CEntity):
#    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',
#

class CEllipse(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'ELLIPSE', 'Mesh')
        self.center = Vector()
        self.end_point = Vector()
        self.ratio = 1.0
        self.start = 0.0
        self.end = 2*math.pi
        self.thickness = 0.0
        self.normal = Vector((0,0,1))

    def display(self):
        CEntity.display(self)
        print(self.center)
        print("%.4f" % self.ratio)
                
    def build(self, vn=0):
        dphi = (self.end - self.start)
        phi0 = self.start
        w = dphi/theCircleRes
        r = self.end_point.length
        f = self.ratio
        a = self.end_point.x/r
        b = self.end_point.y/r
        center = self.center
        v0 = vn
        points = []
        edges, faces = [], []
        for n in range(theCircleRes):
            x = r*math.sin(n*w + phi0)
            y = f*r*math.cos(n*w + phi0)
            v = (center.x - a*x + b*y, center.y - a*y - b*x, center.z)
            points.append(v)

        pn = len(points)
        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)
            faces = [(v0+nr,v0+nr+1,pn+v0+nr+1,pn+v0+nr) for nr in range(pn)]
            nr = pn -1
            faces[-1] = (v0+nr,v0,pn+v0,pn+v0+nr)
            #self.drawtype = 'Mesh'
            vn += 2*pn
        else:
            edges = [(v0+nr,v0+nr+1) for nr in range(pn)]
            nr = pn -1
            edges[-1] = (v0+nr,v0)
            vn += pn


        if thic != 0 and (toggle & T_ThicON):
            pass
        if self.normal!=Vector((0,0,1)):
            ma = getOCS(self.normal)
            if ma:
                #ma.invert()
                points = [v * ma for v in points]
        return ((points, edges, faces, vn))

#
#    class CHatch(CEntity):
#    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',
#

class CHatch(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'HATCH', None)
        self.pattern = 0
        self.point = Vector()
        self.scale = 1.0
        self.pixelsize = 1.0
        self.angle = 0.0
        self.fill = 0
        self.associativity = 0
        self.style = 0
        self.double = 0
        self.numlines = 0
        self.numpaths = 0
        self.numseeds = 0
        self.normal = Vector((0,0,1))


#    class CImage(CEntity):
#    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'
#

class CImage(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'IMAGE', None)
        self.insertion_point = Vector()
        self.u_vector = Vector()
        self.v_vector = Vector()
        self.size = Vector()
        self.clip = Vector()
        self.display = 0
        self.cliptype = 0
        self.version = 1
        self.clipstate = 0
        self.brightness = 0
        self.constrast = 0
        self.fade = 0
        self.image = None
        self.reactor = None
        self.normal = Vector((0,0,1))

#
#    class CInsert(CEntity):
#    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', 
#

class CInsert(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'INSERT', None)
        self.attributes_follow = 1
        self.name = ""
        self.insertion_point = Vector()
        self.x_scale = 1.0
        self.y_scale = 1.0
        self.z_scale = 1.0
        self.column_spacing = 1.0
        self.row_spacing = 1.0
        self.rotation_angle = 0.0
        self.column_count = 1
        self.row_count = 1
        self.attributes_follow = 0
        self.normal = Vector((0,0,1))

    def display(self):
        CEntity.display(self)
        print(self.insertion_point)

    def draw(self):
        # Todo
        return

#
#    class CLeader(CEntity):
#    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', 
#

class CLeader(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'LEADER', 'Mesh')
        self.style = ""
        self.vertex = None
        self.verts = []
        self.height = 1.0
        self.width = 1.0
        self.arrowhead = 0
        self.pathtype = 0
        self.creation = 0
        self.hookdir = 0
        self.hookline = 0
        self.numverts = 0
        self.color = 0
        self.normal = Vector((0,0,1))
        self.horizon = Vector()
        self.offset_ins = Vector()
        self.offset_ann = Vector()

    def new_vertex(self, data):
        self.vertex = Vector()
        self.vertex.x = data
        self.verts.append(self.vertex)

    def build(self, vn=0):
        edges = []
        for v in self.verts:
            edges.append((vn, vn+1))
            vn += 1
        edges.pop()
        return (self.verts, edges, [], vn)

#    class CLwPolyLine(CEntity):
#    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'
#

class CLWPolyLine(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'LWPOLYLINE', None)
        self.vertex = None
        self.verts = []
        self.elevation = 0
        self.thickness = 0.0
        self.start_width = 0.0
        self.end_width = 0.0
        self.bulge = 0.0
        self.constant_width = 0.0
        self.flags = 0
        self.numverts = 0
        self.normal = Vector((0,0,1))

    def new_vertex(self, data):
        self.vertex = Vector()
        self.vertex.x = data
        self.verts.append(self.vertex)

    def build(self, vn=0):
        edges = []
        v_start = vn
        for v in self.verts:
            edges.append((vn, vn+1))
            vn += 1
        if self.flags & PL_CLOSED:
            edges[-1] = (vn-1, v_start)
        else:
            edges.pop()
        verts = self.verts
        if self.normal!=Vector((0,0,1)):
            ma = getOCS(self.normal)
            if ma:
                #ma.invert()
                verts = [v * ma for v in verts]
        return (verts, edges, [], vn-1)
        
#
#    class CLine(CEntity):
#    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',
#

class CLine(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'LINE', 'Mesh')
        self.start_point = Vector()
        self.end_point = Vector()
        self.thickness = 0.0
        self.normal = Vector((0,0,1))

    def display(self):
        CEntity.display(self)
        print(self.start_point)
        print(self.end_point)

    def build(self, vn=0):
        points = [self.start_point, self.end_point]
        faces, edges = [], []
        n = vn
        thic = self.thickness
        if thic != 0 and (toggle & T_ThicON):
            t_vector = thic * self.normal
            #print 'deb:thic_vector: ', t_vector #---------------------
            points.extend([v + t_vector for v in points])
            faces = [[0+n, 1+n, 3+n, 2+n]]
            self.drawtype = 'Mesh'
        else:
            edges = [[0+n, 1+n]]
        vn +=2
        return((points, edges, faces, vn))

#    class CMLine(CEntity):
#    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', 
#    40 : 'scale', 41 : 'elem_param', 42 : 'fill_param',
#    70 : 'justification', 71 : 'flags'
#    72 : 'numverts', 73 : 'numelems', 74 : 'numparam', 75 : 'numfills',
#    340 : 'id'
#

class CMLine(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'MLINE', None)
        self.start_point = Vector()
        self.vertex = None
        self.seg_dir = None
        self.miter_dir = None
        self.verts = []
        self.seg_dirs = []
        self.miter_dirs = []
        self.scale = 1.0
        self.elem_param = 0
        self.fill_param = 0
        self.justification = 0
        self.flags = 0
        self.numverts = 0
        self.numelems = 0
        self.numparam = 0
        self.numfills = 0
        self.id = 0
        self.normal = Vector((0,0,1))

    def new_vertex(self, data):
        self.vertex = Vector()
        self.vertex.x = data
        self.verts.append(self.vertex)

    def new_seg_dir(self, data):
        self.seg_dir = Vector()
        self.seg_dir.x = data
        self.seg_dirs.append(self.seg_dir)

    def new_miter_dir(self, data):
        self.miter_dir = Vector()
        self.miter_dir.x = data
        self.miter_dirs.append(self.miter_dir)



#
#    class CMText(CText):
#    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',    
#

class CMText(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'MTEXT', 'Text')
        self.text = ""
        self.more_text = ""
        self.style = ""
        self.insertion_point = Vector()
        self.alignment_point = Vector()
        self.nominal_height = 1.0
        self.reference_width = 1.0
        self.width = 1.0
        self.height = 1.0
        self.rotation_angle = 0.0
        self.attachment_point = 0
        self.drawing_direction = 0
        self.spacing_style = 0
        self.normal = Vector((0,0,1))

    def display(self):
        CEntity.display(self)
        print("%s %s" % (self.text, self.style))
        print('MTEXTinsertion_point=',self.insertion_point)
        print('MTEXTalignment_point=',self.alignment_point)

    def draw(self):
        drawText(self.text,  self.insertion_point, self.height, self.width, self.rotation_angle, 0.0, self.normal)
        return

#
#    class CPoint(CEntity):
#    10 : 'point.x', 20 : 'point.y', 30 : 'point.z', 
#    39 : 'thickness', 50 : 'orientation'
#

class CPoint(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'POINT', 'Mesh')
        self.point = Vector()
        self.thickness = 0.0
        self.orientation = 0.0

    def display(self):
        CEntity.display(self)
        print(self.point)
        print("%.4f" % self.orientation)

    def build(self, vn=0):
        # draw as mesh-vertex
        verts = [self.point]
        return((verts, [], [], vn+1))

    def draw(self):
        #todo
        # draw as empty-object
        loc = self.point
        #bpy.ops.object.new('DXFpoint')

#
#    class CPolyLine(CEntity):
#    1 : 'verts_follow', 2 : 'name',
#    10 : 'elevation.x', 20 : 'elevation.y', 30 : 'elevation.z', 
#    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',
#

class CPolyLine(CEntity):
    def __init__(self):
        CEntity.__init__(self, 'POLYLINE', 'Mesh')
        self.verts = []
        self.verts_follow = 1
        self.name = ""
        self.elevation = Vector()
        self.thickness = 0.0
        self.start_width = 0.0
        self.end_width = 0.0
        self.verts_follow_flags = 0
        self.flags = 0
        self.row_count = 1
        self.column_count = 1
        self.row_density = 1.0
        self.column_density = 1.0
        self.linetype = 1
        self.normal = Vector((0,0,1))

    def display(self):
        CEntity.display(self)