Author Topic: Pick a Viewport and Change Xref Layer Color  (Read 6769 times)

0 Members and 1 Guest are viewing this topic.

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Pick a Viewport and Change Xref Layer Color
« on: August 13, 2012, 08:59:49 PM »
Hi Swampers;

can you help me on how to ask the user to pick a viewport in which i need to change a certain Xref Layer. I want to run the lisp and ask the user which wieport he wants the xref layer be change

upon running the lips it will automatically switch to layout tab

how do i ask the user which layout tab he wish to run the routine and how do i check if he is already in the layout tab?


Code: [Select]
(DEFUN C:C8VP () (COMMAND "Tilemode" "0" "._vplayer" "C" "8" "*AARCH*" ) (princ))
« Last Edit: August 13, 2012, 09:04:57 PM by NOD684 »
"memories fade but the scars still linger"

ribarm

  • Gator
  • Posts: 3306
  • Marko Ribar, architect
Re: Pick a Viewport and Change Xref Layer Color
« Reply #1 on: August 13, 2012, 11:32:09 PM »
how do i check if he is already in the layout tab?

Code: [Select]
(vl-load-com)
(defun c:CheckActiveLayout ( / *alay* *blck* *name* )
  (setq *alay* (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
  (setq *blck* (vla-get-block *alay*))
  (setq *name* (vla-get-name *blck*))
  (princ *name*)
  (princ)
)

Or if you want name of Layout that is shown in switch bar :
Code: [Select]
(vl-load-com)
(defun c:CheckActiveLayout ( / *alay* alayename )
  (setq *alay* (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
  (setq alayename (vlax-vla-object->ename *alay*))
  (princ (cdr (assoc 1 (reverse (entget alayename)))))
  (princ)
)

M.R.
« Last Edit: August 14, 2012, 12:30:23 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12925
  • London, England
Re: Pick a Viewport and Change Xref Layer Color
« Reply #2 on: August 14, 2012, 08:08:41 AM »
how do i ask the user which layout tab he wish to run the routine and how do i check if he is already in the layout tab?

Perhaps use a DCL List Box to prompt the user.

The function to which I have linked could be called in the following way:

Code - Auto/Visual Lisp: [Select]
  1. (if (setq tab (LM:ListBox "Select Layout Tab" (layoutlist) nil))
  2.     (progn
  3.         (setvar 'ctab tab)
  4.         ;; ... < do your stuff here > ...
  5.     )
  6. )

Or, if you wanted the layouts listed in tab order:

Code - Auto/Visual Lisp: [Select]
  1.     (if (/= "Model" (vla-get-name lay))
  2.         (setq lst (cons (cons (vla-get-name lay) (vla-get-taborder lay)) lst))
  3.     )
  4. )
  5. (if (setq tab (LM:ListBox "Select Layout Tab" (mapcar 'car (vl-sort lst '(lambda ( a b ) (< (cdr a) (cdr b))))) nil))
  6.     (progn
  7.         (setvar 'ctab tab)
  8.         ;; ... < do your stuff here > ...
  9.     )
  10. )

(Remember to localise the variables!)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Pick a Viewport and Change Xref Layer Color
« Reply #3 on: August 14, 2012, 09:24:45 AM »
You can follow a similar approach for the selecting viewport as the ChSpace command. Issue the MSpace command, ask the user for some input (any input will do), then check if the CVport system variable > 1. Then simply run the vplayer command to set the relevant layer(s) overrides.

If you're setting the overrides through programming, then you simply need an entsel after ensuring the paperspace is current (cvport <= 1, use PSpace command to set). Then you'll need to look at the layer's xdictionaries and add a "ADSK_XREC_LAYER_COLOR_OVR" dictionary. This dictionary would then contain one of these for each VP in which it is overridden:
Code: [Select]
(102 . "{ADSK_LYR_COLOR_OVERRIDE")
(335 . <Viewport's EName>)
(420 . -1023410173) ;RGB value of colour - e.g. this is green (color 3), -1023410175=Red(1), -1023409921=255, etc.
(102 . "}")
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Re: Pick a Viewport and Change Xref Layer Color
« Reply #4 on: August 14, 2012, 09:51:13 AM »
:slapforehead:

i think what am planning to do is far more advance than my knowledge
am quite new to this lisp programming thing :)

dont know much bout vla and stuff...

but i am willing to learn

thanks for the suggestions...i'll see if i can do it :)
"memories fade but the scars still linger"

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Pick a Viewport and Change Xref Layer Color
« Reply #5 on: August 14, 2012, 10:27:42 AM »
The vla stuff ribarm's shown is simply using the ActiveX/COM object as you would have done in VBA. In normal lisp you would rather just get the value of the current tab system variable (as Lee's code shows). The following gives the exact same result as the last of ribarm's commands:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:CheckActiveLayout ()
  2.   (princ (getvar "CTab"))
  3.   (princ))
It's only when you need to work with the ActiveX object of the current layout tab's block definition that you need to go about it the way ribarm's shown.

If you follow my first idea, it's not a lot different from what you already have. You'll need to check if a paperspace tab is current, either check if the CTab /= "Model" or that TileMode = 0. Next ensure that you're inside a viewport, you can check by something like this:
Code - Auto/Visual Lisp: [Select]
  1. (if (> (getvar "CVport") 1)
  2.   (princ "Inside a ViewPort or on the Model tab")
  3.   (princ "On a paperspace tab"))
If you simply want to enforce being inside a viewport on the last layout tab which was open, just do this:
Code - Auto/Visual Lisp: [Select]
  1. (setvar "TileMode" 0)
  2. (command "._MSPACE")
Then ask the user to pick a viewport using a getpoint call. Then simply run your code to set the layer override for the current viewport.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Re: Pick a Viewport and Change Xref Layer Color
« Reply #6 on: August 14, 2012, 11:22:31 PM »
i got it working...

Code: [Select]
(defun c:try ()
(command "Tilemode" 0 "pspace")
(setq UserVP (entsel "\nSelect Viewport to work on:"))
(COMMAND "._vplayer" "C" "8" "*AARCH*" "select" uservp "" "")
(princ))

the problem is that if the current situation is if the user is in Layout tab and MSpace is active...the user will not be able to pick a viewport

and if the CTAB is not the Layout that he wants to work on
not really good with the "if" and "cond" and error taps:scratchhead:
« Last Edit: August 15, 2012, 12:03:08 AM by NOD684 »
"memories fade but the scars still linger"

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Pick a Viewport and Change Xref Layer Color
« Reply #7 on: August 15, 2012, 02:42:13 AM »
Have you used the ChSpace (Change Space) command before? You don't "select" the viewport, you click once inside it to make it active.

If you do want to select the viewport, then you'd need to find its ID. Code 69 in its entget list. Then after issuing the MSpace command to change into model space of a viewport, set the CVPort sysvar to the value of that ID number. The 1st Viewport is always 2, 1 being reserved for the PaperSpace itself.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Re: Pick a Viewport and Change Xref Layer Color
« Reply #8 on: August 15, 2012, 03:07:27 AM »
Have you used the ChSpace (Change Space) command before? You don't "select" the viewport, you click once inside it to make it active.

If you do want to select the viewport, then you'd need to find its ID. Code 69 in its entget list. Then after issuing the MSpace command to change into model space of a viewport, set the CVPort sysvar to the value of that ID number. The 1st Viewport is always 2, 1 being reserved for the PaperSpace itself.

Yes, i use CHSpace to Copy object from Mspace to Pspace and vice versa

So what you're saying is that i go to Layout, Pick inside a viewport to make it active, Select a Layer in Mspace (layer can be xref or non-xrefed), Do changes and go back to Pspace

That would be very nice but i don't think am capable of doing that at this time...i will need to learn more :sigh:

what i did is just to change a certain layer in a certain viewport only and change it to color 8
"memories fade but the scars still linger"

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Pick a Viewport and Change Xref Layer Color
« Reply #9 on: August 15, 2012, 03:53:41 AM »
Something like this perhaps:
Code - Auto/Visual Lisp: [Select]
  1. ;; Function to check if at least one layer exists matching a wildcard name
  2. (defun CheckLayerExists  (name-match / lay found)
  3.   (setq name-match (strcase name-match)) ;Ensure uppercase wildcard
  4.   (while (and (not found) (setq lay (tblnext "LAYER" (not lay)))) ;Step through all layers until one found
  5.     (setq found (wcmatch (strcase (cdr (assoc 2 lay))) name-match))) ;Match the current layer's name to the wildcard
  6.   found) ;Return found= nil/T
  7.  
  8. ;; Command to override the colour of matching layers inside a selected viewport
  9. (defun c:OvrCol  (/ *error* match)
  10.   (setq match "*AARCH*") ;Save the wildcard match, since it's used more than once
  11.   (cond ((CheckLayerExists match) ;Check that at least one layer with this name exists
  12.          (defun *error*  (msg) ;Error handler to work even if Esc pressed
  13.            (cond ((or (not msg) (wcmatch (strcase msg) "*EXIT*,*CANCEL*,*ABORT*")) ;Check if no error / Esc-type-error
  14.                   (if (> (getvar "CVPort") 1) ;Check if inside a viewport
  15.                     ;; Change the colour override for all matching layers inside the current viewport
  16.                     (command "._VPLAYER" "_Color" 8 match "_Current" "")))
  17.                  ((princ "\nError: ") (princ msg))) ;Else a normal error occured
  18.            (princ))
  19.          (setvar "TILEMODE" 0) ;Ensure you're on a paperspace tab
  20.          (command "._PSPACE") ;Ensure you're NOT inside a viewport
  21.          (command "._Zoom" "_Extents") ;Ensure all viewports are visible
  22.          (command "._MSPACE") ;Go into the last active viewport
  23.          ;; Ask user to do something inside the VP they want. If Esc pressed the *error* routine will fire
  24.          (getpoint "\nPick inside the viewport you want to set.")
  25.          (*error* nil) ;Call the error routine if user picked a point / pressed Space/Enter
  26.          )
  27.         ;; Else if no matching layers found
  28.         ((princ (strcat "\nNo layers matching " match " exists, routine stopped."))))
  29.   (princ))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Re: Pick a Viewport and Change Xref Layer Color
« Reply #10 on: August 15, 2012, 05:45:16 AM »
Something like this perhaps:
Code - Auto/Visual Lisp: [Select]
  1. ;; Function to check if at least one layer exists matching a wildcard name
  2. (defun CheckLayerExists  (name-match / lay found)
  3.   (setq name-match (strcase name-match)) ;Ensure uppercase wildcard
  4.   (while (and (not found) (setq lay (tblnext "LAYER" (not lay)))) ;Step through all layers until one found
  5.     (setq found (wcmatch (strcase (cdr (assoc 2 lay))) name-match))) ;Match the current layer's name to the wildcard
  6.   found) ;Return found= nil/T
  7.  
  8. ;; Command to override the colour of matching layers inside a selected viewport
  9. (defun c:OvrCol  (/ *error* match)
  10.   (setq match "*AARCH*") ;Save the wildcard match, since it's used more than once
  11.   (cond ((CheckLayerExists match) ;Check that at least one layer with this name exists
  12.          (defun *error*  (msg) ;Error handler to work even if Esc pressed
  13.            (cond ((or (not msg) (wcmatch (strcase msg) "*EXIT*,*CANCEL*,*ABORT*")) ;Check if no error / Esc-type-error
  14.                   (if (> (getvar "CVPort") 1) ;Check if inside a viewport
  15.                     ;; Change the colour override for all matching layers inside the current viewport
  16.                     (command "._VPLAYER" "_Color" 8 match "_Current" "")))
  17.                  ((princ "\nError: ") (princ msg))) ;Else a normal error occured
  18.            (princ))
  19.          (setvar "TILEMODE" 0) ;Ensure you're on a paperspace tab
  20.          (command "._PSPACE") ;Ensure you're NOT inside a viewport
  21.          (command "._Zoom" "_Extents") ;Ensure all viewports are visible
  22.          (command "._MSPACE") ;Go into the last active viewport
  23.          ;; Ask user to do something inside the VP they want. If Esc pressed the *error* routine will fire
  24.          (getpoint "\nPick inside the viewport you want to set.")
  25.          (*error* nil) ;Call the error routine if user picked a point / pressed Space/Enter
  26.          )
  27.         ;; Else if no matching layers found
  28.         ((princ (strcat "\nNo layers matching " match " exists, routine stopped."))))
  29.   (princ))


Thanks a lot irneb! it worked like a charm!
this is far more better than what i have in mind!
"memories fade but the scars still linger"

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Pick a Viewport and Change Xref Layer Color
« Reply #11 on: August 15, 2012, 06:52:35 AM »
Glad it worked. Actually I was just thinking about it - it is possible to use the VPLayer command's Select option. That would make the routine a lot simpler
Code - Auto/Visual Lisp: [Select]
  1. (defun c:OvrCol (/ en match)
  2.   (setq match "*AARCH*") ;Save the wildcard match, since it's used more than once
  3.   (cond ((CheckLayerExists match)
  4.          (setvar "TileMode" 0)
  5.          (command "._PSPACE")
  6.          (if (progn (princ "\nSelect vieports: ")
  7.                (setq en (ssget '((0 . "VIEWPORT")))))
  8.            (command "._VPLAYER" "_Color" 8 match "_Select" en "" "")
  9.            (princ "\nNo viewports selected.")))
  10.         ((princ (strcat "\nNo layers matching " match " exists, routine stopped."))))
  11.   (princ))
Still uses the CheckLayerExists function. But it now allows multiple viewports in one go. You could use an entsel instead of the ssget which would ask for only one single object - but then you'd need to test manually if that object is a viewport (unlike ssget which filters automatically).
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Re: Pick a Viewport and Change Xref Layer Color
« Reply #12 on: August 15, 2012, 08:14:47 AM »
Glad it worked. Actually I was just thinking about it - it is possible to use the VPLayer command's Select option. That would make the routine a lot simpler
Code - Auto/Visual Lisp: [Select]
  1. (defun c:OvrCol (/ en match)
  2.   (setq match "*AARCH*") ;Save the wildcard match, since it's used more than once
  3.   (cond ((CheckLayerExists match)
  4.          (setvar "TileMode" 0)
  5.          (command "._PSPACE")
  6.          (if (progn (princ "\nSelect vieports: ")
  7.                (setq en (ssget '((0 . "VIEWPORT")))))
  8.            (command "._VPLAYER" "_Color" 8 match "_Select" en "" "")
  9.            (princ "\nNo viewports selected.")))
  10.         ((princ (strcat "\nNo layers matching " match " exists, routine stopped."))))
  11.   (princ))
Still uses the CheckLayerExists function. But it now allows multiple viewports in one go. You could use an entsel instead of the ssget which would ask for only one single object - but then you'd need to test manually if that object is a viewport (unlike ssget which filters automatically).

yes that is why i use entsel and then select the viewport...
i am thinking of ssget but cannot find the group code for viewport...so i settle for entsel for the moment..

my original plan was for the user to go to layout, then activate a viewport, select a layer either xrefed / non-xref then asked for the color...

but that is way too difficult for me..

thanks again irned...i will study how you made this lisp so i can learn something from it....thanks truly!
"memories fade but the scars still linger"

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Pick a Viewport and Change Xref Layer Color
« Reply #13 on: August 15, 2012, 09:38:09 AM »
You're absolutely welcome!

I tried to comment as much as possible. Also you'll note some of the keywords used in my post become links to a description of what they do - thanks to The Swamp's awesome code tags.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Re: Pick a Viewport and Change Xref Layer Color
« Reply #14 on: August 15, 2012, 11:03:48 AM »
You're absolutely welcome!

I tried to comment as much as possible. Also you'll note some of the keywords used in my post become links to a description of what they do - thanks to The Swamp's awesome code tags.

one more question inreb...
just thinking if it is possible to add more variable?

say match1 *furn* match2 *stfr*
"memories fade but the scars still linger"

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Pick a Viewport and Change Xref Layer Color
« Reply #15 on: August 15, 2012, 11:43:24 AM »
You can add that into the same variable. It's basically a wcmatch string.

So you could have the following:
Code - Auto/Visual Lisp: [Select]
  1. (setq match "*AARCH*,*furn*,*stfr*")
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Re: Pick a Viewport and Change Xref Layer Color
« Reply #16 on: August 15, 2012, 11:52:00 AM »
You can add that into the same variable. It's basically a wcmatch string.

So you could have the following:
Code - Auto/Visual Lisp: [Select]
  1. (setq match "*AARCH*,*furn*,*stfr*")

okay. thanks for the info and support inreb
been a great help
"memories fade but the scars still linger"