
CAD discussion forum - ask any CAD-related questions here, share your CAD knowledge on AutoCAD, Inventor, Revit and other Autodesk software with your peers from all over the world. To start a new topic, choose an appropriate forum.
Please abide by the
rules of this forum.
This is a peer-to-peer forum. The forum doesn't replace the official direct technical support provided by ARKANCE for its customers.
How to post questions: register or login, go to the specific forum and click the NEW TOPIC button.
| Author |
Topic Search Topic Options
|
GaryM
Newbie
Joined: 20.Aug.2007
Location: South Africa
Status: Offline
Points: 16
|
Topic: VBA Coding Posted: 20.Aug.2007 at 10:23 |
Hi Looking 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.
Cheers 
Edited by GaryM - 22.Aug.2007 at 15:58
|
|
Regards,
Gary Mansfield
|
 |
Vladimir Michl
Senior Member
Joined: 26.Jul.2007
Location: Czech Republic
Using: Autodesk software
Status: Offline
Points: 2196
|
Posted: 24.Aug.2007 at 22:21 |
|
You can look at the ThisDrawing.Layers collection and iterate through it.
|
|
|
 |
GaryM
Newbie
Joined: 20.Aug.2007
Location: South Africa
Status: Offline
Points: 16
|
Posted: 28.Aug.2007 at 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 box For Each objLayer In ThisDrawing.Layers frmDelLayer.cboLayerName.AddItem (objLayer.Name) Next End Sub
|
|
Regards,
Gary Mansfield
|
 |
Vladimir Michl
Senior Member
Joined: 26.Jul.2007
Location: Czech Republic
Using: Autodesk software
Status: Offline
Points: 2196
|
Posted: 28.Aug.2007 at 12:07 |
|
Yes, this is the ThisDrawing.Layers iteration :-)
|
|
|
 |
GaryM
Newbie
Joined: 20.Aug.2007
Location: South Africa
Status: Offline
Points: 16
|
Posted: 28.Aug.2007 at 13:21 |
|
HI
Now 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 Sub
Giving me a error: "Object is referenced by other object(s)"
Any ideas on what i'm doing wrong.
|
|
Regards,
Gary Mansfield
|
 |
Vladimir Michl
Senior Member
Joined: 26.Jul.2007
Location: Czech Republic
Using: Autodesk software
Status: Offline
Points: 2196
|
Posted: 28.Aug.2007 at 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
Newbie
Joined: 20.Aug.2007
Location: South Africa
Status: Offline
Points: 16
|
Posted: 28.Aug.2007 at 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?
|
|
Regards,
Gary Mansfield
|
 |
Vladimir Michl
Senior Member
Joined: 26.Jul.2007
Location: Czech Republic
Using: Autodesk software
Status: Offline
Points: 2196
|
Posted: 28.Aug.2007 at 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
Newbie
Joined: 20.Aug.2007
Location: South Africa
Status: Offline
Points: 16
|
Posted: 28.Aug.2007 at 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.
|
|
Regards,
Gary Mansfield
|
 |
Vladimir Michl
Senior Member
Joined: 26.Jul.2007
Location: Czech Republic
Using: Autodesk software
Status: Offline
Points: 2196
|
Posted: 28.Aug.2007 at 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
|
|
|
 |