-
Notifications
You must be signed in to change notification settings - Fork 34
OndrejMichal: Printout Report #193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import os | ||
| import sys | ||
| import RFEM.dependencies | ||
| import socket | ||
|
|
@@ -66,8 +67,7 @@ | |
|
|
||
| class Model(): | ||
| clientModel = None | ||
| clientModelLst = [] | ||
| activeSession = False | ||
| clientModelDct = {} | ||
|
|
||
| def __init__(self, | ||
| new_model: bool=True, | ||
|
|
@@ -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() | ||
|
|
@@ -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): | ||
| ''' | ||
|
|
@@ -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. | ||
| ''' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.' | ||
|
|
||
|
|
||
| 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) |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.