Setting all entities to bylayer

Setting all entities to bylayer

Anonymous
Not applicable
885 Views
16 Replies
Message 1 of 17

Setting all entities to bylayer

Anonymous
Not applicable
I would like my macro to change all entities colour, lineweight and linestyle to bylayer.

I tried using SendCommand with "CHANGE" etc. but if it considers no entities can have their properties changed (which does happen) it then fails.

So i assume I must make a selection set of all entities or something and iterate each one? Please clarifiy. Snippet of code if possible?

Thanks.

Andrew
0 Likes
886 Views
16 Replies
Replies (16)
Message 2 of 17

Anonymous
Not applicable
Please try this code:

Dim ENTITY_NO As Integer
Dim i As Integer

For i = 0 To ENTITY_NO - 1
ThisDrawing.ModelSpace.Item(i).Linetype = "bylayer"
ThisDrawing.ModelSpace.Item(i).Lineweight = acLnWtByLayer
ThisDrawing.ModelSpace.Item(i).color = acByLayer
Next i
0 Likes
Message 3 of 17

Anonymous
Not applicable
You have not stated what teh value of ENTITY_NO should be for the for loop???
0 Likes
Message 4 of 17

Anonymous
Not applicable
You are right. there was a little mistake, the correct code is as below:

Dim ENTITY_NO As Integer
Dim i As Integer

ENTITY_NO = ThisDrawing.ModelSpace.Count
For i = 0 To ENTITY_NO - 1
ThisDrawing.ModelSpace.Item(i).Linetype = "bylayer"
ThisDrawing.ModelSpace.Item(i).Lineweight = acLnWtByLayer
ThisDrawing.ModelSpace.Item(i).color = acByLayer
Next i
0 Likes
Message 5 of 17

Anonymous
Not applicable
Thanks, this works fine!

Now I have to work out how to change this code:

Call ThisDrawing.SendCommand("UCS WORLD ")
Call ThisDrawing.SendCommand("PLAN WORLD ")

Into non-SendCommand style. Can you help me?
0 Likes
Message 6 of 17

Anonymous
Not applicable
Although this is not the way I would go about it, there are a couple of
things that need to pointed out.

First, you don't need to use a variable to hold the entity count since it is
already being held in the the Count property.

Second, since there could be more than 32768 entities in model space you
should set the data type of your Index variable to Long.


wrote in message news:4875215@discussion.autodesk.com...
You are right. there was a little mistake, the correct code is as below:

Dim ENTITY_NO As Integer
Dim i As Integer

ENTITY_NO = ThisDrawing.ModelSpace.Count
For i = 0 To ENTITY_NO - 1
ThisDrawing.ModelSpace.Item(i).Linetype = "bylayer"
ThisDrawing.ModelSpace.Item(i).Lineweight = acLnWtByLayer
ThisDrawing.ModelSpace.Item(i).color = acByLayer
Next i
0 Likes
Message 7 of 17

Anonymous
Not applicable
According to the help, the count property is of type integer..
0 Likes
Message 8 of 17

Anonymous
Not applicable
You say you would not do it this way, so why don't you offer you way?
0 Likes
Message 9 of 17

Anonymous
Not applicable
According to help the For statement uses a counter as a numeric variable. The start and end elements are values of the counter (numeric)...not integer or long specific.

The help you have given instructed you to change:

For I = 0 To Entity_No - 1

To

For I = 0 To ThisDrawing.ModelSpace.Count - 1
0 Likes
Message 10 of 17

Anonymous
Not applicable
What I was saying is:

the count property is INTEGER in the autocad help. i was not referring to the for loop syntax..

is it wise to have umpteen repreated calls to two objects "thisdrawing" and "modelspace" just to get to the count property? if i were coding in visual c++ using com, i would have to do two com attaches.

it is common practice to store a value so that it can be directly accessed.
0 Likes
Message 11 of 17

