eNotOpenForWrite error adding a layer

eNotOpenForWrite error adding a layer

Anonymous
Not applicable
925 Views
3 Replies
Message 1 of 4

eNotOpenForWrite error adding a layer

Anonymous
Not applicable
After opening btr for write (~line 23), btr.AppendEntity(rect) (~line 37) throws an error in AutoCAD: eNotOpenForWrite. Why would this happen and how can I fix it?


Code:
Private Sub Insert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Insert.Click
'should be similar to cmdOK_Click()
myDWG = Application.DocumentManager.MdiActiveDocument
myDB = myDWG.Database
myTransMan = myDWG.TransactionManager
myTrans = myTransMan.StartTransaction
Dim point As Point3d, point1 As Point3d, point2 As Point3d, point3 As Point3d, point4 As Point3d, verts As New Point3dCollection, rect As Polyline2d
Me.Hide()
point = myDWG.Editor.GetPoint("INSERTION POINT IS THE LOWER LEFT CORNER OF THE FRAME").Value
If FramingOpts.SelectedIndex > -1 Then
If FramingOpts.SelectedItem.ToString <> "no frame" Then
Select Case FramingOpts.SelectedItem.ToString
Case "4x8"
point1 = New Point3d(point.X - 0.25, point.Y - 0.25, 0)
point2 = New Point3d(point1.X + 96, point1.Y, 0)
point3 = New Point3d(point1.X + 96, point1.Y + 48, 0)
point4 = New Point3d(point1.X, point1.Y + 48, 0)
Case "4x10"
point1 = New Point3d(point.X - 0.25, point.Y - 0.25, 0)
point2 = New Point3d(point1.X + 120, point1.Y, 0)
point3 = New Point3d(point1.X + 120, point1.Y + 48, 0)
point4 = New Point3d(point1.X, point1.Y + 48, 0)
Case "4x12"
point1 = New Point3d(point.X - 0.25, point.Y - 0.25, 0)
point2 = New Point3d(point1.X + 144, point1.Y, 0)
point3 = New Point3d(point1.X + 144, point1.Y + 48, 0)
point4 = New Point3d(point1.X, point1.Y + 48, 0)
Case "5x5"
point1 = New Point3d(point.X - 0.25, point.Y - 0.25, 0)
point2 = New Point3d(point1.X + 60, point1.Y, 0)
point3 = New Point3d(point1.X + 60, point1.Y + 60, 0)
point4 = New Point3d(point1.X, point1.Y + 60, 0)
Case "5x8"
point1 = New Point3d(point.X - 0.25, point.Y - 0.25, 0)
point2 = New Point3d(point1.X + 96, point1.Y, 0)
point3 = New Point3d(point1.X + 96, point1.Y + 60, 0)
point4 = New Point3d(point1.X, point1.Y + 60, 0)
Case "5x10"
point1 = New Point3d(point.X - 0.25, point.Y - 0.25, 0)
point2 = New Point3d(point1.X + 120, point1.Y, 0)
point3 = New Point3d(point1.X + 120, point1.Y + 60, 0)
point4 = New Point3d(point1.X, point1.Y + 60, 0)
End Select

verts.Add(point1)
verts.Add(point2)
verts.Add(point3)
verts.Add(point4)

rect = New Polyline2d(Poly2dType.SimplePoly, verts, 0, False, 0, 0, Nothing)
rect.Closed = True

Dim check As Boolean
check = False

Dim myLT As LayerTable
Dim myLTR As LayerTableRecord
Dim mySTE As SymbolTableEnumerator

myLT = myDB.LayerTableId.GetObject(OpenMode.ForWrite)
mySTE = myLT.GetEnumerator

While mySTE.MoveNext
myLTR = mySTE.Current.GetObject(OpenMode.ForRead)
If UCase(myLTR.Name) = "STOCK" Then
check = True
Exit While
End If
End While

If check = True Then
rect.Layer = "STOCK"
Else
Dim bt As BlockTable = myTrans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead)
'myBT = myDWG.Database.BlockTableId.GetObject(OpenMode.ForWrite)
Dim btrID As ObjectId = bt.Item(BlockTableRecord.ModelSpace)
Dim btr As BlockTableRecord = myTrans.GetObject(btrID, OpenMode.ForWrite)
'myBTR = myBT(DatabaseServices.BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)
Dim newlayer As New LayerTableRecord
newlayer.Name = "STOCK"
newlayer.Color = Colors.Color.FromColor(Color.Black)
newlayer.LineWeight = LineWeight.LineWeight005
newlayer.IsOff = True
newlayer.IsFrozen = False
newlayer.IsLocked = True
newlayer.IsPlottable = False
myLT.Add(newlayer)
myTrans.AddNewlyCreatedDBObject(newlayer, True)
myTrans.Commit()
rect.Layer = newlayer.Name
btr.AppendEntity(rect)
myTrans.AddNewlyCreatedDBObject(rect, True)
myTrans.Commit()

myTrans.Dispose()
myTransMan.Dispose()

End If
If ListSelected.Items.Count > 0 Then
Dim point5 As Point3d
If FramingOpts.SelectedItem.ToString = "4x8" Or FramingOpts.SelectedItem.ToString = "4x10" Or FramingOpts.SelectedItem.ToString = "4x12" Then
point5 = New Point3d(point1.X - 92, point1.Y + 48, 0)
Else
point5 = New Point3d(point1.X - 92, point1.Y + 60, 0)
End If
GenerateLayers(point5)
End If
Else
Dim markerblock As BlockReference

