As the SolidWorks 2009 Simulation API help states, the typical use of Cosmos Works begins by creating an instance of the ICosmosWorks class:
private object iCosWorks = null;
iCosWorks = (ICosmosWorks) SwApp.GetAddInObject(“CosmosWorks.CosmosWorks”);
However if you try the code above, you will find that the iCosWorks object is always null .
Where does the string (“CosmosWorks.CosmosWorks”) come from, and how is it named? It is not very clear in the documentation: The help manual tells us that the signature of this function is:
virtual object GetAddInObject( string clsid ) "This method also accepts the ProgID, both version independent and version dependent, for clsid."
So I wonder, the ProgID has updated in the new 2009 API, while the documentation falls behind and remain intact. What lazy engineers! It costs me much time...
So I tried to get the string by myself. In SolidWorks 2009, the Simulation DLL is ‘cosworks.dll’,
- Search the registry with this dll as the keyword
- The ProgID as well as VerionIndependentProgID can be found.
Now, retry the following code:
iCosWorks = (ICosmosWorks) SwApp.GetAddInObject(“SldWorks.Simulation.2“); or
iCosWorks = (ICosmosWorks) SwApp.GetAddInObject(“SldWorks.Simulation“);
Debug.Assert(iCosWorks != null);
It works now!