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

copy a layer from a drawing to another

8 REPLIES 8
Reply
Message 1 of 9
Anonymous
737 Views, 8 Replies

copy a layer from a drawing to another

Hello, Guys.

My name is Bruno. I'm trying to copy a layer from a drawing to another, using dot net, but I'm not getting to do it. I already did it with VBA (using copybjects), and with ARX (using deepclone), but when I do using dot net, appears a error message when I open the layer properties manager: "no active transactions".

Here is the code I'm using:

Private Sub copyLayerFromAnotherDrawing(ByVal layerStr As String, ByVal fromFileStr As String)
Dim LT, myLt As LayerTable
Dim tm, myTm As DBTransMan
Dim ta, myTa As Transaction
Dim dbMe, dbFrom As New Database
Dim lyr, copy As LayerTableRecord
Dim itens As SymbolTableEnumerator
Dim id As ObjectId
Dim idMap As New IdMapping()


dbMe = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
dbFrom.ReadDwgFile(fromFileStr, IO.FileShare.Read, True, "")

tm = dbFrom.TransactionManager
ta = tm.StartTransaction
LT = ta.GetObject(dbFrom.LayerTableId, OpenMode.ForRead, False)

myTm = dbMe.TransactionManager
myTa = myTm.StartTransaction
myLt = myTa.GetObject(dbMe.LayerTableId, OpenMode.ForWrite, False)

itens = LT.GetEnumerator
Do While itens.MoveNext
id = itens.Current
lyr = tm.GetObject(id, OpenMode.ForRead)
If lyr.Name.CompareTo(layerStr) = 0 Then
copy = lyr.DeepClone(myLt, idMap, False)
myLt.Add(copy)
myTa.AddNewlyCreatedDBObject(copy, True)
Exit Do
End If

Loop
myTa.Commit()
ta.Commit()
End Sub

Thanx for de help.
8 REPLIES 8
Message 2 of 9
Anonymous
in reply to: Anonymous

Hi Bruno
Try this instead

Public Sub CopyLayerFrom(ByVal lname As String, ByVal fname As String)
Try
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim thisDb As Database = HostApplicationServices.WorkingDatabase
Using docLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument
Try
Using db As Database = New Database(False, False)
Try
db.ReadDwgFile(fname, System.IO.FileShare.Read, True, Nothing)
Using tr As Transaction = db.TransactionManager.StartTransaction
Try
Dim lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead, False)
Dim ltr As LayerTableRecord = tr.GetObject(lt(lname), OpenMode.ForRead)
Using thisTr As Transaction = thisDb.TransactionManager.StartTransaction
Dim thisLt As LayerTable = thisTr.GetObject(thisDb.LayerTableId, OpenMode.ForWrite)
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 Using
tr.Commit()
Finally
CType(tr, IDisposable).Dispose()
End Try
End Using
Finally
CType(db, IDisposable).Dispose()
End Try
End Using
Finally
CType(docLock, IDisposable).Dispose()
End Try
End Using
Catch ex As Autodesk.AutoCAD.Runtime.Exception
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
ed.WriteMessage(ControlChars.CrLf & "Error {0} Copy layer from other document" & ControlChars.CrLf, ex)
ed.WriteMessage(ex.StackTrace)
End Try
End Sub

~'J'~
Message 3 of 9
Anonymous
in reply to: Anonymous

Fatty - At the risk of discouraging you from posting code, I am going to say this anways, because I think others that come here to get help and learn to code using the .NET API, are entitled to know it.

I mentioned this to you in the past, but you chosen to ignore it for whatever reason, and as a result, you are posting code that does not serve as a good example for anyone.

So mainly for the benefit of others, here it goes yet once again:

You do not call Dispose() on objects that are under control of 'Using', because that is what 'Using' is for, to begin with. 'Using' automatically calls Dispose() for you.

And so, this:

Using obj As MyDisposable = New MyDisposable()
obj.DoSomething() ' use 'obj' here
End Using

