Please check the problem with this code. loop draw error.

Please check the problem with this code. loop draw error.

mirwing76
Contributor Contributor
850 Views
4 Replies
Message 1 of 5

Please check the problem with this code. loop draw error.

mirwing76
Contributor
Contributor

one Circle Create OK! , next Circle Create Error!

 

Sub New(ByVal infoPipe As textProcess)
Dim drawCount As Integer
drawCount = infoPipe.circlePnt.Count
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database

 

        Using tr As Transaction = db.TransactionManager.StartTransaction()
        Try
                For i = 0 To 0 'drawCount - 1
                        Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, True, True)   '' Error Line
                        Dim objCircle As New Circle(infoPipe.circlePnt(i), Vector3d.ZAxis, infoPipe.circleSize(i) / 2)
                        btr.AppendEntity(objCircle)

                        tr.AddNewlyCreatedDBObject(objCircle, True)
                        tr.Commit()
                Next
        Catch ex As autodesk.AutoCAD.Runtime.Exception
                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Exception : " & ex.Message)
        Finally
                tr.Dispose()
        End Try

 

        End Using
End Sub

 

 

Please Help me.

0 Likes
Accepted solutions (1)
851 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant
Accepted solution

Hi,

 

What about instantiating the current space BlockTableRecord only once outside of the for loop?

 

Also, the two last arguments are superfluous (the current space cannot be erased or on a locked layer):

Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 5

sdphg
Enthusiast
Enthusiast

Try to move the tr.Commit() outside the loop. because once the transaction has been committed, you can't use it anymore.

 Try
Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, True, True) For i = 0 To 0 'drawCount - 1 Dim objCircle As New Circle(infoPipe.circlePnt(i), Vector3d.ZAxis, infoPipe.circleSize(i) / 2) btr.AppendEntity(objCircle) tr.AddNewlyCreatedDBObject(objCircle, True) Next tr.Commit() Catch ex As autodesk.AutoCAD.Runtime.Exception

and _gile's advise also correct, if you move the commit outside the loop, that line may not throw exception, but you had better  move that line before the loop to avoid performance issue.

 

Message 4 of 5

mirwing76
Contributor
Contributor

I solved it this way.

Thank you for your continued interest.

 

 

 

Public Class drawPipe
'Private _object As textProcess

 

Sub New(ByVal pipeInfo As textProcess)
    '_object = pipeInfo
    For i = 0 To pipeInfo.circlePnt.Count - 1
        cBlockTable(pipeInfo.circlePnt(i), pipeInfo.circleSize(i), pipeInfo.hasHatch(i))
    Next
End Sub

 


Sub cBlockTable(ByVal cntPnt As Point3d, ByVal dia As Double, ByVal hasHatch As Boolean)
    Dim radius As Double = dia / 2
    Dim doc As Document = Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database
    Using tr As Transaction = db.TransactionManager.StartTransaction()
        Try
            Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, True, True)
            Dim objCircle As New Circle(cntPnt, Vector3d.ZAxis, radius)

            'Dim defNomal As Vector3d = Vector3d.ZAxis
            btr.AppendEntity(objCircle)
            tr.AddNewlyCreatedDBObject(objCircle, True)


            If hasHatch Then
                Dim objHatch As New Hatch()
                Dim idCircle As ObjectId = objCircle.ObjectId
                Dim idsCircle As New ObjectIdCollection()
                idsCircle.Add(idCircle)
                ' Create Hatch On objCircle

                objHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31")
                objHatch.Normal = Vector3d.ZAxis
                objHatch.PatternScale = 3
                objHatch.Elevation = 0.0

                objHatch.AppendLoop(HatchLoopTypes.Default, idsCircle)
                btr.AppendEntity(objHatch)
                tr.AddNewlyCreatedDBObject(objHatch, True)
            End If

 

            tr.Commit()

 

            Catch ex As Autodesk.AutoCAD.Runtime.Exception

                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Exception : " & ex.Message)
            Finally

                tr.Dispose()
        End Try
    End Using
End Sub

End Class

0 Likes
Message 5 of 5

mirwing76
Contributor
Contributor

Thank you for your continued interest.

0 Likes