Author Topic: Unable to view drawing in Layout  (Read 7451 times)

0 Members and 1 Guest are viewing this topic.

Proctor

  • Guest
Unable to view drawing in Layout
« on: May 16, 2008, 04:12:10 PM »
Hello: I create a polyline but don't want to write it to modelspace. if I leave out this line however:

trans.AddNewlyCreatedDBObject(newPl, True)

it gives me an unhandled Access violation Read error when I try to click on any paperspace tab. What am I doing wrong?

Code: [Select]
                  trans = ed.Document.TransactionManager.StartTransaction()

                    Dim bt As BlockTable = doc.Database.BlockTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
                    Dim btr As BlockTableRecord = bt(BlockTableRecord.ModelSpace).GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
                    Dim blkdef As BlockTableRecord = bt(sBlockName).GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
                    Dim docLock As DocumentLock = doc.LockDocument()


                    Dim newPl As Polyline2d = DirectCast(curves.Item(i), Polyline2d)
                    btr.AppendEntity(newPl)

                   [color=red]'this is the line that breaks it:[/color]
                  [color=red]  'trans.AddNewlyCreatedDBObject(newPl, True)[/color]
                    Call AddLayer()
                    ''GET LENGTH OF NEW POLY
                    Dim length As Double
                    length = newPl.Length

                    'use the length of the poly chosen to tell us how many leds(blocks) to insert()
                    Dim noOfLeds As Integer = CInt((length / 12) * num)

                    If dCount = 0 Then
                        dCount = noOfLeds
                    Else
                        dCount = dCount + noOfLeds
                    End If
                    Call WriteLEDCount(dCount, newPl, curves.Count, i)

                    For z As Integer = 0 To noOfLeds - 1

                        Dim pt As Point3d = newPl.GetPointAtDist((12 / num) * z)
                        Dim endPoint As Point3d = newPl.GetPointAtDist(((12 / num) * z) + 0.1)

                        Dim v As Vector3d = endPoint - pt
                        Dim plane As New Plane(New Point3d(0, 0, 0), New Vector3d(0, 0, 1))
                        Dim ang As Double = v.AngleOnPlane(plane)
                        Dim blkref As New BlockReference(pt, bt(sBlockName))
                        blkref.Layer = myLayerName 'Derrived from sub AddLayer()
                        btr.AppendEntity(blkref)
                        blkref.Rotation = ang
                        trans.AddNewlyCreatedDBObject(blkref, True)
                    Next
                    ed.Regen()
                    trans.Commit()
                    trans.Dispose()
                    Application.DocumentManager.MdiActiveDocument.LockDocument.Dispose()



Any helps appreciated,
Proctor

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Unable to view drawing in Layout
« Reply #1 on: May 16, 2008, 04:15:03 PM »

Have you tried to run it in debug mode ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Proctor

  • Guest
Re: Unable to view drawing in Layout
« Reply #2 on: May 16, 2008, 04:18:12 PM »
Hi - thanks for your reply. Yes, I tried; however, it doesn't give me the error until after all my code executes and I click on the paperspace tab ...so was unable to tell anything. I just noticed that if I rem out that one line that adds newly created...it's happy...so assumed it has some connection w/ that.

Can you tell me if I'm starting, commiting and disposing my transaction correctly?

thanks again,
Proctor

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Unable to view drawing in Layout
« Reply #3 on: May 16, 2008, 04:33:40 PM »
If you don't want it to be added to the db, why are you adding it as a block table record ?

btr.AppendEntity(newPl)


just a tip ( and for others) ,
when posting code with issues, it MAY help if the code is complete. ( or at least compilable)

added:
... though I don't do VB, so probably ignore me.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Proctor

  • Guest
Re: Unable to view drawing in Layout
« Reply #4 on: May 16, 2008, 05:02:40 PM »
wow, why the heck am i adding it to the block table record? that's a very good question....

ok...also, sorry that I didn't include all the code...i'll be sure to from now on.

I remmed out the code that appends the poly to the table record, however, now it errors out on the line below:

Dim pt As Point3d = newPl.GetPointAtDist((12 / num) * z)

I'm confused as to when I need to add the poly to the block table record.

