Create SOLIDWORKS BOM with thumbnail preview

Export SOLIDWORKS BOM to Excel with thumbnail preview – SOLIDWORKS MACRO

This macro exports a selected SOLIDWORKS BOM to Excel. A thumbnail preview is created for each of the BOM components.

Problems running the macro? Report a bug here.

Exception handling for SOLIDWORKS PDM API objects – PDM API TIPS

This article applies for C# and VB.NET.

I have recently rotated towards doing more of SOLIDWORKS PDM add-ins. Interestingly enough, the SOLIDWORKS PDM is quite similar to the SOLIDWORKS API and the learning curve is not steep at all.

Chances are decent that you are using the IEdmVault5::GetObject method to get specific PDM interface objects. A typical syntax would look something like follow:

The example above will yield an exception if the MyFolderID is not an ID of a PDM Folder. That being said, the normal reaction would be to handle this by wrapping the GetObject in a try-catch block. An updated version of this would look something like this:

I need to assert that, in the code above, we are not handling all possible exceptions, we are only interested in the COM exceptions thrown if the object is not a proper PDM folder. A NullReferenceException can be raised for example if the MyVault is a null. We are not interested in that however. If you would like to handle all exception, you should a second catch block where you catch exceptions that are not caught by the prior catch block.

In the case of IEdmVault5::GetObject, two COM exceptions can be thrown and are illustrated by the HResult returns of this method had we been using c++:

  • E_EDM_INVALID_ID: The provided ID was not valid.
  • E_EDM_PERMISSION_DENIED: The logged-in user does not have permission to see the requested object.

The above approach can be cumbersome. I personally don’t like to use try catch blocks a lot. I prefer if statements.

We can take some inspiration from Int32.TryParse.

MSDN says that Int32.TryParse:

Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.

Remarks

The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails. It eliminates the need to use exception handling to test for a FormatException in the event that s is invalid and cannot be successfully parsed.

Consequently, we can extend IEdmVault5::GetObject to suppress the COM exceptions like follow:

Now, let’s dissect this extension method:
+ This method extends the IEdmVault5 interface using a generic T. T represents the return type and is generic since there are dozens of PDM API interfaces we can get (List here). The only requirement is that T is class.
+ In syntax, using this method is no different to using  IEdmVault5::GetObject.
+ If any exception occurs, TryGetObject will return a null.
Unfortunately, swallowing an exception is not really a good idea. It’s preferable to handle the exception in such a way that we make use of it either by logging it to disk or displaying to the user for.

Enter the MethodReturn<T> class.

 

This MethodReturn class adds a little of elegance to our code by capturing the returned PDM API interface object if IEdmVault5::GetObject was successful and returns the COM exception message if the process of getting the specific API Object failed.

An updated version of the TryGetObject woud look like follow:

 

Remark: Add

to be able to reference COMException.

Delete all properties for the active configuration – SOLIDWORKS MACRO

The following macro deletes all the properties from the active configuration. The macro is written used late-binding so it is version independant.
The deletion action will be confirmed by the user.

Problems running the macro? Report a bug here.