+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Full Member CaveMan's Avatar
    Computer Details
    CaveMan's Computer Details
    Operating System:
    XP - Win7 32 & 64
    Computer:
    64Bit Hybrid
    CPU:
    Intel i7
    RAM:
    12 Gig
    Graphics:
    GeForce GT 220
    Monitor:
    Dual Dell 22"
    Using
    AutoCAD 2011
    Join Date
    Jul 2011
    Location
    South Africa - JHB
    Posts
    40

    Default Add Items to Region - Error?

    Registered forum members do not see this ad.

    Good Day

    Struggling a little with some code to create a region of various items:

    Code gives exception on the Red Highlited line:

    Code:
    
    Using acTrans As Autodesk.AutoCAD.DatabaseServices.Transaction = acCurDb.TransactionManager.StartTransaction()
    Dim acBlkTbl As Autodesk.AutoCAD.DatabaseServices.BlockTable
    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
    Dim acBlkTblRec As Autodesk.AutoCAD.DatabaseServices.BlockTableRecord
    acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
    Dim acDBObjCol As DBObjectCollection = New DBObjectCollection()
    acDBObjCol.Add(Line1)
    acDBObjCol.Add(Line2)
    acDBObjCol.Add(Line3)
    acDBObjCol.Add(Line4)
    acDBObjCol.Add(Line5)
    acDBObjCol.Add(Line6)
    MsgBox(Line1.EndPoint.X)
    Dim myRegionCol As DBObjectCollection = New DBObjectCollection()
    myRegionCol = Autodesk.AutoCAD.DatabaseServices.Region.CreateFromCurves(acDBObjCol)
    Dim acRegion As Autodesk.AutoCAD.DatabaseServices.Region = myRegionCol(0)
    acBlkTblRec.AppendEntity(acRegion)
    acTrans.AddNewlyCreatedDBObject(acRegion, True)
    EndUsing
    Catch ex As Exception
    MsgBox("Error")
    EndTry
    
    The Lines Are cteated with this Public Function: "Friend Function DrawLine"
    which returns the line.

    Lines Generated as such: "
    Line1 = DrawLine(PntSt, ModDrawLine.DTR(AngLine), LineLng)"

    Code:
    
    FriendFunction DrawLine(ByVal startPoint As Point3d, ByVal angle AsDouble, ByVal length AsDouble) As Line
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database
    acDoc.LockDocument()
    Dim id As ObjectId
    Dim line As Line = Nothing
    Using db As Database = HostApplicationServices.WorkingDatabase()
    Dim endpoint As Point3d = ModDrawLine.PolarPoint(startPoint, angle, length)
    startPoint = ModDrawLine.TransformByUCS(startPoint, db)
    endpoint = ModDrawLine.TransformByUCS(endpoint, db)
    Using tr As Transaction = db.TransactionManager.StartTransaction
    Try
    Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, False)
    line = New Line(startPoint, endpoint)
    id = btr.AppendEntity(line)
    db.TransactionManager.AddNewlyCreatedDBObject(line, True)
    tr.Commit()
    Catch ex As Exception
    tr.Abort()
    EndTry
    EndUsing
    EndUsing
    Return line
    EndFunction
    
    Any help welcome please
    Regards
    CaveMan Code

  2. #2
    Super Member fixo's Avatar
    Computer Details
    fixo's Computer Details
    Operating System:
    Windows 7
    Motherboard:
    E7500
    CPU:
    Intel(R)Core(TM)2 DUO CPU 2.93HGz
    RAM:
    4098 Gb
    Graphics:
    1024 Gb
    Using
    AutoCAD 2009
    Join Date
    Jul 2005
    Location
    Pietari, Venäjä
    Posts
    1,598

    Default

    I'm pretty sure your lines didn't create closed area, because this is working code
    To make sureyou better use closed polyline instead or draw lines chainage
    dependend on previous line points, e.g.:
    Code:
     
       ' by Tony Tanzillo 
            Friend Function PolarPoint(basepoint As Point3d, angle As Double, distance As Double) As Point3d
                Return New Point3d(basepoint.X + (distance * Math.Cos(angle)), basepoint.Y + (distance * Math.Sin(angle)), basepoint.Z)
            End Function
            <CommandMethod("demoreg")> _
            Public Sub RegionDemo()
                Dim acCurDb As Database = HostApplicationServices.WorkingDatabase
                Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
                Try
                    Using acTrans As Autodesk.AutoCAD.DatabaseServices.Transaction = acCurDb.TransactionManager.StartTransaction()
                        Dim acBlkTbl As Autodesk.AutoCAD.DatabaseServices.BlockTable
                        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
                        Dim acBlkTblRec As Autodesk.AutoCAD.DatabaseServices.BlockTableRecord
                        acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                        Dim acDBObjCol As DBObjectCollection = New DBObjectCollection()
                        Dim Line1 As Line = DrawLine(New Point3d(0, 0, 0), 0.0, 100)
                        Dim Line2 As Line = DrawLine(Line1.EndPoint, Math.PI / 2 - Math.PI / 6, 100)
                        Dim Line3 As Line = DrawLine(Line2.EndPoint, Math.PI / 2 + Math.PI / 6, 100)
                        Dim Line4 As Line = DrawLine(Line3.EndPoint, Math.PI, 100)
                        Dim Line5 As Line = DrawLine(Line4.EndPoint, Math.PI * 1.5 - Math.PI / 6, 100)
                        Dim Line6 As Line = DrawLine(Line5.EndPoint, Math.PI * 1.5 + Math.PI / 6, 100)
                        acDBObjCol.Add(Line1)
                        acDBObjCol.Add(Line2)
                        acDBObjCol.Add(Line3)
                        acDBObjCol.Add(Line4)
                        acDBObjCol.Add(Line5)
                        acDBObjCol.Add(Line6)
                        ''MsgBox(Line1.EndPoint.X)
                        Dim myRegionCol As DBObjectCollection = New DBObjectCollection()
                        myRegionCol = Autodesk.AutoCAD.DatabaseServices.Region.CreateFromCurves(acDBObjCol)
                        Dim acRegion As Autodesk.AutoCAD.DatabaseServices.Region = DirectCast(myRegionCol(0), Autodesk.AutoCAD.DatabaseServices.Region)
                        acBlkTblRec.AppendEntity(acRegion)
                        acTrans.AddNewlyCreatedDBObject(acRegion, True)
                        ' ---> here you might be want to erase and dispose lines after
                        acTrans.Commit()
                    End Using
                Catch ex As System.Exception
                    MsgBox("Error" + vbLf + ex.ToString)
                End Try
            End Sub
    ~'J'~
    The soul is healed by being with children. - Fyodor Dostoyevsky, novelist (1821-1881)

  3. #3
    Full Member CaveMan's Avatar
    Computer Details
    CaveMan's Computer Details
    Operating System:
    XP - Win7 32 & 64
    Computer:
    64Bit Hybrid
    CPU:
    Intel i7
    RAM:
    12 Gig
    Graphics:
    GeForce GT 220
    Monitor:
    Dual Dell 22"
    Using
    AutoCAD 2011
    Join Date
    Jul 2011
    Location
    South Africa - JHB
    Posts
    40

    Default Example Code Routine

    Hi There

    The function i create in the beginning Drew the lines to the current UCS
    I then decided to want to extrude the items in code
    If i ID'ed the drawn points all were correct, If i manually in AutoCAD i selected the items to cteate a region all worked. But the same items in code give the error to create the region.

    Error i know of: 1) Method i used to create the lines not 100% correct to create a region in code? 2) When i used region in code i may not be on the correct ucs?

    Your routine of code more simple
    Will work through your example

    Appreciate the assistance
    Best Regards
    CaveMan

  4. #4
    Super Member fixo's Avatar
    Computer Details
    fixo's Computer Details
    Operating System:
    Windows 7
    Motherboard:
    E7500
    CPU:
    Intel(R)Core(TM)2 DUO CPU 2.93HGz
    RAM:
    4098 Gb
    Graphics:
    1024 Gb
    Using
    AutoCAD 2009
    Join Date
    Jul 2005
    Location
    Pietari, Venäjä
    Posts
    1,598

    Default

    Nice cave btw
    The soul is healed by being with children. - Fyodor Dostoyevsky, novelist (1821-1881)

  5. #5
    Full Member CaveMan's Avatar
    Computer Details
    CaveMan's Computer Details
    Operating System:
    XP - Win7 32 & 64
    Computer:
    64Bit Hybrid
    CPU:
    Intel i7
    RAM:
    12 Gig
    Graphics:
    GeForce GT 220
    Monitor:
    Dual Dell 22"
    Using
    AutoCAD 2011
    Join Date
    Jul 2011
    Location
    South Africa - JHB
    Posts
    40

    Exclamation Changing UCS Causes Error

    Hi

    when the item is generated in WCS all is file
    If the UCS is changed then Error As Below:

    Error - UCS -Related.png


    what i have picked up is that i have not transformed the points to the new User UCS.

    in progress
    Any ideas most welcome

    Forgive - first time writing code to draw something - so have to discover the correct methology

    In Appreciation
    CaveMan
    Attached Files

  6. #6
    Super Member SEANT's Avatar
    Using
    AutoCAD 2012
    Join Date
    Aug 2005
    Location
    Rhode Island
    Posts
    1,968

    Default

    Your code mildly restructured. I think the original had some transformations out of sequence.
    Attached Files

  7. #7
    Super Member SEANT's Avatar
    Using
    AutoCAD 2012
    Join Date
    Aug 2005
    Location
    Rhode Island
    Posts
    1,968

    Default

    Oops. I forgot to transform the initial point. This should provide a better match with the current UCS origin.


    Code:
     
    <CommandMethod("demoreg")> PublicSub RegionDemo()
            Dim acCurDb As Database= HostApplicationServices.WorkingDatabase
            Dim ed As Editor =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
            Try
                Using acTrans As Autodesk.AutoCAD.DatabaseServices.Transaction = 
    acCurDb.TransactionManager.StartTransaction()
                 Dim acBlkTbl As Autodesk.AutoCAD.DatabaseServices.BlockTable
                 acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
                 Dim acBlkTblRec As Autodesk.AutoCAD.DatabaseServices.BlockTableRecord
                 acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace),OpenMode.ForWrite) 
                 Dim acDBObjCol As DBObjectCollection= New DBObjectCollection()
                 Dim MatToUcs As Matrix3d= GetUcsMatrix(acCurDb)
                 Dim Initial As Point3d= New Point3d().TransformBy(MatToUcs)
                 Dim Line1 As Line= DrawLine(Initial, 0.0, 100, MatToUcs)
                 Dim Line2 As Line= DrawLine(Line1.EndPoint, Math.PI / 2 - Math.PI / 6, 100, MatToUcs)
                 Dim Line3 As Line= DrawLine(Line2.EndPoint, Math.PI / 2 + Math.PI / 6, 100, MatToUcs)
                 Dim Line4 As Line= DrawLine(Line3.EndPoint, Math.PI, 100,MatToUcs)
                 Dim Line5 As Line= DrawLine(Line4.EndPoint, Math.PI * 1.5 - Math.PI / 6, 100, MatToUcs)
                 Dim Line6 As Line= DrawLine(Line5.EndPoint, Math.PI * 1.5 + Math.PI / 6, 100, MatToUcs)
                 acDBObjCol.Add(Line1)
                 acDBObjCol.Add(Line2)
                 acDBObjCol.Add(Line3)
                 acDBObjCol.Add(Line4)
                 acDBObjCol.Add(Line5)
                 acDBObjCol.Add(Line6)
                 Dim myRegionCol As DBObjectCollection= New DBObjectCollection()
                 myRegionCol = Autodesk.AutoCAD.DatabaseServices.Region.CreateFromCurves(acDBObjCol)
                 Dim acRegion As Autodesk.AutoCAD.DatabaseServices.Region = DirectCast(myRegionCol(0),Autodesk.AutoCAD.DatabaseServices.Region)
                   acBlkTblRec.AppendEntity(acRegion)
                   acTrans.AddNewlyCreatedDBObject(acRegion, True)
                    '---> here you might be want to erase and dispose lines after
                   acTrans.Commit()
                End Using
    Catch ex As System.Exception
                MsgBox("Error"+ vbLf + ex.ToString)
            End Try
        End Sub
    Last edited by SEANT; 4th Feb 2012 at 10:53 pm.

  8. #8
    Full Member CaveMan's Avatar
    Computer Details
    CaveMan's Computer Details
    Operating System:
    XP - Win7 32 & 64
    Computer:
    64Bit Hybrid
    CPU:
    Intel i7
    RAM:
    12 Gig
    Graphics:
    GeForce GT 220
    Monitor:
    Dual Dell 22"
    Using
    AutoCAD 2011
    Join Date
    Jul 2011
    Location
    South Africa - JHB
    Posts
    40

    Default Drawing Regions on Differing UCS Planes

    Registered forum members do not see this ad.

    Hi There


    Thanks a lot for the assistance
    My code functioning to what was desired.

    "Dim Line1 As Line= DrawLine(Initial, 0.0, 100, MatToUcs)"
    Your method excellent as highlited above - keeps code simple

    Now stuck on the next challenges

    Cheers
    Thanks a Lot

Similar Threads

  1. Region Inside an other Region
    By ANDREWPCX in forum .NET, ObjectARX & VBA
    Replies: 6
    Last Post: 13th Oct 2010, 10:29 pm
  2. Replies: 6
    Last Post: 15th Sep 2010, 01:07 pm
  3. remove items from a comboBox and fatal error
    By Proctor in forum AutoLISP, Visual LISP & DCL
    Replies: 14
    Last Post: 9th Mar 2008, 07:22 pm
  4. Region help
    By quasifun in forum AutoCAD Drawing Management & Output
    Replies: 3
    Last Post: 26th Sep 2007, 03:12 pm
  5. Region
    By sentinel in forum AutoCAD Drawing Management & Output
    Replies: 6
    Last Post: 3rd Aug 2006, 07:07 pm

Tags for this Thread

Bookmarks

Posting Permissions

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