TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: jlogan02 on November 16, 2018, 05:33:47 PM

Title: If statement. Layer gets made but color won't set.
Post by: jlogan02 on November 16, 2018, 05:33:47 PM
I can't figure out why in the second "progn" the layer will get made, but it's color won't get set to 136.


Code - Auto/Visual Lisp: [Select]
  1.  
  2. (if (tblsearch "LAYER" "FiberMgt_136") ;Check if the layer exists
  3.    (setq fm136 (ssget "x" '((8 . "FiberMgt_56"))))
  4.    (command "_chprop" fm136 "" "p" "la" "FiberMgt_136" "" (command)(command))
  5. ) ;End if progn?
  6.    (setq fm136 (ssget "x" '((8 . "FiberMgt_56"))))
  7.    (command "._Layer" "Make" "FiberMgt_136" "_Color" "136" "" "" (command)(command));;[b]Color won't set here. The layer gets made and objects get moved to the layer. But the color is White:[/i]
  8.    (command "_chprop" fm136 "" "p" "la" "Fibermgt_136" "" (command)(command))
  9.  ) ;End if progn
  10.  ) ;End of if
  11.  
  12.     (setvar 'osmode osm)
  13.     (princ)
  14. )

J. Logan
ACAD 218
Title: Re: If statement. Layer gets made but color won't set.
Post by: jlogan02 on November 16, 2018, 05:55:45 PM
Edit: All of this works fine but my approach is wonky. The FiberMgt layers will only exist on a building equipment plan. There's another portion of the code (not shown) which deals with changing objects from their bylayer color to a construction/rev color. Those changes occur on the building equipment plan and a multitude of schematics, wiring diagrams and etc. As it stands now the code below will create the FiberMgt layer on every type of drawing if the layer doesn't exist.

I don't want that. I want it to do everything it's doing just not on every type of drawing.
Title Block is attributed - Could I...

(if (attribute "titleline4" reads Building Plan
(do the deed

or

(if (block FM insert exists
(do the deed

or is there something more I can add to what I have?


End Edit:

Never mind

Using Chprop doesn't require the "P" in it. Removed it and all is fine.

Code - Auto/Visual Lisp: [Select]
  1. (if (tblsearch "LAYER" "FiberMgt_136") ;Check if the layer exists
  2.    (setq fm136 (ssget "x" '((8 . "FiberMgt_56"))))
  3.    (command "_chprop" fm136 "" "la" "FiberMgt_136" "" (command)(command))[i]Removed "P" here[/i]
  4. ) ;End if progn?
  5.    (setq fm136 (ssget "x" '((8 . "FiberMgt_56"))))
  6.    (command "._Layer" "Make" "FiberMgt_136" "_Color" "136" "" "" (command)(command))
  7.    (command "_chprop" fm136 "" "la" "FiberMgt_136" "" (command)(command))[i]Removed "P" here[/i]
  8.  ) ;End if progn
  9.  ) ;End of if
Title: Re: If statement. Layer gets made but color won't set.
Post by: kdub_nz on November 16, 2018, 06:55:47 PM
Does this work for you.

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (setq OldLayerName "AAC"
  3.       NewLayerName "ABCD"
  4.       LayerColor   "136"
  5.       fm136        (ssget "_X" (list (cons 8 OldLayerName)))
  6. )
  7.  
  8. (if (not (= (atoi LayerColor)
  9.             (cdr (assoc 62 (tblsearch "LAYER" NewLayerName)))
  10.          )
  11.     )
  12.   (command "._Layer" "_Make" NewLayerName "_Color" LayerColor "" "")
  13. )
  14. (if fm136
  15.   (command "_chprop" fm136 "" "_la" NewLayerName "")
  16. )
  17.  
Title: Re: If statement. Layer gets made but color won't set.
Post by: jlogan02 on November 19, 2018, 05:14:36 PM
That worked find Kdub.

Using this code gets me the value of the tag "SUBJECTCODE" in the attribute "TBLK_ATT_CTL"

Code - Auto/Visual Lisp: [Select]
  1.  
  2.  
  3. (defun c:FOO (/ LM:GetAttributeValue ss attValue)
  4.  
  5.   (defun LM:GetAttributeValue (blk tag / val enx)
  6.     (while
  7.       (and
  8.         (null val)
  9.         (= "ATTRIB"
  10.            (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk))))))
  11.         )
  12.       )
  13.        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
  14.          (setq val (cdr (assoc 1 enx)))
  15.        )
  16.     )
  17.   )
  18.  
  19.   (setq ss (ssget "x" '((0 . "INSERT") (2 . "TBLK_ATT_CTL") (66 . 1))))
  20.   (setq attValue (LM:GetAttributeValue (ssname ss 0) "SUBJECTCODE"))
  21.  
  22. (if (ATTRIBUTE TAG VALUE = 90????
  23.    (setq fm136 (ssget "x" '((8 . "FiberMgt_56"))))
  24.    (command "_chprop" fm136 "" "la" "FiberMgt_136" "" (command)(command))
  25. ) ;End if progn?
  26.    (setq fm136 (ssget "x" '((8 . "FiberMgt_56"))))
  27.    (command "._Layer" "Make" "FiberMgt_136" "_Color" "136" "" "" (command)(command))
  28.    (command "_chprop" fm136 "" "la" "FiberMgt_136" "" (command)(command))
  29.  ) ;End if progn
  30.  ) ;End of if  
  31.   (princ)
  32. )
  33. )

Here's where I'm not having any luck. Seems to me, once I verify the attribute value is "90" I should be able to..

(if (subject code is "90"...
do stuff...

If (subject code isn't "90"...
Don't do stuff..

but I'm stuck.

J. Logan
ACAD2018
Title: Re: If statement. Layer gets made but color won't set.
Post by: Lee Mac on November 19, 2018, 05:38:09 PM
Code: [Select]
(if (= attValue "90")
Or:

Code: [Select]
(if (= (atoi attValue) 90)
Title: Re: If statement. Layer gets made but color won't set.
Post by: kdub_nz on November 19, 2018, 05:42:10 PM

and :
are you certain the value will represent an integer ??
Title: Re: If statement. Layer gets made but color won't set.
Post by: jlogan02 on November 19, 2018, 06:33:08 PM
Lee, I swear to god I tried that!!! Going to try again.

And yes Kdub, it always will. Now it could represent many different integer's, but I'm looking specifically for "90".

J. Logan
ACAD 2018
Title: Re: If statement. Layer gets made but color won't set.
Post by: jlogan02 on November 19, 2018, 07:25:30 PM
Works, but doesn't work...

It will do as expected but doesn't honor the (if (= attValue 90)
It skips that line of code.

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun c:FOO (/ LM:GetAttributeValue ss attValue)
  3.  
  4.   (defun LM:GetAttributeValue (blk tag / val enx)
  5.     (while
  6.       (and
  7.         (null val)
  8.         (= "ATTRIB"
  9.            (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk))))))
  10.         )
  11.       )
  12.        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
  13.          (setq val (cdr (assoc 1 enx)))
  14.        )
  15.     )
  16.   )
  17.  
  18.   (setq cl56 (ssget "x" '((62 . 56)(0 . "LINE,POLYLINE,LWPOLYLINE,TEXT,MTEXT"))))
  19.     (command "_chprop" cl56 "" "C" "136" "" (command)(command))
  20.  
  21.   (setq ss (ssget "x" '((0 . "INSERT") (2 . "TBLK_ATT_CTL") (66 . 1))))
  22.   (setq attValue (LM:GetAttributeValue (ssname ss 0) "SUBJECTCODE")) ?????DO I have this line right ????
  23.  
  24. (if (= attValue 90)
  25.    (setq fm136 (ssget "x" '((8 . "FiberMgt_56"))))
  26.    (command "._Layer" "Make" "FiberMgt_136" "_Color" "136" "FiberMgt_136" "" (command)(command))
  27.    (command "_chprop" fm136 "" "P" "LA" "FiberMgt_136" "" (command)(command))
  28. ) ;End if progn?
  29.    (setq fm136 (ssget "x" '((8 . "FiberMgt_56"))))
  30.    (command "._Layer" "Make" "FiberMgt_136" "_Color" "136" "FiberMgt_136" "" (command)(command))
  31.    (command "_chprop" fm136 "" "P" "LA" "FiberMgt_136" "" (command)(command))
  32.  ) ;End if progn
  33.  ) ;End of if  
  34.   (princ)
  35. )

Edit Below:

I also get this message from the <Build  Output> window when I "Check TEXT in Editor"

[CHECKING TEXT FM136.lsp loading...]
..
; warning: local variable used as function: LM:GETATTRIBUTEVALUE
; Check done.

J. Logan
ACAD 2018
Title: Re: If statement. Layer gets made but color won't set.
Post by: Lee Mac on November 20, 2018, 08:14:31 AM
It will do as expected but doesn't honor the (if (= attValue 90)
It skips that line of code.

The variable attValue will always be a string (or nil), therefore the test should be either:

Code: [Select]
(= attValue "90")or:
Code: [Select]
(= (atoi attValue) 90)
Per my earlier response. (https://www.theswamp.org/index.php?topic=54675.msg591516#msg591516)

I also get this message from the <Build  Output> window when I "Check TEXT in Editor"

[CHECKING TEXT FM136.lsp loading...]
..
; warning: local variable used as function: LM:GETATTRIBUTEVALUE
; Check done.

This is only a warning and will not affect the code operation, but the warning appears because you have declared the LM:GetAttributeValue as a symbol local to c:FOO. You can safely remove LM:GetAttributeValue from the list of local variables, as there is no need for this function to be local unless you have another (different) definition of the same function in the document namespace (which I highly doubt).

i.e.:
Code: [Select]
(/ LM:GetAttributeValue ss attValue)
Can become:
Code: [Select]
(/ cl56 fm136 ss attValue)
(I've added your other variables)
Title: Re: If statement. Layer gets made but color won't set.
Post by: jlogan02 on November 20, 2018, 12:11:50 PM
Thanks for the help Lee. I'm just too old for this shite.

I changed...

Code - Auto/Visual Lisp: [Select]
  1. (setq attValue (LM:GetAttributeValue (ssname ss 0) "SUBJECTCODE"))

to

Code - Auto/Visual Lisp: [Select]
  1. (setq attValue (LM:GetAttributeValue (ssname ss 0) "TITLELINE4"))

and went with...

Code - Auto/Visual Lisp: [Select]
  1. (if (= attValue "BUILDING EQUIPMENT PLAN")

all works fine. the TitleLine4 and SubjectCode tags are the same thing. One can't be different from the other. All works fine this way.

J. Logan
ACAD2018
Title: Re: If statement. Layer gets made but color won't set.
Post by: Lee Mac on November 20, 2018, 12:26:19 PM
Good stuff  :-)
Title: Re: If statement. Layer gets made but color won't set.
Post by: jlogan02 on November 21, 2018, 03:50:05 PM
Damn!!! Discovered I need to add another attValue...

I needed to add Interior Elevations.

Code - Auto/Visual Lisp: [Select]
  1.     (setq attValue (LM:GetAttributeValue (ssname ss 0) "TITLELINE4"))
  2.  
  3.   (if (= attValue "EQUIPMENT PLAN")
  4.    (progn
  5.     (setq fm1 (ssget "x" '((8 . "FiberMgt_1"))))
  6.      (command "._Layer" "Make" "FiberMgt_56" "_Color" "56" "FiberMgt_56" "" (command)(command))
  7.      (command "_chprop" fm1 "" "P" "LA" "FiberMgt_56" "" (command)(command))
  8.     );End if progn  
   
So I added a second
Code - Auto/Visual Lisp: [Select]
Would that be the correct method? It seems to work. EDIT: No it doesn't

Code - Auto/Visual Lisp: [Select]
  1.       (= attValue "INTERIOR ELEVATIONS")
  2.       (setq fm1 (ssget "x" '((8 . "FiberMgt_1"))))
  3.      (command "._Layer" "Make" "FiberMgt_56" "_Color" "56" "FiberMgt_56" "" (command)(command))
  4.      (command "_chprop" fm1 "" "P" "LA" "FiberMgt_56" "" (command)(command))
  5.     ) ;End if progn
  6.    ) ;End of if

J. Logan
ACAD2018
Title: Re: If statement. Layer gets made but color won't set.
Post by: Lee Mac on November 21, 2018, 05:14:45 PM
With multiple conditions, the cond function becomes more applicable, e.g.:
Code - Auto/Visual Lisp: [Select]
  1.     (   (= attValue "EQUIPMENT PLAN")
  2.         (setq fm1 (ssget "_x" '((8 . "FiberMgt_1"))))
  3.         ;; ... etc.
  4.     )
  5.     (   (= attValue "INTERIOR ELEVATIONS")
  6.         (setq fm1 (ssget "x" '((8 . "FiberMgt_1"))))
  7.         ;; ... etc.
  8.     )
  9. )
Title: Re: If statement. Layer gets made but color won't set.
Post by: jlogan02 on November 26, 2018, 03:05:07 PM
Perfect. Thanks Lee.
Title: Re: If statement. Layer gets made but color won't set.
Post by: ronjonp on November 26, 2018, 04:01:07 PM
Since it appears you are changing the same properties for both conditions you could use wcmatch as well:
Code - Auto/Visual Lisp: [Select]
  1. (cond ((wcmatch (strcase attvalue) "EQUIPMENT PLAN,INTERIOR ELEVATIONS")
  2.        (setq fm1 (ssget "x" '((8 . "FiberMgt_1"))))
  3.        (command "._Layer" "Make" "FiberMgt_56" "_Color" "56" "FiberMgt_56" "" (command) (command))
  4.        (command "_chprop" fm1 "" "P" "LA" "FiberMgt_56" "" (command) (command))
  5.       )                                 ;End if prog
  6. )
Title: Re: If statement. Layer gets made but color won't set.
Post by: jlogan02 on November 27, 2018, 04:50:05 PM
So here's my result with your suggestion for using wcmatch. Worked nicely and shortened the routine. Like it!

Code - Auto/Visual Lisp: [Select]
  1. (defun c:ST136to191 (/ *error* osm cl136 fm136 ss attValue)
  2.   (defun *error* ( msg )
  3.         (if osm (setvar 'osmode osm))
  4.         (if (not (member msg '("Function cancelled" "quit / exit abort")))
  5.             (princ (strcat "\nError: " msg))
  6.         )
  7.         (princ)
  8.     )
  9.  
  10.     (setq osm (getvar 'osmode))
  11.     (setvar 'osmode 0)
  12.     (setvar 'cmdecho 0)
  13.   (defun LM:GetAttributeValue (blk tag / val enx)
  14.     (while
  15.       (and
  16.         (null val)
  17.         (= "ATTRIB"
  18.            (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk))))))
  19.         )
  20.       )
  21.        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
  22.          (setq val (cdr (assoc 1 enx)))
  23.        )
  24.     )
  25.   )
  26.  
  27.    (setq cl136 (ssget "x" '((62 . 136)(0 . "LINE,POLYLINE,LWPOLYLINE,TEXT,MTEXT"))))
  28.      (command "_chprop" cl136 "" "_COLOR" "191" (command)(command))
  29.  
  30.     (setq ss (ssget "x" '((0 . "INSERT") (2 . "TBLK_ATT_CTL")(66 . 1))))
  31.     (setq attValue (LM:GetAttributeValue (ssname ss 0) "TITLELINE4"))
  32.  
  33.  (cond ((wcmatch (strcase attvalue) "EQUIPMENT PLAN,INTERIOR ELEVATIONS")
  34.        (setq fm136 (ssget "x" '((8 . "FiberMgt_136"))))
  35.         (command "._Layer" "Make" "FiberMgt_191" "_Color" "191" "FiberMgt_191" "Make" "FiberMgt_1" "Color" "RED" (command)(command))
  36.         (command "_chprop" fm136 "" "LA" "FiberMgt_191" "" (command)(command))
  37.         (command "._layer" "filter" "edit" "Stations Standard Layers" "add" "FiberMgt_191" "exit" (command)(command))
  38.   )
  39. )
  40.  (rtos (getreal "\nPress Esc to exit, press Enter to force an error ..."))
  41.  
  42.     (setvar 'osmode osm)
  43.   (setvar 'cmdecho 1)  
  44.  
  45.   (princ)
  46. )

I get...

Command: ST136TO191
Unknown command "ST136TO191".  Press F1 for help.

I've looked for spaces and missing spaces. Just can't find the issue.

J. Logan
ACAD2019
Title: Re: If statement. Layer gets made but color won't set.
Post by: ronjonp on November 27, 2018, 05:03:52 PM
First thing that looks strange is all the (command)(command) calls?
 Maybe "._layer" should be ".-layer" ?
Title: Re: If statement. Layer gets made but color won't set.
Post by: ronjonp on November 27, 2018, 05:22:09 PM
Here's a quick ( untested ) stab at removing some of the command calls:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:st136to191 (/ *error* osm cl136 fm136 ss attvalue)
  2.   (defun _addlayer (name color ltype plot)
  3.     (cond ((not (tblsearch "layer" name))
  4.            (entmakex (list '(0 . "LAYER")
  5.                            '(100 . "AcDbSymbolTableRecord")
  6.                            '(100 . "AcDbLayerTableRecord")
  7.                            '(70 . 0)
  8.                            (cons 2 name)
  9.                            (cons 62 color)
  10.                            (cons 6
  11.                                  (cond ((tblobjname "ltype" ltype))
  12.                                        ("continuous")
  13.                                  )
  14.                            )
  15.                            (cons 290 plot)
  16.                            ;;1 = plottable 0 = not=plottable
  17.                      )
  18.            )
  19.           )
  20.           ((tblobjname "layer" name))
  21.     )
  22.   )
  23.   (defun *error* (msg)
  24.     (if osm
  25.       (setvar 'osmode osm)
  26.     )
  27.     (if (not (member msg '("Function cancelled" "quit / exit abort")))
  28.       (princ (strcat "\nError: " msg))
  29.     )
  30.     (princ)
  31.   )
  32.   (setq osm (getvar 'osmode))
  33.   (setvar 'osmode 0)
  34.   (setvar 'cmdecho 0)
  35.   (defun lm:getattributevalue (blk tag / val enx)
  36.     (while
  37.       (and (null val) (= "ATTRIB" (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk))))))))
  38.        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
  39.          (setq val (cdr (assoc 1 enx)))
  40.        )
  41.     )
  42.   )
  43.   (if (setq cl136 (ssget "x" '((62 . 136) (0 . "LINE,POLYLINE,LWPOLYLINE,TEXT,MTEXT"))))
  44.     (foreach x (mapcar 'cadr (ssnamex cl136)) (entmod (append (entget x) '((62 . 191)))))
  45.   )
  46.   (setq ss (ssget "x" '((0 . "INSERT") (2 . "TBLK_ATT_CTL") (66 . 1))))
  47.   (setq attvalue (lm:getattributevalue (ssname ss 0) "TITLELINE4"))
  48.   (cond ((wcmatch (strcase attvalue) "EQUIPMENT PLAN,INTERIOR ELEVATIONS")
  49.          (_addlayer "FiberMgt_191" 191 "Continous" 1)
  50.          (_addlayer "FiberMgt_1" 1 "Continous" 1)
  51.          (if (setq fm136 (ssget "x" '((8 . "FiberMgt_136"))))
  52.            (foreach x (mapcar 'cadr (ssnamex fm136))
  53.              (entmod (append (entget x) '((8 . "FiberMgt_191"))))
  54.            )
  55.          )
  56.          ;; Changed to -layer
  57.          (command ".-layer"
  58.                   "filter"
  59.                   "edit"
  60.                   "Stations Standard Layers"
  61.                   "add"
  62.                   "FiberMgt_191"
  63.                   "exit"
  64.                   (command)
  65.                   (command)
  66.          )
  67.         )
  68.   )
  69.   ;; Why this?
  70.   (rtos (getreal "\nPress Esc to exit, press Enter to force an error ..."))
  71.   (setvar 'osmode osm)
  72.   (setvar 'cmdecho 1)
  73.   (princ)
  74. )
Title: Re: If statement. Layer gets made but color won't set.
Post by: jlogan02 on November 29, 2018, 12:10:57 PM
Sorry Ron, I didn't see this post. I had the command calls in there because it seemed that was the only way I could avoid getting "Unknown command..." errors at the command line.

I've been assuming I'm getting those because I'm ending the Change properties commands or the layers commands wrong/ungracefully. that was my fix. Perhaps I'm mistaken. I don't know how the message at the end got left in there. I did have error checking in it but removed it, well not all of it, while testing.

J. Logan
ACAD 2018