Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions RFEM/Reports/printoutReport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from RFEM.initModel import Model

class PrintoutReport():
"""
Printout report class encopassing available printout report methods.

TODO: Create printout report US is paused US-8034.
"""

@staticmethod
def delete(id_list, model = Model):
"""
Delete printout report
"""
model.clientModel.service.delete_printout_reports(id_list)

@staticmethod
def exportToHTML(report_id: int, target_file_path: str, model = Model):
"""
Export printout report to a HTML.
"""
model.clientModel.service.export_printout_report_to_html(report_id, target_file_path)

@staticmethod
def exportToPDF(report_id: int, target_file_path: str, model = Model):
"""
Export printout report to a PDF.
"""
model.clientModel.service.export_printout_report_to_pdf(report_id, target_file_path)

@staticmethod
def getList(model = Model):
"""
Get list of printout reports.
"""
return model.clientModel.service.get_list_of_printout_reports()[0]

@staticmethod
def print(printout_report_id: int = 1, model = Model):
"""
Print printout report.
"""
model.clientModel.service.print_printout_report(printout_report_id)
145 changes: 97 additions & 48 deletions RFEM/initModel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
import RFEM.dependencies
import socket
Expand Down Expand Up @@ -66,8 +67,7 @@

class Model():
clientModel = None
clientModelLst = []
activeSession = False
clientModelDct = {}

