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

Hi!

I’m sorry to re-open this question but I don’t feel link to open a new one.

Searching online I saw to ways to change the color of a component:

  1. using the method suggeste by @artem MaterialPropertyValues
  2. using GetDisplayStateSetting and DisplayStateSpecMaterialPropertyValues as shown here

I wonder what are the main differences between the two and if there are any pro or cons using one method or the other.

Furthermore if I use the second method, even with the example from the SolidWorks API Help, the changed component appears black unless the component is closed and re-opened or the appearance is changed from the UI. I saw others complaining about this behavior:

Any idea or suggestion?

Thanks in advance.

I would highly highly appreciate it if you could open a new question.

This will give your question more visibility on the site and the internet.
Opening a new question takes as long as writing a new reply.

Thank you.