from OCC import TopExp,TopoDS,BRepBuilderAPI,BRepPrimAPI,BRep,gp,TopAbs

"""
 Print the details of an object from the top down
"""	
def dumpTopology(shape,level=0):
	brt = BRep.BRep_Tool();
	s = shape.ShapeType();
	ts = TopoDS.TopoDS();
	print
	print "." * level,shapeTypeString(shape),
		
	if s == TopAbs.TopAbs_VERTEX:
		#print type(shape )
		#if this is a vertext, print coords
		#pnt = shape.TShape().Pnt();
		pnt = brt.Pnt(ts.Vertex(shape))
		print "(",pnt.X()/25.4," ",pnt.Y()/25.4," ",pnt.Z()/25.4,")",
		return;
		
	it = TopoDS.TopoDS_Iterator(shape);
	while it.More():
		shp = it.Value();
		it.Next();
		dumpTopology(shp,level + 1 );		

def shapeTypeString(shape):

	st = shape.ShapeType();
	s = "?";
	if st == TopAbs.TopAbs_VERTEX:
		s = "Vertex";
	if st == TopAbs.TopAbs_SOLID:
		s = "Solid";
	if st == TopAbs.TopAbs_EDGE:
		s = "Edge";
	if st == TopAbs.TopAbs_FACE:
		s = "Face";
	if st == TopAbs.TopAbs_SHELL:
		s = "Shell";
	if st == TopAbs.TopAbs_WIRE:
		s = "Wire";
	if st == TopAbs.TopAbs_COMPOUND:
		s = "Compound."
	if st == TopAbs.TopAbs_COMPSOLID:
		s = "Compsolid."
	
	return s + ":" + str(shape.HashCode(23232232));


"""
 return all the faces 
"""
def findFaces(shape):
	faceList = [];
	texp = TopExp.TopExp_Explorer();
	ts = TopoDS.TopoDS();
	texp.Init(shape,TopAbs.TopAbs_FACE);
	while ( texp.More() ):
		face = ts.Face(texp.Current());
		faceList.append(face);
		texp.Next();
	return faceList;


#make a box
bapi = BRepPrimAPI.BRepPrimAPI_MakeBox(gp.gp_Pnt(-100,-100,-30),gp.gp_Pnt(100,100,-10))
bapi.Build();
box=bapi.Shape();

#dump the topology
print "Created a Box. Now, printing the topology of the shape..."
dumpTopology(box);

print 
print "Ok, now dumping the faces one at a time."

#iterate through
#and dump topolgy for faces locally
texp = TopExp.TopExp_Explorer();
ts = TopoDS.TopoDS();
texp.Init(box,TopAbs.TopAbs_FACE);
while ( texp.More() ):
	face = ts.Face(texp.Current());
	print "**** Face:"
	dumpTopology(face);
	print "****"
	texp.Next();

print
print "Ok, now dumping them using same code, but storing in a list"
print "here i get a hard crash."
#now do the same thing use values from a list
facelist =  findFaces(box);
for f in facelist:
	dumpTopology(f);