Skip to content

Commit

Permalink
Refactored file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
imirzov committed Jul 24, 2020
1 parent 13a6132 commit 2474673
Show file tree
Hide file tree
Showing 53 changed files with 333 additions and 139 deletions.
11 changes: 8 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.vscode
__pycache__
releases
.vscode/
__pycache__/
dummy
/releases/
/examples/*
!/examples/*.inp
!/examples/*.unv
!/examples/*.png
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
© Ihor Mirzov, May 2020
© Ihor Mirzov, July 2020
Distributed under GNU General Public License v3.0

<br/><br/>
Expand All @@ -7,7 +7,7 @@ Distributed under GNU General Public License v3.0

---

[Downloads](#downloads) |
[Downloads](https://github.com/calculix/unv2ccx/releases) |
[How to use](#how-to-use) |
[Screenshots](#screenshots) |
[Your help](#your-help) |
Expand All @@ -23,29 +23,23 @@ Distributed under GNU General Public License v3.0

Converts [Salome](https://www.salome-platform.org/) .unv file to [CalculiX](http://dhondt.de/) .inp format.

This converter is based on Joël's Cugnoni UNV parser available with [CalculiX Launcher](http://www.calculixforwin.com/) distribution. I allowed myself to slightely improve it and translate into Python 3. INPWriter's methods are fully refactored and now allow to convert beams. See folder [tests](./tests) for list of tested UNV elements. All generated INP files are processed by CalculiX GraphiX and [CalculiX CAE](https://github.com/calculix/cae) without any errors.
This converter is based on Joël's Cugnoni UNV parser available with [CalculiX Launcher](http://www.calculixforwin.com/) distribution. I allowed myself to slightely improve it and translate into Python 3. INPWriter's methods are fully refactored and now allow to convert beams. See folder [tests](./tests) for list of tested UNV elements. All generated INP files are processed by CalculiX GraphiX and [CalculiX Adanced Environment](https://github.com/calculix/cae) without any errors.

Thanks to [Niclas Stenberg](https://github.com/Xnst) for fixing bug with empty sets.

<br/><br/>



# Downloads
# How to use

Download Linux and Windows binaries from the [releases page](https://github.com/calculix/unv2ccx/releases). Binaries don't need to be installed.

<br/><br/>



# How to use

Result INP-file name is the same as UNV-file name. So only one argument should be passed to the converter:

in Linux: ./unv2ccx file.unv
in Windows: unv2ccx.exe file.unv
crossplatform: python3 unv2ccx.py file.unv
crossplatform: python3 ./src/unv2ccx.py file.unv

For the last command you'll need [Python 3](https://www.python.org/downloads/) to be installed on your OS. The main script is *unv2ccx.py*, it depends on *FEM.py*, *UNVParser.py* and *INPWriter.py* - other files you won't need.

Expand Down
Binary file added bin/unv2ccx
Binary file not shown.
Binary file added bin/unv2ccx.exe
Binary file not shown.
36 changes: 0 additions & 36 deletions clean.py

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 8 additions & 8 deletions make_release.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# -*- coding: utf-8 -*-

""" © Ihor Mirzov, September 2019
""" © Ihor Mirzov, July 2020
Distributed under GNU General Public License v3.0
Prepare binaries for publishing:
- python3 make_release.py
or 'Ctrl+F5' from VSCode """
Prepare binaries for publishing
Ctrl+F5 from VSCode to run """

import clean
import os
import shutil
import datetime
import PyInstaller.__main__
from src import clean

def copy(src, dst, skip):
for f in os.listdir(src):
Expand Down Expand Up @@ -48,7 +47,7 @@ def copy(src, dst, skip):

# Run pyinstaller to create binaries
args = [
'./unv2ccx.py',
'./src/unv2ccx.py',
'--workpath=' + TEMP, # temp dir
'-w', # no console during app run
'--onefile',
Expand All @@ -67,7 +66,8 @@ def copy(src, dst, skip):
lines = f.readlines()
for i in range(len(lines)):
skip += (lines[i].rstrip().lstrip('*'), )
skip += ('.git', '.gitignore', '.py', 'dist')
skip += ('.git', '.gitignore', '.py', '.vscode',
'bin', 'dist', 'releases', 'src', 'tests')

# Copy files and folders from sources to 'dist'
copy('.', 'dist', skip)
Expand All @@ -76,7 +76,7 @@ def copy(src, dst, skip):
if os.path.isfile(ARCH + '.zip'):
os.remove(ARCH + '.zip') # delete old

# Complress whole directory
# Compress whole directory
shutil.make_archive(ARCH, 'zip', 'dist')

# Remove unneeded files and folders
Expand Down
4 changes: 3 additions & 1 deletion FEM.py → src/FEM.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# © Joël Cugnoni, September 2006

""" © Joël Cugnoni, September 2006 """


# A simple FEM object structure
Expand Down
11 changes: 7 additions & 4 deletions INPWriter.py → src/INPWriter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Expand All @@ -8,13 +9,15 @@
Writes FEM nodes, elements and groups
(node and element sets) into INP file. """

import os, logging, re
import os
import logging
import re

PADDING = ' '*4 # four spaces

# Main function
def write(FEM, filename):

with open(filename[:-4]+'.inp', 'w') as f: # change extension .unv -> .inp
def write(FEM, file_name):
with open(file_name, 'w') as f:

# Nodes
f.write('*NODE, NSET=NALL\n')
Expand Down
5 changes: 4 additions & 1 deletion UNVParser.py → src/UNVParser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Expand All @@ -18,7 +19,9 @@
http://sdrl.uc.edu/sdrl/referenceinfo/universalfileformats/file-format-storehouse/universal-file-datasets-summary
"""

import logging, FEM
import logging

from . import FEM
FLAG = ' -1'


Expand Down
Empty file added src/__init__.py
Empty file.
59 changes: 59 additions & 0 deletions src/clean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

""" © Ihor Mirzov, January 2020
Distributed under GNU General Public License v3.0
Methods for cleaning up temporary/unused files/folders """

import os
import sys
import shutil


# Clean screen
def screen():
os.system('cls' if os.name=='nt' else 'clear')


# Recursively delete cached files in all subfolders
def cache(folder=None):
if not folder:
folder = os.getcwd()
pycache = os.path.join(folder, '__pycache__')
if os.path.isdir(pycache):
shutil.rmtree(pycache) # works in Linux as in Windows

# Recursively clear cache in child folders
for f in os.scandir(folder):
if f.is_dir():
cache(f.path)


# Cleaup trash files in startFolder and all subfolders
def files(startFolder=None):
extensions = ( '.12d', '.cvg', '.dat', '.vwf', '.out', '.nam', '.inp1', '.inp2',
'.sta', '.equ', '.eig', '.stm', '.mtx', '.net', '.inp0', '.rin',
'.fcv', 'dummy' )
if not startFolder:
startFolder = os.getcwd()
for f in os.scandir(startFolder):
if f.is_dir(): # if folder
files(f.path)
elif f.is_file() and f.name.endswith(extensions):
try:
os.remove(f.path)
sys.__stdout__.write('Delelted: ' + f.path + '\n')
except:
sys.__stdout__.write(f.path + ': ' + sys.exc_info()[1][1] + '\n')


# Cleaup old result files
def results():
extensions = ('.frd', '.vtk', '.vtu')
for f in os.scandir('.'):
if f.name.endswith(extensions):
try:
os.remove(f.path)
sys.__stdout__.write('Delelted: ' + f.path + '\n')
except:
sys.__stdout__.write(f.path + ': ' + sys.exc_info()[1][1] + '\n')
61 changes: 61 additions & 0 deletions src/unv2ccx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

""" © Ihor Mirzov, July 2020
Distributed under GNU General Public License v3.0
Converts UNV file from Salome to CalculiX INP mesh:
python3 ./src/unv2ccx.py ./examples/116.unv
Reads UNV_file, creates an internal FEM object,
then writes the INP_file. """

import os
import sys
import argparse
import logging

sys.path.append('.')
from src import clean
from src import UNVParser
from src import INPWriter


class Converter:

def __init__(self, unv_file_name):
self.unv_file_name = os.path.normpath(unv_file_name)
self.inp_file_name = self.unv_file_name[:-4]+'.inp'

def run(self):

# Parse UNV file
relpath = os.path.relpath(self.unv_file_name,
start=os.path.dirname(__file__))
logging.info('Parsing ' + relpath)
fem = UNVParser.UNVParser(self.unv_file_name).parse()

# Write INP file
relpath = os.path.relpath(self.inp_file_name,
start=os.path.dirname(__file__))
logging.info('Writing ' + relpath)
INPWriter.write(fem, self.inp_file_name)


if __name__ == '__main__':

# Configure logging
logging.basicConfig(level=logging.INFO,
format='%(levelname)s: %(message)s')

# Command line parameters
parser = argparse.ArgumentParser()
parser.add_argument('filename', type=str,
help='UNV file name with extension')
args = parser.parse_args()

# Create converter and run it
unv2ccx = Converter(args.filename)
unv2ccx.run()

clean.cache()
38 changes: 0 additions & 38 deletions tests.py

This file was deleted.

37 changes: 37 additions & 0 deletions tests/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

""" © Ihor Mirzov, June 2020
Distributed under GNU General Public License v3.0
Logging handler for all my projects """

import os
import sys
import logging


log_file = os.path.join(os.path.dirname(__file__), 'test.log')


# Configure logging to emit messages via 'print' method
class myHandler(logging.Handler):

def __init__(self):
super().__init__()
self.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))

# Remove old log file
if os.path.isfile(log_file):
os.remove(log_file)

def emit(self, LogRecord):
print(self.format(LogRecord))


# Redefine print method to write logs to file
def print(*args):
line = ' '.join([str(arg) for arg in args])
line = line.rstrip() + '\n'
with open(log_file, 'a') as f:
f.write(line)
sys.stdout.write(line)
Loading

0 comments on commit 2474673

Please sign in to comment.