Display full version of the post: VBA Coding

GaryM
20.08.2007, 10:23
HiLooking for help on adding current layer names in you active drawings to a  combo box?Let me explain what i would like to do.
 
I designed a userform wich includes your basic layer operations turning ON & OFF, FREEZING, THAW etc, but know i would like to add a another function where you can delete a layer, but when you click the delete button it opens another userform where you can either pick a layer using a command button or select a layer in a combo box.
 
Know the question how would i code the form or combo box to go search for current layers in active drawing and list it in the combo box index.
 
I'm kinda rusty on my vba coding so any kind of help would be appreciated.
 
CheersGaryM2007-08-22 15:58:27

Vladimir Michl
24.08.2007, 22:21
You can look at the ThisDrawing.Layers collection and iterate through it.

GaryM
28.08.2007, 10:33
Thanks for the suggestion, but came right doing it another way... check it out! Private Sub UserForm_Activate()  Dim objLayers As AcadLayers  Dim objLayer As AcadLayer  'search for layers in current drawing and add to combo boxFor Each objLayer In ThisDrawing.Layers  frmDelLayer.cboLayerName.AddItem (objLayer.Name)  NextEnd Sub

Vladimir Michl
28.08.2007, 12:07
Yes, this is the ThisDrawing.Layers iteration :-)

GaryM
28.08.2007, 13:21
HINow i'm struggling to delete a layer. How would i go about this...Here is the code i'm using. What i'm actually trying to do is that i want to select from the combo box a layer to delete.Here is the code i'm using;Private Sub cmdOk_Click()On Error Resume Next  Dim strLayerName As String  Dim objLayer As AcadLayer    strLayerName = frmDelLayer.cboLayerName.Text    Set objLayer = ThisDrawing.Layers(strLayerName)    If objLayer Is Nothing Then      MsgBox "Layer " & strLayerName & " not found"      Exit Sub    End If    objLayer.Delete    If Err Then      MsgBox "Unable to delete layer: " & vbCr & Err.Description    Else      MsgBox "Layer '" & strLayerName & "' deleted"    End If   End SubGiving me a error: "Object is referenced by other object(s)"Any ideas on what i'm doing wrong.

Vladimir Michl
28.08.2007, 13:27
The layer your are trying to delete is probably not empty (contains entities, is referenced from a block, etc.). You can only delete empty layers in this way.

GaryM
28.08.2007, 13:39
cool you are quite right.Autocad gives you the LAYDEL command where you can delete layers with all entities etc. Is there anything similar i could do with vba?

Vladimir Michl
28.08.2007, 13:47
You can either write the code to remove all possible references to this layer or you can run LAYDEL from VBA (through SendCommand).

GaryM
28.08.2007, 14:16
How would i write the code to remove all possible references.And the Sendcommand "LAYDEL" just activates the command does not actually delete the layer,  you still l need to do the normal steps after the laydel command is entered. I want to delete from my delete layer form after selecting the layer from the combo box and clicking ok.

Vladimir Michl
28.08.2007, 14:28
The code would be quite complex - it would need to go through nested blocks, xrefs, etc.
 
Try to use LAYDEL - with all following options on the commandline - e.g.:
-LAYDEL Name mylayer

GaryM
31.08.2007, 11:03
Thanks Vladimir working cool.