Building and interactive Script: Closing and Re-Opening RFEM WebServices Connections #385
Replies: 5 comments 2 replies
-
Hey @MaximilianFranz , it seems that the problem is with recreating connection with RFEM. As far as I know |
Beta Was this translation helpful? Give feedback.
-
The script is roughly this: from RFEM.initModel import Model
from RFEM.Results.resultTables import ResultTables
from RFEM.enums import ObjectTypes
from RFEM.BasicObjects.member import Member
from RFEM.Tools.GetObjectNumbersByType import GetObjectNumbersByType
from RFEM.enums import CaseObjectType
import logging
load_case_no_string = input(
"Bitte die ID (Nummer) des Lastfalls Antwortspektrenverfahren "
"eingeben (Bsp: 6): "
)
load_case_no = int(load_case_no_string)
input(
"Bitte jetzt 'Umhüllende X' anwählen und "
"mit beliebiger Eingabe hier bestätigen: "
)
model = Model(new_model=False, model_name=rf6_model_name)
model.clientModel.service.begin_modification()
members_present = GetObjectNumbersByType(
ObjectTypes.E_OBJECT_TYPE_MEMBER, model
)
inner_forces_per_segment_guid = dict()
for member_idx in members_present:
member = Member.GetMember(member_idx, model)
member_inner_forces = ResultTables.SpectralAnalysisMembersInternalForces(
loading_type=CaseObjectType.E_OBJECT_TYPE_LOAD_CASE,
loading_no=load_case_no,
object_no=member_idx,
model=model,
)
segment_guid = member["comment"]
if segment_guid:
inner_forces_per_segment_guid[segment_guid] = member_inner_forces
else:
logging.warning(
f"Member {member_idx} has no segment-guid, so not "
f"reading results"
)
model.clientModel.service.finish_modification()
model.clientModel.service.close_connection()
input(
"Bitte jetzt 'Umhüllende Y' anwählen und "
"mit beliebiger Eingabe hier bestätigen: "
)
model = Model(new_model=False, model_name=rf6_model_name)
model.clientModel.service.begin_modification()
which in short with comments does this: model = Model(new_model=False, model_name=rf6_model_name)
model.clientModel.service.begin_modification()
# read stuff from the model
# ...
model.clientModel.service.finish_modification()
# close connection so RFEM becomes unlocked for interaction
model.clientModel.service.close_connection()
# re-connect to read more results
model = Model(new_model=False, model_name=rf6_model_name)
# This here fails, because clientModel is None apparently.
model.clientModel.service.begin_modification()
```
|
Beta Was this translation helpful? Give feedback.
-
Chiming in as I also seem to be having a similar issue where 'clientmodel' is somehow getting deleted. I'm not sure if close_connection is the culprit. I'm trying a simple script like below. If the model connects it runs okay, but when I run the script a second time I end up with a failure. model = Model(False, "myModel.rf6")
model.clientModel.service.begin_modification("new")
nodes = GetObjectNumbersByType(ObjectTypes.E_OBJECT_TYPE_NODE, model)
nodes = getAllNodeInds()
for node in nodes:
out = ResultTables.NodesSupportForces(CaseObjectType.E_OBJECT_TYPE_LOAD_COMBINATION, 9, int(node), model = model)
model.clientModel.service.finish_modification()
model.clientModel.service.close_connection() Error File c:\users\cslotboom\documents\rfem-testing\interaction.py:23
Model.clientModel.service.begin_modification("new")
AttributeError: 'NoneType' object has no attribute 'service'
|
Beta Was this translation helpful? Give feedback.
-
Hey all, the RFEM Python-Client saves its connection-details in Disclaimer: Most of the following code is copied from the import requests
from RFEM.initModel Import Model
from RFEM import connectionsGlobal
from RFEM.suds_requests import RequestsTransports
from suds.client import Client
model = Model(False, "myModel.rf6")
model.clientModel.service.begin_modification()
# first access to RFEM
model.clientModel.service.finish_modification()
model.clientModel.service.close_connection()
# nothing new so far
# ---
# prerequesites to establish a new connection (see RFEM.initModel.Model)
modelPath = connectionGlobals.client.service.get_active_model()
modelPort = modelPath[-5:-1]
modelUrlPort = connectionGlobals.url+':'+modelPort
modelCompletePath = modelUrlPort+'/wsdl'
# connectionGlobals.session = requests.Session() # not needed as already given in connectionGlobals
adapter = requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1)
connectionGlobals.session.mount('http://', adapter)
connectionGlobals.session.mount('https://', adapter)
trans = RequestsTransport(session = connectionGlobals.session)
cModel = Client(
url=f'{connectionGlobals.url}:{modelPort}/wsdl',
transport=trans,
location = f'{connectionGlobals.url}:{modelPort}',
cache=connectionGlobals.ca,
timeout=360,
)
model = Model
model.clientModel = cModel
model.clientModel['myModel']
# ---
# modification of the model as shown above
model.clientModel.service.begin_modification()
# second access to RFEM
model.clientModel.service.finish_modification()
model.clientModel.service.close_connection() Further information To reduce the lines of code and make sure my script closes the connection to RFEM a context-manager is useful: from contextlib import ContextDecorator
from RFEM.initModel import Model
from RFEM import connectionGlobals
from RFEM.suds_requests import RequestsTransport
from suds.client import Client
import requests
class connect_model(ContextDecorator):
def __init__(self, model_name:str):
self._model_name = model_name
def __enter__(self):
if connectionGlobals.connected:
modelPath = connectionGlobals.client.service.get_active_model()
modelPort = modelPath[-5:-1]
adapter = requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1)
connectionGlobals.session.mount('http://', adapter)
trans = RequestsTransport(connectionGlobals.session)
cModel = Client(
url=f'{connectionGlobals.url}:{modelPort}/wsdl',
transport=trans,
location = f'{connectionGlobals.url}:{modelPort}',
cache=connectionGlobals.ca,
timeout=360,
)
self._model = Model
self._model.clientModel = cModel
self._model.clientModelDct[self._model_name] = cModel
else:
self._model = Model(new_model=False, model_name=self._model_name)
print('Begin modification ...')
self._service.begin_modification()
return self._model
def __exit__(self, exc_type, exc, exc_tb):
print('Finished modification ... ')
self._service.finish_modification()
self._service.close_connection()
print('Disconnected from server')
@property
def _service(self):
return self._model.clientModel.service The usage is as follows. with connect_model(„model_name“) as model:
function_working_on_rfem(model, …) # <-- your personal function / script for modifying RFEM Including the code above into my code-base, allows me to connect for one or more times to RFEM by invoking the Hope this helps and still works. I am currently using version Kind regards |
Beta Was this translation helpful? Give feedback.
-
I found another way to resolve this:
So in order to use this i wrote the following code at the end of the script witch modifies RFEM Anyway the solution posted by @JohannesSchorr works very well and I will use it. I just modified this part, since no new calculations started
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Use-Case
Because of the problem mentioned here we need to interact with the RFEM UI to change some setting while running a script that retrieves the results.
I hence want a script that:
Problem
Trying to close the connection via:
and then opening a new one by creating another model:
leads to this bug:
The problem seems to be that
close_connection
deletes the clientModel instance on theModel
class.The behaviour here is very intransparent, so I don't know how I could correctly close and re-open a connection.
Beta Was this translation helpful? Give feedback.
All reactions