3 Replies Latest reply on Jul 29, 2016 8:30 AM by Alexandre Gragnano

    MultiSelect2 does not accept AddComponents3 result

    Alexandre Gragnano

      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.

       

      var components = (object[])assembly.AddComponents3(names, transforms, coordSystemNames);
      
      var count = docExtension.MultiSelect2(components, false, null); // Select no component and returns 0
      

       

      To fix it:

       

       

      var components = (object[])assembly.AddComponents3(names, transforms, coordSystemNames);
      
      // FIX
      var array = new Component2[components.Length];
      for (var i = 0; i < components.Length; i += 1)
          array[i] = (Component2)components[i];
      
      var count = docExtension.MultiSelect2(array, false, null); // Select all components and returns components.Length
      

       

      Is it a normal behavior or an API problem ?

       

      Best regards,

      Alexandre

        • Re: MultiSelect2 does not accept AddComponents3 result
          Jim Sculley

          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:

           

          Component2[] comps = Array.ConvertAll<object, Component2>((object[])components, delegate(object comp) { return (Component2)comp; });

           

          It is a lot less verbose in .NET 4.0 with lambda expressions:

           

          Component2[] comps= Array.ConvertAll((object[])components, comp => (Component2)comp);

           

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

            • Re: MultiSelect2 does not accept AddComponents3 result
              Alexandre Gragnano

              Thank you for your response Jim,

               

              What is confusing is:

               

              ReferenceEquals(components[i], array[i]) == true
              

              and

              (components[i] is Component2) == true
              

               

              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

            • Re: MultiSelect2 does not accept AddComponents3 result
              Alexandre Gragnano

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

              Parameters

              Objects
                  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