Hi,
Just to report the MultiSelect2 method does not accept the AddComponents3 method result.
I have to cast COM object array into a IComponent2 object array before.
<p>var <span style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;">components</span> = <span style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;">(object[])</span>assembly.AddComponents3(names, transforms, coordSystemNames);</p><p></p><p>var count = docExtension.MultiSelect2(<span style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;">components</span>, false, null); // Select no component and returns 0</p>
To fix it:
<p><span style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;">var <span style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;">components</span> = (object[])assembly.AddComponents3(names, transforms, coordSystemNames);</span></p><p></p><p><span style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;">// FIX</span></p><p>var array = new Component2[components.Length];</p><p>for (var i = 0; i < components.Length; i += 1)</p><p> array[i] = (Component2)components[i];</p><p></p><p>var <span style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;">count</span> = docExtension.MultiSelect2(<span style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;">array</span>, false, null); // Select all components and returns <span style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;">components</span>.Length</p>
Is it a normal behavior or an API problem ?
Best regards,
Alexandre
SolidworksApi macros
Normal behavior. AddComponents3 returns a System.Object[], which is an array of objects, which is not the same thing as an array of IComponent2 objects. There is a ConvertAll method in the Array class that will let you do the conversion in one line like this:
<span style="font-family: arial,helvetica,sans-serif;">Component2[] comps = Array.ConvertAll<object, Component2>((object[])components, delegate(object comp) { return (Component2)comp; });</span>It is a lot less verbose in .NET 4.0 with lambda expressions:
However, the SW macro editor is still using .NET 2.0, so you will have to use the first version.
Thank you for your response Jim,
What is confusing is:
and
If an object is not necessary a Component2, a Component2 is necessary an object.
According to the documentation:
So if API waiting for an object array which can be heterogeneous, why do I need to type this array ?
Best regards,
Alexandre
My SR was accepted and implemented in SW 2016 SP4 (documentation updated)
Parameters
Now, Objects parameter can be an object[] type, not necessary a typed array.
Alexandre