def __init__(self,
new_model: bool=True,
Expand All @@ -87,56 +87,63 @@ def __init__(self,
"""

cModel = None
modelLs = client.service.get_model_list()
modelLst = []
modelVct = 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 '.rf6' in model_name:
model_name = model_name[:-4]

if new_model:
if modelLs and model_name in modelLs.name:
modelIndex = 0
for i,j in enumerate(modelLs.name):
if modelLs.name[i] == model_name:
modelIndex = i
modelPath = client.service.get_model(modelIndex)
modelPort = modelPath[-5:-1]
modelUrlPort = url+':'+modelPort
modelCompletePath = modelUrlPort+'/wsdl'

# Set transport parameter if it is the first model
if Model.activeSession:
cModel = Client(modelCompletePath, location = modelUrlPort)
else:
cModel = Client(modelCompletePath, transport=trans, location = modelUrlPort)
# Requested new model but the model with given name was already connected
if model_name in self.clientModelDct:
cModel = self.clientModelDct[model_name]
cModel.service.delete_all_results()
cModel.service.delete_all()

# Requested new model, model with given name DOESN'T exist yet
else:
modelPath = client.service.new_model(model_name)
modelPath = ''
# Requested new model, model with given name was NOT connected yet but file with the same name was opened
if model_name in modelLst:
id = 0
for i,j in enumerate(modelLst):
if modelLst[i] == model_name:
id = i
modelPath = client.service.get_model(id)
else:
modelPath = client.service.new_model(original_model_name)
modelPort = modelPath[-5:-1]
modelUrlPort = url+':'+modelPort
modelCompletePath = modelUrlPort+'/wsdl'

if Model.activeSession:
if self.clientModelDct:
cModel = Client(modelCompletePath, location = modelUrlPort)
else:
cModel = Client(modelCompletePath, transport=trans, location = modelUrlPort)
if not modelLs:
Model.activeSession = True
self.clientModelDct[model_name] = cModel

else:
modelIndex = 0
for i,j in enumerate(modelLs.name):
if modelLs.name[i] == model_name:
modelIndex = i
modelPath = client.service.get_model(modelIndex)
modelPort = modelPath[-5:-1]
modelUrlPort = url+':'+modelPort
modelCompletePath = modelUrlPort+'/wsdl'

if Model.activeSession:
cModel = Client(modelCompletePath, location = modelUrlPort)
# Requested model which was already connected
assert model_name in self.clientModelDct or model_name in modelLst, 'WARNING: '+model_name +'is not conected neither opened in RFEM.'

if model_name in self.clientModelDct:
cModel = self.clientModelDct[model_name]
else:
cModel = Client(modelCompletePath, transport=trans, location = modelUrlPort)
id = 0
for i,j in enumerate(modelLst):
if modelLst[i] == model_name:
id = i
modelPath = client.service.get_model(id)
modelPort = modelPath[-5:-1]
modelUrlPort = url+':'+modelPort
modelCompletePath = modelUrlPort+'/wsdl'
cModel = Client(modelCompletePath, location = modelUrlPort)
self.clientModelDct[model_name] = cModel

if delete:
print('Deleting results...')
cModel.service.delete_all_results()
Expand All @@ -146,19 +153,35 @@ def __init__(self,

# when using multiple intances/model
self.clientModel = cModel
if not modelLs or not model_name in modelLs.name:
Model.clientModelLst.append(cModel)
# when using only one instace/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 mustiple models either created directly in RFEM or opened from file.

def __delete__(self, index):
if len(self.clientModelLst) == 1:
self.clientModelLst.clear()
self.clientModel = None
else:
self.clientModelLst.pop(index)
self.clientModel = self.clientModelLst[-1]
Args:
index_or_name (str or int): Name of the index of model
'''
if isinstance(index_or_name, str):
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 = client.service.get_model_list()
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):
'''
Expand All @@ -175,23 +198,49 @@ def clearAttributes(obj):
obj[i[0]] = None
return obj

def openFile(model_path):
'''
Open file with a name.
This routine primarily adds client instance into
Model.clientModelLst 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 RFEM6 model.
Returns:
model (client instance): RFEM model instance
'''
assert os.path.exists(model_path)

file_name = os.path.basename(model_path)
client.service.open_model(model_path)
return Model(True, file_name)

def closeModel(index_or_name, save_changes = False):
"""
Close any model with index or name. Be sure to close the first created
model last (2,1, and then 0). 0 index carries whole session.
'''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor comment;

To be able to use openModel function, we do not have to create a session with Model() class. On the other hand to use the closeModel function first we need create the access with Model(False, modelName).
It'd be great if we mention it in the docStrings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

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 RFEM).

Args:
index_or_name : Model Index or Name to be Close
save_changes (bool): Enable/Diable Save Changes Option
"""
'''
if isinstance(index_or_name, int):
client.service.close_model(index_or_name, save_changes)
Model.__delete__(Model, index_or_name)
client.service.close_model(index_or_name, save_changes)

elif isinstance(index_or_name, str):
if '.rf6' in index_or_name:
index_or_name = index_or_name[:-4]

modelLs = client.service.get_model_list()
for i,j in enumerate(modelLs.name):
if modelLs.name[i] == index_or_name:
Model.__delete__(Model, index_or_name)
client.service.close_model(i, save_changes)
continue
else:
assert False, 'Parameter index_or_name must be int or string.'

Expand Down
Binary file added UnitTests/src/printout.rf6
Binary file not shown.
59 changes: 59 additions & 0 deletions UnitTests/test_Reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import sys
import os
PROJECT_ROOT = os.path.abspath(os.path.join(
os.path.dirname(__file__),
os.pardir)
)
sys.path.append(PROJECT_ROOT)
from RFEM.Reports.printoutReport import PrintoutReport
from RFEM.Reports.html import ExportResultTablesToHtml
from RFEM.initModel import Model, url, closeModel, openFile
from shutil import rmtree
import pytest

if Model.clientModel is None:
Model()

@pytest.mark.skipif(url != 'http://127.0.0.1', reason="This test fails on remote PC due to incorrect file path. \
Althought it is easy to change, it would not be easy to update on every remote computer.\
It is not necessary to evaluate Client as functional. Localy this tests still gets executed.")
def test_html_report():
Model.clientModel.service.delete_all()
Model.clientModel.service.run_script('..\\scripts\\internal\\Demos\\Demo-003 Castellated Beam.js')
Model.clientModel.service.calculate_all(False)

dirname = os.path.join(os.getcwd(), os.path.dirname(__file__))
folderPath = os.path.join(dirname, 'testResults')
# Remove any previous results if they exist
if os.path.isdir(folderPath):
rmtree(folderPath)
ExportResultTablesToHtml(folderPath)

assert os.path.exists(folderPath)

def test_printout_report():
# Remove any previous results if they exist
dirname = os.path.join(os.getcwd(), os.path.dirname(__file__))
folderPath = os.path.join(dirname, 'testResults')
if not os.path.isdir(folderPath):
os.mkdir(folderPath)

if os.path.exists(os.path.join(folderPath, 'printout.html')):
os.remove(os.path.join(folderPath, 'printout.html'))
if os.path.exists(os.path.join(folderPath, 'printout.pdf')):
os.remove(os.path.join(folderPath, 'printout.pdf'))
if os.path.isdir(os.path.join(folderPath, 'printout_data')):
rmtree(os.path.join(folderPath, 'printout_data'))

openFile(os.path.join(dirname, 'src', 'printout.rf6'))

PrintoutReport.delete(3)
assert len(PrintoutReport.getList()) == 2

PrintoutReport.exportToHTML(1, os.path.join(folderPath, 'printout.html'))
assert os.path.exists(os.path.join(folderPath, 'printout.html')) == True

PrintoutReport.exportToPDF(2, os.path.join(folderPath, 'printout.pdf'))
assert os.path.exists(os.path.join(folderPath, 'printout.pdf')) == True

closeModel(1)
4 changes: 1 addition & 3 deletions UnitTests/test_ResultTables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
os.pardir)
)
sys.path.append(PROJECT_ROOT)
from RFEM.initModel import Model, CheckIfMethodOrTypeExists
from RFEM.initModel import Model
from RFEM.enums import CaseObjectType
from RFEM.Results.resultTables import ResultTables, GetMaxValue, GetMinValue
import pytest

if Model.clientModel is None:
Model()

@pytest.mark.skipif(CheckIfMethodOrTypeExists(Model.clientModel, 'has_results', True), reason="has_results not in RFEM GM yet")
def test_result_tables():
Model.clientModel.service.delete_all()
Model.clientModel.service.run_script('..\\scripts\\internal\\Demos\\Demo-004 Bus Station-Concrete Design.js')
Expand Down
31 changes: 0 additions & 31 deletions UnitTests/test_htmlReport.py

This file was deleted.