1 #!/usr/bin/python
2 # -*- indent-tabs-mode:1 tab-width:4 mode:python minor-mode:whitespace -*-
3 #*****************************************************************************/
4 #* LibreDWG - free implementation of the DWG file format */
5 #* */
6 #* Copyright (C) 2011, 2018 Free Software Foundation, Inc. */
7 #* Copyright (C) 2011 Guruprasad Rane */
8 #* */
9 #* This library is free software, licensed under the terms of the GNU */
10 #* General Public License as published by the Free Software Foundation, */
11 #* either version 3 of the License, or (at your option) any later version. */
12 #* You should have received a copy of the GNU General Public License */
13 #* along with this program. If not, see <http://www.gnu.org/licenses/>. */
14 #*****************************************************************************/
15 #
16 # txttoxml.py Version 0.1 Date:6th November 2011
17 # created by Guruprasad Rane <raneguruprasad@gmail.com>
18 # Description: Converts the txt file generated by dwgtotxt.lsp to xml
19 # Usage: ./txttoxml.py DWGtextfile.txt
20 # and a new xml file DWGtextfile.xml gets created
21
22 import sys
23 import os
24 import re
25
26 def processData(strTemp):
27 First_line = re.compile(r"\w{3}\s\w{3}\s\d+\s\d{1,2}:\d{1,2}:\d{1,2}\s\d{4}")
28 if re.search(First_line, strTemp):
29 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<DwgData>\n"
30
31 strTemp = strTemp.replace(";", "")
32 strTemp = strTemp.strip()
33 listTemp = strTemp.split(" = ")
34 if strTemp == "Property values:":
35 strTemp = "\" "
36
37 elif strTemp[:13] == "#<VLA-OBJECT ":
38 strTemp = strTemp.replace("#<VLA-OBJECT ", "type=\"")
39 strTemp = strTemp.replace(" ", "\" id=\"")
40 strTemp = strTemp.replace("type=\"", " type=\"")
41 strTemp = strTemp.replace(">", "\" desc=\"")
42
43 elif len(listTemp)==2:
44 strTemp1 = listTemp[0]
45 strTemp2 = listTemp[1]
46 strTemp1 = strTemp1.replace(" (RO)", "")
47 strTemp2 = strTemp2.replace("#<VLA-OBJECT ", "")
48 strTemp2 = strTemp2.replace(">", "")
49 strTemp2 = strTemp2.strip("\"")
50 strTemp = strTemp1 + "=\"" + strTemp2 + "\" "
51
52 elif strTemp == "/>":
53 strTemp = strTemp + "\n"
54
55 return strTemp
56
57 DwgTxtFileName = sys.argv[1]
58 if not os.path.exists(DwgTxtFileName):
59 sys.exit("File not found")
60 if len(sys.argv) > 1:
61 OutDir = sys.argv[2]
62 else:
63 OutDir = "."
64
65 Extension = DwgTxtFileName.split('.')
66 if Extension[-1] == 'txt':
67 XMLFileName = OutDir + "/" + DwgTxtFileName.rstrip('txt') + "xml"
68 else:
69 XMLFileName = OutDir + "/" + DwgTxtFileName + "xml"
70
71 if os.path.exists(DwgTxtFileName) and os.path.exists(OutDir):
72 FR = open(DwgTxtFileName,'r')
73 FW = open(XMLFileName,'w')
74 for Line in FR:
75 FW.write(processData(Line))
76 FW.write("/>\n</DwgData>")