Hi,
I tried to port the "Create Save Bodies Feature and Create an Assembly (VBA)" Sample from the API documentation to C#:
private void button2_Click(object sender, EventArgs e)
{
ModelDoc2 modelDoc = (ModelDoc2) _solidWorks.ActiveDoc;
SelectionMgr selectionMgr = (SelectionMgr) modelDoc.SelectionManager;
Feature feature = (Feature) modelDoc.FirstFeature();
FeatureManager featureManager = modelDoc.FeatureManager;
object[] bodies = new object[] {};
string featureName = string.Empty;
bool continueLoop =true;
while((null != feature) && (continueLoop==true)) {
featureName = feature.GetTypeName2();
if("SolidBodyFolder" == featureName) {
bodies =GetBodies(feature);
continueLoop = false;
}
if(true == continueLoop) {
feature = (Feature) feature.GetNextFeature();
}
}
int bodyCount = bodies.Count();
object[] fileNames = new object[bodyCount];
if(0 < bodyCount) {
for(int count = 0;count <= bodyCount-1;count++) {
string fileName = string.Format("C:\\\\Temp\\\\body{0}.sldprt", count + 1);
fileNames[count] = fileName;
}
}
object fileNamesObject = fileNames;
object bodiesObject = bodies;
featureManager.CreateSaveBodyFeature(bodiesObject, fileNamesObject, "C:\\\\Temp\\\\MultiBodyAssembly.sldasm", false, false);
}private object[] GetBodies(Feature feature)
{
BodyFolder bodyFolder = (BodyFolder)feature.GetSpecificFeature2();
int bodyCount = bodyFolder.GetBodyCount();
object[] bodies = new object[] { };
if (0 < bodyCount)
{
bodies = (object[])bodyFolder.GetBodies();
}
return bodies;
}
But when running it (SWX 2009 x64 SP4.1), I get a COMExecption "The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))
ErrorCode is -2147417851
When running the VBA example, everything works fine.
Update:
When I try to run it from within an addin, I get an AccessViolationException "("Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Alex
SolidworksApi macros