Colorbar macro test
Moderator: bernd
Forum rules
and Helpful information for the FEM forum
and Helpful information for the FEM forum
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 .
preview: Regards.
Sorry for my bad English.
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 .
preview: Regards.
Sorry for my bad English.
Ing. Gomez Lucio
Scope Ingenieria (scopeingenieria.com)
Scope Ingenieria (scopeingenieria.com)
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);
}
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)
in FreeCAD :
1 = 255
the color n° 185 red : in FreeCAD = (1.0 / 255.0 ) * 185 = 0.7254901960784313
mario
the value color is calculate :
red = 256 colors (0 to 255)
green = 256 colors (0 to 255)
blue = 256 colors (0 to 255)
in FreeCAD :
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)
>>>
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
My macros on Gist.github here complete macros Wiki and forum.
-
- Posts: 107
- Joined: Tue Feb 10, 2015 12:33 pm
- Location: General Pacheco (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?
Thanks in advance.
I'm trying to run the macro on Windows, with a model with results on screen I have this error, any advice?
Thanks in advance.
Engineering Consultor at 4P Ingeniería
http://www.4p-ingenieria.com/
https://www.youtube.com/channel/UClZPfO ... Cx-_J36o_Q
http://www.4p-ingenieria.com/
https://www.youtube.com/channel/UClZPfO ... Cx-_J36o_Q
Re: Colorbar macro test
hi
replace PyQt4 to PySide
mario
replace PyQt4 to PySide
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
My macros on Gist.github here complete macros Wiki and forum.
-
- Posts: 107
- Joined: Tue Feb 10, 2015 12:33 pm
- Location: General Pacheco (Buenos Aires) - Argentina
- Contact:
Re: Colorbar macro test
Thanks for the support. I have changed but now the new error is:
Engineering Consultor at 4P Ingeniería
http://www.4p-ingenieria.com/
https://www.youtube.com/channel/UClZPfO ... Cx-_J36o_Q
http://www.4p-ingenieria.com/
https://www.youtube.com/channel/UClZPfO ... Cx-_J36o_Q
Re: Colorbar macro test
attached a changed macro which works for me on win7 and FreeCAD 0.16Serchu wrote:Thanks for the support. I have changed but now the new error is:
Code: Select all
import sys
import FreeCAD
import FreeCADGui
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()
layout.addWidget(self.canvas)
self.setLayout(layout)
def draw_colormap(self):
sel = FreeCADGui.Selection.getSelection() # Selection
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
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,ColorMapWidget) # add the widget to the main window
- 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.
-
- Posts: 107
- Joined: Tue Feb 10, 2015 12:33 pm
- Location: General Pacheco (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.
Regards.
Engineering Consultor at 4P Ingeniería
http://www.4p-ingenieria.com/
https://www.youtube.com/channel/UClZPfO ... Cx-_J36o_Q
http://www.4p-ingenieria.com/
https://www.youtube.com/channel/UClZPfO ... Cx-_J36o_Q
Re: Colorbar macro test
Yeah runs without error for me as wellSerchu 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 ...
Re: Colorbar macro test
I get the following error trying to rum this macro ?
I think having a legend on your output is crucial.
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__
layout.addWidget(self.canvas)
<type 'exceptions.TypeError'>: 'PySide.QtGui.QBoxLayout.addWidget' called with wrong argument types:
PySide.QtGui.QBoxLayout.addWidget(FigureCanvasQTAgg)
Supported signatures:
PySide.QtGui.QBoxLayout.addWidget(PySide.QtGui.QWidget, int = 0, PySide.QtCore.Qt.Alignment = 0)