Wed, 12/16/2009 - 15:54
Hi:
I need to keep pointers to shapes outside of occ as void * ... when i come back, i need to cast back to the shape type and then do stuff with the shape.
For example:
for ( Ex.Init(aCompound,TopAbs_SOLID);Ex.more;Ex.next) {
cast Ex.current as a void *;
store pointer to solid shape outside of occ;
}
do things outside of occ.
When i need to go back to occ, i want to cast back the void * to a pointer to a Solid shape and then do things with it in occ.
i've tried that but it doesn't work. when i cast back and dereference the pointer, it crashes.
What am I doing wrong ? what's the proper way to store a pointer/handle to a shape outside of occ so that when you come back, you can still access it.
Note: i am not too versed in C++.
thanks in advance for your help.
Wed, 12/16/2009 - 20:06
i have the feeling it cannot be done.
what is the proper way to reference occ shapes from outside occ ? so that you can go in and out of occ without too much pain.
do you have to assign a unique identifier to each shape and maintain your own ID table ?
i just need a few clues.
Wed, 12/16/2009 - 20:17
you can create a class to include a shape like :
class ShapeContainer
{
public:
ShapeContainer(const TopoDS_Shape& shape):_shape(shape){}
TopoDS_Shape _shape;
};
and in your code :
//external api
void ExternalFct(void* shapecontainer);
void* ReturnShapeContainer();
//occ code
TopoDS_Shape s = Exp.Current();
ShapeContainer* sc = new ShapeContainer(s);
ExternalFct(sc);
void* ptr = ReturnShapeContainer();
ShapeContainer* sc2 = (ShapeContainer*)ptr;
TopoDS_Shape s2 = sc2->_shape;
//cleanup
sc2 = NULL;
delete sc; sc = NULL;
it should work
HTH,
Stephane
Wed, 12/16/2009 - 20:24
Stephane is correct.
TopoDS_HShape can do the work for you.
TopoDS_HShape* aHShape = new TopoDS_HShape (aShape);
Note that in this case you must take care of memory deallocation yourself. In the case of using Open CASCADE handles (Handle_TopoDS_HShape), which are smart pointers you do not have to care about that.
Roman
Wed, 12/16/2009 - 22:33
thanks a lot.
so the pointer that I store is this aHShape?
void *ptr=aHShape;
Now, assuming the aShape was a Solid to begin with.
when I cast it back to a Solid, do I use:
Solid=*(static_cast(ptr)); ???
i've tried that but i don't think it works.
Wed, 12/16/2009 - 23:50
No, you always cast to TopoDS_HShape*.
TopoDS_HShape* aHShape = static_cast (ptr);
const TopoDS_Shape& aShape = aHShape->Shape();
then you deal with aShape as usual. For instance:
const TopoDS_Solid& aSolid = TopoDS::Solid (aShape) if you are sure it's a solid.
Wed, 12/16/2009 - 22:35
merci bien.
is it the same as using TopoDS_HShape ?
Wed, 12/16/2009 - 22:36
above was directed to Stéphane and his solution.