.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Convert Layer Problem

6 REPLIES 6
Reply
Message 1 of 7
gilseorin
181 Views, 6 Replies

Convert Layer Problem

Hi,all.
My problem is to convert selected layer to current layer.
Some help?
Thanks in advance.
6 REPLIES 6
Message 2 of 7
MarkPendergraft
in reply to: gilseorin

all you need to do is get the object id of the selected layer, and then set it as the current layer....

Public Sub SetLayer()

Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim db As Database = HostApplicationServices.WorkingDatabase

CreateLayerSelectForm()

Using trans As Transaction = db.TransactionManager.StartTransaction
Dim lt As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead, False, True)
Dim ltr As LayerTableRecord = Nothing

If lt.Has(returnlayname) Then
ltr = trans.GetObject(lt.Item(returnlayname), OpenMode.ForWrite, False, True)
If Not db.Clayer = ltr.ObjectId Then
ltr.IsFrozen = False
ltr.IsLocked = False
ltr.IsOff = False
db.Clayer = ltr.ObjectId
End If
ElseIf ReturnLayName "" Then
ed.WriteMessage(vbCrLf + "The layer '" + ReturnLayName + "' does not exist.")
Exit Sub
End If
trans.Commit()
ReturnLayName = ""
End Using

End Sub

this example is a program i have written. the line :
CreateLayerSelectForm()
calls a form into being, with all the layers in the drawing loaded into it, then it allows you to select one, and returns the string of the layer name as the public shared value ReturnLayName. if the layer exists (lt.has(returnlayname) ) then it makes sure it isn't off, frozen, or locked, and then sets it.

Hope this helps.
Message 3 of 7
gilseorin
in reply to: gilseorin

Thanks for the reply, but it's not for me.
It has no contents of converting to current layer.
What I need is the next code of the following code.
Plz, give me a hand.

Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim tr As Transaction = db.TransactionManager.StartTransaction()
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Try
Dim entselopt As PromptEntityOptions = New PromptEntityOptions("" & Microsoft.VisualBasic.Chr(10) & "Select Entity: ")
entselopt.SetRejectMessage("" & Microsoft.VisualBasic.Chr(10) & "Entity NOt Selected!")
entselopt.AddAllowedClass(GetType(DBText), True)

Dim entselres As PromptEntityResult = ed.GetEntity(entselopt)

Dim Ent As Entity = CType(tr.GetObject(entselres.ObjectId, OpenMode.ForRead), Entity)
Dim returnlayname As String = Ent.Layer
tr.Commit()
Finally
tr.Dispose()
End Try
Message 4 of 7
MarkPendergraft
in reply to: gilseorin

Public Sub testing()

Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor

Try
Dim entselopt As PromptEntityOptions = New PromptEntityOptions(vbCrLf + "Select Entity: ")
entselopt.AllowNone = False
entselopt.AllowObjectOnLockedLayer = True

Dim entselres As PromptEntityResult = ed.GetEntity(entselopt)
If entselres.Status <> PromptStatus.OK Then Exit Sub
Using trans As Transaction = db.TransactionManager.StartTransaction

Dim Ent As Entity = trans.GetObject(entselres.ObjectId, OpenMode.ForRead, False, True)
Dim lt As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead, False, True)
Dim ltr As LayerTableRecord = trans.GetObject(lt.Item(Ent.Layer), OpenMode.ForWrite, False, True)

If Not db.Clayer = ltr.ObjectId Then
ltr.IsFrozen = False
ltr.IsLocked = False
ltr.IsOff = False
db.Clayer = ltr.ObjectId
End If
trans.Commit()
End Using
Catch ex As Exception
ed.WriteMessage("Error: " + ex.Message)
End Try
End Sub
Message 5 of 7
gilseorin
in reply to: gilseorin

It seemes to be my fault.
Pardon my short expression.
What I need is to convert selected layer to current layer.

If existing layers are "0" and "layer1" and "layer2" and also
if selected layer is "layer1" and also
If current layer is "0" then the result should be as follows.

selected layer("layer1") ----> layer "0"

Finally, existing layers are "0" and "layer2".
Message 6 of 7
MarkPendergraft
in reply to: gilseorin

Public Sub ChangeLayerToCurrent()

Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor


Try
Dim entselopt As PromptEntityOptions = New PromptEntityOptions(vbCrLf + "Select Entity: ")
entselopt.AllowNone = False
entselopt.AllowObjectOnLockedLayer = True

Dim entselres As PromptEntityResult = ed.GetEntity(entselopt)
If entselres.Status <> PromptStatus.OK Then Exit Sub

Using trans As Transaction = db.TransactionManager.StartTransaction

Dim ent As Entity = trans.GetObject(entselres.ObjectId, OpenMode.ForRead, False, True)
Dim entlayer As String = ent.Layer : ent = Nothing
Dim lt As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead, False, True)
Dim ltr As LayerTableRecord = trans.GetObject(db.Clayer, OpenMode.ForRead, False, True)

Dim Values() As TypedValue = {New TypedValue(DxfCode.LayerName, entlayer)}
Dim prFilter As SelectionFilter = New SelectionFilter(Values)

Dim prSelRes As PromptSelectionResult = ed.SelectAll(prFilter)

If prSelRes.Status <> PromptStatus.OK Then Exit Sub

Dim selset As SelectionSet = prSelRes.Value
Dim ids() As ObjectId = selset.GetObjectIds
Dim id As ObjectId

For Each id In ids
ent = trans.GetObject(id, OpenMode.ForWrite, False, True)
ent.Layer = ltr.Name
Next

ltr = trans.GetObject(lt.Item(entlayer), OpenMode.ForWrite, False, True)
ltr.Erase()

trans.Commit()
End Using
Catch ex As Exception
ed.WriteMessage("Error: " + ex.Message)
End Try
End Sub
Message 7 of 7
gilseorin
in reply to: gilseorin

Thank you so---ooooooooooooo much.
I deeply appreciate for your help.
Thank you again. Happy Day!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost