5 Replies Latest reply on May 13, 2018 3:47 AM by Roman Mettler

    Choose body in View (c#)

    Roman Mettler

      Hello, im trying to define view body, it should be easily done by "swView.Bodies = new object[]{ somebody }", i created a vba macro with same principle (taken from api help ) and all works fine, in c# i cant make it work, debugging shows that the contents of "View.Bodies" never get replaced with new array... some clue?

      c# version:

      
              public void SetBody()
              {
                  object[] b = new object[1];
                  ModelDoc2 modDoc = SwApp.ActiveDoc;
                  SelectionMgr selectionMgr = modDoc.ISelectionManager;
                  SolidWorks.Interop.sldworks.View v = selectionMgr.GetSelectedObject6(1, -1);
                  modDoc = v.ReferencedDocument;
                  var bodies = ((PartDoc)modDoc).GetBodies2((int)swBodyType_e.swSolidBody, true);
                  b[0] = bodies[1];
      
      
                  v.Bodies = b;
              }
      

       

      VBA version:

       

      Dim Bodies(1) As Object
      Dim swModel As SldWorks.ModelDoc2
      Dim swSelMgr As SldWorks.SelectionMgr
      Dim swView As SldWorks.View
      Dim arrBody As Variant
      
      Set swModel = swApp.ActiveDoc
      Set swSelMgr = swModel.SelectionManager
      Set swView = swSelMgr.GetSelectedObject6(1, -1)
      Set swModel = swView.ReferencedDocument
      arrBody = swModel.GetBodies2(swSolidBody, True)
      Set Bodies(0) = arrBody(1)
      swView.Bodies = Bodies
      

       

       

      any help would be much appreciated...

        • Re: Choose body in View (c#)
          Josh Brady

          Too lazy to run your code... sorry, I'm all VBA, can't comment on your C#

           

          Why is Bodies declared with upper bound 1 (meaning size 2)?  I don't see where Bodies(1) is set to anything, so you're passing a 2 element array with nothing in the second element, right?  Probably not gonna work.  Also, I assume you've looked at the example in the API help?  Of course, I haven't tested that one either, but they pass the variant array to swView.Bodies.  You're passing the Object array.  SW seems pretty picky about the types of arrays it actually likes.

            • Re: Choose body in View (c#)
              Roman Mettler

              Hi Josh, about vba it works nice as is in my example, i played a bit with the types passed to swView.Bodies to see if that could be a type problem, but it runs well with Object Array, so i assume it should run as well in c#...

               

              in c# "bodies" is a array with all bodies in "swView" referenced part it has 22 bodies, so i take second body from it and put in b[0] (object array to be passed to swView.Bobies)  b[0] = bodies[1];

               

              about Bodies(1) was again playing with code to see if i find some poit where vba will also fail, but it runs even if Bodies(20)...

               

              so the point is:

              vb run perfect like it is in that snippet, somehow im making some mistake in converting it to c# or some other detail that i cant figure it out...

               

              swView.Bodies is it self an object array (can be checked in vs debugger, it contains all 22 bodies from part) but somehow its not accessible to be replaced by the object array with the only body i want visible... im not a vba guy but i think i understood the main idea from the API help...

               

              thanks for your time, and hope you can help me further...

              best regards,

              Bruno.

            • Re: Choose body in View (c#)
              Amen Allah Jlili

              Roman,

              In the .NET Framework, an object array can be used to store System.Object types like integer, double, etc. .NET marshals these data types automatically. However, non-standard object types like SOLIDWORKS interfaces must be explicitly marshaled to an IDispatch object array.

              Some SOLIDWORKS methods and properties have input arrays of non-standard objects. Before passing these arrays, you must marshal them to IDispatch object arrays using the System.Runtime.InteropServices.DispatchWrapper class.

              Several SOLIDWORKS methods and properties have input object arrays that must be marshaled to an IDispatch object array. The following Help topics contain links to VB.NET and C# examples that demonstrate how to marshal an array of non-standard input objects to an IDispatch object array.

               

               

              Cheers,

              Amen

              www.cadsharp.com

              • Re: Choose body in View (c#)
                Amen Allah Jlili

                In other words, wrap your object using DispatchWrapper and pass the DispatcherWrapper[] instead of the object[].

                Cheers,

                Amen

                www.cadsharp.com