is exactly the same as this:

Dim obj As MyDisposable = New MyDisposable()
Try
obj.DoSomething() ' use 'obj' here
Finally
obj.Dispose()
End Try

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5831311@discussion.autodesk.com...
Hi Bruno
Try this instead

Public Sub CopyLayerFrom(ByVal lname As String, ByVal fname As String)
Try
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim thisDb As Database = HostApplicationServices.WorkingDatabase
Using docLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument
Try
Using db As Database = New Database(False, False)
Try
db.ReadDwgFile(fname, System.IO.FileShare.Read, True, Nothing)
Using tr As Transaction = db.TransactionManager.StartTransaction
Try
Dim lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead, False)
Dim ltr As LayerTableRecord = tr.GetObject(lt(lname), OpenMode.ForRead)
Using thisTr As Transaction = thisDb.TransactionManager.StartTransaction
Dim thisLt As LayerTable = thisTr.GetObject(thisDb.LayerTableId, OpenMode.ForWrite)
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 Using
tr.Commit()
Finally
CType(tr, IDisposable).Dispose()
End Try
End Using
Finally
CType(db, IDisposable).Dispose()
End Try
End Using
Finally
CType(docLock, IDisposable).Dispose()
End Try
End Using
Catch ex As Autodesk.AutoCAD.Runtime.Exception
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
ed.WriteMessage(ControlChars.CrLf & "Error {0} Copy layer from other document" & ControlChars.CrLf, ex)
ed.WriteMessage(ex.StackTrace)
End Try
End Sub

~'J'~
Message 4 of 9
Anonymous
in reply to: Anonymous

Sorry, Tony
You are right completely, as always
I have been in a hurry when I have writing
this code
Thanks again for the lesson about 'Using'

With deep regards,

~'J'~
Message 5 of 9
Anonymous
in reply to: Anonymous

Here is edited version, I hope it will correct

Public Sub CopyLayerFrom(ByVal lname As String, ByVal fname As String)
Try
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim thisDb As Database = HostApplicationServices.WorkingDatabase
Using docLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument
Using db As Database = New Database(False, False)
db.ReadDwgFile(fname, System.IO.FileShare.Read, True, Nothing)
Using tr As Transaction = db.TransactionManager.StartTransaction
Dim lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead, False)
If lt.Has(lname) Then
Dim ltr As LayerTableRecord = tr.GetObject(lt(lname), OpenMode.ForRead)
If Not ltr.IsErased Then
Using thisTr As Transaction = thisDb.TransactionManager.StartTransaction
Dim thisLt As LayerTable = thisTr.GetObject(thisDb.LayerTableId, OpenMode.ForWrite)
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 Using
tr.Commit()
End If
End If
End Using
End Using
End Using
Catch ex As Autodesk.AutoCAD.Runtime.Exception
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
ed.WriteMessage(ControlChars.CrLf & "Error {0} Copy layer from other document" & ControlChars.CrLf, ex)
ed.WriteMessage(ex.StackTrace)
End Try
End Sub

~'J'~
Message 6 of 9
Anonymous
in reply to: Anonymous

Hello, Fatty. Thanx for the answer.

I tryied this before, and got the following error: "filler error", when I used WblockCloneObjects method.
Message 7 of 9
Anonymous
in reply to: Anonymous

Hi Bruno,
I don't know why this not working for you
This worked for me 10 times from 10 with several
drawings and layers
A2008/VS2005

~'J'~
Message 8 of 9
Anonymous
in reply to: Anonymous

I Tryied again and worked this time. Your code isn't identical to the code a wrote before. I don't know what I was doing wrong. I've just copyied your code and worked.


Thanx, Fatty. I was almost givin up.

Bruno Neves Pires Silva
Message 9 of 9
Anonymous
in reply to: Anonymous

Hi Bruno
Glad if this worked now
Happy computing 🙂

~'J'~

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