AnsweredAssumed Answered

General questions about how using SolidWorks API

Question asked by Eric Snyder on Jun 3, 2020
Latest reply on Jun 4, 2020 by Eric Snyder

I have a few general questions about using SolidWorks API. I'd appreciate everyone's thoughts.


First, I use Visual Studio 2019 and C#. A friend convinced me to switch from Visual Basic to C# about a year ago and I am glad I have. Although there are a lot of Visual Basic examples in SolidWorks it seems VB is dying and C# is rising. There are a lot more examples and tutorials and the user base of C# seems pretty deep.



  1. Seems that using the SW API I can't get debugging information from quite a few Of the SolidWorks objects directly. They seem to be COM objects. If I hover to get values I get "System._ComObject". It would be nice to see property values, etc. Is there a good strategy around this?
  2. When doing what seem to be common tasks like iterate the feature tree it is a multi step operation. In fact many of the things I would like to be able to inherit from IModelDoc2 but in C# that is actually implementing the interface. To get around this I have created objects that I pass the IModelDoc2 into and keep the underlying IModelDoc2 as a property so I can access it's methods, properties, etc. Doing this I can write custom methods in my custom object that does the repetitive heavy lifting of all the getting and casting and iterating. All I have to do is call the custom method rather than rewrite the multi step process over and over. Is there a better way to architect this?
  3. Casting. It seems like everything in the API that is a get returns an object. It seems like life is about casting, casting, casting when using Solidworks.

    object[] bodies = (object[])partDoc.GetBodies2((int)swBodyType_e.swSolidBody, true);
    List<IBody2> bodyList = new List<IBody2>();
    foreach (IBody2 item in bodies)
    I received a good suggestion from Artem Taturevich to do the following:
    IBody2[] bodies = (partDoc.GetBodies2((int)swBodyType_e.swSheetBody, true) as object[]).Cast<IBody2>().ToArray();
    This seems like a nice one liner. Are there other approaches to all the casting that has to be done?
  4. Geometric transforms and operations with vectors, 3D points, lines. I have used a couple different libraries for this that seem pretty easy to use. Geometri and Again, the Solidworks built in stuff seems clumsy to me. I would think that underneath the hood the Solidworks stuff would be precision. An example of this is you have to use a class factory to create a 3D math point in Solidworks. You can't just declare an instance of a 3D point.

    What are you doing when it comes to these kinds of operations? What are the trade offs?