VBA macro to apply appearance (color) to a component?

I’m looking for a helper method in VBA or C# that sets an appearance to a component. I’m interested in applying a color to a selected component.

Any suggestions would be helpful!

For VBA look at this example

For C# you can see how it is implemented in the xCAD.NET:

And here is integration/unit test for usage:

1 Like

Thanks @artem! So this can be summarized to this:

 public static void SetColor(this Component2 component, Color color)
        {
            SetColor(color, null,
                (m, o, c) => component.SetMaterialPropertyValues2(m, (int)o, c),
                (o, c) => component.RemoveMaterialProperty2((int)o, c));
        }
        internal static void GetColorScope(IComponent2 comp, out swInConfigurationOpts_e confOpts, out string[] confs)
        {
            confOpts = comp != null
                ? swInConfigurationOpts_e.swSpecifyConfiguration
                : swInConfigurationOpts_e.swThisConfiguration;

            confs = comp != null
                ? new string[] { comp.ReferencedConfiguration }
                : null;
        }
        internal static double[] ToMaterialProperties(Color color)
          => new double[]
          {
                color.R / 255d,
                color.G / 255d,
                color.B / 255d,
                1, 1, 0.5, 0.4,
                (255 - color.A) / 255d,
                0
          };
        internal static void SetColor(Color? color,
            IComponent2 ownerComp,
            Action<double[], swInConfigurationOpts_e, string[]> setColorAction,
            Action<swInConfigurationOpts_e, string[]> removeColorAction)
        {
            GetColorScope(ownerComp, out swInConfigurationOpts_e confOpts, out string[] confs);

            if (color.HasValue)
            {
                var matPrps = ToMaterialProperties(color.Value);

                setColorAction.Invoke(matPrps, confOpts, confs);
            }
            else
            {
                removeColorAction?.Invoke(confOpts, confs);
            }
        }

2 Likes