changing drawing

changing drawing

Anonymous
Not applicable
956 Views
9 Replies
Message 1 of 10

changing drawing

Anonymous
Not applicable
I use the handler DocumentBecameCurrent to detect a switching of drawings. When I try to use the database to check for non existing layers I got an fatal error.

I use the following code:

On load of the dll the handler is loaded:
Public Sub Initialize() Implements IExtensionApplication.Initialize
'----------------------------------------
AddHandler Application, AddressOf SwitchStart
End Sub

At drawing switch the following handler is executed. The error occurs in Layers = New LayerManager
Here the layers are checked and if the do not exist created.
Public Sub SwitchStart(ByVal Sender As Object, ByVal e As Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs)
' Make reference to drawing / Database
ed = e.Document.Editor 'Application.DocumentManager.MdiActiveDocument.Editor
db = e.Document.Database 'HostApplicationServices.WorkingDatabase
' Make a new Layermanager
Layers = New LayerManager
Layers.DetectTrackLayers()
End Sub

Has any one a clue off the error?

Greetings,

Gerard van der Sel.
0 Likes
957 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable
You should't expect to get help with a problem if you don't post the code where the problem is occuring.

--
http://www.caddzone.com

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

wrote in message news:5857105@discussion.autodesk.com...
I use the handler DocumentBecameCurrent to detect a switching of drawings. When I try to use the database to check for non existing layers I got an fatal error.

I use the following code:

On load of the dll the handler is loaded:
Public Sub Initialize() Implements IExtensionApplication.Initialize
'----------------------------------------
AddHandler Application, AddressOf SwitchStart
End Sub

At drawing switch the following handler is executed. The error occurs in Layers = New LayerManager
Here the layers are checked and if the do not exist created.
Public Sub SwitchStart(ByVal Sender As Object, ByVal e As Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs)
' Make reference to drawing / Database
ed = e.Document.Editor 'Application.DocumentManager.MdiActiveDocument.Editor
db = e.Document.Database 'HostApplicationServices.WorkingDatabase
' Make a new Layermanager
Layers = New LayerManager
Layers.DetectTrackLayers()
End Sub

Has any one a clue off the error?

Greetings,

Gerard van der Sel.
0 Likes
Message 3 of 10

Anonymous
Not applicable
You are right. Here is the code with the error:

Private Function CreateLayer(ByVal strucNewLayer As LayerStruc) As ObjectId 'identiek aan 'Private Function CheckLayer(ByVal newLayer As LayerStruc) As ObjectId
'the return value for this function
Dim layerId As ObjectId
Using tr As Transaction = db.TransactionManager.StartTransaction()
Try
'Controleren of de laag al bestaat ...
If LayerExists(strucNewLayer.Name) Then
'Eerst de layertable ophalen en openen voor lezen
Dim lt As LayerTable = DirectCast(tr.GetObject(db.LayerTableId, OpenMode.ForRead), LayerTable)
'Ophalen laag
layerId = GetLayer(strucNewLayer.Name)
Dim ltr As LayerTableRecord = DirectCast(tr.GetObject(layerId, OpenMode.ForWrite), LayerTableRecord)
'laagkleur instellen
ltr.Color = Color.FromColorIndex(ColorMethod.ByLayer, strucNewLayer.col.ColorIndex) '/strucNewLayer.col
'linetype er aan koppelen
ltr.LinetypeObjectId = GetLineTypeId(strucNewLayer.LineType)
'visible instellen
ltr.IsOff = Not (strucNewLayer.Visible)
'isPlottable instellen
ltr.IsPlottable = strucNewLayer.IsPlottable
'Locked instellen
ltr.IsLocked = strucNewLayer.IsLocked
'Frozen instellen
ltr.IsFrozen = False
Else
'Laag aanmaken
Dim ltr As LayerTableRecord = New LayerTableRecord()
'laagnaam instellen
ltr.Name = strucNewLayer.Name
'laagkleur instellen
ltr.Color = Color.FromColorIndex(ColorMethod.ByLayer, strucNewLayer.col.ColorIndex) '/strucNewLayer.col
'linetype er aan koppelen
ltr.LinetypeObjectId = GetLineTypeId(strucNewLayer.LineType)
'visible instellen
ltr.IsOff = Not (strucNewLayer.Visible)
'isPlottable instellen
ltr.IsPlottable = strucNewLayer.IsPlottable
'Locked instellen
ltr.IsLocked = strucNewLayer.IsLocked
'Frozen instellen
ltr.IsFrozen = False
'laag toevoegen
Dim lt As LayerTable = DirectCast(tr.GetObject(db.LayerTableId, OpenMode.ForWrite), LayerTable)
layerId = lt.Add(ltr)
tr.AddNewlyCreatedDBObject(ltr, True)
End If
tr.Commit()
Catch ex As Exception
tr.Dispose()
layerId = Nothing
End Try
End Using
'laagId teruggeven
Return layerId

