Author Topic: Proper use of "IF" ??  (Read 365 times)

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Newt
  • Posts: 96
Proper use of "IF" ??
« on: April 15, 2019, 01:47:07 PM »
This works as I've done it, but I would like someone to verify I've done it correctly.

Using CAB's great explanation found here...
http://www.theswamp.org/index.php?topic=13046.msg158557#msg158557

I chose to use "IF"
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (if (= 1 (getvar 'tilemode))
  3.     < user is in paperspace >
  4.     < user is in modelspace >
  5. )

For this piece of code from Michael Puckett. Starting at line 46 is where I have the "IF" condition.

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun mpx-get-bounding-box ( object / a b )
  3.     (vl-catch-all-apply 'vlax-invoke-method (list object 'GetBoundingBox 'a 'b))
  4.     (if a (mapcar 'vlax-safearray->list (list a b)))
  5. )
  6.  
  7. (defun mpx-restrict-attrib-widths ( block-reference tag-spec max-width / bb dx ratio flag )
  8.     (and
  9.         (eq 'vla-object (type block-reference))
  10.         (eq "AcDbBlockReference" (vla-get-objectname block-reference))
  11.         (eq :vlax-true (vla-get-hasattributes block-reference))
  12.         (setq tag-spec (strcase (if (eq 'str (type tag-spec)) tag-spec "*")))
  13.         (foreach a (vlax-invoke block-reference 'GetAttributes)
  14.             (and
  15.                 (wcmatch (vla-get-tagstring a) tag-spec)
  16.                 (setq bb (mpx-get-bounding-box a))
  17.                 (setq dx (abs (apply '- (mapcar 'car bb))))
  18.                 (< 1.0 (setq ratio (/ dx max-width)))
  19.                 (setq flag t)
  20.                 (vla-put-scalefactor a (* (vla-get-scalefactor a) (/ 1.0 ratio)))
  21.             )
  22.        
  23.         )
  24.         (if flag (vla-update block-reference))
  25.     )
  26.     (princ)
  27. )
  28.  
  29. (defun test ( / block-spec tag-spec max-width ss i )
  30.  
  31.    (setq temperr *error*)
  32.    (setq *errer* trap1)
  33.    (setq oldecho (getvar "cmdecho"))
  34.    (setvar "cmdecho" 0)
  35.    (princ)
  36.  
  37.         block-spec "TBLK_ATT_CTL"
  38.         max-width  (* 5.25 (getvar 'dimscale));;multiply 5.25 dimscale
  39.         )
  40.    (setq tag-spec (entsel "\nselect TitleLine: ")  ;;removed tag-spec "TitleLine#"
  41.  
  42.         )
  43.    
  44.      (if
  45.        (= 1 (getvar 'tilemode))
  46.        (progn
  47.        (setq ss (ssget "x" (list '(0 . "insert")'(66 . 1)(cons 2 block-spec))))
  48.         (repeat (setq i (sslength ss))
  49.             (mpx-restrict-attrib-widths
  50.                 (vlax-ename->vla-object (ssname ss (setq i (1- i))))
  51.                 tag-spec
  52.                 max-width
  53.             )
  54.         )
  55.      )
  56.        (progn
  57.        (setq ss (ssget "x" (list '(0 . "insert")'(66 . 1)(cons 2 block-spec))))
  58.         (repeat (setq i (sslength ss))
  59.             (mpx-restrict-attrib-widths
  60.                 (vlax-ename->vla-object (ssname ss (setq i (1- i))))
  61.                 tag-spec
  62.                 max-width
  63.             )
  64.         )
  65.      )
  66.   )
  67.     (setvar "cmdecho" oldecho)
  68.     (princ)
  69.    
  70. )
  71.  
  72. (defun trap1 ( / errmsg)
  73.     (command "u" "b")
  74.     (setvar "cmdecho" oldecho)
  75.     (setq *error* temperr)
  76.     (prompt "\nResetting System Variables ")
  77.   (princ)
  78. )
  79. (test)
  80.  


Like I said all seems to work fine, just want to make sure I'm using "IF" or the correct conditional for the job.

This will be used on two of the same title blocks that are in different drawings. One uses Paperspace while the other uses Modelspace. So, dependent on which title block the user is in, make the adjustments to the titleline tag widths as required by the standard.

Thanks for any help
J.


J. Logan
ACAD 2018

I am one with the Force and the Force is with me.

ribarm

  • Water Moccasin
  • Posts: 2126
  • Marko Ribar, architect
Re: Proper use of "IF" ??
« Reply #1 on: April 15, 2019, 02:23:57 PM »
I wouldn't use (if) this time... If blocks are in Paper Space or Model Space, DXF group code 410 determines correct choice of selected entities that reside active current layout specification (space)... So something like this is adequate for all kind of active space situations :

Quote
        (setq ss (ssget "x" (list '(0 . "insert") '(66 . 1) (cons 2 block-spec) (cons 410 (getvar 'ctab)))))
        (repeat (setq i (sslength ss))
            (mpx-restrict-attrib-widths
                (vlax-ename->vla-object (ssname ss (setq i (1- i))))
                tag-spec
                max-width
            )
        )
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12265
  • London, England
Re: Proper use of "IF" ??
« Reply #2 on: April 15, 2019, 05:01:38 PM »
Since you appear to be performing the same operation on either branch of the if statement, the statement is redundant:
Code - Auto/Visual Lisp: [Select]
  1. (if (= 1 (getvar 'tilemode))
  2.     (progn
  3.         (setq ss (ssget "x" (list '(0 . "insert")'(66 . 1)(cons 2 block-spec))))
  4.         (repeat (setq i (sslength ss))
  5.             (mpx-restrict-attrib-widths
  6.                 (vlax-ename->vla-object (ssname ss (setq i (1- i))))
  7.                 tag-spec
  8.                 max-width
  9.             )
  10.         )
  11.     )
  12.     (progn
  13.         (setq ss (ssget "x" (list '(0 . "insert")'(66 . 1)(cons 2 block-spec))))
  14.         (repeat (setq i (sslength ss))
  15.             (mpx-restrict-attrib-widths
  16.                 (vlax-ename->vla-object (ssname ss (setq i (1- i))))
  17.                 tag-spec
  18.                 max-width
  19.             )
  20.         )
  21.     )
  22. )

The above could be replaced with either one of the arguments for the if function and would perform identically:
Code - Auto/Visual Lisp: [Select]
  1.         (setq ss (ssget "x" (list '(0 . "insert")'(66 . 1)(cons 2 block-spec))))
  2.         (repeat (setq i (sslength ss))
  3.             (mpx-restrict-attrib-widths
  4.                 (vlax-ename->vla-object (ssname ss (setq i (1- i))))
  5.                 tag-spec
  6.                 max-width
  7.             )
  8.         )

jlogan02

  • Newt
  • Posts: 96
Re: Proper use of "IF" ??
« Reply #3 on: April 15, 2019, 05:19:12 PM »
Yes Lee, that is one of the reasons I asked. I couldn't see that I was doing anything special between the two branches but couldn't get it to work with just the simple line. Which was the original code Michael Puckett gave me.

I was receiving the

Error: divide by zero message

I believe from this line

Code - Auto/Visual Lisp: [Select]
  1. max-width  (* 5.25 (getvar 'dimscale));;multiply 5.25 dimscale

I added
max-width1 5.25 here


(setq
        block-spec "TBLK_ATT_CTL"
        max-width (* 5.25 (getvar 'dimscale));;multiply 5.25 dimscale
        max-width1 5.25

   )
   (setq tag-spec (entsel "\nselect TitleLine: ")  ;;removed tag-spec "TitleLine#"
 
   )
 
  (if
    (= 1 (getvar 'tilemode))
      (progn
       (setq ss (ssget "x" (list '(0 . "insert")'(66 . 1)(cons 2 block-spec))))
        (repeat (setq i (sslength ss))
            (mpx-restrict-attrib-widths
                (vlax-ename->vla-object (ssname ss (setq i (1- i))))
                tag-spec
                max-width
            )
        )
     )
       (progn
       (setq ss (ssget "x" (list '(0 . "insert")'(66 . 1)(cons 2 block-spec))))
        (repeat (setq i (sslength ss))
            (mpx-restrict-attrib-widths
                (vlax-ename->vla-object (ssname ss (setq i (1- i))))
                tag-spec
                max-width1

            )
        )
     )
  )
    (setvar "cmdecho" oldecho)
    (princ)
 
)

Ribarm:

I started out with your suggestion and it worked fine under certain circumstances. As I mention above, I kept getting the Divide by Zero error with your suggestion too. I reverted back to my "IF" statement approach and was able to get it to work in a real world application for both title blocks.

I'm sure there's a method for addressing the "divide by zero" using your method, but it is out of my brain's reach.

Lee:
I don't know if that was the correct approach add the extra setq "max width1" but it seemed to work appropriately in both title blocks.

As usual if you or anyone has any other suggestions, I'm listening.
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.

Lee Mac

  • Seagull
  • Posts: 12265
  • London, England
Re: Proper use of "IF" ??
« Reply #4 on: April 15, 2019, 05:44:43 PM »
I would suggest the following code:
Code - Auto/Visual Lisp: [Select]
  1. (defun mpx-get-bounding-box ( object / a b )
  2.     (vl-catch-all-apply 'vlax-invoke-method (list object 'GetBoundingBox 'a 'b))
  3.     (if a (mapcar 'vlax-safearray->list (list a b)))
  4. )
  5. (defun mpx-restrict-attrib-widths ( block-reference tag-spec max-width / bb dx ratio flag )
  6.     (and
  7.         (eq 'vla-object (type block-reference))
  8.         (eq "AcDbBlockReference" (vla-get-objectname block-reference))
  9.         (eq :vlax-true (vla-get-hasattributes block-reference))
  10.         (setq tag-spec (strcase (if (eq 'str (type tag-spec)) tag-spec "*")))
  11.         (foreach a (vlax-invoke block-reference 'GetAttributes)
  12.             (and
  13.                 (wcmatch (vla-get-tagstring a) tag-spec)
  14.                 (setq bb (mpx-get-bounding-box a))
  15.                 (setq dx (abs (apply '- (mapcar 'car bb))))
  16.                 (< 1.0 (setq ratio (/ dx max-width)))
  17.                 (setq flag t)
  18.                 (vla-put-scalefactor a (* (vla-get-scalefactor a) (/ 1.0 ratio)))
  19.             )
  20.         )
  21.         (if flag (vla-update block-reference))
  22.     )
  23.     (princ)
  24. )
  25. (defun test ( / ent idx sel tag wid )
  26.     (setq wid 5.25)
  27.     (if (< 0 (getvar 'dimscale))
  28.         (setq wid (* wid (getvar 'dimscale)))
  29.     )
  30.     (if (and (setq ent (car (nentsel "\nSelect titleline <skip>: ")))
  31.              (= "ATTRIB" (cdr (assoc 0 (entget ent))))
  32.         )
  33.         (setq tag (cdr (assoc 2 (entget ent))))
  34.     )
  35.     (if (setq sel (ssget "_X" '((0 . "INSERT") (66 . 1) (2 . "TBLK_ATT_CTL"))))
  36.         (repeat (setq idx (sslength sel))
  37.             (mpx-restrict-attrib-widths (vlax-ename->vla-object (ssname sel (setq idx (1- idx)))) tag wid)
  38.         )
  39.     )
  40.     (princ)
  41. )

The above is untested but should hopefully perform as required.

jlogan02

  • Newt
  • Posts: 96
Re: Proper use of "IF" ??
« Reply #5 on: April 15, 2019, 06:46:14 PM »
See!!! Again, overly simple. So to be sure I understand...

Code - Auto/Visual Lisp: [Select]
  1. (setq wid 5.25)
  2.     (if (< 0 (getvar 'dimscale))
  3.         (setq wid (* wid (getvar 'dimscale)))
  4.     )
  5.  

You've setq "wid" to 5.25...
then saying "IF" dimscale is greater than 0 multiply "wid" times the current dimscale.

thanks again.



 
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.

Lee Mac

  • Seagull
  • Posts: 12265
  • London, England
Re: Proper use of "IF" ??
« Reply #6 on: April 16, 2019, 08:16:29 AM »
So to be sure I understand...
Code - Auto/Visual Lisp: [Select]
  1. (setq wid 5.25)
  2.     (if (< 0 (getvar 'dimscale))
  3.         (setq wid (* wid (getvar 'dimscale)))
  4.     )
  5.  
You've setq "wid" to 5.25...
then saying "IF" dimscale is greater than 0 multiply "wid" times the current dimscale.

Exactly.  :-)

I've also fixed the titleline selection, since, in your original code, you were supplying this as a list to the mpx-restrict-attrib-widths function and so this would never have had an effect on which attributes were modified by the function, regardless of the user selection.