AnsweredAssumed Answered

System.AccessViolationException at sketch creation

Question asked by Thomas Ormandi on Jul 23, 2019
Latest reply on Aug 6, 2019 by Thomas Ormandi

I was writing a VBA macro which was working already. I'm working on an addin now in C#. I would like to write the same thing which was working in a macro with some modifications, but when my code reaches the line which should create a CenterRectangle, SolidWorks crashes with memory access violation.

Here is the code and every error detail what I got:

 

swModel = iSwApp.IActiveDoc2;
swSelection = swModel.SelectionManager;
selectionCount = swSelection.GetSelectedObjectCount2(-1);
for (int i = 0; i < selectionCount; i++)
{
swFace[i] = (Face2)swSelection.GetSelectedObject6(i + 1, 2);
swFeature = swFace[i].GetFeature();
parents = swFeature.GetParents();
swParentFeat = (Feature)parents[1];
sketchForMeasure = swParentFeat.Name;
Debug.Print(sketchForMeasure);
swDim = swModel.Parameter("Bf@" + sketchForMeasure);
dimVal = swDim.GetSystemValue3(1, null);
mWidth = dimVal[0];
swDim = swModel.Parameter("
Depth@" + sketchForMeasure);
dimVal = swDim.GetSystemValue3(1, null);
mHeight = dimVal[0];
uvBounds = swFace[i].GetUVBounds();
centerU = (uvBounds[0] + uvBounds[1]) / 2;
centerV = (uvBounds[2] + uvBounds[3]) / 2;
swSurf = swFace[i].GetSurface();
evalRes = swSurf.Evaluate(centerU, centerV, 0, 0);
swSkMgr = swModel.SketchManager;
swSkMgr.InsertSketch(false);
swSketch = swSkMgr.ActiveSketch;
swTransform = swSketch.ModelToSketchTransform;
swMathU = SwApp.GetMathUtility();
dPt[0] = evalRes[0];
dPt[1] = evalRes[1];
dPt[2] = evalRes[2];
swMathPt = swMathU.CreatePoint(dPt);
swMathPt = swMathPt.MultiplyTransform(swTransform);
//Debug.Print(tPt[0].ToString());
tPt = swMathPt.ArrayData;
swSkMgr.CreateCenterRectangle(tPt[0], tPt[1], tPt[2], (tPt[0] + (mWidth / 2)), (tPt[1] + (mHeight / 2)), tPt[2]);
swSkMgr.InsertSketch(false);

Everything works and looks fine until this line:

swSkMgr.CreateCenterRectangle(tPt[0], tPt[1], tPt[2], (tPt[0] + (mWidth / 2)), (tPt[1] + (mHeight / 2)), tPt[2]);

Errors from debug and the output window:

System.AccessViolationException
  HResult=0x80004003
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

Debug output:

~~~Before iBuildSceneGraphView~~~After iBuildSceneGraphViewException thrown at 0x00000000489F325E (slduiu.dll) in SLDWORKS.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
Exception thrown: 'System.AccessViolationException' in TemEpszer AddIn.dll
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Any ideas what can cause this? Unfortunatley its a too big error for me and I couldnt find any solutions by myself.

Thank you very much in advance.

Outcomes