End Function

The code itself is working, when executed from an other place. In this case this function gives the error in the statement:
Dim lt As LayerTable = DirectCast(tr.GetObject(db.LayerTableId, OpenMode.ForWrite), LayerTable)
The reported error is "locked database".
The switching of the database in the handler doesn't go well.

Greetings,

Gerard van der Sel
0 Likes
Message 4 of 10

Anonymous
Not applicable
In general, I don't think it is possible to do what you are trying to do from the DocumentBecameCurrent handler.

The reason is because the handler can fire at any time, including when there is something happening in the document that is being made current. For example, a command may be in progress, and the document could be locked and not accessable at that point.

If you would care to describe more generally, your need (e.g., why you need to create layers at that point), perhaps we can suggest another approach, but I very much doubt that the one you're pursuing is going to work.

--
http://www.caddzone.com

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

wrote in message news:5857134@discussion.autodesk.com...
You are right. Here is the code with the error:

Private Function CreateLayer(ByVal strucNewLayer As LayerStruc) As ObjectId 'identiek aan 'Private Function CheckLayer(ByVal newLayer As LayerStruc) As ObjectId
'the return value for this function
Dim layerId As ObjectId
Using tr As Transaction = db.TransactionManager.StartTransaction()
Try
'Controleren of de laag al bestaat ...
If LayerExists(strucNewLayer.Name) Then
'Eerst de layertable ophalen en openen voor lezen
Dim lt As LayerTable = DirectCast(tr.GetObject(db.LayerTableId, OpenMode.ForRead), LayerTable)
'Ophalen laag
layerId = GetLayer(strucNewLayer.Name)
Dim ltr As LayerTableRecord = DirectCast(tr.GetObject(layerId, OpenMode.ForWrite), LayerTableRecord)
'laagkleur instellen
ltr.Color = Color.FromColorIndex(ColorMethod.ByLayer, strucNewLayer.col.ColorIndex) '/strucNewLayer.col
'linetype er aan koppelen
ltr.LinetypeObjectId = GetLineTypeId(strucNewLayer.LineType)
'visible instellen
ltr.IsOff = Not (strucNewLayer.Visible)
'isPlottable instellen
ltr.IsPlottable = strucNewLayer.IsPlottable
'Locked instellen
ltr.IsLocked = strucNewLayer.IsLocked
'Frozen instellen
ltr.IsFrozen = False
Else
'Laag aanmaken
Dim ltr As LayerTableRecord = New LayerTableRecord()
'laagnaam instellen
ltr.Name = strucNewLayer.Name
'laagkleur instellen
ltr.Color = Color.FromColorIndex(ColorMethod.ByLayer, strucNewLayer.col.ColorIndex) '/strucNewLayer.col
'linetype er aan koppelen
ltr.LinetypeObjectId = GetLineTypeId(strucNewLayer.LineType)
'visible instellen
ltr.IsOff = Not (strucNewLayer.Visible)
'isPlottable instellen
ltr.IsPlottable = strucNewLayer.IsPlottable
'Locked instellen
ltr.IsLocked = strucNewLayer.IsLocked
'Frozen instellen
ltr.IsFrozen = False
'laag toevoegen
Dim lt As LayerTable = DirectCast(tr.GetObject(db.LayerTableId, OpenMode.ForWrite), LayerTable)
layerId = lt.Add(ltr)
tr.AddNewlyCreatedDBObject(ltr, True)
End If
tr.Commit()
Catch ex As Exception
tr.Dispose()
layerId = Nothing
End Try
End Using
'laagId teruggeven
Return layerId

