Author Topic: eLockViolation - Transfering Layer/LineType Info (open Dwg to SideDB)  (Read 3079 times)

0 Members and 1 Guest are viewing this topic.

GumbyCAD

  • Newt
  • Posts: 84
Hi Guys,

I am transfering information from a Current Drawing that is open to a side Database.

But when I try to copy across the LineType I get a eLockViolation error.

Any help would be greatly appreciated.

Offenending Piece of code is:
Code: [Select]
DB.WblockCloneObjects(CloneOIDS, mLayers(FoundIndex).SetLLineTypeTableID, idMap, DuplicateRecordCloning.Replace, False)

Code: [Select]
            DB = New Database(False, True) 'Open DWG to be copied to
            DB.ReadDwgFile(ExistingFileName, FileOpenMode.OpenForReadAndWriteNoShare, False, "")

            Using DB

                Changed = False

                Dim layer As LayerTableRecord
                Using tr As Transaction = DB.TransactionManager.StartTransaction()
                    Dim lt As LayerTable = TryCast(tr.GetObject(DB.LayerTableId, OpenMode.ForWrite), LayerTable)
                    For Each layerId As ObjectId In lt
                        layer = TryCast(tr.GetObject(layerId, OpenMode.ForWrite), LayerTableRecord)
                       
                        'List Of Classes that contain my Layers that need to be copied.
                        Dim FoundIndex As Integer = mLayers.FindIndex(Function(Lay As clsLayerDetails) Lay.LayerName = layer.Name)

                        If FoundIndex > -1 Then 'Has found a Layer to needs to be copied

                            mLayers(FoundIndex).LayerFound_Res = True

                            'Modify layer Status to suit
                            If mLayers(FoundIndex).eFreeze Then layer.IsFrozen = mLayers(FoundIndex).IsFrozen
                            If mLayers(FoundIndex).eOnOff Then layer.IsOff = mLayers(FoundIndex).IsOff
                            If mLayers(FoundIndex).ePlot Then layer.IsPlottable = mLayers(FoundIndex).IsPlotAble
                            If mLayers(FoundIndex).eLock Then layer.IsLocked = mLayers(FoundIndex).IsLocked

                            'Change Color
                            If mLayers(FoundIndex).eLColor Then layer.Color = mLayers(FoundIndex).SetLColor

                            'If line type needs to be changed
                            If mLayers(FoundIndex).eLLineType Then

                                Dim ltt As LinetypeTable = TryCast(tr.GetObject(DB.LinetypeTableId, OpenMode.ForWrite), LinetypeTable)
                               
                                'Check to see if line type i missing in Destination Database (SetLLineType is ObjectID of linetype)
                                If ltt.Has(mLayers(FoundIndex).SetLLineType) = False Then

                                    Dim CloneOIDS As New ObjectIdCollection
                                    CloneOIDS.Add(mLayers(FoundIndex).SetLLineType)

                                    Dim idMap As IdMapping = New IdMapping()
                                    DB.WblockCloneObjects(CloneOIDS, mLayers(FoundIndex).SetLLineTypeTableID, idMap, DuplicateRecordCloning.Replace, False) 'FAIL HERE eLockViolation

                                End If

                                layer.LinetypeObjectId = mLayers(FoundIndex).SetLLineType

                            End If

                            Changed = True

                        End If

                    Next
                    tr.Commit()
                End Using

                If Changed Then
                    DB.RetainOriginalThumbnailBitmap = True
                    DB.SaveAs(SaveAsFileName, DB.OriginalFileVersion) ' DwgVersion.Current)
                End If

            End Using

            DB = Nothing

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: eLockViolation - Transfering Layer/LineType Info (open Dwg to SideDB)
« Reply #1 on: March 05, 2014, 12:21:31 AM »
What is your
[CommandMethod(..... CommandFlags.XXXX

.. is it set to .Session ??    ie. in  Application context

changing the database in Session mode without locking the database may be the issue.


... or is this called from a modeless dialog ? ?

same issue !


Have a look at Document.LockDocument()
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: eLockViolation - Transfering Layer/LineType Info (open Dwg to SideDB)
« Reply #2 on: March 05, 2014, 01:42:33 AM »
I had these referenced in some code I was playing with

Copy Objects Between Databases (.NET)
http://docs.autodesk.com/ACD/2014/ENU/files/GUID-E02A8AAF-61FF-4C72-8960-0AEEBBEC2594.htm

Lock and Unlock a Document (.NET)
http://docs.autodesk.com/ACD/2014/ENU/files/GUID-A2CD7540-69C5-4085-BCE8-2A8ACE16BFDD.htm

added:
fixed links.
« Last Edit: March 05, 2014, 02:19:05 AM by Kerry »
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.

GumbyCAD

  • Newt
  • Posts: 84
Re: eLockViolation - Transfering Layer/LineType Info (open Dwg to SideDB)
« Reply #3 on: March 05, 2014, 01:54:23 AM »
I read both these topics (unless I missed something) it copy objects with Databases that are in Document Open Databases not side Database.
 :-(

I am coping from Open Document Database to Side Database

GumbyCAD

  • Newt
  • Posts: 84
Re: eLockViolation - Transfering Layer/LineType Info (open Dwg to SideDB)
« Reply #4 on: March 05, 2014, 02:17:31 AM »
So If I run the following code everything works well!
But of cause it is missing the LineStyles segment.     :cry:


Code - vb.net: [Select]
  1.  
  2.             Dim CurrentDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  3.             Dim CurrentDB As Database = CurrentDoc.Database
  4.  
  5.             Dim Changed As Boolean = False
  6.  
  7.             Using DocKock As DocumentLock = CurrentDoc.LockDocument
  8.  
  9.                 SideDB = New Database(False, True)
  10.                 SideDB.ReadDwgFile(ExistingFileName, FileOpenMode.OpenForReadAndWriteNoShare, False, "")
  11.  
  12.                 Using SideDB
  13.  
  14.                     Changed = False
  15.  
  16.                     Dim layer As LayerTableRecord
  17.                     Using tr As Transaction = SideDB.TransactionManager.StartTransaction()
  18.  
  19.                         Dim lt As LayerTable = TryCast(tr.GetObject(SideDB.LayerTableId, OpenMode.ForWrite), LayerTable)
  20.  
  21.                         For Each layerId As ObjectId In lt
  22.  
  23.                             layer = TryCast(tr.GetObject(layerId, OpenMode.ForWrite), LayerTableRecord)
  24.  
  25.                             Dim FoundIndex As Integer = mLayers.FindIndex(Function(Lay As clsLayerDetails) Lay.LayerName = layer.Name)
  26.  
  27.                             If FoundIndex > -1 Then
  28.  
  29.                                 mLayers(FoundIndex).LayerFound_Res = True
  30.  
  31.                                 If mLayers(FoundIndex).eFreeze Then layer.IsFrozen = mLayers(FoundIndex).IsFrozen
  32.                                 If mLayers(FoundIndex).eOnOff Then layer.IsOff = mLayers(FoundIndex).IsOff
  33.                                 If mLayers(FoundIndex).ePlot Then layer.IsPlottable = mLayers(FoundIndex).IsPlotAble
  34.                                 If mLayers(FoundIndex).eLock Then layer.IsLocked = mLayers(FoundIndex).IsLocked
  35.  
  36.                                 If mLayers(FoundIndex).eLColor Then layer.Color = mLayers(FoundIndex).SetLColor
  37.  
  38.                                 'If mLayers(FoundIndex).eLLineType Then
  39.  
  40.                                 '    Dim ltt As LinetypeTable = TryCast(tr.GetObject(SideDB.LinetypeTableId, OpenMode.ForWrite), LinetypeTable)
  41.  
  42.                                 '    If ltt.Has(mLayers(FoundIndex).SetLLineType) = False Then
  43.  
  44.                                 '        Dim CloneOIDS As New ObjectIdCollection
  45.                                 '        CloneOIDS.Add(mLayers(FoundIndex).SetLLineType)
  46.  
  47.                                 '        Dim idMap As IdMapping = New IdMapping()
  48.                                 '        CurrentDB.WblockCloneObjects(CloneOIDS, mLayers(FoundIndex).SetLLineTypeTableID, idMap, DuplicateRecordCloning.Replace, False)
  49.  
  50.                                 '    End If
  51.  
  52.                                 '    layer.LinetypeObjectId = mLayers(FoundIndex).SetLLineType
  53.  
  54.                                 'End If
  55.  
  56.                                 Changed = True
  57.  
  58.                             End If
  59.  
  60.                         Next
  61.                         tr.Commit()
  62.                     End Using
  63.  
  64.                     If Changed Then
  65.                         tsslMessage.Text = "Saving Drawing..."
  66.                         Me.Refresh()
  67.                         SideDB.RetainOriginalThumbnailBitmap = True
  68.                         SideDB.SaveAs(SaveAsFileName, SideDB.OriginalFileVersion) ' DwgVersion.Current)
  69.                     End If
  70.  
  71.                 End Using
  72.  
  73.                 SideDB = Nothing
  74.  
  75.             End Using



And as I said before the error occurs at the following line:

But Error is actually eWrongDatabase

Code: [Select]
CurrentDB.WblockCloneObjects(CloneOIDS, mLayers(FoundIndex).SetLLineTypeTableID, idMap, DuplicateRecordCloning.Replace, False)


Just for interest sakes.

On Dialog Load
I gather the ObjectID's of the Linetypes used so that if I have I can reuse them when I copy.

Code - vb.net: [Select]
  1.         Dim Doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  2.         Dim layer As LayerTableRecord
  3.  
  4.         Dim Layers As List(Of clsLayerDetails) = New List(Of clsLayerDetails)
  5.  
  6.         Using DocKock As DocumentLock = Doc.LockDocument
  7.  
  8.             Using db As Database = Doc.Database
  9.  
  10.                 Using tr As Transaction = db.TransactionManager.StartTransaction()
  11.  
  12.                     Dim lt As LayerTable = TryCast(tr.GetObject(db.LayerTableId, OpenMode.ForRead), LayerTable)
  13.                     For Each layerId As ObjectId In lt
  14.  
  15.                         layer = TryCast(tr.GetObject(layerId, OpenMode.ForWrite), LayerTableRecord)
  16.  
  17.                         Dim T As New clsLayerDetails(layer.Name)
  18.  
  19.                         T.IsFrozen = layer.IsFrozen
  20.                         T.IsOff = layer.IsOff
  21.                         T.IsPlotAble = layer.IsPlottable
  22.                         T.IsLocked = layer.IsLocked
  23.                         T.SetLColor = layer.Color
  24.                         T.SetLLineType = layer.LinetypeObjectId
  25.                         T.SetLLineTypeTableID = db.LinetypeTableId
  26.                         T.SetLLineWeight = layer.LineWeight
  27.  
  28.                         Layers.Add(T)
  29.  
  30.                     Next
  31.  
  32.                     tr.Commit()
  33.  
  34.                 End Using
  35.  
  36.             End Using
  37.  
  38.         End Using
  39.  

edit:kdub -> formatting code=vbnet
« Last Edit: March 05, 2014, 03:16:13 AM by Kerry »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: eLockViolation - Transfering Layer/LineType Info (open Dwg to SideDB)
« Reply #5 on: March 05, 2014, 02:21:51 AM »
Stephan,
I'll try to make time to have a look tonight.

I didn't have a really good look at your code 'case vb gives me a pain.

Are you able to post compilable code ( a complete snippet )  to demonstrate.
This is generally a good idea for 2 reasons:
  • Sometimes you can find the problem when the code is reduced.
  • It makes it easier for others to try the code without making assumptions
« Last Edit: March 05, 2014, 02:25:18 AM by Kerry »
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.

GumbyCAD

  • Newt
  • Posts: 84
Re: eLockViolation - Transfering Layer/LineType Info (open Dwg to SideDB)
« Reply #6 on: March 05, 2014, 02:44:40 AM »
So I think I am going to go home and kick myself ten times......  :pissed:

Problem was I was refering to the wrong LineTypeTableID.

I was refering to the ID from the CurrentOpenDB and NewSideDB

Code: [Select]
CurrentDB.WblockCloneObjects(CloneOIDS, ltt.ObjectId, idMap, DuplicateRecordCloning.Replace, False)
I hate these ones.......
I will repost fixed code after testing a little more.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: eLockViolation - Transfering Layer/LineType Info (open Dwg to SideDB)
« Reply #7 on: March 05, 2014, 03:05:44 AM »
So I think I am going to go home and kick myself ten times......  :pissed:
< .. >

I hate these ones.......


:) Yep, me too.
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.