Anonymous
Not applicable
> the count property is INTEGER in the autocad help.

For a suitable definition of integer, that is correct. It's a 32-bit integer.

> is it wise to have umpteen repreated calls to two objects "thisdrawing" and "modelspace" just to get to the count property?

The VBA For statement evaluates the upper limit for its index just once. If you don't need that value for something else, it is useless to store it in a variable.
0 Likes
Message 12 of 17

Anonymous
Not applicable
OK, here is my code then:

' Sets the line weight, style and colour to bylayer
Public Sub ChangeToBylayer()
Dim iEntity As Long

For iEntity = 0 To ThisDrawing.ModelSpace.Count - 1
With ThisDrawing.ModelSpace.Item(iEntity)
.Linetype = "bylayer"
.Lineweight = acLnWtByLayer
.color = acByLayer
End With
Next iEntity

End Sub

Andrew
0 Likes
Message 13 of 17

Anonymous
Not applicable
Do you have anything in PaperSpace? Any Blocks?

[pre]
Public Sub ChangeToBylayer()

Dim Block As AcadBlock
Dim Entity As AcadEntity

For Each Entity In ThisDrawing.ModelSpace
Entity.Linetype = "ByLayer"
Entity.Lineweight = acLnWtByLayer
Entity.Color = acByLayer
Next Entity

For Each Entity In ThisDrawing.PaperSpace
Entity.Linetype = "ByLayer"
Entity.Lineweight = acLnWtByLayer
Entity.Color = acByLayer
Next Entity

For Each Block In ThisDrawing.Blocks
For Each Entity In Block
Entity.Linetype = "ByLayer"
Entity.Lineweight = acLnWtByLayer
Entity.Color = acByLayer
Next Entity
Next Block

End Sub
[/pre]
0 Likes
Message 14 of 17

Anonymous
Not applicable
In my situation, the data set is expected to be in modelspace. If there are entities in paperspace it is a non-conformance.

Concerning the type of data in the file, they are polylines, points etc. No blocks. Infact, an earlier part of my macro converts all blocks to points by using an external command.

So this should be OK now.

I have two outstanding issues now:

1. I would like to know how to set the UCS to the WCS and do a plan (at the moment I use two SendCommands to do it).

2. I would like to put the content of a script into the macro. This script assigns the colors for quite a few layers, including line styles. I am not sure of the easiest way to do it.
0 Likes
Message 15 of 17

Anonymous
Not applicable
1. Using SendCommand is the only way I know of to set the WCS.

2. Try something similar to this...

[pre]
Public Sub SetUpLayers()

Dim Layer As AcadLayer

For Each Layer In ThisDrawing.Layers
If Layer.Name Like "DIM*" Then
Layer.Linetype = "HIDDEN"
End If
Next Layer

End Sub
[/pre]
0 Likes
Message 16 of 17

Anonymous
Not applicable
> I would like to know how to set the UCS to the WCS and do a plan
> (at the moment I use two SendCommands to do it).

Does this do what you want?

[code]
Public Sub test()
Dim origin(0 To 2) As Double
Dim xAxis(0 To 2) As Double: xAxis(0) = 1
Dim yAxis(0 To 2) As Double: yAxis(1) = 1
Dim ucsFollow As Long

ucsFollow = ThisDrawing.GetVariable("UCSFOLLOW")
ThisDrawing.SetVariable "UCSFOLLOW", 1
ThisDrawing.ActiveUCS = ThisDrawing.UserCoordinateSystems.Add(origin, xAxis, yAxis, "World")
ThisDrawing.SetVariable "UCSFOLLOW", ucsFollow

End Sub
[/code]
0 Likes
Message 17 of 17

Anonymous
Not applicable
this one works best, it delves deeply into all the block entities as well, thank you!!

does anybody know how to get the attribute text to show up right without having to go thru the block attribute manager, all my text still shows up as not bylayer B(
0 Likes