## Colorbar macro test

About the development of the FEM module/workbench.

Moderator: bernd

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

### Colorbar macro test

I tried to create a small macro that uses numpy and matplotlib to show a colorbar with the values of Von Mises stress for a selected FEM analysis.
Not knowing how FreeCAD converts colors to the values of the results and not have too much time I could not make much progress in this macro . Anyway I share, if it is useful to someone .
FEM_ColorBAR.FCMacro.zip
It's not really a zip
preview:
FEM_ColorBAR.png (164.98 KiB) Viewed 1865 times
Regards.
Ing. Gomez Lucio
Scope Ingenieria (scopeingenieria.com)
PrzemoF
Posts: 2902
Joined: Fri Jul 25, 2014 4:52 pm
Contact:

### Re: Colorbar macro test

Looks nice! That's how FreeCAD calculates color (src/Mod/Fem/Gui/ViewProviderFemMeshPyImp.cpp):

Code: Select all

`````` App::Color calcColor(double value,double min, double max)
{
if (max < 0) max = 0;
if (min > 0) min = 0;

if (value < min)
return App::Color (0.0,0.0,1.0);
if (value > max)
return App::Color (1.0,0.0,0.0);
if (value == 0.0)
return App::Color (0.0,1.0,0.0);
if ( value > max/2.0 )
return App::Color (1.0,1-((value-(max/2.0)) / (max/2.0)),0.0);
if ( value > 0.0 )
return App::Color (value/(max/2.0),1.0,0.0) ;
if ( value < min/2.0 )
return App::Color (0.0,1-((value-(min/2.0)) / (min/2.0)),1.0);
if ( value < 0.0 )
return App::Color (0.0,1.0,value/(min/2.0)) ;
return App::Color (0,0,0);
}``````
mario52
Posts: 2925
Joined: Wed May 16, 2012 2:13 pm

### Re: Colorbar macro test

hi
the value color is calculate :

red = 256 colors (0 to 255)
green = 256 colors (0 to 255)
blue = 256 colors (0 to 255)

1 = 255

the color n° 185 red : in FreeCAD = (1.0 / 255.0 ) * 185 = 0.7254901960784313

Code: Select all

``````FreeCADGui.getDocument("Sans_nom").getObject("Box").ShapeColor = (0.7254902,0.0000000,0.0000000)
>>> ``````
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Dialog creation. My macros on Gist.github.
Serchu
Posts: 90
Joined: Tue Feb 10, 2015 12:33 pm
Location: Tigre (Buenos Aires) - Argentina
Contact:

### Re: Colorbar macro test

Hi,

I'm trying to run the macro on Windows, with a model with results on screen I have this error, any advice?
captura_027.png (3.19 KiB) Viewed 1784 times

mario52
Posts: 2925
Joined: Wed May 16, 2012 2:13 pm

### Re: Colorbar macro test

hi
replace PyQt4 to PySide
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Dialog creation. My macros on Gist.github.
Serchu
Posts: 90
Joined: Tue Feb 10, 2015 12:33 pm
Location: Tigre (Buenos Aires) - Argentina
Contact:

### Re: Colorbar macro test

Thanks for the support. I have changed but now the new error is:

captura_027.png (3.19 KiB) Viewed 1747 times
bernd
Posts: 9859
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

### Re: Colorbar macro test

Serchu wrote:Thanks for the support. I have changed but now the new error is:
attached a changed macro which works for me on win7 and FreeCAD 0.16

Code: Select all

``````import sys
from PySide import QtGui, QtCore
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

class ColorMap(QtGui.QWidget):
def __init__(self, parent=None):
super(ColorMap, self).__init__(parent)
##
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)

self.draw_colormap()

# set the layout
layout = QtGui.QVBoxLayout()
self.setLayout(layout)

def draw_colormap(self):
if not sel:
QtGui.QMessageBox.critical(None, "ColorMap macro", "An object has to be selected to run")
return
sel1=sel[0]
# Stess Values in MPa
datos=sel1.StressValues
val_max=max(datos)
val_min=min(datos)
# axes
ax = self.figure.add_axes([0.05, 0.10, 0.5, 0.8])
cmap = mpl.cm.jet
norm = mpl.colors.Normalize(vmin=val_min, vmax=val_max)
ticks_cm = np.linspace(val_min, val_max, 10, endpoint=True)
cb1 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
norm=norm,
ticks=ticks_cm,
orientation='vertical')
label_cm = 'Von Misses Stress [MPa]'
cb1.set_label(label=label_cm,weight='bold')
cb1.ax.tick_params(labelsize=16)
self.canvas.draw()

mw = FreeCADGui.getMainWindow()  # access the main window
ColorMapWidget = QtGui.QDockWidget() # create a new dockwidget
ColorMapWidget.setWidget(ColorMap()) # load the Ui script
``````
EDIT: Mhh, only if it is used the following way
- open the macro in edit modus
- select the resulsts in TreeView
- Switch to edit the Macro Document tab
- run the macro
- switch back to the FEM Document tab

EDIT2
- run analysis
- open results widget
- switch to Von Mises stress
- run the macro
- enjoy color tool bar
Last edited by bernd on Tue Sep 08, 2015 6:15 pm, edited 2 times in total.
Serchu
Posts: 90
Joined: Tue Feb 10, 2015 12:33 pm
Location: Tigre (Buenos Aires) - Argentina
Contact:

### Re: Colorbar macro test

Works for me also in Windows 7 64 bits, but what I did to put to work is select the results in the Freecad tree, then select V. Misses stress in the postprocessor windows and then run the macro (from an icon on a new toolbar).

Regards.
bernd
Posts: 9859
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

### Re: Colorbar macro test

Serchu wrote:...what I did to put to work is select the results in the Freecad tree, then select V. Misses stress in the postprocessor windows and then run the macro ...
Yeah runs without error for me as well
makkemal
Posts: 395
Joined: Wed Apr 29, 2015 12:41 pm
Location: South Africa
Contact:

### Re: Colorbar macro test

I get the following error trying to rum this macro ?

Code: Select all

``````Traceback (most recent call last):
File "/home/makke/freecadlegend.py", line 50, in <module>
ColorMapWidget.setWidget(ColorMap()) # load the Ui script
File "/home/makke/freecadlegend.py", line 21, in __init__