Macro to export Calculix Files

About the development of the FEM module/workbench.

Moderator: bernd

Post Reply
User avatar
psicofil
Posts: 154
Joined: Thu Dec 15, 2011 11:02 pm

Macro to export Calculix Files

Post by psicofil »

Hello everyone. I share a simple macro to export files created by the FEM module.
The idea is to be able to continue working with temporary files Calculix outside of FreeCAD. This way you can work the .inp file, or display the results of calculations in other post-programs such as CGX, CandyStore, or the best open source software for post-processing Paraview ;).

I share a video of the macro: http://youtu.be/_DdT7Wxk4yw

The macro:

Code: Select all

import os
import FreeCADGui
import FreeCAD
import shutil
from PySide import QtGui

### START OF MACRO ###

# Mehcanical Analysis Select
sel = FreeCADGui.Selection.getSelection() # Selection
sel1=sel[0]

# Save folder select
dialog = QtGui.QFileDialog.getExistingDirectory()
destiny_folder = str(dialog) 

# Proceed
if sel1.TypeId == 'Fem::FemAnalysisPython':
  try:
     dir1 = sel1.Document.TransientDir #Temporaly Directoy
     nam_fold = sel1.Uid[32:] # Analysis temporaly fold name
     # Analysis Final Directory 
     direc = str(dir1 + '/FemAnl_' + nam_fold + '/')
     calculix_files = os.listdir(direc)
     for files in calculix_files:
          shutil.copy(direc + files,destiny_folder)
     FreeCAD.Console.PrintMessage('Mechanical Analysis files save in' + destiny_folder)
  except:
     FreeCAD.Console.PrintError('Sorry but none temporaly file exists')
else:
  FreeCAD.Console.PrintError('Error in Selection: Select a correct Mechanical Analysis')
ExportFem.fcmacro.zip
Remember it is not a .zip, just change the name to upload to the forum
(1.05 KiB) Downloaded 1133 times
Sorry for my bad English
regards
Ing. Gomez Lucio
Scope Ingenieria (scopeingenieria.com)
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Macro to export Calculix Files

Post by microelly2 »

I have added the macro to the FreeCAD-Macros
and to the pluginmanager

after some tests I will add paraview too.
Serchu
Posts: 107
Joined: Tue Feb 10, 2015 12:33 pm
Location: General Pacheco (Buenos Aires) - Argentina
Contact:

Re: Macro to export Calculix Files

Post by Serchu »

And what about now that ccx files are stored in a user selected folder? Is there a way to update the macro to open the frd file from where is stored?

Regards!
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Macro to export Calculix Files

Post by bernd »

Serchu wrote:And what about now that ccx files are stored in a user selected folder? Is there a way to update the macro to open the frd file from where is stored?
Here is what I use to postprocess the results in CGX. On Windows SciTE ( http://www.bconverged.com/calculix/doc/index.html ) has to be installed and the *.int has to be accociated with SciTE. The macro wil open the inputfile in SciTE and in SciTE jus press "Shift+F10"

Code: Select all

## CGX PostProccesing in FreeCAD
# Linux --> cgx command --> cgx
# Windows --> SciTE is installed and *.inp opens with SciTE

import FreeCADGui
import FreeCAD

selection = FreeCADGui.Selection.getSelection() 
if len(selection) == 1:
    sel = selection[0]
    # print '\nSelected Object: ' ,sel.Name
    if hasattr(sel,"Proxy") and sel.Proxy.Type == 'FemAnalysis':
        # print sel.Name
        analysis = sel
        for m in FreeCAD.activeDocument().MechanicalAnalysis.Member:
            if m.isDerivedFrom("Fem::FemMeshObject"):
                filename = m.Name
                break
        else: # for loop runs through all members but no mesh found 
            print 'No Mesh --> No Output'
        from platform import system
        if system() == "Linux":
            if analysis.OutputDir == '':
                ccxoutputdir = '/tmp/'   # std output on Linux if OutputDir is left blank
            else:
                ccxoutputdir = analysis.OutputDir
            filename = ccxoutputdir + filename + '.frd'
            cgxcommand = 'cgx ' + filename
            print cgxcommand
            import subprocess
            try:
                output = subprocess.check_output([cgxcommand, '-1'], shell = True, stderr=subprocess.STDOUT,)
            except:
                FreeCAD.Console.PrintError('Error!!, sorry..')
        elif system() == "Windows":
            if analysis.OutputDir == '':
                ccxoutputdir = 'C:/tmp/'   # std output on Windows if OutputDir is left blank
            else:
                ccxoutputdir = analysis.OutputDir
                print 'The OutputDir of the MechanicalAnalysis was changed. The macro may fail to work!'
            filename = ccxoutputdir + filename + '.inp'
            print filename
            # the webbrowser.open() does not work directly on *.frd --> *.inp are used to open SciTE
            # in windows inp-file extension has to be assigned to open SciTE on double click (works if SciTE is installed)
            # The OutputDir of the MechanicalAnalysis should left blank --> C:/tmp is used
            print 'In SciTE --> Menue Tools --> Post Process or Shift+F10'
            import webbrowser
            webbrowser.open(filename)
        else:
            print "Your OS is ", system(), ", which is not yet supported !"
    else:
        print 'Select a MechanicalAnalysis Object'
else:
	print 'Select one MechanicalAnalysis Object'
Serchu
Posts: 107
Joined: Tue Feb 10, 2015 12:33 pm
Location: General Pacheco (Buenos Aires) - Argentina
Contact:

Re: Macro to export Calculix Files

Post by Serchu »

Bernd, thanks for sharing. Guess that I make a mistake, I ask for opening the file in CGX in a post related to copy/move the result folder. By the way, seeing that there are some issues with the postprocessing in FreeCAD, a macro that directly open the results file in CGX would be very helpfull in this moment (as we could map to a new icon on a customized toolbar and get the results quickly).

In mi understanding, the results folder and name are stored in some variable on the FreeCAD file (as he can "remember" were they are when we close and open the file), so would be a matter of editing the macro to open this file. Somebody knows whats the variable name of the "selected analysis"??? I could try to edit the macro, but need this variable name.

Regards!
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Macro to export Calculix Files

Post by bernd »

Serchu wrote:Bernd, thanks for sharing. Guess that I make a mistake, I ask for opening the file in CGX in a post related to copy/move the result folder.
My mistake. I was confused with this thread viewtopic.php?f=18&t=10789
Serchu wrote:… In mi understanding, the results folder and name are stored in some variable on the FreeCAD file (as he can "remember" were they are when we close and open the file),
FreeCAD does not display the contens of the frd file. FreeCAD imports the frd data and saves the data in an own data structure in the result object in the dcument !
Serchu wrote: so would be a matter of editing the macro to open this file. Somebody knows whats the variable name of the "selected analysis"??? I could try to edit the macro, but need this variable name.
Input and thus the output file name are defined here:
https://github.com/FreeCAD/FreeCAD/blob ... ter.py#L27

If the output directory of the analysis is not set the files are stored in /tmp or C:\tmp The name will be the name of the mesh object. See my macro to open the cgx how the name is retrieved.
Post Reply