-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathh2poly.py
More file actions
234 lines (196 loc) · 8.71 KB
/
h2poly.py
File metadata and controls
234 lines (196 loc) · 8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import os
import pathlib as pl
import keyboard
from copy import copy
import re
import time
import pyperclip
def printline(x,y):
if x: start = '\n'
else: start = ''
if y: end = '\n'
else: end = ''
print(start+'- - - - - - - - - - - - - - - - - - - - -'+end)
def extCoords(text):
if text.strip().startswith('at point'):
coordsRaw = re.findall('-?\d+\.?\d*',text)
x = str(round(float(coordsRaw[0])/scale,3))
y = str(round(float(coordsRaw[1])/scale,3))
return x + ' ' + y + ' \t'
else: return ''
def testrec(lines, i, mline):
if lines[i-1].strip().lower().startswith('at point') and lines[i-2].strip().lower().startswith('at point') and lines[i-3].strip().lower().startswith('at point') and lines[i-4].strip().lower().startswith('at point'):
reclines = lines[i-4:i]
x = []; y = []
for text in reclines:
coords = re.findall('-?\d+\.?\d*',text)
x.append(coords[0])
y.append(coords[1])
# Kontrol: to sæt ens koordinater
if not len([*set(x)]) == len([*set(y)]) == 2: return mline
# Kontrol: skiftende koordinater, som er ens
x.insert(0, x.pop()) # Forskyd liste x for at matche liste y
if (x[0] == x[-1] and y[0] == y[-1] and x[1] == x[2] and y[1] == y[2]) \
or (x[0] == x[1] and y[0] == y[1] and x[2] == x[3] and y[2] == y[3]):
# Der er nu en rektangel
mpos = mline.rfind('m \t')
keepline = mline[:mpos]
sCoords = mline[mpos:].split()[1:]
sbox = [sCoords[0], sCoords[1], str(round(float(sCoords[4]) - float(sCoords[0]),3)), str(round(float(sCoords[5]) - float(sCoords[1]),3))]
instext = ['s \t',' \t',' \t',' \t']
sline = ""
for (s,t) in zip(sbox,instext):
sline += t+s
return keepline + sline + ' \t'
else: return mline # Ikke et rekt
else: return mline # Ikke 'at point' i alle 4 linjer
def finish():
if input('- Run again? (Enter \'x\' to exit.) \n- ').lower() not in ['exit','x','n','no']:
return True
errorfile = 'If you see this message and Notepad does not close automatically, an error occured...\n'
errorfile += 'The objects are still copied, but if you paste them to HEAT2 now, the objects might not show in individuel lines. (Try it for yourself?)\n\n'
errorfile += '- - SOULUTION: - -\nMark all text in here, paste the code, mark it again and copy it. Now the code is ready for HEAT2.\n'
errorfile += 'Press and hold the CTRL key and press the following combination:\n\n'
errorfile += '(Ctrl) + A\n'
errorfile += '(Ctrl) + V\n'
errorfile += '(Ctrl) + A\n'
errorfile += '(Ctrl) + C\n\n'
errorfile += 'Then close Notepad. (No need to save the file.)\n'
errorfile += 'Sorry for inconvenience!'
### SCRIPT START ###
printline(1,0)
print('| - Script by Lasse Hamborg - 27.10.2022 -')
print('| ')
print('| This programme translates AutoCAD polylines to HEAT2-script lines.')
print('| ')
print('| - - - - -')
print('| ')
print('| First:\tRun the \'LIST\' command in AutoCAD on the polylines you want to translate.')
print('| ')
print('| Second:\tCopy the lines containing the polyline coordinates.')
print('| \t\tThis programme here takes the coordinates of the lines with \'at point...\' coordinates.')
print('| ')
print('| Third:\tIf multiple polylines are entered, the programme will make an object for each.')
print('| ')
print('| Lastly:\tThe new HEAT2 objects are copied to the clipboard via this programme.')
print('| \t\tNow simply, paste these directly into your HEAT2 script and assign materials.')
print('| \t\tVoila!...')
print('| \t\t(Psst: check the hint at the end of this programme for easier overview.)')
printline(0,1)
time.sleep(0.5)
### SCALE
scale = 1000
print('Scale factor is 1000 (i.e.: mm --> m).')
valg = input('If you want to change this, write a new scalefactor (number). Else, press ENTER:\n- ')
if len(valg) > 0 :
try:
scale = float(valg)
print('Scale factor changed to',scale)
except: print('Invalid number! Scale remains at', scale)
finally: time.sleep(0.5)
else: print('(Scale remains: ' + str(scale) + ')')
### LOOP IGENNEM 'LIST'
scriptOn = True
while scriptOn:
i = 0
lines = []
startline = 'm \t'
copyline = startline
matr = 'INSET_MATR_HERE'
morepolys = False
time.sleep(0.5)
print('\n- Paste polyline coordinates from AutoCAD (\'LIST\'): (Press ENTER (twice) when done)')
while True:
inputText = input('')
if inputText.strip().lower().startswith('press enter'): continue
copyline += extCoords(inputText)
lines.append(inputText)
if len(lines) > 2:
# Tests for multiple polygons:
if lines[i-2].strip().lower().startswith('at point') and lines[i].strip().lower().startswith('lwpoly'):
# print('hej')
copyline += matr + '\n' + startline
morepolys = True
# Script ends with two empty lines:
if len(lines[i-1]) < 1 and len(lines[i]) < 1:
break
# Tests for rectangle object:
if len(lines) > 5:
if len(lines[i]) < 1 and len(lines[i-5]) < 1:
copyline = testrec(lines, i, copyline)
i += 1
copyline += matr
# Tests for empty 'matr.-line' at the end:
delIdx = copyline.rfind(startline)
if copyline[delIdx:].startswith(startline+matr):
copyline = copyline[:delIdx-1]
# # # # # # PUT OBJECTS IN CLIPBOARD # # # # # #
if copyline == startline + matr:
print('- - - - - No coordinates found! - - - - -')
print('Use \'LIST\' in AutoCAD and copy all the coordinates (\"at point X=... Y=...\") into the script.')
time.sleep(1)
print('\nPlease, try again.')
printline(0,1)
time.sleep(1)
scriptOn = finish()
else:
pyperclip.copy(copyline)
print('- - - - DONE! - - - - -\n')
time.sleep(0.5)
print('This code for the object(s) is now copied to the clipboard:')
print(copyline)
### Tests for multiple objects:
if morepolys:
time.sleep(0.5)
print('\nWAIT! Pasting more polygons to HEAT2 is not perfect. Hence Notepad will now open to copy/paste into this.')
print('Please wait a few seconds.')
try:
time.sleep(0.5)
fw = open('dummy.txt',"w")
fw.write(errorfile)
fw.close()
### Use Notepad as pasting board:
os.startfile(pl.Path(str('dummy.txt')))
time.sleep(0.2) # If the errorfile message shows, and Notepad does not close again, increase this number (seconds).
keyboard.press_and_release('ctrl+a')
keyboard.press_and_release('ctrl+v')
keyboard.press_and_release('ctrl+a')
keyboard.press_and_release('ctrl+x')
keyboard.press_and_release('ctrl+w')
keyboard.press_and_release('alt+n')
print('\n- - Success! Now, paste it into your HEAT2 script! - -\n')
time.sleep(0.2)
os.remove('dummy.txt')
except:
time.sleep(5)
printline(1,0)
print('OBS! Notepad could not be opened!\n')
time.sleep(5)
print('Please, OPEN NOTEPAD manually, paste it and copy everything again from there (Ctrl + V -- Ctrl + A -- Ctrl + C).')
print('Now paste into your HEAT2 script!')
print('You can then just close Notepad again.\n')
printline(0,1)
time.sleep(30)
else:
time.sleep(0.5)
print('\n- - Paste it into your HEAT2 script! - -\n')
### END OF SCRIPT
time.sleep(1)
printline(0,0)
print('| - HINT! -')
print('| ')
print('| In HEAT, the material of an object is either entered at the end of the line or from the line above.')
print('| Thus, make a line above the object with the material in an \'empty\' object:')
print('| \'s 0 0 0 0 MATERIAL\'')
print('| (your object goes here)')
print('| ')
print('| In this way, you can more easily see the materials of complex objects.')
printline(0,1)
time.sleep(10)
scriptOn = finish()
print('\nGoodbye!\n')
try: os.remove('dummy.txt'); print('fil blev slettet')
except: 1+1
finally:
time.sleep(2)
quit()