CGX in FreeCAD little code

About the development of the FEM module/workbench.

Moderator: bernd

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

CGX in FreeCAD little code

Post by psicofil »

Hi all, I was trying to visualize cgx Calculix results.
I wrote a small macro for use in FreeCAD, basically what it does is look .frd temporary file and opens with CGX.

Code: Select all

## CGX in FreeCAD

import ImportGui
import Fem
import subprocess,tempfile
import FreeCADGui

########### INPUTS #############

nam_mesh = 'NAME OF MESH'

########### MACRO #############
sel = FreeCADGui.Selection.getSelection()
sel1=sel[0]

dir1 = sel1.Document.TransientDir #Directorio
nam_fold = sel1.Uid[32:]

# Directorio Final
direc = str(dir1 + '/FemAnl_' + nam_fold)
frd_file = direc + '/' + nam_mesh +'.frd'

## Command
command = 'cgx_2.8 -v ' + frd_file
output = subprocess.check_output([command, '-1'], shell = True, stderr=subprocess.STDOUT,)
FreeCAD.Console.PrintMessage(output)

The macro problem is that it only works when the FEM file is created and FRD are available in the system temporary folder.

I made a little video of how it works and take to show how you can perform a frequency analysis in the FEM module freecad, although we can not see the results in the same.
A binary cgx 2.8 for linux you can find it here: http://www.dhondt.de/cgx_2.8.bz2

video demonstration: https://youtu.be/dBTtkjjL5kU

Sorry for my bad English
Ing. Gomez Lucio
Scope Ingenieria (scopeingenieria.com)
User avatar
psicofil
Posts: 154
Joined: Thu Dec 15, 2011 11:02 pm

Re: CGX in FreeCAD little code

Post by psicofil »

I've updated the macro correcting several errors and improving the writing of the code

Code: Select all

## CGX PostProccesing in FreeCAD

import os
import Fem
import subprocess,tempfile
import FreeCADGui
import FreeCAD

######## INPUT CGX binary ########

cgx_bin = '/usr/bin/cgx_2.8'  # CGX binary

########### MACRO #############

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

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)
    files = os.listdir(direc)
    for i in files:
      [name,extension]=os.path.splitext(i)
      if (extension == ".frd"):
        frd_file=name+extension
  except:
      FreeCAD.Console.PrintError('None temporaly file exists')
  try:
    ## Command
    command = cgx_bin + ' -v ' + direc + '/' +frd_file
    output = subprocess.check_output([command, '-1'], shell = True, stderr=subprocess.STDOUT,)
    FreeCAD.Console.PrintMessage(output)
  except:
    FreeCAD.Console.PrintError('CGX binary problems')
else:
  FreeCAD.Console.PrintError('Error in Selection: Select a correct Fem Analysis') 
Also leave available the macro to download it
CGX FEM.FCMacro.zip
Remember it is not a .zip, just change the name to upload to the forum
(1.11 KiB) Downloaded 174 times
regards
sorry for my bad English
Ing. Gomez Lucio
Scope Ingenieria (scopeingenieria.com)
HoWil
Veteran
Posts: 1279
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: CGX in FreeCAD little code

Post by HoWil »

Hallo psicofil,
I want to use your macro, thats why I ask if you somehow improved updated it in the meantime?
thx
User avatar
psicofil
Posts: 154
Joined: Thu Dec 15, 2011 11:02 pm

Re: CGX in FreeCAD little code

Post by psicofil »

HoWil wrote: you somehow improved updated it in the meantime
Hello. I do not think the macro can be updated. But I created a new macro with which you can export all Calculix files created by FreeCAD to a new folder. Then you can open the results file (.frd) with CGX or CandyStore. You can also use CandyStore to export the file ".frd" to a VTK file (.vtu) and use Paraview for post-processing of data.
Sorry for my bad English.
Regards

New Macro: viewtopic.php?f=18&t=11455
Ing. Gomez Lucio
Scope Ingenieria (scopeingenieria.com)
Serchu
Posts: 107
Joined: Tue Feb 10, 2015 12:33 pm
Location: General Pacheco (Buenos Aires) - Argentina
Contact:

Re: CGX in FreeCAD little code

Post by Serchu »

Hi, is there a way to update the macro to open the files stored in some specific folder? I meant, now we have the option to specify a folder for the results, so the local temp is not always the place to look for them.

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

Re: CGX in FreeCAD little code

Post by psicofil »

Hello. I modified the macro by Bernd posted here: viewtopic.php?f=18&t=11455.

I tried Ubuntu / linux and it works. I have not tested on windows.

