Get value in Drawing Properties

Get value in Drawing Properties

Browning_Zed
Advocate Advocate
2,138 Views
8 Replies
Message 1 of 9

Get value in Drawing Properties

Browning_Zed
Advocate
Advocate

Hi,

I need code that, depending on the existing value in Drawing Properties, would load certain lisp files. And if the required value is missing, it should be written in Custom Info. I'm newbie to Autolisp, and I don’t know how to make the if / else statements correctly, also, I don’t know how to check if an entry exists in Custom Info. I tried to make the code, as I see it, but how to make it work?

 

(defun c:LoadScale (/ App Doc DwgProps)

(setq	App (vlax-Get-Acad-Object)
		Doc (vla-Get-ActiveDocument App)
		DwgProps (vla-Get-SummaryInfo Doc)
	)

; If one of the values exists in custom info, then lsp file must be loaded:
(if (CustomInfo "Scale" "1:100") load "100.lsp")
(if (CustomInfo "Scale" "1:200") load "200.lsp")
(if (CustomInfo "Scale" "1:500") load "500.lsp")

; Otherwise, write the value and load lsp file:
(vla-AddCustomInfo DwgProps "Scale" "1:500")
(load "500.lsp")

(princ)
)

 

 

0 Likes
Accepted solutions (1)
2,139 Views
8 Replies
Replies (8)
Message 2 of 9

CodeDing
Advisor
Advisor

@Browning_Zed ,

 

Here are 2 useful functions that I have added to a utility lisp that I load with EVERY drawing. That way I can use them in situations like yours. I have also written how I believe your command would look. Hope this helps!

(defun c:LoadScale ( / scale lsp)
(setq scale (GetCustomDwgProp "Scale"))
(setq lsp
  (cond
    ((eq scale "1:100") "100.lsp")
    ((eq scale "1:200") "200.lsp")
    (t (SetCustomDwgProp "Scale" "1:500")
       "500.lsp"
    )
  );cond
);setq
(load lsp)
(princ)
);defun

(defun GetCustomDwgProp (key / docProps return num keyVal propVal)
;key - string, representing key value of custom dwgprop to search for
;returns string of dwgprop value if found, or nil
(setq docProps (vla-get-SummaryInfo (vla-get-ActiveDocument (vlax-get-acad-object))))
(setq return nil num 0)
(if (and (>= (setq total (vla-NumCustomInfo docProps)) 1)
	 (eq 'STR (type key)))
  (while (< num total)
    (vla-GetCustomByIndex docProps num 'keyVal 'propVal)
    (if (eq (strcase key) (strcase keyVal)) (setq return propVal))
    (setq num (1+ num))
  );while
);if
return
);defun

(defun SetCustomDwgProp (key val / docProps return num keyVal propVal)
;key - string, representing key value of custom dwgprop to search for
;val - string, representing custom value to be used for the "key" custom dwgprop
;returns t if successful, or nil
(setq docProps (vla-get-SummaryInfo (vla-get-ActiveDocument (vlax-get-acad-object))))
(setq return nil num 0)
(if (and (>= (setq total (vla-NumCustomInfo docProps)) 1)
	 (eq 'STR (type key))
	 (eq 'STR (type val)))
  (while (< num total)
    (vla-GetCustomByIndex docProps num 'keyVal 'propVal)
    (if (eq (strcase key) (strcase keyVal))
      (progn
	(vla-SetCustomByIndex docProps num keyVal val)
	(setq return t)
      );progn
    );if
    (setq num (1+ num))
  );while
);if
return
);defun

Best,

~DD

0 Likes
Message 3 of 9

Browning_Zed
Advocate
Advocate

Thanks, this works if there is a value in Custom Info. But how to check - if there is no entry "Scale" "" in Custom Info, need to write it, and then load the lsp file? Sort of:
(vla-AddCustomInfo "Scale" "1: 500")
(load "500.lsp")

The second function in your code does not work for me.

0 Likes
Message 4 of 9

CodeDing
Advisor
Advisor

@Browning_Zed ,

 

Can you provide further explanation?

The command + 2 functions I provided to you should accomplish exactly what you're asking about.

I do not understand where it fails for you.

 

Best,

~DD

0 Likes
Message 5 of 9

Browning_Zed
Advocate
Advocate

The first function: GetCustomDwgProp - works fine (if the value exists, lsp file is loaded)
The second function: SetCustomDwgProp - only works if there is a "Scale" entry, in this case, the value will be set to "1:500". But if the Custom Info is empty (there is no "Scale" entry), then the code will not work. Need a check: if "Scale" is missing, then need create "Scale", and set it to value " 1: 500 ".

0 Likes
Message 6 of 9

CodeDing
Advisor
Advisor
Accepted solution

@Browning_Zed ,

 

Ah, ok. I think I understand now. I updated the command. Is this what you're looking for?

EDIT: Updated code.

 

 

(defun c:LoadScale ( / scale lsp docProps)
(setq docProps (vla-get-SummaryInfo (vla-get-ActiveDocument (vlax-get-acad-object))))
(setq scale (GetCustomDwgProp "Scale"))
(setq lsp
  (cond
    ((eq scale "1:100") "100.lsp")
    ((eq scale "1:200") "200.lsp")
    (t
      (if scale
        (SetCustomDwgProp "Scale" "1:500")
        (vla-AddCustomInfo docProps "Scale" "1:500")
      );if
      "500.lsp"
    )
  );cond
);setq
(load lsp)
(princ)
);defun

 

 

Best,

~DD

0 Likes
Message 7 of 9

Browning_Zed
Advocate
Advocate

The updated command still does not create "Scale" string if it is missing. In addition, if "Scale" string exists in Custom Info, then "error: bad argument type: VLA-OBJECT nil".

0 Likes
Message 8 of 9

CodeDing
Advisor
Advisor

@Browning_Zed ,

 

EDIT: Updated code.

I had updated the code about 5 minutes after posting. Will you try the code again to be 100% sure?

0 Likes
Message 9 of 9

Browning_Zed
Advocate
Advocate

Oh, sorry for the carelessness. It works great. Thank you so much!