Author Topic: Why Brep crash if.....  (Read 2159 times)

0 Members and 1 Guest are viewing this topic.

bikelink

  • Guest
Why Brep crash if.....
« on: March 29, 2010, 04:34:12 AM »
a quick question about my code and an error that it made me mad (now it appears resolved...)
but i don't understood the real reason.
Autocad without the red bold rows in the code below , after a loop (8/10 times with 35 solids ) did crash.
with this rows added now it's going ok.Have You got some idea ?
I arrived here at this modify with debugger set "code unmanaged" and his file dmp.
Thanks in advance

public static IList PrendiPuntiDelSolido(Solid3d sol)
{

IList ptResult = new List(0);
try
{
if (sol.IsNull)
return ptResult;

using (AcBr.Brep brp = new AcBr.Brep(sol))
{
AcBr.BrepEdgeCollection edgeTrav = brp.Edges;
foreach (AcBr.Edge edge in edgeTrav)
{
Point3d p = Point3d.Origin;
Point3d p2 = Point3d.Origin;
using (Autodesk.AutoCAD.BoundaryRepresentation.Vertex v = edge.Vertex1)
{
p = v.Point;
if (!ptResult.Contains(p))
ptResult.Add(p);


}
using (Autodesk.AutoCAD.BoundaryRepresentation.Vertex v2 = edge.Vertex2)
{
p2 = v2.Point;
if (!ptResult.Contains(p2))
ptResult.Add(p2);

}


using (Curve3d c3d = edge.Curve)
{
// without this after some loop autocad crash.
if (c3d == null)
continue;


if (p.IsEqualTo(p2, new Tolerance(0.01, 0.01)) && c3d.IsClosed())
{
using (ExternalCurve3d ca = edge.Curve as ExternalCurve3d)
{
using (CircularArc3d c = ca.NativeCurve as CircularArc3d)
{
if (c != null && c.IsClosed())
{
Vector3d vraggio = c.Center.GetAsVector() - p.GetAsVector();
p2 = p + (vraggio * 2);
if (!ptResult.Contains(p2))
ptResult.Add(p2);

}
}
}

}
}
edge.Dispose();
}
}

}
catch (Autodesk.AutoCAD.BoundaryRepresentation.Exception exp)
{
System.Diagnostics.Trace.WriteLine("PrendiPuntiDelSolido errore " + exp.Message);


}
catch (System.Exception ex)
{
System.Diagnostics.Trace.WriteLine("PrendiPuntiDelSolido errore " + ex.Message);
}
return ptResult;
}

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Why Brep crash if.....
« Reply #1 on: March 29, 2010, 05:05:24 AM »
There are many types of edges and faces, you must first determine what type it is then you can cast to it.

here's some sample code in Boo, you should be able to get the gist of what you need to do.
Code: [Select]
[CommandMethod('soltrav')]
public static def travsol():
""" Code to demonstrate traversal of a 3d solid using the Brep lib """

// Obtain the necessary objects, Database, editor and transaction
db as Database = HostApplicationServices.WorkingDatabase
ed as Editor = Application.DocumentManager.MdiActiveDocument.Editor

tr as Transaction = db.TransactionManager.StartTransaction()

try:
// Prompt for selection of a solid to be traversed
prEntOpt = PromptEntityOptions('Select a 3D solid: ')
prEntOpt.SetRejectMessage('\nPlease select only a 3D Solid')
prEntOpt.AddAllowedClass(typeof(Solid3d), true)

prEntRes as PromptEntityResult = ed.GetEntity(prEntOpt)

//FullSubentityPath fullSubPath = new FullSubentityPath(objIds, SubentityId.Null);
//Brep brp = new Brep(fullSubPath);

sol = cast(Solid3d, tr.GetObject(prEntRes.ObjectId, OpenMode.ForRead))

// Build the BRep topology object that will be traversed.
brp = Brep(sol)
cmplxCount = 0
shllCount = 0
fceCount = 0
lpCount = 0
edgCount = 0

// Get all the Complexes which are primary BRep elements and represent a
// conceptual topological entity of connected shell boundaries.
for cmplx as Complex in brp.Complexes:
ed.WriteMessage('\n\nComplex Count: {0}', ++cmplxCount)
// Get all the shells within a complex. Shells are secondary BRep entities
// that corresponds to a collection of neighboring surfaces on a solid
for shll as Shell in cmplx.Shells:
ed.WriteMessage('\nShell Count: {0}', ++shllCount)
ed.WriteMessage('\nShell Type: {0}', shll.ShellType.ToString())

// Get all the faces in a shell. Faces are primary BRep topological entities that directly
// correspond to face subentities on AutoCAD entities like solid, region and body
for fce as Autodesk.AutoCAD.BoundaryRepresentation.Face in shll.Faces:
ed.WriteMessage('\nFace Count: {0}', ++fceCount)

// Get all the boundary loops within a face (Secondary BRep entities and no corresponding
// AutoCAD entities/subentities).
for lp as BoundaryLoop in fce.Loops:
ed.WriteMessage('\nLoop Count: {0}', ++lpCount)
ed.WriteMessage('\nLoop Type: {0}', lp.LoopType.ToString())

// Get all the Edges in a loop (Edges are primary BRep entities and correspond to
// Geometric entities). 
for edg as Edge in lp.Edges:
ed.WriteMessage('\nEdge Count: {0}', ++edgCount)
crv = edg.Curve as ExternalCurve3d
if crv.IsCircularArc:
ed.WriteMessage('\nEdge Type: CircularArc')
ca = crv.NativeCurve as CircularArc3d
ed.WriteMessage('\nStartPnt  : {0}', ca.StartPoint.ToString())
ed.WriteMessage('\nEndPnt    : {0}', ca.EndPoint.ToString())
ed.WriteMessage('\nStartAngle: {0}', ca.StartAngle.ToString())
ed.WriteMessage('\nEndAngle  : {0}', ca.EndAngle.ToString())
ed.WriteMessage('\nCentre    : {0}', ca.Center.ToString())
ed.WriteMessage('\nRadius    : {0}\n', ca.Radius.ToString())
ca.Dispose()
elif crv.IsEllipticalArc:
ed.WriteMessage('\nEdge Type: EllipticalArc')
ea = crv.NativeCurve as EllipticalArc3d
ed.WriteMessage('\nStartPnt  : {0}', ea.StartPoint.ToString())
ed.WriteMessage('\nEndPnt    : {0}', ea.EndPoint.ToString())
ed.WriteMessage('\nStartAngle: {0}', ea.StartAngle.ToString())
ed.WriteMessage('\nEndAngle  : {0}', ea.EndAngle.ToString())
ed.WriteMessage('\nCentre    : {0}', ea.Center.ToString())
ed.WriteMessage('\nMajRadius : {0}', ea.MajorRadius.ToString())
ed.WriteMessage('\nMinRadius : {0}\n', ea.MinorRadius.ToString())
ea.Dispose()
elif crv.IsLineSegment:
ed.WriteMessage('\nEdge Type: LineSegment')
l = crv.NativeCurve as LineSegment3d
ed.WriteMessage('\nEdge Vertex 1: {0}', l.StartPoint.ToString())
ed.WriteMessage('\nEdge Vertex 2: {0}\n', l.EndPoint.ToString())
l.Dispose()
elif crv.IsDefined == false:
ed.WriteMessage('\nUndifined Curve entity - no data!')
crv.Dispose()
tr.Commit()
except ex as System.Exception:
ed.WriteMessage('\nException: {0}', ex.Message)
ensure:
tr.Dispose()
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien