VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

selecting all xrefs in modelspace

7 REPLIES 7
Reply
Message 1 of 8
Anonymous
156 Views, 7 Replies

selecting all xrefs in modelspace

Hello all,

How can I select all xrefs attached in a modelspace drawing and change their
layer into "XREFS" for example ???

Here is the code I created but it does not work in all my drawings. Don't
understand why...

1° When I create a new drawing and attach XREFS inside it (in modelspace) it
works fine.
2° But when I try it in existing files it doesn't work any more..... 😞


I would really appreciate if someone more experienced than I am could help
me.

Thanks


Gilles



Option Explicit

Public Sub Add_Layer_Vport()


Dim Layer_Name As String
Dim layerXREFS As AcadLayer

Layer_Name = "XREFS"

Set layerXREFS = ThisDrawing.Layers.Add(Layer_Name)

layerXREFS.Color = acWhite
layerXREFS.LayerOn = True
layerXREFS.Linetype = "continuous"
layerXREFS.Lock = False
layerXREFS.PlotStyleName = "Normal"
layerXREFS.Lineweight = acLnWtByLwDefault
layerXREFS.Plottable = True


Dim acXREFS As AcadExternalReference

For Each acXREFS In ThisDrawing.ModelSpace

acXREFS.Layer = "XREFS"

Next acXREFS


End Sub
7 REPLIES 7
Message 2 of 8
Anonymous
in reply to: Anonymous

What if ModelSpace has objects *other* than XRefs?! 😉

Public Sub Test()

Dim Layer_Name As String
Layer_Name = "XREFS"

Dim layerXREFS As AcadLayer
Set layerXREFS = ThisDrawing.Layers.Add(Layer_Name)

layerXREFS.color = acWhite
layerXREFS.LayerOn = True
layerXREFS.Linetype = "continuous"
layerXREFS.Lock = False
layerXREFS.PlotStyleName = "Normal"
layerXREFS.Lineweight = acLnWtByLwDefault
layerXREFS.Plottable = True

Dim thisObj As AcadEntity

For Each thisObj In ThisDrawing.ModelSpace
If TypeOf thisObj Is AcadExternalReference Then thisObj.Layer =
Layer_Name
Next thisObj

End Sub

BTW, you used a variable for the layer name in one spot, and the actual
string in another (can you spell p-o-t-e-n-t-i-a-l b-u-g? )


--
R. Robert Bell, MCSE
www.AcadX.com


"Gilles" wrote in message
news:074A51AC13DB08BC531859BBAA5F631E@in.WebX.maYIadrTaRb...
| Hello all,
|
| How can I select all xrefs attached in a modelspace drawing and change
their
| layer into "XREFS" for example ???
|
| Here is the code I created but it does not work in all my drawings. Don't
| understand why...
|
| 1° When I create a new drawing and attach XREFS inside it (in modelspace)
it
| works fine.
| 2° But when I try it in existing files it doesn't work any more..... 😞
|
|
| I would really appreciate if someone more experienced than I am could help
| me.
|
| Thanks
|
|
| Gilles
|
|
|
| Option Explicit
|
| Public Sub Add_Layer_Vport()
|
|
| Dim Layer_Name As String
| Dim layerXREFS As AcadLayer
|
| Layer_Name = "XREFS"
|
| Set layerXREFS = ThisDrawing.Layers.Add(Layer_Name)
|
| layerXREFS.Color = acWhite
| layerXREFS.LayerOn = True
| layerXREFS.Linetype = "continuous"
| layerXREFS.Lock = False
| layerXREFS.PlotStyleName = "Normal"
| layerXREFS.Lineweight = acLnWtByLwDefault
| layerXREFS.Plottable = True
|
|
| Dim acXREFS As AcadExternalReference
|
| For Each acXREFS In ThisDrawing.ModelSpace
|
| acXREFS.Layer = "XREFS"
|
| Next acXREFS
|
|
| End Sub
|
|
Message 3 of 8
Anonymous
in reply to: Anonymous


Dim Entity As AcadEntity
For Each Entity In ThisDrawing.ModelSpace
If TypeOf Entity is AcadExternalReference then
Entity.Layer = Layer_Name
End If
Next Entity
Message 4 of 8
Anonymous
in reply to: Anonymous

Thank you for your answer which was much more efficient than my first code.
I tried it through a few drawings and everything is OK... As a new user I
am everyday surprised by the potential of VBA but I need to learn some (
what dou I say... a lot of) things to succeed in my objectives.
By the way, what do you mean exactly by "BTW, you used a variable for the
layer name in one spot, and the actual string in another (can you spell
p-o-t-e-n-t-i-a-l b-u-g? )". What I understand is that I should use the
same variable to declare the layer name and layer object ??? ... ???


Thanks again....

Gilles

----- Original Message -----
From: "R. Robert Bell"
Newsgroups: autodesk.autocad.customization.vba
Sent: Wednesday, May 28, 2003 7:12 PM
Subject: Re: selecting all xrefs in modelspace


> What if ModelSpace has objects *other* than XRefs?! 😉
>
> Public Sub Test()
>
> Dim Layer_Name As String
> Layer_Name = "XREFS"
>
> Dim layerXREFS As AcadLayer
> Set layerXREFS = ThisDrawing.Layers.Add(Layer_Name)
>
> layerXREFS.color = acWhite
> layerXREFS.LayerOn = True
> layerXREFS.Linetype = "continuous"
> layerXREFS.Lock = False
> layerXREFS.PlotStyleName = "Normal"
> layerXREFS.Lineweight = acLnWtByLwDefault
> layerXREFS.Plottable = True
>
> Dim thisObj As AcadEntity
>
> For Each thisObj In ThisDrawing.ModelSpace
> If TypeOf thisObj Is AcadExternalReference Then thisObj.Layer =
> Layer_Name
> Next thisObj
>
> End Sub
>
> BTW, you used a variable for the layer name in one spot, and the actual
> string in another (can you spell p-o-t-e-n-t-i-a-l b-u-g? )
>
>
> --
> R. Robert Bell, MCSE
> www.AcadX.com
>
>
> "Gilles" wrote in message
> news:074A51AC13DB08BC531859BBAA5F631E@in.WebX.maYIadrTaRb...
> | Hello all,
> |
> | How can I select all xrefs attached in a modelspace drawing and change
> their
> | layer into "XREFS" for example ???
> |
> | Here is the code I created but it does not work in all my drawings.
Don't
> | understand why...
> |
> | 1° When I create a new drawing and attach XREFS inside it (in
modelspace)
> it
> | works fine.
> | 2° But when I try it in existing files it doesn't work any more..... 😞
> |
> |
> | I would really appreciate if someone more experienced than I am could
help
> | me.
> |
> | Thanks
> |
> |
> | Gilles
> |
> |
> |
> | Option Explicit
> |
> | Public Sub Add_Layer_Vport()
> |
> |
> | Dim Layer_Name As String
> | Dim layerXREFS As AcadLayer
> |
> | Layer_Name = "XREFS"
> |
> | Set layerXREFS = ThisDrawing.Layers.Add(Layer_Name)
> |
> | layerXREFS.Color = acWhite
> | layerXREFS.LayerOn = True
> | layerXREFS.Linetype = "continuous"
> | layerXREFS.Lock = False
> | layerXREFS.PlotStyleName = "Normal"
> | layerXREFS.Lineweight = acLnWtByLwDefault
> | layerXREFS.Plottable = True
> |
> |
> | Dim acXREFS As AcadExternalReference
> |
> | For Each acXREFS In ThisDrawing.ModelSpace
> |
> | acXREFS.Layer = "XREFS"
> |
> | Next acXREFS
> |
> |
> | End Sub
> |
> |
>
>
Message 5 of 8
Anonymous
in reply to: Anonymous

Thank you too for your answer.

It was the confirmation I was using a wrong way
before... Now, I am on the right one...

 

 

Thanks again

 

Gilles


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
 
Dim Entity As AcadEntity
For Each Entity In ThisDrawing.ModelSpace
  If TypeOf Entity is AcadExternalReference then
    Entity.Layer = Layer_Name
  End If
Next Entity
Message 6 of 8
Anonymous
in reply to: Anonymous

Ok, a bit more detail...

Dim LayerName as String
LayerName = "MyCoolLayer"
ThisDrawing.Layers.Add(LayerName)

This is good. Permits you to change the layer name sometime in the future at
one point.

acXREFS.Layer = "XREFS"

This is bad. In the same procedure you have used a "hard-coded" string. So
if you change the string for the variable, and *forget* to look for other
instances of that string in the procedure, you have created a bug.

It would be far better to do this, in the 2nd case:

acXREFS.Layer = LayerName

This way, you only need to change the string in one place, and be confident
that the rest of the code will still work.


--
R. Robert Bell, MCSE
www.AcadX.com


"Gilles" wrote in message
news:C91FC2F023959C91326BDC5063545FE9@in.WebX.maYIadrTaRb...
| Thank you for your answer which was much more efficient than my first
code.
| I tried it through a few drawings and everything is OK... As a new user I
| am everyday surprised by the potential of VBA but I need to learn some (
| what dou I say... a lot of) things to succeed in my objectives.
| By the way, what do you mean exactly by "BTW, you used a variable for the
| layer name in one spot, and the actual string in another (can you spell
| p-o-t-e-n-t-i-a-l b-u-g? )". What I understand is that I should use the
| same variable to declare the layer name and layer object ??? ... ???
|
|
| Thanks again....
|
| Gilles
|
| ----- Original Message -----
| From: "R. Robert Bell"
| Newsgroups: autodesk.autocad.customization.vba
| Sent: Wednesday, May 28, 2003 7:12 PM
| Subject: Re: selecting all xrefs in modelspace
|
|
| > What if ModelSpace has objects *other* than XRefs?! 😉
| >
| > Public Sub Test()
| >
| > Dim Layer_Name As String
| > Layer_Name = "XREFS"
| >
| > Dim layerXREFS As AcadLayer
| > Set layerXREFS = ThisDrawing.Layers.Add(Layer_Name)
| >
| > layerXREFS.color = acWhite
| > layerXREFS.LayerOn = True
| > layerXREFS.Linetype = "continuous"
| > layerXREFS.Lock = False
| > layerXREFS.PlotStyleName = "Normal"
| > layerXREFS.Lineweight = acLnWtByLwDefault
| > layerXREFS.Plottable = True
| >
| > Dim thisObj As AcadEntity
| >
| > For Each thisObj In ThisDrawing.ModelSpace
| > If TypeOf thisObj Is AcadExternalReference Then thisObj.Layer =
| > Layer_Name
| > Next thisObj
| >
| > End Sub
| >
| > BTW, you used a variable for the layer name in one spot, and the actual
| > string in another (can you spell p-o-t-e-n-t-i-a-l b-u-g? )
| >
| >
| > --
| > R. Robert Bell, MCSE
| > www.AcadX.com
| >
| >
| > "Gilles" wrote in message
| > news:074A51AC13DB08BC531859BBAA5F631E@in.WebX.maYIadrTaRb...
| > | Hello all,
| > |
| > | How can I select all xrefs attached in a modelspace drawing and change
| > their
| > | layer into "XREFS" for example ???
| > |
| > | Here is the code I created but it does not work in all my drawings.
| Don't
| > | understand why...
| > |
| > | 1° When I create a new drawing and attach XREFS inside it (in
| modelspace)
| > it
| > | works fine.
| > | 2° But when I try it in existing files it doesn't work any more.....
:-(
| > |
| > |
| > | I would really appreciate if someone more experienced than I am could
| help
| > | me.
| > |
| > | Thanks
| > |
| > |
| > | Gilles
| > |
| > |
| > |
| > | Option Explicit
| > |
| > | Public Sub Add_Layer_Vport()
| > |
| > |
| > | Dim Layer_Name As String
| > | Dim layerXREFS As AcadLayer
| > |
| > | Layer_Name = "XREFS"
| > |
| > | Set layerXREFS = ThisDrawing.Layers.Add(Layer_Name)
| > |
| > | layerXREFS.Color = acWhite
| > | layerXREFS.LayerOn = True
| > | layerXREFS.Linetype = "continuous"
| > | layerXREFS.Lock = False
| > | layerXREFS.PlotStyleName = "Normal"
| > | layerXREFS.Lineweight = acLnWtByLwDefault
| > | layerXREFS.Plottable = True
| > |
| > |
| > | Dim acXREFS As AcadExternalReference
| > |
| > | For Each acXREFS In ThisDrawing.ModelSpace
| > |
| > | acXREFS.Layer = "XREFS"
| > |
| > | Next acXREFS
| > |
| > |
| > | End Sub
| > |
| > |
| >
| >
|
|
Message 7 of 8
Anonymous
in reply to: Anonymous

OK I see what you mean. Always use a variable and never a *string* in the
code itself. You declare it one time and then use it through all the
procedure.

Thank you for the advice...

Gilles






"R. Robert Bell" a écrit dans le message de
news:21A63BB3248BAAA80B92F7D642523DE6@in.WebX.maYIadrTaRb...
> Ok, a bit more detail...
>
> Dim LayerName as String
> LayerName = "MyCoolLayer"
> ThisDrawing.Layers.Add(LayerName)
>
> This is good. Permits you to change the layer name sometime in the future
at
> one point.
>
> acXREFS.Layer = "XREFS"
>
> This is bad. In the same procedure you have used a "hard-coded" string. So
> if you change the string for the variable, and *forget* to look for other
> instances of that string in the procedure, you have created a bug.
>
> It would be far better to do this, in the 2nd case:
>
> acXREFS.Layer = LayerName
>
> This way, you only need to change the string in one place, and be
confident
> that the rest of the code will still work.
>
>
> --
> R. Robert Bell, MCSE
> www.AcadX.com
>
>
> "Gilles" wrote in message
> news:C91FC2F023959C91326BDC5063545FE9@in.WebX.maYIadrTaRb...
> | Thank you for your answer which was much more efficient than my first
> code.
> | I tried it through a few drawings and everything is OK... As a new user
I
> | am everyday surprised by the potential of VBA but I need to learn some (
> | what dou I say... a lot of) things to succeed in my objectives.
> | By the way, what do you mean exactly by "BTW, you used a variable for
the
> | layer name in one spot, and the actual string in another (can you spell
> | p-o-t-e-n-t-i-a-l b-u-g? )". What I understand is that I should use
the
> | same variable to declare the layer name and layer object ??? ... ???
> |
> |
> | Thanks again....
> |
> | Gilles
> |
> | ----- Original Message -----
> | From: "R. Robert Bell"
> | Newsgroups: autodesk.autocad.customization.vba
> | Sent: Wednesday, May 28, 2003 7:12 PM
> | Subject: Re: selecting all xrefs in modelspace
> |
> |
> | > What if ModelSpace has objects *other* than XRefs?! 😉
> | >
> | > Public Sub Test()
> | >
> | > Dim Layer_Name As String
> | > Layer_Name = "XREFS"
> | >
> | > Dim layerXREFS As AcadLayer
> | > Set layerXREFS = ThisDrawing.Layers.Add(Layer_Name)
> | >
> | > layerXREFS.color = acWhite
> | > layerXREFS.LayerOn = True
> | > layerXREFS.Linetype = "continuous"
> | > layerXREFS.Lock = False
> | > layerXREFS.PlotStyleName = "Normal"
> | > layerXREFS.Lineweight = acLnWtByLwDefault
> | > layerXREFS.Plottable = True
> | >
> | > Dim thisObj As AcadEntity
> | >
> | > For Each thisObj In ThisDrawing.ModelSpace
> | > If TypeOf thisObj Is AcadExternalReference Then thisObj.Layer =
> | > Layer_Name
> | > Next thisObj
> | >
> | > End Sub
> | >
> | > BTW, you used a variable for the layer name in one spot, and the
actual
> | > string in another (can you spell p-o-t-e-n-t-i-a-l b-u-g? )
> | >
> | >
> | > --
> | > R. Robert Bell, MCSE
> | > www.AcadX.com
> | >
> | >
> | > "Gilles" wrote in message
> | > news:074A51AC13DB08BC531859BBAA5F631E@in.WebX.maYIadrTaRb...
> | > | Hello all,
> | > |
> | > | How can I select all xrefs attached in a modelspace drawing and
change
> | > their
> | > | layer into "XREFS" for example ???
> | > |
> | > | Here is the code I created but it does not work in all my drawings.
> | Don't
> | > | understand why...
> | > |
> | > | 1° When I create a new drawing and attach XREFS inside it (in
> | modelspace)
> | > it
> | > | works fine.
> | > | 2° But when I try it in existing files it doesn't work any more.....
> 😞
> | > |
> | > |
> | > | I would really appreciate if someone more experienced than I am
could
> | help
> | > | me.
> | > |
> | > | Thanks
> | > |
> | > |
> | > | Gilles
> | > |
> | > |
> | > |
> | > | Option Explicit
> | > |
> | > | Public Sub Add_Layer_Vport()
> | > |
> | > |
> | > | Dim Layer_Name As String
> | > | Dim layerXREFS As AcadLayer
> | > |
> | > | Layer_Name = "XREFS"
> | > |
> | > | Set layerXREFS = ThisDrawing.Layers.Add(Layer_Name)
> | > |
> | > | layerXREFS.Color = acWhite
> | > | layerXREFS.LayerOn = True
> | > | layerXREFS.Linetype = "continuous"
> | > | layerXREFS.Lock = False
> | > | layerXREFS.PlotStyleName = "Normal"
> | > | layerXREFS.Lineweight = acLnWtByLwDefault
> | > | layerXREFS.Plottable = True
> | > |
> | > |
> | > | Dim acXREFS As AcadExternalReference
> | > |
> | > | For Each acXREFS In ThisDrawing.ModelSpace
> | > |
> | > | acXREFS.Layer = "XREFS"
> | > |
> | > | Next acXREFS
> | > |
> | > |
> | > | End Sub
> | > |
> | > |
> | >
> | >
> |
> |
>
>
Message 8 of 8
Anonymous
in reply to: Anonymous

Glad I could help.

--
R. Robert Bell, MCSE
www.AcadX.com


"Gilles" wrote in message
news:583136FBE8BEDEF3676852AF178ACE58@in.WebX.maYIadrTaRb...
| OK I see what you mean. Always use a variable and never a *string* in the
| code itself. You declare it one time and then use it through all the
| procedure.
|
| Thank you for the advice...
|
| Gilles
|
|

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

Post to forums  

Autodesk Design & Make Report

”Boost