Author Topic: Convert all to BYLAYER  (Read 19186 times)

0 Members and 1 Guest are viewing this topic.

nivuahc

  • Guest
Re: Convert all to BYLAYER
« Reply #30 on: February 01, 2006, 05:01:43 PM »
I hope you guys have Flash installed so you can see (and hear) this. :)

Jeff_M

  • King Gator
  • Posts: 4086
  • C3D user & customizer
Re: Convert all to BYLAYER
« Reply #31 on: February 01, 2006, 05:39:42 PM »
OK, here's what I threw together.....I have not tested on a large drawing, just a small test dwg. I think it satisfies everything Chuck wanted. I kept it all in one Sub to eliminate any multiple accesses to the blocks/model space.

Trouble is, I gotta leave for the remainder of the day so I won't be available for questions or floggings.

Make sure to add a reference, as noted in the code, to the MS Scripting Runtime....this is so we can use the Dictionary object which has an EXISTS method, slick way to see if something is in a list of items.

Good Luck!
Code: [Select]
Sub Delete_Freeze_N_Off_Fix_Color_N_Linetype()
'requires reference to MS Scripting Runtime
Dim oLayer As AcadLayer
Dim oEnt As AcadEntity
Dim I As Integer
Dim colLays As New Dictionary
Dim oBlkRef As AcadBlockReference
Dim oatts As Variant
Dim oatt As AcadAttributeReference

'First let's create the Base layer with the color 252
Set oLayer = ThisDrawing.Layers.Add("E-BASE-PLAN")
oLayer.Color = 252
'Now set the current layer to it
ThisDrawing.ActiveLayer = oLayer
'create a dictionary of frozen/off layers
For Each oLayer In ThisDrawing.Layers
    If (oLayer.Freeze = True) Or (oLayer.LayerOn = False) Then
        colLays.Add oLayer.Name, I
        I = I + 1
    End If
    oLayer.Lock = False 'make sure they are all unlocked
Next

''Now check the block defs for objects on these layers
Dim oBlk As AcadBlock
For Each oBlk In ThisDrawing.Blocks
    If oBlk.Name = "*Model_Space" Then 'First up, Modelspace
        For Each oEnt In oBlk
            If colLays.Exists(oEnt.Layer) Then 'Delete frozen/Off objects
                oEnt.Delete
            Else ' it's visible, change the color/linetype to suit
                oEnt.Color = acByLayer
                If UCase(oEnt.Linetype) = "BYLAYER" Then
                    oEnt.Linetype = ThisDrawing.Layers.Item(oEnt.Layer).Linetype
                End If
                oEnt.Layer = "E-BASE-PLAN"
                If TypeOf oEnt Is AcadBlockReference Then
                    Set oBlkRef = oEnt
                    If oBlkRef.HasAttributes Then
                        oatts = oBlkRef.GetAttributes
                        For I = 0 To UBound(oatts)
                            Set oatt = oatts(I)
                            oatt.Color = acByLayer
                            oatt.Layer = "E-BASE-PLAN"
                        Next
                    End If
                End If
            End If
        Next
    ElseIf (oBlk.IsLayout = False) And (oBlk.IsXRef = False) Then
    'repeat for all other blocks, but leaving BYBLOCK objects alone
        For Each oEnt In oBlk
            If colLays.Exists(oEnt.Layer) Then
                oEnt.Delete
            Else
                If oEnt.Color <> acByBlock Then oEnt.Color = acByLayer
                If UCase(oEnt.Linetype) <> "BYBLOCK" Or UCase(oEnt.Linetype) = "BYLAYER" Then
                    oEnt.Linetype = ThisDrawing.Layers.Item(oEnt.Layer).Linetype
                End If
                oEnt.Layer = "E-BASE-PLAN"
                If TypeOf oEnt Is AcadBlockReference Then
                    Set oBlkRef = oEnt
                    If oBlkRef.HasAttributes Then
                        oatts = oBlkRef.GetAttributes
                        For I = 0 To UBound(oatts)
                            Set oatt = oatts(I)
                            oatt.Color = acByLayer
                            oatt.Layer = "E-BASE-PLAN"
                        Next
                    End If
                End If
            End If
        Next
    End If
Next
End Sub

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4073
Re: Convert all to BYLAYER
« Reply #32 on: February 01, 2006, 08:58:53 PM »
good job, I see that I have a lot to learn still
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)

nivuahc

  • Guest
Re: Convert all to BYLAYER
« Reply #33 on: February 02, 2006, 11:15:28 AM »
Code: [Select]
'requires reference to MS Scripting Runtime :?

is that why I get an error on this line when I try to run it?

Code: [Select]
Dim colLays As New Dictionary
Quote from: That nifty little error dialog
Compile error:

User-defined type not defined

A little enlightenment might be in order  :angel:

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4073
Re: Convert all to BYLAYER
« Reply #34 on: February 02, 2006, 11:24:19 AM »
i'll go out on a limb here, but i think in the IDE, go to Tools->references and find the MS Scripting Runtime
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)

Jeff_M

  • King Gator
  • Posts: 4086
  • C3D user & customizer
Re: Convert all to BYLAYER
« Reply #35 on: February 02, 2006, 11:29:45 AM »
Yep, that's it! Just make sure to add a check mark next to it once found :-)

nivuahc

  • Guest
Re: Convert all to BYLAYER
« Reply #36 on: February 02, 2006, 11:44:00 AM »
yep, that was it. :)

And the routine almost works :D

It leaves a few layers behind for some reason... I'll attach the drawing I tried it on if you wanna check it out (this drawing is pretty typical of what I get on a regular basis)

nivuahc

  • Guest
Re: Convert all to BYLAYER
« Reply #37 on: February 02, 2006, 11:55:45 AM »
oops!

Forgot to mention that I added this towards the end:

Code: [Select]
    End If
Next
ThisDrawing.PurgeAll
End Sub

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4073
Re: Convert all to BYLAYER
« Reply #38 on: February 02, 2006, 11:56:41 AM »
I would also add ThisDrawing.Save
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)

Jeff_M

  • King Gator
  • Posts: 4086
  • C3D user & customizer
Re: Convert all to BYLAYER
« Reply #39 on: February 02, 2006, 05:33:22 PM »
The layers that won't purge are kind of tough to get out......I'm working on a solution, but every time i think it's done, one more little problem crops up. And now, when I'm so close to getting it, I was just informed of some REAL work that, for some reason, they want done today. So I'm off of this until later on or maybe tomorrow.

Oh, the reason for them not being easily removed, is due to that layer being assigned to the EndBlk entity which cannot be obtained through normal means. I have a function that was posted on the old cadvault site that works sometimes, but other times it fails/errors out. I think that a part of this is due to a number of block definitions are empty but they won't purge since they are also inserted......'tis a vicious circle.

Once I get back to it I'll post my solution, or if someone else would like to pick it up from here......

nivuahc

  • Guest
Re: Convert all to BYLAYER
« Reply #40 on: February 02, 2006, 07:12:43 PM »
That's great Jeff, thanks :)

One other thing I ran into today (I was playing around with it, trying it out on different drawings) was when I open a drawing that has proxy objects in it (in this case it was a civil plan) the routine runs... but nothing happens (other than the layer being created). No error or anything. And, when I closed AutoCAD (2002) it crashed. :)

I appreciate both of you helping me with this, now I need to understand it better.

Jeff_M

  • King Gator
  • Posts: 4086
  • C3D user & customizer
Re: Convert all to BYLAYER
« Reply #41 on: February 02, 2006, 08:18:47 PM »
Hi Chuck, you're welcome. The proxy_object issue I think will be pretty easy to fix (although the layers won't be modified for them), which should stop the crash. I'll try to setup a vanilla install to test with.

I'll try to place some comments in the final code for you to follow.....

nivuahc

  • Guest
Re: Convert all to BYLAYER
« Reply #42 on: February 03, 2006, 08:33:37 AM »
ooooo... and MText. How would I go about changing the color of Mtext? That doesn't seem to work either  :|

Jeff_M

  • King Gator
  • Posts: 4086
  • C3D user & customizer
Re: Convert all to BYLAYER
« Reply #43 on: February 03, 2006, 12:01:22 PM »
ooooo... and MText. How would I go about changing the color of Mtext? That doesn't seem to work either :|
This is a bit tougher since Mtext can have multiple colors embedded within the text.......each Mtext object would need to have the Textstring searched for all occurances of {\C#; and remove them, along with the closing } of each one found.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Convert all to BYLAYER
« Reply #44 on: February 03, 2006, 12:06:00 PM »
This is a bit tougher since Mtext can have multiple colors embedded within the text.......each Mtext object would need to have the Textstring searched for all occurances of {\C#; and remove them, along with the closing } of each one found.
Here is a lisp code to strip some of the formatting for mtext.
Code: [Select]
(defun StripString (String / cstr1 cstr2 nString cnt1 tstr1)
; Strips out formation for color, font, height and width.

(setq cnt1 1)
(while (and (setq cstr1 (substr String 1 1)) (> (strlen String) 0))
 (if (= cstr1 "\\")
  (progn
   (setq cstr2 (substr String 2 1))
   (if (member (strcase cstr2) '("C" "F" "H" "W"))
    (progn
     (while (/= (substr String cnt1 1) ";")
      (setq cnt1 (1+ cnt1))
     ); while
     (setq String (substr String (1+ cnt1) (strlen String)))
     (setq cnt1 1)
    ); progn
    (progn
     (if nString
      (setq nString (strcat nString (substr String 1 1)))
      (setq nString (substr String 1 1))
     ); if
     (setq String (substr String 2 (strlen String)))
    ); progn
   ); if
  ); progn
  (progn
   (if nString
    (setq nString (strcat nString (substr String 1 1)))
    (setq nString (substr String 1 1))
   ); if
   (setq String (substr String 2 (strlen String)))
  ); progn
 ); if
); while
(setq tstr1 (vl-string->list nString))
(if (and (not (member 92 tstr1)) (member 123 tstr1))
 (setq tstr1 (vl-remove-if '(lambda (x) (or (= x 123) (= x 125))) tstr1))
); if
(vl-list->string tstr1)
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.