AutoCAD LISP Script Issues

AutoCAD LISP Script Issues

cslibnm
Explorer Explorer
764 Views
5 Replies
Message 1 of 6

AutoCAD LISP Script Issues

cslibnm
Explorer
Explorer
(defun c:FinalHatchR (/ ss i obj vla-obj area oldsnap)
  (setq ss (ssget "X" '((0 . "LWPOLYLINE,POLYLINE,LINE,ARC,CIRCLE,ELLIPSE"))))
  (if ss
    (progn
      (command "_.undo" "_begin")
      (setq i -1)
      (while (setq obj (ssname ss (setq i (1+ i))))
	(setvar "cecolor" "green")
        (princ "\ni = ")
        (princ i)
        (princ "\n")
        (setq vla-obj (vlax-ename->vla-object obj))
        (setq area nil)
        (if (vl-catch-all-error-p (setq area (vl-catch-all-apply 'vla-get-area (list vla-obj))))
          (setq area nil)
        )
        (setq ent (entget (ssname ss i)))
        (setq layer (cdr (assoc 8 ent)))

        (princ "\n")
        (princ layer)
        (princ "\n")

        (if (and area (not (ssget "_X" (list (cons 0 "HATCH") (cons 10 (vlax-curve-getstartpoint vla-obj))))))
          (progn
            (setq oldsnap (getvar "osmode"))
            (setvar "osmode" 0)
	    (cond
  		((wcmatch layer "*LayerYellow*")
   			(setvar "cecolor" "yellow")
  		)
  		((wcmatch layer "*LayerBlue*")
    			(setvar "cecolor" "blue")
  		)
  		((wcmatch layer "*LayerGrey*")
    			(setvar "cecolor" "8")
  		)
	    )
            (if (not (equal "green" (getvar "cecolor")))
              (progn
                (command "_.bpoly" (vlax-curve-getstartpoint vla-obj) "") ; Create a boundary polyline around the enclosed area
                (setq obj (entlast)) ; Get the last created entity (boundary polyline)
                (command "_.hatch" "S" "P" obj "") ; Hatch the enclosed area
                (command "_.erase" obj "") ; Erase the boundary polyline
		(setvar "cecolor" "green")
              )
            )
            (setvar "osmode" oldsnap)
          )
        )
      )
      (command "_.undo" "_end")
      (princ "\nAll enclosed areas have been hatched with color.")
    )
  )
  (princ "\nNo enclosed areas found.")
  (princ)
)

May I ask why when it comes to "LayerGrey", it paints the area as "blue"?

0 Likes
765 Views
5 Replies
Replies (5)
Message 2 of 6

paullimapa
Mentor
Mentor

The following code:

(princ i)

Since i is an integer then the code should be:

(princ (itoa i))

Also for testing you may want to add an alert in each of the conditions just so you know which one is executing:

    (cond

  ((wcmatch layer "*LayerYellow*")

   (setvar "cecolor" "yellow")

(alert “This is yellow”)

  )

  ((wcmatch layer "*LayerBlue*")

    (setvar "cecolor" “blue”)

(alert “This is blue”)

  )

  ((wcmatch layer "*LayerGrey*")

    (setvar "cecolor" "8")

  (alert “This is grey”)

  )

    )


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 6

Kent1Cooper
Consultant
Consultant

@paullimapa wrote:

The following code:

(princ i)

Since i is an integer then the code should be:

(princ (itoa i))

.....


[Not necessary.  If it were the (prompt) function, it would require a text string argument, but (princ) can use a number, either integer or real.]

Kent Cooper, AIA
Message 4 of 6

Kent1Cooper
Consultant
Consultant

Without testing, I wonder....  Could it be that your Layer names are not always spelled in the right case combination?  The (wcmatch) function is case-sensitive, so a Layer name containing [for example] "Layeryellow" [with the lower-case y] will not be recognized.

 

You can get around that possibility by forcing everything to upper-case for the comparison in the (cond) tests:

((wcmatch (strcase layer) "*LAYERYELLOW*")

[or force lower-case if you prefer -- read about the (strcase) function.]

Kent Cooper, AIA
0 Likes
Message 5 of 6

paullimapa
Mentor
Mentor

Thanks for pointing that out. I wasn’t aware of this since I typically use the strcat function to princ combo of both strings & integers. So @cslibnm  following princ lines: 

        (print "\ni = ")
        (princ i)
        (princ "\n")

can be combined as one:

 (princ (strcat "\ni = " (itoa i) "\n"))

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 6

Sea-Haven
Mentor
Mentor

Maybe "green" is not "Green" and (getvar 'cecolor) returns "3".