.NET

Reply
Valued Contributor
Posts: 75
Registered: ‎02-18-2010
Message 1 of 4 (147 Views)

locking layers in a routine with a new database not working

147 Views, 3 Replies
08-07-2012 01:33 PM

Awhile back I was trying to get some stuff to work & somewhere stumbled upon some code that created a new database object from a filename. I didn't really understand it, but it seemed to work, so fine.

 

Now I'm trying to add some features to my routine & the following code is not working:

 

    Shared Function LayerLockDB(ByVal dwgFileName As String, ByVal LayerName As String) As Boolean



        LayerLockDB = False

        Dim acLayerTable As LayerTable
        Dim acLyrTblRec As LayerTableRecord
        Dim myLayerId As ObjectId

        Dim myDb As New Database(False, True)

        myDb.ReadDwgFile(dwgFileName, FileOpenMode.OpenForReadAndAllShare, False, "")

        Using acTrans As Transaction = myDb.TransactionManager.StartTransaction

            Try     ' verify layer exists - If Layer Exist recover LayerID

                acLayerTable = CType(acTrans.GetObject(myDb.LayerTableId, OpenMode.ForRead, True, True), LayerTable)

                myLayerId = acLayerTable.Item(LayerName)

                If myLayerId.IsErased Then          ' If Deleted, Recover Layer

                    acLayerTable.UpgradeOpen()
                    acLayerTable.Item(LayerName).GetObject(OpenMode.ForWrite, True, True).Erase(False)

                End If

            Catch ex As Exception               ' Layer Doesn't Exist: Create it

                CreateLayerDB(dwgFileName, LayerName, 7)

                acLayerTable = CType(acTrans.GetObject(myDb.LayerTableId, OpenMode.ForRead, False), LayerTable)     ' Recover LayerID of newly created Layer

            End Try

            acLyrTblRec = acTrans.GetObject(acLayerTable(LayerName), OpenMode.ForWrite)

            acLyrTblRec.IsLocked = True  ' lock layer

            LayerLockDB = True

            acTrans.Commit()

        End Using

        myDb.Dispose()

    End Function

 

dwgfilename in this case is the name of the current dwg.

 

I get no erros when running/debugging this, but my layer remains unlocked.

 

I'm guessing it has to do with the new database object, as if the layer is locked in that database but is not updated in the current dwg.

 

I guess, fundementally I need to understand what this:

 

myDb.ReadDwgFile(dwgFileName, FileOpenMode.OpenForReadAndAllShare, False, "")

 is trying to do & why it seemed like I needed it in the first place (note my original program (where I thought this worked) opens many drawings & lock a particular layer in them all)

 

 

Distinguished Contributor
khoa.ho
Posts: 136
Registered: ‎09-15-2011
Message 2 of 4 (130 Views)

Re: locking layers in a routine with a new database not working

08-07-2012 08:58 PM in reply to: kcimos

I see nothing wrong with your code, it's working. However, you forgot to save changes back to database. Just add the code myDb.SaveAs(myDb.Filename, DwgVersion.Current) before myDb.Dispose() and it's good to go.

 

private static bool LayerLockDB(string dwgFileName, string layerName)
{
    bool functionReturnValue;
    var myDb = new Database(false, true);
    myDb.ReadDwgFile(dwgFileName, FileOpenMode.OpenForReadAndAllShare, false, "");
    using (Transaction acTrans = myDb.TransactionManager.StartTransaction())
    {
        // verify layer exists - If Layer Exist recover LayerID
        LayerTable acLayerTable;
        try
        {
            acLayerTable = (LayerTable)acTrans.GetObject(myDb.LayerTableId, OpenMode.ForRead, true, true);
            ObjectId myLayerId = acLayerTable[layerName];
            // If Deleted, Recover Layer
            if (myLayerId.IsErased)
            {
                acLayerTable.UpgradeOpen();
                acLayerTable[layerName].GetObject(OpenMode.ForWrite, true, true).Erase(false);
            }
            // Layer Doesn't Exist: Create it
        }
        catch (System.Exception ex)
        {
            CreateLayerDB(dwgFileName, layerName, 7);
            acLayerTable = (LayerTable)acTrans.GetObject(myDb.LayerTableId, OpenMode.ForRead, false);
            // Recover LayerID of newly created Layer
        }
        var acLyrTblRec = (LayerTableRecord)acTrans.GetObject(acLayerTable[layerName], OpenMode.ForWrite);
        acLyrTblRec.IsLocked = true;
        // lock layer
        functionReturnValue = true;
        acTrans.Commit();
    }

    // Update the current database with the new locked layer
    myDb.SaveAs(myDb.Filename, DwgVersion.Current);
    myDb.Dispose();
    return functionReturnValue;
}

-Khoa

Valued Contributor
Posts: 75
Registered: ‎02-18-2010
Message 3 of 4 (123 Views)

Re: locking layers in a routine with a new database not working

08-07-2012 09:39 PM in reply to: kcimos

Thanks. That seems like the exact sort of thing that it was missing.

Distinguished Contributor
khoa.ho
Posts: 136
Registered: ‎09-15-2011
Message 4 of 4 (99 Views)

Re: locking layers in a routine with a new database not working

08-08-2012 06:52 AM in reply to: kcimos

You are welcome. Glad to see it works.

 

-Khoa

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you interested in helping shape the Autodesk Community?
We’re looking at a few different ways to improve the “All Forums” landing page and need your feedback! If interested, please take a few minutes to fill out the following Usability Study. Thank you for your time!

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community