I suspect the solution to my problem is pretty simple, but I'll probably get pretty heavy on the explanation of what I am doing, hopefully to alleviate any confusion.
This code was originally a VBA macro, but due to its terrible x64 performance, I have been transitioning it to VB.NET. I have tried to keep it as close to the VBA code as possible, until I can get it to produce the same results as the VBA source.
In the meantime, the code seems to function correctly, however, when I select larger object sets (the maximum number is not consistent), I get an eGeneralModelingFailure from VB.NET and "The Boolean operation on solid/or surface bodies failed. Modeling Operation Error: Access Violation" in the AutoCAD commandline.
My code is shown, but I have pared it down to what I think are the essential concepts. The error is occuring in the IdentifyObject Sub, when I set sRegion1 to a section using SectionPlane. I am not interested in keeping the results of sRegion1 or the XPlode array once I've made them, hence I don't add them to the database or anything. I am concerned that maybe I should be disposing them or clearing them somehow. Any ideas or advice would be appreciated. Thanks!
Imports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.Geometry Imports Autodesk.AutoCAD.EditorInput Imports System.Windows.Forms Imports System.Security.Permissions Imports System.Security Imports System.IO Public Class FLACSmcrWriter <CommandMethod("A2F")> _ Public Sub AutoCAD2FLACS() Dim ObjectArray() As ObjectId 'Solid3d 'Got user selected 3dsolids via filter and put all ObjectIDs into ObjectArray Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.D
ocumentManager.MdiActiveDocument Dim acCurDb As Database = acDoc.Database Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction() For I = 0 To UBound(ObjectArray) objFlacs = New FLACSObject 'a custom object ProcessObject(acTrans.GetObject(ObjectArray(I), OpenMode.ForWrite), objFlacs) Next I End Using 'Writing some stuff out to text files End Sub Private Sub ProcessObject(ByVal obj As Solid3d, ByRef objFlacs As FLACSObject) 'Minor custom object setup 'Determine object shape, orientation, and properties Call IdentifyObject(obj, Direction, Length, Radius, Width, Height, minP) 'Some other stuff End Sub Private Sub IdentifyObject(ByRef obj As Solid3d, ByRef Direction As String, ByRef Length As Double, ByRef Radius As Double, ByRef Width As Double, ByRef Height As Double, ByRef minP As Point3d) Dim maxP As Point3d Dim pnt1 As Point3d Dim sRegion1 As Region Dim xPlode As DBObjectCollection = New DBObjectCollection Dim xCount As Long Dim eye As Long rCount = 0 xObj = 0 pnt1 = ChangePoint(minP, minP, minP) minP = obj.GeometricExtents.MinPoint maxP = obj.GeometricExtents.MaxPoint Dim SectionPlane As Plane = New Plane(pnt1, pnt2, maxP) sRegion1 = obj.GetSection(SectionPlane) If Not sRegion1 Is Nothing Then sRegion1.Explode(xPlode) xCount = xPlode.Count - 1 For eye = 0 To xCount If TypeOf xPlode.Item(eye) Is Ellipse Or TypeOf xPlode.Item(eye) Is Circle Then xObj = 1 End If Next eye Else Exit Sub End If xPlode.Clear() 'Lots of other similar tests ... End Sub End Class
Solved! Go to Solution.
Solved! by wein3967. See the answer in context.
It certainly looks like my problem revolved around not properly disposing of objects as they were created. Since I was iterating through thousands of objects, they were really piling up.
In the end, I needed to make sure I performed the .Dispose() on my Regions, Planes, and xPlode.Items (lines, ellipses, and circles) as soon as I was finished with them.
Thanks to all took a look and/or offered suggestions.
I believe you should be disposing the Region and the objects in the xplode collection, but I don't think failing to do so would give you the error you are getting.
No, I don't believe that is a typo. I don't have the IDE (intellisense) in front of me, but I am pretty sure you can define a plane with 3 points. It certainly doesn't throw any errors anyway.
Oh, yes, pnt2 is defined in my "complete" code. I'm sure it was just swept up in several of the Dim statements I removed.
I think he was pointing out that pnt2 is not defined in the code you posted, but I am assuming that is just because its definition got axed when you cut down your code for posting. You can certainly define a plane with 3 points.
I have tried putting a .Dispose() for both the sRegion1 and the xPlote objects, but it didn't seem to resolve the error. I was also kind of assuming that since that code is inside the "Using" statement, they would be properly disposed of by the code when the "End Using" was hit. Of course, the execution isn't making it that far right now.
Since you are not adding them to the database, those objects are not associated with the transaction, and will therefore not be disposed by the using statement on the transaction.
Like I said, I had a feeling that was not what was causing your problem, but I do believe they need to be disposed separately.
I'll certainly add the Dispose() statments back in, since regardless of the error, that is the proper practice. Thanks.
Check all input if the error occurs at:
sRegion1 = obj.GetSection(SectionPlane)
It appears that Autodesk.AutoCAD.Geometry.Plane will not throw an exception if the three points are collinear. It just returns an invalid Plane (with a normal of 0.0,0.0,0.0).
Solid3Ds use a different tolerance setting than conventional 2D geometry – a GetSection may falter if the points describe a triangle that is too thin a sliver.
Thanks for your suggestion. I went ahead and verified that I was using the appropriate 3 points to create the plane, and that the created plane's normal was correct.
Additionally, I ran the code until the error occurred. I then identified the object in the model and re-ran the code against that single object. At that point the code completed successfully.
It is things like that, which were making me think that perhaps I wasn't disposing or clearing reasources properly. Since the code never errors on the same object, and if I select only one object, the code completes succesfully.