Author Topic: Move object to layers  (Read 9329 times)

0 Members and 1 Guest are viewing this topic.

CADwoman

  • Guest
Move object to layers
« on: February 10, 2005, 11:36:00 AM »
Hello,

I'm new to this forum. I need help on writing a startup lisp routine to move all viewport objects to the defpoint layer. THis is what I have so far:

MOVE VIEWPORT OBJECTS TO DEFPOINT
(setq ss1 (ssget "X" '((0 . "VIEWPORT"))))
(command "chprop" "p" """layer""defpoints""")

It works, but the only problem is when you open a file that does not have a viewport, the autocad returns an error:

Invalid option keyword.

! Function cancelled

What can I use to detect if the file is in paperspace?

Thanks

SMadsen

  • Guest
Move object to layers
« Reply #1 on: February 10, 2005, 11:42:59 AM »
You could check TILEMODE:

Code: [Select]
(cond ((= (getvar "TILEMODE") 0)
       (setq ss1 (ssget "X" '((0 . "VIEWPORT"))))
       (command "chprop" ss1 "" "layer" "defpoints" ""))
)


or you could simply issue the command only if SSGET created a selection:

Code: [Select]
(if (setq ss1 (ssget "X" '((0 . "VIEWPORT"))))
  (command "chprop" ss1 "" "layer" "defpoints" "")
)


Welcome to theSwamp, by the way.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Move object to layers
« Reply #2 on: February 10, 2005, 11:45:32 AM »
Maybe I misunderstood the question.   :roll:
But what about something like this?
Code: [Select]

(if (> (length (setq ss1 (ssget "X" '((0 . "VIEWPORT")))))  0)
    (command "chprop" "p" """layer""defpoints"""))
    )




*stig wins again*
:D
TheSwamp.org  (serving the CAD community since 2003)

CADwoman

  • Guest
Move object to layers
« Reply #3 on: February 10, 2005, 12:04:32 PM »
Thank you so much! this is great!

Quote from: SMadsen
You could check TILEMODE:

Code: [Select]
(cond ((= (getvar "TILEMODE") 0)
       (setq ss1 (ssget "X" '((0 . "VIEWPORT"))))
       (command "chprop" ss1 "" "layer" "defpoints" ""))
)


or you could simply issue the command only if SSGET created a selection:

Code: [Select]
(if (setq ss1 (ssget "X" '((0 . "VIEWPORT"))))
  (command "chprop" ss1 "" "layer" "defpoints" "")
)


Welcome to theSwamp, by the way.

CADwoman

  • Guest
Move object to layers
« Reply #4 on: February 22, 2005, 10:42:50 AM »
Thanks, when I ran it, AUtocad returned:

! bad argument type: listp <Selection set: 9>

Quote from: Mark Thomas
Maybe I misunderstood the question.   :roll:
But what about something like this?
Code: [Select]

(if (> (length (setq ss1 (ssget "X" '((0 . "VIEWPORT")))))  0)
    (command "chprop" "p" """layer""defpoints"""))
    )




*stig wins again*
:D

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Move object to layers
« Reply #5 on: February 22, 2005, 11:29:08 AM »
Mark meant to use sslength but inadvertently used length. Change that and it will work.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Move object to layers
« Reply #6 on: February 22, 2005, 11:44:37 AM »
You may want to include error trap for empty ss1, like this:
Code: [Select]
(if (and (setq ss1 (ssget "X" '((0 . "VIEWPORT"))))
         (> (sslength ss1)  0))
   (command "chprop" "p" """layer""defpoints""")
)


Edit
Now that i look at it ss1 will never be zero length, right?
so just test for nil.
Code: [Select]
(if (setq ss1 (ssget "X" '((0 . "VIEWPORT"))))
   (command "chprop" "p" """layer""defpoints""")
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Move object to layers
« Reply #7 on: February 22, 2005, 02:20:36 PM »
And on further review, this won't always work either. (ssget "x" '((0 . "VIEWPORT"))) will ALWAYS return a selection set, even if you have no VP's defined. Each layout tab has a VP associated with it that you cannot do anything with (without generating errors anyway). Also, since the command is using "p" for the selection set, there is no reason to use (setq ss1.....

Command: chprop

Select objects: p
1 found
1 was the paper space viewport.

Select objects:
Command:

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Move object to layers
« Reply #8 on: February 22, 2005, 02:26:14 PM »
Shoot, hit the submit button by mistake....
Here's code that will work providing that the defpoints layer exists.
Code: [Select]

(if (> (sslength (ssget "X" '((0 . "VIEWPORT")))) 1)
   (command "chprop" "p" """layer""defpoints""")
)

CADwoman

  • Guest
Move object to layers
« Reply #9 on: February 22, 2005, 03:06:32 PM »
Thank you all for your help. Now I have a startup script that checks for Tilemode 0, then move all viewport objects to defpoint and then lock the VP display. This is very useful to me because I'm dealing with a large group of users who don't always remember to put Vports in the right layers.

Thanks again!


;DESCRIPTION:;MOVE VIEWPORT OBJECTS TO DEFPOINTS and DISPLAY LOCK
(cond ((= (getvar "TILEMODE") 0)
       if(setq ss1 (ssget "X" '((0 . "VIEWPORT"))))
       (command "chprop" ss1 "" "layer" "defpoints" "")
   (command "-vports" "lock" "on" ss1""))
)

ronjonp

  • Needs a day job
  • Posts: 7529
Move object to layers
« Reply #10 on: February 22, 2005, 03:24:59 PM »
CadWoman,

I don't know how many people you work with, but if you could get this vba routine into their startup suite.....you wouldn't have to worry about the viewports being on the wrong layer.

http://theswamp.org/lilly_pond/ronjonp/create_lyrs.zip?nossi=1

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Move object to layers
« Reply #11 on: February 22, 2005, 03:32:28 PM »
CADwoman,
If you use more than 1 layout then your routine will only work with the active one. In order to modify the viewports in ANY layout, regardless of TILEMODE value, you could use ActiveX to accomplish the task. If you'd like to see that solution, just say so......

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Move object to layers
« Reply #12 on: February 22, 2005, 03:34:15 PM »
Jeff,
Thanks for the info.
If the selection set is always 1 or greater, you don't need the test, just
Code: [Select]
(ssget "X" '((0 . "VIEWPORT")))
(command "chprop" "p" """layer""defpoints""")


BUT, I did some testing and got this error
Code: [Select]

chprop
Select objects: p 1 found
1 was not in current space.

Select objects:
Command: layer
Current layer:  "0"
Enter an option
[?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/Plot/PStyle/Freeze/Thaw/LOck/Unlock]:
 0
Invalid option keyword.


I tried several combinations & what I found was this.
If you are in model space the chprop command generates an error
If you are in a tab with no vp it generates an error
If you have DWG with 2 tabs and no VP will generate an error
If you have a DWG with 2 tabs & one vp not in the current tab it generates an error
If you have a DWG with 2 tabs & one vp in the current tab & active it generates an error

Conclusion:
chprop only works on items in the current space.
(ssget "X" '((0 . "VIEWPORT"))) gets all tabs as well as view ports so the tabs must be filtered

Looks like more code is needed. 8)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Move object to layers
« Reply #13 on: February 22, 2005, 03:36:35 PM »
Ugh, Looks like Jeff already knew that. :shock:
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Move object to layers
« Reply #14 on: February 22, 2005, 03:58:58 PM »
Quote from: Jeff_M
Mark meant to use sslength but inadvertently used length. Change that and it will work.

Past due reply to follow

Thanks for pointing that out Jeff.
TheSwamp.org  (serving the CAD community since 2003)

CADwoman

  • Guest
Move object to layers
« Reply #15 on: February 23, 2005, 02:37:45 PM »
So does this mean we need to look into VB for this function? Seems so easy.

Quote from: CAB
Ugh, Looks like Jeff already knew that. :shock:

sinc

  • Guest
Move object to layers
« Reply #16 on: February 23, 2005, 02:52:42 PM »
Just as a note, you might want to consider putting your viewports on some layer other than Defpoints.  Defpoints is weird - it is linked to layer 0 and behaves in odd ways sometimes.  I know in the old days, putting objects on Defpoints was the only way to keep visible objects from plotting on paper, but that isn't true anymore.

Having a dedicated viewport layer is nicer on several levels.  I actually have two that I call -VP-HIDDEN and -VP-VISIBLE.  The hidden layer has plotting disabled in the layer manager.  Preceding the viewport names with a hyphen makes them appear in the layer manager right before layer 0, along with my -XREF layer.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Move object to layers
« Reply #17 on: February 23, 2005, 02:57:41 PM »
This is close to what you need for a VBA solution
Code: [Select]
Private Sub AcadDocument_BeginClose()

Dim objLay As AcadLayer, colLay As AcadLayers
Dim Ent As AcadEntity, pVP As AcadPViewport
    On Error GoTo Exit_Here
    If ThisDrawing.Saved = True Then
   
   
        For Each Ent In ThisDrawing.PaperSpace
            If Ent.ObjectName = "AcDbViewport" Then
                Set pVP = Ent
                pVP.Layer = "Defpoints"
                If Not pVP.DisplayLocked Then pVP.DisplayLocked = True
            End If
        Next Ent
       
        ZoomExtents
        ThisDrawing.PurgeAll
        Else: Exit Sub
    End If
 
    ThisDrawing.Save
Exit Sub

Exit_Here:
  MsgBox Err.Description & Err.Number
Exit Sub

End Sub
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)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Move object to layers
« Reply #18 on: February 23, 2005, 03:02:15 PM »
Quote from: CADwoman
So does this mean we need to look into VB for this function? Seems so easy.

Quote from: CAB
Ugh, Looks like Jeff already knew that. :shock:


No, you can do it in plain or vislisp. Just can't use the command chprop.
If Jeff or others don't offer you some code I'll look at it on Friday.
Darn work keeps getting in the way. :shock:
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Move object to layers
« Reply #19 on: February 23, 2005, 03:18:33 PM »
OK, work can wait. :)
Try this:
Code: [Select]
(defun c:vp2layer( / lyr ssvp idx)
  (vl-load-com)
  (setq lyr "0" ; enter your layer here
        clr "")
  (if (not(setq tbl (tblsearch "LAYER" lyr)))
      (command "._Layer" "_Make" lyr "_Color" (if (= Clr "") "_White" Clr) lyr "")
  )

  (and (setq ssvp (ssget "X" '((0 . "VIEWPORT"))))
       (repeat (setq idx (sslength ssvp))
  (vla-put-layer
  (vlax-ename->vla-object (ssname ssvp (setq idx (1- idx))))
  lyr
)
       )
  )
  (print (strcat "All viewports are now onlayer " lyr "."))
  (princ)
)
(prompt "\nViewport to layer loaded, Enter vp2layer to run.")
(princ)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Move object to layers
« Reply #20 on: February 23, 2005, 04:35:49 PM »
Good job CAB! I would've put it before work too. :D

Here's yours modified to add the lock portion.
Code: [Select]

(defun c:vp2layer( / lyr ssvp idx)
  (vl-load-com)
  (setq lyr "0" ; enter your layer here
        clr "")
  (if (not(setq tbl (tblsearch "LAYER" lyr)))
      (command "._Layer" "_Make" lyr "_Color" (if (= Clr "") "_White" Clr) lyr "")
  )

  (and (setq ssvp (ssget "X" '((0 . "VIEWPORT"))))
       (repeat (setq idx (sslength ssvp))
(setq vp (vlax-ename->vla-object (ssname ssvp (setq idx (1- idx)))))
(vla-put-layer vp lyr)
(vla-put-displaylocked vp :vlax-true)
    )
       )
  (print (strcat "All viewports are now locked and on layer " lyr "."))
  (princ)
)
(prompt "\nViewport to layer loaded, Enter vp2layer to run.")
(princ)

CADwoman

  • Guest
Move object to layers
« Reply #21 on: February 24, 2005, 12:42:00 PM »
You guys are great! The only thing is, when I ran it' the Layer command displays everything it's doing. So I've been trying to modify by using Subst. Something like this. I'm still trying to figure it out, but what do you think?

(setq ssvp (subst (cons 8 lyr)(assoc 8 ssvp) ssvp))

._Layer
Current layer:  "A-ANNO-REFR"
Enter an option
[?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/Plot/Freeze/Thaw/LOck/Unlock/stAte]:
_Make
Enter name for new layer (becomes the current layer) <A-ANNO-REFR>: A-ANNO-VPRT
Enter an option
[?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/Plot/Freeze/Thaw/LOck/Unlock/stAte]:
Plot
Enter a plotting preference [Plot/No plot] <Plot>: N
Enter layer name(s) for this plot preference <A-ANNO-VPRT>: A-ANNO-VPRT Enter
an option
[?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/Plot/Freeze/Thaw/LOck/Unlock/stAte]:
_Color
New color [Truecolor/COlorbook] <7 (white)>: _White
Enter name list of layer(s) for color 7 (white) <A-ANNO-VPRT>: A-ANNO-VPRT
Enter an option
[?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/Plot/Freeze/Thaw/LOck/Unlock/stAte]:
"All viewports are now display locked and moved to layer A-ANNO-VPRT."

Quote from: Jeff_M
Good job CAB! I would've put it before work too. :D

Here's yours modified to add the lock portion.
Code: [Select]

(defun c:vp2layer( / lyr ssvp idx)
  (vl-load-com)
  (setq lyr "0" ; enter your layer here
        clr "")
  (if (not(setq tbl (tblsearch "LAYER" lyr)))
      (command "._Layer" "_Make" lyr "_Color" (if (= Clr "") "_White" Clr) lyr "")
  )

  (and (setq ssvp (ssget "X" '((0 . "VIEWPORT"))))
       (repeat (setq idx (sslength ssvp))
(setq vp (vlax-ename->vla-object (ssname ssvp (setq idx (1- idx)))))
(vla-put-layer vp lyr)
(vla-put-displaylocked vp :vlax-true)
    )
       )
  (print (strcat "All viewports are now locked and on layer " lyr "."))
  (princ)
)
(prompt "\nViewport to layer loaded, Enter vp2layer to run.")
(princ)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Move object to layers
« Reply #22 on: February 24, 2005, 12:50:05 PM »
Quite Mode. :)
Code: [Select]
(defun c:vp2layer( / lyr ssvp idx usercmd)
  (vl-load-com)
  (setq usercmd (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (setq lyr "0" ; enter your layer here
        clr "")
  (if (not(setq tbl (tblsearch "LAYER" lyr)))
      (command "._Layer" "_Make" lyr "_Color" (if (= Clr "") "_White" Clr) lyr "")
  )

  (and (setq ssvp (ssget "X" '((0 . "VIEWPORT"))))
       (repeat (setq idx (sslength ssvp))
    (setq vp (vlax-ename->vla-object (ssname ssvp (setq idx (1- idx)))))
    (vla-put-layer vp lyr)
    (vla-put-displaylocked vp :vlax-true)
    )
       )
  (print (strcat "All viewports are now locked and on layer " lyr "."))
  (setvar "CMDECHO" usercmd)
  (princ)
)
(prompt "\nViewport to layer loaded, Enter vp2layer to run.")
(princ)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CADwoman

  • Guest
Move object to layers
« Reply #23 on: February 24, 2005, 12:54:01 PM »
oops, I forgot about that. Thanks. See what happens when I try to sound smart like you guys.   :wink:

Quote from: CAB
Quite Mode. :)
Code: [Select]
(defun c:vp2layer( / lyr ssvp idx usercmd)
  (vl-load-com)
  (setq usercmd (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (setq lyr "0" ; enter your layer here
        clr "")
  (if (not(setq tbl (tblsearch "LAYER" lyr)))
      (command "._Layer" "_Make" lyr "_Color" (if (= Clr "") "_White" Clr) lyr "")
  )

  (and (setq ssvp (ssget "X" '((0 . "VIEWPORT"))))
       (repeat (setq idx (sslength ssvp))
    (setq vp (vlax-ename->vla-object (ssname ssvp (setq idx (1- idx)))))
    (vla-put-layer vp lyr)
    (vla-put-displaylocked vp :vlax-true)
    )
       )
  (print (strcat "All viewports are now locked and on layer " lyr "."))
  (setvar "CMDECHO" usercmd)
  (princ)
)
(prompt "\nViewport to layer loaded, Enter vp2layer to run.")
(princ)