Hi
I tried to port the "Create Save Bodies Feature and Create an Assembly (VBA)" Sample from the API documentation to C#:
public void Run(ISldWorks solidWorks) {
ModelDoc2 modelDoc = (ModelDoc2)solidWorks.ActiveDoc;
SelectionMgr selectionMgr = (SelectionMgr)modelDoc.SelectionManager;
Feature feature = (Feature)modelDoc.FirstFeature();
FeatureManager featureManager = modelDoc.FeatureManager;
object bodies = null;
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 = ((object[])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;
featureManager.CreateSaveBodyFeature(bodies, fileNamesObject, @"C:\temp\MultiBodyAssembly.sldasm", false, false);
}
private object GetBodies(Feature feature)
{
BodyFolder bodyFolder = (BodyFolder)feature.GetSpecificFeature2();
int bodyCount = bodyFolder.GetBodyCount();
object bodies = null;
if (0 < bodyCount)
{
bodies = bodyFolder.GetBodies();
}
return bodies;
}
When running the Addin, I get a AccessViolationException ("Attempted to read or write protected memory. This is often an indication that other memory is corrupt.") in Line
featureManager.CreateSaveBodyFeature(bodies, fileNamesObject, @"C:\temp\MultiBodyAssembly.sldasm", false, false);
When running the VBA example, everything works fine.
Exception occurs both on SWX 2009 SP4.1 x64 + SWX 2010 SP0 x64
Alex
In debug mode try to watch the content of the 2 main objects -> bodies
and fileNamesObject
If everything seems OK, maybe you should post your problem to
apisu...@solidoworks.com
On 30 Dez, 12:59, "Alexander Zeitler" <a...@alexonasp.net> wrote:
> I already checked that bodies and fileNamesObjects are not null or empty.
>
> I already sent it to apisupport.
>
> Alex
>
>
>
> -----Original Message-----
> From: solidwo...@googlegroups.com
>
> [mailto:solidwo...@googlegroups.com] On Behalf Of Eduardo
> Sent: Mittwoch, 30. Dezember 2009 13:23
> To: SolidWorks-API
> Subject: Re: Calling CreateSaveBodyFeature throws AccessViolationException
>
> That exception occurs when you try to read data that is not available.
>
> In debug mode try to watch the content of the 2 main objects -> bodies
> and fileNamesObject
>
> If everything seems OK, maybe you should post your problem to
> > Alex- Ocultar texto citado -
>
> - Mostrar texto citado -
Other addins I create(d) work fine. It's only this feature that throws the
exception.
> > - Mostrar texto citado -- Ocultar texto citado -