Colorbar macro test

About the development of the FEM module/workbench.

Moderator: bernd

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

Colorbar macro test

Post by psicofil »

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 :x . Anyway I share, if it is useful to someone :roll:.
FEM_ColorBAR.FCMacro.zip
It's not really a zip
(993 Bytes) Downloaded 151 times
preview:
FEM_ColorBAR.png
FEM_ColorBAR.png (164.98 KiB) Viewed 3620 times
Regards.
Sorry for my bad English.
Ing. Gomez Lucio
Scope Ingenieria (scopeingenieria.com)
User avatar
PrzemoF
Veteran
Posts: 3520
Joined: Fri Jul 25, 2014 4:52 pm
Contact:

Re: Colorbar macro test

Post by PrzemoF »

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
Veteran
Posts: 4698
Joined: Wed May 16, 2012 2:13 pm

Re: Colorbar macro test

Post by mario52 »

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

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, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
Serchu
Posts: 107
Joined: Tue Feb 10, 2015 12:33 pm
Location: General Pacheco (Buenos Aires) - Argentina
Contact:

Re: Colorbar macro test

Post by Serchu »

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
captura_027.png (3.19 KiB) Viewed 3539 times

Thanks in advance.
mario52
Veteran
Posts: 4698
Joined: Wed May 16, 2012 2:13 pm

Re: Colorbar macro test

Post by mario52 »

hi
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.
Serchu
Posts: 107
Joined: Tue Feb 10, 2015 12:33 pm
Location: General Pacheco (Buenos Aires) - Argentina
Contact:

Re: Colorbar macro test

Post by Serchu »

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

captura_027.png
captura_027.png (3.19 KiB) Viewed 3502 times
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Colorbar macro test

Post by bernd »

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
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
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: 107
Joined: Tue Feb 10, 2015 12:33 pm
Location: General Pacheco (Buenos Aires) - Argentina
Contact:

Re: Colorbar macro test

Post by Serchu »

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.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Colorbar macro test

Post by bernd »

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
User avatar
makkemal
Posts: 395
Joined: Wed Apr 29, 2015 12:41 pm
Location: South Africa
Contact:

Re: Colorbar macro test

Post by makkemal »

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__
    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)
I think having a legend on your output is crucial.
Post Reply