MultiSelect2 does not accept AddComponents3 result

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
Answers 3
JS
JS 2016-01-19 Best Answer

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:

<span style="font-family: arial,helvetica,sans-serif;">Component2</span><span class="pun">[]</span><span class="pln"> comps</span><span class="pun">=</span><span class="pln"> </span><span class="typ">Array</span><span class="pun">.</span><span class="typ">ConvertAll</span><span class="pun">((object[])components</span><span class="pun">,</span><span class="pln"> comp </span><span class="pun">=></span><span class="pln"> </span><span class="pun">(</span>Component2<span class="pun">)</span>comp<span class="pun">);</span>

However, the SW macro editor is still using .NET 2.0, so you will have to use the first version.

AG
AG 2016-01-19

Thank you for your response Jim,

What is confusing is:

<p>ReferenceEquals(components[i], array[i]) == true</p>

and

<p>(components[i] is Component2) == true</p>

If an object is not necessary a Component2, a Component2 is necessary an object.

According to the documentation:

  • objects can be heterogeneous

So if API waiting for an object array which can be heterogeneous, why do I need to type this array ?

Best regards,

Alexandre

AG
AG 2016-07-29

My SR was accepted and implemented in SW 2016 SP4 (documentation​ updated)

Parameters

<em>Objects</em><br>    Array of selectable objects:
  • can be the same type of objects (e.g., an array of faces or an array of edges) or different types of objects (e.g., an array of entities of faces and edges )
  • if any object is not selected, then it is ignored

Now, Objects parameter can be an object[] type, not necessary a typed array.

Alexandre