Macro Colorize

From FreeCAD Documentation
Revision as of 21:18, 10 December 2021 by TheMarkster (talk | contribs) (create Macro Colorize page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Other languages:

Colorize

Description
Quickly and easily set colors of individual faces, edges, and vertices, including transparency

Macro version: 1.0
Last modified: 2021-12-10
FreeCAD version: 0.19 and later
Download: ToolBar Icon
Author: TheMarkster
Author
TheMarkster
Download
ToolBar Icon
Links
Macro Version
1.0
Date last modified
2021-12-10
FreeCAD Version(s)
0.19 and later
Default shortcut
None
See also
None

Description

Allows to quickly and easily set the colors and transparency levels for individual faces, edges, and vertices. Usage: Select the subobjects and run the macro. The standard Qt color picker dialog will appear. Select the color and set the desired alpha (255 = fully opaque, 0 = fully transparent), click OK to close the dialog and use the color, or Cancel to cancel the macro. If more than one object is selected a different dialog will appear for each object.

Script

ToolBar Icon Macro_Colorize.FCMacro

# -*- coding: utf-8 -*-
#Colorize macro v1.0, 2021, by <TheMarkster> LGPL2.1 or later
#Usage: Select subobjects, run macro
#A color picker dialog will appear
#Select the color for the selected subobjects.
#The alpha channel corresponds to the transparency, but it
#is inverted since the default would be fully transparent
from PySide import QtGui,QtCore

def getIdx(sen):
    '''get subelement index from sub element name, e.g. from "Face6"  we get 5'''
    if 'Face' in sen or 'Edge' in sen or 'Wire' in sen:
        val = int(sen[4:])
    elif 'Vertex' in sen:
        val = int(sen[6:])
    return val-1

def buildDefaultColorArray(color, count):
    return[color] * count

def setColors():
    selx = Gui.Selection.getSelectionEx()
    for sel in selx:
        if sel.HasSubObjects:
            defaultColor = QtCore.Qt.gray if "Face" in sel.SubElementNames[0] else QtCore.Qt.black

            subs = sel.SubElementNames
            faces = [name for name in subs if "Face" in name]
            edges = [name for name in subs if "Edge" in name]
            vertices = [name for name in subs if "Vertex" in name]
            col = QtGui.QColorDialog.getColor(defaultColor,None,'Alpha = transparency, 0 = fully transparent, 255 = fully opaque',QtGui.QColorDialog.ShowAlphaChannel)
            if not col.isValid(): #user canceled
                return
            col = col.getRgbF()
            alpha = 1.0 - col[-1] #invert since default is fully transparent
            col = tuple(list(col[:3])+[alpha])
            if faces:
                dc = sel.Object.ViewObject.DiffuseColor
                if len(dc) == 1:
                    dc = buildDefaultColorArray(dc[0], len(sel.Object.Shape.Faces))
                indices = [getIdx(name) for name in faces]
                for idx in indices:
                    dc[idx] = col
                sel.Object.ViewObject.DiffuseColor = dc
            if edges:
                lca = sel.Object.ViewObject.LineColorArray
                if len(lca) == 1:
                    lca = buildDefaultColorArray(lca[0], len(sel.Object.Shape.Edges))
                indices = [getIdx(name) for name in edges]
                for idx in indices:
                    lca[idx] = col
                sel.Object.ViewObject.LineColorArray = lca

            if vertices:
                pca = sel.Object.ViewObject.PointColorArray
                if len(pca) == 1:
                    pca = buildDefaultColorArray(pca[0], len(sel.Object.Shape.Vertexes))
                indices = [getIdx(name) for name in vertices]
                for idx in indices:
                    pca[idx] = col
                sel.Object.ViewObject.PointColorArray = pca
        else:
            FreeCAD.Console.PrintError("No subobjects selected\n")

setColors()