Bernd:
I made the following changes
- A line to set the binary address cgx
- The "-v" option in the command on linux to see the results in cgx
- More message or information errors are now also shown in a window using QBoxMessage.

The macro:

Code: Select all

## CGX PostProccesing in FreeCAD

########### CONFIGURATION (Linux) ######

cgx_bin = '/usr/bin/cgx' # CGX binary

########### START MACRO #############

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

import FreeCADGui
import FreeCAD
from PySide import QtCore, QtGui

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_bin +'  -v ' + filename
            print cgxcommand
            import subprocess
            try:
                output = subprocess.check_output([cgxcommand, '-1'], shell = True, stderr=subprocess.STDOUT,)
            except:
                FreeCAD.Console.PrintError('Error!!, sorry..')
                QtGui.QMessageBox.critical(None,'Error','Error!!, Sorry.Please report the error in the FreeCAD forum',QtGui.QMessageBox.Abort)
        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!'
                QtGui.QMessageBox.critical(None,'Error','The OutputDir of the MechanicalAnalysis was changed. The macro may fail to work!',QtGui.QMessageBox.Abort)
            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'
            QtGui.QMessageBox.information(None,'Information','In SciTE --> Menue Tools --> Post Process or Shift+F10',QtGui.QMessageBox.Abort)
            import webbrowser
            webbrowser.open(filename)
        else:
            print "Your OS is ", system(), ", which is not yet supported !"
            QtGui.QMessageBox.critical(None,'Error',"Your OS is ", system(), ", which is not yet supported !",QtGui.QMessageBox.Abort)
    else:
        print 'Select a MechanicalAnalysis Object'
        QtGui.QMessageBox.information(None,'Information','Select a MechanicalAnalysis Object',QtGui.QMessageBox.Abort)
else:
   print 'Select one MechanicalAnalysis Object'
   QtGui.QMessageBox.information(None,'Information','Select a MechanicalAnalysis Object',QtGui.QMessageBox.Abort)
Sorry for my bad English.
regards
Ing. Gomez Lucio
Scope Ingenieria (scopeingenieria.com)
Serchu
Posts: 107
Joined: Tue Feb 10, 2015 12:33 pm
Location: General Pacheco (Buenos Aires) - Argentina
Contact:

Re: CGX in FreeCAD little code

Post by Serchu »

Hi,

I have tried to run on Windows 64 bits and it doesn't work, it's open an Internet Explorer window (!). Following the code, there are two lines that I guess that are the key to the problem, the last ones of the two cited

Code: Select all

            print 'In SciTE --> Menue Tools --> Post Process or Shift+F10'
            QtGui.QMessageBox.information(None,'Information','In SciTE --> Menue Tools --> Post Process or Shift+F10',QtGui.QMessageBox.Abort)
            import webbrowser
            webbrowser.open(filename)
How could I change to specificly run a program (cgx) with the apropiated parameters (filename in this case)????
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: CGX in FreeCAD little code

Post by bernd »

Serchu wrote:Hi,

I have tried to run on Windows 64 bits and it doesn't work, it's open an Internet Explorer window (!). Following the code, there are two lines that I guess that are the key to the problem, the last ones of the two cited

Code: Select all

            print 'In SciTE --> Menue Tools --> Post Process or Shift+F10'
            QtGui.QMessageBox.information(None,'Information','In SciTE --> Menue Tools --> Post Process or Shift+F10',QtGui.QMessageBox.Abort)
            import webbrowser
            webbrowser.open(filename)
How could I change to specificly run a program (cgx) with the apropiated parameters (filename in this case)????
It works for me in Win7

What does happen if you double click on a *.inp file in Microsoft File Explorer ? It should open SciTE environment ! If it works the webbrowser.open(filename) will open SciTE too. This is a simple workaround which opens a filename with the asociated binary. If there is no asociated binary it uses the webbrowser.

In SciTE you can easily open cgx.
Serchu
Posts: 107
Joined: Tue Feb 10, 2015 12:33 pm
Location: General Pacheco (Buenos Aires) - Argentina
Contact:

Re: CGX in FreeCAD little code

Post by Serchu »

Thanks for the feedback. I'm trying to avoid SciTE, I want to postprocess directly in CGX. In my installation, if I double clic an inp file it's open Notepad.exe.

I'm working saving my results in specific folders, could it be that the cause of the error?
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: CGX in FreeCAD little code

Post by bernd »

The only binary of cgx I have is the one of bConvergered. I havn't been able to directly opens a frd in cgx from FreeCAD Python console on Windows. That is why I open SciTE and from SciTE cgx. To be honest I did not try hard since the workaround works.

If you have a better solution I'd be interested as well.
Post Reply