-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoVestaDraw.py
212 lines (156 loc) · 7.51 KB
/
AutoVestaDraw.py
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
#!/usr/bin/env python
# coding: utf-8
__author__ = 'wankw ([email protected])'
import os
import pyautogui as pag
pag.FAILSAFE = False #PyAutoGUI fail-safe triggered from mouse moving to a corner of the screen.
import time
import win32gui
from shutil import copy
vesta_path = r'E:\Academic_Software\VESTA-win64\VESTA-win64\VESTA.exe'
button_image_path = r'E:\System\Desktop\AVD\button_image'
maxtrytime = 15
def open_vesta():
os.popen(f'{vesta_path}')
time.sleep(4)
handle = win32gui.FindWindow(None, 'VESTA')
left, top, right, bottom = win32gui.GetWindowRect(handle)
title_pos = (left+100, top+10)
#move to (0,0) upper left of main screen
pag.moveTo(title_pos)
pag.dragTo(0,0,0.5,button='left')
pag.hotkey('alt','space','x')
def click_button(button_figure,click=1):
time_start=time.time()
button = pag.locateOnScreen(button_figure, grayscale=True,confidence=0.9)
while not button:
button = pag.locateOnScreen(button_figure, grayscale=True,confidence=0.9)
time_end=time.time()
if time_end - time_start >= maxtrytime:
print (f'{os.path.basename(button_figure).replace(".png", "")} error, wrong VESTA version or UI!')
os._exit(0)
button_position = pag.center(button)
pag.click(button_position,clicks=click,interval=0.05)
def click_button_left(button_figure,click=1):
time_start=time.time()
button = pag.locateOnScreen(button_figure, grayscale=True,confidence=0.9)
while not button:
button = pag.locateOnScreen(button_figure, grayscale=True,confidence=0.9)
time_end=time.time()
if time_end - time_start >= maxtrytime:
print (f'{os.path.basename(button_figure).replace(".png", "")} error, wrong VESTA version or UI!')
os._exit(0)
pag.click((button[0], button[1]+button[3]/2),clicks=click,interval=0.05)
def export_raster_image(filename,tifname):
click_button(rf'{button_image_path}\zoom_in.png',click=7)
click_button_left(rf'{button_image_path}\space_filling.png')
#side view
click_button(rf'{button_image_path}\a.png')
click_button(rf'{button_image_path}\file.png')
click_button(rf'{button_image_path}\export_raster.png')
pag.hotkey('ctrl','a')
pag.typewrite(f'{tifname}-{filename}-side.tif')
click_button(rf'{button_image_path}\export_raster_save.png')
click_button_left(rf'{button_image_path}\export_raster_transparent.png')
click_button(rf'{button_image_path}\export_raster_ok.png')
pag.press(['enter'])
#top view
click_button(rf'{button_image_path}\c.png')
click_button(rf'{button_image_path}\file.png')
click_button(rf'{button_image_path}\export_raster.png')
pag.hotkey('ctrl','a')
pag.typewrite(f'{tifname}-{filename}-top.tif')
click_button(rf'{button_image_path}\export_raster_save.png')
click_button_left(rf'{button_image_path}\export_raster_transparent.png')
click_button(rf'{button_image_path}\export_raster_ok.png')
pag.press(['enter'])
pag.hotkey('ctrl','w')
def draw_vesta_figure(filename,tifname):
#export raster image
os.popen(f'{vesta_path} {filename}')
export_raster_image(filename,tifname)
print ('VESTA draw successfully!')
def draw_in_all_folders(path_list, filename):
ori_path = os.getcwd()
open_vesta()
for path in path_list:
os.chdir(path)
print (f'{path}: ',end='')
tifname = os.path.basename(path)
draw_vesta_figure(filename,tifname)
if not os.path.isfile(fr'{ori_path}\{tifname}-{filename}-side.tif'):
copy(fr'{path}\{tifname}-{filename}-side.tif',fr'{ori_path}\{tifname}-{filename}-side.tif')
else:
path_name = path.replace('\\',' ').replace(':', '')
copy(fr'{path}\{tifname}-{filename}-side.tif',fr'{ori_path}\{path_name}-{filename}-side.tif')
if not os.path.isfile(fr'{ori_path}\{tifname}-{filename}-top.tif'):
copy(fr'{path}\{tifname}-{filename}-top.tif',fr'{ori_path}\{tifname}-{filename}-top.tif')
else:
path_name = path.replace('\\',' ').replace(':', '')
copy(fr'{path}\{tifname}-{filename}-top.tif',fr'{ori_path}\{path_name}-{filename}-top.tif')
def get_path_list_from_file(dir_file,filename):
path_list = []
with open(dir_file) as file:
for line in file.readlines():
path = line.replace('\n','')
if os.path.isfile(fr'{path}\{filename}'):
path_list.append(path)
return path_list
# In[9]:
def get_all_folder_path_list(filename):
dir = os.getcwd()
path_list = []
for path, dir_list, file_list in os.walk(dir):
for dir_name in dir_list:
whole_path = os.path.join(path, dir_name)
if os.path.isfile(fr'{whole_path}\{filename}'):
path_list.append(whole_path)
return path_list
def get_subfolder_path_list(filename):
dir = os.getcwd()
path_list = []
for path in os.listdir(dir):
if os.path.isfile(fr'{path}\{filename}'):
whole_path = os.path.join(dir,path)
if os.path.isfile(fr'{whole_path}\{filename}'):
path_list.append(whole_path)
return path_list
def get_version():
return '1.0 (2020.11.26, [email protected])'
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Draw vaspcar file or cif file by VESTA automatically.',
epilog='Without [-df | -w | -l] option will only draw by the file in the current directory.')
parser.add_argument('-v', '--version', action='version', version=get_version(),help='Display version')
parser.add_argument('-f','--input_filename', type=str, action='store', default='CONTCAR',
help='The name of file you want to draw. [Optional] [default=CONTCAR] ')
#Add conflicted option.
group = parser.add_mutually_exclusive_group()
group.add_argument('-df','--dir_file', type=str, action='store',
help='Conflicts with -w -l option. The input file with the path list of folder you want to draw (One line writes one path). [Optional]')
group.add_argument('-w','--walk', action='store_true', default=False,
help='Conflicts with -df -l option. Traverse all folders (depth=max) in the current directory. [Optional] [default=False] ')
group.add_argument('-l','--loop_through', action='store_true', default=False,
help='Conflicts with -df -w option. Loop through all subfolders (depth=1) under the current folder. [Optional] [default=False] ')
args = parser.parse_args()
if args.dir_file != None:
print ('Directory file mode.')
path_list = get_path_list_from_file(args.dir_file,args.input_filename)
draw_in_all_folders(path_list,args.input_filename)
pag.hotkey('ctrl','w')
elif args.walk == True:
print ('Walk mode.')
path_list = get_all_folder_path_list(args.input_filename)
draw_in_all_folders(path_list,args.input_filename)
pag.hotkey('ctrl','w')
elif args.loop_through == True:
print ('Loop through mode.')
path_list = get_subfolder_path_list(args.input_filename)
draw_in_all_folders(path_list,args.input_filename)
pag.hotkey('ctrl','w')
else:
print ('Current directory mode.')
tifname = os.path.basename(os.getcwd())
open_vesta()
draw_vesta_figure(args.input_filename,tifname)
pag.hotkey('ctrl','w')