-
Notifications
You must be signed in to change notification settings - Fork 4
/
initModel.py
928 lines (772 loc) · 33.5 KB
/
initModel.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
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
import os
import sys
import RSTAB.dependencies # dependency check ahead of imports
import socket
import ssl
import requests
import xmltodict
from urllib import request
from suds import WebFault
from suds.client import Client
from RSTAB.enums import ObjectTypes, ModelType, AddOn
from RSTAB.suds_requests import RequestsTransport
from suds.cache import DocumentCache
from tempfile import gettempdir
import time
from RSTAB import connectionGlobals
def connectToServer(url=connectionGlobals.url, port=connectionGlobals.port):
"""
Function for connecting to the server - code moved to function,
so it is not executed on import of the module
"""
# Check server port range set in "Program Options & Settings"
# By default range is set between 8081 ... 8089
if connectionGlobals.connected:
return
print('Connecting to server...')
# local machine url format: 'http://127.0.0.1'
urlAndPort = f'{url}:{port}'
# Parse the hostname from the URL
if url.startswith('https://'):
hostname = url[8:] # Remove 'https://'
context = ssl.create_default_context()
if isinstance(connectionGlobals.verify, str):
context.load_verify_locations(cafile=connectionGlobals.verify)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
a_socket = context.wrap_socket(sock, server_hostname=hostname)
new_wsdl = request.urlopen(urlAndPort+'/wsdl', context=context)
elif url.startswith('http://'):
hostname = url[7:] # Remove 'http://'
a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
new_wsdl = request.urlopen(urlAndPort+'/wsdl')
else:
hostname = url
a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
new_wsdl = request.urlopen(urlAndPort+'/wsdl')
location = (hostname, int(port))
# Check if port is listening
result_of_check = a_socket.connect_ex(location)
if result_of_check == 0:
a_socket.close()
else:
print(f'Error: Port {urlAndPort} is not open.')
print('Please check:')
print('- If you have started RSTAB application at the remote destination correctly.')
a_socket.close()
sys.exit()
# Delete old cache if the version or mode doesn't correlate
connectionGlobals.cacheLoc = os.path.join(gettempdir(), 'WSDL')
new_wsdl_data = new_wsdl.read()
new_wsdl.close()
new_tns = xmltodict.parse(new_wsdl_data)['definitions']['@targetNamespace']
if os.path.exists(connectionGlobals.cacheLoc):
for file in os.listdir(connectionGlobals.cacheLoc):
filePath = os.path.join(connectionGlobals.cacheLoc, file)
if file.endswith('.xml'):
with open(filePath,'r', encoding='utf-8') as old_wsdl:
old_wsdl_data = old_wsdl.read()
old_wsdl.close()
old_tns = xmltodict.parse(old_wsdl_data)['definitions']['@targetNamespace']
if new_tns != old_tns:
os.remove(filePath)
# Check for issues locally and remotely
try:
connectionGlobals.ca = DocumentCache(location=connectionGlobals.cacheLoc)
trans = RequestsTransport(
api_key=connectionGlobals.api_key,
session=connectionGlobals.session,
verify=connectionGlobals.verify
)
connectionGlobals.client = Client(urlAndPort+'/wsdl', location = urlAndPort, cache=connectionGlobals.ca, transport=trans)
connectionGlobals.connected = True
except Exception:
print('Error: Connection to server failed!')
print('Please check:')
print('- If you have started RSTAB application')
print('- If all RSTAB dialogs are closed')
print('- If server port range is set correctly')
print('- If you have a valid Web Services license')
print('- Check Program Options & Settings > Web Services')
print('On remote PC please check:')
print('- If the firewall enables you to listen to selected port.')
sys.exit()
try:
modelLst = connectionGlobals.client.service.get_model_list()
except Exception:
print('Error: Please check if all RSTAB dialogs are closed.')
input('Press Enter to exit...')
sys.exit()
# Persistent connection
# 'session' and 'trans'(port) enable Client to work within 1 session which is much faster to execute.
# Without it the session lasts only one request which results in poor performance.
# Assigning session to application Client (here client) instead of model Client
# results also in poor performance.
class Model():
clientModel = None
clientModelDct = {}
def __init__(self,
new_model: bool=True,
model_name: str="TestModel.rs9",
delete: bool=False,
delete_all: bool=False,
connect_to_server: bool=True):
"""
Class object representing individual model in RSTAB.
Class enables to edit multiple models in one session through holding
one transport session active by not setting 'trans' into Client.
Args:
new_model (bool, optional): Set to True if new model is requested.
If model is opened in RSTAB, FALSE should be used.
model_name (str, optional): Defaults to "TestModel". If "" call get_active_model.
delete (bool, optional): Delete results
delete_all (bool, optional): Delete all objects in Model.
"""
# This condition is here so there is backward compatibility for test etc.
# But it is possible now to connect to server in different place
# and then use Model(connect_toserver=False)
if connect_to_server:
connectToServer()
cModel = None
modelLst = []
modelVct = connectionGlobals.client.service.get_model_list()
if modelVct:
modelLst = modelVct.name
# The model suffix is omitted in modelLs, so it must be omitted in model_name to match exactly
original_model_name = model_name
if '.' in model_name:
model_name = model_name.split('.')[0]
if new_model:
# Requested new model but the model with given name is already connected
if model_name in self.clientModelDct:
cModel = self.clientModelDct[model_name]
# Asuming the existing model should be recycled everything have to be deleted,
# so the script won't add new objects on top of the old ones.
# Mainly used in cycles.
cModel.service.delete_all_results()
cModel.service.delete_all()
else:
modelPath = ''
# Requested new model, model with given name was NOT connected yet but file with the same name is opened in RSTAB
if model_name in modelLst:
id = 0
for i,j in enumerate(modelLst):
if modelLst[i] == model_name:
id = i
modelPath = connectionGlobals.client.service.get_model(id)
# Requested new model, model with given name DOESN'T exist yet
else:
# If name is empty, active will be selected
if model_name == "":
modelPath = connectionGlobals.client.service.get_active_model()
# If there is no nodel with given name, new RSTAB model will be created
else:
modelPath = connectionGlobals.client.service.new_model(original_model_name)
modelPort = modelPath[-5:-1]
modelUrlPort = connectionGlobals.url+':'+modelPort
modelCompletePath = modelUrlPort+'/wsdl'
connectionGlobals.session = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1)
connectionGlobals.session.mount('http://', adapter)
connectionGlobals.session.mount('https://', adapter)
trans = RequestsTransport(
api_key=connectionGlobals.api_key,
session = connectionGlobals.session,
verify=connectionGlobals.verify
)
cModel = Client(modelCompletePath, transport=trans, location = modelUrlPort, cache=connectionGlobals.ca, timeout=360)
self.clientModelDct[model_name] = cModel
else:
# Requested model is already opened in RSTAB or even connected in self.clientModelDct.
# In this statement RSTAB doesn't create new model in RSTAB via new_model().
# assert model_name in self.clientModelDct or model_name in modelLst, 'WARNING: '+model_name +' is not connected neither opened in RSTAB.'
# If model with same name is opened and alredy in clientModelDct.
# This is typicaly model created by RSTAB Python Client.
if model_name in self.clientModelDct:
cModel = self.clientModelDct[model_name]
# If opening new file.
# Model is opened in RSTAB (model in modelLst) but it is not in clientModelDct yet to be edited or closed.
elif model_name in modelLst:
id = 0
for i,j in enumerate(modelLst):
if modelLst[i] == model_name:
id = i
modelPath = connectionGlobals.client.service.get_model(id)
self.clientModelDct[model_name] = cModel
modelPort = modelPath[-5:-1]
modelUrlPort = connectionGlobals.url+':'+modelPort
modelCompletePath = modelUrlPort+'/wsdl'
connectionGlobals.session = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1)
connectionGlobals.session.mount('http://', adapter)
connectionGlobals.session.mount('https://', adapter)
trans = RequestsTransport(
api_key=connectionGlobals.api_key,
session = connectionGlobals.session,
verify=connectionGlobals.verify
)
cModel = Client(modelCompletePath, transport=trans, location = modelUrlPort, cache=connectionGlobals.ca, timeout=360)
elif model_name == "":
modelPath = connectionGlobals.client.service.get_active_model()
modelPort = modelPath[-5:-1]
modelUrlPort = connectionGlobals.url+':'+modelPort
modelCompletePath = modelUrlPort+'/wsdl'
connectionGlobals.session = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1)
connectionGlobals.session.mount('http://', adapter)
connectionGlobals.session.mount('https://', adapter)
trans = RequestsTransport(
api_key=connectionGlobals.api_key,
session = connectionGlobals.session,
verify=connectionGlobals.verify
)
cModel = Client(modelCompletePath, transport=trans, location = modelUrlPort, cache=connectionGlobals.ca, timeout=360)
else:
print('Model name "'+model_name+'" is not created in RSTAB. Consider changing new_model parameter in Model class from False to True.')
sys.exit()
if delete:
print('Deleting results...')
cModel.service.delete_all_results()
if delete_all:
print('Delete all...')
cModel.service.delete_all()
# when using multiple instances/model
self.clientModel = cModel
# when using only one instance/model
Model.clientModel = cModel
def __delete__(self, index_or_name):
'''
Purpose of this function is to facilitate removing client instances
from clientModelDct dictionary, which is held in Model for the purpose of
working with multiple models either created directly in RSTAB or opened from file.
Args:
index_or_name (str or int): Name of the index of model
'''
if isinstance(index_or_name, str) and index_or_name in self.clientModelDct:
assert index_or_name in list(self.clientModelDct)
self.clientModelDct.pop(index_or_name)
if len(self.clientModelDct) > 0:
model_key = list(self.clientModelDct)[-1]
self.clientModel = self.clientModelDct[model_key]
else:
self.clientModel = None
if isinstance(index_or_name, int):
assert index_or_name <= len(self.clientModelDct)
modelLs = connectionGlobals.client.service.get_model_list()
if modelLs and (modelLs.name[index_or_name] in self.clientModelDct):
self.clientModelDct.pop(modelLs.name[index_or_name])
if len(self.clientModelDct) > 0:
model_key = list(self.clientModelDct)[-1]
self.clientModel = self.clientModelDct[model_key]
else:
self.clientModel = None
def clearAttributes(obj):
'''
Clears object attributes.
Sets all attributes to None.
Use it whenever you create new (sub)object.
Args:
obj: object to clear
'''
# iterator
it = iter(obj)
for i in it:
obj[i[0]] = None
return obj
def deleteEmptyAttributes(obj):
from enum import Enum
'''
Delete all attributes that are None for better performance.
Args:
obj: object to clear
'''
it = [] # iterator
try:
it = iter(obj)
except:
ValueError('WARNING: Object feeded to deleteEmptyAttributes function is not iterable. It is type: '+str(type(obj))+'.')
for i in it:
if isinstance(i, str) or isinstance(i, int) or isinstance(i, float) or isinstance(i, bool) or isinstance(i, Enum) or not isinstance(i, tuple):
continue
if len(i) > 2:
i = deleteEmptyAttributes(i)
elif i[1] is None or i[1] == "":
delattr(obj, i[0])
elif isinstance(i[1], str) or isinstance(i[1], int) or isinstance(i[1], float) or isinstance(i[1], bool) or isinstance(i[1], Enum):
pass
else:
if isinstance(i, tuple):
i = list(i)
i[1] = deleteEmptyAttributes(i[1])
i = tuple(i)
else:
i[1] = deleteEmptyAttributes(i[1])
return obj
def openFile(model_path):
'''
Open file with a name.
This routine primarily adds client instance into
Model.clientModelDct which manages all connections to the models.
New Model class instance is invoked.
It should be used when opening a file.
Args:
model_path (str): Path to RSTAB9 model.
Returns:
model (client instance): RSTAB model instance
'''
assert os.path.exists(model_path)
file_name = os.path.basename(model_path)
connectToServer()
connectionGlobals.client.service.open_model(model_path)
return Model(False, file_name)
def closeModel(index_or_name, save_changes = False):
'''
Close any model connected to client with index or name.
Make sure to close the first open model last.
First model carries whole session (locking of the RSTAB).
Args:
index_or_name : Model Index or Name to be Close
save_changes (bool): Enable/Disable Save Changes Option
'''
connectToServer()
if isinstance(index_or_name, int):
Model.__delete__(Model, index_or_name)
connectionGlobals.client.service.close_model(index_or_name, save_changes)
elif isinstance(index_or_name, str):
if index_or_name[-4:] == '.rs9':
index_or_name = index_or_name[:-4]
modelLs = connectionGlobals.client.service.get_model_list().name
if index_or_name in modelLs:
try:
Model.__delete__(Model, index_or_name)
connectionGlobals.client.service.close_model(modelLs.index(index_or_name), save_changes)
except:
print('Model did NOT close properly.')
else:
print('\nINFO: Model "'+index_or_name+'" is not opened.')
else:
assert False, 'Parameter index_or_name must be int or string.'
def closeAllModels(save_changes = False):
'''
Function that closes all opened models in reverse order.
Args:
save_changes (bool): Enable/Disable Save Changes Option
'''
try:
modelLs = connectionGlobals.client.service.get_model_list().name
for j in reversed(modelLs):
closeModel(j, save_changes)
except:
print('No models opened.')
def saveFile(model_path):
'''
This function saves a model in a .rs9 file.
Args:
index_or_name : Model Index or Name to be Close
model_path: Path to RSTAB9 model.
'''
if model_path[len(model_path) - 4:len(model_path)].lower() != '.rs9':
model_path = model_path + '.rs9'
Model.clientModel.service.save(model_path)
def insertSpaces(lst: list):
'''
Add spaces between list of numbers.
Returns list of values.
'''
return ' '.join(str(item) for item in lst)
def Calculate_all(skipWarnings: bool = False, model = Model):
'''
Calculates model.
CAUTION: Don't use it in unit tests!
It works when executing tests individually but when running all of them
it causes RSTAB to stuck and generates failures, which are hard to investigate.
Args:
skipWarnings (bool): Warnings will be skipped
model (RSTAB Class, optional): Model to be edited
'''
from RSTAB.Tools.PlausibilityCheck import PlausibilityCheck
PlausibilityCheck()
calculationMessages = model.clientModel.service.calculate_all(skipWarnings)
return calculationMessages
def CalculateInCloud(machine_id, run_plausibility_check, calculate_despite_warnings_and_errors, email_notification, model = Model):
'''
Sends the current model to the defined server to be calculated in the cloud. Plausibility check before and email notification after the cloud calculation are optional.
CAUTION: Don't use it in unit tests!
It works when executing tests individually but when running all of them
it causes RSTAB to stuck and generates failures, which are hard to investigate.
Args:
machine_id (str): virtual machine ID (Dlu_1, F4s_v2, F8s_v2, F16s_v2, F32s_v2)
run_plausibility_check (bool): Activate/Deactivate plausibility check of model before cloud calculation is started
calculate_despite_warnings_and_errors (bool): Activate/Deactivate to start cloud calculation despite warnings and errors during plausibility check
email_notification (bool): Activate/Deactivate email notification about start and end of cloud calculation
'''
try:
cloudCalculationResult = model.clientModel.service.calculate_all_in_cloud(machine_id, run_plausibility_check, calculate_despite_warnings_and_errors, email_notification)
print("Cloud calculation was started.")
return cloudCalculationResult # list
except WebFault as e:
print(f"Caught exception: {e.fault.faultstring}")
def ConvertToDlString(s):
'''
The function converts strings commonly used in RSTAB so that they
can be used In WebServices. It solved issue #4.
Examples:
'1,3' -> '1 3'
'1, 3' -> '1 3'
'1-3' -> '1 2 3'
'1,3,5-9' -> '1 3 5 6 7 8 9'
Args:
s (str): RSTAB Common String
Returns a WS conform string.
'''
# Parameter is not of required type.
assert isinstance(s, (list, str))
if isinstance(s, list):
return ' '.join(map(str, s))
s = s.strip()
s = s.replace(',', ' ')
s = s.replace(' ', ' ')
lst = s.split(' ')
new_lst = []
for element in lst:
if '-' in element:
inLst = element.split('-')
start = int(inLst[0])
end = int(inLst[1])
inLst = []
for i in range(start, end + 1):
inLst.append(str(i))
inS = ' '.join(inLst)
new_lst.append(inS)
else:
new_lst.append(element)
return ' '.join(new_lst)
def ConvertStrToListOfInt(st):
"""
This function coverts string to list of integers.
Args:
st (str): RSTAB Common String
"""
st = ConvertToDlString(st)
lstInt = []
while st:
intNumber = 0
if ' ' in st:
idx = st.index(' ')
intNumber = int(st[:idx])
st = st[idx+1:]
else:
intNumber = int(st)
st = ''
lstInt.append(intNumber)
return lstInt
def CheckIfMethodOrTypeExists(modelClient, method_or_type, unitTestMode=False):
"""
Check if SOAP method or type is present in your version of RSTAB.
Use it only in your examples.
Unit tests except msg from SUDS where this is checked already.
Args:
modelClient (Model.clientModel)
method_or_type (str): Method or Type of SOAP Client
unitTestMode (bool): Unit Test Mode
Returns:
bool: Status of method or type.
Note:
To get list of methods invoke:
list_of_methods = [method for method in Model.clientModel.wsdl.services[0].ports[0]]
"""
assert modelClient, "WARNING: modelClient is not initialized."
if method_or_type not in str(modelClient):
if unitTestMode:
return True
else:
assert False, "WARNING: Used method/type: %s is not implemented in Web Services yet." % (method_or_type)
return not unitTestMode
def GetAllAddonStatuses(modelClient):
"""
Get statuses for all Addons.
Args:
modelClient (Model.clientModel)
Returns:
(dict): Addons with their statuses as values
"""
if modelClient is None:
print("WARNING: modelClient is not initialized.")
return False
addons = modelClient.service.get_addon_statuses()
dct = {}
for lstType in addons:
if not isinstance(lstType[1], bool) and len(lstType[1]) > 1:
addon = [lst for lst in lstType[1]]
for item in addon:
dct[str(item[0])] = bool(item[1])
elif isinstance(lstType[1], bool):
dct[str(lstType[0])] = bool(lstType[1])
else:
assert False
return dct
def GetAddonStatus(modelClient, addOn = AddOn.stress_analysis_active):
"""
Check if Add-on is reachable and active.
For some types of objects, specific Add-ons need to be enabled.
Args:
modelClient (Model.clientModel)
addOn (enum): AddOn Enumeration
Returns:
(bool): Status of Add-on
"""
dct = GetAllAddonStatuses(modelClient)
# sanity check
assert addOn.name in dct, f"WARNING: {addOn.name} Add-on can not be reached."
return dct[addOn.name]
def SetAddonStatus(modelClient, addOn = AddOn.stress_analysis_active, status = True):
"""
Activate or deactivate Add-on.
For some types of objects, specific Add-ons need to be enabled.
Args:
modelClient (Model.clientModel)
addOn (enum): AddOn Enumeration
status (bool): in/active
Analysis addOns list:
material_nonlinear_analysis_active
structure_stability_active
construction_stages_active
time_dependent_active
influence_lines_areas_active
form_finding_active
cutting_patterns_active
torsional_warping_active
cost_estimation_active
Design addOns list:
stress_analysis_active
concrete_design_active
steel_design_active
timber_design_active
aluminum_design_active
steel_joints_active
timber_joints_active
craneway_design_active
Dynamic addOns list:
modal_active
equivalent_lateral_forces_active
spectral_active
time_history_active
pushover_active
harmonic_response_active
Special aadOns list:
building_model_active
wind_simulation_active
tower_wizard_active
tower_equipment_wizard_active
piping_active
air_cushions_active
geotechnical_analysis_active
"""
# this will also provide sanity check
currentStatus = GetAddonStatus(modelClient, addOn)
if currentStatus != status:
addonLst = modelClient.service.get_addon_statuses()
if addOn.name in addonLst['__keylist__']:
addonLst[addOn.name] = status
else:
for listType in addonLst['__keylist__']:
if not isinstance(addonLst[listType], bool) and addOn.name in addonLst[listType]:
addonLst[listType][addOn.name] = status
modelClient.service.set_addon_statuses(addonLst)
def SetAddonStatuses(AddOnDict, model = Model):
"""
Set all or selected Add-on.
Args:
modelClient (Model.clientModel)
AddOnDict (dict): AddOn Dictionary
Returns:
(bool): If all Add-ons were set
"""
currentStatus = model.clientModel.service.get_addon_statuses()
for addon in currentStatus['__keylist__']:
if addon in AddOnDict:
currentStatus[addon] = AddOnDict[addon]
elif not isinstance(currentStatus[addon], bool):
for listType in currentStatus[addon]['__keylist__']:
if isinstance(currentStatus[addon][listType], bool) and listType in AddOnDict:
currentStatus[addon][listType] = AddOnDict[listType]
model.clientModel.service.set_addon_statuses(currentStatus)
def CalculateSelectedCases(loadCases: list = None, designSituations: list = None, loadCombinations: list = None,skipWarnings = True, model = Model):
'''
This method calculate just selected objects - load cases, designSituations, loadCombinations
Args:
loadCases (list, optional): Load Case List
designSituations (list, optional): Design Situations List
loadCombinations (list, optional): Load Combinations List
model (RSTAB Class, optional): Model to be edited
'''
specificObjectsToCalculate = model.clientModel.factory.create('ns0:calculate_specific_loadings')
if loadCases:
for loadCase in loadCases:
specificObjectsToCalculateLS = model.clientModel.factory.create('ns0:calculate_specific_loadings.loading')
specificObjectsToCalculateLS.no = loadCase
specificObjectsToCalculateLS.type = ObjectTypes.E_OBJECT_TYPE_LOAD_CASE.name
specificObjectsToCalculate.loading.append(specificObjectsToCalculateLS)
if designSituations:
for designSituation in designSituations:
specificObjectsToCalculateDS = model.clientModel.factory.create('ns0:calculate_specific_loadings.loading')
specificObjectsToCalculateDS.no = designSituation
specificObjectsToCalculateDS.type = ObjectTypes.E_OBJECT_TYPE_DESIGN_SITUATION.name
specificObjectsToCalculate.loading.append(specificObjectsToCalculateDS)
if loadCombinations:
for loadCombination in loadCombinations:
specificObjectsToCalculateCC = model.clientModel.factory.create('ns0:calculate_specific_loadings.loading')
specificObjectsToCalculateCC.no = loadCombination
specificObjectsToCalculateCC.type = ObjectTypes.E_OBJECT_TYPE_LOAD_COMBINATION.name
specificObjectsToCalculate.loading.append(specificObjectsToCalculateCC)
errors_and_warnings = []
calculationMessages = []
try:
calculationMessages = model.clientModel.service.calculate_specific(specificObjectsToCalculate, skipWarnings)
except Exception as exp:
errors_and_warnings = ["Calculation was unsuccessful: " + repr(exp)]
if calculationMessages["errors_and_warnings"] and calculationMessages["errors_and_warnings"]["message"]:
errors_and_warnings = ["".join([message.message_type,\
": Input field: ", message.input_field,\
", object: ", message.object,\
", current value: ", message.current_value,\
". Message: ", message.message]) if message.message_type == "ERROR"\
else "".join([message.message_type, ": ", message.message]) if not skipWarnings else None for message in calculationMessages["errors_and_warnings"]["message"]]
return errors_and_warnings
def FirstFreeIdNumber(memType = ObjectTypes.E_OBJECT_TYPE_MEMBER, parent_no: int = 0, model = Model):
'''
This method returns the next available Id Number for the selected object type
Args:
memType (enum): Object Type Enumeration
parent_no (int): Object Parent Number
Note:
(1) A geometric object has, in general, a parent_no = 0
(2) The parent_no parameter becomes significant for example with loads
model (RSTAB Class, optional): Model to be edited
'''
return model.clientModel.service.get_first_free_number(memType.name, parent_no)
def SetModelType(model_type = ModelType.E_MODEL_TYPE_3D, model = Model):
'''
This method sets the model type. The model type is E_MODEL_TYPE_3D by default.
Args:
model_type (enum): Modal Type Enumeration. The available model types are listed below.
ModelType.E_MODEL_TYPE_1D_X_3D
ModelType.E_MODEL_TYPE_1D_X_AXIAL
ModelType.E_MODEL_TYPE_2D_XY_3D
ModelType.E_MODEL_TYPE_2D_XY_PLATE
ModelType.E_MODEL_TYPE_2D_XZ_3D
ModelType.E_MODEL_TYPE_2D_XZ_PLANE_STRAIN
ModelType.E_MODEL_TYPE_2D_XZ_PLANE_STRESS
ModelType.E_MODEL_TYPE_3D
model (RSTAB Class, optional): Model to be edited
'''
model.clientModel.service.set_model_type(model_type.name)
def GetModelType(model = Model):
'''
The method returns a string of the current model type.
Args:
model (RSTAB Class): Model Instance
'''
return model.clientModel.service.get_model_type()
def NewModelAsCopy(old_model_name: str = '',
old_model_folder: str = ''):
'''
The method creates a new model as copy from an existing model
Args:
old_model_name (str): Old Model Name
old_model_folder (str): Old Model Folder
'''
# Old Model Name
new_model_name = ''
if '.rs9' in old_model_name:
new_model_name = old_model_name[:-4] + '_copy'
else:
assert TypeError('Model ' + old_model_name + ' does not exist')
old_model_path = os.path.join(old_model_folder, old_model_name)
# New Model Name
newModelAsCopy = connectionGlobals.client.service.new_model_as_copy(new_model_name, old_model_path)
return newModelAsCopy
def GetModelMainParameters(model = Model):
'''
The method returns the main parameters of the current model.
Args:
model (RSTAB Class, optional): Model to be edited
'''
# Client Model | Get Model Main Parameters
return model.clientModel.service.get_model_main_parameters()
def GetModelId(model = Model):
'''
This method returns model id as a string.
Args:
model (RSTAB Class, optional): Model to be edited
'''
# Client Model | Get Model ID
return model.clientModel.service.get_model_main_parameters().model_id
def GetModelParameters(model = Model):
'''
This method retuns the parameters of the current model.
Args:
model (RSTAB Class, optional): Model to be edited
'''
# Client Model | Get Model Parameters
return model.clientModel.service.get_model_parameters()
def GetModelSessionId(model = Model):
'''
This method returns model session id as a string.
Args:
model (RSTAB Class, optional): Model to be edited
'''
# Client Model | Get Session Id
return model.clientModel.service.get_session_id()
def GetName():
'''
This method returns app name as a string.
'''
# Client Application | Get Information
return connectionGlobals.client.service.get_information().name
def GetVersion():
'''
This method returns version as a string.
'''
# Client Application | Get Information
return connectionGlobals.client.service.get_information().version
def GetLanguage():
'''
This method returns language as a string.
'''
# Client Application | Get Information
return connectionGlobals.client.service.get_information().language_name
def GetAppSessionId():
'''
This method returns session id as a string.
'''
# Client Application | Get Session ID
return connectionGlobals.client.service.get_session_id()
def getPathToRunningRSTAB():
'''
Find the path to the directory where RSTAB is currently running.
This is helpful when using server version, because it can't process relative paths.
'''
import psutil
rfem6 = False
rfem6Server = False
path = ''
for p in psutil.process_iter(['name', 'exe']):
if p.info['name'] == 'RSTAB9.exe':
idx = p.info['exe'].find('bin')
path = p.info['exe'][:idx]
elif p.info['name'] == 'RSTAB9Server.exe':
idx = p.info['exe'].find('bin')
path = p.info['exe'][:idx]
elif p.info['name'] == 'RFEM6.exe':
rfem6 = True
elif p.info['name'] == 'RFEM6Server.exe':
rfem6Server = True
if rfem6 or rfem6Server:
raise ValueError('Careful! You are running RSTAB Python Client on RSTAB.')
if not path:
raise ValueError('Is it possible that RSTAB is not runnnning?')
return path
def GetListOfOpenedModels():
connectToServer()
models = connectionGlobals.client.service.get_model_list()
return models