For all issues regarding the Forums use, please, refer to the Forum Rules.

Our Solutions

Need professional assistance?
Consider our:

Support Offerings

 

Need to speed up your development?
Have a look at our:

Samples & Tools

 

Need some functionality extending standard OCCT capabilities?
Check out our:

Adv. Components

Related pages

AccessViolation from C#

Henrik Rune Jakobsen's picture
Forums: 

Hello.

I'm trying to use opencascade from C#. I have started from the .NET example with the OCCTProxy.
To keep track of the shapes created i use this class, NativeWrapper, to keep a pointer to the objects.

public ref class NativeWrapper
{
public:
NativeWrapper();

// Allocate the native object on the C++ Heap via a constructor
NativeWrapper(void* topoDs) : m_Impl(topoDs) {}

// Deallocate the native object on a destructor
~NativeWrapper() {
delete m_Impl;
}

protected:
// Deallocate the native object on the finalizer just in case no destructor is called
!NativeWrapper() {
delete m_Impl;
}

public:
void* GetPointer() { return m_Impl; }

private:
void* m_Impl;
};

Here the shape is drawn and the NativeWrapper is set as a tag on the treenode

TopoDS_Shape MakeBox(Standard_Real x0, Standard_Real y0, Standard_Real z0, Standard_Real xDelta, Standard_Real yDelta, Standard_Real zDelta)
{
gp_Pnt center(x0, y0, z0);
BRepPrimAPI_MakeBox mkBox(center, xDelta, yDelta, zDelta);
TopoDS_Shape shape = mkBox.Shape();
return shape;
}

NativeWrapper^ DrawBox(Standard_Real x0, Standard_Real y0, Standard_Real z0, Standard_Real xDelta, Standard_Real yDelta, Standard_Real zDelta)
{
TopoDS_Shape shape = MakeBox(x0, y0, z0, xDelta, yDelta, zDelta);
myAISContext()->Display(new AIS_Shape(shape));
TopoDS_Shape* retVal = new TopoDS_Shape(shape);
NativeWrapper ^f = gcnew NativeWrapper(retVal);
return f;
}

private void ribbonButtonAddBox_Click(object sender, EventArgs e)
{
var dlg = new DrawCubeDialog();
dlg.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var data = dlg.CubeData;
var o = Viewer.DrawCube(data.Name, data.Point, data.XDelta, data.YDelta, data.ZDelta);
treeNodePrimitives.Nodes.Add(new TreeNode(data.Name) { Tag = o });
}
}

But when i try to use the object i get this exception

System.AccessViolationException was unhandled
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=OCCTProxy
StackTrace:
at Handle_Standard_Transient.BeginScope(Handle_Standard_Transient* )
at Handle_Standard_Transient.{ctor}(Handle_Standard_Transient* , Handle_Standard_Transient* aTid) in c:\opencascade6.7.0\opencascade-6.7.0\inc\handle_standard_transient.hxx:line 82
at Handle_MMgt_TShared.{ctor}(Handle_MMgt_TShared* , Handle_MMgt_TShared* aHandle) in c:\opencascade6.7.0\opencascade-6.7.0\inc\handle_mmgt_tshared.hxx:line 25
at Handle_TopoDS_TShape.{ctor}(Handle_TopoDS_TShape* , Handle_TopoDS_TShape* aHandle) in c:\opencascade6.7.0\opencascade-6.7.0\inc\handle_topods_tshape.hxx:line 25
at TopoDS_Shape.{ctor}(TopoDS_Shape* , TopoDS_Shape* A_0)
...

NativeWrapper^ Partition(void* objA, void* objB)
{
TopoDS_Shape* p_A = static_cast(objA);
TopoDS_Shape* p_B = static_cast(objB);

TopoDS_Shape a = *p_A;
TopoDS_Shape b = *p_B;

BOPAlgo_Builder aBuilder;
aBuilder.AddArgument(a);
aBuilder.AddArgument(b);
aBuilder.Perform();
Standard_Integer iErr = aBuilder.ErrorStatus();
if (iErr)
{
// TODO: HANDLE ERROR
}
const TopoDS_Shape& shape = aBuilder.Shape();
TopoDS_Shape retVal = shape;

ClearView();
Draw(retVal);

NativeWrapper ^f = gcnew NativeWrapper(&retVal);
return f;
}

Any one who can explain to me what is going wrong here, and how to do this right?

Any help much appreciated :)

Rabih's picture

Hi,

I hope this helps.

void Add_TopoDS_ShapePointer(IntPtr p_ShapePointer)
{
typedef int* intpointer;
TopoDS_Shape* topoShape = (TopoDS_Shape*) p_ShapePointer.ToPointer();
}

or

public ref class NativeWrapper
{
public:
NativeWrapper();

// Allocate the native object on the C++ Heap via a constructor
NativeWrapper(IntPtr topoDs)
{
typedef int* intpointer;
TopoDS_Shape* topoShape = (TopoDS_Shape*) p_ShapePointer.ToPointer();

m_Impl = topoShape
}

// Deallocate the native object on a destructor
~NativeWrapper() {
delete m_Impl;
}

protected:
// Deallocate the native object on the finalizer just in case no destructor is called
!NativeWrapper() {
delete m_Impl;
}

public:
TopoDS_Shape* GetPointer() { return m_Impl; }

private:
TopoDS_Shape* m_Impl;
};

best regards
Rabih