.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Layout import code fails in AutoCAD 2015

4 REPLIES 4
Reply
Message 1 of 5
gjmanuel
831 Views, 4 Replies

Layout import code fails in AutoCAD 2015

The following code for importing a layout from another drawing works fine except for in 2015. In 2015, I get an eSetFailed error and the layout does not get created. I would to get any help to get this code working in 2015 also.

 

Public Shared Sub CopyLayerFrom(ByVal lname As String)

       ' Check if the application is still initalizing.

       If Starting Then

           Exit Sub

       End If

       Try

           ' Get the active Document, Editor, and Database objects.

           Dim doc As Document = Core.Application.DocumentManager.MdiActiveDocument

           Dim ed As Editor = doc.Editor

           Dim thisDb As Database = HostApplicationServices.WorkingDatabase

 

           ' Lock our Active Database.

           Using docLock As DocumentLock = Core.Application.DocumentManager.MdiActiveDocument.LockDocument

 

               ' Start a Transaction with the "Standard DB"

               Using tr As Transaction = Db.TransactionManager.StartTransaction

 

                   ' Get our Layer Table from our ""Standard DB"" and double check the Layer exists.

                   Dim lt As LayerTable = CType(tr.GetObject(Db.LayerTableId, OpenMode.ForRead, False), LayerTable)

                   If lt.Has(lname) Then

 

                       ' Get our Layer as an object and make sure it isn't erased (this would cause AutoCAD to crash if we imported a deleted layer).

                       Dim ltr As LayerTableRecord = CType(tr.GetObject(lt(lname), OpenMode.ForRead), LayerTableRecord)

                       If Not ltr.IsErased Then

 

                           ' Start a transaction with the Active Database

                           Using thisTr As Transaction = thisDb.TransactionManager.StartTransaction

                               Dim thisLt As LayerTable = CType(thisTr.GetObject(thisDb.LayerTableId, OpenMode.ForWrite), LayerTable)

                               If thisLt.Has(lname) Then

                                   ' Clone the Layer

                                   Dim map As New IdMapping()

                                   Dim objIDs As ObjectIdCollection = New ObjectIdCollection

                                   objIDs.Add(ltr.ObjectId)

                                   thisDb.WblockCloneObjects(objIDs, thisLt.ObjectId, map, DuplicateRecordCloning.Ignore, False'no defer

                                   thisTr.Commit()

                               Else

                                   ' Clone the Layer

                                   Dim map As New IdMapping()

                                   Dim objIDs As ObjectIdCollection = New ObjectIdCollection

                                   objIDs.Add(ltr.ObjectId)

                                   thisDb.WblockCloneObjects(objIDs, thisLt.ObjectId, map, DuplicateRecordCloning.Replace, False'no defer

                                   thisTr.Commit()

                               End If

                               thisDb.Clayer = thisLt(lname)

                           End Using

                           tr.Commit()

                       End If

                   End If

               End Using

           End Using

       Catch ex As Autodesk.AutoCAD.Runtime.Exception

           MsgBox(ex.Message + vbCrLf + vbCrLf + ex.ToString)

       End Try

   End Sub

 

Tags (3)
4 REPLIES 4
Message 2 of 5
khoa.ho
in reply to: gjmanuel

Your code is for copying a layer between two AutoCAD databases. It is not for importing layouts.

 

Message 3 of 5
gjmanuel
in reply to: khoa.ho

Oops...wrong code...Here is the layout code...

 

Public Shared Sub CopyLayoutFrom(ByVal name As String)
      Try
          ' Get the active Document and Database, Lock the document and start a transaction with it.
          Dim doc As Document = Core.Application.DocumentManager.MdiActiveDocument
          Dim ed As Editor = doc.Editor
          Dim activeDb As Database = doc.Database
          Using docLock As DocumentLock = doc.LockDocument()
              Using tr As Transaction = activeDb.TransactionManager.StartTransaction()
 
                  ' Start a Transaction with our Database and get the DBDictionary from its LayoutDictionary.
                  Using tr2 As Transaction = Db.TransactionManager.StartTransaction()
                      Dim dbob As DBObject = Db.LayoutDictionaryId.GetObject(OpenMode.ForRead)
                      Dim d As DBDictionary = CType(dbob, DBDictionary)
 
                      ' Get the active LayoutDictionary
                      Dim dbobActive As DBObject = activeDb.LayoutDictionaryId.GetObject(OpenMode.ForRead)
                      Dim dActive As DBDictionary = CType(dbobActive, DBDictionary)
 
                      ' Get the current Layout Manager being used by AutoCAD
 
                      Dim manager As LayoutManager = LayoutManager.Current
 
                      If dActive.Contains(name) Then
                          manager.CurrentLayout = name
                      Else
                          If d.Contains(name) Then
                              ' Get Layout and all of its IDs
                              Dim objIDs As New ObjectIdCollection()
                              Dim stdLayout As Layout = CType(d.GetAt(name).GetObject(OpenMode.ForWrite), Layout)
                              Dim stdLayBtr As BlockTableRecord = stdLayout.BlockTableRecordId.GetObject(OpenMode.ForRead)
                              For Each objectId As ObjectId In stdLayBtr
                                  objIDs.Add(objectId)
                              Next
 
                              ' Create a new Layout and clone over the base properties from the stdLayout
                              Dim newLayout As Layout = manager.CreateLayout(name).GetObject(OpenMode.ForWrite)
                              newLayout.CopyFrom(stdLayout)
 
                              ' Clone in our IDs
                              activeDb.WblockCloneObjects(objIDs, newLayout.BlockTableRecordId, New IdMapping(), DuplicateRecordCloning.Replace, False)
 
                              ' Regen the UI and set the Layout as current.
                              doc.Editor.Regen()
                              manager.CurrentLayout = name
                          Else
                              Exit Sub
                          End If
                      End If
                      tr2.Commit()
                      tr.Commit()
                  End Using
              End Using
          End Using
          
      Catch ex As Exception
          MsgBox(ex.Message)
      End Try
  End Sub
Message 4 of 5
moogalm
in reply to: gjmanuel

Hi gjmanuel,

 

Can you please verify with our .NET sample at Import a layout from an external drawing

I have checked it is working on 2015, do let me know if you still have problem.

Message 5 of 5
gjmanuel
in reply to: moogalm

ADN Support,

 

The .NET sample at Import a layout from an external drawing does solve the issue. I did have to add the following line to avoid a access denied error...besides that...it now works in 2015...thanks!

 

Using docLock As DocumentLock = acDoc.LockDocument()

 

Gary

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost