Author Topic: Writing Layers to a text file  (Read 6499 times)

0 Members and 1 Guest are viewing this topic.

ML

  • Guest
Writing Layers to a text file
« on: October 11, 2007, 06:51:08 PM »

Hi

I am trying to write out my layers from a drawing to a text file.
As far as creating the text file, that part is working fine; it is grabbing the drawing name and creating a text file however
I can not get the layers to write to the file.

My guess is because layers are an object and not a string, even though the layer names are a string..... hummm?????
If I replace the line MyFile.WriteLine ThisDrawing.Layers line with something like MyFile.WriteLine "Hello", it works just fine.

Does anyone know how I can get this to work?

Thank you,

Mark

Code: [Select]
Sub WriteLayersToATextFile()

Dim FSO, MyFile As Variant
Dim Layr As AcadLayer
Dim Dwgname As String


Set FSO = CreateObject("Scripting.FileSystemObject")


Dwgname = UCase(Left$(ThisDrawing.GetVariable("Dwgname"), Len(ThisDrawing.GetVariable("Dwgname")) - 8))
 
For Each Layr In ThisDrawing.Layers
 Set MyFile = FSO.CreateTextFile("I:\Path\Path\Path\Path\" & Dwgname & ".TXT", True)
 MyFile.WriteLine ThisDrawing.Layers
Next Layr

End Sub

Glenn R

  • Guest
Re: Writing Layers to a text file
« Reply #1 on: October 11, 2007, 06:54:38 PM »
That's because you're trying to write out to the file the entire layer collection in one hit.

Change this:
Code: [Select]
MyFile.WriteLine ThisDrawing.Layers

to this:
Code: [Select]
MyFile.WriteLine Layr.Name

Glenn R

  • Guest
Re: Writing Layers to a text file
« Reply #2 on: October 11, 2007, 06:56:16 PM »
Also move your CreateTextFile line OUTSIDE of the for loop - before it in fact, otherwise you will be creating a textfile for each layer in the drawing.
Also don't forget to close the textfile when finished.

ML

  • Guest
Re: Writing Layers to a text file
« Reply #3 on: October 11, 2007, 07:09:57 PM »

Dam Glenn

Without even trying it, I know you nailed it!

Of course layer.name DUH

Also, you are correct, if I keep the writetextfile in the loop, I would get a .txt file for each layer

I will move it out

Thank you very much!

Mark

ML

  • Guest
Re: Writing Layers to a text file
« Reply #4 on: October 11, 2007, 07:34:40 PM »

Actually,

This may seem like a dumb question but I can do
Set layrs = Thisdrawing.layers

However, if I pull the layr variable out of the loop, then it needs me to set a reference to layr
and I can only use the name method on individual layers

MyFile.WriteLine Layr.Name

So, how would I set a reference to the layr variable so that each layer in the layer collection can be read, then written?

I tried something like this, but no luck
Set layr = Thisdrawing.layers.item (0)

Thanks again,

Mark

Glenn R

  • Guest
Re: Writing Layers to a text file
« Reply #5 on: October 11, 2007, 07:41:46 PM »
I don't follow you. Your code posted looks about right, but here is a quick example:

Code: [Select]
Public Sub SpewLayers()
    Dim pLayers As AcadLayers
    Dim pLayer As AcadLayer
   
    Set pLayers = ThisDrawing.Layers
   
    For Each pLayer In pLayers
        ThisDrawing.Utility.Prompt "Layer name: " & pLayer.Name & vbCrLf
    Next
End Sub

ML

  • Guest
Re: Writing Layers to a text file
« Reply #6 on: October 11, 2007, 10:58:39 PM »

Hey Glenn

OK, I figured it out and you were partially right.
Absolutely, I had to use the layer.name method to get the layer name
However, the layr.name code must remain in the loop for AutoCAD to grab the name of each layer in the layer collection.
As far as The Writeline method; it will do precisely that; it will write a line for each in layer in the layer collection
Also, as you add layers, you can re run the macro and it will overwrite the existing file with the new layers.

So, here is the working code:

Thank you for the help!

Mark

Code: [Select]
Sub WriteLayersToATextFile()

Dim FSO, MyFile As Variant
Dim Layr As AcadLayer
Dim Layrs As AcadLayers
Dim Dwgname As String

Set FSO = CreateObject("Scripting.FileSystemObject")
Set Layrs = ThisDrawing.Layers

Dwgname = UCase(Left$(ThisDrawing.GetVariable("Dwgname"), Len(ThisDrawing.GetVariable("Dwgname")) - 4))
Set MyFile = FSO.CreateTextFile("C:\Path\Path\Path\" & Dwgname & ".TXT", True)
 
For Each Layr In ThisDrawing.Layers
 MyFile.WriteLine Layr.Name
Next Layr

MyFile.Close

End Sub

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Writing Layers to a text file
« Reply #7 on: October 11, 2007, 11:30:00 PM »

Hey Glenn

OK, I figured it out and you were partially right.
Absolutely, I had to use the layer.name method to get the layer name
However, the layr.name code must remain in the loop for AutoCAD to grab the name of each layer in the layer collection.


Mark, I read that Glenn said move the File creation, not the file WriteLine.
.. I can't see where he suggested relocating the MyFile.WriteLine Layr.Name
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: Writing Layers to a text file
« Reply #8 on: October 11, 2007, 11:32:11 PM »
However, the layr.name code must remain in the loop for AutoCAD to grab the name of each layer in the layer collection.

Of course...I never said to remove it from the loop.

Glenn R

  • Guest
Re: Writing Layers to a text file
« Reply #9 on: October 11, 2007, 11:33:47 PM »
OK, I figured it out and you were partially right.

No, my answer was exactly right for the question asked.

Kerry, you beat me to it.

ML

  • Guest
Re: Writing Layers to a text file
« Reply #10 on: October 12, 2007, 10:01:41 AM »

From your second post:
Quote
Also move your CreateTextFile line OUTSIDE of the for loop - before it in fact, otherwise you will be creating a textfile for each layer in the drawing.
Also don't forget to close the textfile when finished.

OK, you're right, you said move the CreateTextFile line outside of the loop.

My fault.

You were right......feel better? :)

Thank you again

Mark


ML

  • Guest
Re: Writing Layers to a text file
« Reply #11 on: October 12, 2007, 10:04:24 AM »

I was very tired yesterday.
Sorry guys....
I looked at it last night and it then hit me.
It goes to show that sometimes you do need to walk away from it

Well, anyway, may be someone else will benefit from the final result "posted"

Thank you again

Mark

M-dub

  • Guest
Re: Writing Layers to a text file
« Reply #12 on: October 12, 2007, 10:04:32 AM »
Just a quick note, you might find something helpful here -> http://www.theswamp.org/index.php?topic=1341.15

ML

  • Guest
Re: Writing Layers to a text file
« Reply #13 on: October 12, 2007, 10:09:00 AM »

Cool
Thanks M-

Mark

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Writing Layers to a text file
« Reply #14 on: October 12, 2007, 10:21:18 AM »

Why not add Layer.Color and Layer.Linetype and make it tab delimited, then you can pull it into Excel?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)