Calling CreateSaveBodyFeature throws AccessViolationException

150 views
Skip to first unread message

Alexander Zeitler

unread,
Dec 30, 2009, 5:03:25 AM12/30/09
to solidwo...@googlegroups.com

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

 

Eduardo

unread,
Dec 30, 2009, 7:23:20 AM12/30/09
to SolidWorks-API
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
apisu...@solidoworks.com

Alexander Zeitler

unread,
Dec 30, 2009, 7:59:40 AM12/30/09
to solidwo...@googlegroups.com
I already checked that bodies and fileNamesObjects are not null or empty.

I already sent it to apisupport.

Alex

Eduardo

unread,
Dec 30, 2009, 9:21:19 AM12/30/09
to SolidWorks-API
Are you using Visual Studio?

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

> apisupp...@solidoworks.com

> > Alex- Ocultar texto citado -
>
> - Mostrar texto citado -

Alexander Zeitler

unread,
Dec 30, 2009, 9:31:20 AM12/30/09
to solidwo...@googlegroups.com
I'm using VS 2008 Team Edition

Other addins I create(d) work fine. It's only this feature that throws the
exception.

Eduardo

unread,
Dec 30, 2009, 9:57:48 AM12/30/09
to SolidWorks-API
In debug mode, if you insert one breakpoint at the line that is
throwing the exception then place the mouse over the 2 object
variables, (one at a time) can you see their contents when the macro
execution comes to that breakpoint?

> > - Mostrar texto citado -- Ocultar texto citado -

Reply all
Reply to author
Forward
0 new messages