myBT = myDWG.Database.BlockTableId.GetObject(OpenMode.ForWrite)
myBTR = myBT(DatabaseServices.BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)
Try
If RoutingButton.Checked Then
myBTR.PathName = "s:\CNC MARKERS\KOMO INSERTION MARKER.dwg"
markerblock = New BlockReference(point, myBTR.Id)
point1 = New Point3d(point.X - 92, point.Y + 48, 0)
Else
myBTR.PathName = "s:\CNC MARKERS\FADAL INSERTION MARKER.dwg"
markerblock = New BlockReference(point, myBTR.Id)
point1 = New Point3d(point.X - 90.5, point.Y, 0)
End If
myBTR.AppendEntity(markerblock)
myTrans.AddNewlyCreatedDBObject(markerblock, True)
myTrans.Commit()
Catch ex As Exception
MessageBox.Show("Cannot create/ insert markerblock: " + ex.ToString)
End Try

myTrans.Dispose()
myTransMan.Dispose()

GenerateLayers(point)
End If
Else
Dim markerblock As BlockReference

myBT = myDWG.Database.BlockTableId.GetObject(OpenMode.ForWrite)
myBTR = myBT(DatabaseServices.BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)

Dim BTR = New BlockTableRecord()
Try
If RoutingButton.Checked Then
BTR.PathName = "s:\CNC MARKERS\KOMO INSERTION MARKER.dwg"
markerblock = New BlockReference(point, BTR.Id)
point1 = New Point3d(point.X - 92, point.Y + 48, 0)
Else
BTR.PathName = "s:\CNC MARKERS\FADAL INSERTION MARKER.dwg"
markerblock = New BlockReference(point, BTR.Id)
point1 = New Point3d(point.X - 90.5, point.Y, 0)
End If
myBTR.AppendEntity(markerblock)
myTrans.AddNewlyCreatedDBObject(markerblock, True)
myTrans.Commit()
Catch ex As Exception
MessageBox.Show("Cannot create/ insert markerblock: " + ex.ToString)
End Try

myTrans.Dispose()
myTransMan.Dispose()

GenerateLayers(point)
End If
End Sub Edited by: vampirefromtheITcave2 on Oct 23, 2009 3:25 PM
0 Likes
926 Views
3 Replies
Replies (3)
Message 2 of 4

chiefbraincloud
Collaborator
Collaborator
{code}
Dim btr As BlockTableRecord = myTrans.GetObject(btrID, OpenMode.ForWrite)
'myBTR = myBT(DatabaseServices.BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)
Dim newlayer As New LayerTableRecord
newlayer.Name = "STOCK"
newlayer.Color = Colors.Color.FromColor(Color.Black)
newlayer.LineWeight = LineWeight.LineWeight005
newlayer.IsOff = True
newlayer.IsFrozen = False
newlayer.IsLocked = True
newlayer.IsPlottable = False
myLT.Add(newlayer)
myTrans.AddNewlyCreatedDBObject(newlayer, True)

myTrans.Commit()

rect.Layer = newlayer.Name
btr.AppendEntity(rect)
myTrans.AddNewlyCreatedDBObject(rect, True)
myTrans.Commit()
{code}

You can not continue to use a transaction after committing it. The only acceptable action after committing a transaction is disposing it. As soon as you commit the transaction, it closes the references it had associated with it.
If my brain is screwed on right this morning, Adding the new layer to the layer table and the transaction should be enough to be able to assign it to the block, so you could just remove the commit in between. If that errors saying the layer doesn't exist then you will need two seperate transactions.
Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 3 of 4

arcticad
Advisor
Advisor
You can also speed up your code by not Enumerating the layer list.

{code}
Dim LayerName As String
Dim rtnValue As Boolean
Dim LT As LayerTable
LT = db.LayerTableId.GetObject(OpenMode.ForRead)
Try
Dim objID = LT(LayerName)
rtnValue = True
Catch ex As Exception
rtnValue = False
End Try
{code}
---------------------------



(defun botsbuildbots() (botsbuildbots))
0 Likes
Message 4 of 4

kpblc2000
Advisor
Advisor
If you try to create layer by this code and then purge it, your code won't works correctly. You should use something like this
{code}using System;
using System.Collections.Generic;
using System.Windows.Forms;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.Runtime;

using CaddZone.AutoCAD.DatabaseServices;


namespace kpblcLayer
{
public class kpblcAddLayer
{
[CommandMethod("cmd")]
public static void CmdAddLayer()
{
Database db = HostApplicationServices.WorkingDatabase;
ObjectId res;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
string LayerName = "MyLayer";
LayerTable tblLayers = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
if (tblLayers.Has(LayerName))
{
res = tblLayers[LayerName];
}
else
{
LayerTableRecord tblRec = new LayerTableRecord();
tblRec.Name = LayerName;
tblLayers.Add(tblRec);
tr.AddNewlyCreatedDBObject(tblRec, true);
res = tblLayers[LayerName];
}
if (res.IsErased)
{
res = CaddZone.AutoCAD.DatabaseServices.DBUtils.GetSymbolTableRecordId(tblLayers, LayerName);
}
if ((res != null) && (!res.IsErased))
{
MessageBox.Show("Layer created");
}
tr.Commit();
}
}
}
}{/code}
This code is referenced to http://www.caddzone.com/DBUtils.cs

Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям! | Do you find the posts helpful? "LIKE" these posts!
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.


Алексей Кулик aka kpblc | Aleksei Kulik aka kpblc Facebook | LinkedIn
autolisp.ru
Техническая поддержка программистов Autodesk в СНГ
Библиотека пользовательских lisp-функций | Custom Lisp-function library

0 Likes