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')
Remigiusz Fiedler's avatar
Remigiusz Fiedler committed
        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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
            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)
        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 = [v * ma for v in verts]
        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 = [v * ma for v in points]
        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

#
#    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 = [v * ma for v in points]
        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))
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)
    return Matrix((ax, ay, az))
Remigiusz Fiedler's avatar
Remigiusz Fiedler committed



def transform(normal, rotation, obj):  #--------------------------------------------
    """Use the calculated ocs to determine the objects location/orientation in space.
    """
    ma = Matrix(((1,0,0,0),(0,1,0,0),(0,0,1,0),(0,0,0,1)))
Remigiusz Fiedler's avatar
Remigiusz Fiedler committed
    o = Vector(obj.location)
    ma_new = getOCS(normal)
    if ma_new:
        ma = ma_new
        ma.resize_4x4()
Remigiusz Fiedler's avatar
Remigiusz Fiedler committed

    if rotation != 0:
        g = radians(-rotation)
        rmat = Matrix(((cos(g), -sin(g), 0), (sin(g), cos(g), 0), (0, 0, 1)))
        ma = ma * rmat.to_4x4()
Remigiusz Fiedler's avatar
Remigiusz Fiedler committed
1434 1435 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 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200

    obj.matrix_world = ma #must be matrix4x4
    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)
            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:
            table.nEntries = int(word)
    '''
    return
    
#      0
#    SECTION
#      2
#    BLOCKS
#    
#      0
#    BLOCK
#      5
#    <handle>
#    100
#    AcDbEntity
#      8
#    <layer>
#    100
#    AcDbBlockBegin
#      2
#    <block name>
#    70
#    <flag>
#    10
#    <X value>
#    20
#    <Y value>
#    30
#    <Z value>
#      3
#    <block name>
#      1
#    <xref path>
#    
#      0
#    <entity type>
#    .
#    . <data>
#    .
#    
#      0
#    ENDBLK
#      5
#    <handle>
#    100
#    AcDbBlockEnd
#    
#      0
#    ENDSEC 

def parseBlocks(section, statements, handles):
    while statements:
        (code,data) = statements.pop()
        if code == 0:
            if data == 'ENDSEC':
                return

    return

#      0
#    SECTION
#      2
#    ENTITIES
#    
#      0
#    <entity type>
#      5
#    <handle>
#    330
#    <pointer to owner>
#    100
#    AcDbEntity
#      8
#    <layer>
#    100
#    AcDb<classname>
#    .
#    . <data>
#    .
#    
#      0
#    ENDSEC

Ignorables = ['DIMENSION', 'TEXT', 'VIEWPORT']

ClassCreators = {
    '3DFACE':         'C3dFace()', 
    '3DSOLID':        'C3dSolid()',
    'ACAD_PROXY_ENTITY':    'CAcadProxyEntity()',
    'ACAD_ZOMBIE_ENTITY':    0,
    'ARC':            'CArc()',
    'ARCALIGNEDTEXT':    'CArcAlignedText()',
    'ATTDEF':        'CAttdef()',
    'ATTRIB':        'CAttrib()',
    'BODY':            0,
    'CIRCLE':        'CCircle()',
    'DIMENSION':        'CDimension()',
    'ELLIPSE':        'CEllipse()',
    'HATCH':        'CHatch()',
    'IMAGE':        'CImage()',
    'INSERT':        'CInsert()',
    'LEADER':        'CLeader()',
    'LINE':            'CLine()',
    'LWPOLYLINE':        'CLWPolyLine()',
    'MLINE':        'CMLine()',
    'MTEXT':        'CMText()',
    'OLEFRAME':        0,
    'OLE2FRAME':        0,
    'POINT':        'CPoint()',
    'POLYLINE':        'CPolyLine()',
    'RAY':            'CRay()',
    'REGION':        0,
    'RTEXT':        'CRText',
    'SEQEND':        0,
    'SHAPE':        'CShape()',
    'SOLID':        'CSolid()',
    'SPLINE':        'CSpline()',
    'TEXT':            'CText()',
    'TOLERANCE':        'CTolerance()',
    'TRACE':        'CTrace()',
    'VERTEX':        'CVertex()',
    'VIEWPORT':        'CViewPort()',
    'WIPEOUT':        'CWipeOut()',
    'XLINE':        'CXLine()',
}

def parseEntities(section, statements, handles):
    entities = []
    section.data = entities
    while statements:
        (code,data) = statements.pop()
        if toggle & T_Verbose:
            print("ent", code,data)
        if code == 0:
            known = True
            if data in Ignorables:
                ignore = True
            else:
                ignore = False

            try:
                creator = ClassCreators[data]
            except:
                creator = None
                
            if creator:
                entity = eval(creator)
            elif data == 'ENDSEC':
                return
            else:
                known = False
                
            if data == 'POLYLINE':
                verts = entity.verts
            elif data == 'VERTEX':
                verts.append(entity)
            
            if data == 'SEQEND':
                attributes = []
                known = False
            elif creator == 0:
                ignore = True
            elif known:
                entities.append(entity)
                attributes = DxfEntityAttributes[data]
            else:
                raise NameError("Unknown data %s" % data)

        elif not known:
            pass
        else:
            expr = getAttribute(attributes, code)
            if expr:
                exec(expr)
            else:
                expr = getAttribute(DxfCommonAttributes, code)
                if expr:
                    exec(expr)
                elif code >= 1000 or ignore:
                    pass
                elif toggle & T_Debug:
                    raise NameError("Unknown code %d for %s" % (code, entity.type))
                
    return

def getAttribute(attributes, code):
    try:
        ext = attributes[code]
        if type(ext) == str:
            expr = "entity.%s = data" % ext
        else:
            name = ext[0]
            expr = "entity.%s" % name
    except:
        expr = None
    return expr


#      0
#    SECTION
#      2
#    OBJECTS
Loading
Loading full blame...