here's the full code:
Code: [Select]
Public Shared Sub OffsetMyPoly(ByVal pEstimatePolyId, ByVal dDouble)

            Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor()
            Dim num As Double = iModulesPerUnit 'this number represents the distance between the leds given the product chosen
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database

            Dim trans As Transaction
            trans = ed.Document.TransactionManager.StartTransaction()
            Dim ent As Entity = DirectCast(trans.GetObject(pEstimatePolyId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead), Entity)
            Dim curves As DBObjectCollection

            Dim pl As Polyline2d = DirectCast(ent, Polyline2d)

        Try

            curves = pl.GetOffsetCurves(dDouble)
            trans.Dispose()

            For i As Integer = 0 To curves.Count - 1
       
                    trans = ed.Document.TransactionManager.StartTransaction()

                    Dim bt As BlockTable = doc.Database.BlockTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
                    Dim btr As BlockTableRecord = bt(BlockTableRecord.ModelSpace).GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
                    Dim blkdef As BlockTableRecord = bt(sBlockName).GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
                    Dim docLock As DocumentLock = doc.LockDocument()


                    Dim newPl As Polyline2d = DirectCast(curves.Item(i), Polyline2d)
            [color=red][font=Verdana]
                    'remming this out now
                   'btr.AppendEntity(newPl)[/font][/color]

                    'when this line is added, it creates the offset polyline.
                    'trans.AddNewlyCreatedDBObject(newPl, True)

                    Call AddLayer()
                    ''GET LENGTH OF NEW POLY
                    Dim length As Double
                    length = newPl.Length

                    'use the length of the poly chosen to tell us how many leds(blocks) to insert()
                    Dim noOfLeds As Integer = CInt((length / 12) * num)

                    If dCount = 0 Then
                        dCount = noOfLeds
                    Else
                        dCount = dCount + noOfLeds
                    End If
                    Call WriteLEDCount(dCount, newPl, curves.Count, i)

                    For z As Integer = 0 To noOfLeds - 1

                        [color=red]

                        'erroring out on this line now
                        Dim pt As Point3d = newPl.GetPointAtDist((12 / num) * z)[/color]                         
                        Dim endPoint As Point3d = newPl.GetPointAtDist(((12 / num) * z) + 0.1)

                        Dim v As Vector3d = endPoint - pt
                        Dim plane As New Plane(New Point3d(0, 0, 0), New Vector3d(0, 0, 1))
                        Dim ang As Double = v.AngleOnPlane(plane)
                        Dim blkref As New BlockReference(pt, bt(sBlockName))
                        blkref.Layer = myLayerName 'Derrived from sub AddLayer()
                        btr.AppendEntity(blkref)
                        blkref.Rotation = ang
                        trans.AddNewlyCreatedDBObject(blkref, True)
                    Next
                    ed.Regen()
                    trans.Commit()
                    trans.Dispose()
                    Application.DocumentManager.MdiActiveDocument.LockDocument.Dispose()

            Next
            curves.Dispose()

        Catch ex As Autodesk.AutoCAD.Runtime.Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Unable to view drawing in Layout
« Reply #5 on: May 16, 2008, 05:16:51 PM »
Code: [Select]
however, now it errors out on the line below:
<snip>

.. and the error is ??

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Unable to view drawing in Layout
« Reply #6 on: May 16, 2008, 05:19:50 PM »
and as I posted elsewhere,
Have you considered structuring your code something like this :-

Code: [Select]


      using (AcAp.DocumentLock doclock = ed.Document.LockDocument())
      {
        using (AcDb.Transaction tr = db.TransactionManager.StartTransaction())
        {

            /// do mojo here

            tr.Commit();

        }
      }

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Proctor

  • Guest
Re: Unable to view drawing in Layout
« Reply #7 on: May 16, 2008, 05:20:45 PM »
I get this error:

eInvalidInput


Proctor

  • Guest
Re: Unable to view drawing in Layout
« Reply #8 on: May 16, 2008, 05:21:40 PM »
I just saw your post ....

let me try that....

Thank you

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Unable to view drawing in Layout
« Reply #9 on: May 16, 2008, 05:23:34 PM »
Have you stepped through the code in debug mode .. with the LocalVariables pane open ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Proctor

  • Guest
Re: Unable to view drawing in Layout
« Reply #10 on: May 16, 2008, 05:25:20 PM »
LocalVariables pane?

Glenn R

  • Guest
Re: Unable to view drawing in Layout
« Reply #11 on: May 16, 2008, 05:33:08 PM »
If you are just creating a polyline for calculations and don't want to add it to the dwg, then ditch the transaction altogether and wrap the creation of the polyline in a 'using' statement so it's destroyed when it's done.

