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)
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
Can't find what you're looking for? Ask the community or share your knowledge.