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

Help converting VBA to .NET DLL

6 REPLIES 6
Reply
Message 1 of 7
andym1
418 Views, 6 Replies

Help converting VBA to .NET DLL

I'm trying to convert this VBA code to a .NET dll. I could really use some help with the part that sets the active layer.

Public Sub Layersnit()
Dim objLayer As AcadLayer
Dim txtvar As String
'For Each objLayer In ThisDrawing.Layers
'objLayer.LayerOn = False
'Next objLayer
On Error Resume Next
Set objLayer = ThisDrawing.Layers("A-Area-Iden")
If objLayer Is Nothing Then
Set objLayer = ThisDrawing.Layers.Add("A-Area-Iden")
objLayer.color = acWhite
objLayer.Linetype = "Continuous"
End If
objLayer.LayerOn = True
Set objLayer = ThisDrawing.Layers("A-Area-Patt")
If objLayer Is Nothing Then
Set objLayer = ThisDrawing.Layers.Add("A-Area-Patt")
objLayer.color = acWhite
objLayer.Linetype = "Continuous"
End If
objLayer.LayerOn = True
Set objLayer = ThisDrawing.Layers("A-Area-Line")
If objLayer Is Nothing Then
Set objLayer = ThisDrawing.Layers.Add("A-Area-Line")
objLayer.color = acBlue
objLayer.Linetype = "Continuous"
End If
objLayer.LayerOn = True
' This is the part I need help with
txtvar = "CLAYER"
ThisDrawing.SetVariable txtvar, objLayer
ThisDrawing.ActiveLayer = objLayer
ThisDrawing.Application.Update
End Sub

Thanks,
Andy
6 REPLIES 6
Message 2 of 7
Anonymous
in reply to: andym1

One thing that jumps out at me is the system variable CLAYER expects a string. Use objLayer.Name to set CLAYER. Other than that, it looks ok to me at a glance.
Message 3 of 7
Anonymous
in reply to: andym1

This is what I'm using to set the active layer, it does require a layer name as an argument though:


Public Shared Sub setactlay(ByVal lay_name As String)
Dim myDWG As ApplicationServices.Document
Dim myDB As DatabaseServices.Database
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
Dim foundLayer As Boolean

myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myDB = myDWG.Database
myTransMan = myDWG.TransactionManager
myTrans = myTransMan.StartTransaction
foundLayer = False

Try
Dim myLT As DatabaseServices.LayerTable
Dim myLayer As DatabaseServices.LayerTableRecord
Dim mySTE As DatabaseServices.SymbolTableEnumerator

myLayer = myDWG.Database.LayerZero.GetObject(OpenMode.ForRead) 'initializes the layer

myLT = myDB.LayerTableId.GetObject(DatabaseServices.OpenMode.ForRead)
mySTE = myLT.GetEnumerator
While mySTE.MoveNext
myLayer = mySTE.Current.GetObject(OpenMode.ForWrite)
If myLayer.Name = lay_name Then
If myLayer.IsFrozen Then myLayer.IsFrozen = False
If myLayer.IsOff Then myLayer.IsOff = False
foundLayer = True
Exit While
End If
End While



If foundLayer Then
myDB.Clayer = myLayer.ObjectId
End If
Catch ex As Exception
Debug.Print(ex.message)

End Try

myTrans.Commit()
myTrans.Dispose()
End Sub
Message 4 of 7
andym1
in reply to: andym1

Thanks for the help! I did just get this to work:

Protected Sub SetCurrentLayer(ByVal LayerName As String)
Dim LayerId As ObjectId = Nothing
Dim Database1 As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim Database2 As Database = HostApplicationServices.WorkingDatabase
Dim Transaction1 As Transaction = Database1.TransactionManager.StartTransaction()
Dim LayerTable1 As LayerTable = Transaction1.GetObject(Database1.LayerTableId, OpenMode.ForRead, False)
LayerId = LayerTable1.Item(LayerName)
Database2.Clayer = LayerId
Transaction1.Commit()
Transaction1.Dispose()
End Sub

Thanks,
Andy
Message 5 of 7
Anonymous
in reply to: andym1

Why are you using 2 database variables?
Message 6 of 7
andym1
in reply to: andym1

I'm not sure. Are these two databases the same:

Dim Database1 As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim Database2 As Database = HostApplicationServices.WorkingDatabase

If so, I would appreciate any help to improve this bit of code.

Thanks,
Andy
Message 7 of 7
Anonymous
in reply to: andym1

I would suggest downloading and reading this newsgroup.

What you're not sure about is covered in a recent post here.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm


wrote in message news:6165706@discussion.autodesk.com...
I'm not sure. Are these two databases the same: Dim Database1 As Database =
Application.DocumentManager.MdiActiveDocument.Database Dim Database2 As
Database = HostApplicationServices.WorkingDatabase If so, I would appreciate
any help to improve this bit of code. Thanks, Andy

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