End Function

The code itself is working, when executed from an other place. In this case this function gives the error in the statement:
Dim lt As LayerTable = DirectCast(tr.GetObject(db.LayerTableId, OpenMode.ForWrite), LayerTable)
The reported error is "locked database".
The switching of the database in the handler doesn't go well.

Greetings,

Gerard van der Sel
0 Likes
Message 5 of 10

Anonymous
Not applicable
Hi Selg,

Maybe you'll have better luck with the "documentActivated" event.

I use the documentActivated to retrieve the various styles and blocks of a drawing.

You should consider creating a class to manage the MDI.
and create You're own pre-document class.
I believe in the help sections of the ObjectARX SDK there some tips to consider.

ps. looking at you're first post: I think you're AddHandler is wrong: shouldn't it be:
AddHandler Application.DocumentManager.DocumentBecameCurrent, AddressOf SwitchStart ????

A. Caddie.
AutoCAD2008, VS2005, WinXP pro sp2.
0 Likes
Message 6 of 10

Anonymous
Not applicable
>> Maybe you'll have better luck with the "documentActivated" event.

No, actually he'll have worse luck with that.




--
http://www.caddzone.com

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

wrote in message news:5857622@discussion.autodesk.com...
Hi Selg,


I use the documentActivated to retrieve the various styles and blocks of a drawing.

You should consider creating a class to manage the MDI.
and create You're own pre-document class.
I believe in the help sections of the ObjectARX SDK there some tips to consider.

ps. looking at you're first post: I think you're AddHandler is wrong: shouldn't it be:
AddHandler Application.DocumentManager.DocumentBecameCurrent, AddressOf SwitchStart ????

A. Caddie.
AutoCAD2008, VS2005, WinXP pro sp2.
0 Likes
Message 7 of 10

Anonymous
Not applicable
Process description:

I have started a dot net program with drawing1. When I load a new drawing2 I need to check if some default layers are present. If they are not present i need to create them. The layers must be present, because the block i have to insert rely on those layers.

In the "documentbecamecurrent" event is fired at all the situations i want to catch.

Greetings,

Gerard van der Sel
0 Likes
Message 8 of 10

Anonymous
Not applicable
Depends on wath you imports:
"Imports Autodesk.AutoCAD.ApplicationServices.Application" then you don't need Application.
"Imports Autodesk.AutoCAD.ApplicationServices" you need the application first.
0 Likes
Message 9 of 10

Anonymous
Not applicable
Is your code inserting the block?

If it is, why not create the layers just before you insert the block you have to insert?

--
http://www.caddzone.com

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

wrote in message news:5858189@discussion.autodesk.com...
Process description:

I have started a dot net program with drawing1. When I load a new drawing2 I need to check if some default layers are present. If they are not present i need to create them. The layers must be present, because the block i have to insert rely on those layers.

In the "documentbecamecurrent" event is fired at all the situations i want to catch.

Greetings,

Gerard van der Sel
0 Likes
Message 10 of 10

Anonymous
Not applicable
The code is inserting the blocks. So making the layers while inserting the blocks is an option. But in our firm all layers are made. At the end is looked if a layer is empty and then removed.

I reconsidered what I want. And I noticed I have made an error. If I switch to a drawing it doesn't have to be this sort of drawing. It could be an other type, in which case the wrong layers are inserted. So only switching of database and editor is enough. This is working, so now only for my own understanding of AutoCAD it is necessary to know what went wrong.

Greetings,

Gerard.
0 Likes