Posting the same question on multiple forums is considered by some as 'bad form' by the way.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Unable to view drawing in Layout
« Reply #12 on: May 16, 2008, 05:40:06 PM »
LocalVariables pane?

a piccy ...
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Proctor

  • Guest
Re: Unable to view drawing in Layout
« Reply #13 on: May 16, 2008, 06:45:16 PM »
I didn't realize the effects of posting the same topic to multiple sites till I Kerry answering for me in both ...and felt bad. I'm really sorry.

Kerry...thanks for the pic on using local variable. I added the Using statement for the transaction and it's still erroring on the same line. as I step through using looking at the local variable, i see that the acadobject says HRESULT E_Fail has been returned from a call to a com component. The poly showing other properties...e.g. length.

Code: [Select]

Public Shared Sub OffsetMyPoly_2Row(ByVal pEstimatePolyId, ByVal dDouble)
        Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor()
        Dim num As Double = iModulesPerUnit 'this number represents the distance between the leds given the product chosen
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database

        Using docLock As DocumentLock = ed.Document.LockDocument()
            Using trans As Transaction = ed.Document.TransactionManager.StartTransaction()

                Try

                    '  Dim trans As Transaction
                    ' trans = ed.Document.TransactionManager.StartTransaction()
                    'using AcAp.DocumentLock doclock = ed.Document.LockDocument()

                    Dim ent As Entity = DirectCast(trans.GetObject(pEstimatePolyId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead), Entity)
                    Dim curves As DBObjectCollection

                    Dim pl As Polyline2d = DirectCast(ent, Polyline2d)
                    curves = pl.GetOffsetCurves(dDouble)


                    For i As Integer = 0 To curves.Count - 1
                        'trans = ed.Document.TransactionManager.StartTransaction()

                        Dim bt As BlockTable = doc.Database.BlockTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
                        Dim btr As BlockTableRecord = bt(BlockTableRecord.ModelSpace).GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
                        Dim blkdef As BlockTableRecord = bt(sBlockName).GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
                        'Dim docLock As DocumentLock = doc.LockDocument()

                        Dim newPl As Polyline2d = DirectCast(curves.Item(i), Polyline2d)
                        'btr.AppendEntity(newPl)
                        'trans.AddNewlyCreatedDBObject(newPl, True)

                        Call AddLayer()
                        ''GET LENGTH OF NEW POLY
                        Dim length As Double
                        length = newPl.Length

                        'use the length of the poly chosen to tell us how many leds(blocks) to insert()
                        Dim noOfLeds As Integer = CInt((length / 12) * num)

                        If dCount = 0 Then
                            dCount = noOfLeds
                        Else
                            dCount = dCount + noOfLeds
                        End If
                        Call WriteLEDCount(dCount, newPl, curves.Count, i)

                        'MEASURE
                        For z As Integer = 0 To noOfLeds - 1
                            Dim pt As Point3d = newPl.GetPointAtDist((12 / num) * z)
                            Dim endPoint As Point3d = newPl.GetPointAtDist(((12 / num) * z) + 0.1)

                            Dim v As Vector3d = endPoint - pt
                            Dim plane As New Plane(New Point3d(0, 0, 0), New Vector3d(0, 0, 1))
                            Dim ang As Double = v.AngleOnPlane(plane)
                            Dim blkref As New BlockReference(pt, bt(sBlockName))
                            blkref.Layer = myLayerName 'Derrived from sub AddLayer()
                            btr.AppendEntity(blkref)
                            blkref.Rotation = ang
                            trans.AddNewlyCreatedDBObject(blkref, True)
                        Next
                        ed.Regen()
                        trans.Commit()
                        'trans.Dispose()
                        'Application.DocumentManager.MdiActiveDocument.LockDocument.Dispose()
                        ''End If
                    Next
                    curves.Dispose()


                Catch ex As Autodesk.AutoCAD.Runtime.Exception
                    MsgBox(ex.Message)
                Finally
                End Try
            End Using
        End Using
    End Sub


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Unable to view drawing in Layout
« Reply #14 on: May 16, 2008, 06:59:15 PM »
Does VB require that parameters be typed ?

Public Shared Sub OffsetMyPoly_2Row(ByVal pEstimatePolyId, ByVal dDouble)

as you have it
or

Public Shared Sub OffsetMyPoly_2Row(ByVal pEstimatePolyId As whatever , ByVal dDouble As whatever )


What are your includes ?

vb brings be out in boils , and I'm a little busy,

if the message is 'eInvalidInput'
where do you think it is occuring ?

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.