See the top rated post in this thread. Click here

Results 1 to 3 of 3

Thread: Help draw polyline with AddVertexAt vb.net!!!

  1. #1
    Login to Give a bone
    0

    Default Help draw polyline with AddVertexAt vb.net!!!

    i just change from vba cad to .net.
    it's so difficult.

    my code draw polyline then addvextex until escape command
    but have problem at red code
    if i haven't red code i can't see polyline in screen autocad
    if i have it i can not addvextex and error
    Attachment 106668

    any idea for issue??
    + i want to see polyline in autocad

    tks all.

    Code:
    Imports AutoCAD
    
    Imports Autodesk.AutoCAD.ApplicationServices
    Imports Autodesk.AutoCAD.EditorInput
    Imports Autodesk.AutoCAD.Runtime
    Imports System
    Imports System.Windows.Forms
    Imports Autodesk.AutoCAD.DatabaseServices
    Imports Autodesk.AutoCAD.Geometry
    
    
     Public Class THU
            Public Const WidthFactor As Integer = -10
            <CommandMethod("vepl")> _
            Public Sub VEPOLY()
    
    
                Dim dblNew(0 To 2) As Double
                Dim lngLastVertex As Long
                Dim varPick As Object
                Dim varPick1 As Object
                Dim varWCS As Object
                Dim dblPoints(3) As Double
                Dim Lineweight As Double
                Dim varpoint, varpoint1 As Object
                Dim i, min, max, NoOfIndices
    
    
                Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
                Dim db As Database = doc.Database
                Dim ed As Editor = doc.Editor
    
                Dim pPtRes As PromptPointResult
                Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
    
                On Error Resume Next
    
    
                lngLastVertex = 2
                Lineweight = ThisDrawing.ActiveLayer.Lineweight * WidthFactor
               
    
                '' Prompt for the start point
                pPtOpts.Message = vbLf & "Enter the start point of the line: "
                pPtRes = doc.Editor.GetPoint(pPtOpts)
                Dim ptStart As Point3d = pPtRes.Value
                Dim StarPt As Point2d = New Point2d(pPtRes.Value.X, pPtRes.Value.Y)
    
                '' Exit if the user presses ESC or cancels the command
                If pPtRes.Status = PromptStatus.Cancel Then Exit Sub
    
                '' Prompt for the end point
                pPtOpts.Message = vbLf & "\nSelect Next Point: "
                'pPtOpts.SetMessageAndKeywords("\nSelect Next Point: or Exit [Y]", "Yes")
                'pPtOpts.AppendKeywordsToMessage = True
                'pPtOpts.AllowArbitraryInput = True
                pPtOpts.UseBasePoint = True
                pPtOpts.BasePoint = ptStart
                pPtRes = doc.Editor.GetPoint(pPtOpts)
    
                Dim PtEnd As Point3d = pPtRes.Value
                Dim EndPT As Point2d = New Point2d(pPtRes.Value.X, pPtRes.Value.Y)
    
                If pPtRes.Status = PromptStatus.Cancel Then Exit Sub
    
                Using acTrans As Transaction = db.TransactionManager.StartTransaction
    
                    Dim acBlkTbl As BlockTable = acTrans.GetObject(db.BlockTableId, OpenMode.ForRead)
                    '' Open the Block table record Model space for write
                    Dim acBlkTblRec As BlockTableRecord = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
    
                    Dim acPoly As Polyline = New Polyline()
    
                    acPoly.SetDatabaseDefaults()
                    acPoly.AddVertexAt(0, StarPt, 0, 0, 0)
                    acPoly.AddVertexAt(1, EndPT, 0, 0, 0)
    
    
                    '' Add the new object to the block table record and the transaction
                    acBlkTblRec.AppendEntity(acPoly)
                    acTrans.AddNewlyCreatedDBObject(acPoly, True)
                    acTrans.Commit()
                   
                    'ed.UpdateScreen()
                
                    Do
    
                        pPtOpts.Message = vbLf & "Enter the Next point of the Polyline: "
                        pPtOpts.UseBasePoint = True
                        pPtOpts.BasePoint = PtEnd
                        pPtRes = doc.Editor.GetPoint(pPtOpts)
    
                        Dim PtNext As Point2d = New Point2d(pPtRes.Value.X, pPtRes.Value.Y)
    
                        If pPtRes.Status = PromptStatus.Cancel Then
                            PtEnd = pPtRes.Value
                            Exit Do
    
                        End If
    
                        'acPoly.AddVertexAt(acPoly.NumberOfVertices + 1, PtNext, 0, 0, 0)
                        'ed.Regen()
                        acPoly.AddVertexAt(lngLastVertex, PtNext, 0, 0, 0)
    
                        lngLastVertex = lngLastVertex + 1
    
                       
                       
                        acBlkTblRec.AppendEntity(acPoly)
                        acTrans.AddNewlyCreatedDBObject(acPoly, True)
                        acTrans.Commit()
    
                      
                    Loop While 1
    
               
                    'AddHandler acPoly.Modified, AddressOf acpolymod
                    acTrans.Commit()
    
                End Using
    
            End Sub

  2. #2
    Member
    Join Date
    2009-04
    Posts
    13
    Login to Give a bone
    1

    Default Re: Help draw polyline with AddVertexAt vb.net!!!

    Your code have TOO MANY Transaction.Commit() calls.

    Whenever Commit() is called, the Transaction is, well, committed and closed. That is why, after the code in red is executed, the code after generates error, because the Transaction is done and closed. You need to remove all the Commit() call in the middle and only keep the very last one. Remember, only either Commit(), or Abort() a Transaction at the very end of the process where the Transaction is needed.

  3. #3
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: Help draw polyline with AddVertexAt vb.net!!!

    Quote Originally Posted by norman.yuan View Post
    Your code have TOO MANY Transaction.Commit() calls.

    Whenever Commit() is called, the Transaction is, well, committed and closed. That is why, after the code in red is executed, the code after generates error, because the Transaction is done and closed. You need to remove all the Commit() call in the middle and only keep the very last one. Remember, only either Commit(), or Abort() a Transaction at the very end of the process where the Transaction is needed.
    Correct; Commit() should be called only once within a given Transaction.

    However, one can also employ nested Transactions.

    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

Similar Threads

  1. draw polyline as a series of arrows
    By motee in forum AutoLISP
    Replies: 8
    Last Post: 2013-11-11, 06:23 AM
  2. draw a polyline relative to other objects
    By fabrice.Demiel in forum AutoLISP
    Replies: 5
    Last Post: 2012-02-03, 08:34 PM
  3. draw polyline on selected layer; help
    By ReachAndre in forum AutoLISP
    Replies: 6
    Last Post: 2007-10-03, 12:15 PM
  4. How to draw a 3d polyline from a list of points?
    By GreyHippo in forum AutoLISP
    Replies: 2
    Last Post: 2006-12-15, 06:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •