problem retrieving mass properties

203 views
Skip to first unread message

tbischel

unread,
May 28, 2009, 2:21:55 PM5/28/09
to SolidWorks-API
Hi,
I'm trying to retrieve the mass properties (mass, center of mass,
moments of inertia, etc) from an assembly in C#. I keep getting an
exception "Attempted to read or write protected memory. This is often
an indication that other memory is corrupt". However, this largely
seems to follow the model presented in the "Get Mass Properties of
Multibody Assembly Component Example (VB6)" document in the Solidworks
API. Attached is a shortened version that still fails... any
suggestions?

private void APIexample(SldWorks swApp)
{
try
{
ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;
ModelDocExtension swDocExt = (ModelDocExtension)
swModel.Extension;
MassProperty swMass = (MassProperty)
swDocExt.CreateMassProperty();
SelectionMgr swSelMgr = (SelectionMgr)
swModel.SelectionManager;
Component2 swComp = (Component2)
swSelMgr.GetSelectedObjectsComponent2(1);

//This is the line that fails...
swMass.AddBodies(swComp.GetBodies2((int)
swBodyType_e.swSolidBody));
System.Windows.Forms.MessageBox.Show("Mass: " +
swMass.Mass);
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}
}

Georg D.

unread,
May 29, 2009, 8:29:22 AM5/29/09
to tbis...@gmail.com, SolidWorks-API
I can show you a vb code for getting the Massproperties of the active document, maybe this will help you:

Dim swApp As sldworks.SldWorks
Dim swDoc As sldworks.ModelDoc2
Dim swDocConfigurations
Dim massProperties
Dim swDocPath As String

swApp = CreateObject("sldworks.application")
swDoc = swApp.ActiveDoc
swDocConfigurations = swDoc.GetConfigurationNames
swDocPath = (swDoc.GetPathName())
For Each swconfig In swDocConfigurations
   massProperties = swApp.GetMassProperties2(swDocPath, swconfig, 1)
Next
--
****************************************************
*   Skype: gegy,,,gegy                                  
*   Web:   http://draxbar.no-ip.org          
*   Mail:    geg...@gmail.com            
*   Twitter: http://twitter.com/gegy    
-----------------------------------------------------------------  
*   A strange game. The only winning move is not to play.          
****************************************************

tbischel

unread,
May 29, 2009, 5:13:30 PM5/29/09
to SolidWorks-API
Yeah I've found that just creating a mass properties object will
automatically use the properties for all sub-assemblies. I've also
found that instead of using "GetBodies2", and use "GetBody", there is
no exception. However, it seems that still grabs the global
properties instead of those for only the selected bodies.
--Tyler
> *   Mail:    gegy...@gmail.com

tbischel

unread,
Jun 5, 2009, 5:21:20 PM6/5/09
to SolidWorks-API
So I've found that using swBodyType_e.swGeneralBody instead will get
past the exception. However, the mass properties returns the mass for
the entire body no matter what I do. The one thing I can kind of do
to get around this is just suppress all parts except the current
selected one, and then the overall mass properties reflect the mass of
the selected component. But that has its own problems. I'm fishing
for other suggestions (or solutions) here...
Reply all
Reply to author
Forward
0 new messages